You are currently viewing SSH (Secure Shell) Commands: Examples and Syntax

SSH (Secure Shell) Commands: Examples and Syntax

  • Post author:
  • Post category:SSH
  • Post comments:0 Comments
  • Post last modified:February 24, 2024

SSH (Secure Shell) is a protocol that provides a secure way to access and manage remote servers. It allows you to securely log into a remote system over an unsecured network, such as the internet, and execute commands as if you were directly connected to that system. Here are some common SSH commands with examples:

1. Connecting to a Remote Server

Syntax:

ssh username@hostname

Example:

ssh user@example.com

Explanation:

  • ssh: The command to initiate an SSH connection.
  • username: Your username on the remote server.
  • hostname: The hostname or IP address of the remote server.

2. Specifying a Port

Syntax:

ssh -p port_number username@hostname

Example:

ssh -p 2222 user@example.com

Explanation:

  • -p port_number: Specifies the port number to connect to.
  • port_number: The port number of the SSH service on the remote server.

3. Providing a Private Key (Identity File)

Syntax:

ssh -i path_to_private_key username@hostname

Example:

ssh -i ~/.ssh/id_rsa user@example.com

Explanation:

  • -i path_to_private_key: Specifies the path to your private key file.
  • path_to_private_key: The path to the private key file used for authentication.

4. Copying Files with SCP (Secure Copy)

Syntax:

scp source_file username@hostname:destination_path

Example (Copying from Local to Remote):

scp /path/to/local/file.txt user@example.com:/path/to/remote/directory/

Example (Copying from Remote to Local):

scp user@example.com:/path/to/remote/file.txt /path/to/local/directory/

Explanation:

  • scp: The command to securely copy files between hosts.
  • source_file: The path to the local file you want to copy.
  • username@hostname:destination_path: The destination where you want to copy the file.
  • For copying from local to remote, username@hostname refers to the remote server.
  • For copying from remote to local, username@hostname refers to the remote server.

5. Generating SSH Key Pair

Syntax:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

Example:

ssh-keygen -t rsa -b 4096 -C "user@example.com"

Explanation:

  • ssh-keygen: The command to generate an SSH key pair.
  • -t rsa: Specifies the type of key to create (RSA).
  • -b 4096: Specifies the number of bits in the key (4096 for stronger security).
  • -C "your_email@example.com": Provides a comment to help identify the key (usually your email).

6. Listing SSH Keys

Syntax:

ls ~/.ssh

Example:

ls ~/.ssh

Explanation:

  • ls: The command to list files and directories.
  • ~/.ssh: The default directory where SSH keys are stored.

7. SSH Agent

Syntax (Start SSH Agent):

eval $(ssh-agent -s)

Syntax (Add SSH Key to Agent):

ssh-add path_to_private_key

Example (Start SSH Agent):

eval $(ssh-agent -s)

Example (Add SSH Key to Agent):

ssh-add ~/.ssh/id_rsa

Explanation:

  • ssh-agent: A program to hold private keys used for public key authentication.
  • eval $(ssh-agent -s): Starts the SSH agent and outputs commands to set up the SSH_AUTH_SOCK and SSH_AGENT_PID environment variables.
  • ssh-add: Adds a private key to the SSH agent for authentication.

8. Securely Logging Out

Syntax:

exit

Example:

exit

Explanation:

  • exit: The command to log out of the SSH session and close the connection.

9. SSH Config File

Location:

The SSH config file is located at ~/.ssh/config.

Example Configuration:

Host example
  HostName example.com
  User user
  Port 2222
  IdentityFile ~/.ssh/id_rsa

Explanation:

  • Host: Specifies a shorthand name for the remote host.
  • HostName: Specifies the actual hostname or IP address of the remote server.
  • User: Specifies the username to use when connecting.
  • Port: Specifies the port number to connect to.
  • IdentityFile: Specifies the path to the private key file for authentication.

10. SSH Tunneling (Port Forwarding)

Syntax:

ssh -L local_port:remote_host:remote_port user@remote_host

Example:

ssh -L 8080:localhost:80 user@example.com

Explanation:

  • -L local_port:remote_host:remote_port: Specifies the local port, remote host, and remote port for port forwarding.

11. SSH Known Hosts

Location:

The known hosts file is located at ~/.ssh/known_hosts.

Example:

If you connect to a new server, SSH will prompt you to verify the host’s authenticity. Once you accept, the server’s host key will be added to the known_hosts file.

Conclusion

These are some of the commonly used SSH commands with examples. SSH is a powerful tool for securely connecting to and managing remote servers. It provides encryption and authentication mechanisms to ensure secure communication over an untrusted network. Understanding these commands will help you effectively work with remote servers and transfer files securely.

Leave a Reply