您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)CentOS7下配置ip forward的方法,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
Linux 中ip forward功能是一個(gè)比較方便實(shí)用的功能,只需要簡單配置,添加幾條路由,即可實(shí)現(xiàn)ip轉(zhuǎn)發(fā)。本文基于CentOS 7下采取一臺(tái)機(jī)器充當(dāng)虛擬路由,來實(shí)現(xiàn)Windows環(huán)境與Linux環(huán)境中相互通信。具體見下文,供大家參考。
一、環(huán)境及需求說明
本地測試環(huán)境,由于Win7+VMware這臺(tái)機(jī)器網(wǎng)卡太新,無法被VMware EXSi識(shí)別,無奈之下就直接裝了個(gè)Win7,然后再上面基于Vmware workstation 12開啟了一些臨時(shí)測試的虛擬機(jī)。其次本地直連網(wǎng)段192.168.1.0 IP有限,而又希望直接從本機(jī)(如圖Win10) ssh連接到這些虛擬機(jī),因此考慮使用Linux的ip轉(zhuǎn)發(fā)功能來實(shí)現(xiàn)。
上述圖片中已經(jīng)列出了各個(gè)節(jié)點(diǎn)的IP信息,紅色的虛擬機(jī)充當(dāng)軟路由,配置好之后,實(shí)現(xiàn)Win10 直接ssh到虛擬機(jī)。
二、虛擬路由服務(wù)器配置
###查看網(wǎng)卡信息 [root@centos7-router ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 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: eno16777728: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:9c:eb:2d brd ff:ff:ff:ff:ff:ff inet 172.24.8.254/24 brd 172.24.8.255 scope global eno16777728 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe9c:eb2d/64 scope link valid_lft forever preferred_lft forever 3: eno33554960: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:9c:eb:37 brd ff:ff:ff:ff:ff:ff inet 192.168.1.175/24 brd 192.168.1.255 scope global dynamic eno33554960 valid_lft 82384sec preferred_lft 82384sec inet6 fe80::20c:29ff:fe9c:eb37/64 scope link valid_lft forever preferred_lft forever ###查看路由表 [root@centos7-router ~]# route -n Kernel IP routing table ###缺省情況下,每塊網(wǎng)卡都有一個(gè)基于本網(wǎng)段的路由記錄 Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.1.1 0.0.0.0 UG 100 0 0 eno33554960 172.24.8.0 0.0.0.0 255.255.255.0 U 100 0 0 eno16777728 192.168.1.0 0.0.0.0 255.255.255.0 U 100 0 0 eno33554960 ###使用ip命令方式查看路由 [root@centos7-router ~]# ip route list default via 192.168.1.1 dev eno33554960 proto static metric 100 172.24.8.0/24 dev eno16777728 proto kernel scope link src 172.24.8.254 metric 100 192.168.1.0/24 dev eno33554960 proto kernel scope link src 192.168.1.175 metric 100 ###開啟ip forward [root@centos7-router ~]# cp /usr/lib/sysctl.d/50-default.conf /usr/lib/sysctl.d/50-default.conf.bk [root@centos7-router ~]# echo "net.ipv4.ip_forward = 1" >>/usr/lib/sysctl.d/50-default.conf [root@centos7-router ~]# grep ip_forward /usr/lib/sysctl.d/50-default.conf net.ipv4.ip_forward = 1 [root@centos7-router ~]# sysctl -p ###如不生效重啟一下OS
三、CentOS7-a主機(jī)下添加路由
###查看當(dāng)前主機(jī)ip 信息 [root@centos7-a ~]# ip addr list 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 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: eno16777728: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:57:26:9d brd ff:ff:ff:ff:ff:ff inet 172.24.8.131/24 brd 172.24.8.255 scope global dynamic eno16777728 valid_lft 1088sec preferred_lft 1088sec inet6 fe80::20c:29ff:fe57:269d/64 scope link valid_lft forever preferred_lft forever ###查看當(dāng)前主機(jī)路由信息 [root@centos7-a ~]# ip route list 172.24.8.0/24 dev eno16777728 proto kernel scope link src 172.24.8.131 metric 100 [root@centos7-a ~]# # Author : Leshami [root@centos7-a ~]# # Blog : http://blog.csdn.net/leshami [root@centos7-a ~]# ip route add default via 172.24.8.254 [root@centos7-a ~]# ip route list default via 172.24.8.254 dev eno16777728 172.24.8.0/24 dev eno16777728 proto kernel scope link src 172.24.8.131 metric 100 ###測試到虛擬路由器網(wǎng)關(guān)ip連通性 [root@centos7-a ~]# ping 172.24.8.254 -c 2 PING 172.24.8.254 (172.24.8.254) 56(84) bytes of data. 64 bytes from 172.24.8.254: icmp_seq=1 ttl=64 time=0.268 ms 64 bytes from 172.24.8.254: icmp_seq=2 ttl=64 time=0.302 ms --- 172.24.8.254 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1000ms rtt min/avg/max/mdev = 0.268/0.285/0.302/0.017 ms ###測試到虛擬路由器ip連通性 [root@centos7-a ~]# ping 192.168.1.175 -c 2 PING 192.168.1.175 (192.168.1.175) 56(84) bytes of data. 64 bytes from 192.168.1.175: icmp_seq=1 ttl=64 time=0.475 ms 64 bytes from 192.168.1.175: icmp_seq=2 ttl=64 time=0.443 ms --- 192.168.1.175 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1000ms rtt min/avg/max/mdev = 0.443/0.459/0.475/0.016 ms
四、Windows 10下配置路由
###當(dāng)前環(huán)境 C:\>systeminfo | findstr Build OS Version: 10.0.14393 N/A Build 14393 OS Build Type: Multiprocessor Free ###查看當(dāng)前的路由信息 C:\>route print -4 =========================================================================== Interface List 7...54 ab 3a xx b8 ea ......Intel(R) Ethernet Connection I219-V 21...ac 2b 6e 5f 3b 57 ......Microsoft Wi-Fi Direct Virtual Adapter ...... =========================================================================== IPv4 Route Table =========================================================================== Active Routes: Network Destination Netmask Gateway Interface Metric 0.0.0.0 0.0.0.0 192.168.1.1 192.168.1.242 35 127.0.0.0 255.0.0.0 On-link 127.0.0.1 331 127.0.0.1 255.255.255.255 On-link 127.0.0.1 331 127.255.255.255 255.255.255.255 On-link 127.0.0.1 331 172.24.10.0 255.255.255.0 On-link 172.24.10.1 291 172.24.10.1 255.255.255.255 On-link 172.24.10.1 291 172.24.10.255 255.255.255.255 On-link 172.24.10.1 291 192.168.1.0 255.255.255.0 On-link 192.168.1.242 291 192.168.1.242 255.255.255.255 On-link 192.168.1.242 291 192.168.1.255 255.255.255.255 On-link 192.168.1.242 291 192.168.16.0 255.255.255.0 On-link 192.168.16.1 291 192.168.16.1 255.255.255.255 On-link 192.168.16.1 291 192.168.16.255 255.255.255.255 On-link 192.168.16.1 291 224.0.0.0 240.0.0.0 On-link 127.0.0.1 331 224.0.0.0 240.0.0.0 On-link 192.168.1.242 291 224.0.0.0 240.0.0.0 On-link 192.168.16.1 291 224.0.0.0 240.0.0.0 On-link 172.24.10.1 291 255.255.255.255 255.255.255.255 On-link 127.0.0.1 331 255.255.255.255 255.255.255.255 On-link 192.168.1.242 291 255.255.255.255 255.255.255.255 On-link 192.168.16.1 291 255.255.255.255 255.255.255.255 On-link 172.24.10.1 291 =========================================================================== Persistent Routes: None ###添加持久化路由 C:\>route add 172.24.8.0 mask 255.255.255.0 192.168.1.175 -p OK! C:\>route print -4 IPv4 Route Table =========================================================================== Active Routes: Network Destination Netmask Gateway Interface Metric 0.0.0.0 0.0.0.0 192.168.1.1 192.168.1.242 35 127.0.0.0 255.0.0.0 On-link 127.0.0.1 331 127.0.0.1 255.255.255.255 On-link 127.0.0.1 331 127.255.255.255 255.255.255.255 On-link 127.0.0.1 331 172.24.8.0 255.255.255.0 192.168.1.175 192.168.1.242 36 172.24.10.0 255.255.255.0 On-link 172.24.10.1 291 172.24.10.1 255.255.255.255 On-link 172.24.10.1 291 172.24.10.255 255.255.255.255 On-link 172.24.10.1 291 192.168.1.0 255.255.255.0 On-link 192.168.1.242 291 192.168.1.242 255.255.255.255 On-link 192.168.1.242 291 192.168.1.255 255.255.255.255 On-link 192.168.1.242 291 192.168.16.0 255.255.255.0 On-link 192.168.16.1 291 192.168.16.1 255.255.255.255 On-link 192.168.16.1 291 192.168.16.255 255.255.255.255 On-link 192.168.16.1 291 224.0.0.0 240.0.0.0 On-link 127.0.0.1 331 224.0.0.0 240.0.0.0 On-link 192.168.1.242 291 224.0.0.0 240.0.0.0 On-link 192.168.16.1 291 224.0.0.0 240.0.0.0 On-link 172.24.10.1 291 255.255.255.255 255.255.255.255 On-link 127.0.0.1 331 255.255.255.255 255.255.255.255 On-link 192.168.1.242 291 255.255.255.255 255.255.255.255 On-link 192.168.16.1 291 255.255.255.255 255.255.255.255 On-link 172.24.10.1 291 =========================================================================== Persistent Routes: ###以下為添加的持久化路由記錄 Network Address Netmask Gateway Address Metric 172.24.8.0 255.255.255.0 192.168.1.175 1 ===========================================================================
五、驗(yàn)證兩邊連通性
###從Linux端ping windows 10 [root@centos7-a ~]# ping 192.168.1.242 -c 2 PING 192.168.1.242 (192.168.1.242) 56(84) bytes of data. 64 bytes from 192.168.1.242: icmp_seq=1 ttl=63 time=1.70 ms 64 bytes from 192.168.1.242: icmp_seq=2 ttl=63 time=1.10 ms --- 192.168.1.242 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1002ms rtt min/avg/max/mdev = 1.101/1.404/1.707/0.303 ms ###從windows 10端ping Linux C:\>ping 172.24.8.131 -t Pinging 172.24.8.131 with 32 bytes of data: Reply from 172.24.8.131: bytes=32 time=1ms TTL=63 Reply from 172.24.8.131: bytes=32 time=1ms TTL=63
注,如果測試有問題,請考慮是否被防火墻攔住。
關(guān)于“CentOS7下配置ip forward的方法”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。