How To Connect a Remote Server Via SSH
Tags: linux, beginners, tutorial, ubuntu
Published: 2022-01-15 16:47:27


This is a beginner's guide to set up SSH connection on a remote server using *Password Authentication* in Linux.

### What is SSH and Why we need it?

**SSH** stands for **Secure Shell** which is based on client-server protocol, An SSH client program is typically used for establishing secure connections between computers. If you want code execution or transfer files into a remote machine or server in a secure way in the Internet, SSH is the best way to do that. There are many applications of **SSH** like
* Secure remote access to resources
* Delivery of software patches and updates
* Secure File transfer (sftp) etc.

Enough theory Let's jump into the practical part.
### Installation of SSH
Most of the Linux Distribution comes with SSH installed within it but if not we can install OpenSSH server easily using this command 
```
$ sudo apt install openssh-client openssh-server
 ```
*Important:* If you are not in a Debian/Ubuntu based distro change "apt" according to your distribution's package manager.
*Pro-tip:* If you system uses systemD as init system and booted with systemD you can use `systemctl` command. OtherWise Google for specific command.

![Installing OpenSSH server](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ls5p3uxroc1k0pf5qein.png)
## Checking Status of sshd
Let's check SSH daemon is running/active or not in our system, this can be done by this command
```
$ sudo systemctl status sshd
``` 
or
```
$ sudo service ssh status
```
![Checking Status of sshd](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r2m0opk0vghzg3bwohcb.png)

if the service is inactive/dead , start the service using this command 
``` 
$ sudo systemctl start sshd
``` 
or
```
$ sudo service ssh start
```

![Service ssh start](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/licr66iqry28kcfkao3b.png)

### Configuration of ssh
We don't need to deep dive into ssh config just take a look at some important configuration stored in  the `/etc/ssh/sshd_config` file. Open the file in a text editor, i'm using vim
`sudo vim /etc/ssh/sshd_config`
by default, SSH uses port 22

![SSH Config Port 22](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d8hh2ua186ulanylb7wf.png)

If Password Auth is disabled by default , turn it on


![SSH config passwd auth yes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1p8ukua62yqhcii1q1a9.png)

and turn X11 forwarding on

![SSH config X11 yes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kbs6wrrwng7u44gufb4w.png)
then reload the config 
```
$ sudo systemctl restart sshd
```

## Connect SSH into remote Machine
```
$ ssh remote-username@remote-ip
```

![SSH username@ip  in remote](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wg5fzv05rjnvn2eoc05h.png)
It will prompt you to enter password of the machine type the password  & you should be in the server :)

![SSH Sucessful Login](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9eh2gug65qrdb835pds1.png)

incase if you are not familiar with CLI you can find ip by `$ ifconfig` or `$ ip a` command

![ifconfig output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w7nbc9sxxlqs4ydz1lxm.png)

## Commands at a glance
```
$ sudo apt install openssh-client openssh-server
$ sudo service ssh status
$ sudo service ssh start
$ sudo systemctl restart sshd
$ ssh remote-username@remote-ip
$ ifconfig
```