sudocommand

The sudo command

When it comes to administrative tasks, “the sudo“ command is what you are looking for. Instead of logging in as the root user directly and endangering the system by typing the wrong command, which may harm your system, the sudo command comes as an extra layer of protection and security by asking you for a password before executing a command.

Also, executing commands as the root user is not recommended due to security issues.

Why sudo and not root directly?

There are some particular reasons why sudo is more beneficial that login as root directly into the terminal. Here are selected few:

  1. Linux privileges philosophy & security

While you can maintain your session as a regular user, with sudo you run only the specific command that needs elevated privileges. But if you use root directly, even a typo or a bad command like rm -rf / (instead of rm -rf ./) can destroy your entire system.

2. Accountability and Auditing

While using sudo, every elevated command is logged with a timestamp, username, and the exact command run. System administrators can review /var/log/auth.log or /var/log/secure to see who did what.

But with root, multiple people sharing the root password makes it impossible to know who actually executed a command. Everyone is “root.”

   3. No shared passwords

The sudo command authenticates users with their own password and not a shared root password. This means that you can revoke a user’s access without changing the root password, grant granular permissions (user A can do administrator work like update the system, while user B can only restart the pc and is limited), and no one needs to know the root password at all.

4. Reduced attacks

Root login requires the root account to be enabled and exposed, making it a prime target for brute-force attacks. But sudo, on the other hand, requires physical presence or existing user credentials, while the root account’s password can be disabled (especially for SSH).

 

5. Time-limit privilege

The sudo command credentials are cached for 15 minutes (by default), and after that the user must re-authenticate. This will limit the window of opportunity for malicious processes or accidental misuse.

And now a story that explains in detail why sudo and not root is the way…

The Tale of Dave and the Vanishing /usr

It was 2:47 AM on a sleepy Sunday. Dave was exhausted after a long week, but a critical security patch needed installing on the company’s main web server.

Instead of using sudo for each command, Dave thought, “I’m going to be doing a lot of installations tonight. I’ll just switch to root to save time.”

He typed su – and entered the root password. Boom. He was now #. King of the machine.

Dave navigated to the /tmp folder where he had downloaded the patch. He listed the files with ls -la and saw a stray, empty test directory he’d created earlier called test dir (with a space in the name).

“I’ll clean that up,” Dave muttered sleepily.

He typed: rm -rf / test dir

What Dave meant to type was: rm -rf test dir (delete the directory called “test dir”).

But because he was exhausted, his finger hit the slash key before the space. The command he actually sent to the server was:

rm -rf / test dir

Do you see the fatal flaw? That space after the / told Linux: “Delete everything from the root directory (/), and also delete that ‘test dir’ folder.”

Because he was root, Linux didn’t ask, “Are you sure?” It didn’t say, “Permission denied.” It simply bowed to its king and began recursively deleting every single system binary, library, and configuration file from the hard drive—starting with /usr, /bin, and /etc.

The Snowball Effect:

Seconds later, the rm command itself was deleted from the drive while it was still running.

1 minute later, Dave tried to cancel it with CTRL+C, but the shell had already been deleted. The terminal froze.

2 minutes later, he tried to restart the server. But the bootloader couldn’t find the kernel. The server was dead. Completely unbootable.

The Aftermath:

The company’s e-commerce site went down at 3:00 AM. Dave had to drive 45 minutes to the data center in his pajamas with a physical USB recovery stick. It took the entire engineering team 6 hours on a Sunday to reinstall the OS, restore from backups, and reconfigure the web server. The company lost $40,000 in sales during the outage.

Now, let’s rewind and replay with sudo:

If Dave had simply typed sudo rm -rf / test dir as his normal user:

His shell would have instantly thrown a massive error:
sudo: rm: command not found (because rm is in /usr/bin, and the system wouldn’t let him delete / anyway without the –no-preserve-root flag).

Even if he somehow forced it, sudo logs exactly what he typed—/var/log/auth.log would show:
dave: COMMAND=/usr/bin/rm -rf / test dir
So his boss would at least know who made the mistake.

But most importantly, the system would have refused to delete critical system directories unless he explicitly added –no-preserve-root.

Because Dave wasn’t operating as root 24/7, the operating system itself would have acted as a safety net and caught his typo before it could destroy the business.

Moral of the story:
When Dave was #, the system treated him like a king and blindly obeyed his every typo. When Dave is $, the system treats him like a human who makes mistakes—and protects him (and his company) from himself.

Always use sudo. Your future self, your system and your company’s bank account will thank you.

Final thoughts

The sudo command gives you “surgical precision with a safety net” while root gives you “a sledgehammer with no undo button.” That is why many modern Linux distributions are disabling the root account by default.

But root privileges are needed only if you need to rescue your system or do recovery work on the operating system where no other users exist.

For further questions related to this topic, please use the Ask Vlad! page to contact me via email. I will reply in a short time.

All the best,

Vlad

 

Similar Posts

Leave a Reply

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