[Linux] SSH 로그인 시 MFA(Multi-Factor Authentication) 적용하기

리눅스에 루트 계정으로 접근 시 비밀번호, SSH PEM Key 이외에 MFA(Multi-Factor Authentication)를 적용하여 추가 인증수단을 확보하는 절차입니다. 아래와 같은 테스트 환경에서 MFA를 적용할 예정입니다. 사전에 휴대폰에 Oracle Mobile Authenticator를 설치해야 합니다.

  • OS: Ubuntu 24.04.2 LTS
  • Authenticator: Oracle Mobile Authenticator
    Oracle Mobile Authenticator
  1. 리눅스에 로그인한 후 Google Authentication 패키지를 설치합니다.
    • OS 업그레이드가 필요한 경우에는 먼저 업그레이드한 다음에 reboot를 합니다.
    • TOTP(Time-based One-time Password) 기반으로 MFA를 구현할 예정이며, 관련 PAM모듈을 설치합니다. google-authenticator 모듈이 널리 사용됩니다.
      root@ubuntu:~$ apt update
      root@ubuntu:~$ apt upgrade -y
      root@ubuntu:~$ reboot
      
      root@ubuntu:~$ apt install -y libpam-google-authenticator
      
  2. root 사용자로 google-authenticator 명령어를 실행하여 OMA에 계정을 등록하고 OTP 키를 생성합니다.
    • google-authenticator 명령어를 실행하면 QR 코드가 터미널에 나타나고, OMA 앱에서 이 QR 코드를 스캔하여 계정(root@ubuntu)을 등록합니다.
    • OMA 앱 화면에 출력되는 OTP(555442)를 Enter code from app 프롬프트에 입력합니다.
    • 이 과정에서 비상용 스크래치 코드(emergency scratch codes)도 제공되니 반드시 안전한 곳에 보관해야 합니다.
    • 나머지 항목들은 확인한 후 “y”를 입력합니다.
    • OMA 앱에 계정을 등록한 사용자는 ~/.google_authenticator 파일이 생성됩니다.
      root@ubuntu:~# which google-authenticator
      /usr/bin/google-authenticator
      root@ubuntu:~# 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@ubuntu%3Fsecret%3DUTT6ZSWI4FW3DJJVYVZU7BOUMI%26issuer%3Dubuntu OMA 앱 QR코드 Your new secret key is: UTT6ZSWI4FW3DJJVYVZU7BOUMI Enter code from app (-1 to skip): 555442 Code confirmed Your emergency scratch codes are: 46336625 27934680 27665030 10284825 27283701 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, 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
  3. SSH 로그인 시 OTP를 요구하도록 PAM모듈의 sshd 설정을 변경해야 합니다. 파일 맨 아래에 추가 합니다.
    • nullok 옵션을 사용하면 ~/.google_authenticator 파일이 생성된 사용자만(OMA 계정을 추가한 사용자) MFA를 적용하고,
      ~/.google_authenticator 파일이 없는 사용자는 기존과 동일하게 비밀번호로 로그인할 수 있습니다.
    • su 명령어로 root 사용자 전환하는 경우(su - root)에도 MFA를 적용하고자 한다면 /etc/pam.d/su 파일도 동일하게 수정합니다.
      root@ubuntu:~# vi /etc/pam.d/sshd
      (생략) auth required pam_google_authenticator.so nullok
  4. SSH 접속 시 MFA가 사용될 수 있도록 sshd_config 파일을 수정한 후 sshd를 재시작합니다.
    • sshd_config 파일의 KbdInteractiveAuthentication 지시자의 값을 yes로 설정합니다.
      root@ubuntu:~# vi /etc/ssh/sshd_config
      PermitRootLogin yes KbdInteractiveAuthentication yes
      root@ubuntu:~# systemctl restart ssh
  5. ssh 접속 툴을 사용해서 서버에 접속할 때 MFA가 적용되는지 확인합니다.
    • PuTTY 사용할 경우, Password 검증 후 Verification code를 입력하는 프롬프트가 나타납니다. 해당 프롬프트에 OMA앱에 생성된 6자리 숫자를 입력하면 로그인됩니다.
      PuTTY 사용
      PuTTY 사용 시 Verification code 프롬프트 확인
    • SecureCRT를 사용할 경우, Authentication method로 선택한 Keyboard Interactive를 맨 위로 이동시킵니다. Password가 저장되어 있다면 Verification code를 입력하는 Input 필드가 표시됩니다. Input 필드에 OMA앱에 생성된 6자리 숫자를 입력하면 로그인할 수 있습니다.
      SecureCRT 사용
      SecureCRT Verification code input box

1.

You may also like...

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다