How to RSA key using ssh-keygen
วิธีสร้าง keys ssh-keygen
- 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 - 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> - ใส่ password เสร็จแล้ว copy file <remote hostname>.pub ไปวางที่เครื่อง remote host
$ scp .ssh/<remote hostname>.pub username@remotehost:/home/username/.ssh/ - กลับมาที่ remote host อีกครั้ง เพื่อจัดการ keys ให้เข้าระบบ
$ cd .ssh/
$ ls - จะต้องมี file <remote hostname>.pub ถ้ามีแล้วก็คัดลอก keys เข้าสู่ระบบ
$ cat <remote hostname>.pub >> authorized_keys - ลบไฟล์ <remote hostname>.pub ทิ้ง
$ rm <remote hostname>.pub - เสร็จ 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
- create folder .ssh in your home directory
$ mkdir ~/.ssh - create file authorized_keys in .ssh folder
$ touch ~/.ssh/authorized_keys - change permission file authorized_keys to 600 (-rw — — — -)
$ chmod 600 ~/.ssh/authorized_keys
create RSA keys on your local host
- create RSA keys in .ssh folder
$ cd ~/.ssh/ && ssh-keygen -t rsa -b 4096 -f <remote hostname> - enter your passphrase and retype same passphrase
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again: - create RSA keys completed. 2 RSA file exist in .ssh folder
<remote hostname>
<remote hostname>.pub
copy public key to remote host
- 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
- if file <remote hostname>.pub exists in this folder. than copy to system
$ cd ~/.ssh/ && ls -l - copy key to system
$ cat <remote hostname>.pub >> authorized_keys - remove <remote hostname>.pub
$ rm <remote hostname>.pub - logout from remote host
$ exit
use this command instead of (3.) and (4.)
- 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
- create config file in ~/.ssh/
$ vi ~/.ssh/ - file example for each host setting
#comment account name
Host <remote hostname>
HostName <remote hostname>
User <username>
IdentityFile ~/.ssh/<remote hostname>
Other Command
- delete all cached keys
$ ssh-add -D - 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