Strovent SystemsSign in
Reference

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 -p keeps file times and modes, so scp -p 2222 file host: does not set a port. Port in the config file works the same in all three.
  • -F file reads another config file instead of ~/.ssh/config. It is passed straight to ssh.
  • -o Option=value passes any ssh_config option, -i picks a key, and -J sets a jump host, as in ssh.
  • The colon matters. scp report.txt prod copies to a local file named prod. Write prod: 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 -A or sftp -A if the remote side needs your agent.
  • PermitLocalCommand is turned off, so LocalCommand does 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 prod prints the final settings. scp -v and sftp -v show the Reading configuration data lines.
  • First value wins. A Host * block placed above Host prod sets values that the later block cannot change.

Sources

Spotted a mistake? Email [email protected].