---
layout: post
title: "SSH Keys"
description: ""
author: sthope
image: 
categories: [ ssh, ssh keys ]
comments: true
---
Generate a long random password with:
```
cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 64 | head -n 1
```

Replace `very_long_secret` with the long generated password and replace `/opt/.certs/service` with the location for your keys and the name (different name for each key "service")
Default generally is `~/.ssh/id_rsa`, you can omit `-f "/opt/.certs/service"` if you don't want to choose it.
```
ssh-keygen -t rsa -b 4096 -f "/opt/.certs/service" -C "Private Key Comment"
```

Key should be created along also with .pub key

```
eval $(ssh-agent -s)
ssh-add /opt/.certs/service
ssh-copy-id -i /opt/.certs/service USERNAME@IP -p PORT
```
Login one last time using your old username and password and remember to disable them or remove them.
<br>

After that you can login into the host with:
```
ssh -i /opt/.certs/service -p PORT USERNAME@IP
```
<br>

Or you can go little further and create file:
```
sudo nano /etc/ssh/ssh_config.d/myssh.conf
```
<br>
  
With:
```
Host 192.168.1.*
    AddKeysToAgent yes
    IdentityFile /opt/.certs/service
    Port 22

Host service.local 192.168.1.2
    User USERNAME
    HostName 192.168.1.2

### Github.com
# don't forget to add the .pub key into your profile
Host github.com
    User git
    Hostname github.com
    AddKeysToAgent yes
    IdentityFile /opt/.certs/github

### Gitea
# don't forget to add the .pub key into your profile
Host gitea.com
    User git
    Hostname gitea.com
    AddKeysToAgent yes
    IdentityFile /opt/.certs/gitea

### Gitlab
# don't forget to add the .pub key into your profile
Host gitlab.com
    User git
    Hostname gitlab.com
    AddKeysToAgent yes
    IdentityFile /opt/.certs/gitlab
```
Now you should be able to connect using `ssh service` or ssh git clone your repos from respective git repository
Test if it's working with: ```ssh -T git@github.com```