2

firstly, look at the snippet below:

$ ssh -i ~/.ssh/ubuntu.pem ubuntu@127.0.0.1
ubuntu@kingdom:~$ echo $PATH | tr ':' '\n' | grep sbin
/usr/local/sbin
/usr/sbin
/sbin
ubuntu@kingdom:~$ sudo su -l ubuntu
ubuntu@kingdom:~$ echo $PATH | tr ':' '\n' | grep sbin
ubuntu@kingdom:~$

i have modified /etc/profile, /etc/bash.bashrc, /home/ubuntu/.profile and /home/ubuntu/.bashrc (the ubuntu user has no other profile and bash configuration) to print the PATH in the beginning and the end of the file.

now i can see that the problem occurs on the second login:

$ ssh -i ~/.ssh/ubuntu.pem ubuntu@127.0.0.1
/etc/profile: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
/etc/bash.bashrc: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
/etc/bash.bashrc: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
/etc/profile: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
/home/ubuntu/.profile: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
/home/ubuntu/.bashrc: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
/home/ubuntu/.bashrc: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
/home/ubuntu/.profile: /home/ubuntu/bin:/home/ubuntu/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

ubuntu@kingdom:~$ sudo su -l ubuntu
/etc/profile: /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
/etc/bash.bashrc: /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
/etc/bash.bashrc: /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
/etc/profile: /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/snap/bin
/home/ubuntu/.profile: /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/snap/bin
/home/ubuntu/.bashrc: /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/snap/bin
/home/ubuntu/.bashrc: /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/snap/bin
/home/ubuntu/.profile: /home/ubuntu/bin:/home/ubuntu/.local/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/snap/bin

it felt like it has something to do with the sudo command. so i even tried to add !reset_env to /etc/sudoers, but it does not seem to solve the problem.

how can i use sudo su -l ubuntu while preserving the environment variables (without any extra arguments to sudo command) that ubuntu user had before the sudo su -l

clarification: i would like to get the same output for echo $PATH | tr ':' '\n' | grep sbin before and after sudo su -l ubuntu. right now, this is not the case:

$ ssh -i ~/.ssh/ubuntu.pem ubuntu@127.0.0.1
ubuntu@kingdom:~$ echo $PATH | tr ':' '\n' | grep sbin
/usr/local/sbin
/usr/sbin
/sbin
ubuntu@kingdom:~$ sudo su -l ubuntu
ubuntu@kingdom:~$ echo $PATH | tr ':' '\n' | grep sbin
ubuntu@kingdom:~$
Mr.
  • 415
  • 1
  • 9
  • 21
  • You should not be using sudo su, use sudo -i . IMO the best method is to properly configure root's path / environmental variables rather than import them. See https://help.ubuntu.com/community/RootSudo#Special_notes_on_sudo_and_shells – Panther Nov 29 '17 at 15:17
  • @Panther: thank you for the reply, yet i have not asked for bypass. rather, i am looking for a solution that suits my need. if you have any other idea, i would be happy to hear. – Mr. Nov 29 '17 at 15:26
  • See https://askubuntu.com/questions/161924/how-do-i-set-persistent-environment-variables-for-root , I personally set root's path in /root/.envrc although the second answer in the link I gave you is perhaps better. You have to change your behavior or configure root's environment / path, take your pick, between my comments I think I gave you at least 4 options. – Panther Nov 29 '17 at 15:59
  • @Panther: could you please provide an explanation what my issue has with root environment? – Mr. Nov 29 '17 at 16:39
  • can you please calrify your question. I can not tell from what you posted what you expect from the output of all those commands, and as far as I can tell your sudo su is working as expected. I can not tell what your question is other than how do you set environmental variables both as a user and as root. – Panther Nov 29 '17 at 16:50
  • @Panther: i made a clarification on my original post. – Mr. Nov 29 '17 at 18:06
  • So you want to set roots path and I have given you at least 4 options of how to do that. – Panther Nov 29 '17 at 19:05
  • 1
    @Panther: i don't know if that is what i want, why you keep repeating saying 'you want to set the roots path' without any explanation? do you know why the PATH is changing when using sudo su -l ubuntu and why is it different login than ssh login? – Mr. Nov 30 '17 at 07:30
  • 2
    @Panther MrRoth is not using sudo su to start a root shell, but to start a login shell for another user, ubuntu. For some reason, using sudo to su to this user changes the PATH. When they SSH to that user account on localhost they get a different PATH variable, the one they want. They don't want to change root's environment. But it might have something to do with sudo's secure_path. MrRoth, how did you get the output for the config files after the login commands? I don't understand that... – Zanna Nov 30 '17 at 08:35
  • @Zanna Your comment made more sense than all of the question. I can not understand the point of all that terminal output. We would need to look as the contents of all those config files . There is a discussion here https://askubuntu.com/questions/784186/whats-the-difference-between-login-as-user-and-changing-users-using-su-through . But , in addition, you are using sudo rather than su directly, so root and / or sudo configuration comes into play as well. You will have to look at the various differences between all those logins. – Panther Nov 30 '17 at 13:50
  • I really can not follow all the output nor do I understand what the OP wants exactly or the point of the ubutnu user to su to herself with sudo su -l ubuntu , so my interest is low. If you look at the mechanics of login and sudo and su you will likely sort it out. – Panther Nov 30 '17 at 13:52

0 Answers0