sudo
Change to root user
_$: sudo su -
_$: sudo -i # With root environment ($PWD, $HOME)
_$: sudo -s # With current user environment ($PWD, $HOME)
user@work: su - # Impossible if root account is passwordless
Change to another (non-root) user
_$: sudo su - <user>
Add a user to the groups that can sudo
_$: usermod <user> -aG adm
_$: usermod <user> -aG sudo
Passwordless sudo
In the sudoers file, write NOPASSWD
:
%admingroup ALL=(ALL) NOPASSWD:ALL
Run commands as another user
_$: sudo -u <user> command
_$: sudo -u postgres pwd # Compare the output
_$: sudo -u postgres -i pwd # Compare the output
_$: sudo -u postgres -i -- sh -c "whoami ; pwd"
Example: sudoers
/etc/sudoers:
-------------
...
# Include directives
#include /etc/sudoers.d/auser # Yes, use a hash immediately followed by 'include'
/etc/sudoers.d/auser:
---------------------
# Allow auser to manage jenkins
auser ALL=(root:root) /etc/init.d/jenkins start
auser ALL=(root:root) /etc/init.d/jenkins stop
auser ALL=(root:root) /etc/init.d/jenkins status
auser ALL=(root:root) /usr/bin/service jenkins start
auser ALL=(root:root) /usr/bin/service jenkins stop
auser ALL=(root:root) /usr/bin/service jenkins status
Check permission bits on a file as another user
_$: sudo -u www-data test cat /path/to/file
_$: sudo -u www-data test -r /path/to/file && echo "OK"
_$: sudo -u www-data test -w /path/to/file && echo "OK"
_$: sudo -u www-data test -x /path/to/file && echo "OK"
_$: sudo -u www-data test -x /path/to/dir && echo "OK"