Permission denied: how to fix it on Linux

Topic: Servers linux

Summary

Diagnose and fix permission denied on files, directories, and SSH. Check owner, group, and bits with ls; fix with chmod and chown; for SSH check authorized_keys and server-side permissions. Never use chmod 777 as a fix.

Intent: Troubleshooting

Quick answer

  • For files: ls -la to see owner/group/mode; chmod and chown to grant the least privilege needed; ensure directory execute (x) on the path.
  • For SSH: server-side ~/.ssh 700, authorized_keys 600; key one line; client offers correct key (ssh -v to see what is offered).
  • For sudo: user in sudo/wheel group; /etc/sudoers and drop-ins; check NOPASSWD and command restrictions if you changed them.

Prerequisites

Steps

  1. Identify what was denied

    Note the exact command or action (read file, write file, execute, SSH, sudo); run the command again and capture the error message and path.

  2. Check file permissions and ownership

    ls -la path; if directory, ensure all path components have x for the user; fix with chmod (e.g. 755 dir, 640 file) and chown if you have privilege.

  3. Fix SSH permission denied (publickey)

    On server: chmod 700 ~/.ssh, chmod 600 ~/.ssh/authorized_keys; ensure key is one full line; ssh -v from client to see which key is tried and why rejected.

  4. Fix sudo permission denied

    Ensure user is in sudo or wheel group; check /etc/sudoers and /etc/sudoers.d for syntax and NOPASSWD; use visudo to edit.

Summary

You will diagnose and fix “permission denied” for file access, SSH, and sudo by checking ownership and permissions and applying chmod/chown or SSH/sudo config fixes. Use this when a user or service cannot access a path or run a command.

Prerequisites

  • Shell access (or console if SSH is broken); ability to run chmod/chown or edit config as root.

Steps

Step 1: Identify what was denied

Reproduce the failure and note: the command, the path (if any), and the user. Error may say “Permission denied” or “Operation not permitted.”

Step 2: Check file permissions and ownership

ls -la /path/to/file
ls -la /path/to/dir

For a path like /a/b/c, user needs x on /, /a, /a/b and r (or w) on the target as needed. Fix: chmod 755 dir, chmod 640 file, chown user:group file.

Step 3: Fix SSH permission denied (publickey)

On the server (as that user): chmod 700 ~/.ssh, chmod 600 ~/.ssh/authorized_keys. Ensure the public key is one line. From client: ssh -v user@host and check “Offering public key” and the rejection reason.

Step 4: Fix sudo permission denied

groups user
sudo cat /etc/sudoers
sudo visudo   # fix syntax; add user to sudo/wheel or add rule

Ensure no typo in username or group; check for ! in front of a rule (deny).

Verification

  • The same command or SSH/sudo action succeeds; ls shows intended mode and owner; no 777 used.

Troubleshooting

Still denied after chmod — Process may be running as a different user; check with ps. SELinux/AppArmor may deny; check avc/denied in logs and set context or policy.

SSH works from one machine but not another — Different key or agent; specify key with ssh -i; ensure the right key is in authorized_keys.

Next steps

Continue to