본문 바로가기
Server/Linux

[ssh google authenticator] Rocky8 ssh 접속 OTP 2차인증 설정

by 얽디 2023. 11. 16.

 

서버 보안을 위해 ssh 접속 시 google OTP를 활용하여 2차 인증을 하도록 설정 진행

 

난이도 : 하

설정 소요 시간 : 10분~20분

# dnf install epel-release 
# dnf install google-authenticator qrencode qrencode-libs -y // 구글 오티피 설치 

 

1.OTP 설치 진행

# dnf install epel-release
마지막 메타자료 만료확인(0:13:08 이전): 2023년 11월 13일 (월) 오전 11시 27분 43초.
종속성이 해결되었습니다.
================================================================================
 꾸러미                구조            버전               저장소           크기
================================================================================
설치 중:
 epel-release          noarch          8-18.el8           extras           24 k

연결 요약
================================================================================
설치  1 꾸러미

전체 내려받기 크기: 24 k
설치된 크기 : 35 k
진행할까요? [y/N]: y
꾸러미 내려받기 중:
epel-release-8-18.el8.noarch.rpm                 70 kB/s |  24 kB     00:00    
--------------------------------------------------------------------------------
합계                                             27 kB/s |  24 kB     00:00     
연결 확인 실행 중
연결 확인에 성공했습니다.
연결 시험 실행 중
연결 시험에 성공했습니다.
연결 실행 중
  준비 중     :                                                             1/1 
  설치 중     : epel-release-8-18.el8.noarch                                1/1 
  구현 중     : epel-release-8-18.el8.noarch                                1/1 
Many EPEL packages require the CodeReady Builder (CRB) repository.
It is recommended that you run /usr/bin/crb enable to enable the CRB repository.

  확인 중     : epel-release-8-18.el8.noarch                                1/1 

설치되었습니다:
  epel-release-8-18.el8.noarch                                                  

완료되었습니다!

# dnf install google-authenticator qrencode qrencode-libs -y
마지막 메타자료 만료확인(0:00:17 이전): 2023년 11월 13일 (월) 오전 11시 59분 22초.
종속성이 해결되었습니다.
======================================================================================================================
 꾸러미                              구조                  버전                        저장소                    크기
======================================================================================================================
설치 중:
 google-authenticator                x86_64                1.07-1.el8                  epel                      57 k
 qrencode                            x86_64                3.4.4-5.el8                 appstream                 25 k
 qrencode-libs                       x86_64                3.4.4-5.el8                 appstream                 58 k

연결 요약
======================================================================================================================
설치  3 꾸러미

전체 내려받기 크기: 140 k
설치된 크기 : 302 k
꾸러미 내려받기 중:
(1/3): google-authenticator-1.07-1.el8.x86_64.rpm                                     214 kB/s |  57 kB     00:00    
(2/3): qrencode-3.4.4-5.el8.x86_64.rpm                                                 80 kB/s |  25 kB     00:00    
(3/3): qrencode-libs-3.4.4-5.el8.x86_64.rpm                                           158 kB/s |  58 kB     00:00    
----------------------------------------------------------------------------------------------------------------------
합계                                                                                   66 kB/s | 140 kB     00:02     
Extra Packages for Enterprise Linux 8 - x86_64                                        1.6 MB/s | 1.6 kB     00:00    
GPG키 0x2F86D6A1 가져오는 중:
사용자 ID : "Fedora EPEL (8) <epel@fedoraproject.org>"
지문: 94E2 79EB 8D8F 25B2 1810 ADF1 21EA 45AB 2F86 D6A1
출처 : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-8
키 가져오기에 성공했습니다
연결 확인 실행 중
연결 확인에 성공했습니다.
연결 시험 실행 중
연결 시험에 성공했습니다.
연결 실행 중
  준비 중     :                                                                                                   1/1 
  설치 중     : qrencode-libs-3.4.4-5.el8.x86_64                                                                  1/3 
  설치 중     : qrencode-3.4.4-5.el8.x86_64                                                                       2/3 
  설치 중     : google-authenticator-1.07-1.el8.x86_64                                                            3/3 
  구현 중     : google-authenticator-1.07-1.el8.x86_64                                                            3/3 
  확인 중     : qrencode-3.4.4-5.el8.x86_64                                                                       1/3 
  확인 중     : qrencode-libs-3.4.4-5.el8.x86_64                                                                  2/3 
  확인 중     : google-authenticator-1.07-1.el8.x86_64                                                            3/3 

설치되었습니다:
  google-authenticator-1.07-1.el8.x86_64      qrencode-3.4.4-5.el8.x86_64      qrencode-libs-3.4.4-5.el8.x86_64

 

2.pam.d/sshd 파일 설정

# cp -arp /etc/pam.d/sshd /etc/pam.d/sshd_bak //기존 설정파일 백업
# vi /etc/pam.d/sshd //설정파일 오픈
auth      required      pam_google_authenticator.so      nullok     //맨 밑에 추가
저장
#vi /etc/pam.d/sshd

auth       required     pam_google_authenticator.so nullok
//아래 추가

 

3.sshd_config 파일 설정

# cp -arp /etc/ssh/sshd_config /etc/ssh/sshd_config_bak //기존 설정파일 백업
# vi /etc/ssh/sshd_config
PermitEmptyPassword no // 주석 해제 또는 yes -> no
PasswordAuthentication no //주석 해제 또는 yes -> no 
ChallengeResponseAuthentication yes //주석 해제 또는 no -> yes
UsePam yes //기존 yes로 되어있으나 혹시 모르니 한번 확인.

변경 후 systemctl restart sshd 하여야 적용된다.
#vi /etc/ssh/sshd_config
PermitEmptyPasswords no
PasswordAuthentication no
ChallengeResponseAuthentication yes
UsePAM yes

 

4.selinux 비활성화

selinux 비활성화 
# getenfirce //selinux 현재 상태 확인
# vi /etc/selinux/config
# SELINUX=disabled //enforcing -> disabled로 변경

변경 후 reboot하여야 적용된다.
vi /etc/selinux/config

SELINUX=disabled //변경

 

otp 발급 방법

ssh 접속하는 계정으로 로그인

# google_authenticator
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=otpau....~~~ //QR코드 보이는 URL
   
[QR code출력] //google otp APP에서 QR코드 인식                                                                                        
                                                                                                                                                                          
Your new secret key is: [secret key 값 출력]
Your verification code is 760577
Your emergency scratch codes are:
  2705????
  2570????
  3742????
  1975????
  9563????

Do you want me to update your "/home/Hagorud99/.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

 

 

# ll /home/Hagorud99/
total 16
drwxr-xr-x 3 Hagorud99 Hagorud99 4096 Nov 14 14:10 ./
drwxr-xr-x 4 root      root      4096 Nov 14 14:09 ../
drwx------ 2 Hagorud99 Hagorud99 4096 Nov 14 14:09 .cache/
-r-------- 1 Hagorud99 Hagorud99  136 Nov 14 14:10 .google_authenticator //계정 홈디렉토리에 숨김 파일로 생성된다.

로그인 시 기존 계정 패스워드 입력 후 otp입력

 

[네트워크 분리된 서버일 경우 설치방법]

네트워크 통신이 가능한 동일한 OS버전 서버에서 해당 패키지 다운로드하여 옮긴 후 설치 진행..

dnf download --downloadonly google-authenticator
마지막 메타자료 만료확인 3:02:46 이전인: 2023년 11월 14일 (화) 오전 11시 22분 13초.
google-authenticator-1.07-1.el8.x86_64.rpm       16 kB/s |  57 kB     00:03

 

 

[다른 서버에 동일한 OTP를 중복 사용하는 방법]

다른 서버에 동일한 otp를 사용하고 싶을 시

dnf로 구글 otp 설치 후 google_authenticator 진행하지 않고 파일만 복사

  1. cd 접속하는 계정 디렉토리
  2. .google_authenticator 파일생성 후 키값 내용 복사
  3. chmod 400 .google_authenticator
  4. chwon 계정.계정 .google_authenticator 진행하시면 됩니다.

 

댓글