Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

When I tried to "sudo su" instead of "sudo su -" after having been logged in as root and su-ing to another user, it tries to sudo me as the new user, but via root...

When I type env, it shows still username=root in the environment. Is sudo not looking at the currently logged in user, but at the enviroment parameters?

[oracle@tst-01]$ sudo su grid
Sorry, user oracle is not allowed to execute '/bin/su grid' as root on tst-01.testdomain.com.
[oracle@tst-01]$ env
HOSTNAME=tst-01.testdomain.com
TERM=xterm
SHELL=/bin/bash
HISTSIZE=1000
QTDIR=/usr/lib64/qt-3.3
USER=oracle
SUDO_USER=ujjain
USERNAME=root
MAIL=/var/spool/mail/ujjain
PATH=/usr/lib/oracle/10.2.0.4/client64/bin:/usr/lib/oracle/10.2.0.4/client64/bin:/sbin:/bin:/usr/sbin:/usr/bin
PWD=/home/ujjain
LANG=en_US.utf8
HOME=/opt/apps/oracle
SUDO_COMMAND=/bin/su
SHLVL=2
LOGNAME=oracle
CVS_RSH=ssh
LESSOPEN=|/usr/bin/lesspipe.sh %s
SUDO_GID=3000
ORACLE_HOME=/usr/lib/oracle/10.2.0.4/client64
G_BROKEN_FILENAMES=1
_=/bin/env
[oracle@tst-01 ujjain]$ sudo su - grid
[grid@tst-01 ~]$ 

It seems that sudo su newuser should also work if sudo would only be looking if the currently logged in user has sudo rights to sudo to the new user. What is sudo su doing and why is it important for non-root users to use sudo su - newuser with the hyphen?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

In su - username, the hyphen means that the user's environment is replicated, i.e. the default shell and working directory are read from /etc/passwd, and any login config files for the user (e.g. ~/.profile) are sourced. In short, you pretty much get the same environment as if you logged in normally. (Though the new user may not own the terminal, causing programs like screen to fail.)

Not using the hyphen, will cause you to more or less keep the environment of the user that invoked su, including leaving you in the same working directory, where you may not have permissions.

su won't ask for a password if the root user invoked it, so if you first have used sudo (or su) to become root, you won't need a password to become any other user.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.