0

According to man sudo sudo -i starts a login shell and should in my understanding read/source ~/.profile, i.e. /root/profile or [non-root user home/.profile and /etc/profile. Adding aliases, e.g. alias ll='ls -la' to both files doesn't make the alias available in the shell started by sudo -i. If I run bash in the shell stared by sudo -i aliases are available.

I already figured out to put aliases in /etc/profile.d/00-aliases.sh in https://askubuntu.com/questions/810730/how-to-share-bash-aliases-between-non-root-user-bash-and-shell-opened-by-sudo. Here I just want to know why the expected behaviour doesn't occur.

I reported this as a sudo bug on launchpad.net, but now I'm not sure whether this might be a tricky aspect of expected behaviour. The way man sudo describes -i is too trivial anyway.

2
  • sudo: -i: Run the shell specified by the target user's password database entry as a login shell. bash: A login shell is one whose first character of argument zero is a -, or one started with the --login option. sudo just sets the dash as the first character in the shell's name when running it.
    – ilkkachu
    Aug 11, 2016 at 16:55
  • 1
    the complete description for Bash's startup files is in the manual, here. It's a bit hard to find out what the exact problem with the aliases would be without seeing all of your /root/.*{profile,login} and /etc/profile*. And the bashrc files.
    – ilkkachu
    Aug 11, 2016 at 16:58

1 Answer 1

0

The files that are read depends on the target user's shell. So, for example, if you sudo -u user1 -i and user1 uses csh then it'll read ~user1/.login. For ksh users it would be ~user1/.profile.

For bash users (like root) the file read is ~/.bash_profile if it exists, with a fallback to ~/.profile

eg

% sudo head -1 /root/.bash_profile /root/.profile 
==> /root/.bash_profile <==
echo RUNNING BASH_PROFILE

==> /root/.profile <==
echo RUNNING PROFILE

% sudo -i
RUNNING BASH_PROFILE
root@server:~# rm /root/.bash_profile
root@server:~# exit
logout

% sudo -i
RUNNING PROFILE
root@server:~# 

So we can see the presence of .bash_profile prevented .profile from being run. This is normal behaviour for bash login shells, whether it's accessed via a ssh login or via sudo -i.

1
  • Now, I got it. /root/.profile is only sourced at the first sudo -i after reboot (or maybe display manager (re-)login), not at every following sudo -i - at least in gnome-terminal. If sudo -i is run once when /root/.bash_profile is present it is sourced (and /root/.profile skipped as expected) and if it /root/.bash_profile is removed every following invokation of sudo -i causes /root/.profile to be sourced. That not right, right? Aug 11, 2016 at 13:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .