How to specify file permission when putting a file using OpenSSH sftp command
Solution 1:
The sftp-server will create the new file with the default umask of the user. This can be overridden with the -u umask
option to sftp-server
SFTP-SERVER(8)
-u umask
Sets an explicit umask(2) to be applied to newly-created files
and directories, instead of the user's default mask.
The client (OpenSSH sftp) can choose to preserve local permission by using the -p
option, which will send the file then fchmod it appropriately.
SFTP(1)
-p Preserves modification times, access times, and modes from the
original files transferred.
If you're strictly asking how to set a mandatory file mode with the sftp client; you can't (at least with openssh sftp-client). You will have to send a chmod after sending the file.
Solution 2:
If an SFTP client does not specify permissions for uploaded files, the OpenSSH SFTP server assigns 0664 permissions to newly created files. That's for the default umask 0002, which you can change using the -u
switch as the answer by @JimB shows.
If an SFTP client specifies the permissions, OpenSSH server uses the specified permissions (the umask does not apply).
Overview of some popular SFTP clients:
PSFTP: Does not allow you to set permissions for uploaded files. (But Unix version preserves files of source local file)
OpenSSH
sftp
: Does not allow you to set permissions for uploaded files. Preserves files of source local file. For details on the-P
switch, see Does OpenSSH SFTP server use umask or preserve client side permissions after put command (chrooted environment)?WinSCP: You can set permissions for uploaded files
See https://winscp.net/eng/docs/ui_transfer_custom
(I'm the author of WinSCP)
FileZilla: Does not allow you to set permissions for uploaded files. Server's default (umask) applies.