If you see “permission denied” in a terminal, it usually means your user account lacks the file or directory permissions required to execute the command. The fix is to either change the file’s permissions with chmod, run the command with elevated privileges using sudo, or verify you are in the correct directory with the right ownership.
What “Permission Denied” Actually Means
Unix-like systems (Linux, macOS, and the Windows Subsystem for Linux) enforce a permission model for every file and directory. Each has an owner, a group, and an “others” category, each with read (r), write (w), and execute (x) permissions. When you type a command and the shell responds with Permission denied, it means the kernel rejected the operation because your user ID does not match the required permission bits.
This is different from command not found, which means the shell cannot locate the program in your PATH. Permission denied specifically means the file exists and is accessible, but your user is not allowed to perform the requested action (reading, writing, or executing).
Common Scenarios and Immediate Fixes
Here are the most frequent situations and their direct solutions:
- Running a script or binary: If you just downloaded a script and run
./script.sh, you get permission denied because the file lacks execute permission. Fix:chmod +x script.shthen run it again. - Accessing a system directory: Trying to
cd /rootor write to/etcas a normal user will fail. Fix: usesudofor that specific command, e.g.,sudo nano /etc/hosts. - Reading a file owned by another user: If the file has permissions
600(owner read/write only) and you are not the owner, you get denied. Fix: ask the owner to change permissions or add you to the group. - Mount point issues: Sometimes a USB drive or network share is mounted with restrictive options. Fix: remount with
sudo mount -o remount,uid=youruseror use the mount options in/etc/fstab.
How to Check and Change Permissions
To see what permissions are set, use ls -l. The first column shows something like -rw-r--r--. The first character is the file type (- for regular file, d for directory). The next nine characters are three groups of three: owner, group, others. Each group has r, w, x in order, or a dash if that permission is absent.
To change permissions, use chmod with either symbolic or numeric notation:
- Symbolic:
chmod u+x fileadds execute for the owner.chmod go-w fileremoves write for group and others. - Numeric: Each permission has a value: r=4, w=2, x=1. Add them for each class. For example,
chmod 755 filegives owner rwx (7), group r-x (5), others r-x (5).
For directories, execute permission is what allows you to enter the directory (cd). If you get permission denied when trying to cd, check that you have execute on the directory itself and on all parent directories up to the root.
Using sudo Correctly
The sudo command lets you run a single command as the superuser (root) if your user is in the sudoers file. For example, sudo apt update on Debian/Ubuntu or sudo yum install on RHEL-based systems. It is not a permanent switch; it only affects the command you prefix.
Common mistakes include using sudo with redirection. For instance, sudo echo "hello" > /etc/test will still fail because the redirection is done by your shell, not by sudo. Use sudo tee /etc/test instead. Also, avoid running graphical applications with sudo unless necessary, as it can create files owned by root that you later cannot edit without sudo.
Ownership and Groups
Sometimes the permission bits are correct, but you are not the owner or in the required group. Use ls -l to see the owner and group. To change ownership, use chown: sudo chown yourname file changes the owner, sudo chgrp groupname file changes the group. For example, if a file is owned by root and you need to edit it, you can either use sudo or change the ownership to yourself if you have admin rights.
If you are collaborating with a group, you can add a user to a group with sudo usermod -aG groupname username. After that, the user must log out and back in for the group change to take effect.
Special Cases: Root, Home Directory, and Mounts
Your home directory (/home/yourname) should always be writable by you. If you get permission denied there, it is likely because a previous sudo command created files owned by root. Fix by changing ownership back: sudo chown -R yourname:yourname /home/yourname.
For external drives or network mounts, the mount options may restrict access. Check with mount to see the options. If it shows noexec, you cannot run executables from that mount. If it shows ro, it is read-only. Remount with the desired options, e.g., sudo mount -o remount,rw,exec /mnt/usb.
Windows Subsystem for Linux (WSL) has its own quirks. Files in the Windows filesystem (e.g., /mnt/c) are mounted with permissions that may not match Linux expectations. You can change the mount options in /etc/wsl.conf to set metadata or umask to get more control.
Frequently Asked Questions
Why do I get “Permission denied” when I run ./script.sh?
Most likely the script does not have execute permission. Run chmod +x script.sh and try again. If it still fails, check the shebang line (first line like #!/bin/bash) and make sure the interpreter exists.
What is the difference between chmod 755 and chmod 777?
755 gives the owner read/write/execute, and group and others read/execute. 777 gives everyone read/write/execute. 777 is generally risky because any user can modify the file. Use 755 for executables and directories, 644 for regular files.
I used sudo to edit a file, now I can’t edit it without sudo. How do I fix it?
Change the ownership back to your user: sudo chown yourname:yourname filename. If it is a directory and you want to change everything inside, use -R (recursive).
Can I run a command as another user without sudo?
Yes, you can use su - otheruser -c 'command' if you know that user’s password. Alternatively, if you have sudo rights, you can use sudo -u otheruser command.
Why does “sudo” not work on my system?
If you get “sudo: command not found”, your distribution may not have sudo installed. Install it with your package manager (e.g., apt install sudo). If you are already root, you can use su - to switch to root instead.
What is the sticky bit and how does it affect permissions?
The sticky bit (shown as t in the execute position for others) is typically set on directories like /tmp. It means only the file owner, directory owner, or root can delete or rename files within that directory, even if the directory is world-writable. You can set it with chmod +t directory.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Campus Tech Innovation Hubs: What They Are
- Poco C51 Airtel Prepaid Locked Variant India?
- Jenkins vs GitHub Actions: Which CI/CD Fits Your Team?
- Turn Off Backup Encryption in iTunes?
- Shared VPS Hosting: Is It Better?
- Disk Defrag Pro: What It Does and Whether You Need It