Score:1

root user has no permission to edit environment file

us flag

I recently got a new problem in my Ubuntu system. I have to change the contents of the /etc/environment file but it is not possible, even the root user has no permission to do that.

As you can see root has write permission here:

>> ls -lia /etc/environment 
262296 -rw-r--r-- 1 root root 241 Oct  3 17:59 /etc/environment

It will not let me change the file.

>> sudo su
>> echo sample=1 >> /etc/environment
   operation not permitted: /etc/environment

There is no way to change the file even with an editor tool like vim and nano.

enter image description here

The outputs:

>> findmnt -T /etc/environment
TARGET SOURCE         FSTYPE OPTIONS
/      /dev/nvme0n1p5 ext4   rw,relatime,errors=remount-ro


>> lsattr /etc/environment
----i---------e------- /etc/environment
PonJar avatar
in flag
There isn’t normally a root user setup in Ubuntu like there is in many other distributions. Consequently I’m not sure what `sudo su` will achieve and I don’t have a quick way to check at the moment. The output you have provided doesn’t show us which user you are acting as when you issue the commands. I suspect you may not have become root when you think you have. Have you tried `sudo nano /etc/environment`
k90mirzaei avatar
us flag
ya I do it, and `sudo su` switches current session user to the root user
hr flag
`EPERM 1 Operation not permitted` is different from `EACCES 13 Permission denied` and usually indicates something like a read-only volume rather than a simple unix permission bit issue
k90mirzaei avatar
us flag
So how I can change my env variable inside `/etc/environment`
k90mirzaei avatar
us flag
from my window experience, it would happen when the file is used or opened somewhere is this the same @steeldriver
mattb avatar
in flag
Does it work with `sudo su -`? (note the `-` at the end) or maybe by just doing `sudoedit /path/to/file`?
k90mirzaei avatar
us flag
@mattb I add an image, as you see it not editable as all,
hr flag
Please add the output of `findmnt -T /etc/environment`
cn flag
Also, please add the output of `lsattr /etc/environment`. The file _may_ have the "immutable" flag set.
k90mirzaei avatar
us flag
@steeldriver Does it mean that mounting prohibits editing the file? if yes how to resolve it?
cn flag
@k90mirzaei No, you need to mount a file system to get to the files on that file system in the first place, for reading, writing or whatever ;) You may want to read up on the differences between the organization of file systems in Windows and in Unix-like operating systems like Linux.
Score:4
cn flag

According to the output of lsattr, the file in question has the "immutable" attribute set (note the i in the list of attribute flags in the beginning of the output):

A file with the 'i' attribute cannot be modified: it cannot be deleted or renamed, no link can be created to this file and no data can be written to the file. Only the superuser or a process possessing the CAP_LINUX_IMMUTABLE capability can set or clear this attribute.

(from the man page of chattr).

The "immutable" attribute is one of a list of extended file attributes that are available on ext file systems (ext2, ext3, ext4).

You can set or unset those attributes with chattr like this

chattr +s /some/file # sets the 's' attribute
chattr -s /some/file # removes the 's' attribute

With some attributes, among them the i or "immutable" attribute, you need root privileges to set or remove them, so you'll do something like

sudo chattr +i /some/file
sudo chattr -i /some/file

From my experience, you don't run into those extended attributes very often. So I would recommend to look into why /etc/environment got the i attribute set in the first place. It doesn't have on my system ;)

k90mirzaei avatar
us flag
Wow, It works thank you so much
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.