Solved

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

Posted on 2016-10-18
6
176 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 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 30

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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 

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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

621 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