How to RSA key using ssh-keygen

TiTi all in a day’s work
2 min readNov 27, 2020

--

วิธีสร้าง keys ssh-keygen

  1. login to remotehost (server ปลายทาง) ที่จะวาง keys
    สร้าง foder .ssh → สร้าง file authorized_keys → เปลี่ยนสิทธิ์ file authorized_keys เป็น 600
    $ mkdir .ssh
    $ touch .ssh/authorized_keys
    $ chmod 700 .ssh/
    $ chmod 600 .ssh/authorized_keys
  2. local notebook (เครื่องเราเอง)
    สร้าง keys แบบ RSA
    $ cd .ssh/
    $ ssh-keygen -t rsa -b 4096 -f <remote hostname>
    or
    $ ssh-keygen -o -a 100 -t ed25519 -f <remote hostname>
  3. ใส่ password เสร็จแล้ว copy file <remote hostname>.pub ไปวางที่เครื่อง remote host
    $ scp .ssh/<remote hostname>.pub username@remotehost:/home/username/.ssh/
  4. กลับมาที่ remote host อีกครั้ง เพื่อจัดการ keys ให้เข้าระบบ
    $ cd .ssh/
    $ ls
  5. จะต้องมี file <remote hostname>.pub ถ้ามีแล้วก็คัดลอก keys เข้าสู่ระบบ
    $ cat <remote hostname>.pub >> authorized_keys
  6. ลบไฟล์ <remote hostname>.pub ทิ้ง
    $ rm <remote hostname>.pub
  7. เสร็จ logout
    $ exit

How to RSA key using ssh-keygen

remote host : destination server
local host : your computer

A. For 1–5 key

ssh to remote host

  1. create folder .ssh in your home directory
    $ mkdir ~/.ssh
  2. create file authorized_keys in .ssh folder
    $ touch ~/.ssh/authorized_keys
  3. change permission file authorized_keys to 600 (-rw — — — -)
    $ chmod 600 ~/.ssh/authorized_keys

create RSA keys on your local host

  1. create RSA keys in .ssh folder
    $ cd ~/.ssh/ && ssh-keygen -t rsa -b 4096 -f <remote hostname>
  2. enter your passphrase and retype same passphrase
    Generating public/private rsa key pair.
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
  3. create RSA keys completed. 2 RSA file exist in .ssh folder
    <remote hostname>
    <remote hostname>.pub

copy public key to remote host

  1. copy file <remote hostname>.pub to remote host
    $ scp ~/.ssh/<remote hostname>.pub <username>@<remote hostname>:~/.ssh/

at remote host and manage keys for ssh auth by RSA

  1. if file <remote hostname>.pub exists in this folder. than copy to system
    $ cd ~/.ssh/ && ls -l
  2. copy key to system
    $ cat <remote hostname>.pub >> authorized_keys
  3. remove <remote hostname>.pub
    $ rm <remote hostname>.pub
  4. logout from remote host
    $ exit

use this command instead of (3.) and (4.)

  1. ssh-copy-id will copy specific id to your host and append to authorized_keys
    $ ssh-copy-id -i <remote hostname>.pub <username>@<remote hostname>

Multiple SSH Keys settings for different github account

  1. create config file in ~/.ssh/
    $ vi ~/.ssh/
  2. file example for each host setting
    #comment account name
    Host <remote hostname>
    HostName <remote hostname>
    User <username>
    IdentityFile ~/.ssh/<remote hostname>

Other Command

  1. delete all cached keys
    $ ssh-add -D
  2. check your saved keys
    $ ssh-add -l

ref1: https://medium.com/risan/upgrade-your-ssh-key-to-ed25519-c6e8d60d3c54
ref2: https://www.ssh.com/ssh/keygen/
ref3: https://docs.gitlab.com/ee/ssh/README.html#common-steps-for-generating-an-ssh-key-pair

--

--

TiTi all in a day’s work
TiTi all in a day’s work

Written by TiTi all in a day’s work

Innovation life blend it like latte! Programmer, System Engineer, System Integrator, Network Engineer

No responses yet