Uploading and Downloading Files via SSH in Linux

1 min read .

Secure Shell (SSH) is a widely used protocol for securely accessing and managing remote servers. In addition to remote command execution, SSH can also be used to upload and download files between your local machine and a remote server. We’ll cover how to upload and download files using SSH in Linux.

Prerequisites

  • SSH Access: Ensure you have SSH access to the remote server. You need the server’s IP address or hostname and your SSH credentials.
  • SSH Client: You should have an SSH client installed on your local machine. Linux and macOS come with ssh pre-installed, while Windows users may use tools like PuTTY or Windows Subsystem for Linux (WSL).

Uploading Files to a Remote Server

To upload files to a remote server via SSH, you can use the scp (Secure Copy) command. Here’s the basic syntax:

scp [local_file_path] [username]@[remote_host]:[remote_directory]
  • local_file_path: Path to the file on your local machine.
  • username: Your SSH username.
  • remote_host: IP address or hostname of the remote server.
  • remote_directory: Path to the directory on the remote server where you want to upload the file.

Example

Upload a file named example.txt from your local machine to the /home/username/ directory on the remote server:

scp example.txt username@remote_host:/home/username/

Downloading Files from a Remote Server

To download files from a remote server, you again use the scp command but reverse the source and destination:

scp [username]@[remote_host]:[remote_file_path] [local_directory]
  • remote_file_path: Path to the file on the remote server.
  • local_directory: Path to the directory on your local machine where you want to save the file.

Example

Download a file named example.txt from the /home/username/ directory on the remote server to your local machine:

scp username@remote_host:/home/username/example.txt /path/to/local/directory/

Uploading and Downloading Directories

To upload or download entire directories, use the -r (recursive) option with scp:

  • Upload a Directory:

    scp -r local_directory username@remote_host:/path/to/remote/directory/
  • Download a Directory:

    scp -r username@remote_host:/path/to/remote/directory/ local_directory

Additional Options

  • Specify a Port: If your SSH server is running on a non-standard port (other than 22), use the -P option:

    scp -P [port_number] local_file username@remote_host:/path/to/remote/directory/
  • Use SSH Key Authentication: If you are using SSH key authentication instead of a password, ensure your key is properly configured.

Conclusion

Using scp is a straightforward way to securely upload and download files between your local machine and a remote server. With these basic commands, you can manage your files efficiently and ensure that your data transfers are secure.

Tags:
Linux

See Also

chevron-up