On my linux server I signed in as the root user to access some files: su root... After checking the files I went to sign back in as my main user su username. In my terminal window it now says server ~: instead of username@server ~: to the left of the command line. Is there any way I can get this back to normal?
Do a few
exit commands. Your
su root started a subshell where you're root, and your
su user started another. Typing
exit will end those subshells and bring you back where you started -- one level deep.
It just exited me out of my remote connection... but I do think that I'm in a subshell. Is there any command for me to view all the subshells I have running?– DavidAug 6, 2009 at 3:06
2With "echo $SHLVL" you can see the subshell level. Aug 6, 2009 at 6:58
You just need to change the prompt. You can use the following command:
export PS1="\u@\h \w: "
I personally prefer the following as my prompt, but that's just me:
export PS1="[\u@\h \w]\$ "
You can prevent your prompt from changing by adding the statement to the relevant bashrc files. See also Bash Shell PS1: 10 Examples to Make Your Linux Prompt like Angelina Jolie for more info about PS1.
Why would changing users change my prompt? This seems like more of a workaround than a fix.– DavidAug 6, 2009 at 3:08
Because the prompt will indicate as WHAT user you're currently operating, e.g. the prompt would look like root@myserver: when you're operating as the root user or someuser@myserver: when you're operating under the credentials of someuser.– LukeAug 6, 2009 at 3:21
David: Prompts are not global; They are user-specific. Therefore, changing users can change your prompt. In particular, the root user oftentimes has a very bare-bones generic prompt. What Michael suggested here is exactly the correct way to address your problem; It's not at all a workaround.– FhoxhAug 6, 2009 at 3:23
Since they are user-specific, why would changing to the root user change the prompt for the user "david." I understand that when I am in the sub-shell for the root user that I would have a different prompt, but as soon as I exit out of that subshell I don't understand why it would change the prompt for the user david. Didn't you just say that they are independent of each other? I'm definitely a noob at linux, and I would appreciate it if someone could explain the error in my logic here.– DavidAug 6, 2009 at 3:43
1While this answer is correct about how to change bash prompts, it misses the point that to "switch back" to your own user after a
su, you typically simply want to
su-shell, not start another one.– earlAug 6, 2009 at 4:18
su switches users by spawning a new (sub)shell. So when you logged in, you started a shell. With the first
su to root, you started a subshell as root. The next
su to your user started yet another subshell -- note that at this moment you have three shells running. So instead of
su-ing to your user from the rootshell,
exit-ing back to your login shell will solve your problems.
If you have
pstree -h will show you a graphical representation of what's going on.
That reads: -sshd---sshd---sshd---bash---pstree. When I enter the root sub-shell I get: -sshd---sshd---sshd---bash---su---bash---pstree. Since there is only one bash in the first reading would I be correct in assuming that I'm not in a sub-shell?– DavidAug 6, 2009 at 3:46
Exactly. So to get out of any unnecessary subshell, just
exituntil there's only one
bashleft.– earlAug 6, 2009 at 3:56
Your prompt appears to be normally set by one of your shell startup files that is not being run when you
su from root to your user. If you're using bash,
man bash in the INVOCATION section might be enlightening. There are several startup files that bash might read, including but not limited to:
PS1 environment variable is just an environment variable, and may or may not get set depending on which combination of the above files is run.
su command has a
--login) switch which may be abbreviated to just
- that makes the new shell a login shell. This is probably what you want:
su - david
I always use
su with this option.
That's correct. A plain su would not invoke the startup files whereas su - would. I didn't know su - is short for su -l ! Thanks Greg for the info.– alokAug 6, 2009 at 7:41
also, for 'sudo' people, "sudo -i" is immensely handy Aug 6, 2009 at 8:19
if this is a bash shell, try:
if that doesnt work run
echo $PS1 and see what has changed.
su by itself does not create a new session of the shell. that's why it doesn't set up the shell as it normally does. however,
will start a new login session