Skip to content

Starting an SSH Session and Transferring Files with SCP in Ubuntu

This guide demonstrates how to establish an SSH connection to a remote Ubuntu server and transfer files using SCP (Secure Copy Protocol) within the same SSH session.

Prerequisites

  • Ubuntu operating system (local machine)
  • Access credentials (username and password or SSH key) for the remote server
  • SSH client installed (pre-installed in Ubuntu)

Step 1: Connecting to a Remote Server via SSH

SSH (Secure Shell) allows you to securely connect to a remote server to perform commands.

Basic SSH Connection

bash
ssh username@remote_server_ip

Example:

bash
ssh admin@192.168.1.100

To connect to a server using a specific port (if not using the default port 22):

bash
ssh -p port_number username@remote_server_ip

Example:

bash
ssh -p 2222 admin@192.168.1.100

Using SSH Keys for Authentication

If you have an SSH key for passwordless authentication:

bash
ssh -i /path/to/private_key username@remote_server_ip

Example:

bash
ssh -i ~/.ssh/id_rsa admin@192.168.1.100

Step 2: Transferring Files Using SCP

SCP allows secure file transfers between hosts over an SSH connection.

Transferring a Single File to Remote Server

bash
scp /path/to/local/file username@remote_server_ip:/path/to/remote/directory/

Example:

bash
scp ~/Documents/report.pdf admin@192.168.1.100:/home/admin/documents/

Transferring a Directory to Remote Server

To transfer an entire directory, use the -r (recursive) option:

bash
scp -r /path/to/local/directory username@remote_server_ip:/path/to/remote/directory/

Example:

bash
scp -r ~/Projects/website admin@192.168.1.100:/var/www/

Transferring Files from Remote Server to Local Machine

To download files from the remote server:

bash
scp username@remote_server_ip:/path/to/remote/file /path/to/local/directory/

Example:

bash
scp admin@192.168.1.100:/home/admin/logs/server.log ~/Downloads/

Advanced SCP Options

Preserving file attributes:

bash
scp -p /path/to/local/file username@remote_server_ip:/path/to/remote/directory/

Limiting bandwidth usage (e.g., to 1000 Kilobits/second):

bash
scp -l 1000 /path/to/local/file username@remote_server_ip:/path/to/remote/directory/

Compress data during transfer:

bash
scp -C /path/to/local/file username@remote_server_ip:/path/to/remote/directory/

Step 3: Using SFTP for Interactive File Transfers

For an interactive file transfer session:

bash
sftp username@remote_server_ip

SFTP commands:

  • put localfile - Upload a file
  • get remotefile - Download a file
  • ls - List directory contents
  • cd directory - Change directory
  • pwd - Show current directory
  • mkdir directory - Create directory
  • exit - Close the connection

Step 4: Using SSH and SCP in the Same Session

Method 1: Use SCP After SSH Session

  1. First, connect via SSH:

    bash
    ssh admin@192.168.1.100
  2. Complete your SSH tasks, then exit the SSH session:

    bash
    exit
  3. Use SCP to transfer files:

    bash
    scp ~/Documents/config.json admin@192.168.1.100:/etc/myapp/

Method 2: Use SSH to Execute SCP Remotely

While connected via SSH, you can initiate file transfers from the remote server:

bash
# From local machine
ssh admin@192.168.1.100

# Now on remote server, pull files from another server
scp user@another-server:/path/to/file /path/on/current/server/

# Or push files to another server
scp /local/path/file user@another-server:/remote/path/

Method 3: Using SSH Tunneling for File Transfer

You can create an SSH tunnel and perform file operations through it:

bash
ssh -L 8000:localhost:8000 username@remote_server_ip

Then use a web browser or other tools to access localhost:8000 for file operations.

Troubleshooting

  • Permission Denied: Ensure you have the correct permissions on both the source and destination.

    bash
    chmod 600 ~/.ssh/id_rsa  # For key-based authentication
  • Connection Timed Out: Check network connectivity and firewall settings.

    bash
    ping remote_server_ip
  • Host Key Verification Failed: If you've reconnected to a server with a changed host key:

    bash
    ssh-keygen -R remote_server_ip
  • "Load key '/path/to/key': error in libcrypto": This error typically occurs due to:

    1. Incorrect key format or corruption: The SSH key file may be corrupted or in an unsupported format
    2. OpenSSL library issues: There might be a mismatch between the OpenSSL version used to create the key and the one trying to read it
    3. Permissions issues: The key file permissions may be too open

    Solutions:

    bash
    # Fix permissions (most common solution)
    chmod 600 ~/.ssh/id_rsa
    
    # Check the key format
    ssh-keygen -l -f ~/.ssh/id_rsa
    
    # If needed, regenerate the key
    mv ~/.ssh/id_rsa ~/.ssh/id_rsa.old
    ssh-keygen -t rsa -b 4096
    
    # Ensure correct OpenSSL libraries are installed
    sudo apt update
    sudo apt install --reinstall openssl libssl-dev

Best Practices

  1. Use SSH keys instead of passwords for improved security
  2. Keep your SSH client and server software updated
  3. Consider using rsync for synchronizing directories with many files
    bash
    rsync -avz ~/local/directory/ username@remote_server_ip:/remote/directory/
  4. Use SSH config file to simplify connections:
    bash
    # In ~/.ssh/config
    Host myserver
        HostName 192.168.1.100
        User admin
        Port 22
        IdentityFile ~/.ssh/id_rsa
    
    # Then connect with:
    ssh myserver
    # Or transfer files with:
    scp myfile.txt myserver:/remote/path/

With these commands and techniques, you can efficiently manage remote server connections and file transfers in Ubuntu.