SSH (Secure Shel)
Published: 2022-09-26 16:15:43


**A. What Is SSH?**
Secure Shell (SSH) is an adminnistrative protocol that allows users to access and modify various settings and files on the server that can run command bassed on the command line interface (CLI).

**B. How SSH Works**
The way the SSH protocol works is by implementing a client-server model. The connection that occurs is the SSH client (the computer used by the user) connects to the SSH server (the destination remote server).The SSH process starts from the client that connects and uses the key (SSH Key) to verify the SSH server. To implement the SSH protocol can use the command _"ssh username@server_address"_
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wfkhsdwl4gvyrsplx1jc.png)SSH client and SSH server can connect using **the same key**, as long as the key used by the SSH client is not the same as the SSH server, the connection will never be able to connect.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/38w6jl2703luzbnx2ymd.png)When client enters password which is not the same as SSH server then **permission is denied** then try asking for correct password again to be able to connect with server.

**C. Identifying Remote Users**
To show which user is logged in using ssh can use the **w** command.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fawlxswd8mnmvpi95mbh.png)

**D. SSH Host Keys**
When using SSH protocol There are 2 types of SSH Key. The first is _**the Public Key**_ stored on the SSH server and the second is **_the Private Key_** stored on the SSH client.

**1. Generating SSH Keys**
To create a private key and matching public key for authentication, use the **ssh-keygen** command.  ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ozf3v30xz5ae0um56gr.png)Notes :
The public key is stored in _/home/user/.ssh/id_rsa.pub_
The private key is stored in _/home/user/.ssh/id_rsa_
**2. SSH Public Key Tutorial**

```
ssh-keygen
```
When prompted for information on where to save the SSH file, just press ENTER on the keyboard.Open the public key with the **cat** command.

```
cat /home/user/.ssh/id_rsa.pub
```
Copy the contents of id_rsa.pub to the user and follow the destination server.

```
ssh-copy-id user@server_address
```
While running the above command, you will be prompted for the password for the user. Then try logging in again, and you can log in to the server without needing to enter a password.

```
ssh user@server_address
```

**3. SSH Private Key Tutorial**

```
ssh-keygen -f  ~/.ssh/encrypted_rsa

or can use command

ssh-keygen -f .ssh/key-with-pass
```
The -f option with the ssh-keygen command determines the files where the keys are saved. In the preceding example, the private and public keys are saved in the _/home/user/.ssh/key-with-pass_ 
  _/home/user/.ssh/key-with-pass.pub_ files, respectively. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bp8co3gq8do1nadqtvlz.png)Enter your password in the Enter passphrase (empty for no passphrase): and Enter the same passphrase again:, the password you typed will not appear, if you are finished press ENTER.
Copy the new public key to the server.

```
ssh-copy-id -i ~/.ssh/encrypted_rsa.pub user@server_address

or can use

ssh-copy-id -i .ssh/key-with-pass.pub user@server_address
```
Then try to login

```
ssh -i ~/.ssh/encrypted_rsa user@server_address
```
This time you have to enter the password used for encryption encrypted_rsa.

*Private and Public keys will be stored in ~/.ssh, which can be checked with _ls -l ~/.ssh_

```
ls -l ~/.ssh
```