How to Set Up SSH Keys on Ubuntu 22.04 / 24.04

Step 1 — Login to Your Instance via SSH

ssh -i "KeyName.pem" ubuntu@SERVER_IP or ssh username@SERVER_IP

Step 2 — Generate SSH Key Pair

ssh-keygen -o

Keep all the default settings, by hitting the ENTER button.

Step 3 — Display the SSH Key Pair

cat /home/ubuntu/.ssh/id_rsa.pub # Ubuntu 22.04

cat /home/ubuntu/.ssh/id_ed25519.pub. # Ubuntu 24.04

This is the default SSH location. You can replace the ubuntu username with yours.

Step 4 — Copy The SSH Key Pair Manually

OUTPUT (Ubuntu 22.04):
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCd5AZYRkPnwFWtJ6hroDGRr9r/xtpnUTqhajvSwjDtyj3CvL7DAIpa9egNgFeSlSib3bYMwOl1tWdH0XHckdbHbT/IQ9zfamnMEhl6TiB4odjGMWue1/271eUcZ2qsGU2eZjymIK5t+ueRa4Sc2oYMQ/mlbRqCsi2lvppFPlcADmM2lmfg7FzU3x49M6Vu0XvQg9BzRjlHZBMyN4yGjwo/kfNoSWcDeEoplMbCXXE+djogT8BX9oOi66yZ6ZhI+rmWtHvjrxR7cDI0W8aUqYfDGWmRnaXhOr2OBN4bacaNSG6XeyRUluXwrxnO/2sjhkwQBM13VtuhiFuFOg9UwAtlhcxrMX2y3Llv9xkqj82sq+uEz9oen6EoVxR2kyLYwZzU7dN/elUa4TlY0Z6MPLv/6RxfqZev7p8gI20hvJQtfa36F2PryqU8peCIgcL17rsMgmzkCDlHfpPpulqORKLN5b/0cvwOhPMH4a
VxO9GzwtEi2YdSHtmvx7Mrrw2UM1c= ubuntu@ip-172-31-44-203

OUTPUT (Ubuntu 24.04):
ssh-ed25519 AAAAC3NzaC1lZDI1NTE25AAAAIDN/gKKosOi9UJ+w+wWdWsPC4qhWwZwdi3Vamm5tVCp3 ubuntu@ip-172-31-33-255

Copy from the entire key pair. (ssh-rsa to -203)

Step 5 — Adding the Key Pair to GitHub / GitLab

For adding the key pair to GitHub SSH and GPG keys

GitHub > Settings > SSH and GPG keys > New SSH Key

For adding the key pair to GitLab SSH keys

GitHub > Preferences > SSH Keys > Add new key

That's All, Now you can easily work with git directly from your server.