HOWTO: Become Another User Without Logging Off.
The magic su command is normally only used to change into the superuser. However, it can be used to change into any user.
In a terminal window, type:
su username
Where username is the name of the user you want to become.
If you’re logged in as the superuser, then you will be able to become any user you like without being prompted for a password. If you’re not the superuser, you will be prompted for the password of the user you’re attempting to become.
Related Stories
POSTED IN: How To
10 opinions for HOWTO: Become Another User Without Logging Off.
Phillip Molly Malone
Nov 10, 2005 at 12:02 am
You might be interested to research the use of a “-” in the command.
(Please take the following with a lump of Salt)
su mollyfud
is different to
su - mollyfud
and both are useful.
From memory, and you probably want to check this out, without the “-” you are just changing the User you are but leaving your envirnment the same as it is (i.e. environment variables like PATH will be the same before and after the su command).
If you use the “-”, it is the same as logging in as that user and you get there environment.
This is handy to know especally if you su to root but can’t run some admin commands you su’d for in the first place. Its probably because the user you su’d from doesn’t have the admin directorys in there path.
HTH
Molly
Http://www.mollyzine.com
PS. I take no responsiblity if this info turns out as wrong.
Jon
Nov 10, 2005 at 6:59 am
Thanks Molly.
Another personal favourite of mine is su -c command. This will make you the root user (after prompting for the root password) just for that single command and then you will be returned to your normal mundane self after execution.
It’s a little safer than just su’ing because you may forget to exit back to your normal self and continue operating as the root user.
gilberto
Dec 6, 2005 at 1:17 pm
Hello friends, i’m need help to enter like root with a only line command like this “su username password=xxx” is it posible?
Thanks a lot
Jon
Dec 6, 2005 at 1:20 pm
I’m not 100% sure what you mean gilberto. If you mean that you are already root and you want to become another user, just type
su username
Since you are already root you don’t need the user’s password.
If you mean how do you become root with only one line, then you’ll have to wait until I get home to my Linux box to test that out.
geoff
Dec 6, 2005 at 5:27 pm
The only way I can think is perhaps:
echo "password" | sugeoff
Dec 6, 2005 at 5:28 pm
Or, to become a different non-root user:
echo "password" | su usernameJon
Dec 6, 2005 at 10:25 pm
I get a ’su must be run from a terminal’ message when I try either of those options.
This is a good one…anyone?
geoff
Dec 10, 2005 at 10:24 pm
Are you not typing them at a terminal?
Jon
Dec 11, 2005 at 1:28 pm
Yeah. Sure am…stupid error message…
Jason
Dec 21, 2006 at 8:53 am
It means you have to type the password interactively (not piped in). This is a safety measure, so that you don’t (for example) type your password into a script, where it can easily be read by others.
If you want to run a command as root within a script, you should look at ’sudo’, where you can configure whether a password is required, and which commands should be allowed to be run as root.
Have an opinion? Leave a comment: