2011年4月5日 星期二

CentOS 做 NAT

本教學只做實際設定範例,詳細原理可以參考鳥哥的Linux網站 http://linux.vbird.org/


環境設定:


先設定好兩張網路卡的IP和網段,所有區網內的IP從eth1這個介面(private ip)進來,由eth0介面(public ip)出去


第一張網卡 eth0 : 


ip : 192.192.249.1
netmask : 255.255.255.0
brocast : 192.192.249.255
gateway : 192.192.249.254


第二張網卡 eth1:


ip : 192.168.100.1
netmask : 255.255.255.0
brocast : 192.168.1.255


1. 設定網路介面


# vim /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0
BOOTPROTO=static
TYPE=Ethernet
IPADDR=192.192.114.1
NETMASK=255.255.255.0
NETWORK=192.192.114.0
BROADCAST=192.192.114.255
ONBOOT=yes
GATEWAY=192.192.114.254


# vim /etc/sysconfig/network-scripts/ifcfg-eth1

DEVICE=eth1
BOOTPROTO=static
TYPE=Ethernet
IPADDR=192.168.100.1
NETMASK=255.255.255.0
NETWORK=192.168.100.0
BROADCAST=192.168.100.255
ONBOOT=yes


2. 開啟封包轉送


# echo "1" > /proc/sys/net/ipv4/ip_forward


3. 設定SNAT


# iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o eth0 -j SNAT --to 192.192.114.1


4. 檢查路由


[root@NAT ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
61.221.253.160  0.0.0.0         255.255.255.224 U     0      0        0 eth1
192.168.100.0   0.0.0.0         255.255.252.0   U     0      0        0 eth0
0.0.0.0         61.221.253.161  0.0.0.0         UG    0      0        0 eth1


 


5. 將設定寫入scripts , 並設定開機自動執行


以上測試成功後就把它寫成scripts讓電腦開機時自動執行


# vim /etc/rc.d/rc.local


/root/firewall.sh


# touch /root/firewall.sh


# chmod +x /root/firewall.sh


# vim /root/firewall.sh


#!/bin/bash


# 開啟封包轉送


echo "1" > /proc/sys/net/ipv4/ip_forward


# 清除防火牆規則


for table in filter nat mangle


do


 iptables -t $table -F


iptables -t $table -X


iptables -t $table -Z


done


# 設定NAT


iptables -t nat -A POSTROUTING -s 10.1.100.0/24 -o eth0 -j SNAT --to 192.192.114.1


沒有留言:

張貼留言

如何在 golang 裡面引用 gitlab private repo

參考文章:https://stackoverflow.com/questions/27500861/whats-the-proper-way-to-go-get-a-private-repository 原文是用 gitlab.company.com 當範例,這邊改成一般帳號...