Do scp and sftp use ~/.ssh/config? Host aliases, -F and -P
Yes. scp and sftp start ssh, so Host aliases, User, Port, IdentityFile and ProxyJump in ~/.ssh/config apply. Forwards, agent and RemoteCommand act differently.
Yes. scp and sftp do not make their own connection; they start ssh, and ssh reads options from the command line, then ~/.ssh/config, then /etc/ssh/ssh_config. Every alias and option you use for ssh works for file transfers too.
With this block in ~/.ssh/config:
Host prod
HostName prod.example.com
User user1
Port 2222
IdentityFile ~/.ssh/id_ed25519
ProxyJump [email protected]
these all use the same host, user, port, key and jump host:
ssh prod
scp report.txt prod:/tmp/
sftp prod
Command-line options that differ from ssh
- Port is
-P, capital. In scp and sftp, lowercase-pkeeps file times and modes, soscp -p 2222 file host:does not set a port.Portin the config file works the same in all three. -F filereads another config file instead of~/.ssh/config. It is passed straight to ssh.-o Option=valuepasses any ssh_config option,-ipicks a key, and-Jsets a jump host, as in ssh.- The colon matters.
scp report.txt prodcopies to a local file namedprod. Writeprod:to mean the host.
Config options scp and sftp change
Both programs give ssh a few options of their own on its command line. Command-line values beat the config file, so these lines in a Host block do not take effect for transfers:
- LocalForward, RemoteForward, DynamicForward and X11 forwarding are cleared, so a transfer does not open your port forwards.
- ForwardAgent is turned off. Use
scp -Aorsftp -Aif the remote side needs your agent. - PermitLocalCommand is turned off, so
LocalCommanddoes not run. - RemoteCommand and RequestTTY are cleared by scp. sftp does not clear
RemoteCommand, so a Host block that has one makes sftp stop with:
Cannot execute command-line and remote command.
Fix it with a second alias without RemoteCommand, or run sftp -o RemoteCommand=none prod, the same setting scp uses.
Copies between two remote hosts
scp prod:a.log backup: goes through your computer by default, so your config is used for both hosts. With -R, scp logs in to the first host and runs scp there, and that host's own config and keys are used for the second connection.
When the config seems ignored
- sudo scp runs as root and reads root's config file (usually
/root/.ssh/config), not yours. - Check what ssh will use:
ssh -G prodprints the final settings.scp -vandsftp -vshow theReading configuration datalines. - First value wins. A
Host *block placed aboveHost prodsets values that the later block cannot change.
Related
- The ssh config file
- Jump hosts (ProxyJump)
- SFTP
- SFTP with sudo: edit root-owned files with sftp -s
- ssh-copy-id through a jump host (ProxyJump)
- SSH agent forwarding not working: Could not open a connection to your authentication agent
- SSH ControlMaster: reuse one connection with ControlPath and ControlPersist