Connect with SSH on Shared Hosting
Tags: tutorial, devops, beginners, productivity
Published: 2022-06-18 11:47:44

Using FTP account with FileZilla is cool but sometimes you need to run commands like `php artisan` or `npm run build`, which is not possible with FTP so let's connect you to your server with SSH using terminal.

I will be using Git Bash for Windows, but the process will work with Linux and Mac.

## Create SSH key

Let's starting with creating a new key. Go to root level with command below in your terminal.

```bash
cd ~
```

Now create a new SSH key.

```bash
ssh-keygen
```

You will prompted for with some questions:
1. name of key
2. location of key
3. passphrase

The default name of the key is `id_rsa` and if this is your first time with ssh keys then don't change the default name otherwise you'll need to add the key using `ssh-agent`. Keep the default location and empty passphrase.

Here's the output of above command.

```
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Arvind/.ssh/id_rsa): id_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in id_rsa
Your public key has been saved in id_rsa.pub
The key fingerprint is:
SHA256:XduoVWQCUyPfQejGMynGljsajMSfZeJz1aV+u7tiYC4
The key's randomart image is:
+---[RSA 3072]----+
|            +o*oo|
|             * *.|
|    .     + * o o|
|     o . O * X  .|
|    . = S * + + o|
|     . B * . . + |
|        B o . o  |
|       E . o .   |
|          . .++  |
+----[SHA256]-----+
```

Two keys are created, private key (id_rsa) and public key (id_rsa.pub).

## Connect Public key to Server

Every shared hosting server has different user interface so try to find **SSH Access** in your cPanel then look for place to import/add key. Then copy the text content of public key which has extension .pub (id_rsa.pub file) and paste in the public key section of import/add key input area.

Now there will be an option to "authorize key", look for it and authorize your public key.

## Connect with username and host

In your cPanel somewhere you can find your username and hostname. If you can't find it then contact your hosting chat support.
Use the following format with username and host to start connection. Hostname is also written as "Shared IP Address" in some hosting services.

```bash
ssh username@host
```

Here's an example

```bash
ssh arvind@126.291.80.81
```

When connecting first time with ssh, you will be prompted with following message. There is a confirmation to add the connection to known hosts, accept that by typing `yes`.

```
The authenticity of host '126.291.80.81 (126.291.80.81)' can't be established.
ED25519 key fingerprint is SHA256:xr7uFgj37m4f+RoKKITfSN3DNwkDcYKSs+yD7GxisQA.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '126.291.80.81' (ED25529) to the list of known hosts.
Last login: Sat Jun 18 00:57:25 2022 from 47.9.69.184
arvind@126.291.80.81 [~]#
```

If you see the message like above with login timestamp, then congrats you connected to server. Now you can run commands on server just like you do locally.

In next article we will install Laravel on shared hosting server and learn some other things like:
1. install latest composer
1. set latest php alias for composer
1. create github like repo base so there's no need to store code on github
1. and some other basics.

If you get stuck at any step then leave a comment here and I'll try to answer with my experience, or you can try a google search.