How to download a file from server using SSH?
By FoxLearn Published on Dec 11, 2024 212
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):
scp [email protected]: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:
scp -i key_file.pem [email protected]:/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.
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:
cd /path/to/remote/file ls
Next, Download the file using the get
command
For example:
get sample.txt
Example SFTP:
sftp [email protected] 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
).