본문 바로가기

Technical/Network

Linux NAT router 설정하기 - Ubuntu 14.4 dom0, xen pv guest 환경



외부 네트워크(192.168.25.*)와 내부 네트워크(10.10.*.*)가 분리된 상황에서 내부 네트워크의 머신들이 wget이나 패키지 설치 등의 외부(인터넷) 접속을 통한 작업을 진행할 경우의 routing, ip forwarding 을 설정하는 과정에 대해 정리한다. 이러한 기능의 핵심이 되는 서버는 그림에서 보듯이 NAT router이다(참고: 본 테스트의 수행은 Ubuntu 14.4 LTS, Xen host 상에서, 여러 개의 ubuntu guest vm들을 생성하여 진행하였다).


1. Gateway 서버와 내부 서버의 NIC 설정


* gw1(NAT router, 여기서의 서버 이름은 ubuntu14-pvha1) 서버는 2 개의 네트워크 인터페이스(NIC)을 가진다

 - eth0, ip:192.168.25.201/24, internet gw ip:192.168.25.1(인터넷공유기-라우터의 외부게이트웨이)

 - eth1, ip:10.0.0.1/16


* n개의 내부서버(여기서의 서버 이름은 ubuntu14-pv1~ 4)는 각각 1개씩의 네트워크 인터페이스를 가진다

 - Netmask 는 255.255.0.0 즉 /16 이므로 gw1의 eth1 과 동일 네트워크가 된다

 - eth0, ip:10.0.10.1 ~ 10.0.10.4/16, gateway ip:10.0.0.1


* 그림에 걸맞는 상황을 설정하기 위해 각 서버의 NIC의 ip 주소를 static으로 설정한다. ifconfig 명령을 쓸 수도 있지만, 리부트 후에도 계속 작동하도록 config file을 직접 수정한다(NIC이 여러 개일지라도 gateway 는 1개만 존재해야 함에 유의).

root@ubuntu14-pvha1:~# vi /etc/network/interfaces

# The loopback network interface

auto lo

iface lo inet loopback


# The primary network interface

auto eth0

#iface eth0 inet dhcp

iface eth0 inet static

address 192.168.25.201

netmask 255.255.255.0

network 192.168.25.0

broadcast 192.168.25.255

gateway 192.168.25.1

dns-nameservers 8.8.8.8


# The secondary network interface

auto eth1

iface eth1 inet static

address 10.0.0.1

netmask 255.255.0.0

network 10.0.0.0

broadcast 10.0.255.255


root@ubuntu14-pv4:~# vi /etc/network/interfaces
# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
#iface eth0 inet dhcp
iface eth0 inet static
address 10.0.10.4
netmask 255.255.0.0
network 10.0.0.0
broadcast 10.0.255.255
gateway 10.0.0.1
dns-nameservers 8.8.8.8

* 각 서버들의 NIC 설정을 마친 후 /etc/init.d/networking restart 또는 reboot 를 한 후 내부 외부에 대한 접속이 가능한지를 ping test 해 본다. 당연히 2개의 NIC을 통해 외부 및 내부와의 접속이 모두 가능하다.

root@ubuntu14-pvha1:~# ping 8.8.8.8

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=52 time=36.0 ms

64 bytes from 8.8.8.8: icmp_seq=2 ttl=52 time=35.5 ms ...


root@ubuntu14-pvha1:~# ping 10.0.10.4

PING 10.0.10.4 (10.0.10.4) 56(84) bytes of data.

64 bytes from 10.0.10.4: icmp_seq=1 ttl=64 time=0.351 ms

64 bytes from 10.0.10.4: icmp_seq=2 ttl=64 time=0.517 ms ...


* n개의 내부서버 중 4번에서 외부로 ping test 를 수행해 본다. ping 수행이 실패한다. 다음 단계에서 해결해 보자.

root@ubuntu14-pv4:~# ping 8.8.8.8

PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.

^C

--- 8.8.8.8 ping statistics ---

11 packets transmitted, 0 received, 100% packet loss, time 10078ms


2. NAT Router 설정, 그리고 테스트


* NAT router 서버에서 다음과 같은 과정을 거쳐 설정을 진행한다

root@ubuntu14-pvha1:~# modprobe iptable_nat -> iptable_nat 커널모듈의 사용을 가능하게 한다


root@ubuntu14-pvha1:~# vi /etc/sysctl.conf -> 아래 라인을 찾아 comment 를 해제한다

net.ipv4.ip_forward=1


root@ubuntu14-pvha1:~# sysctl -p (또는 리부트, 변경된 Kernel 옵션을 작동시킴)

* echo 1 > /proc/sys/net/ipv4/ip_forward 로 명령어를 통해 설정할 수도 있으나, 리부트시에는 설정이 사라지므로 여기서는 사용하지 않는다.


root@ubuntu14-pvha1:~# cat /proc/sys/net/ipv4/ip_forward ->설정 값이 1인지 확인

1


root@ubuntu14-pvha1:~# vi /etc/rc.local (부팅 후 최종 수행 script, 아래 2개 라인을 추가, 완료시 리부트)

iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

iptables -A FORWARD -i eth1 -j ACCEPT


* 내부서버에서 외부로 접속이 가능한지 최종 테스트를 해 본다

  (참고: /etc/network/interfaces 에서 gateway 를 잡지 않았다면 route add default gw 10.0.0.1 로 따로 잡아 주어야 한다)

root@ubuntu14-pv4:~# ping 8.8.8.8

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=51 time=36.2 ms

64 bytes from 8.8.8.8: icmp_seq=2 ttl=51 time=36.0 ms

^C

--- 8.8.8.8 ping statistics ---

2 packets transmitted, 2 received, 0% packet loss, time 1001ms

rtt min/avg/max/mdev = 36.099/36.172/36.246/0.203 ms


root@ubuntu14-pv4:~# wget www.naver.com

--2015-06-30 17:31:58--  http://www.naver.com/

Resolving www.naver.com (www.naver.com)... 202.179.177.22, 125.209.222.141

Connecting to www.naver.com (www.naver.com)|202.179.177.22|:80... connected.

HTTP request sent, awaiting response... 200 OK

Length: unspecified [text/html]

Saving to: ‘index.html’


    [ <=>                                                                           ] 82,478      --.-K/s   in 0.008s  


2015-06-30 17:31:58 (10.3 MB/s) - ‘index.html’ saved [82478]


[관련 글]

2015/06/28 - [Xen 가상화 2] ubuntu pv guest on Ubuntu 14.4 LTS, Xen 4.4.1 - 설치 및 설정 가이드

2015/07/01 - Haproxy, Keepalived, nginx 를 이용한 고가용성(High Availablity) 웹서비스 환경 구현



- Barracuda -