SSH Command (with examples)

SSH Command (with examples)

Connect to a remote server

ssh username@remote_host

Motivation: This command allows you to establish a secure shell connection to a remote server. It is useful when you need to remotely access and manage a server.

Explanation:

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

Example output:

Enter passphrase for key '/home/username/.ssh/id_rsa':
Welcome to remote_server!

Connect to a remote server with a specific identity (private key)

ssh -i path/to/key_file username@remote_host

Motivation: This command allows you to connect to a remote server using a specific private key for authentication. It is useful when you have multiple private keys and want to specify which one to use.

Explanation:

  • -i path/to/key_file: Specifies the path to the private key file.
  • username: The username used to authenticate on the remote server.
  • remote_host: The hostname or IP address of the remote server.

Example output:

Welcome to remote_server!

Connect to a remote server using a specific port

ssh username@remote_host -p 2222

Motivation: This command allows you to connect to a remote server using a non-default SSH port. It is useful when the remote server is configured to listen on a different port than the default (port 22).

Explanation:

  • username: The username used to authenticate on the remote server.
  • remote_host: The hostname or IP address of the remote server.
  • -p 2222: Specifies the port number to connect to.

Example output:

Welcome to remote_server!

Run a command on a remote server with a [t]ty allocation

ssh username@remote_host -t command command_arguments

Motivation: This command allows you to run a specific command on a remote server while allocating a TTY (Terminal) for interaction with the remote command. It is useful when you need to execute a command that requires input or user interaction on the remote server.

Explanation:

  • username: The username used to authenticate on the remote server.
  • remote_host: The hostname or IP address of the remote server.
  • -t: Allocates a pseudo-terminal for interaction with the remote command.
  • command: The command to be executed on the remote server.
  • command_arguments: Arguments or options to be passed to the command.

Example output:

[sudo] password for username:
Executing command: ls -l
file1.txt
file2.txt
file3.txt

SSH tunneling: Dynamic port forwarding (SOCKS proxy on localhost:1080)

ssh -D 1080 username@remote_host

Motivation: This command allows you to set up a dynamic port forwarding tunnel, creating a SOCKS proxy on your local machine. It is useful when you want to securely browse the internet or access resources through a remote server.

Explanation:

  • -D 1080: Specifies the local port to listen to for dynamic port forwarding.
  • username: The username used to authenticate on the remote server.
  • remote_host: The hostname or IP address of the remote server.

Example output: N/A

SSH tunneling: Forward a specific port

ssh -L 9999:example.org:80 -N -T username@remote_host

Motivation: This command allows you to forward traffic from a specific local port to a remote destination on a remote server. It is useful when you want to access a service running on the remote server through a local port.

Explanation:

  • -L 9999:example.org:80: Specifies the local port to forward (9999), the remote destination (example.org:80), and the direction of the forwarding (local to remote).
  • -N: Disables the execution of remote commands after establishing the tunnel.
  • -T: Disables pseudo-terminal allocation.

Example output: N/A

SSH jumping: Connect through a jumphost to a remote server

ssh -J username@jump_host username@remote_host

Motivation: This command allows you to connect to a remote server through a jumphost (proxy server). It is useful when direct SSH access to the remote server is not possible or allowed, and you need to hop through an intermediate server.

Explanation:

  • -J username@jump_host: Specifies the jumphost (proxy server) to connect through.
  • username: The username used to authenticate on the remote server.
  • remote_host: The hostname or IP address of the remote server.

Example output:

Welcome to remote_server!

Agent forwarding: Forward the authentication information to the remote machine

ssh -A username@remote_host

Motivation: This command allows you to forward your SSH Agent’s authentication information to the remote machine. It is useful when you need to authenticate to other servers or services from the remote machine using your local SSH keys.

Explanation:

  • -A: Enables SSH agent forwarding.
  • username: The username used to authenticate on the remote server.
  • remote_host: The hostname or IP address of the remote server.

Example output:

Welcome to remote_server!

Conclusion

In this article, we explored various use cases of the SSH command. From establishing simple connections to running commands on remote servers, SSH provides a secure and flexible way of managing remote resources. By understanding and utilizing different SSH options, such as port forwarding, agent forwarding, and jumping through jumphosts, you can efficiently navigate and administer your remote infrastructure.

Related Posts

How to use the command mkfs.ext4 (with examples)

How to use the command mkfs.ext4 (with examples)

The mkfs.ext4 command is used to create an ext4 filesystem inside a partition.

Read More
How to use the command 'protonvpn-cli' (with examples)

How to use the command 'protonvpn-cli' (with examples)

The ‘protonvpn-cli’ command is the official ProtonVPN client for Linux systems.

Read More
How to use the command 'moe' (with examples)

How to use the command 'moe' (with examples)

Moe is a WYSIWYG text editor that is designed for ISO-8859-15 encoded text.

Read More