SSH Tutorial With Command Examples

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/ssh-tutorial-command-examples/
PINTEREST
PINTEREST
INSTAGRAM

SSH is the one of the most popular tool in the Linux and Unix world. SSH or Secure SHel as its name suggests creates secure shell connections to the remote systems. Most  of the Linux system administrators prefer SSH to manage remotely. SSH creates encrypted channels to the remote system and transmits data through this secure channels.

Syntax

Basic ssh syntax is like below.

Connect To Remote

Simple usage of ssh is just providing hostname or IP address of remote system and connect. User name is supplied from current user. Simply current users username is used for remote system.

Connect To Remote
Connect To Remote

Specify Username Explicitly

In previous example username is supplied by the session. What is we need to use different username for the remote connection? Adding username to the remote system IP address or hostname with @ sign like below can solve this.

Specify Username
Specify Username

OR

We can provide the username with -l  parameter like below.

Specify Username
Specify Username

Specify Port Number

Ssh protocol uses TCP port 22 by default. Ssh clients also assumes remote system uses default port number. In some cases the port number can be different than 22 so the remote port number should be provided to the ssh explicitly with -p parameter like below.

Specify Port Number
Specify Port Number

Create Public Private Keys With SSH Keygen

SSH is very advanced and feature-full protocol. SSH provides different protocols for authentication. Default one is password based authentication as we previously did. Key based authentication is more secure than password based authentication. To use key based security public and private keys should be created for the user.

LEARN MORE  How To List MySQL/MariaDB Databases

During key generation process some questions are asked. Here are steps.

  • By default keys are stored in users home directory. The exact path is /home/ismail/.ssh/ and keys are named id_rsa.pub, id_rsa . If we want to change default values we can provide them if not justpress enter and skip.

  • Keys can be protected by encypting them. Especially private key named id_rsa is important. If we want to encrypt then enter password, if not just press enter and skip.

Key Based Authentication

We have previously created our keys. We can use these keys to automatically authenticate without providing any password. First we should deploy our public key to the remote system. Deployment is easy because of SSH tool named ssh-copy-id . Remote username and host provided like below. Our public key is deployed to the remote users authorized keys database. By default our public key is /home/ismail/.ssh/id_rsa.pub

Key Based Authentication
Key Based Authentication

Explicitly Specify Keys

SSH behaivour is by default using keys /home/ismail/.ssh/id_rsa.pub and /home/ismail/.ssh/id_rsa but this is not practical some times. We can provide keys explicitly with the -i parameter like below.

Explicitly Specify Keys
Explicitly Specify Keys

Run Command Remotely

To run single line command on the remote hosts opening new shell and typing command may become a hard work for us. SSH provides ability to run commands remotely without opening interactive bash shell. This will run command ip address show on the remote system.

LEARN MORE  How To Join and Convert Multipart Vmdk Files Into One Qcow2 File?

Run Command Remotely
Run Command Remotely

Explicit Configuration

SSH configuration generally resides on the /etc/ssh/ssh_config or ~/.ssh/config . Alternative configuration can be specified with -F parameter like below.

Explicit Configuration
Explicit Configuration

Port Forwarding

Port forwarding  is useful feature provided by SSH. General definition of port forwarding is tunneling local or remote system ports each other. For example I want to connect google.com from local system through remote system. Port forwarding should be enabled while connecting to the remote system.

Local Port Forwarding

In local port forwarding local port will forwarded to the remote system and then to the destination system host and port.

Local Port Forwarding
Local Port Forwarding

After we have connected to the remote system local port number 2222 is opened in the local system. We can check local port 2222 with the following command.

Local Port Forwarding
Local Port Forwarding

Now if we try to connect local port 2222 this port will be forwarded to the host poftut.com and port 2222

Remote Port Forwarding

Remote port forwarding is the reverse of the local pot forwarding. Hostname provided for the forwarding will be tunelled from remote system through our local system.

$ ssh -R 5900:localhost:5900 192.168.122.22

Remote Port Forwarding
Remote Port Forwarding

In the remote system with IP address 192.168.122.22 port 5900 is opened and this port is forwarded to the local systems port 5900.

Dynamic Port Forwarding

Dynamic port forwarding will use SOCKS which default port number is 1080. But another port number can be used. SOCKS generally used to proxy browsers like Chrome, Firefox, Opera. Proxy traffic will be forwarded to the remote system.

LEARN MORE  Php - Install Php In Linux and Create Development Environment

Dynamic Port Forwarding
Dynamic Port Forwarding

X11 Forwarding

Linux systems uses for GUI X11 server. One of the best feature of SSH is remote application with GUI can be run on local system. Application actually runs on remote system but GUI or X11 protocol is forwarded to the local system and shown like a local application. To enable forwarding provide -X parameter.

X11 Forwarding
X11 Forwarding

OR

Just with a single shot

X11 Forwarding
X11 Forwarding

Compression

SSH can save from bandwidth and network usage by compressing its network traffic. Compression can provide benefits is the data is high compression rate like text. Compression can be enabled per session with -C parameter.

Compression
Compression

Specify Source Address

Some local systems may have multiple interfaces and/or multiple IP addresses. This can be a problem if remote system have IP address based network restrictions. Setting specific source IP address can over come this problem.

Specify Source Address
Specify Source Address

Log File

SSH client can produce logs. Normally these logs will be printed out to the console. But they can be written to a file with -E option.

Verbose Mode and Debug

Some times we can have trouble to connect remote system with SSH. In this situations the best solution is to debug connection steps. SSH client can provide verbose and debug output with -v . Adding more like -vvv will increase debug level and output.

Verbose Mode and Debug
Verbose Mode and Debug

Version

To display SSH client version and exit use option -V

Version
Version
RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/ssh-tutorial-command-examples/
PINTEREST
PINTEREST
INSTAGRAM

2 Replies to “SSH Tutorial With Command Examples

Leave a Reply

Your email address will not be published.