웹사이트 검색

Linux에서 SSH에 대한 2단계 인증을 설정하는 방법


기본적으로 SSH는 이미 원격 시스템 간의 보안 데이터 통신을 사용하고 있지만 SSH 연결에 추가 보안 계층을 추가하려는 경우 Google OTP를 추가할 수 있습니다(< SSH 서버에 연결하는 동안 임의의 일회용 비밀번호(TOTP) 확인 코드를 입력할 수 있는 Strong>2단계 인증) 모듈입니다. 연결 시 스마트폰 또는 PC에서 인증 코드를 입력해야 합니다.

Google OTPGoogle에서 개발한 일회용 비밀번호(TOTP) 확인 토큰의 구현을 포함하는 오픈 소스 모듈입니다.

다양한 모바일 플랫폼과 PAM(Pluggable Authentication Module)을 지원합니다. 이러한 일회용 비밀번호는 OATH 개방형 인증 이니셔티브)에서 만든 개방형 표준을 사용하여 생성됩니다.

이 기사에서는 Fedora, CentOS Stream, Rocky Linux 및 AlmaLinux와 같은 RedHat 기반 및 Debian 기반 Linux 배포판에서 2단계 인증을 위해 SSH를 설정하고 구성하는 방법을 보여 드리겠습니다. 우분투, 데비안, 민트.

Linux에 Google OTP 설치

2단계 인증을 설정하려는 시스템을 열고 PAM 모듈이 <에서 올바르게 작동하는 데 필요한 개발 라이브러리와 함께 다음 PAM 라이브러리를 설치합니다. Strong>Google 인증자 모듈.

RedHat 기반 시스템에서는 다음 yum 명령을 사용하여 'pam-devel' 패키지를 설치합니다.

yum install google-authenticator -y

Debian 기반 시스템에서는 다음 apt 명령을 사용하여 'libpam0g-dev' 패키지를 설치합니다.

sudo apt install libpam-google-authenticator -y

Google 인증 토큰 생성

'google-authenticator' 명령을 실행하면 일련의 질문이 표시됩니다.

google-authenticator

대부분의 경우 대답으로 'y'()를 입력하기만 하면 됩니다. 문제가 발생하면 'google-authenticator' 명령을 다시 입력하여 설정을 재설정할 수 있습니다.

  • 인증 토큰을 시간 기반(y/n) y으로 설정하시겠습니까?

이 질문을 마치면 '비밀 키'와 '긴급 코드'를 받게 됩니다. 이러한 세부정보를 어딘가에 적어두세요. 나중에 Google OTP 앱을 설정하려면 '비밀 키'가 필요합니다.

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"(yes)로 입력합니다.

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

Linux에서 Google OTP를 사용하도록 SSH 구성

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 OTP 앱 구성

스마트폰에서 Google OTP 앱을 실행하세요. +를 누르고 '설정 키 입력'을 선택합니다. 이 앱이 없으면 Android/iPhone/Blackberry 기기에 Google Authenticator 앱을 다운로드하여 설치할 수 있습니다.

계정 '이름'을 추가하고 앞서 생성한 '비밀 키'를 입력하세요.

휴대전화에서 30초마다 지속적으로 변경되는 일회용 비밀번호(인증 코드)가 생성됩니다.

이제 SSH를 통해 로그인을 시도하면 Google OTP 코드(인증 코드) 및 비밀번호를 묻는 메시지가 표시됩니다. > 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 ~]#

스마트폰이 없는 경우 Authenticator라는 Firefox 추가 기능을 사용하여 2단계 인증을 수행할 수도 있습니다.

중요: 2단계 인증은 비밀번호 기반 SSH 로그인과 함께 작동합니다. 개인/공개 키 SSH 세션을 사용하는 경우 2단계 인증을 무시하고 직접 로그인합니다.