You’re on a Linux server. You run a command. You get: Permission denied.
So you panic and run sudo chmod 777 my-file, and it works. You move on. But you just made your file readable and writable by anyone — your database credentials, your private key, your whatever. That’s not safe.
Linux file permissions are not cryptic once you see the pattern. It takes about 5 minutes to understand, and 20 minutes to be confident. After that, you’ll stop using sudo as a crutch.
The Permission Triplet: User, Group, Other (ugo)
Every file has three permission sets:
- User (u): the owner of the file
- Group (g): all users in the file’s group
- Other (o): everyone else on the system
Each set has three permissions:
- Read (r): can open and view the file (4)
- Write (w): can modify or delete the file (2)
- Execute (x): can run the file as a program, or (for directories) enter it (1)
When you see -rw-r--r-- on a file, read it as three groups:
-rw-= user can read and write (not execute)r--= group can read (not write or execute)r--= other can read (not write or execute)
This is called symbolic notation. The numbers come next. The Linux manual for chmod provides authoritative documentation on the full range of permission operations.
The Numeric Shortcut: 644, 755, 777 (Why Numbers Are Faster)
Each permission is a number:
- r (read) = 4
- w (write) = 2
- x (execute) = 1
Add them up per group:
rwx= 4+2+1 = 7rw-= 4+2 = 6r-x= 4+1 = 5r--= 4-w-= 2--x= 1
So chmod 644 file means: user gets 6 (rw-), group gets 4 (r–), other gets 4 (r–). The file is readable by everyone, writable only by the owner, not executable by anyone.
Common patterns:
644— regular file (read by all, write by owner only)755— script or executable (run by all, modify by owner only)600— secret file like a key (owner only, nobody else)700— secret directory (owner only, cannot even list it)777— anybody can do anything (dangerous, avoid)
Changing Permissions: chmod
chmod changes the permissions on a file or directory.
Numeric method (fastest):
chmod 644 my-file # user rw, group r, other r
chmod 755 my-script.sh # user rwx, group rx, other rx
chmod 600 ~/.ssh/id_rsa # user rw, group nothing, other nothing
Symbolic method (more explicit):
chmod u+x my-script.sh # add execute for user
chmod g-w my-file # remove write for group
chmod o-rwx my-secret # remove all permissions for other
chmod a+r my-file # add read for all (user, group, other)
chmod u=rw,g=r,o= my-file # set exactly: user rw, group r, other nothing
The symbolic method is clearer when you’re making small changes. The numeric method is faster for one-offs.
Changing Owner: chown
chown changes who owns a file. You need to be the current owner (or use sudo).
chown newuser my-file # change owner to newuser
chown newuser:newgroup my-file # change owner and group
chown -R newuser ./directory # change owner recursively
Common use: after creating files as root, chown them back to your user so you can edit them without sudo.
sudo chown -R $USER ./my-project
Group Permissions: Why They Matter
When you’re on a team, group permissions let you share files without making them world-readable.
Example: you and your team work on a database backup. The file should be readable by your team, but not by random users on the system.
chmod 640 database-backup.sql # user rw, group r, other nothing
chown myuser:myteam database-backup.sql
Now everyone in the myteam group can read the file, but strangers cannot. This is security without locking everyone out.
The umask: Default Permissions When You Create Files
umask sets the default permissions for new files and directories. It works backwards — it’s the permissions to remove from the default.
The default for files is 666 (rw-rw-rw-). The default for directories is 777 (rwxrwxrwx).
If your umask is 022, new files get 644 (666 – 022) and new directories get 755 (777 – 022). That’s the common default and it’s sensible.
umask # check your current umask
umask 022 # set it (add to ~/.bashrc to persist)
The Permission Denied Error: How to Fix It Correctly
When you get Permission denied, ask three questions:
- Am I the owner?
ls -l my-fileshows the owner. If not, you might need to chown or ask the owner. - Do I have permission? Check the user bit (first 3 characters after the dash). If you’re the owner and you don’t have read/write, add it:
chmod u+r my-fileorchmod 644 my-file. - Is it a directory issue? To enter a directory, you need execute on the directory.
chmod 755 my-directorygives everyone permission to enter and list it.
That’s the fix. You’re not trying to chmod 777 and hope. You’re diagnosing the actual problem and fixing the specific permission.
Related reads on command-line mastery: