Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com Aug 6 '09 at 4:26

This question came from our site for professional and enthusiast programmers.

add comment

6 Answers

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.

share|improve this answer
    
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 '09 at 3:06
2  
With "echo $SHLVL" you can see the subshell level. –  Mark van Lent Aug 6 '09 at 6:58
add comment

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.

share|improve this answer
    
Why would changing users change my prompt? This seems like more of a workaround than a fix. –  David Aug 6 '09 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 '09 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 '09 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 '09 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 '09 at 4:18
show 1 more comment

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.

share|improve this answer
    
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 '09 at 3:46
    
Exactly. So to get out of any unnecessary subshell, just exit until there's only one bash left. –  earl Aug 6 '09 at 3:56
add comment

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.

share|improve this answer
    
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 '09 at 7:41
    
also, for 'sudo' people, "sudo -i" is immensely handy –  Kent Fredric Aug 6 '09 at 8:19
add comment

if this is a bash shell, try:

source ~/.bashrc

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

share|improve this answer
    
. ~/.bashrc does the same thing –  Swish Aug 6 '09 at 23:38
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

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