Apps Artificial Intelligence CSS DevOps Go JavaScript Laravel Linux MongoDB MySQL PHP Python Rust Svelte Vue

Uploading and Downloading Files over SSH on Linux

1 min read .
Uploading and Downloading Files over SSH on Linux

SSH is commonly used for remote shell access, but the same secure connection can also transfer files. The scp command provides a straightforward way to copy individual files or directories between a local machine and an SSH server.

1. Prerequisites

Before using scp, make sure:

  • You can connect to the server with SSH.
  • You know the remote username and hostname or IP address.
  • The destination path is writable by that user.
  • Your SSH key or other authentication method is configured.

2. Upload a File

The general form is:

scp local_file user@remote_host:/remote/directory/

For example:

scp example.txt user@example.com:/home/user/

This copies example.txt from the current machine to /home/user/ on the remote host.

3. Download a File

Reverse the source and destination:

scp user@example.com:/home/user/example.txt ./

The final ./ means the file is saved in the current local directory.

4. Copy a Directory

Use -r for recursive directory copies.

Upload a directory:

scp -r local_directory/ user@example.com:/srv/data/

Download a directory:

scp -r user@example.com:/srv/data/project/ ./project/

For large directory trees or repeated synchronization, rsync over SSH is often more efficient because it can transfer only changed data and resume partial work more effectively.

5. Use a Custom SSH Port

scp uses uppercase -P for the remote port:

scp -P 2222 example.txt user@example.com:/home/user/

If you frequently use the same host and port, configure them in ~/.ssh/config so both ssh and scp can reuse the settings.

6. Use a Specific SSH Key

scp -i ~/.ssh/id_ed25519 example.txt user@example.com:/home/user/

Protect private keys with appropriate filesystem permissions and never commit them to a repository.

7. Modern Alternative: sftp

For interactive transfers, SSH also provides SFTP:

sftp user@example.com

Inside the SFTP prompt, commands such as put, get, ls, and cd are available.

Conclusion

scp is a convenient tool for secure one-off file transfers over SSH. Use -r for directories, -P for a custom port, and an SSH configuration file to keep repeated connections simple. For large or recurring synchronizations, consider rsync over SSH instead.

Related Posts

chevron-up