Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I just faced a really strange situation.

I was trying to edit file /usr/lib/thunderbird/thunderbird.sh, which has the following permissions:

$ ll /usr/lib/thunderbird/thunderbird.sh
-rwxr-xr-x 1 root root 2730 Aug 20 12:55 /usr/lib/thunderbird/thunderbird.sh*

and I found that if I issue

sudo vim /usr/lib/thunderbird/thunderbird.sh

When I switch to insert mode I get a warning message:

/usr/lib/thunderbird/thunderbird.sh [RO]
W10: Warning: Changing a readonly file
-- INSERT --

And I actually cannot write any changes.

However, if I issue:

user@hostname:~$ sudo su -
root@hostname:~# vim /usr/lib/thunderbird/thunderbird.sh

I can edit the file without problems.

The strange thing is that I have edited lots of files with similar permissions, such as the configuration files in /etc/, without issues, and that If I create a file inside the same directory I have no problems editing it:

user@hostname:~$ sudo touch /usr/lib/thunderbird/test.sh
user@hostname:~$ sudo chmod 755 /usr/lib/thunderbird/test.sh
user@hostname:~$ ll /usr/lib/thunderbird/test.sh /usr/lib/thunderbird/thunderbird.sh
-rwxr-xr-x 1 root root    0 Aug 20 13:03 /usr/lib/thunderbird/test.sh*
-rwxr-xr-x 1 root root 2730 Aug 20 12:55 /usr/lib/thunderbird/thunderbird.sh*
user@hostname:~$ sudo vim /usr/lib/thunderbird/test.sh

This is my sudoers file:

# Cmnd alias specification
Cmnd_Alias  APT = /usr/bin/apt-get update, /usr/bin/apt-get upgrade

# User privilege specification
root    ALL=(ALL:ALL) ALL 

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL 

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL 
%sudo   ALL=(ALL:ALL) NOPASSWD: APT

And my user is inside sudo group.

Any idea about why this happens?

share|improve this question
    
When you're in vim editing the problematic file, what do you get when you enter this command: :!echo "$UID:$EUID"? –  Joseph R. Aug 20 '13 at 12:56
    
@JosephR.: Just "0:0". Also, :!id returns: "uid=0(root) gid=0(root) groups=0(root)". Funny...isn't it? –  Carles Sala Aug 20 '13 at 13:15
    
Strange indeed. Your ls output seems to indicate there are no ACLs either. The only thing I can think about is SELinux. Do you happen to have it enabled? –  Joseph R. Aug 20 '13 at 13:26
1  
Does a different editor such as nano exhibit the same behavior? Also what if you sudoedit the file? –  slm Aug 20 '13 at 13:39
    
Thanks for the comments, but I just found the solution to the mistery. Regarding nano, I cannot try because I removed it, but from what I have concluded, I would have had no problem with it. –  Carles Sala Aug 20 '13 at 14:08

1 Answer 1

I just discovered the reason, and it was a collection of circumstances:

First of all, I don't know exactly why but sudo was not grabbing the HOME environment variable properly and used the one of the regular user, so it read the .vimrc from /home/user/.vimrc.

In order to see this, I issued:

user@hostname:~$ sudo bash
[sudo] password for user: 
root@hostname:/home/user# echo $HOME
/home/user

Second, I have folding persistence enabled in my user's vimrc file in order to store cursor position:

au BufWinLeave * mkview
au BufWinEnter * silent loadview

This makes that everytime a file is edited, a properties file is created inside $HOME/.vim/view folder. In my case, it looks like I tried to edit the file without sudo the first time, so the folding file was created as regular user's:

user@hostname:~$ ll .vim/view/ | grep thunderbird.sh
-rw-rw-r-- 1 csala csala  2650 Aug 20 15:56 =+usr=+lib=+thunderbird=+thunderbird.sh=

Since the root took /home/csala as $HOME, the same folding file was (wrongly) used when I issued sudo vim, and for some reason that I ignore, probable related to vim internals, if the folding file is not owned by the editing user, the edited file is opened in ReadOnly mode.

So, I realized that if I removed the file /home/user/.vim/view//home/csala/.vim/view/=+usr=+lib=+thunderbird=+thunderbird.sh= and then tried to edit using sudo vim, I had no problems at all.

So, at the end of the story, in order to fix this situation I just edited /etc/sudoers and added this line:

Defaults always_set_home

Now everything works as expected and I can use sudo reliably again.

share|improve this answer
1  
+1, nice detective work! –  slm Aug 20 '13 at 14:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.