How can I use ZSH when I use sudo commands but not as default root shell?
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.
Related videos on Youtube
aklmie
Updated on September 18, 2022Comments
-
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 runsudo
commands, and thatsudo
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
enterzsh
entertest1
enterI 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 usezsh
with its /root/.zshrc file, and prefix the command I want to run.Any hints ?
-
aklmie over 8 yearsThanks, that works. But, how can I write my alias command to prevent quoting my commands ?
-
fiatux over 8 yearsI don't understand your question.
-
aklmie over 8 yearsSee the last paragraph of my question. I want to write an alias (say
sudozsh
and use it in the same way ofsudo
).alias sudozsh='sudo zsh -ic'
doesn't work with commands such astail -f /var/log/syslog
. I would like also not having to backslash quotes in commands. -
fiatux over 8 yearsYou'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 -