SFTP with sudo: edit root-owned files with sftp -s
SFTP has no sudo command. Start the SFTP server through sudo with sftp -s and a NOPASSWD sudoers rule, or upload to your home folder and move it with sudo.
An SFTP session runs as the account you logged in with. There is no sudo or su command inside SFTP, so a root-owned path such as /etc/nginx gives Permission denied even when that account may use sudo in a shell. There are two ways round it.
Option 1: upload, then move with sudo
No server changes. Upload the file to your home folder, then log in with SSH and put it in place:
sudo install -o root -g root -m 644 ~/nginx.conf /etc/nginx/nginx.conf
install copies the file and sets owner, group and mode in one step. To download a root-only file, copy it to your home folder with sudo cp, give yourself read access with sudo chown user1 ~/file, and download it from there.
Option 2: run the SFTP server through sudo
The sftp command line client has a -s option that picks what to run on the host. When the value contains a /, sftp runs it as a command instead of asking for the normal sftp subsystem. Pointing it at the SFTP server program through sudo gives a session that runs as root:
sftp -s "sudo /usr/lib/openssh/sftp-server" [email protected]
Find the server program on the host
grep -i '^Subsystem' /etc/ssh/sshd_config
Debian and Ubuntu install it as /usr/lib/openssh/sftp-server; Fedora installs it as /usr/libexec/openssh/sftp-server. If the line says internal-sftp, that server is built into sshd and cannot be started this way, so look for the separate program at those paths.
Let sudo run it without a password
sftp runs the command without a terminal, so sudo cannot ask for a password. Without a rule you get:
sudo: a terminal is required to read the password; either use ssh's -t option or configure an askpass helper
Connection closed
Add a rule for that one program with sudo visudo -f /etc/sudoers.d/sftp-root:
user1 ALL=(root) NOPASSWD: /usr/lib/openssh/sftp-server
The file is read only if /etc/sudoers has an @includedir /etc/sudoers.d (or #includedir) line, which many distributions ship. If Defaults requiretty is set, sudo refuses to run without a terminal and this method fails.
This rule lets user1 read, write and delete any file on the host as root over SFTP. Give it only to accounts that already have full sudo rights.
Connect
Run the sftp -s command above. New files belong to root. To set their mode, the server accepts -u with a umask, for example "sudo /usr/lib/openssh/sftp-server -u 022".
Things that do not work
- Accounts with a nologin shell. sshd starts the command through the account's login shell, so it never runs. See SSH "This account is currently not available" (nologin shell).
- scp. It has no option to choose the server program. Use sftp, or option 1.
- Clients with no server-command setting. A client that only asks for the standard
sftpsubsystem always gets a session as the login user.
Related
- SFTP
- sftp put -r: copy a whole folder, and "Couldn't canonicalize"
- SFTP "Received message too long"
- SSH agent not available after sudo or su
- Do scp and sftp use ~/.ssh/config? Host aliases, -F and -P
- sudo "a terminal is required to read the password" when running a command over SSH
Sources
- OpenSSH sftp(1) manual: -s
- OpenSSH sftp-server(8) manual
- OpenSSH source: sftp.c (path versus subsystem for -s)
- sudoers manual: NOPASSWD, requiretty, @includedir
- sudo source: tgetpass.c (terminal required message)
- Debian package openssh-sftp-server file list
- Fedora openssh package spec (sftp-server in libexecdir)