SSH เป็นหนึ่งในวิธียอดนิยมควบคุม Raspberry Pi จากแล็ปท็อปหรือ PC ที่นี่คุณจะเรียนรู้วิธีตั้งค่า two-factor authentication สำหรับ SSH access ไปยัง Raspberry Pi และเพิ่มชั้นความปลอดภัยเพิ่มเติม
หมายเหตุ: หากใช้ SSH key-file เข้าถึง Raspberry Pi two-factor authentication จะไม่ทำงาน
อัปเดต Pi ของคุณ
สมมติตั้งค่า Raspberry Pi ด้วย Raspberry Pi OS แล้ว 最好先检查所有软件都是最新的 打开终端输入:
sudo apt update && sudo apt -y upgrade
เปิดใช้งาน SSH
Raspberry Pi OS ปิด SSH server โดยค่าเริ่มต้น ก่อนเชื่อมต่อ Pi ผ่าน SSH ต้องเปิดใช้งานด้วยคำสั่ง Terminal:
sudo systemctl enable ssh sudo systemctl start ssh
ตอนนี้เชื่อมต่อ SSH server ได้แล้ว
ต้องการ identity authentication แบบ challenge-response
สุดท้าย Raspberry Pi ต้องท้าทายคุณให้ยืนยันตัวตน และประมวลผล response หมายความว่าต้องเปิด challenge-response passwords
เริ่มต้น: เปิด SSH config file เพื่อแก้ไข:
sudo nano /etc/ssh/sshd_config
ในไฟล์นี้ หาส่วน ChallengeResponseAuthentication เปลี่ยนจาก "no" เป็น "yes"
บันทึก "sshd_config" ที่อัปเดตแล้ว: Ctrl+O ตามด้วย Ctrl+X
กลับสู่ Terminal restart SSH daemon ด้วย config ใหม่:
sudo systemctl restart ssh
เนื่องจากมีการเปลี่ยนแปลง SSH config ควรตรวจสอบว่ายังเชื่อมต่อ Pi ผ่าน SSH ได้
ต้องรู้ IP address ของ Raspberry Pi หากยังไม่มีรันคำสั่งบน Pi:
hostname -I
จะคืนค่า IP address ที่ต้องการ
สลับไปแล็ปท็อป/คอมพิวเตอร์ เปิด Terminal เชื่อมต่อ Pi แทนที่ "10.3.000.0" ด้วย IP ของคุณ:
ssh pi@10.3.000.0
เชื่อมต่อ SSH แล้ว
ตั้งค่า Two-Factor Authentication
ต่อไป ดาวน์โหลด Authenticator app สำหรับสร้าง one-time authentication code มี authentication apps หลายตัว แต่ใช้ Google Authenticator ใน tutorial นี้ พร้อม iOS และ Android
ดาวน์โหลด mobile app เสร็จ ต้องติดตั้ง Google Authenticator PAM module บน Raspberry Pi ด้วย
บน Pi เปิด Terminal รัน:
sudo apt install libpam-google-authenticator
ติดตั้ง Google Authenticator บน Pi และ mobile device แล้ว พร้อมตั้งค่า 2FA
สร้างการเชื่อมต่อ: link Pi กับ mobile device
สร้าง link ระหว่าง mobile app และ Pi: สร้าง QR code บน Pi แล้ว scan ด้วย smartphone/tablet
สร้าง QR code: กลับไป Pi รันคำสั่ง Terminal:
google-authenticator
Pi จะถามว่า authentication tokens ควร time-restricted หรือไม่ เนื่องจากปลอดภัยกว่า มักต้องการ time-based authentication tokens เว้นแต่มีเหตุผลเฉพาะ
Terminal จะสร้าง QR code แม้อาจต้อง resize terminal ให้เห็น barcode 全貌
มี emergency codes ด้วย หากทำหาย วางไว้ผิด หรือแตก mobile device รหัสเหล่านี้จะให้เข้าถึง Pi ผ่าน SSH ได้แม้ไม่มี mobile device อย่าสมเสี่ยงถูกล็อกนอก Pi จดรหัสเหล่านี้เก็บไว้ที่ปลอดภัย
ใช้ QR code เชื่อม Pi กับ Google Authenticator app:
- บน smartphone/tablet เปิด Google Authenticator app
- มุมขวาล่าง กด "+" sign
- เลือก "Scan a QR barcode" เมื่อขอ permission เข้าถึงกล้อง ให้อนุญาต
- จัดกล้องอุปกรณ์ไปที่หน้าจอ วางไว้เหนือ QR code ทันทีที่โทรศัพท์/แท็บเล็ต recognize QR code จะสร้าง account และเริ่มสร้าง authentication codes อัตโนมัติ
- กลับไป Pi Terminal จะ prompt ให้ update "google_authenticator" file กด Y
- ถามว่าต้องการป้องกันหลายคนใช้ authentication token เดียวกัน กด Y
- ถามว่าเพิ่ม time skew window กด N เพื่อป้องกัน brute-force attacks
- Terminal ถาม enable rate-limiting ซึ่งจำกัดคุณ (และแฮกเกอร์!) 3 login attempts ทุก 30 วินาที Rate-limiting ช่วยป้องกัน brute-force และ password-based attacks ควรเลือก "Yes" เว้นแต่มีเหตุผลเฉพาะ
Linux Pluggable Authentication Modules (PAM)
สุดท้าย เปิดใช้งาน two-factor authentication ให้ Raspberry Pi ด้วย Linux PAM
เริ่มต้น: เปิดไฟล์ "sshd" ใน Nano text editor:
sudo nano /etc/pam.d/sshd
เพิ่มบรรทัด:
auth required pam_google_authenticator.so
แตตำแหน่งที่เพิ่มสำคัญ:
1. หลังใส่รหัสผ่าน
หากต้องการ prompt one-time authentication code หลังใส่รหัสผ่าน Pi ให้เพิ่มบรรทัดนี้หลัง @include
2. ก่อนใส่รหัสผ่าน
หากต้องการ prompt one-time authentication code ก่อนใส่รหัสผ่าน ให้เพิ่มบรรทัดนี้ก่อน @include
บันทึกไฟล์: Ctrl+O ตามด้วย Ctrl+X
Restart SSH daemon:
sudo systemctl restart ssh
ตอนนี้ทุกครั้งที่พยายามเชื่อมต่อ SSH จะถูกขอ one-time verification code
ตอนนี้ตั้งค่า 2FA บน Raspberry Pi แล้ว สามารถดำเนินต่อไปตั้งค่า personal web server หรือ music server เพิ่มความปลอดภัย SSH อีกด้วยทริคเหล่านี้