Ethical Hacking Series, Part 3. In Part 1 we built a Kali Linux lab, and in Part 2 we learned to move around the file system. Now we look at who is allowed to do what on a Linux system.

Users, groups and permissions are where a lot of real security findings come from. A file that anyone can read, a service running as root, a user who was given sudo “just for now”: none of these look dramatic, but they’re exactly what an attacker looks for after getting a foothold, and what a defender should fix first. The idea behind all of it is the principle of least privilege. Every user and process gets only the access it needs, so if one account is compromised, the damage stays small.

Three kinds of users

Every user on Linux has a numeric user ID (UID), and the number tells you what kind of account it is:

  • root has UID 0 and can do anything on the system.
  • System accounts have UIDs below 1000. They exist for services like web servers, databases and SSH, not for people.
  • Regular users start at UID 1000. Your own Kali user is usually the first one.

You can see all of them in /etc/passwd. Each line holds the username, UID, group ID, home directory and login shell. Despite the name, there are no passwords in it. The password hashes live in /etc/shadow, which only root can read. If you ever find /etc/shadow readable by a normal user during a test, that’s a serious finding, because the hashes can be cracked offline.

cat /etc/passwd
sudo cat /etc/shadow

Creating, switching and removing users

There are two commands for creating users, and the difference catches people out. useradd is the low-level tool and does only what you tell it. adduser (on Debian-based systems like Kali) is a friendlier interactive script that creates the home directory, asks for a password and fills in the details for you.

With useradd, you usually want -m to create a home directory and -s to set the login shell. Without -s, you may end up with a basic /bin/sh shell:

sudo useradd -m -s /bin/bash analyst
sudo passwd analyst
su - analyst

su - analyst switches to that user with their full login environment. Type exit to come back.

To remove a user along with their home directory and mail spool, use -r. Without it, the home directory stays behind with all its files:

sudo userdel -r analyst

sudo: admin rights without logging in as root

Logging in as root for everyday work is a bad habit. One mistyped command can damage the system, and there’s no record of who did what. sudo lets a normal user run a single command with root privileges, and each use is logged in /var/log/auth.log.

On Kali and other Debian-based systems, members of the sudo group can use sudo. To give a user admin rights, add them to that group:

sudo usermod -aG sudo analyst

The -a matters. -aG adds the group to the user’s existing ones, while -G on its own replaces all their secondary groups with only the ones you list. Forgetting -a is a classic way to accidentally remove someone’s access. The user needs to log out and back in before a new group takes effect.

From an attacker’s point of view, sudo -l is one of the first commands to run on a compromised account. It lists what that user may run as root, and a careless sudo rule is one of the most common privilege escalation paths.

Groups

Groups let you give access to several users at once instead of one by one. Every user has one primary group, which owns the files they create, and can belong to any number of secondary (supplementary) groups for extra access. When creating a user, -g sets the primary group and -G sets the secondary groups:

sudo groupadd devteam
sudo useradd -m -s /bin/bash -g devteam -G sudo dev1
id dev1
groups dev1

id shows the UID, primary group and every group the user is in, and groups lists just the group names. All groups are defined in /etc/group. Use grep to find who is in a particular one:

grep sudo /etc/group
grep devteam /etc/group

In the video I use groups to give one team access to a directory while keeping everyone else out. That’s the pattern you’ll use on real servers too: give the access to a group, and add or remove people from the group.

Reading permissions

Run ls -l and every file shows a permission string like -rwxr-x---. After the first character (the file type, covered in Part 2), there are three sets of three:

  • the owner (user) who owns the file
  • the group that owns the file
  • others, meaning everyone else on the system

Each set says whether that class can read, write or execute. For directories, read means listing the contents, write means creating or deleting files inside, and execute means being allowed to enter the directory at all.

The numbers

Each permission has a value: r = 4, w = 2, x = 1. Add them up for each set and you get a three-digit number:

  • 7 = 4+2+1 = rwx
  • 6 = 4+2 = rw-
  • 5 = 4+1 = r-x
  • 4 = r–
  • 1 = –x
  • 0 = no access

So 710 means the owner has full access, the group can only execute (or enter, for a directory), and others get nothing. 622 means the owner can read and write, while group and others can write but not read. That’s an odd combination you’d rarely use, but it’s a good exercise for reading the numbers. The everyday ones to remember are 644 for normal files, 755 for scripts and directories, 600 for private keys, and 640 for config files that one group needs to read.

Changing ownership and permissions

chown changes who owns a file, and can set the group at the same time. -R applies it to a directory and everything inside:

sudo chown analyst:devteam report.txt
sudo chown -R www-data:www-data /var/www/site

chmod changes the permissions. You can use numbers, or symbolic notation where u, g and o stand for user, group and others, and + or - adds or removes a permission:

chmod 640 config.env
chmod u+x deploy.sh
chmod g-w,o-r notes.txt
chmod -R 750 project/

When setting up directories for a demo, mkdir -p creates any missing parent directories in one go, for example mkdir -p /opt/app/config.

A real example: a leaked .env file

This is the part of the video that shows why permissions matter. I set up Nginx serving a site, with a .env file in the web directory containing API keys and database credentials, the kind of file that ends up there more often than you’d think. With loose permissions, Nginx can read it, so anyone who requests /.env in a browser downloads your secrets.

After tightening the permissions so the Nginx user can no longer read the file, the same request returns 403 Forbidden. That single permission change is the difference between a working site and a credential leak.

Permissions are your last line of defence here, though, not the only one. In a real deployment, keep files like .env outside the web root entirely, and add an Nginx rule that denies requests for hidden files. Automated scanners request /.env, /.git/ and backup files on every site they find, all day long.

Two quick checks: who and history

who shows who is logged in right now, and from where. history shows the commands the current user has run. Both are useful in an investigation: an unexpected session in who, or strange commands in a user’s history (stored in ~/.bash_history or ~/.zsh_history), are worth looking into. Attackers know this too, which is why a history file that is suspiciously empty can be a clue in itself.

Tip: hostnames for your lab

Typing IP addresses gets tedious in a lab. You can map an IP to a name in the hosts file so you can use something like ssh kali@kali-lab. On Linux and macOS, edit /etc/hosts. On Windows, it’s C:\Windows\System32\drivers\etc\hosts (open your editor as administrator). Add a line like this:

192.168.1.45   kali-lab

Practice tasks

  • Create a user with useradd and no -m, then check whether it got a home directory. Delete it and recreate it properly.
  • Create a group, add two users to it, and make a directory that only that group can enter (chmod 770). Test it with a third user.
  • Set a file to 622 and 710, then explain each result from ls -l before checking.
  • Run sudo -l as your normal user and read what it says.
  • Find every file in your home directory that others can write to: find ~ -perm -o+w.

A note on ethics: practise these techniques only on your own lab machines or on systems you have written permission to test.

What’s next

Users and permissions tell you who can do things. Next, Part 4: Linux File System Explained shows where everything lives on a Linux system, and how to search logs and configs with cut, grep, awk and sed. Catch up on Part 1: Kali Linux Lab Setup and Part 2: Linux Commands Every Ethical Hacker Must Know, and for the service-account side of permissions, see Linux service users and non-interactive shells.

Watch Part 3 on my YouTube channel, Madhukar Reddy (@awsandevops).

Madhukar Reddy

DevOps engineer focused on AWS, Docker, Kubernetes, cloud infrastructure, and cyber security. Shares practical cloud and DevOps content based on hands-on deployments, infrastructure troubleshooting, and real-world projects.

$ This blog is currently running on AWS EC2 using Docker-based deployment.

Leave a response