Solved

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

Posted on 2016-10-18
6
66 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 76

Assisted Solution

by:arnold
arnold earned 250 total points
Comment Utility
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 27

Accepted Solution

by:
serialband earned 250 total points
Comment Utility
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
Comment Utility
[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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:Jason Yu
Comment Utility
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 76

Expert Comment

by:arnold
Comment Utility
sudo -I you would adjust the root's .profile .bashrc etc.
0
 
LVL 27

Expert Comment

by:serialband
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now