ค้นหาเว็บไซต์

วิธีการตั้งค่าการรับรองความถูกต้องด้วยสองปัจจัยสำหรับ SSH ใน Linux


ตามค่าเริ่มต้น SSH ใช้การสื่อสารข้อมูลที่ปลอดภัยระหว่างเครื่องระยะไกลอยู่แล้ว แต่ถ้าคุณต้องการเพิ่มชั้นการรักษาความปลอดภัยพิเศษให้กับการเชื่อมต่อ SSH ของคุณ คุณสามารถเพิ่ม Google Authenticator (< โมดูลการตรวจสอบสิทธิ์แบบสองปัจจัย) ที่ช่วยให้คุณสามารถป้อนรหัสยืนยันรหัสผ่านครั้งเดียวแบบสุ่ม (TOTP) ในขณะที่เชื่อมต่อกับเซิร์ฟเวอร์ SSH คุณจะต้องป้อนรหัสยืนยันจากสมาร์ทโฟนหรือพีซีของคุณเมื่อคุณเชื่อมต่อ

Google Authenticator เป็นโมดูลโอเพ่นซอร์สที่รวมการใช้งานโทเค็นการยืนยันรหัสผ่านแบบใช้ครั้งเดียว (TOTP) ที่พัฒนาโดย Google

รองรับแพลตฟอร์มมือถือหลายแพลตฟอร์ม เช่นเดียวกับ PAM (Pluggable Authentication Module) รหัสผ่านแบบครั้งเดียวเหล่านี้สร้างขึ้นโดยใช้มาตรฐานแบบเปิดที่สร้างโดย OATH ความคิดริเริ่มสำหรับการตรวจสอบสิทธิ์แบบเปิด)

ในบทความนี้ ฉันจะแสดงวิธีตั้งค่าและกำหนดค่า SSH สำหรับการตรวจสอบสิทธิ์แบบสองปัจจัยภายใต้ Linux ที่ใช้ RedHat และ Debian เช่น Fedora, CentOS Stream, Rocky Linux และ AlmaLinux อูบุนตู เดเบียน และมิ้นต์

การติดตั้ง Google Authenticator ใน Linux

เปิดเครื่องที่คุณต้องการตั้งค่าการตรวจสอบสิทธิ์แบบสองปัจจัย และติดตั้งไลบรารี PAM ต่อไปนี้พร้อมกับไลบรารีการพัฒนาที่จำเป็นสำหรับโมดูล PAM เพื่อให้ทำงานได้อย่างถูกต้องกับ โมดูลตรวจสอบสิทธิ์ของ Google

บนระบบที่ใช้ RedHat ให้ติดตั้งแพ็คเกจ 'pam-devel' โดยใช้คำสั่ง yum ต่อไปนี้

yum install google-authenticator -y

บนระบบที่ใช้ Debian ให้ติดตั้งแพ็คเกจ 'libpam0g-dev' โดยใช้คำสั่ง apt ต่อไปนี้

sudo apt install libpam-google-authenticator -y

สร้างโทเค็นการรับรองความถูกต้องของ Google

เมื่อคุณเรียกใช้คำสั่ง 'google-authenticator' คำสั่งจะถามคุณหลายชุด

google-authenticator

เพียงพิมพ์ “y” (ใช่) เป็นคำตอบในสถานการณ์ส่วนใหญ่ หากมีสิ่งผิดปกติเกิดขึ้น คุณสามารถพิมพ์คำสั่ง 'google-authenticator' อีกครั้งเพื่อรีเซ็ตการตั้งค่า

  • คุณต้องการให้โทเค็นการตรวจสอบสิทธิ์เป็นแบบตามเวลา (y/n) y

หลังจากคำถามนี้ คุณจะได้รับ 'รหัสลับ' และ 'รหัสฉุกเฉิน' จดรายละเอียดเหล่านี้ไว้ที่ใดที่หนึ่ง เราจะต้องใช้ 'รหัสลับ' ในภายหลังเพื่อตั้งค่าแอป Google Authenticator

google-authenticator

Do you want authentication tokens to be time-based (y/n) y
Warning: pasting the following URL into your browser exposes the OTP secret to Google:
  https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/root@tecmint%3Fsecret%3DCYZF2YF7HFGX55ZEPQYLHOO5JM%26issuer%3Dtecmint
Failed to use libqrencode to show QR code visually for scanning.
Consider typing the OTP secret into your app manually.
Your new secret key is: CYZF2YF7HFGX55ZEPQYLHOM
Enter code from app (-1 to skip): -1 Code confirmation skipped Your emergency scratch codes are: 83714291 53083200 80975623 57217008 77496339

จากนั้น ปฏิบัติตามวิซาร์ดการตั้งค่า และในกรณีส่วนใหญ่ให้พิมพ์คำตอบเป็น “y” (ใช่) ดังที่แสดงด้านล่าง

Do you want me to update your "/root/.google_authenticator" file (y/n) y Do you want to disallow multiple uses of the same authentication token? This restricts you to one login about every 30s, but it increases your chances to notice or even prevent man-in-the-middle attacks (y/n) y By default, tokens are good for 30 seconds and in order to compensate for possible time-skew between the client and the server, we allow an extra token before and after the current time. If you experience problems with poor time synchronization, you can increase the window from its default size of 1:30min to about 4min. Do you want to do so (y/n) y If the computer that you are logging into isn't hardened against brute-force login attempts, you can enable rate-limiting for the authentication module. By default, this limits attackers to no more than 3 login attempts every 30s. Do you want to enable rate-limiting (y/n) y

การกำหนดค่า SSH เพื่อใช้ Google Authenticator ใน Linux

เปิดไฟล์การกำหนดค่า PAM '/etc/pam.d/sshd' และเพิ่มบรรทัดต่อไปนี้ที่ด้านล่างของไฟล์

auth required pam_google_authenticator.so nullok
auth required pam_permit.so

จากนั้น เปิดไฟล์การกำหนดค่า SSH '/etc/ssh/sshd_config' และเลื่อนลงเพื่อค้นหาบรรทัดที่ระบุว่า

ChallengeResponseAuthentication no

เปลี่ยนเป็น “ใช่“ มันเลยกลายเป็นแบบนี้

ChallengeResponseAuthentication yes

สุดท้าย ให้รีสตาร์ทบริการ SSH เพื่อทำการเปลี่ยนแปลงใหม่

systemctl restart sshd
Or
sudo systemctl restart sshd

กำลังกำหนดค่าแอป Google Authenticator

เปิดแอป Google Authenticator บนสมาร์ทโฟนของคุณ กด + และเลือก “ป้อนคีย์การตั้งค่า“ หากคุณไม่มีแอปนี้ คุณสามารถดาวน์โหลดและติดตั้งแอป Google Authenticator บนอุปกรณ์ Android/iPhone/Blackberry ของคุณได้

เพิ่มบัญชีของคุณ 'ชื่อ' และป้อน 'รหัสลับ' ที่สร้างขึ้นก่อนหน้านี้

มันจะสร้างรหัสผ่านแบบใช้ครั้งเดียว (รหัสยืนยัน) ซึ่งจะเปลี่ยนแปลงตลอดเวลา 30 วินาที บนโทรศัพท์ของคุณ

ตอนนี้ให้ลองเข้าสู่ระบบผ่าน SSH คุณจะได้รับแจ้งด้วย รหัส Google Authenticator (รหัสยืนยัน) และ รหัสผ่าน เมื่อใดก็ตามที่คุณพยายามเข้าสู่ระบบผ่าน SSH คุณมีเวลาเพียง 30 วินาที ในการป้อนรหัสยืนยันนี้ หากคุณพลาดรหัสดังกล่าวจะสร้างรหัสยืนยันใหม่

login as: tecmint
Access denied
Using keyboard-interactive authentication.
Verification code:
Using keyboard-interactive authentication.
Password:
Last login: Tue Apr 23 13:58:29 2022 from 172.16.25.125
[root@tecmint ~]#

หากคุณไม่มีสมาร์ทโฟน คุณสามารถใช้โปรแกรมเสริม Firefox ชื่อ Authenticator เพื่อทำการตรวจสอบสิทธิ์แบบสองปัจจัยได้

ข้อสำคัญ: การตรวจสอบสิทธิ์แบบสองปัจจัยใช้งานได้กับการเข้าสู่ระบบ SSH ที่ใช้รหัสผ่าน หากคุณใช้เซสชัน SSH คีย์ส่วนตัว/สาธารณะ ระบบจะเพิกเฉยต่อการรับรองความถูกต้องด้วยสองปัจจัยและเข้าสู่ระบบโดยตรง