osx - Change the sudo su shell

07
2014-07
  • Jules Mazur

    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.

  • Answers
  • terdon

    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.

  • ؘؘؘ

    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.


  • Related Question

    bash - Mac OSX shell will not change
  • Chris Edwards

    I'm trying to get ZSH as my default shell, I've installed using the installer from the oh-my-zsh project and it all installs fine, but I cannot change it to be the default shell:

    When I echo $SHELL it gives "/bin/zsh",

    • However ps -p $$ seems to suggest bash (although I have seen this suggest zsh even when it isn't)

    • I've also tried changing it in the Accounts → Advanced settings → Startup shell and in the Terminal preferences.

    • Using chsh -s /bin/zsh says "No changes made..." i.e. it also thinks zsh is in use.

    I can switch to zsh manually by typing zsh or /bin/zsh, just can't change it to default and that is rather annoying.

    Any ideas?


  • Related Answers
  • anatinus

    Worth pointing out that you have to actually add whatever new shell you've put in that isn't there on a vanilla box to the /etc/shells file (or somesuch similar)... You didn't post the whole text from your chsh attempt, and the snippet you did put down at least leaves that door open.

    Good Luck.

  • Chris Edwards

    Very strange guys, but I installed some software updates and after that restart (the computer had already been restarted multiple times) it just worked...

    Very odd but at least there was nothing actually going wrong...

    Thanks to everyone who responded!