Giunsa ang Pag-setup sa Two-Factor Authentication Para sa SSH Sa Linux


Sa kasagaran, ang SSH naggamit na sa luwas nga komunikasyon sa datos tali sa mga remote nga makina, apan kung gusto nimong idugang ang pipila ka dugang nga layer sa seguridad sa imong mga koneksyon sa SSH, mahimo nimong idugang ang Google Authenticator (two-factor authentication) nga module nga nagtugot kanimo sa pagsulod sa usa ka random- time password (TOTP) verification code samtang nagkonektar sa SSH servers. Kinahanglan nimong isulod ang verification code gikan sa imong smartphone o PC kung magkonektar ka.

Ang Google Authenticator usa ka open-source module nga naglakip sa mga pagpatuman sa one-time passcodes (TOTP) verification tokens nga gimugna sa Google.

Gisuportahan niini ang daghang mga mobile platform, ingon man ang PAM (Pluggable Authentication Module). Kining usa ka higayon nga mga passcode gihimo gamit ang bukas nga mga sumbanan nga gihimo sa OATH Initiative alang sa Open Authentication).

Niini nga artikulo, ipakita ko kanimo kung giunsa ang pag-set up ug pag-configure sa SSH alang sa duha ka hinungdan nga pag-authenticate ubos sa mga distribusyon sa Linux nga nakabase sa Debian sama sa Fedora, CentOS Stream, Rocky Linux, ug AlmaLinux, Ubuntu, Debian, ug Mint.

Pag-instalar sa Google Authenticator sa Linux

Ablihi ang makina nga gusto nimong i-set up ang two-factor authentication ug i-install ang mosunod nga mga librarya sa PAM uban ang mga librarya sa pag-uswag nga gikinahanglan aron ang module sa PAM molihok sa husto sa module sa Google authenticator.

Sa RedHat-based nga mga sistema i-install ang 'pam-devel' nga pakete gamit ang mosunod nga yum command.

# yum install google-authenticator -y

Sa mga sistema nga nakabase sa Debian i-install ang package nga 'libpam0g-dev' gamit ang mosunud nga apt command.

$ sudo apt install libpam-google-authenticator -y

Paghimo og Google Authentication Token

Kung gipadagan nimo ang 'google-authenticator' nga mando, kini mag-aghat kanimo sa usa ka serye sa mga pangutana.

# google-authenticator

I-type lang ang y (oo) isip tubag sa kadaghanan nga mga sitwasyon. Kung adunay sayup, mahimo nimong i-type pag-usab ang 'google-authenticator' nga sugo aron i-reset ang mga setting.

  • Gusto ba nimo nga ang mga token sa panghimatuud mahimong gibase sa oras (y/n) y

Pagkahuman niini nga pangutana, makuha nimo ang imong 'secret key' ug 'emergency code'. Isulat kini nga mga detalye sa usa ka lugar, kinahanglan namon ang 'sekreto nga yawe' sa ulahi aron ma-set up ang Google Authenticator app.

# 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/[email %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

Sunod, sunda ang setup wizard ug sa kadaghanan nga mga kaso i-type ang tubag nga y (oo) sama sa gipakita sa ubos.

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

Pag-configure sa SSH aron magamit ang Google Authenticator sa Linux

Ablihi ang PAM configuration file '/etc/pam.d/sshd' ug idugang ang mosunod nga linya sa ubos sa file.

auth required pam_google_authenticator.so nullok
auth required pam_permit.so

Sunod, ablihi ang SSH configuration file '/etc/ssh/sshd_config'ug pag-scroll down aron makit-an ang linya nga giingon.

ChallengeResponseAuthentication no

Usba kini sa oo. Busa, kini nahimong ingon niini.

ChallengeResponseAuthentication yes

Sa katapusan, i-restart ang serbisyo sa SSH aron makakuha mga bag-ong pagbag-o.

# systemctl restart sshd
Or
$ sudo systemctl restart sshd

Pag-configure sa Google Authenticator App

Ilunsad ang Google Authenticator app sa imong smartphone. Pindota ang + ug pilia ang Pagsulod sa usa ka yawe sa pag-setup. Kung wala ka niini nga app, mahimo nimong i-download ug i-install ang Google Authenticator app sa imong Android/iPhone/Blackberry device.

Idugang ang imong account 'Ngalan' ug isulod ang 'sekreto nga yawe' nga nahimo sa sayo pa.

Makamugna kini og usa ka higayon nga password (verification code) nga kanunay mag-usab matag 30sec sa imong telepono.

Karon sulayi ang pag-log in pinaagi sa SSH, maaghat ka sa Google Authenticator code (Verification code) ug Password sa matag higayon nga mosulay ka sa pag-log in pinaagi sa SSH. Duna ka lang 30 segundos aron masulod kini nga verification code, kung masipyat ka niini makamugna og bag-ong verification code.

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

Kung wala ka usa ka smartphone, mahimo ka usab mogamit usa ka add-on sa Firefox nga gitawag Authenticator aron mahimo ang duha ka hinungdan nga pag-authenticate.

Importante: Ang two-factor authentication naglihok sa password-based SSH login. Kung naggamit ka ug bisan unsang pribado/publiko nga yawe nga sesyon sa SSH, kini dili magtagad sa duha ka hinungdan nga pag-authenticate ug direkta nga mag-log in.