Sudoers Ubuntu Commands

I needed to find an easy, and fairly secure, way to allow a user to login on a website and display their current user’s disk usage. Generally I just navigate to the user’s home directory and run
du -hc
But in this case I needed it to work from the web.

By default, Ubuntu runs apache as www-data. Enter sudoers. My editor’s file is nano.
visudo
Add the following to the bottom of the file:
www-data ALL=NOPASSWD: /usr/bin/du, /usr/bin/ls, other/command
Save and quit

Now your www-data user can run those commands by running sudo command. And it will not allow the user to sudo any other commands.

My PHP page is as follows:
echo exec("sudo du -hc /mnt/ | grep " . $_SESSION["valid_user"] . "| awk '{print $1}'") . " Disk Space Used
";

Obviously the SESSION variable comes from a login page, but it’s a pretty good start for me.

Leave a Reply

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