본문 바로가기

리눅스

SSH 로그인 시 Google OTP를 사용하는 2차 인증을 적용하는 방법

728x90

우분투에서 SSH 로그인 시 Google OTP(One-Time Password)를 사용하는 2차 인증을 적용하는 방법

1. OpenSSH 서버 설치

시스템에 OpenSSH 서버가 설치되어 있는지 확인하거나 설치합니다.

sudo apt update
sudo apt install openssh-server

2. Google Authenticator PAM 모듈 설치

Google Authenticator PAM 모듈을 시스템에 설치합니다.

sudo apt install libpam-google-authenticator

3. PAM 설정 수정

PAM 구성 파일 (/etc/pam.d/sshd) 백업합니다.

sudo cp /etc/pam.d/sshd /etc/pam.d/sshd_$(date '+%Y%m%d-%H%M')

/etc/pam.d/sshd 파일을 편집하여 Google Authenticator PAM 모듈을 추가합니다.

  • auth required pam_google_authenticator.so nullok
  • auth required pam_permit.so
sudo vim /etc/pam.d/sshd

파일 내용 중 아래와 같이 수정합니다.

# Standard Un*x password updating.
@include common-password

#auth    required        pam_google_authenticator.so nullok
auth    required        pam_google_authenticator.so secret=${HOME}/.ssh/google_authenticator nullok
auth    required        pam_permit.so

4. SSH 설정 수정

sshd 구성 파일(/etc/ssh/sshd_config) 백업합니다.

sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config_$(date '+%Y%m%d-%H%M')

/etc/ssh/sshd_config 파일을 편집하여 OTP(One-Time Password) 인증을 활성화하고 암호 인증을 비활성화합니다.

sudo vim /etc/ssh/sshd_config

파일 내용 중 아래와 같이 수정합니다.

PasswordAuthentication yes

KbdInteractiveAuthentication yes

UsePAM yes

ChallengeResponseAuthentication yes
AuthenticationMethods keyboard-interactive
  • PasswordAuthentication yes : SSH 암호 인증을 사용하지 않도록 설정합니다.
  • KbdInteractiveAuthentication yes : 키보드 대화형 인증을 사용하도록 설정합니다.
  • UsePAM yes: Pluggable Authentication Modules (PAM)을 사용하도록 설정합니다.
  • ChallengeResponseAuthentication yes: Challenge-Response 방식의 인증을 허용하도록 설정합니다.
  • AuthenticationMethods publickey,password publickey,keyboard-interactive : 인증 방법을 지정합니다. 여기서는 공개 키, 암호 및 키보드 대화형 인증을 사용하도록 설정되어 있습니다.

SSH 서버를 다시 시작합니다.

sudo systemctl restart sshd
728x90

5. 사용자에게 SSH 접근 및 OTP 설정 허용

SSH를 사용할 수 있는 사용자에게 Google Authenticator OTP 설정을 허용합니다.

google-authenticator

각 사용자가 SSH 접근을 허용하려면 다음 명령어를 실행합니다.

google-authenticator -s ~/.ssh/google_authenticator
더보기

---

$ google-authenticator -s ~/.ssh/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/ssh_user1@server_node1%3Fsecret%3DLJJ7QOAQELU7TEUONOYKDPGY3Y%26issuer%3Dserver_node1
---
QR code
---
Your new secret key is: LJJ7ZOAQELU7TEUONOUKDPGY3Y
Enter code from app (-1 to skip): 796728
Code confirmed
Your emergency scratch codes are:
  92191909
  67492275
  94009397
  15249521
  13565994

Do you want me to update your "/home/ssh_user1/.ssh/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, a new token is generated every 30 seconds by the mobile app.
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. This allows for a
time skew of up to 30 seconds between authentication server and client. If you
experience problems with poor time synchronization, you can increase the window
from its default size of 3 permitted codes (one previous code, the current
code, the next code) to 17 permitted codes (the 8 previous codes, the current
code, and the 8 next codes). This will permit for a time skew of up to 4 minutes
between client and server.
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

---

명령을 실행하면 QR 코드와 시크릿 키, 백업 코드 및 재생 코드가 표시됩니다. 이 정보를 안전한 장소에 보관해야 합니다.

6. SSH 로그인 테스트

이제 SSH로 시스템에 로그인하려면 사용자 이름과 암호 외에 OTP를 입력해야 합니다. SSH 세션을 열고 Google Authenticator 앱 또는 OTP 생성기를 사용하여 2차 인증 코드를 입력합니다.

 

SSH 로그인 시 Google OTP를 사용한 2차 인증이 활성화됩니다. 사용자는 추가적인 보안 레이어를 통해 시스템에 접근할 수 있습니다.

OTP 앱(App Strore)

  • Google Authenticator

Google Authenticator

  • Twilio Authy

Twilio Authy

 

로그인 프로세스

1. 사용자는 SSH에 접속하려고 시도하면 사용자 이름과 패스워드를 입력합니다.

ssh ssh_user1@ssh-server
(ssh_user1@ssh-server) Password:

2. 패스워드 인증이 성공하면 Google Authenticator나 다른 OTP 앱에서 생성된 6자리 코드를 입력합니다.

(ssh_user1@ssh-server) Verification code:

두 인증 단계가 모두 성공하면 사용자는 SSH에 성공적으로 로그인합니다.

 

728x90