SFTP “Permission denied”: Causes and Fixes
“Permission denied” is the most common SFTP error, and also the least helpful one. It can mean two quite different things. The fix depends on which one you have.
First: login or file system?
Look at when the error appears.
| You see | Where it fails | Go to |
|---|---|---|
Permission denied (publickey). right after connecting | Authentication | The login is refused |
Permission denied (publickey,password). | Authentication | The login is refused |
Connected, folder list works, upload fails with Permission denied | File system | Upload or rename fails |
remote open("/…"): Permission denied in the command line client | File system | Upload or rename fails |
In the SFTP protocol, the second case is status code 3, SSH_FX_PERMISSION_DENIED. The server sends it back for one single operation. Your session stays open.
Upload or rename fails
1. The directory is not writable for you
This is the cause in most cases. To create, rename or delete a file, you need write and execute permission on the directory that contains it. The rights of the file itself do not help.
Check the whole path at once:
namei -l /var/www/site/uploads
namei lists every directory along the way with owner and rights. One directory without x for your user is enough to block everything below it.
Typical fix, if the user should own the folder:
sudo chown -R alice:alice /var/www/site/uploads
sudo chmod 755 /var/www/site/uploads
If several users share the folder, use a group instead of opening it to everyone:
sudo chgrp -R webteam /var/www/site/uploads
sudo chmod 2775 /var/www/site/uploads
The 2 (setgid) makes new files inherit the group. Avoid chmod 777: it fixes the error by letting everybody write.
2. You are writing into the chroot root
If the server locks SFTP users into a directory with ChrootDirectory, OpenSSH insists that this directory and all its parents are owned by root and not writable by group or others. Otherwise it refuses the login with bad ownership or modes for chroot directory in the server log.
The consequence: the user can never write directly into the chroot root. Create a subdirectory that belongs to the user:
# /etc/ssh/sshd_config
Match User alice
ChrootDirectory /srv/sftp/alice
ForceCommand internal-sftp
sudo chown root:root /srv/sftp/alice
sudo chmod 755 /srv/sftp/alice
sudo mkdir /srv/sftp/alice/upload
sudo chown alice:alice /srv/sftp/alice/upload
Alice now uploads into upload/.
3. The file exists and belongs to someone else
Overwriting a file needs write permission on the file. If a deploy script or another user created it, your user may only be allowed to read it. ls -l shows the owner. Either change the owner or let your client upload under a new name and rename afterwards (renaming needs rights on the directory, see point 1).
4. New files get the wrong rights
Uploads work, but the web server or a colleague can't read them afterwards. That is the umask of the SFTP server. You can set it in sshd_config:
Subsystem sftp internal-sftp -u 0022
With 0022, files are created as 644 at most and directories as 755. With a shared group, 0002 is the usual choice. Restart the SSH service afterwards.
Two things to know:
- If the user has
ForceCommand internal-sftpin aMatchblock (as in the chroot example above), theSubsystemline does not apply to them. Put the option there instead:ForceCommand internal-sftp -u 0022. - The umask only takes rights away. If your client sends the file as
600, it stays600.
5. SELinux blocks it although the rights look right
On Fedora, RHEL, Rocky or Alma Linux, ls -l can look perfect and the upload still fails. Then SELinux is the gatekeeper. Look for denials:
sudo ausearch -m avc -ts recent
For chrooted SFTP users who should write into their home directories there is a ready switch:
sudo setsebool -P ssh_chroot_rw_homedirs on
For directories outside the usual places, set a fitting label with semanage fcontext and restorecon instead of turning SELinux off.
6. The server only allows reading
The SFTP server can be set up to refuse all writing. Then every right is correct and each upload still fails. Look in sshd_config for internal-sftp -R (read-only) or for -P and -p (single operations denied or allowed).
A single file can also be marked immutable. lsattr file shows that with an i, and sudo chattr -i file removes it.
A full disk or a file system mounted read-only is different: OpenSSH does not report it as “Permission denied” but as Failure. If you see that, check df -h and mount | grep ' ro,'.
The login is refused
Here the server does not accept you at all. With SSH keys, these are the usual reasons:
- The key is not on the server. Your public key has to be in
~/.ssh/authorized_keysof exactly the user you log in as. - The rights on the server are too open. With the default
StrictModes yes, OpenSSH ignores your key if the home directory or~/.sshis writable for others. Fix it withchmod 700 ~/.sshandchmod 600 ~/.ssh/authorized_keys. - The client sends a different key.
sftp -v user@hostshows which keys are offered. Choose one with-i ~/.ssh/id_ed25519. - Passwords are switched off.
Permission denied (publickey)withoutpasswordin the brackets means the server only accepts keys. - Windows OpenSSH: Accounts in the Administrators group read their keys from
C:\ProgramData\ssh\administrators_authorized_keys, not from the user profile.
How to find the real reason
The client only ever sees “Permission denied”. The server knows more.
- Client side:
sftp -vvv user@hostshows the whole handshake, including which keys were tried. - Server log:
journalctl -u ssh -f(Debian, Ubuntu) orjournalctl -u sshd -f(Fedora, RHEL), or/var/log/auth.log. - Log every SFTP operation for a while, then switch it back:
Subsystem sftp internal-sftp -l VERBOSE
With ForceCommand internal-sftp in a Match block, this option belongs there too: ForceCommand internal-sftp -l VERBOSE.
Now each open, rename and remove appears in the log together with the path and the result. You see exactly which path failed, which is usually enough to spot the directory with the wrong rights.
Checklist
- Login or file system? Look at when the error appears.
namei -l <path>: write and execute on the target directory?- Chroot: chroot root owned by root, upload into a subdirectory.
- Existing file: who owns it?
- Wrong rights on new files: set
-ufor the umask. - Rights look right: check SELinux with
ausearch. - Still unclear:
internal-sftp -l VERBOSEand read the server log.
If you are looking for a client that runs the same on Linux, macOS and Windows, have a look at openSFTP. It is free and open source. And if you save passwords in your SFTP client: here is where they end up.
FAQ
Why do I get Permission denied when uploading via SFTP?
Most often the SFTP user has no write permission on the target directory. To create a file you need write and execute permission on the directory, not on the file. Check every directory on the path with namei -l.
Why can't I write to the root of my chrooted SFTP user?
OpenSSH requires the chroot directory and all its parents to be owned by root and not writable by group or others. Create a subdirectory inside it that belongs to the user and upload there.
What does Permission denied (publickey) mean?
The login itself failed: the server did not accept any key you offered. Check which key the client sends with ssh -v and the permissions of ~/.ssh (700) and authorized_keys (600) on the server.