How to run sudo

I find myself using sudo whenever I need to run an command as another user. I also encourage users of Linux systems to use it also, for several reasons. It allows commands executed to be tracked and if configured correctly it can effectively restrict elevated privileges. But without the proper flags, using sudo can be pain. This post will clear up some confusion and hopefully make sudo easier for you to use, and explain to others.

First, you will want to configure your sudoers file, LDAP, or wherever you sudo database resides. For this post, let’s assume you are using /etc/sudoers and your are in the wheel group.

By default running sudo, without any options, keeps the environment of the user running the sudo command, meaning things like your $PATH will not change so running

sudo cat /etc/shadow

will work because “cat” is in your $PATH. But running

cat systemctl status nginx.service

will likely fail because “systemctl” is not likely in the users path. To get the path of the user you would like to run the command as, use the following flag

sudo -i <command>

From reading the man page, it tells us, “…Run the shell specified by the target user’s password database entry as a login shell.  This means that login-specific resource files such as .profile or .login will be read by the shell.”

 

That way you get all the commands you would expect when running a command as someone else using sudo. Hope this helps!

Cool work, cool life