Linux/iptables

[Linux] Ubuntu 서버에 iptables NAT 설정

GGkeeper 2021. 11. 13. 05:35

실습> Ubuntu Server 추가하기

DNAT 설정
192.168.108.101 -> 192.168.102.101
192.168.108.102 -> 192.168.102.102

          +-- ens33
          |
          |   +-- ens33 192.168.108.101, 192.168.108.102
          |   |
          |   |     +-- ens37          +-- ens33
          |   |     |                  |
외부망 +---+[server1]+---- 내부망 --------+[server2] 192.168.102.101 VMnet2 (WEB Server)
     |    |   F/W   |                  |   
     |    |         |                  |
     |    |         |                  [Ubuntu Server] 192.168.102.102 VMnet2 (WEB Server)
     |    |         |                    
     |    |         |                  (GateWay : 192.168.102.254)  
     |    |         192.168.102.254  VMnet2
     |    |
     |    192.168.108.3  VMnet8(NAT) (GateWay : 192.168.108.2)
     |
     192.168.108.2


1. 웹 서버 설치
apt : 패키지 관리 툴 (Advanced Package Tool 로 CentOS의 yum과 동일)
apache2 : 아파치 웹 서버 패키지 (CentOS httpd)

# lsb_release
No LSB modules are available.
root@linuxmasternet:~# cat /etc/os-release

PRETTY_NAME="Ubuntu 21.10"
NAME="Ubuntu"
VERSION_ID="21.10"
VERSION="21.10 (Impish Indri)"
VERSION_CODENAME=impish
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=impish


# apt -y install apache2
# systemctl enable apache2
# cd /var/www/html
# echo "Hello Ubunu server" > index.html
# ip a

    inet 192.168.108.7/24 brd 192.168.108.255 scope global ens33

http://192.168.108.7
Hello Ubuntu Server

2. 네트워크 설정
Ubuntu Server 18.04, Ubuntu Server 20.04, Ubuntu Server 21.10
설정 파일 : /etc/netplan/<yaml파일>

고정 IP주소일 경우 
# vi /etc/netplan/00-installer-config.yaml

# This is the network config written by 'subiquity'
network:
  ethernets:
    ens33:
      addresses:
      - 192.168.102.102/24
      gateway4: 192.168.102.254
      nameservers:
        addresses:
        - 168.126.63.1
        search: []
  version: 2


네트워크 재시작
netplan apply

3. 방화벽 서버에 IP 주소 추가
ens33 인터페이스에 192.168.108.102/24 를 추가한다.
vmnet 1# nmtui
ens33 : 192.168.108.102/24

# systemctl restart network

4. 방화벽 룰 설정
iptables-save : 방화벽 룰을 덤프(설정된 룰을 화면에 출력)하는 명령어
iptables-restore : 방화벽 룰을 복구하는 명령어

# iptables-save > nat.txt
# vi nat.txt

# Generated by iptables-save v1.4.21 on Thu Nov  4 19:28:49 2021
*filter
:INPUT ACCEPT [792:59633]
:FORWARD ACCEPT [231:14623]
:OUTPUT ACCEPT [447:70637]
COMMIT
# Completed on Thu Nov  4 19:28:49 2021
# Generated by iptables-save v1.4.21 on Thu Nov  4 19:28:49 2021
*nat
:PREROUTING ACCEPT [1661:106236]
:INPUT ACCEPT [5:332]
:OUTPUT ACCEPT [223:16916]
:POSTROUTING ACCEPT [297:21514]
-A PREROUTING  -d 192.168.108.101/32 -m comment --comment "108.101 -> 102.101" -j DNAT --to-destination 192.168.102.101
-A PREROUTING  -d 192.168.108.102/32 -m comment --comment "108.102 -> 102.102" -j DNAT --to-destination 192.168.102.102
-A POSTROUTING -s 192.168.102.101/32 -m comment --comment "102.101 -> 108.101" -j SNAT --to-source 192.168.108.101
-A POSTROUTING -s 192.168.102.102/32 -m comment --comment "102.102 -> 108.102" -j SNAT --to-source 192.168.108.102
COMMIT
# Completed on Thu Nov  4 19:28:49 2021


nat.txt 파일에 설정된 룰을 복구한다.
# iptables-restore nat.txt

현재 설정된 방화벽 룰을 확인한다.
# iptables -t nat -nL

Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination
DNAT       all  --  0.0.0.0/0            192.168.108.101      /* 108.101 -> 102.101 */ to:192.168.102.101
DNAT       all  --  0.0.0.0/0            192.168.108.102      /* 108.102 -> 102.102 */ to:192.168.102.102

Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination
SNAT       all  --  192.168.102.101      0.0.0.0/0            /* 102.101 -> 108.101 */ to:192.168.108.101
SNAT       all  --  192.168.102.102      0.0.0.0/0            /* 102.102 -> 108.102 */ to:192.168.108.102


5. 통신 확인

root@linuxmasternet:~# ip a

1: lo: <loopback,up,lower_up> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000</loopback,up,lower_up>
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens33: <broadcast,multicast,up,lower_up> mtu 1500 qdisc fq_codel state UP group default qlen 1000</broadcast,multicast,up,lower_up>
    link/ether 00:0c:29:c5:30:56 brd ff:ff:ff:ff:ff:ff
    altname enp2s1
    inet 192.168.102.102/24 brd 192.168.102.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fec5:3056/64 scope link
       valid_lft forever preferred_lft forever


root@linuxmasternet:~# ping 8.8.8.8 -c 3

PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=127 time=46.0 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=127 time=49.2 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=127 time=47.0 ms

--- 8.8.8.8 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2005ms
rtt min/avg/max/mdev = 45.996/47.395/49.230/1.355 ms


웹 페이지에 접속해서 페이지가 잘 뜨면 성공이다.
http://192.168.108.102/
Hello Ubuntu Server