0
0
Linux CLIscripting~5 mins

sudo for elevated privileges in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you need to do tasks on your computer that require special permission. The sudo command lets you run commands as a superuser or administrator to do these tasks safely.
When you want to install new software on your Linux system.
When you need to change system files that normal users cannot edit.
When you want to restart system services like networking or web servers.
When you want to add or remove users from the system.
When you want to update your system packages.
Commands
This command updates the list of available software packages. It needs elevated privileges because it accesses system files.
Terminal
sudo apt update
Expected OutputExpected
Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease Get:2 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB] Get:3 http://archive.ubuntu.com/ubuntu focal-backports InRelease [101 kB] Fetched 215 kB in 1s (300 kB/s) Reading package lists... Done
This command installs the curl tool. The -y flag automatically confirms the installation prompt.
Terminal
sudo apt install curl -y
Expected OutputExpected
Reading package lists... Done Building dependency tree Reading state information... Done The following NEW packages will be installed: curl 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. Need to get 159 kB of archives. After this operation, 455 kB of additional disk space will be used. Get:1 http://archive.ubuntu.com/ubuntu focal/main amd64 curl amd64 7.68.0-1ubuntu2.7 [159 kB] Fetched 159 kB in 1s (200 kB/s) Selecting previously unselected package curl. (Reading database ... 123456 files and directories currently installed.) Preparing to unpack .../curl_7.68.0-1ubuntu2.7_amd64.deb ... Unpacking curl (7.68.0-1ubuntu2.7) ... Setting up curl (7.68.0-1ubuntu2.7) ... Processing triggers for man-db (2.9.1-1) ...
-y - Automatically answer yes to prompts
This command restarts the networking service to apply any changes. It requires elevated privileges to control system services.
Terminal
sudo systemctl restart networking
Expected OutputExpected
No output (command runs silently)
This command adds a new user named 'newuser' to the system. Only administrators can add users.
Terminal
sudo useradd newuser
Expected OutputExpected
No output (command runs silently)
This command sets a password for the new user. It requires elevated privileges to change user passwords.
Terminal
sudo passwd newuser
Expected OutputExpected
Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully
Key Concept

If you remember nothing else from this pattern, remember: sudo lets you run commands with administrator rights safely when needed.

Common Mistakes
Running commands that need sudo without sudo.
The command will fail with a permission denied error because normal users cannot perform that action.
Add sudo before the command to run it with elevated privileges.
Using sudo for every command even when not needed.
It can be risky and may cause accidental system changes or security issues.
Use sudo only when the command requires administrator rights.
Typing the wrong password multiple times when prompted by sudo.
Sudo will deny access after several failed attempts and you must wait before trying again.
Type your password carefully and ensure your keyboard layout is correct.
Summary
Use sudo before commands that need administrator rights to run successfully.
Common tasks with sudo include installing software, managing users, and restarting services.
Always use sudo carefully to avoid unintended system changes.