How can I use ZSH when I use sudo commands but not as default root shell?

10,376

I'm not a zsh user, but sudo zsh -c test1 is failing because the shell has not read your .zshrc file. Try this:

sudo zsh -ic test1

That should tell zsh that this is in interactive shell, so source the .zshrc file.

Share:
10,376

Related videos on Youtube

aklmie
Author by

aklmie

Updated on September 18, 2022

Comments

  • aklmie
    aklmie over 1 year

    So, I would like to keep bash as the default shell for root.

    But, I would prefer to use zsh, my shell, when I run sudo commands, and that sudo executes also the .zshrc in /root.

    For testing purposes, I have created an alias in my /root/.zshrc and ~/.zshrc :

    alias test1='echo "Test OK"'
    

    Now, when I run sudo su enter zsh enter test1 enter

    I got the expected result : Test OK

    However, all of these commands result in : test1: command not found

    sudo test1
    sudo su -c 'zsh -c test1'
    sudo -E zsh -c 'test1'
    sudo ZDOTDIR=/root/.zshrc test1
    sudo ZDOTDIR=/root/.zshrc zsh -c 'test1'
    sudo ZDOTDIR=/root/.zshrc zsh -c 'source /root/.zshrc && test1'
    

    I found this question on askubuntu, but I think this is more related to this one on Unix & Linux.

    However, none of these posts help me.

    What I want to achieve in final is to create a simple alias that could replace sudo when I want to use zsh with its /root/.zshrc file, and prefix the command I want to run.

    Any hints ?

  • aklmie
    aklmie over 8 years
    Thanks, that works. But, how can I write my alias command to prevent quoting my commands ?
  • fiatux
    fiatux over 8 years
    I don't understand your question.
  • aklmie
    aklmie over 8 years
    See the last paragraph of my question. I want to write an alias (say sudozsh and use it in the same way of sudo). alias sudozsh='sudo zsh -ic' doesn't work with commands such as tail -f /var/log/syslog. I would like also not having to backslash quotes in commands.
  • fiatux
    fiatux over 8 years
    You'll have to handle shell control operators like && specially, so use your alias, but quote the command to send: sudozsh 'tail -f', sudozsh 'cd /remote/dir && tar cf - .' | tar xf -