วิธี SSH ข้ามเซิฟเวอร์โดยไม่ใส่รหัสผ่าน

ยกตัวอย่างเช่น ssh จากเครื่อง A ไปเครื่อง B โดยไม่ใช้รหัสผ่านในการ ssh

@เครื่อง A

ssh-keygen -t rsa
(มีอะไรขึ้นมาก็กด enter ผ่านไปให้หมด แล้วพิมพ์คำสั่งด้านล่าง เพื่อเตรียมก็อบค่าไว้)
cat /root/.ssh/id_rsa.pub

*ควรทำ*แบบดี ๆ หน่อยก็ ssh-keygen -t ed25519 แล้วใส่ passphrase เพื่อกรอกพาสอีกครั้งกันเหนียวเวลา privatekey/putty.ppk หลุด

id_rsa = private key เก็บไว้บน เครื่อง(client)ต้นทาง A
id_rsa.pub = public key ก๊อปไปใส่ เครื่อง(server)ปลายทาง B

@เครื่อง B

vim /root/.ssh/authorized_keys

ก็อบค่า id_rsa.pub ของเครื่อง A มาใส่(หรือต่อท้ายบรรทัดใหม่ กรณีทำ A หลายเครื่อง)

คราวนี้ลอง ssh จากเครื่อง A -> B โดยไม่ต้องใส่พาสก็ได้แล้วล่ะ


+ล๊อคอินแบบไม่ใช้พาส

vim /etc/ssh/sshd_config
เปลี่ยน #PermitRootLogin yes เป็น PermitRootLogin without-password เพื่อใช้ rsa ด้านบนคอนเฟิร์มแม้จะพิมพ์พาส root ถูก
หรือเปลี่ยน PasswordAuthentication yes เป็น no เพื่อบังคับไม่ให้ใช้ password เดิมๆตั้งแต่ล๊อคอินในการเข้าใช้งาน 
ร่วมกับ PubkeyAuthentication yes ก็ใช้พาส passphrase จากตอนเจน privatekey/putty.ppk มายืนยันแทน

หรือจัดเต็ม
PermitRootLogin without-password
PasswordAuthentication no
PubkeyAuthentication yes

ChallengeResponseAuthentication no
UsePAM no //เครื่องที่ใช้อยู่ใช้ yes ก็ทำงานได้
service sshd restart

rsa with allow ip https://unix.stackexchange.com/a/490120

vim /etc/ssh/sshd_config
AllowUsers root@192.168.1.* admin2@192.168.1.* otherid1

vim /root/.ssh/authorized_keys
from="192.168.1.*,192.168.2.*" ssh-rsa AAAAB3NzaC1yc2EAA........

ref: blog.rtsp.us/--9dc83e2aaa90
ref: linuxproblem.org/art_9.html


check ssh login

journalctl -u sshd --since -3d | grep "Accepted password for root"
journalctl -u sshd --since -3d | grep "198.23.148.137"
cat /var/log/auth.log* | grep "Accepted password for"
cat /var/log/secure-* | grep "password changed for"

 July 11, 2011 / updated: February 18, 2026

 Blog | # # #

qrcode
Facebook Facebook X / Twitter Twitter Telegram Telegram Pinterest Pinterest Email Email