[centos] 방화벽

2019. 4. 11. 22:19study/centos 7 server & network

실습할 네트워크 구조

웹서버(centos text)를 운영하고, 방화벽(centos 7)통해 외부에서의 접근(ubuntu 18.04)을 제어한다.

(이때, centos 7의 ens33과 ubuntu의 아이피는 공인 아이피라고 가정한다.)



1. centos text 네트워크 설정


1.1 bridge 모드


: Edit virtual machine settings > Network Adapter > Bridged 설정으로 bridge 모드로 설정해준다.



1.2 ip, gateway 설정

[root@localhost ~] # nmtui 

: nmtui 명령어를 사용하면 다음과 같이 ens33의 네트워크 설정을 할 수 있는 창이 뜬다.


: 위의 네트워크 구조에 맞게 ip, 게이트웨이 설정을 한다.


ip : 10.1.1.20/24

gateway : 10.1.1.1 (centos 7 ens37 주소)

이때, dns server 주소는 kt dns server 주소를 넣어준다.


: ip addr을 통해 설정된 내용을 확인할 수 있다.



2. centos 7 네트워크 설정


2.1 랜카드 추가


: 모든 설정이 끝나면 Network Adapter 2처럼 새로운 랜카드가 추가된 것을 볼 수 있다.



2.2 ip, gateway 설정


: centos 7에서도 위의 네트워크 구조에 맞게 ip, gateway 주소를 설정한다.

단, 새로 생긴 인터페이스 (제 환경에서는 ens37이었습니다.) 에 설정해줘야 합니다. )


: ip addr 명령을 통해 설정된 내용을 확인할 수 있다.



3. bridged 컴퓨터들의 외부접속을 위한 준비 작업 (ip 포워딩)


3.1 /etc/sysctl.conf 수정

net.ipv4.ip__forward=1


3.2 

[root@localhost ~] # echo 1 > /proc/sys/net/ipv4/ip_forward

: 아무 메세지가 나오지 않으면 이상 x


위의 작업들이 어떤 설정을 하는 것인지 좀 더 찾아봐야할 것 같다...



4. iptables 설정 

: centos 7의 경우 iptables 대신 firewalld가 기본 방화벽 시스템으로 설정되어 있다.

따라서, iptables를 사용하기 위해서는 현재 작동 중인 firewalld 데몬을 중지 시킨 후 재부팅시에도 올라오지 않도록 설정해야 한다.


[root@localhost ~] # systemctl stop firewalld

[root@localhost ~] # systemctl mask firewalld

: 만약 iptables가 설치 되어있지 않으면 yum -y install iptables-services로 설치해준다.


iptables 설정에 대한 더 자세한 내용은 다음 포스팅에 나와있습니다.


4.1 iptables 정책 초기화

[root@localhost ~] # iptables --policy FORWARD DROP

[root@localhost ~] # iptables --policy INPUT DROP

[root@localhost ~] # iptables --policy OUTPUT DROP



4.2 INPUT 정책 설정 (ens37-내부 네트워크와 연결된 장치)

[root@localhost ~] # iptables --append INPUT --in-interface ens37 --source 10.1.1.0/24 --match state --state NEW, ESTABLISHED --jump ACCEPT

: 10.1.1.0 네트워크 대역에서 ens37 인터페이스로 들어오는 모든 패킷을 허용한다(=접근을 허용한다).

* 10.1.1.0~10.1.1.255 범위에 속하는 모든 ip(출발지)에 위의 정책을 적용한다. 


4.3 OUTPUT 정책 (ens37-내부 네트워크와 연결된 장치)

[root@localhost ~] # iptables --append OUTPUT --out-interface ens37 --destination 10.1.1.0/24 --match state --state NEW, ESTABLISHED --jump ACCEPT

: ens37 인터페이스에서 10.1.1.0 대역으로 나가는 모든 패킷을 허용한다(=접근을 허용한다).


즉, 4.2와 4.3 설정은 centos text에서 centos 7로 보내는 패킷과  centos 7에서 centos text로의 패킷에 대한 정책이다.


4.4 FORWARD 정책 (ens37-내부 네트워크와 연결된 장치)

[root@localhost ~] # iptables --append FORWARD  --in-interface ens37 --source 10.1.1.0/24 --destination 0.0.0.0/0 --match state --state NEW, ESTABLISHED --jump ACCEPT

: 내부 10.1.1.0 대역에서 외부 모든 ip로의 모든 패킷을 허용한다.


즉, 4.4 설정은 centos text(내부 네트워크)에서 centos 7를 거쳐 외부로 나가는 패킷에 대한 정책이다. 


4.5 FORWARD 정책 (ens33-외부 네트워크와 연결된 장치)

[root@localhost ~] # iptables --append FORWARD --in-interface ens33 --destination 10.1.1.0/24 --match state NEW, ESTABLISHED --jump ACCEPT

: 외부에서 10.1.1.0 대역으로의 모든 패킷을 허용한다 . 


즉, 4.5 설정은 외부에서 centos 7을 거쳐 centos text(내부 네트워크)로 들어오는 패킷에 대한 정책이다.


4.6 MASQUERADE 설정

[root@localhost ~] # iptables --table nat --append POSTROUTING --out-interface ens33 --jump MASQUERADE

: 내부 네트워크가 외부로 나갈 수 있도록 설정한다.


마스커레이드

: 사설 ip주소를 공인 ip 주소로 가장하여 외부 인터넷으로 나갈 수 있도록 하는 네트워크 기법

즉, centos text(사설 ip)가 외부 인터넷으로 나갈 때, 마치 centos 7(ens 33-공인 ip(가정))가 나가는 것처럼 가장하여 인터넷이 가능하도록 한다. 이렇게 되면 ens37와 연결된 모든 컴퓨터는 인터넷이 가능해지는 것이다.



5. iptables 변경 내용 저장
[root@localhost ~] # system iptables save  

위의 명령어가 되지 않을 경우
[root@localhost ~] # iptables-save > /etc/sysconfig/iptables


6. 웹 접속 
맨 위의 네트워크 구상도를 보면 centos text(10.1.1.20)에 웹서버가 가동 중이다.
만약, ubuntu에서 웹 페이지에 접속하려고 한다면 어떻게 될까? 
ubuntu는 실제 웹 서버가 돌아가고 있는 centos text의 ip 주소를 모르기 때문에 현재 상태에서는 접속할 수가 없다.

따라서 centos 7으로 http 요청(웹 접속)을 한다면 centos text의 웹 서버로 연결해주는 정책이 필요하다.
즉, ubuntu는 centos 7을 웹서버로 알고 접속하지만, 실제로 웹서버는 centos text(사설 네트워크 내부)인 것이다.

6.1 iptables 설정

[root@localhost ~] # iptables --table nat --append PREROUTING --proto tcp --in-interface ens33 --dport 80 --jump DNAT --to--destination 10.1.1.20

: 외부에서 ens33으로 http(80)으로 요청이 들어오면 10.1.1.20으로 연결해준다. 


6.2 centos text 웹 페이지 


순서 : httpd 설치 > 방화벽 서비스 등록 > 웹 페이지 작성


[root@localhost ~] # yum -y install httpd

[root@localhost ~] # firewall-cmd --add-service=httpd

[root@localhost ~] # vim /var/www/html/index.html




6.3 ubuntu에서 접속


: centos 7의 ip(외부와 연결된 인터페이스 ens33의 ip)를 확인하면 192.168.106.128이다.  



[설정 후]

ubuntu에서 웹 브라우저로 192.168.106.128로 접속을 하면, 

즉, 외부에서 centos 7의 ip(ens33)로 접속하게 되면 방화벽 정책에 따라 

내부의 centos text 웹 서버(10.1.1.20)로 접속이 되는 것을 볼 수 있다.

'study > centos 7 server & network' 카테고리의 다른 글

[centos] firewalld  (0) 2019.04.22
[centos] iptables  (0) 2019.04.15
[centos] ftp  (0) 2019.04.09
[centos] ssh  (0) 2019.04.09
[centos] telnet  (0) 2019.04.09