หน้า 1 จากทั้งหมด 1

เทคนิคการ SSH ระหว่าง CentOS สองเครื่องโดยไม่ใช้พาสเวอร์ด

โพสต์โพสต์แล้ว: 31 ม.ค. 2016 22:57
โดย nuiz
พอดีผมกำลังจะทำ Backup บน FreePBX/Elastix โดยใช้วิธีการอัพโหลดข้อมูลผ่านทาง SSH ซึ่งต้องมีขั้นตอนที่คอนฟิกให้ FreePBX เครื่องหลักล๊อกอินเข้าเครื่อง Backup โดยใช้ SSH แบบไม่ต้องใช้พาสเวิร์ดด้วย แล้วอัพโหลดไฟล์เข้าไปเก็บไว้

แต่ในหน้าคอนฟิก Backup/Restore ของ FreePBX ไม่ได้บอกว่าต้องทำแบบนี้ ผมเลยทำบทความนี้ขึ้นมาครับ

ปกติถ้าเราอยู่บน CentOS เครื่องหนึ่ง (สมมติว่าเป็นไอพี 192.168.100.211) แล้วต้องการรีโมทด้วย SSH ไปยัง CentOS อีกเครื่องหนึ่งด้วยยูสเซอร์ root (สมมติว่าเป็นไอพี 192.168.100.212) เราก็พิมพ์คำสั่ง ssh root@192.168.100.212 ดังตัวอย่างครับ

[root@elastix-a ~]# ssh root@192.168.100.212
The authenticity of host '192.168.100.212 (192.168.100.212)' can't be established.
RSA key fingerprint is 21:bd:83:41:c2:a6:11:51:27:10:01:69:5b:b3:0e:e9.
Are you sure you want to continue connecting (yes/no)? yes รีโมทครั้งแรกต้องพิมพ์ yes ครับ ครั้งต่อไปจะไม่ถามแล้ว
Warning: Permanently added '192.168.100.212' (RSA) to the list of known hosts.
Nasty PTR record "192.168.100.212" is set up for 192.168.100.212, ignoring
root@192.168.100.212's password: ใส่พาสเวอร์ด root
Last login: Sun Jan 31 15:26:54 2016 from nuizpc

Welcome to Elastix
----------------------------------------------------

Elastix is a product meant to be configured through a web browser.
Any changes made from within the command line may corrupt the system
configuration and produce unexpected behavior; in addition, changes
made to system files through here may be lost when doing an update.

To access your Elastix System, using a separate workstation (PC/MAC/Linux)
Open the Internet Browser using the following URL:
http://192.168.100.212

[root@elastix-b ~]# พร้อมท์ของเครื่อง 192.168.100.212


วิธีการคอนฟิกมีดังต่อไปนี้ครับ ถ้าติดปัญหาอะไรให้ดูหมายเหตุท้ายบทความนี้ครับ

1. คอนฟิก Hostname ในไฟล์ /etc/hosts
ตั้งชื่อโฮสต์ในไฟล์นี้จะทำให้ผมรีโมทได้ง่ายขึ้น พิมพ์แค่ชื่อ ไม่ต้องพิมพ์ IP Address ครับ ทำทั้งเครื่องหลักและเครื่องแบ็คอัพ

127.0.0.1 elastix-a localhost
192.168.100.211 elastix-a
192.168.100.212 elastix-b

2. ทดสอบ Ping ด้วยชื่อ
เทสทั้งสองเครื่องเลยนะครับ เพื่อความมั่นใจ
[root@elastix-a ~]# ping elastix-b
PING elastix-b (10.10.10.212) 56(84) bytes of data.
64 bytes from elastix-b (192.168.100.212): icmp_seq=1 ttl=64 time=1.26 ms
64 bytes from elastix-b (192.168.100.212): icmp_seq=2 ttl=64 time=0.157 ms
64 bytes from elastix-b (192.168.100.212): icmp_seq=3 ttl=64 time=0.185 ms

3. ทดสอบว่า SSH ด้วยชื่อ Host ได้
บนเครื่อง elastix-a
[root@elastix-a ~]# ssh elastix-b
The authenticity of host 'elastix-b (192.168.100.212)' can't be established.
RSA key fingerprint is 21:bd:83:41:c2:a6:11:51:27:10:01:69:5b:b3:0e:e9.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'elastix-b,192.168.100.212' (RSA) to the list of known hosts.
root@elastix-b's password: ใส่ root พาสเวอร์ด
Last login: Sun Jan 31 22:48:52 2016 from 192.168.100.211

Welcome to Elastix
----------------------------------------------------

Elastix is a product meant to be configured through a web browser.
Any changes made from within the command line may corrupt the system
configuration and produce unexpected behavior; in addition, changes
made to system files through here may be lost when doing an update.

To access your Elastix System, using a separate workstation (PC/MAC/Linux)
Open the Internet Browser using the following URL:
http://192.168.100.212

[root@elastix-b ~]#

เมื่อชัวร์แล้วว่ารีโมทด้วยชื่อได้ ก็พิมพ์ exit ออกมากครับ

4. สร้าง Public Key
ถ้าต้องการให้รีโมทจาก elastix-a ไปยัง elastix-b โดยไม่ต้องใช้ root password ก็ต้องพิมพ์คำสั่งต่อไปนี้บนเครื่อง elastix-a นะครับ
[root@elastix-a ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): กด enter
Enter passphrase (empty for no passphrase): กด enter
Enter same passphrase again: กด enter
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:
38:72:02:20:7b:40:25:57:8e:0d:f1:95:dd:c1:e2:da root@elastix-a

- จะมีไฟล์ 2 ไฟล์สร้างในโฟลเดอร์ /root/.ssh/id_rsa คือไฟล์ id_rsa และ id_rsa.pub ครับ
- บรรทัด passphrase ไม่ต้องใส่นะครับ เพราะถ้าใส่มันจะถามหา passphrase แทน ก็แทบไม่ต่างอะไรกับถามหา password หน่ะครับ

5. รีโมทไปสร้างโฟลเดอร์ /root/.ssh บนเครื่อง elastix-b
[root@elastix-a ~]# ssh elastix-b "mkdir /root/.ssh"
root@elastix-b's password: ใส่ root password

6. ก๊อบไฟล์ /root/.ssh/id_rsa.pub จากเครื่อง elastix-a ไปไว้ในเครื่อง elastix-b
** ตั้งชื่อไฟล์ว่า authorized_keys **
[root@elastix-a ~]# scp /root/.ssh/id_rsa.pub root@elastix-b://root/.ssh/authorized_keys
root@elastix-b's password: ใส่ root password
ผลลัพธ์
id_rsa.pub 100% 396 0.4KB/s 00:00
บนเครื่อง elastix-b จะมีไฟล์ authorized_keys อยู่ในโฟลเดอร์ /root/.ssh

7. ทดสอบ SSH จากเครื่อง elastix-a ไปยัง elastix-b
ถ้าไม่ถูกถามหา password แสดงว่าเวอร์ค
[root@elastix-a .ssh]# ssh elastix-b
Last login: Sun Jan 31 23:29:00 2016 from nuizpc

Welcome to Elastix
----------------------------------------------------

Elastix is a product meant to be configured through a web browser.
Any changes made from within the command line may corrupt the system
configuration and produce unexpected behavior; in addition, changes
made to system files through here may be lost when doing an update.

To access your Elastix System, using a separate workstation (PC/MAC/Linux)
Open the Internet Browser using the following URL:
http://192.168.100.212

[root@elastix-b ~]#

เวอร์คครับ!!! ไม่ต้องใส่ password แล้ว

หมายเหตุ
1. ถ้าต้องการทำกลับกัน คือรีโมทจากเครื่อง elastix-b ไปยัง elastix-a ก็ยังต้องใส่พาสเวิร์ดอยู่นะครับ ถ้าจะไม่ให้ถามพาสเวอร์ดก็ต้องสร้าง Public Key บนเครื่อง elastix-b ก่อน แล้วก๊อบไฟล์ authorized_keys ไปไว้ใน /root/.ssh/ ของเครื่อง elastix-a
[root@elastix-b ~]# ssh-keygen
[root@elastix-b ~]# scp /root/.ssh/id_rsa.pub root@elastix-b://root/.ssh/authorized_keys

2. ตอนรีโมท จะใส่ไอพีก็ได้ ใส่ชื่อก็ได้
[root@elastix-a ~]# ssh elastix-b
[root@elastix-a ~]# ssh 192.168.100.212

3. ในไฟล์ /etc/ssh/sshd_config มี 2 บรรทัดนี้อยู่
#PubkeyAuthentication yes
#AuthorizedKeysFile .ssh/authorized_keys
ไม่ต้องแก้ไข 2 บรรทัดนี้ครับ (หรือจะแก้ไขก็ได้ เอาเครื่องหมาย # ออก ทำไปก็ไม่มีผล)

4. กรณีของ FreePBX ต้องเอาไฟล์ authorized_keys ไปไว้ในโฟลเดอร์ /var/lib/asterisk/.ssh ด้วยนะครับ
สมมติว่าจะให้ FreePBX บนเครื่อง elastix-a อัพโหลดไฟล์ไปยังเครื่อง elastix-b

[root@elastix-b ~]# mkdir -p /var/lib/asterisk/.ssh
[root@elastix-b ~]# cp /root/.ssh/authorized_keys /var/lib/asterisk/.ssh
[root@elastix-b ~]# chown asterisk:asterisk /var/lib/asterisk/.ssh/

บทความที่เกี่ยวข้อง
เทคนิคการแบ็คอัพ FreePBX/Elastix โดยวิธีอัพโหลดผ่านทาง SSH