Linux Permissions and sudo in Practical DevSecOps Labs
Understanding file permissions and privilege escalation is essential for working in the Practical DevSecOps labs. This article covers how to check permissions, make files executable, and run commands as different users.
Table of Contents
Understanding File Permissions
Checking permissions
ls -l myfile # long listing shows permissions
stat myfile # detailed file status
Making files executable
To run a file as a script or program, it must have executable permissions:
chmod +x myfile
After adding executable permissions, verify with ls -l:
-rwxr-xr-x 1 root root 3 Aug 13 20:36 myfile
The x in the permission string indicates the file is now executable.
Running the file
./myfile
Note: The
./prefix tells Linux to look for the executable in the current directory. Without it, Linux only searches directories in$PATH.
Running Commands with sudo
The sudo command lets you run commands with elevated privileges (as root by default).
Basic usage
sudo ls
Creating and switching to a new user
adduser --gecos "" john # create user john
usermod -aG sudo john # add john to sudo group
sudo su - john # switch to john
Once logged in as john, try reading a sensitive file:
cat /etc/shadow
cat: /etc/shadow: Permission denied
Even as a regular user in the sudo group, you need to explicitly use sudo to access privileged resources:
echo pdevsecops | sudo -S cat /etc/shadow
The -S flag tells sudo to read the password from standard input, enabling automation.
Returning to root
exit
Note: The
sudoersfile controls which users and groups can usesudo. Administrators use it to delegate permissions without sharing the root password.
Read more about Linux file permissions at Understanding Linux File Permissions.
Common Questions
| Question | Answer |
|---|---|
Why do I need ./ before running a script? |
./ tells the shell to look in the current directory. Without it, the shell only searches $PATH directories. |
What does chmod +x do? |
It adds the executable bit (x) to the file's permissions for owner, group, and others. |
What's the difference between su and sudo? |
su switches to another user entirely. sudo runs a single command as another user (default: root). |
What does -S in sudo -S mean? |
It reads the password from standard input instead of the terminal, enabling script automation. |
| Can I remove executable permissions? | Yes, use chmod -x myfile. |
Wrap-Up
File permissions control who can read, write, and execute files. Use chmod +x to make scripts executable, sudo to escalate privileges when needed, and ls -l to inspect permissions. These are fundamental skills used throughout every DevSecOps lab.