วิธีการทำให้ Ubuntu กลายเป็น Router/Firewall/Gateway/DHCP/DNS
วันนี้เราจะมาทำการเปลี่ยนจาก Ubuntu Server ธรรมดาให้กลายเป็น Gateway สู่โลก internet ครับ โดยในที่นี้ Gateway ของเราจะครบถ้วนมาก เป็นทั้ง DHCP Server, DNS Server(Forward DNS Server), Firewall ในตัวทีเดียวเลยครับ
โดยในที่นี้ Ubuntu Server ของเราจะมี 2 ขาด้วยกัน
- eth0 – ขา Internal Network (LAN)
- eth1 – ขา External Network (WAN) ต่อออก internet
1. ติดตั้ง Ubuntu Server
2. Enable การ forwarding package โดยเข้าไปแก้ไข /etc/sysctl.conf จากนั้นแก้ไข configuration เป็น
1 |
net.ipv4.ip_forward=1 |
ทำการ reload configuration
1 |
sysctl -p /etc/sysctl.conf |
3. สร้าง iptables rule เป็น
1 2 3 |
iptables -A FORWARD -i eth1 -o eth0 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT iptables -A FORWARD -i eth0 -o eth1 -j ACCEPT iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE |
ทำการ save rule
1 |
iptables-save > /etc/default/firewall |
(หมายเหตุหากต้องการ setup deny rule ใดๆ สามารถดูรายละเอียดได้ที่ link นี้)
หากต้องการ log traffic ให้เพิ่ม rule เป็น
1 |
iptables -A POSTROUTING -o eth1 -j LOG |
4. Configuration network interface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# This file describes the network interfaces available on your system # and how to activate them. For more information, see interfaces(5). # The loopback network interface auto lo iface lo inet loopback # The primary network interface # LAN auto eth0 iface eth0 inet static address 192.168.210.1 netmask 255.255.255.0 post-up iptables-restore < /etc/default/firewall # WAN auto eth1 iface eth1 inet dhcp |
เพื่อทำการ reload iptables rule ทุกครั้งที่ network interface up
5. ติดตั้ง DHCP Server
1 |
apt-get install isc-dhcp-server |
สร้าง /etc/dhcp/dhcpd.conf
1 2 3 4 5 6 7 8 9 10 11 |
ddns-update-style none; default-lease-time 600; max-lease-time 7200; authoritative; log-facility local7; subnet 192.168.210.0 netmask 255.255.255.0 { range 192.168.210.100 192.168.210.250; option domain-name-servers 192.168.210.1, 8.8.8.8; option domain-name "secplayground"; option routers 192.168.210.1; } |
ทำการแก้ไข /etc/default/isc-dhcp-server ให้ค่า INTERFACES เป็น
1 |
INTERFACES="eth0" |
ทำการ restart dhcp server
1 |
/etc/init.d/isc-dhcp-server restart |
6. ทำการติดตั้ง Bind9
1 |
apt-get install bind9 |
ทำการแก้ไข /etc/bind/named.conf.options
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
acl internal { 192.168.210.0/24; localhost; localnets; }; options { directory "/var/cache/bind"; recursion yes; allow-query { internal; }; forwarders { 8.8.8.8; 8.8.4.4; }; forward only; dnssec-validation auto; auth-nxdomain no; # conform to RFC1035 listen-on-v6 { any; }; }; |
หากต้องการทำการ log DNS Query ทั้งหมด ให้เพิ่ม option นี้ใน named.conf.options
1 2 3 4 5 6 7 8 9 10 11 12 13 |
logging { channel query.log { file "/var/log/named/query.log"; severity dynamic; print-time yes; }; channel syslog { syslog local0; severity dynamic; print-time yes; }; category queries { query.log; syslog; }; }; |
7. ทำการ restart bind
1 |
/etc/init.d/bind9 restart |
8. ทดสอบการใช้งานที่ฝั่ง Client ว่าได้ IP หรือไม่, สามารถออก internet ได้หรือไม่