Copying files from server to local computer using SSH

Learn copying files from server to local computer using ssh with practical examples, diagrams, and best practices. Covers ssh, scp development techniques with visual explanations.

Copying Files from a Remote Server to Your Local Machine using SSH

Hero image for Copying files from server to local computer using SSH

Learn how to securely transfer files from a remote server to your local computer using SSH-based tools like scp and sftp.

Transferring files between a remote server and your local machine is a fundamental task for developers, system administrators, and anyone working with remote data. While many methods exist, using SSH (Secure Shell) provides a robust and secure way to perform these transfers. This article will guide you through the most common and effective SSH-based tools: scp (Secure Copy Protocol) and sftp (SSH File Transfer Protocol).

Understanding SSH for File Transfers

SSH is a cryptographic network protocol for operating network services securely over an unsecured network. Beyond providing secure remote access via a shell, SSH also underpins secure file transfer utilities. Both scp and sftp leverage the encryption and authentication mechanisms of SSH, ensuring that your data remains confidential and integral during transit.

flowchart TD
    A[Local Computer] --> B{SSH Connection}
    B --> C[Remote Server]
    C --> D[Remote File]
    D -- scp/sftp --> B
    B --> E[Local Destination]
    subgraph Security
        B -- Authentication --> F(Keys/Password)
        B -- Encryption --> G(Data Protection)
    end
    F & G --> B

Overview of SSH-based file transfer process

Method 1: Using scp for Quick Transfers

scp is a command-line utility that allows you to securely copy files and directories between hosts on a network. It uses SSH for data transfer and provides the same authentication and security as SSH. scp is generally preferred for simple, one-off file transfers due to its straightforward syntax.

1. Copying a Single File

To copy a single file from a remote server to your local machine, use the following syntax. Replace username, remote_host, remote_path, and local_path with your specific details.

2. Copying a Directory Recursively

To copy an entire directory and its contents, you need to add the -r (recursive) option to the scp command.

3. Specifying a Different SSH Port

If your SSH server is listening on a non-standard port (not 22), you can specify it using the -P flag (note the uppercase 'P').

scp username@remote_host:/remote/path/to/file.txt /local/path/to/destination/
scp -r username@remote_host:/remote/path/to/directory/ /local/path/to/destination/
scp -P 2222 username@remote_host:/remote/path/to/file.txt /local/path/to/destination/

Examples of scp commands for file and directory transfers

Method 2: Using sftp for Interactive Sessions

sftp provides an interactive command-line interface similar to a traditional FTP client, but it operates over SSH. This makes it ideal for more complex tasks like browsing remote directories, creating directories, deleting files, and transferring multiple files in a single session. It's particularly useful when you're unsure of the exact file paths or need to perform several operations.

1. Initiate an SFTP Session

Open your terminal and connect to the remote server using the sftp command.

2. Navigate Remote and Local Directories

Once connected, you can use ls to list remote files, cd to change remote directories, lls to list local files, and lcd to change local directories.

3. Download Files

Use the get command to download files from the remote server to your local machine. You can specify a local destination path if different from your current local directory.

4. Upload Files (Optional)

While this article focuses on downloading, you can also upload files using the put command.

5. Exit SFTP

When you're done, type exit or bye to close the sftp session.

sftp username@remote_host
# Once connected:
ls
cd /remote/path/to/files/
lls
lcd /local/destination/
get remote_file.txt
get -r remote_directory/
put local_file.txt
exit

Common sftp commands for interactive file transfers