Is there any way to run chsh -s /bin/zsh without being root and without being asked for the password? I want to put it into a non-interactive script that isn't called by root.
3 Answers
If you don't have the permission to change your login shell, you can tell bash (I assume bash is your login shell) to replace itself with zsh. In your ~/.bash_login, add this line:
exec /bin/zsh --login
-
3Whoa, careful! With some distributions or desktop environments (I don't remember specific cases outright), if you do this, you'll find that you can't log in under X (because
/etc/?dm/Xsessioncallsbash --login).Gilles 'SO- stop being evil'– Gilles 'SO- stop being evil'2011-08-04 19:39:20 +00:00Commented Aug 4, 2011 at 19:39 -
Gilles, what would you suggest instead?Matt Alexander– Matt Alexander2011-08-04 19:51:55 +00:00Commented Aug 4, 2011 at 19:51
-
1zsh.sourceforge.net/FAQ/zshfaq01.html#l8kirill_igum– kirill_igum2013-02-19 22:24:53 +00:00Commented Feb 19, 2013 at 22:24
-
This seems to break all non interactive commands(scp, rsync).Wei Qiu– Wei Qiu2018-10-12 09:29:40 +00:00Commented Oct 12, 2018 at 9:29
If your version of chsh requires a password (mine does), you will need to setup sudo with special permissions. You can configure it to allow access to any program without prompting for a password like this:
user ALL=(ALL) NOPASSWD:/usr/bin/chsh
Where "user" is your username (or prefix with % for a group name). Make sure specify the full path to your chsh. This will not allow the user to run anything ELSE as root (including from a script) but it will allow that one binary to be run as root without a password, thus bypassing the user-password prompt.
Depending on your exact needs, if you want to do this because you’re running a long script and don’t want it to hang in the middle to that question, you can ask for the password upfront and then execute chsh with sudo privileges, reading the password from stdin.
read -p 'What is your password?' sudo_password
# rest of the script here
sudo -S chsh -s '/bin/zsh' "${USER}" <<< "${sudo_password}"
-S causes sudo to read the password from stdin. Be aware of possible security risks with this approach (I haven’t been able to reproduce that, though, and again, depending on your needs this might not be an issue).
sudoersrule)? It's rare to need to automate something over multiple accounts without being root.