linux下的nat設定

來源:互聯網
上載者:User

標籤:linux營運

配置採用雙網卡:eth0、eth1 
    eth0的配置:[[email protected] network-scripts]# cat ifcfg-eth0

DEVICE=eth0

BOOTPROTO=static

BROADCAST=211.101.46.255

HWADDR=4C:00:10:59:6B:20

IPADDR=211.101.46.251

NETMASK=255.255.255.0

NETWORK=211.101.46.0

ONBOOT=yes

TYPE=Ethernet

eth1的配置: [[email protected] network-scripts]# cat ifcfg-eth1

DEVICE=eth1

ONBOOT=yes

BOOTPROTO=static

IPADDR=192.168.1.1

NETMASK=255.255.255.0

HWADDR=4C:00:10:10:03:55
TYPE=Ethernet

註:“我才用的是固定IP,外網IP:211.101.46.251,內網IP是:192.168.1.1”

DNS配置:
 [[email protected] ~]# cat /etc/resolv.conf

nameserver 192.168.1.1   內網DNS地址

nameserver 202.106.0.20  外網DNS地址

網關配置:
 [[email protected] /]# vi /etc/sysconfig/network

NETWORKING=yes

HOSTNAME=tpwb

GATEWAY=211.101.46.1    你的網關(211.101.46.251的網關)

 

[[email protected] sysconfig]# route

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

211.101.46.0    *               255.255.255.0   U     0      0        0 eth0

192.168.1.0     *               255.255.255.0   U     0      0        0 eth1

169.254.0.0     *               255.255.0.0     U     0      0        0 eth1

default      211.101.46.1    0.0.0.0     UG    0      0      0 eth0

保證default是 211.101.46.1

 重啟dns:
[[email protected] ~]# service named restart

現在開始啟用nat:
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j SNAT --to 211.101.46.251
註:-A:表示在所選擇的的鏈末添加一條或多條規則, POSTROUTING:修改準備出去的包 ,-s:源地址,-o:輸出介面,-j:指定規則的目標;也就是說,如果包匹配應當做什麼,--to:只用於nat表的POSTROUTING鏈,相當於給包發出時所經過介面的IP地址設定一個映像。“
 

 開啟網卡的轉寄功能:[[email protected] ~]#echo 1 > /proc/sys/net/ipv4/ip_forward

允許dns服務連接埠的tcp、udp資料包通過:
 [[email protected] ~]#iptables -A OUTPUT -p tcp --dport 53 -j ACCEPT

[[email protected] ~]#iptables -A OUTPUT -p udp --dport 53 -j ACCEPT
如果想重啟之後依然生效: /etc/rc.d/init.d/iptables save
vi /etc/rc.d/rc.local下添加一行:
 echo 1 > /proc/sys/net/ipv4/ip_forward儲存退出
[[email protected] ~]# ntsysv    
 在named,iptables的[]裡點空格見出現*號後,確定,表示開機自動載入!
重啟即可: 


本文出自 “wangconggao” 部落格,請務必保留此出處http://wangconggao.blog.51cto.com/3253868/1579695

linux下的nat設定

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.