sudo command examples

sudo command examples

sudo, sudoedit — execute a command as another user

Note: the following examples assume a properly configured security policy.

To get a file listing of an unreadable directory:

$ sudo ls /usr/local/protected

To list the home directory of user yaz on a machine where the file system holding ~yaz is not exported as root:

$ sudo -u yaz ls ~yaz

To edit the index.html file as user www:

$ sudo -u www vi ~www/htdocs/index.html

To view system logs only accessible to root and users in the adm group:

$ sudo -g adm view /var/log/syslog

To run aneditor as jim with a different primary group:

$ sudo -u jim -g audio vi ~jim/sound.txt

To shut down a machine:

$ sudo shutdown -r +15 "quick reboot"

To make a usage listing of the directories in the /home partition. Note that this runs the commands in a sub-shell to make the cd and file redirection work.

$ sudo sh -c "cd /home ; du -s * | sort -rn > USAGE"


Leave a Reply

Your email address will not be published. Required fields are marked *