เปิดการใช้งาน ซีเคียวเชล (Secure Shell หรือ SSH) รีโมท linux server โดยไม่ต้องใช้ password

Anuchit Prasertsang - Aug 3 - - Dev Community

เราสามารถรีโมทไปที่ server โดยใช้ ssh ได้ โดยไม่ต้องใช้ password โดยการใช้ ssh key แทน ซึ่งเราสามารถสร้าง ssh key ได้ด้วยคำสั่ง ssh-keygen และสามารถ copy ssh key ไปที่ server ได้ด้วยคำสั่ง ssh-copy-id username@xxx.xxx.xxx.xxx และเราสามารถ config ssh ได้ด้วยการแก้ไขไฟล์ /etc/ssh/sshd_config และ restart ssh ด้วยคำสั่ง systemctl restart sshd

ขั้นตอนที่ต้องทำคือ

รันคำสั่งต่อไปนี้บน server

  1. อัพเดทข้อมูลแพ็คเกจ apt-get update
  2. ติดตั้ง openssh-server apt-get install openssh-server (ถ้ายังไม่ได้ติดตั้ง)
  3. สร้าง ssh key ssh-keygen (ถ้ายังไม่มี)
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub
Enter fullscreen mode Exit fullscreen mode

4.แก้ไขไฟล์ /etc/ssh/sshd_config
PermitRootLogin yes ถ้าต้องการให้ root ล็อกอินได้ (ssh root@xxx.xxx.xxx)
PubkeyAuthentication yes
PasswordAuthentication yes

5.รีสตาร์ท ssh systemctl restart sshd

รันคำสั่งต่อไปนี้บน client

  1. อัพเดทข้อมูลแพ็คเกจ apt-get update
  2. ติดตั้ง openssh-server apt-get install openssh-server (ถ้ายังไม่ได้ติดตั้ง)
  3. สร้าง ssh key ssh-keygen (ถ้ายังไม่มี)
  4. คัดลอก ssh key ไปที่ server ~/.ssh/authroized_keys โดยใช้คำสั่ง ssh-copy-id username@xxx.xxx.xxx
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Enter fullscreen mode Exit fullscreen mode

ครั้งแรกระบบจะถาม password ของ server ครั้งต่อไปจะไม่ถาม password อีก

5.ล็อกเอ้าท์และล็อกอินเข้าไปใหม่ ssh username@xxx.xxx.xxx จะเห็นว่าไม่ถาม password

เพียงเท่านี้เราก็สามารถรีโมท server โดยไม่ต้องใช้ password ได้แล้ว

. . . . . . . . . .
Terabox Video Player