[CentOS] sshd 설정 파일 /etc/ssh/sshd_config 살펴보기

Anna-jeong·2023년 2월 20일
0

ec2인스턴스 생성해 AWS 접속
OS > red hat 7.9 버전

vi /etc/ssh/sshd_config

BastionHost Config

  • root login 설정(sshd)
PermitRootLogin yes
#PermitRootLogin no

#생략

PasswordAuthentication yes
#PasswordAuthentication no

ChallengeResponseAuthentication yes
#ChallengeResponseAuthentication no

sshd_config 파일 주요 설정

port 22

  • ssh가 사용할 기본 포트 지정

Allowusers user1 root

  • 로그인 허락할 계정을 기록

  • user1과 root 두 계정에게만 로그인 허용

Protocol 2

  • openssh는 프로토콜 버전을 원하는 대로 선택

ListenAddress 0.0.0.0

  • sshd 데몬이 통신가능한 주소

  • 0.0.0.0은 모든 네트워크

HostKey for protocol version 1
HostKey /etc/ssh/ssh_host-key

KeyRegenerationInterval 3600 -->protocol 1 사용안함

  • 서버의 키는 한번 접속 이뤄진 뒤 자동적으로 다시 만듦
    다시 만드는 목적은 나중에 호스트의 세션에 있는 키를 캡처해서
    암호를 해독하거나 훔친 키를 사용하지 못하도록 하기 위함

  • 값이 0이면 키는 다시 만들어지지 않는다. 기본값은 3600최다.

ServerKeyBits 1024

  • 서버 키에서 어느정도의 비트수를 사용할지 정의

  • 최소값은 512, 디폴트 값 768

#Autentication:
LoginGraceTime 600

  • 유저의 로그인이 성공적으로 이루어지지 않았을 때 이 시간 후에
    서버가 연결을 끊는 시간이다.

  • 값이 0이면 제한 시간이 없다. 기본값 600초

  • PermitEmptyPasswords no

패스워드 인증을 할 때 서버가 비어있는 패스워드를 인정하는 것

  • PermitRootLogin yes

root 로그인 허용여부를 결정하는 것

  • PasswordAuthentication yes

패스워드 인증을 허용

  • ChallengeResponseAuthentication yes

Challenge-Response 인증을 허용할지 여부 설정

no인 경우 mfa 인증 불가

sudo설정

sudo 권한 부여
Allow root to run any commands anywhere

패스워드가 없이 sudo 권한 부여
Read drop-in files from /etc/sudoers.d (the # here does not mean a comment)
#includedir /etc/sudoers.d

특정명령어 enable
(example 1)
someuser ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom

(example 2)
someuser (host)=(runUser:[runGroup]) option:

someuser는 (host)에서 (command)를 [runGroup]의 runUser의 권한으로 시행 가능함

로그 설정

SSH LogLevel설정

vi /etc/ssh/sshd_config

LogLevel DEBUG3

sshd 재시작

systemctl restart sshd

SCP Log 출력

tail - 1000f /var/log/secure | egrep “accepted|scp|Connection”

로그 파일 생성

vi /etc/logrotate.d/secure
#추가
/var/log/secure
{
weekly #주단위
rotate 260 #260주로 적재
compress #압축활성화
missingok #로그파일이 없어도 에러처리X
create 600 root root #권한설정
dateext #YYYYMMDD확장자 추가
sharedscripts #pre/postrotate한번만실행
postrotate #순환작업 후 실행할 작업 설정
     /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
endscript
}

prerotate / endscript: 순환작업 전 실행할 작업 설정
postrotate / endscript: 순환작업 후 실행할 작업 설정

ostrotate #순환작업 후 실행할 작업 설정
/bin/kill -HUP cat /var/run/syslogd.pid 2> /dev/null 2> /dev/null || true

 # SIGHUP 을 syslogd 로 보내 모든 오류 메시지를 버리고 성공하면 true를 반환

endscript

crontab 추가

crontab -e
*    *     *       *       * command(s)
분(0-59)  시간(0-23)  일(1-31)  월(1-12)   요일(0-7)

crontab -e 
  • 0 * * * /usr/sbin/logrotate /etc/logrotate.conf

로그주기설정

vi /etc/logrotate.conf

monthly #monthly로 수정

/var/log/*{
missingok
monthly
create 0600 root utmp
rotate 12
}

systemctl restart rsyslog

selinux 해제

vi /etc/selinux/config

#SELINUX=enforcing
SELINUX=disabled

reboot (or # setenforce 0)

Google Authenticator 설치

yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

yum -y install google-authenticator

첫 로그인 시 MFA 강제 설정 script

vi /etc/profile.d/mfa.sh

if [ ! -e ~/.google_authenticator ]  &&  [ $USER != "root" ]; then
google-authenticator --time-based --disallow-reuse --force --rate-limit=3 --rate-time=30 --window-size=3
echo
echo "Save the generated emergency scratch codes and use secret key or scan the QR code to
register your device for multifactor authentication." 
echo
echo "Login again using your ssh key pair and the generated One-Time Password on your registered
device." 
echo
logout
fi

Error 해결

root 계정으로 접속
service sshd rstart (sshd[ssh 데몬] 재시작) 명령어 입력 시
ADVERTISEMENT Unable to register authentication agent: GDBus.Error:org.freedesktop.PolicyKit1.Error.Failed
해당 에러가 발생하는 문제가 발생. 또한 polkit 데몬이 작동하지 않음.

문제 원인: 데몬 서비스가 동작하지 않았으나 실제 원인은 root 디렉토리의 권한을 바꾸면서 발생.(권한 문제)

해결 조치: root 디렉토리의 권한이 744로 되어 있는 상태를 555 상태로 변경함
접근권한 / 퍼미션
744 - 소유자에게 모든 접근 가능, 그룹에게 읽기 가능, 기타에게 읽기 가능
555 - 소유자, 그룹, 기타에게 읽기, 실행 가능

0개의 댓글