Youtube

SSH Part-1
SSH part-1
SSH Part-2
SSH part-2

SSH(Secure Shell) is a command line tool to connect to another linux machine and to run some commands on it.

SSH

To install ssh-client

sudo apt install -y openssh-client

To install ssh-server

sudo apt install -y openssh-server

To check sshd service is running or not

sudo systemctl status sshd

Two ways to connect to linux servers using ssh

  • ssh using password
  • ssh using public/private keys

To Enable password

By default in most of the linux servers password authetication for ssh will be disabled

Edit the /etc/ssh/sshd_config and update PasswordAuthentication value from “no” to “yes” If the line is commented, please uncomment

ssh using password

ssh user_name@ip-address

Generating ssh keys in default location

ubuntu@test1:~$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/ubuntu/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/ubuntu/.ssh/id_rsa
Your public key has been saved in /home/ubuntu/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:YDknfQjuZk0dTrUs99HhUsoGNiMIuiDqt+tn6hyncuA ubuntu@test1
The key's randomart image is:
+---[RSA 3072]----+
|     .o ..o*.  o |
|    .. = =oo=.+..|
|.. .  B = = +=...|
|o . .o B . o.... |
|.  .  + S     .  |
|..   o           |
|...o .           |
| Eo.=o           |
|  =O=            |
+----[SHA256]-----+

id_rsa –> Private key
id_rsa.pub –> Public key

ubuntu@test1:~$ cd .ssh/
ubuntu@test1:~/.ssh$ pwd
/home/ubuntu/.ssh
ubuntu@test1:~/.ssh$ ll
total 28
drwx------ 2 ubuntu ubuntu 4096 Apr 26 10:54 ./
drwxr-x--- 4 ubuntu ubuntu 4096 Apr 26 10:37 ../
-rw------- 1 ubuntu ubuntu  400 Apr 26 10:33 authorized_keys
-rw------- 1 ubuntu ubuntu 2602 Apr 26 10:54 id_rsa
-rw-r--r-- 1 ubuntu ubuntu  566 Apr 26 10:54 id_rsa.pub
-rw------- 1 ubuntu ubuntu  978 Apr 26 10:51 known_hosts
-rw-r--r-- 1 ubuntu ubuntu  142 Apr 26 10:51 known_hosts.old

id_rsa

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orized_keys is a special file of shh, which holds the lists of public keys

This file should be located in the target server. Default Path: ~/.ssh/authorized_keys

ssh using public/private key

ssh -i private_key user_name@ip-address

Note: Make sure private key is having only 400 permission

Customizing the ssh

All the ssh configuration can be customized in /etc/ssh/sshd_config file

By defauly ssh process listens on port 22

In /etc/ssh/sshd_config file, we can change the port to 8222

Present

#Port 22

Uncomment and Update the port number to 8222

Port 8222

Restart the ssh service

sudo systemctl restart ssh

Try connecting to server using new port

ssh -p 8222 username@ip_address