Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2016-10-18
6
Medium Priority
?
201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 79

Assisted Solution

by:arnold
arnold earned 1000 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 30

Accepted Solution

by:
serialband earned 1000 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
Docker-Compose to Simplify Multi-Container Builds

Our veteran DevOps Author takes you through how to build a multi-container environment, managed with a single utility in order to simplify your deployments.

 

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 79

Expert Comment

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

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

Docker-Compose to Simplify Multi-Container Builds

Our veteran DevOps Author takes you through how to build a multi-container environment, managed with a single utility in order to simplify your deployments.

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses

688 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