In Ubuntu Linux and other Unix-like operating systems, sudo
stands for “superuser do” or “substitute user do.” It is a command that allows authorized users to execute specific commands with elevated privileges, often as the root user. The primary purpose of sudo
is to provide a controlled and secure way for users to perform administrative tasks without having to log in as the root user, which could be risky due to the extensive control over the system that the root user possesses.
Using sudo
, regular users can execute commands that require administrative privileges by temporarily switching to the root user or another user with appropriate permissions. This enhances security by limiting the exposure of the root account and providing a granular way to grant specific administrative access to individual users.
To use sudo
, you typically prepend it to the command you want to execute with elevated privileges. For example:
sudo apt update
In this command, sudo
is used to run the apt update
command with administrative rights, allowing the system to update its package information.
To be able to use sudo
, a user must be granted the necessary permissions through configuration files like /etc/sudoers
. Usually, the first user created during the Ubuntu installation is granted sudo privileges by default, and additional users can be added to the sudo group or granted sudo access individually by the system administrator.
How to create a sudo user
Step 1: First, connect to Ubuntu
server via ssh
ssh root@192.168.1.234
Once you are logged in to your remote Ubuntu server, follow the below guide:
Step 2: Create new user
In Ubuntu Linux, the adduser
command is used to add new user accounts to the system. It is a user management utility that simplifies the process of creating user accounts, setting their attributes, and configuring their initial settings. The adduser
command is often used with administrative privileges, typically via the sudo
command, since creating user accounts requires administrative access.
adduser rahil
Output:
root@ubuntu:~# adduser rahil
Adding user `rahil' ...
Adding new group `rahil' (1000) ...
Adding new user `rahil' (1000) with group `rahil' ...
Creating home directory `/home/rahil' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for rahil
Enter the new value, or press ENTER for the default
Full Name []: Rahil
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n] Y
Step 3: Add newly created user to the sudo
group
usermod -aG sudo rahil
The usermod -aG sudo rahil
command is used to modify the user attributes in Linux. In this specific case, it adds the user named “rahil” to the “sudo” group, granting them administrative privileges. The -aG
flag indicates adding the user to the specified group, while the “sudo” group provides permission to execute commands with elevated privileges using the sudo
command. This enables the user “rahil” to perform administrative tasks on the system while maintaining a higher level of security by allowing limited access to privileged operations.
Step 4: Switch user from root
to rahil
su - rahil
The su - rahil
command is used in Linux to switch the current user’s session to the user account named “rahil.” The hyphen (-
) after su
indicates that a complete login environment for “rahil” should be loaded, simulating a full user login. This can be helpful when you need to access the user’s environment, including their shell preferences and environment variables. Once executed, the command prompts for “rahil”‘s password and, upon successful authentication, grants access to their user environment and permissions, allowing you to work within their context for various tasks.
Step 5: Use sudo
command to run commands with privileges
sudo ls -la /root/
Output:
rahil@ubuntu:~$ sudo ls -la /root/
total 32
drwx------ 5 root root 4096 Aug 30 19:42 .
drwxr-xr-x 19 root root 4096 Aug 30 19:38 ..
-rw-r--r-- 1 root root 3106 Oct 15 2021 .bashrc
drwx------ 2 root root 4096 Aug 30 19:42 .cache
-rw-r--r-- 1 root root 0 Aug 30 19:38 .cloud-locale-test.skip
-rw-r--r-- 1 root root 161 Jul 9 2019 .profile
drwx------ 2 root root 4096 Aug 30 19:38 .ssh
-rw-r--r-- 1 root root 185 Aug 30 19:39 .wget-hsts
drwx------ 3 root root 4096 Aug 30 19:38 snap
Keep in mind that using sudo
requires responsibility, as it provides powerful control over the system. Misusing sudo
commands can lead to unintended consequences or security vulnerabilities. It’s recommended to use sudo
only when necessary and to exercise caution when executing commands with administrative privileges.