Introduction
I’ve started following the Linux Upskill Challenge to sharpen my Linux knowledge and hands-on skills. This is the third in a series of 21 lessons designed to be completed on a daily basis.
In this lesson, I learned about the types of users that exist in a Linux system, along with their scope and limitations. I also explored the proper way to handle administrative tasks that require elevated privileges—how to temporarily become root in order to perform sensitive actions while minimizing the risk of making a mistake that could break your server.
Day 3 - Power trip!
- URL: https://linuxupskillchallenge.org/03/
- Previous Lesson: My Linux Upskill Challenge: Day 2
🚪 Introduction
-
rootis the power user on a Linux System, it’s the “super user” account that is all powerful. That also means that a typo in a command being root could mess up your server! - As a Best Practice you shouldn’t use
rootuser, instead in a production server, instead use a specific user and run root-only commands via thesudocommand. -
sudois a command that allows a user to execute commands with elevated privileges — typically asroot. What a user is allowed to run withsudois defined in the sudoers configuration.
Lesson’s tasks
- Change the password or your
sudouser - Change the hostname
- Change the timezone
👨👦👦 Linux type of users
- In a Linux system you can do two kind of changes
- Global changes: affects all users, programs/environments shared with any user.
- Local changes: affects only one user, programs/environments for a particular user.
- In Linux there are three type of user:
-
root- the all-mighty powerful superuser that can execute any command and can do global changes and local changes for any user as well. -
sudoers- those are regular users (or groups) that are allowed to usesudoto run commands with elevated privileges. It’s common (and best practice) to grant at least onesudoerwith the same power as root, while others with may be restricted on only certain commands, you specify this in thesudoersconfiguration file. -
regular user- only can do local changes, and not allowed to usesudo
-
🚀 Sudo Access and User Creation on EC2
- On an AWS EC2 instance, the
rootuser is disabled by default. The default user isubuntu, who already hassudoprivileges — it’s already a sudoer. - I confirmed this with the following commands:
whoami
# ubuntu
groups ubuntu
# ubuntu : ubuntu adm cdrom sudo dip lxd
sudo whoami
# root
- You can enable SSH login as the root user by following this guide, but I didn’t try it — it seemed risky and unnecessary for my purposes.
- Instead, for practice, I created a new user named
carlosand gave him the same privileges asubuntuusing the following commands:
sudo adduser carlos
sudo usermod -aG sudo carlos
- There’s another way to grant elevated privileges to a user: by modifying the
/etc/sudoersfile. To safely edit this special file, you should always use thevisudocommand. For now, I chose the simpler group-based method instead. - You can change the password for any given user with
passwdcommand
passwd carlos
Changing password for carlos.
Current password:
New password:
Retype new password:
passwd: password updated successfully
🔐 SSH Access with Shared Key
- When I added the new user, it asked me to set a password. However, I wanted
carlosto log in using the same SSH key as the ubuntu user, and also make sure password-based authentication remained disabled. - I used the following steps:
# Create .ssh directory for carlos
sudo mkdir -p /home/carlos/.ssh
# Copy authorized_keys from ubuntu
sudo cp /home/ubuntu/.ssh/authorized_keys /home/carlos/.ssh/
# Set correct ownership and permissions
sudo chown -R carlos:carlos /home/carlos/.ssh
sudo chmod 700 /home/carlos/.ssh
sudo chmod 600 /home/carlos/.ssh/authorized_keys
- I then started a new SSH session as
carlos, using the samelinux.pemkey file. - I also tested login using a password instead of the SSH key, and it was rejected. As it turns out, password-based SSH authentication is disabled by default on this EC2 AMI, so no extra configuration was needed.
🧪 Password Prompt on sudo
- One difference I noticed: when running sudo as
carlos, it prompted for the user password, unlikeubuntu, which doesn’t require it. Here’s the behavior:
carlos@ip-172-31-92-220:~$ whoami
carlos
carlos@ip-172-31-92-220:~$ sudo whoami
[sudo] password for carlos:
root
- This is because the default
sudoconfiguration in EC2 letsubunturunsudowithout a password, but any new user added to thesudogroup will be required to authenticate with their password (unless you explicitly configure otherwise in/etc/sudoers). - To make
carlosrunsudowithout a password you need to create the file/etc/sudoers.d/carlosusingvisudo:
sudo visudo -f /etc/sudoers.d/carlos
- and add the following line:
carlos ALL=(ALL) NOPASSWD:ALL
- Actually AWS pre-configure this for the
ubuntuuser in the file/etc/sudoers.d/90-cloud-init-users, as I checked here:
sudo cat /etc/sudoers.d/90-cloud-init-users
# Created by cloud-init v. 24.4.1-0ubuntu0~24.04.1 on Fri, 25 Apr 2025 00:08:00 +0000
# User rules for ubuntu
ubuntu ALL=(ALL) NOPASSWD:ALL
Testing the power of sudo
- If you try to use
cat /etc/shadowto view the contents of the file, access will be denied, but if you usesudo cat /etc/shadowit will work. 🤓 This file contains the hashed password for all system users, so it’s a intentionally restricted or non-authorized users. - Also If I tried the
rebootcommand will be rejected, but withsudo rebootit works and the server will fully restarted. In confirmed withuptimewhen the server was online again. - With the
lastcommand you can see the login history, you can filter by username (e.g.last ubuntu,last carlosor `last root)- As expected, on my EC2 instance there wasn’t entries for
root
- As expected, on my EC2 instance there wasn’t entries for
last root
wtmp begins Fri Apr 25 00:07:50 2025
- I checked the failed login attempts with the command
sudo lastb, and I found that an IP from China had tried to log in to my server using the auseruser several times on May 2nd. 😱 - Apparently, this is kind of normal for internet-facing servers, and there’s nothing to worry about at this time. However, implementing
fail2bancould be a good idea.
- You can use the command
sudo -ito fully become therootuser. That means from now on, you won’t need to prefix commands withsudofor any high-privilege tasks. This is handy when you have a series of commands to run as “root.” You’ll also notice that the prompt changes. I recommend doing this with caution. - To get back to your normal “sudoer” account you need to type
exitorlogout
carlos@ip-172-31-92-220:~$ whoami
carlos
carlos@ip-172-31-92-220:~$ sudo -i
root@ip-172-31-92-220:~# whoami
root
root@ip-172-31-92-220:~# logout
carlos@ip-172-31-92-220:~$ whoami
carlos
carlos@ip-172-31-92-220:~$
- With
sudo journalctl -e /usr/bin/sudoyou can see the last times whensudowas used including the user which used and with which command.
👷 Administrative Tasks
This are some examples of simple administrative tasks that a sysadmin regualar do using sudo:
Change the hostname
One of the must basic task for you’ll need sudo priviledges is to rename your server, that means change the hostname.
Probably the current hostname of the server is what you see in the prompt after “username@”, however if you want to double check, there are several ways to do it on linux: you can issue any of this commands: hostname , uname-h, or check the content of this file: cat /etc/hostname.
Also, if you need to change it, you can use any of this options:
- Edit these two files:
/etc/hostnameand/etc/hosts, and then reboot the server - (Recommended) Use the command
hostnamectlwithouth rebooting the server, like this:
sudo hostnamectl set-hostname myec2ubuntu
To observe the change in the prompt you should open a new session using the command bash or just logout and login again.
carlos@ip-172-31-92-220:~$ sudo hostnamectl set-hostname myec2ubuntu
carlos@ip-172-31-92-220:~$ bash
carlos@myec2ubuntu:~$
I stopped and re-started the EC2 instance just to be sure the new hostname preservers after the server restart.
NOTE
I decided to deploy a local VM on my computer to not depend on starting an EC2 instance in AWS every time I want to lab, this saves me time (and money!)
So I installed UTM on my Mac computer and used Ubuntu Server for ARM (24.04.2 LTS) ISO image to launch my Linux VM, and I destroyed (Terminate) the EC2 in AWS.
I may do a Installation Guide someday
Change the Timezone
Another regular task when you are in charge or server administration is to setup the correct timezone (this could be the timezone of the region the server is actually located, or a timezone the company agreed). The default for any Linux server is UTM. (that is same as GMT).
To check the current settings you use:
timedatectl
You can check the available timezones with:
timedatectl list-timezones
After decide which one to use, I change it using this command
sudo timedatectl set-timezone Americas/Guatemala
Then I confirmed the change, again with:
`
timedatectl
Local time: Thu 2025-05-08 12:51:30 CST
Universal time: Thu 2025-05-08 18:51:30 UTC
RTC time: Thu 2025-05-08 18:51:30
Time zone: America/Guatemala (CST, -0600)
System clock synchronized: no
NTP service: active
RTC in local TZ: no
`
Selecting the correct timezone for all your servers take importance when you need configure scheduled tasks that you need to run at certain time, it also allows you to have consistency in the timestamps of your log files (those under /var/log).
Final Question?
What’s the difference between sudo -i and sudo -s?
-
sudo -iandsudo -sare two commands that allow you to “become root” and run elevated-privilege commands without needing to prefix each one withsudo. However, they have some key differences: -
sudo -ilaunches a root shell—it’s like starting a new shell session as if you had logged in directly as the root user. It also loads the root user’s environment and places you in the/rootdirectory (cd /root). If you typeecho $HOME, it will point to/root. The-istands for “simulate initial login”. -
sudo -salso launches a root shell, but it doesn’t load the root environment and keeps you in your current working directory. The-sstands for “shell”, and it’s less powerful than-i.
Conclusion
- Always double check before pressing
Enterwhen you’re acting asrooton a server. - Always try to use for daily operations a “normal” account and add to
sudoersgroup so need to usesudofor elevated priviledge commands. - For critical changes on production systems, take extra precaution actions like using a test environment, check for syntax errors and typos, and monitor the log files.
Additional Resources
- How To Edit the Sudoers File
- Hardening SSH
- SSH Tunneling
- How To Set Up Multi-Factor Authentication for SSH on Ubuntu 20.04
Top comments (0)