4

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?

6 Answers 6

12

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.

2
  • 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?
    – David
    Aug 6, 2009 at 3:06
  • 2
    With "echo $SHLVL" you can see the subshell level. Aug 6, 2009 at 6:58
6

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.

6
  • Why would changing users change my prompt? This seems like more of a workaround than a fix.
    – David
    Aug 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.
    – Luke
    Aug 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.
    – Fhoxh
    Aug 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.
    – David
    Aug 6, 2009 at 3:43
  • 1
    While 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 exit the su-shell, not start another one.
    – earl
    Aug 6, 2009 at 4:18
5

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 installed, pstree -h will show you a graphical representation of what's going on.

2
  • 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?
    – David
    Aug 6, 2009 at 3:46
  • Exactly. So to get out of any unnecessary subshell, just exit until there's only one bash left.
    – earl
    Aug 6, 2009 at 3:56
4

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:

  • /etc/profile
  • ~/.bash_profile
  • ~/.bash_login
  • ~/.profile
  • ~/.bashrc

The 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.

The su command has a -l (or --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.

2
  • 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.
    – alok
    Aug 6, 2009 at 7:41
  • also, for 'sudo' people, "sudo -i" is immensely handy Aug 6, 2009 at 8:19
1

if this is a bash shell, try:

source ~/.bashrc

if that doesnt work run echo $PS1 and see what has changed.

1
  • . ~/.bashrc does the same thing Aug 6, 2009 at 23:38
1

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,

su -

will start a new login session

You must log in to answer this question.