Mastering SCP: Secure File Transfer with OpenSSH

Listen to this Post

SCP (Secure Copy Protocol) is a command-line utility provided by the OpenSSH suite, enabling secure file transfers between a local host and a remote host using the SSH protocol. Below are some practical SCP commands and examples to help you efficiently manage file transfers in a secure manner.

Basic SCP Commands

  1. Copy a file from local to remote host:
    scp /path/to/local/file.txt username@remote_host:/path/to/remote/directory/
    

  2. Copy a file from remote host to local:

    scp username@remote_host:/path/to/remote/file.txt /path/to/local/directory/
    

  3. Copy a directory recursively from local to remote:

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

  4. Copy a directory recursively from remote to local:

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

  5. Copy a file with a specific port (e.g., port 2222):

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

  6. Limit bandwidth usage during transfer (e.g., 1000 KB/s):

    scp -l 1000 /path/to/local/file.txt username@remote_host:/path/to/remote/directory/
    

7. Preserve file attributes (timestamps, modes):

scp -p /path/to/local/file.txt username@remote_host:/path/to/remote/directory/

8. Use a specific SSH key for authentication:

scp -i /path/to/private/key /path/to/local/file.txt username@remote_host:/path/to/remote/directory/

Advanced SCP Commands

  1. Transfer files between two remote hosts via local host:
    scp username1@remote_host1:/path/to/file.txt username2@remote_host2:/path/to/directory/
    

2. Compress files during transfer for faster transmission:

scp -C /path/to/local/file.txt username@remote_host:/path/to/remote/directory/

3. Verbose output for debugging:

scp -v /path/to/local/file.txt username@remote_host:/path/to/remote/directory/

4. Copy files using IPv6 address:

scp /path/to/local/file.txt username@[IPv6_address]:/path/to/remote/directory/
  1. Copy files with a specific cipher (e.g., AES-128):
    scp -c aes128-ctr /path/to/local/file.txt username@remote_host:/path/to/remote/directory/
    

What Undercode Say

SCP is an indispensable tool for securely transferring files between systems, especially in environments where data integrity and confidentiality are paramount. By leveraging SCP, you can ensure that your file transfers are encrypted and protected from potential eavesdropping. The commands provided above cover a wide range of use cases, from basic file transfers to more advanced scenarios involving bandwidth throttling, compression, and cross-host transfers.

In addition to SCP, other tools like `rsync` and `sftp` can also be used for secure file transfers, each with its own set of features and advantages. For instance, `rsync` is particularly useful for synchronizing directories and performing incremental backups, while `sftp` offers an interactive shell for managing files on remote servers.

For those working extensively with Linux, mastering these commands is crucial. Here are some additional Linux commands that complement SCP:

  • Check SSH connection to a remote host:
    ssh -T username@remote_host
    

  • Generate SSH keys:

    ssh-keygen -t rsa -b 4096
    

  • Copy SSH key to remote host for password-less login:

    ssh-copy-id username@remote_host
    

  • Monitor network traffic:

    sudo tcpdump -i eth0
    

  • Check disk usage:

    df -h
    

  • List open files and the processes that opened them:

    lsof
    

  • Search for files by name:

    find /path/to/search -name "filename"
    

  • Archive files using tar:

    tar -czvf archive.tar.gz /path/to/files
    

  • Extract files from a tar archive:

    tar -xzvf archive.tar.gz
    

  • Monitor system processes:

    top
    

  • Check system logs:

    sudo tail -f /var/log/syslog
    

  • Change file permissions:

    chmod 755 filename
    

  • Change file ownership:

    chown username:groupname filename
    

  • Mount a filesystem:

    sudo mount /dev/sdX1 /mnt
    

  • Unmount a filesystem:

    sudo umount /mnt
    

  • Check system uptime:

    uptime
    

  • List all running services:

    systemctl list-units --type=service
    

  • Restart a service:

    sudo systemctl restart servicename
    

  • Check network connectivity:

    ping remote_host
    

  • Trace the route to a remote host:

    traceroute remote_host
    

  • Display network interfaces:

    ip a
    

  • Flush DNS cache:

    sudo systemd-resolve --flush-caches
    

  • Check system memory usage:

    free -h
    

  • Kill a process by PID:

    kill -9 PID
    

  • List all users:

    cut -d: -f1 /etc/passwd
    

  • Add a new user:

    sudo adduser newuser
    

  • Delete a user:

    sudo deluser username
    

  • Change user password:

    passwd username
    

  • Check system kernel version:

    uname -r
    

  • Update package list:

    sudo apt-get update
    

  • Upgrade installed packages:

    sudo apt-get upgrade
    

  • Install a new package:

    sudo apt-get install packagename
    

  • Remove a package:

    sudo apt-get remove packagename
    

  • Search for a package:

    apt-cache search keyword
    

  • Check package information:

    apt-cache show packagename
    

  • List installed packages:

    dpkg --list
    

  • Clean up unused packages:

    sudo apt-get autoremove
    

  • Reboot the system:

    sudo reboot
    

  • Shutdown the system:

    sudo shutdown -h now
    

By integrating these commands into your workflow, you can enhance your productivity and ensure that your systems are running smoothly. Whether you’re managing files, monitoring system performance, or troubleshooting network issues, these commands provide a solid foundation for any IT professional.

For further reading on SCP and related topics, you can visit the official OpenSSH documentation at https://www.openssh.com/. Additionally, the Linux man pages (man scp, man ssh, etc.) are invaluable resources for understanding the full capabilities of these tools.

In conclusion, SCP is a powerful tool that, when used correctly, can significantly streamline your file transfer processes while maintaining the highest levels of security. By combining SCP with other Linux commands and tools, you can create a robust and efficient workflow that meets the demands of modern IT environments.

References:

initially reported by: https://www.linkedin.com/posts/xmodulo_linux-activity-7301976000823726080-PNMe – Hackers Feeds
Extra Hub:
Undercode AIFeatured Image