Why su to root instead of logging in as root?
I've heard often that it is better to su to root rather than log in directly as the root user (and of course people also say that it's even better to use sudo). I've never really understood why one is better than the other(s), insight?
Solution 1:
The inference is to only su
or sudo
when required.
Most everyday tasks don't require a root shell. So it is good practice to use an unprivileged shell as your default behaviour and then only elevate to root when you need to perform special tasks.
By doing so you are reducing scope for dangerous mistakes (bad scripting, misplaced wildcards, etc) and vulnerabilities from any applications that you use. Especially those which connect to the Internet - see the old adage "Don't IRC as root".
sudo
is often recommended because it allows you fine grain and audit the use of such privileges.
By observing these practices you are also in a position to disable remote root logins. This increases the bar of entry for any would-be attacker, as they would need to compromise both a regular user account that was a member of the "wheel" group and ideally only authorised by SSH public keys, then the root account itself.
Solution 2:
You should disable root access from remote so an attacker can't compromise root without first compromising a user then escalating to root. We enable root access at the console only.
Plus it creates accountability. :)
Solution 3:
The main reason is to create an audit trail. If you need to log into a system as a regular user and then su, it is possible to trace who is responsible for given actions.
Solution 4:
sudo also automatically logs every command to syslog and you can define the commands each user can user.
Solution 5:
Another good reason: When elevating to root via sudo a user authenticates with their own credentials, meaning that they don't necessarily have to be given the root password, making it easier to lock things down when someone leaves your organization.