โดย mindphp » 02/08/2019 3:39 pm
บทความนี้ที่จะสอนคือสร้าง Public/Private key สำหรับ ลดขั้นตอนการทำ rsync ข้าม Server แล้วต้องมากรอกรหัสผ่าน
เมื่อทำตามขั้นตอนนี้ เสร็จ เราสามารถ login ข้ามเครื่อง ด้วย ssh โดยไม่ต้องใช้ user, password และสามารถนำมาร่วมกับ
rsync ตามบทความที่ได้นำเสนอไปก่อนหน้านี้ที่นี่โดยไม่ต้องคอยกรอกรหัสผ่านทุกครั้ง
ขั้นตอน การสร้าง Public/Private key ทำได้ง่ายๆ เพียงสองขั้นตอน
1. Login เข้า Server ต้นทางด้วย
ssh ปกติ แล้วใช้คำสั่ง สร้าง Public/Private Key ดังนี้
จะได้ผลประมาณนี้
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.
The key fingerprint is:
SHA256:N8n79gdhujTvEb3o9e/eNugfQ67YQ0DvNOjnWbJfuSI
[email protected]
The key's randomart image is:
+---[RSA 4096]----+
| |
| . |
2. เอา Public Key ที่ได้จากข้อ 1 ไปไว้ที่ Server ปลายทาง ด้วย FTP หรือ ssh ก็ได้ ในบทความนี้จะใช้ ssh อัพโหลดไฟล์ Public Key ไปด้วยคำสั่ง
โค้ด: เลือกทั้งหมด
ssh-copy-id -i /root/.ssh/id_rsa.pub -p [port] user@[ip เครื่องปลายทาง]
เท่านี้ ครั้งต่อไปเวลาเรา Login เครื่องปลายทาง หรือ rsync ด้วย ssh ก็ไม่ต้อง กรอก user password อีกต่อไป
บทความนี้ที่จะสอนคือสร้าง Public/Private key สำหรับ ลดขั้นตอนการทำ rsync ข้าม Server แล้วต้องมากรอกรหัสผ่าน
เมื่อทำตามขั้นตอนนี้ เสร็จ เราสามารถ login ข้ามเครื่อง ด้วย ssh โดยไม่ต้องใช้ user, password และสามารถนำมาร่วมกับ [url=https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1/66-server-hosting/3221-rsync-backup-shh.html]rsync ตามบทความที่ได้นำเสนอไปก่อนหน้านี้ที่นี่[/url]โดยไม่ต้องคอยกรอกรหัสผ่านทุกครั้ง
ขั้นตอน การสร้าง Public/Private key ทำได้ง่ายๆ เพียงสองขั้นตอน
1. Login เข้า Server ต้นทางด้วย [url=https://www.mindphp.com/%E0%B8%84%E0%B8%B9%E0%B9%88%E0%B8%A1%E0%B8%B7%E0%B8%AD/73-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3/3329-%E0%B8%97%E0%B8%B3%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1%E0%B8%A3%E0%B8%B9%E0%B9%89%E0%B8%88%E0%B8%B1%E0%B8%81%E0%B8%81%E0%B8%B1%E0%B8%9A-ssh-secure-shell.html]ssh[/url] ปกติ แล้วใช้คำสั่ง สร้าง Public/Private Key ดังนี้
[code]ssh-keygen -t rsa -b 4096[/code]
จะได้ผลประมาณนี้
[quote]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.
The key fingerprint is:
SHA256:N8n79gdhujTvEb3o9e/eNugfQ67YQ0DvNOjnWbJfuSI
[email protected]The key's randomart image is:
+---[RSA 4096]----+
| |
| . |
[/quote]
2. เอา Public Key ที่ได้จากข้อ 1 ไปไว้ที่ Server ปลายทาง ด้วย FTP หรือ ssh ก็ได้ ในบทความนี้จะใช้ ssh อัพโหลดไฟล์ Public Key ไปด้วยคำสั่ง
[code]ssh-copy-id -i /root/.ssh/id_rsa.pub -p [port] user@[ip เครื่องปลายทาง][/code]
เท่านี้ ครั้งต่อไปเวลาเรา Login เครื่องปลายทาง หรือ rsync ด้วย ssh ก็ไม่ต้อง กรอก user password อีกต่อไป
[code]ssh -p [port] user@[ip เครื่องปลายทาง][/code]