Link to home
Start Free TrialLog in
Avatar of Jason Yu
Jason YuFlag for United States of America

asked on

what is the difference between "sudo su" and "su - root"

I have a test linux instance and I found a strange thing. I have defined the JAVA_HOME environment variable inside the .bash_profile for root user. But when I login as my personal user and "sudo su" to root, the JAVA_HOME still points to the old java home, but if I run "su - root" command and input the root password, the .bash_profile got executed and I have the modified JAVA_HOME.  Is this the difference between login shell and non-login shell? How could I make the modified JAVA_HOME comes out as default.

Thanks.


[jyu@nginxserver ~]$ su - root
Password:
Last login: Tue Oct 18 22:08:41 UTC 2016 on pts/0
[root@nginxserver ~]# echo $JAVA_HOME
/usr/lib/jvm/jdk1.8.0_101/bin
[root@nginxserver ~]# exit
logout
[jyu@nginxserver ~]$ sudo su
[root@nginxserver jyu]# echo $JAVA_HOME
/usr/lib/jvm/jre
[root@nginxserver jyu]# exit
exit
[jyu@nginxserver ~]$ su - root
Password:
Last login: Tue Oct 18 22:09:06 UTC 2016 on pts/0
[root@nginxserver ~]# echo $JAVA_HOME
/usr/lib/jvm/jdk1.8.0_101/bin
[root@nginxserver ~]# exit
logout
[jyu@nginxserver ~]$ sudo su
[root@nginxserver jyu]# echo $JAVA_HOME
/usr/lib/jvm/jre
[root@nginxserver jyu]#
SOLUTION
Avatar of arnold
arnold
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jason Yu

ASKER

[root@nginxserver ~]# echo $JAVA_HOME
/usr/lib/jvm/jdk1.8.0_101
[root@nginxserver ~]# exit
logout
[jyu@nginxserver ~]$ sudo -s
[root@nginxserver jyu]# echo $JAVA_HOME
/usr/lib/jvm/jre

[root@nginxserver jyu]# exit
exit

[jyu@nginxserver ~]$ sudo -i
[root@nginxserver ~]# echo $JAVA_HOME
/usr/lib/jvm/jdk1.8.0_101
[root@nginxserver ~]#
looks like sudo -i is equal to "su - root"

If I want to set up JAVA_HOME whenever I login with sudo -i or "sudo su", which profile should I add it to?

I added JAVA_HOME in .bash_profile, but everytime I log in with "sudo su" , it doesn't execute.

If I run "sudo - su" and input password, it was executed.

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH

JAVA_HOME=/usr/lib/jvm/jdk1.8.0_101

export JAVA_HOME
~
sudo -I you would adjust the root's .profile .bashrc etc.
That just means your environment is set differently for the system versus the user.

       -i [command]
                   The -i (simulate initial login) option runs the shell specified by the password database entry of the target
                   user as a login shell.  This means that login-specific resource files such as .profile or .login will be read
                   by the shell.  If a command is specified, it is passed to the shell for execution via the shell's -c option.
                   If no command is specified, an interactive shell is executed.  sudo attempts to change to that user's home
                   directory before running the shell.  It also initializes the environment to a minimal set of variables,
                   similar to what is present when a user logs in.  The Command environment section below documents in detail
                   how the -i option affects the environment in which a command is run.

Open in new window


       -s [command]
                   The -s (shell) option runs the shell specified by the SHELL environment variable if it is set or the shell as
                   specified in the password database.  If a command is specified, it is passed to the shell for execution via
                   the shell's -c option.  If no command is specified, an interactive shell is executed.

Open in new window