Skip to main content

Comprehensive Guide to SSH and SCP

Table of Contents

  1. Introduction
  2. Installing SSH and SCP
  3. Using SSH
  4. Using SCP
  5. Common SSH and SCP Options
  6. Conclusion

Introduction

SSH (Secure Shell) and SCP (Secure Copy Protocol) are essential tools for secure remote access and file transfers over a network. This guide provides detailed instructions on how to install, set up, and use SSH and SCP.

Installing SSH and SCP

Ubuntu/Debian

CentOS/RHEL

Fedora

Using SSH

Connecting to a Remote Server

To connect to a remote server using SSH:
Replace username with your actual username and remote_host with the IP address or domain name of the remote server.

Running Commands on a Remote Server

To run a command on a remote server without logging in:

Using SSH Keys for Authentication

Generate an SSH key pair on your local machine:
Copy the public key to the remote server:

SSH Configuration File

You can simplify SSH commands by creating an SSH configuration file (~/.ssh/config):
Now you can connect using:

Tunneling with SSH

To create an SSH tunnel for secure port forwarding:
Replace local_port with the port on your local machine and remote_port with the port on the remote machine.

Using SCP

Copying Files from Local to Remote

To copy files from your local machine to a remote server:

Copying Files from Remote to Local

To copy files from a remote server to your local machine:

Copying Directories Recursively

To copy directories recursively, use the -r option:

Common SSH and SCP Options

SSH Options

  • -p port: Specify the port to connect to on the remote host.
  • -i identity_file: Specify the identity file (private key) to use for authentication.
  • -L local_port:localhost:remote_port: Create an SSH tunnel.
  • -X: Enable X11 forwarding.
  • -C: Enable compression.

SCP Options

  • -P port: Specify the port to connect to on the remote host.
  • -i identity_file: Specify the identity file (private key) to use for authentication.
  • -r: Copy directories recursively.
  • -C: Enable compression.

Conclusion

This guide provides an overview of how to use SSH and SCP for secure remote access and file transfers. By mastering these commands, you can efficiently manage remote servers and transfer files securely over a network.