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.

Whenever I run sudo su from my normal zsh (which uses the oh-my-zsh framework), I'm forced to use the old Bourne shell (sh) by default (obviously; this is standard behaviour on most *nix-like systems). If I run zsh from within sh after running sudo su, I get the Z shell, but without the improvements from oh-my-zsh.

Is there any way to change the shell sudo su launches to zsh? If so, is it possible to also have that instance of zsh launch using oh-my-zsh?

I'm using OS X 10.8.4.

share|improve this question
    
In addition to my answer below, given this is on OSX, this may classify as a duplicate of: How do I set my shell in Mac OS X? or How do I change a users default shell in OSX? For brevity though, you need to use the chsh tool to change user's shells in OSX, or alternatively, just launch it manually (sudo /bin/zsh). –  Breakthrough Jul 31 '13 at 18:34

3 Answers 3

up vote 3 down vote accepted

From the su manpage, there are two ways you can accomplish this.


The first method is to simply use the -s or --shell flag (assuming you are using a *NIX-based OS with a version of su that supports this argument), followed by the path to the shell of your choice. If the passed shell cannot be found, su reverts to the following method, and failing that, will attempt to invoke /bin/sh.

For example, you can force su to launch zsh (assuming it exists in /bin/zsh) as:

sudo su --shell /bin/zsh

The second method is to modify the default shell specified for the root user (be careful!). This can be done by editing the file /etc/passwd and changing the shell specified for the root user. To see what shell is specified by default, you can run the following command (assuming the superuser is root):

sudo grep root /etc/passwd 

The command should output something like root:x:0:0:root:/root:/bin/bash. You can simply change the /bin/bash (or whatever is set in your system) to point to zsh instead.

share|improve this answer
3  
As always, be careful about changing root's shell. You don't want to be in single user mode, and have a root shell that needs /usr when it's broken. At lease make sure your new shell has no more filesystem dependencies than the one you're replacing –  Rich Homolka Jul 31 '13 at 18:11
    
Running sudo su -s /bin/zsh (or using --shell) returns su: illegal option -- s. I'm on OS X 10.8.4; does OS X take a different command? –  Jules Mazur Jul 31 '13 at 18:15
    
@JulesMazur what is the output of cat /etc/shells? Technically only shells allowed in that file will be launched, although the su manpage says this shouldn't matter if su is called by root :S –  Breakthrough Jul 31 '13 at 18:16
    
cat /etc/shells returns /bin/zsh as an acceptable shell. –  Jules Mazur Jul 31 '13 at 18:18
2  
@JulesMazur please remember to always include your OS to avoid this kind of confusion. –  terdon Jul 31 '13 at 18:24

Another way to execute an interactive shell as the superuser is sudo -s, which uses $SHELL as the shell.

As the comments in the other answer mentioned, su -s /path/to/zsh doesn't work in OS X.

OS X doesn't support changing login shells in /etc/passwd either, but you can use dscl:

$ dscl . -read /Users/root UserShell
/bin/sh
$ sudo dscl . -change /Users/root UserShell /bin/sh /bin/zsh
$ dscl . -read /Users/root UserShell
/bin/zsh
$ sudo su
My-iMac# echo $0
zsh
My-iMac# exit
$ sudo dscl . -change /Users/root UserShell /bin/zsh /bin/sh
$ 

/bin/sh is not a Bourne shell anymore on most platforms. It is a POSIX-compliant version of bash in OS X and dash in Ubuntu.

share|improve this answer
    
This is what I would think the question wanted. It's definitely what worked for me; thank you so much! edit i see what he wanted now - I don't think this is what he wanted, so I can't fairly upvote it for this question, but I think you deserve a medal anyway. You had the answer to my question! –  Wyatt8740 Jan 31 at 22:31

Can be easily done with chpass:

$ sudo chpass -s /bin/zsh
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.