Solved

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

Posted on 2016-10-18
6
116 Views
Last Modified: 2016-11-02
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]#
0
Comment
Question by:Jason Yu
  • 2
  • 2
  • 2
6 Comments
 
LVL 77

Assisted Solution

by:arnold
arnold earned 250 total points
ID: 41849240
Su - or su - root you have to know root's password to elevate your rights.
sudo su and sudo su - in modern system is redundant and has been replaced with sudo -s sudo -I
Sudo is to elevate your rights, the -s -I for sudo is somewhat similar to the - with su which will deal with whether the environment variable of your account or root's will be processed before granting you access to the elevated shell.

Try the same using sudo -s and then with sudo -i
0
 
LVL 28

Accepted Solution

by:
serialband earned 250 total points
ID: 41849378
In your above example, sudo su is different from su - root.  With sudo su, you're not loading the root user's login environment.  It's actually the same as su root.  

If you had run sudo su -, that would be the same as su - root.
Also su - root is actually redundant for su -, so the exact syntax would be sudo su - root, but that's even more redundant than modern sudo systems.

sudo -s or -i loads the environment profiles just as su - or su - root, so they are not actually similar to sudo su or just plain su.
0
 

Author Comment

by:Jason Yu
ID: 41854728
[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 ~]#
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

Author Comment

by:Jason Yu
ID: 41854732
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
~
0
 
LVL 77

Expert Comment

by:arnold
ID: 41854776
sudo -I you would adjust the root's .profile .bashrc etc.
0
 
LVL 28

Expert Comment

by:serialband
ID: 41854783
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

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
The viewer will learn how to implement Singleton Design Pattern in Java.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

806 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question