Users, Permissions & sudo

Read rwx permissions, fix 'Permission denied' with chmod and chown, use groups and sudo correctly, and run your app as its own low-privilege user instead of root.

Beginner⏱ 5 min readLesson 3 of 17#linux#permissions#chmod#chown#sudo#security

The big idea

An office building has keycards. The cleaner can enter every floor but not the safe. An accountant can open the finance room. Only the manager has the master key.

Linux works the same way. Every file has an owner, a group, and a set of permissions that say who can read, write or execute it. The all-powerful user is root, the master key, and like a master key you should use it as rarely as possible.

Reading permissions

$ ls -l /var/www/myapp
-rw-r--r-- 1 deploy www-data  1534 Nov 15 10:02 package.json
drwxr-x--- 5 deploy www-data  4096 Nov 15 10:02 .next
-rwx------ 1 deploy deploy     812 Nov 15 09:40 deploy.sh
-rw------- 1 deploy deploy     220 Nov 15 09:40 .env

A permission string splits into file type, then read-write-execute for the owner, the group and everyone else

LetterOn a fileOn a folder
r (read)View the contentsList the files inside
w (write)Change the contentsCreate, rename, delete files inside
x (execute)Run it as a programEnter it with cd
-Permission not givenPermission not given

So -rw-r--r-- deploy www-data package.json means:

  • the owner deploy can read and write,
  • the group www-data can only read,
  • everyone else can only read.

Numbers: the octal shortcut

Each permission has a value: r = 4, w = 2, x = 1. Add them for each of the three groups.

NumberLettersMeaning
7rwxRead, write, execute
6rw-Read, write
5r-xRead, execute
4r--Read only
0---Nothing
Common valueUse it for
644 (rw-r--r--)Normal files: code, HTML, config without secrets
755 (rwxr-xr-x)Folders and scripts others may run
600 (rw-------)Secrets: .env, SSH private keys
700 (rwx------)Private folders like ~/.ssh
777 (rwxrwxrwx)❌ Never. Anyone on the server can change it

Changing permissions: chmod

chmod 600 .env                # only the owner can read/write the secrets
chmod 755 deploy.sh           # owner can edit, everyone can run
chmod +x deploy.sh            # just add "execute" for everyone
chmod u+x,g-w file            # u=user(owner), g=group, o=others, a=all
chmod -R 755 public/          # recursive (careful: also makes files executable)

A safer recursive fix for a website folder: folders 755, files 644.

find /var/www/myapp -type d -exec chmod 755 {} +
find /var/www/myapp -type f -exec chmod 644 {} +

Changing the owner: chown

sudo chown deploy file.txt               # new owner
sudo chown deploy:www-data file.txt      # new owner and group
sudo chown -R deploy:deploy /var/www/myapp   # whole folder

Users and groups

whoami                         # who am I?
id                             # my user ID and groups
sudo adduser deploy            # create a user (Ubuntu/Debian, interactive)
sudo usermod -aG sudo deploy   # add to the sudo group (-a = append, don't remove others!)
sudo usermod -aG docker deploy # allow running docker without sudo
groups deploy                  # list a user's groups
sudo deluser olduser

⚠️ usermod -G without -a replaces all of the user's groups. Always use -aG. Group changes apply at the next login.

System users have no password and no shell. They exist only to run one program, so a hacked app can't do much:

sudo useradd --system --no-create-home --shell /usr/sbin/nologin myapp

root and sudo

root (user ID 0) can do anything: read every file, delete the whole system, change any password.

sudo lets a normal user run one command as root, and records it in the logs.

sudo systemctl restart nginx   # one privileged command
sudo -i                        # a root shell (use rarely, exit when done)
sudo visudo                    # safely edit who may use sudo
❌ Don't✅ Do
Log in over SSH as rootLog in as a normal user, use sudo when needed
Run your Node app as rootRun it as a dedicated deploy or myapp user
chmod 777 to "fix" permission errorsGive the right owner/group the minimum permission
Share one login between peopleOne account per person, so logs show who did what

Allowing one exact command without a password

Your deploy script may need to restart the app. Instead of giving the deploy user full root, allow only that command:

# sudo visudo -f /etc/sudoers.d/deploy
deploy ALL=(root) NOPASSWD: /usr/bin/systemctl restart myapp.service, /usr/bin/systemctl status myapp.service

Principle of least privilege

Give every user and every program the minimum access it needs, nothing more.

Drawing diagram…

Fixing "Permission denied"

  1. Who am I? whoami
  2. Who owns it and what's allowed? ls -l file (and ls -ld folder for the folder itself)
  3. Can I reach it? Every parent folder needs x for you. Check with namei -l /var/www/myapp/.env
  4. Fix the owner or group first, then the mode, with the smallest change that works.
$ cat /var/www/myapp/.env
cat: /var/www/myapp/.env: Permission denied
$ ls -l /var/www/myapp/.env
-rw------- 1 deploy deploy 220 Nov 15 .env    # only deploy can read it
$ whoami
ana                                           # ← that's why

Key takeaways

  • Every file has an owner, group and rwx permissions for owner / group / others.
  • Octal: r=4, w=2, x=1. Remember 644 files, 755 folders/scripts, 600 secrets.
  • chmod changes permissions, chown changes ownership, usermod -aG adds groups.
  • Use sudo for single commands; never log in as root or run apps as root.
  • chmod 777 is never the right fix.

Try it yourself

Create a user called myapp, a folder /srv/myapp owned by it, and a .env file inside that only myapp can read. Then confirm your own user gets Permission denied when trying to cat it.