How to download a file from a server using the terminal

By

Learn how to download a file from a remote server using the terminal with the scp command, passing your SSH key and the path to copy it to your machine.

~~~

I’ve had the need to transfer a file I had on a server.

I was connected through SSH and I didn’t want to setup the SFTP connection (and I didn’t know if the SFTP connection was allowed), so I opened another terminal window and used the command scp.

Here’s the syntax:

scp -i ~/.ssh/yourkey user@example.com:/path/to/server.txt ./

Replace user@example.com, the remote path, and the SSH key path with your own values.

This will download the file in the same folder you are in, because I specified ./. You can change it to any folder on your computer.

You can copy a directory recursively with -r:

scp -r -i ~/.ssh/yourkey user@example.com:/path/to/folder ./

Run scp from your local terminal, not from the SSH session on the remote server. If your key is already configured in ~/.ssh/config, you can usually omit the -i option.

If you’re SSHing into servers regularly, I built a free SSH hardening checklist that helps you lock down sshd_config.

Tagged: CLI · All topics
~~~

Related posts about cli: