본문 바로가기

Developer/Linux

CentOS7 iptables 설정(Firewalld 해제)

반응형

CentOS7 이후 부터는 기존의 iptables에서 Firewalld 로 방화벽 기본설정이 변경되었습니다.

예전의 글 중에 iptables로 설정을 잡는 것을 적어놓은 포스팅이 있어 Firewalld를 해제하고, iptables를 사용하는 법을 정리하도록 하겠습니다.

 

 

1. 먼저 Firewalld를 중지하도록 합니다.

# systemctl stop firewalld 
# systemctl mask firewalld

 

2. iptables를 설치합니다.

# yum install iptables 
# yum install iptables-services

 

3. iptables가 제대로 설치되었는지 확인합니다.

# rpm -qa | grep iptables

다음과 같이 표시되면 제대로 설치가 된 것입니다.

[root@localhost ~]# rpm -qa | grep iptables
iptables-1.8.4-20.el8.x86_64
iptables-ebtables-1.8.4-20.el8.x86_64
iptables-services-1.8.4-20.el8.x86_64
iptables-libs-1.8.4-20.el8.x86_64

 

4. 재부팅을 하더라도 자동으로 iptables가 시작되도록 설정 합니다.

# systemctl enable iptables

 

5. 이제 iptables를 직접 시작합니다.

# systemctl start iptables

※ start 대신에 stop, restart를 이용해서 iptables를 멈추거나 다시 시작하도록 할 수 있습니다.

 

6. iptables가 제대로 작동하는지 확인해 보겠습니다.

# systemctl status iptables


아래와 같이 OK가 나오면 제대로 잘 돌아가고 있는거겠죠..??ㅎㅎ

[root@localhost ~]# systemctl status iptables
* iptables.service - IPv4 firewall with iptables
   Loaded: loaded (/usr/lib/systemd/system/iptables.service; enabled; vendor preset: disabled)
   Active: active (exited) since Thu 2021-11-25 23:45:26 EST; 2s ago
  Process: 57572 ExecStart=/usr/libexec/iptables/iptables.init start (code=exited, status=0/SUCCESS)
 Main PID: 57572 (code=exited, status=0/SUCCESS)

11<BF><F9> 25 23:45:26 localhost.localdomain systemd[1]: Starting IPv4 firewall with iptables...
11<BF><F9> 25 23:45:26 localhost.localdomain iptables.init[57572]: iptables: Applying firewall rules: [  OK  ]
11<BF><F9> 25 23:45:26 localhost.localdomain systemd[1]: Started IPv4 firewall with iptables.

 

Firewalld를 해제하고 iptables를 실행하는 것은 여기까지로 완료되었습니다.

 

추가로 웹사이트나 필요한 설정을 아래와 같이 진행하시면 되겠습니다.

방화벽 설정을 변경하기 위해 다음과 같이 vi편집기를 실행합니다.

# vi /etc/sysconfig/iptables

 

그리고 사용 할 포트를 추가해 주시면 됩니다

'i'키를 눌러 insert 모드로 변경하고, 아래의 내용을 상황에 맞게 입력해 주시면됩니다.

(포트번호 : ssh(21), ftp(22), httpd(80))

-A INPUT -p tcp -m state --state NEW -m tcp --dport 21 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT

:w를 눌러 저장한 후 :q로 종료하고 나옵니다.

 

이제 iptables와 httpd를 재시작하면 끝..!!! 입니다.

# systemctl restart iptables
# systemctl restart httpd
반응형