How to change users with “su” and have your path work correctly.

It is best to do all your day to day tasks as a user with the minimum privileges to accomplish those tasks. This prevents you from accidentally making drastic changes to your install or inadvertantly running a trojan horse that listens on a privileged port. But what happens when you want to su to root and none of your root commands are in your path. All the sudden you have to run locate or find just to run a program. Whats worse is that the user you were previously using has their path installed so if that account is compromised they could add a trojan horse to their path in the hopes that root would eventually run it.

For the solution, read on…

If you use

su -

You are telling the su command to give you a “login” shell which basically acts like you just logged in. It will create your path correctly, change to your home directory, and generally act like you weren’t logged in before. This is super convenient for all those utilities that live in /sbin and /usr/sbin that you probably don’t keep in the path of the user you use for your actual work.

Leave a Reply

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