Diagnosing permission denied
Video 120 of 140 in Virtualization Foundations, in the unit “Users, groups, permissions and sudo”. 2:39, with English and Hindi audio.
A command fails with the words permission denied. The temptation is to guess, or to change rights until the error goes away. A better method is to check the cause one step at a time. Ask who you are, what rights the file has, whether every folder on the path lets you through, and which account the failing program runs as. This article walks through that method, with a real refusal and the tools that show each cause.
What you will learn
- Check four things in order: who you are, the file's rights, every folder on the path, and the program's account.
- Every folder on the path needs execute for the account that asks, even when the file itself is open to it.
- The stat command shows the owner, the group and the mode of a file.
- The namei command with the l option shows the owner and mode of each folder on a path.
- Change only the one right that is missing, then run the command again to confirm the fix.
Four questions to ask in order
- Who am I? Check the account and its groups with id.
- What are the rights? Check the owner, the group and the mode of the file.
- Can I reach it? Every folder on the path needs execute for your account.
- Who runs the program? A service may run as a different account than you expect.
Start with who you are. The account and its groups decide almost everything, so id is the first check to make. Then look at the file, and check whether the right you need is there for your account.
Reading a refusal and its reason
cat /etc/shadowA normal account gets the message cat: /etc/shadow: Permission denied. The message is honest. This file is not for ordinary accounts.
stat -c '%A %U %G %n' /etc/shadowNote: The owner is root and the group is shadow, and nobody else has any right. Only root and the shadow group may read this file.
Checking every folder on the path
Permission denied can also mean a missing right on a folder above the file, so check the whole path, not only the file. Picture the path as a row of doors: a home folder, then a folder for alice, then a data folder, then the file. If one folder lacks execute for your account, you cannot pass through it, even when the file itself is open to you.
namei -l /home/alice/data/report.txtRead the output from the top down, and find the line where your account lacks the right it needs. A missing x for your account at any level is the usual cause. When the cause is clear, change only that one right, then run the command again to confirm it works.
Check the program's account and any extra rules
The failing program may not run as you. A web server or a cron job may run as someone other than you, so check that account too.
Note: Some systems add extra rules, such as AppArmor, which can refuse access even when the rights look correct. Check those rules when the rights look right and the command still fails.
Try it yourself
You need a Linux terminal, and a normal account rather than root. Run the three checks in order, and compare each result with what the account should be able to reach.
Try to read the shadow file as a normal account. It is refused.
cat /etc/shadowWhat you should see: The shell prints cat: /etc/shadow: Permission denied
Check who owns the shadow file and which rights it carries.
stat -c '%A %U %G %n' /etc/shadowWhat you should see: The shell prints -rw-r----- root shadow /etc/shadow. Only root and the shadow group may read it.
Walk the path to a file, and check each folder on the way. Replace the path with one of your own files if you prefer.
namei -l /home/alice/data/report.txtWhat you should see: The owner and mode of each folder appear on its own line. A missing x for your account at any level is the usual cause.
Terms
- Execute on a folder
- The right that lets an account enter a folder and reach the files inside it, even if it can see their names.
- AppArmor
- An extra access rule system on some Linux machines that can refuse access even when the usual rights look correct.
Check yourself
Why can a user be refused a file that is open to read?
A folder on the path may lack execute for that user. Without execute, the user cannot pass through that folder to reach the file.
What is the first check when permission denied appears?
Who you are, with id. Then check the file's owner and mode, and every folder on the path.
Why check the program's account when a service fails?
The program may run as a different account than you. Its rights, not yours, decide whether it can reach the file.
Previous: ACLs in brief: rights for named people. Next: Reading the process list with ps.