작업환경
플랫폼 구글 클라우드
운영체제 Ubuntu Pro Server, ver 20
머신 유형 e2-medium(vCpu 2개, 8bg 메모리)
고민
ssh, rsa key 인증을 통해서 서버를 이용하고 있었는데 순간 내가 관리하는 키가 노출되면 어떻게 하지? 고민이 많아졌다.
따라서 ssh 보안을 강화하기로 합니다.
리눅스 시스템에서는 PAM(인증 모듈: Pluggable Authenication Modules)을 제공함으로, 사용자의 권한, 인증을 제어하는 모듈 쓸 수 있게 환경을 제공합니다.
따라서, 필요한 패키지를 설치하여, 2차인증을 강화하기로 합니다.
방법
방법은 총 2가지로 쓰일 것 입니다.
1. ssh 접속 요청 -> ssh 아이디, 비밀번호 -> otp 입력 -> 접속
2. ssh rsa key 접속 요청 -> phrase 구문 입력 -> otp 입력 -> 접속
공통사항에서 입맛에 맞춰서 설정값 1, 2번에 맞춰서 진행하면 됩니다.
공통사항
# 이용 가능한 패키지, 버전 리스트 업데이트 및 설치된 패키지 버전 업그레이드
sudo apt update
sudo apt upgrade
# 구글 PAM 설치
sudo apt install libpam-google-authenticator
# 구글 authenticator. configure
google-authenticator
답변은 원하는 것을 맞춰서 고르면 됩니다.
Do you want authentication tokens to be time-based (y/n) y
time-based token, 시간 기반 일회용 암호를 사용할 것이냐고 물어본다.
나의 답변은 y
Do you want me to update your "/home/user/.google_authenticator" file? (y/n) y
이것은 키와 옵션 값이 지정한 경로에 적힌 파일에 업데이트된다는 것을 의미합니다. 만약 no를 누른다면, 선택은 종료됩니다. 즉, 구글 OTP를 이용 못합니다.
나의 답변은 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
필수: 이때 나오는 QR 코드나 인증코드로 구글 OTP를 추가하면 됩니다.
30초마다 한 번만 로그인하게 제한합니다. 이것은 main-in-the-middle 공격을 방지합니다.
나의 답변은 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) n
시간을 동기화하면서 발생할 수 있는 시간상 오차를 방지할 수 있는 옵션으로, 즉, 시간 오차를 허용할 것인지, 안 할 것 인지 물어보는 것이다.
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
이 옵션은 매 30마다 생성되는 토큰을 총 3번 이상 실패할 경우, 로그인을 차단하는 옵션입니다.
# OpenSSH Configure
sudo vi /etc/pam.d/sshd
# Standard Un*x password updating.
@include common-password
auth required pam_google_authenticator.so nullok
아까 설치했던, pam otp 모듈을 설정하고, 저장합니다.
sudo vi /etc/ssh/sshd_config
# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication yes
설정하고 저장합니다.
sudo systemctl restart sshd.service
설정해두었던 것을 적용하기 위해서 sshd서비스를 재시작합니다.
sudo vi /etc/ssh/sshd_config
UsePAM yes
# AuthenticationMethods publickey,password publickey,keyboard-interactive
AuthenticationMethods publickey,keyboard-interactive
otp를 모두 다 요구를 하는데, 사용할 방법만 쓰는 것을 추천합니다.
sudo vi /etc/pam.d/sshd
# Standard Un*x authentication.
#@include common-auth
PAM에서 주석 처리를 진행하지 않으면 비밀번호를 물어봅니다. 그래서 주석처리를 진행한 겁니다.
저는 Key + 2차 인증을 진행하고 싶기 때문이죠, 따라서 필요한 것만 진행하고 싶어서 주석 처리합니다.
이제 설정 사항을 다시 적용합니다.
sudo systemctl restart sshd.service
재접속을 진행해 OTP 적용이 잘되는지 확인합니다.

이제 예전보다 보안이 강화되었습니다.
'etc > 개발하다가 만난 삽질' 카테고리의 다른 글
file is a commonjs module it may be converted to an es6 module 해결 (0) | 2022.02.03 |
---|---|
[LInux] bash shell export (환경변수) (0) | 2022.01.13 |
Shell - 명령어 출력에서 단어 찾기 (0) | 2022.01.06 |
react useEffact (0) | 2021.12.02 |
[linux, mac] 디렉터리(폴더) 삭제, 복사하기 (0) | 2021.02.15 |
작업환경
플랫폼 구글 클라우드
운영체제 Ubuntu Pro Server, ver 20
머신 유형 e2-medium(vCpu 2개, 8bg 메모리)
고민
ssh, rsa key 인증을 통해서 서버를 이용하고 있었는데 순간 내가 관리하는 키가 노출되면 어떻게 하지? 고민이 많아졌다.
따라서 ssh 보안을 강화하기로 합니다.
리눅스 시스템에서는 PAM(인증 모듈: Pluggable Authenication Modules)을 제공함으로, 사용자의 권한, 인증을 제어하는 모듈 쓸 수 있게 환경을 제공합니다.
따라서, 필요한 패키지를 설치하여, 2차인증을 강화하기로 합니다.
방법
방법은 총 2가지로 쓰일 것 입니다.
1. ssh 접속 요청 -> ssh 아이디, 비밀번호 -> otp 입력 -> 접속
2. ssh rsa key 접속 요청 -> phrase 구문 입력 -> otp 입력 -> 접속
공통사항에서 입맛에 맞춰서 설정값 1, 2번에 맞춰서 진행하면 됩니다.
공통사항
# 이용 가능한 패키지, 버전 리스트 업데이트 및 설치된 패키지 버전 업그레이드
sudo apt update
sudo apt upgrade
# 구글 PAM 설치
sudo apt install libpam-google-authenticator
# 구글 authenticator. configure
google-authenticator
답변은 원하는 것을 맞춰서 고르면 됩니다.
Do you want authentication tokens to be time-based (y/n) y
time-based token, 시간 기반 일회용 암호를 사용할 것이냐고 물어본다.
나의 답변은 y
Do you want me to update your "/home/user/.google_authenticator" file? (y/n) y
이것은 키와 옵션 값이 지정한 경로에 적힌 파일에 업데이트된다는 것을 의미합니다. 만약 no를 누른다면, 선택은 종료됩니다. 즉, 구글 OTP를 이용 못합니다.
나의 답변은 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
필수: 이때 나오는 QR 코드나 인증코드로 구글 OTP를 추가하면 됩니다.
30초마다 한 번만 로그인하게 제한합니다. 이것은 main-in-the-middle 공격을 방지합니다.
나의 답변은 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) n
시간을 동기화하면서 발생할 수 있는 시간상 오차를 방지할 수 있는 옵션으로, 즉, 시간 오차를 허용할 것인지, 안 할 것 인지 물어보는 것이다.
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
이 옵션은 매 30마다 생성되는 토큰을 총 3번 이상 실패할 경우, 로그인을 차단하는 옵션입니다.
# OpenSSH Configure
sudo vi /etc/pam.d/sshd
# Standard Un*x password updating.
@include common-password
auth required pam_google_authenticator.so nullok
아까 설치했던, pam otp 모듈을 설정하고, 저장합니다.
sudo vi /etc/ssh/sshd_config
# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication yes
설정하고 저장합니다.
sudo systemctl restart sshd.service
설정해두었던 것을 적용하기 위해서 sshd서비스를 재시작합니다.
sudo vi /etc/ssh/sshd_config
UsePAM yes
# AuthenticationMethods publickey,password publickey,keyboard-interactive
AuthenticationMethods publickey,keyboard-interactive
otp를 모두 다 요구를 하는데, 사용할 방법만 쓰는 것을 추천합니다.
sudo vi /etc/pam.d/sshd
# Standard Un*x authentication.
#@include common-auth
PAM에서 주석 처리를 진행하지 않으면 비밀번호를 물어봅니다. 그래서 주석처리를 진행한 겁니다.
저는 Key + 2차 인증을 진행하고 싶기 때문이죠, 따라서 필요한 것만 진행하고 싶어서 주석 처리합니다.
이제 설정 사항을 다시 적용합니다.
sudo systemctl restart sshd.service
재접속을 진행해 OTP 적용이 잘되는지 확인합니다.

이제 예전보다 보안이 강화되었습니다.
'etc > 개발하다가 만난 삽질' 카테고리의 다른 글
file is a commonjs module it may be converted to an es6 module 해결 (0) | 2022.02.03 |
---|---|
[LInux] bash shell export (환경변수) (0) | 2022.01.13 |
Shell - 명령어 출력에서 단어 찾기 (0) | 2022.01.06 |
react useEffact (0) | 2021.12.02 |
[linux, mac] 디렉터리(폴더) 삭제, 복사하기 (0) | 2021.02.15 |