리눅스

SSH에 OTP 적용하기 (MFA)

엔지니어-여리 2023. 9. 5. 10:08
반응형

 

ssh를 사용하다 보면 다음과 같은 의문이 들 수 있습니다.

 

내 패스워드나 암호키가 탈취당할 수 있는데, 어떤 해결방법이 있을까

 

이를 해결하기 위해 

SSH에 OTP를 적용해서 보안 레벨을 올려봅시다.

 

리눅스 환경에 접근하여 아래 명령을 사용하여

sudo su

root 계정으로 전환합시다. 

 

아래 명령어를 사용하여 google-authenticator를 설치, ssh 로그인 설정을 변경해줍니다.

# Install google-otp
yum install google-authenticator -y -q

# use otp config
cat <<EOF>> /etc/pam.d/sshd
#%PAM-1.0
auth      required    pam_google_authenticator.so secret=\${HOME}/.ssh/google_authenticator
auth      required    pam_sepermit.so
EOF

# change ssh login option
sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config
sed -i 's/ChallengeResponseAuthentication no/ChallengeResponseAuthentication yes/g' /etc/ssh/sshd_config 
sed -i 's/UsePAM no/UsePAM yes/g' /etc/ssh/sshd_config

# restart sshd daemon
systemctl restart sshd

 

이제 서버에서는 로그인 시에 비밀번호와 OTP 암호를 묻는 절차가 추가되었습니다. 

 

각 사용자 계정에서 OTP 설정을 하는 방법은 다음과 같습니다.

먼저, 사용자 계정으로 로그인 합니다.

아래 코드 블럭을 복사, 붙여넣기를 합니다.

cd ~/
mkdir -p ~/.ssh
yes | google-authenticator
mv .google_authenticator .ssh/google_authenticator

 

코드블럭을 실행하게 되면 다음과 같이 QR코드 이미지, QR코드 이미지를 포함하는 링크를 출력합니다.

이 QR이미지(혹은 링크)를 SSH 접속 사용자에게 제공하면 해당 사용자는 QR정보를 OTP 어플리케이션에 등록하여 SSH 로그인시에 활용할 수 있습니다.

이렇게 설정하면 SSH 로그인시 다음과 같이 OTP 를 묻는 절차를 확인할 수 있습니다.

 

 

추천드리는 OTP 프로그램은 다음과 같습니다.

- google authenticator
  - 안드로이드 링크

 

Google Authenticator - Apps on Google Play

Enable 2-step verification to protect your account from hijacking.

play.google.com

  - 아이폰 링크

 

‎Google Authenticator

‎Google Authenticator adds an extra layer of security to your online accounts by adding a second step of verification when you sign in. This means that in addition to your password, you'll also need to enter a code that is generated by the Google Authent

apps.apple.com

- authy (본인 사용중)

  - 링크

 

Authy | Two-factor Authentication (2FA) App & Guides

Two-factor authentication (2FA) adds an additional layer of protection beyond passwords. Download our free app today and follow our easy to use guides to protect your accounts and personal information.

authy.com

 

읽어주셔서 감사합니다.

반응형