SFTP Permission Denied: Fix Ownership, Chroot and Umask
Fix SFTP permission denied on Linux: trace ownership with namei, build a correct chroot, set the upload umask, and read the sshd logs that name the cause.
An SFTP "permission denied" almost always means the server's filesystem refused the operation, not your client. Your login worked. The upload failed because the SFTP user cannot write to the target directory, cannot pass through a parent directory, cannot overwrite a file someone else owns, or is stuck in a chroot set up the wrong way. Check ownership along the whole path first. We run these checks in a terminal next to the DockLab SFTP panel, but any shell on the server will do.
Quick answer
- Run
namei -l /full/target/pathon the server. Every directory needsxfor the SFTP user, and the last one also needsw. - Test as that user:
sudo -u alice test -w /full/target/path && echo writable. - If the user is chrooted, the chroot directory and every parent must be owned by
rootand must not be writable by group or others. Uploads go into a subdirectory the user owns. - If uploads work but other people cannot edit the files, the problem is the umask. Set it with
ForceCommand internal-sftp -u 0002and use a setgid group directory. - Read the log with
sudo journalctl -u ssh -fwhile you retry. sshd usually prints the reason.
Find out which layer is refusing
When you see "Permission denied", the first thing to work out is when it happened. Authentication and file access are checked separately. If you can connect and list files but writing fails, your key or password is fine and the filesystem is what is saying no. A failure before you ever get a directory listing is a different problem.
| What you see | Where it comes from | Usually means |
|---|---|---|
Permission denied (publickey) before any listing | SSH authentication | Wrong key or user, or bad authorized_keys modes |
Connection drops right after login, bad ownership or modes for chroot directory component in the log | sshd chroot check | Chroot path is not root-owned, or is group/world writable |
remote open("/x/file"): Permission denied (OpenSSH sftp) | Filesystem, via SSH_FX_PERMISSION_DENIED | No write on the directory, or no traverse on a parent |
open for write: permission denied (FileZilla) | Same filesystem refusal | Same as above, or the existing file belongs to someone else |
Upload works, teammate gets Permission denied editing it | umask on the SFTP session | Files were created 0644 and owned by another user |
Failure or No space left instead of "denied" | Disk or quota | Not a permissions problem, see Linux server disk full |
Switching clients will not fix any of the filesystem rows. FileZilla, WinSCP, Cyberduck and the sftp command all pass along the same status code from the server.
Check ownership and traverse permissions along the whole path
A common mistake is to run chmod 777 on the target folder and stop there. That fixes nothing if /var/www has mode 750 and the SFTP user is not in its group. To create a file in /var/www/site/uploads, the user needs execute (traverse) permission on /, /var, /var/www, /var/www/site and uploads, plus write permission on uploads itself.
namei from util-linux prints the whole chain in one go:
namei -l /var/www/site/uploads
f: /var/www/site/uploads
drwxr-xr-x root root /
drwxr-xr-x root root var
drwxr-x--- www-data www-data www
drwxr-xr-x deploy deploy site
drwxr-xr-x deploy deploy uploads
Here a user called alice stops at www. She is not www-data, she is not in that group, and "others" have no x. Then check from her side instead of guessing:
id alice
sudo -u alice test -x /var/www && echo can-traverse
sudo -u alice test -w /var/www/site/uploads && echo writable
Be careful with sudo -u: it tests the real filesystem, and a chrooted SFTP user sees a different root. For chrooted users, run the same test on the real path that the chroot maps to.
Overwriting is the other common case. Replacing an existing file needs write permission on that file. Deleting or renaming it needs write permission on the directory. Many clients upload to a temporary name and then rename it, so they need directory write even when you only meant to "overwrite". If the directory has the sticky bit (drwxrwxrwt, as on /tmp), only the file's owner can delete or rename it, even when the directory is group writable.
Two less common causes that also come back as "denied":
lsattr /var/www/site/uploads/config.php # an 'i' flag means immutable, even root cannot write
findmnt -no OPTIONS --target /var/www # 'ro' means the filesystem is mounted read-only
When we track down one of these, we keep namei running in a terminal split next to the DockLab SFTP panel on the same host. That way we can retry the upload and read the ownership chain without switching windows or reconnecting. A plain ssh session gives you the same information.
Chroot: a root-owned jail with a writable subdirectory
This is the cause most often behind "it worked until we locked the user in". The sshd_config man page says that every component of the ChrootDirectory path must be a root-owned directory that is not writable by group or others. It also says this is checked unconditionally, even with StrictModes turned off. If you chown alice the chroot so she can upload, sshd refuses the session. If you leave it root-owned and expect her to upload into the top level, the filesystem refuses the write.
The fix is a root-owned jail with a subdirectory that the user owns.
Create the group and the user
sudo groupadd sftponly sudo useradd -M -d /uploads -s /usr/sbin/nologin -G sftponly alice sudo passwd alice # or install her key, see the note belowBuild the directory tree with the right owners
sudo mkdir -p /srv/sftp/alice/uploads sudo chown root:root /srv/sftp /srv/sftp/alice sudo chmod 755 /srv/sftp /srv/sftp/alice sudo chown alice:sftponly /srv/sftp/alice/uploads sudo chmod 2775 /srv/sftp/alice/uploads namei -l /srv/sftp/alice/uploadsAdd the Match block at the end of sshd_config
Append this to the end of
/etc/ssh/sshd_config. AMatchblock applies to every line after it, so it must come last.Match Group sftponly ChrootDirectory /srv/sftp/%u ForceCommand internal-sftp -d /uploads -u 0002 AllowTcpForwarding no X11Forwarding nointernal-sftpruns the SFTP server inside sshd, so the jail does not need any binaries or libraries.-d /uploadsis resolved inside the chroot, so the user lands in the writable directory and her first upload works.Validate, reload and confirm the effective settings
sudo sshd -t sudo systemctl reload ssh sudo sshd -T -C user=alice,host=client,addr=203.0.113.10 | grep -Ei 'chrootdirectory|forcecommand'
If Alice logs in with a key, authorized_keys stays outside the jail. With -d /uploads as her home, the simplest setup is AuthorizedKeysFile /etc/ssh/authorized_keys/%u inside the same Match block. Make that file owned by root and readable by her.
Umask: uploads succeed, then nobody else can edit them
The quieter version of this problem goes like this: Alice uploads index.php, the deploy user cannot change it, and the error shows up an hour later in a different tool. Files created over SFTP get the session's umask. The umask(2) man page gives the arithmetic: a requested 0666 with the typical umask of 022 gives 0644, so the group has read access but cannot write.
The sftp-server man page documents -u umask, which sets an explicit umask for new files and directories and overrides the user's default. internal-sftp accepts the same flags. For one group of users, put it on the ForceCommand as shown above. For everyone, change the subsystem line in /etc/ssh/sshd_config:
Subsystem sftp /usr/lib/openssh/sftp-server -u 0002
A umask of 0002 only helps if the files also end up in the right group. That is why we used chmod 2775 above: the setgid bit on the directory makes new files inherit the directory's group instead of the uploader's primary group. For a web root shared between several SFTP users and a deploy user, this is the pattern we use:
sudo groupadd webdev
sudo usermod -aG webdev alice
sudo usermod -aG webdev deploy
sudo chgrp -R webdev /var/www/site
sudo find /var/www/site -type d -exec chmod 2775 {} +
sudo find /var/www/site -type f -exec chmod 664 {} +
If you need more than one group, or you cannot rely on every client honoring a umask, use default ACLs (sudo apt install acl if setfacl is missing):
sudo setfacl -R -m g:webdev:rwX /var/www/site
sudo setfacl -R -d -m g:webdev:rwX /var/www/site
getfacl /var/www/site | head
When a directory has a default ACL, the umask is ignored for new entries in it. The same umask(2) page says the inherited ACL decides the permission bits instead. This explains a common surprise: you change -u and nothing happens, because an old ACL is still in charge. Run getfacl before you spend time on the umask.
Group membership changes only apply to new sessions. Disconnect the SFTP client completely and reconnect before you test again.
Some clients also run chmod after an upload to copy the local file's mode ("preserve permissions"). If the server ends up with 0600 files that came from a Mac, turn that option off in the client. The server umask cannot undo a chmod the client sends afterwards.
Read the logs instead of guessing
Ubuntu's OpenSSH service is called ssh, so you can follow it while you retry:
sudo journalctl -u ssh -f
The chroot ownership error appears there by name. By default the SFTP server only logs errors. To see each open and write and the path the server actually used, raise the level for the affected users:
ForceCommand internal-sftp -d /uploads -u 0002 -l INFO
The sftp-server man page says INFO and VERBOSE log client transactions, and that in a chroot, logging may need a log socket inside the chroot on some systems. If a chrooted session logs nothing, check that before you assume nothing happened. The transaction log often shows the real cause: the client wrote to /var/www/site/uploads (a full path that does not exist inside the jail) instead of /uploads. That path mistake shows up in community troubleshooting threads again and again.
Finally, check that someone has not made the server read-only on purpose. sftp-server -R denies every write, and -P can block specific requests. Run sudo sshd -T -C user=alice,host=x,addr=203.0.113.10 | grep -i sftp to see what actually applies to that user.
A note on WinSCP for Mac
Many people searching for this error are on a Mac with a WinSCP guide open. WinSCP's own documentation says there is no native version of WinSCP for OS X. The only official route it describes is running it under Wine, and that page was last updated in 2019. On a Mac you are better off with a native client. We compare the options honestly in WinSCP alternative for Mac. WinSCP is still excellent on Windows, and if all you need is a two-pane file manager, a dedicated SFTP client is simpler than a full workspace.
Whatever client you pick, the fixes in this guide are all on the server. DockLab helps because the terminal, the SFTP panel and the editor share the same connection, so you can run the diagnosis and retry the upload in one place. It does not change what the filesystem allows. If you manage several servers, see the plan details on Pricing.
Frequently Asked Questions
Why do I get permission denied when I can log in over SFTP?
Logging in only proves that your key or password was accepted. Every open, write, rename and delete is checked against the filesystem after that. Run namei -l on the target path and look for a directory where the user has no w or x.
Should I chmod 777 the upload folder?
No. It often does not even help, because a parent directory without traverse permission still blocks the user. It also lets every account on the server change those files. Use a shared group with a setgid directory (2775) and a 0002 umask, or default ACLs.
Why does my chrooted SFTP user get disconnected right after login?
sshd requires every component of the ChrootDirectory path to be owned by root and not writable by group or others, and it checks this even with StrictModes off. Look for bad ownership or modes for chroot directory component in journalctl -u ssh. Give the chroot back to root and let the user write into a subdirectory they own.
How do I make SFTP uploads group writable?
Add -u 0002 to internal-sftp or to sftp-server in sshd_config, and set the setgid bit on the target directory so new files inherit its group. If the directory has a default ACL, the ACL decides the permissions and the umask is ignored. Reconnect after any group change, because existing sessions keep their old groups.
Is SFTP permission denied ever a client problem?
Rarely. The common client-side causes are an absolute path that does not exist inside a chroot, and a "preserve permissions" option that runs chmod after the upload. In both cases the server log at -l INFO shows exactly which path and request failed.
Browse with SFTP, edit in Monaco, save straight to the server.
DockLab puts the file tree, the editor and the terminal side by side, so a config change is one window, not three apps.