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
root
is 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
root
user, instead in a production server, instead use a specific user and run root-only commands via thesudo
command. sudo
is a command that allows a user to execute commands with elevated privileges â typically asroot
. What a user is allowed to run withsudo
is defined in the sudoers configuration.
Lessonâs tasks
- Change the password or your
sudo
user - 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 usesudo
to run commands with elevated privileges. Itâs common (and best practice) to grant at least onesudoer
with the same power as root, while others with may be restricted on only certain commands, you specify this in thesudoers
configuration 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
root
user is disabled by default. The default user isubuntu
, who already hassudo
privileges â 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
carlos
and gave him the same privileges asubuntu
using 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/sudoers
file. To safely edit this special file, you should always use thevisudo
command. For now, I chose the simpler group-based method instead. - You can change the password for any given user with
passwd
command
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
carlos
to 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.pem
key 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
sudo
configuration in EC2 letsubuntu
runsudo
without a password, but any new user added to thesudo
group will be required to authenticate with their password (unless you explicitly configure otherwise in/etc/sudoers
). - To make
carlos
runsudo
without a password you need to create the file/etc/sudoers.d/carlos
usingvisudo
:
sudo visudo -f /etc/sudoers.d/carlos
- and add the following line:
carlos ALL=(ALL) NOPASSWD:ALL
- Actually AWS pre-configure this for the
ubuntu
user 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/shadow
to view the contents of the file, access will be denied, but if you usesudo cat /etc/shadow
it 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
reboot
command will be rejected, but withsudo reboot
it works and the server will fully restarted. In confirmed withuptime
when the server was online again. - With the
last
command you can see the login history, you can filter by username (e.g.last ubuntu
,last carlos
or `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 auser
user 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
fail2ban
could be a good idea.
- You can use the command
sudo -i
to fully become theroot
user. That means from now on, you wonât need to prefix commands withsudo
for 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
exit
orlogout
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/sudo
you can see the last times whensudo
was 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/hostname
and/etc/hosts
, and then reboot the server - (Recommended) Use the command
hostnamectl
withouth 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 -i
andsudo -s
are 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 -i
launches 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/root
directory (cd /root
). If you typeecho $HOME
, it will point to/root
. The-i
stands for âsimulate initial loginâ.sudo -s
also launches a root shell, but it doesnât load the root environment and keeps you in your current working directory. The-s
stands for âshellâ, and itâs less powerful than-i
.
Conclusion
- Always double check before pressing
Enter
when youâre acting asroot
on a server. - Always try to use for daily operations a ânormalâ account and add to
sudoers
group so need to usesudo
for 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