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
ssh username@remote_server_ipExample:
ssh admin@192.168.1.100To connect to a server using a specific port (if not using the default port 22):
ssh -p port_number username@remote_server_ipExample:
ssh -p 2222 admin@192.168.1.100Using SSH Keys for Authentication
If you have an SSH key for passwordless authentication:
ssh -i /path/to/private_key username@remote_server_ipExample:
ssh -i ~/.ssh/id_rsa admin@192.168.1.100Step 2: Transferring Files Using SCP
SCP allows secure file transfers between hosts over an SSH connection.
Transferring a Single File to Remote Server
scp /path/to/local/file username@remote_server_ip:/path/to/remote/directory/Example:
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:
scp -r /path/to/local/directory username@remote_server_ip:/path/to/remote/directory/Example:
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:
scp username@remote_server_ip:/path/to/remote/file /path/to/local/directory/Example:
scp admin@192.168.1.100:/home/admin/logs/server.log ~/Downloads/Advanced SCP Options
Preserving file attributes:
scp -p /path/to/local/file username@remote_server_ip:/path/to/remote/directory/Limiting bandwidth usage (e.g., to 1000 Kilobits/second):
scp -l 1000 /path/to/local/file username@remote_server_ip:/path/to/remote/directory/Compress data during transfer:
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:
sftp username@remote_server_ipSFTP commands:
put localfile- Upload a fileget remotefile- Download a filels- List directory contentscd directory- Change directorypwd- Show current directorymkdir directory- Create directoryexit- Close the connection
Step 4: Using SSH and SCP in the Same Session
Method 1: Use SCP After SSH Session
First, connect via SSH:
bashssh admin@192.168.1.100Complete your SSH tasks, then exit the SSH session:
bashexitUse SCP to transfer files:
bashscp ~/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:
# 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:
ssh -L 8000:localhost:8000 username@remote_server_ipThen 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.
bashchmod 600 ~/.ssh/id_rsa # For key-based authenticationConnection Timed Out: Check network connectivity and firewall settings.
bashping remote_server_ipHost Key Verification Failed: If you've reconnected to a server with a changed host key:
bashssh-keygen -R remote_server_ip"Load key '/path/to/key': error in libcrypto": This error typically occurs due to:
- Incorrect key format or corruption: The SSH key file may be corrupted or in an unsupported format
- OpenSSL library issues: There might be a mismatch between the OpenSSL version used to create the key and the one trying to read it
- 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
- Use SSH keys instead of passwords for improved security
- Keep your SSH client and server software updated
- Consider using rsync for synchronizing directories with many filesbash
rsync -avz ~/local/directory/ username@remote_server_ip:/remote/directory/ - 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.