How to download a file from server using SSH?

By FoxLearn 12/11/2024 2:16:48 AM   78
To download a file from a server using SSH, you'll typically use SCP (Secure Copy Protocol) or SFTP (SSH File Transfer Protocol).

Both protocols rely on SSH for secure communication and are commonly used for file transfers.

How to download files with ssh?

To download a file from a server to your desktop using SSH on macOS (via iTerm 2), follow these steps:

Open iTerm2 Terminal, then run the SCP command from your local machine (Mac OS X):

1
scp your_username@remotehost.edu:foobar.txt /local/dir

Replacing the username, host, remote filename, and local directory as appropriate.

If you want to access EC2 (or other service that requires authenticating with a private key), use the -i option:

1
scp -i key_file.pem your_username@remotehost.edu:/remote/dir/foobar.txt /local/dir

For example, Using SFTP (SSH File Transfer Protocol)

SFTP allows more flexibility and is often used for interactive file transfers. Here's how you can use SFTP to download a file:

Open your terminal/command line on your local machine.

1
sftp username@server

Once connected, use the cd command to navigate to the directory containing the file on the remote server, and the ls command to list the files:

1
2
cd /path/to/remote/file
ls

Next, Download the file using the get command

For example:

1
get sample.txt

Example SFTP:

1
2
3
4
sftp user@192.168.1.10
cd /home/user/files
get sampletxt
exit

Make sure you have SSH access to the server and the necessary permissions to read the file you're downloading.

If the server uses a non-standard SSH port (not port 22), you can specify the port with the -P flag for scp or -oPort for sftp (e.g., scp -P 2222).

Related