Window가 아닌 Linux 서버를 쓰는 이유는 (1) 가볍고 (2) 오픈소스 기반의 무료 이기 떄문이다.
스타트업 초기 오픈소스로 사용하다 회사가 성장하여 필요한 경우 상용 라이센스를 구입할 수 있는 Redhat 기반의 CentOS 를 선택하였다. 보안성, 안정성, 범용성이 있으며 어떤 가상 머신에도 잘 올라간다.
또한, CentOS 는 Fedora 라는 업데이트 주기가 보다 짧은 선행 OS 를 기반으로 만들어지고 있다. 오픈소스는 업데이트 후 버그에 취약할 수 있는데, 버그에 대한 안정화 역할을 하는 OS가 앞단에 하나 더 있다는 점이 다른 리눅스에 비해 장점이다.
# 릴리즈 사이클 (과거)
Fedora → Redhat (상용) → CentOS (오픈소스)
# 릴리즈 사이클 (현재)
Fedora → CentOS (오픈소스) → Redhat (상용)
Linux 서버는 최초 설치한 후에 여러가지 초기 세팅 작업이 필요하다.
CentOS Stream 9 기준으로 설명한다.
CentOS 7 이라면 dnf 설치 대신 yum 설치로 하면 된다.
내용은 계속 추가될 예정입니다.
[hostname 변경]
hostnamectl set-hostname 호스트명
# 확인
hostname
[언어셋 변경]
localectl set-locale LANG=ko_KR.UTF-8
# 확인
localectl
System Locale: LANG=ko_KR.UTF-8
VC Keymap: kr
X11 Layout: kr
[서버 시각 동기화]
간과하기 쉽지만 서버 시각은 아주 중요하다. 로그 파일을 비롯한 모든 파일에 기록되기 때문이다. 시간이 안 맞으면, 언제 무슨일이 났었는지 추적이 어렵게 된다. 꼭 빼먹지 말고 설정하도록 한다.
timedatectl set-timezone Asia/Seoul
vi /etc/chrony.conf
# 기존 시간 서버 라인을 주석처리하고 time.bora.net 으로 대체한다.
#pool 2.centos.pool.ntp.org iburst
server time.kriss.re.kr iburst
systemctl enable --now chronyd
systemctl restart chronyd
#참고 NTP 서버 목록
서버주소 제공 time.bora.net LG유플러스 time.nuri.net 아이네트호스팅 ntp.kornet.net KT time.kriss.re.kr 한국표준과학연구원(KRISS) time.nist.gov NIST
# 확인
timedatectl
# 위 시간이 맞다면 OS 시간으로 H/W 시간을 맞춘다
hwclock --hctosys
#방화벽 오픈
[Epel Release 설치]
dnf config-manager --set-enabled crb
dnf -y install epel-release epel-next-release
#확인
dnf repolist
레포지터리 ID 레포지터리 이름 appstream CentOS Stream 9 - AppStream baseos CentOS Stream 9 - BaseOS crb CentOS Stream 9 - CRB epel Extra Packages for Enterprise Linux 9 - x86_64 epel-next Extra Packages for Enterprise Linux 9 - Next - x86_64 extras-common CentOS Stream 9 - Extras packages
[SSH 포트 변경]
#방화벽 SSH포트 변경 (예:22→1022)
firewall-cmd --permanent --zone=public --remove-port=22/tcp
firewall-cmd --permanent --zone=public --add-port=1022/tcp
firewall-cmd --reload
semanage port -a -t ssh_port_t -p tcp 1022
semanage port -l | grep ssh
ssh_port_t tcp 1022, 22
vi /etc/ssh/sshd_config
# 밖에서 안으로 접속할 때 설정은 ssh_config가 아니라 sshd_config이다.
# 주석을 삭제하고 포트번호를 지정
Port = 1022
# 주석 해제후 no로 변경 - root 로그인 막기
PermitRootLogin no
# 주석 해제 - 최대 로그인 시도 횟수
MaxAuthTries 5
# 주석 해제
PermitEmptyPasswords no
[세션 timeout 설정]
vi /etc/ssh/sshd_config
#클라이언트가 살아 있는지 체크하는 간격(초)
ClientAliveInterval 600
#클라이언트가 살아 있는지 확인하는 횟수
ClientAliveCountMax 3
즉, 600 x 3 = 1800초 (30분) 후에 접속이 끊긴다.
# 반영
systemctl restart sshd
# 보안을 위해 ssh root 로그인은 막고 일반 사용자 아이디로 로그인해서 su - root 로 권한을 얻어 사용한다.
[네트워크 보안 관련]
#네트워크 레벨에서 허가 불가 IP 설정하기 (SSH 가 192.168. 망에서만 되게 하기) - 필요한 경우 설정
vi /etc/hosts.allow
sshd: 192.168.
vi /etc/hosts.deny
sshd: ALL
# 시스템 혹은 개인 계정에 대한 timeout - 마지막 줄에 추가
vi ~/.bash_profile
export TMOUT=3600 (60분 타임아웃)
# 적용
source ~/.bash_profile
[fail2ban 설치]
여러가지 공격을 방어해주는 필수 보안 프로그램이다.
dnf install fail2ban
cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
vi /etc/fail2ban/jail.local
# sshd에 enabled 추가하고 sshd-ddos 도 추가한다. sshd / sshd-ddos 각각 port 에 수정한 port 번호도 맞게 변경해준다.
[sshd]
enabled = true
port = 1022
[sshd-ddos]
enabled = true
port = 1022
filter = sshd-ddos
logpath = %(sshd_log)s
maxretry = 3
만약, 포트 번호가 다른경우 port = ssh,1022 와 같이 ssh 를 같이 쓰면 제대로 동작하지 않는다.
port = 1022 이렇게 포트번호만 써야한다.
# 필터 파일을 추가한다.
vi /etc/fail2ban/filter.d/sshd-ddos.conf
# Fail2Ban configuration file
#
# Author: Yaroslav Halchenko
#
# $Revision$
#
[Definition]
# Option: failregex
# Notes.: regex to match the password failures messages in the logfile. The
# host must be matched by a group named "host". The tag "<HOST>" can
# be used for standard IP/hostname matching and is only an alias for
# (?:::f{4,6}:)?(?P<host>[\w\-.^_]+)
# Values: TEXT
#
failregex = sshd(?:\[\d+\])?: Did not receive identification string from <HOST>$
# Option: ignoreregex
# Notes.: regex to ignore. If this regex matches, the line is ignored.
# Values: TEXT
#
ignoreregex =
systemctl start fail2ban
systemctl enable fail2ban
journalctl -lfu fail2ban
# 위 로그에서 오류가 없이 시작되었는지 확인한다.
2월 09 23:32:16 webcom systemd[1]: Starting Fail2Ban Service...
2월 09 23:32:16 webcom systemd[1]: Started Fail2Ban Service.
2월 09 23:32:16 webcom fail2ban-server[25053]: 2023-02-09 23:32:16,498 fail2ban.configreader [25053]: WARNING 'allowipv6' not defined in 'Definition'. Using default one: 'auto'
2월 09 23:32:16 webcom fail2ban-server[25053]: Server ready
동작중인 감옥 현황
# fail2ban-client status
로그 확인
# vi /var/log/fail2ban.log
차단 푸는법 - SSH 차단 된 경우 아래 방법으로 차단 해제를 한다. 재시작은 불필요하다.
# fail2ban-client set sshd unbanip 192.xxx.xxx.xxx
# SSH 로그인 암호를 일부러 틀린 후, 위 차단 푸는법으로 fail2ban 이 정상 동작하는지 확인해본다.
[fail2ban 설정 추가] - 이 부분은 필요한 경우에만 추가한다.
# 웹서버에만 apache 필터를 추가로 막는다.
#.Program으로 HTTP, HTTPS Protocol을 통해 Server에 접근 차단
[apache-badbots]
enabled = true
#.Proxy를 사용 Server를 경유하여 접근하려는 시도를 검출하여 해당 IP를 차단
vi /etc/fail2ban/filter.d/apache-proxy.conf 파일 추가
[Definition]
failregex = ^(?:(?![0-9\.]* - .*? \[.*\] "([A-Z]* /.* HTTP/1\.[0-9]|-)")<HOST>)
ignoreregex =
vi /etc/fail2ban/jail.local 내용 추가
[apache-proxy]
enabled = true
port = http,https
filter = apache-proxy
logpath = /var/log/apache*/*access.log
maxretry = 0
#. Apache 오버플로우를 시도 차단
[apache-overflows]
enabled = true
#. Apache POST flood 공격 차단
vi /etc/fail2ban/filter.d/apache-postflood.conf 파일 추가
[Definition]
failregex = ^ -.*”POST.*
ignoreregex =
vi /etc/fail2ban/jail.local 내용 추가
[apache-postflood]
enabled = true
filter = apache-postflood
logpath = /var/log/httpd/access_log
findtime = 10
maxretry = 10
badbot (크롤링) 막기 해제
fail2ban-client set apache-badbots unbanip 192.168.0.1
# (참고) iptables 로 브루투스 공격 막기
# 22번 포트로 특정 IP에서 60초 동안에 11번째 접속이 들어오면 60초간 막아버려라
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --set --name BFORCE_22
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --update --seconds 60 --hitcount 10 --rttl --name BFORCE_22 -j DROP
[보안 패치 자동으로하게 설정]
dnf -y install dnf-automatic
# 설치 확인
rpm -qi dnf-automatic
vi /etc/dnf/automatic.conf
# security 인 경우에만 자동 업데이트
upgrade_type = security
# 현재 호스트명으로 변경
system_name = 호스트명
# 설치하라
apply_updates = yes
systemctl enable --now dnf-automatic.timer
#Cockpit GUI 클라이언트에서도 자동 업데이트 설정이 가능하니 한번 확인해본다.
[해외 IP 특정 국가 차단]
아래 글을 참고로 하여 세팅한다.
https://ryans-note.tistory.com/9
CentOS Stream 9 - 해외 IP 차단하기 (중국 등)
Root로 작업한다. (1) mkdir /root/ipblock (2) 전세계 IP 파일을 다운로드 받는다. 아래 주소에 접속해서 가입(Sign Up)하고 라이센스 키를 발급받는다. 라이센스 키는 다시 보여주지 않으므로 반드시 메모
ryans-note.tistory.com
공감과 댓글은 좋은 글이 계속 올라오도록 힘이 됩니다.
'Linux' 카테고리의 다른 글
Jenkins - Git 연동하기 (Public Key 공개키 인증) (0) | 2023.02.18 |
---|---|
Linux CentOS Stream 9 - Git Install 설치 (0) | 2023.02.18 |
Linux CentOS Stream 9 - phpmyadmin 다른 포트로 변경하기 (apache 2.4) (0) | 2023.02.17 |
Linux CentOS - Selinux 관련 명령 (권한 Permission 오류가 발생하는 경우) (0) | 2023.02.16 |
MySQL DUMP import / export 덤프 임포트 익스포트 마이그레이션 (0) | 2023.02.11 |