linux系統的路由功能

1、概要

   大家應該都明白,不同網段的報文包傳播,是需要路由的轉發轉發功能的,那么,一個linux操作系統能不能充當路由器呢?下面不妨來驗證一下。



2、網絡拓撲

wKioL1fL2HvQjQ9HAABcwhriRjg125.png


3、實現過程

   <1>倆個centos6分別充當Router1和Router2。并添加足夠的網卡。

   <2>關閉NetworkManager

   <3>配置centos6-1

[root@centos6 network-scripts]# vim ifcfg-eth0
DEVICE=eth0
IPADDR=192.168.0.1
PREFIX=24
[root@centos6 network-scripts]# vim ifcfg-eth1
DEVICE=eth1
IPADDR=10.0.0.1
PREFIX=8
[root@centos6 network-scripts]# service network restart

    <4>配置centos6-2

[root@centos6 network-scripts]# vim ifcfg-eth0
DEVICE=eth0
IPADDR=172.16.0.1
PREFIX=16
[root@centos6 network-scripts]# vim ifcfg-eth1
DEVICE=eth1
IPADDR=10.0.0.2
PREFIX=8
[root@centos6 network-scripts]# service network restart

    <5>添加路由

# centos6-1添加路由
[root@centos6 network-scripts]# route add -net 172.16.0.0/16 gw 10.0.0.2 dev eth1
[root@centos6 network-scripts]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.0.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0 
10.1.0.0        0.0.0.0         255.255.0.0     U     0      0        0 eth2
172.16.0.0      10.0.0.2        255.255.0.0     UG    0      0        0 eth1 #添加的
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     1004   0        0 eth2
10.0.0.0        0.0.0.0         255.0.0.0       U     0      0        0 eth1
# centos6-2添加路由
[root@centos6 network-scripts]# route add -net 192.168.0.0/24 gw 10.0.0.1 dev eth1
[root@centos6 network-scripts]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.0.0     10.0.0.1        255.255.255.0   UG    0      0        0 eth1 #添加的
10.1.0.0        0.0.0.0         255.255.0.0     U     0      0        0 eth2
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1004   0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     1005   0        0 eth2
172.16.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth0
10.0.0.0        0.0.0.0         255.0.0.0       U     0      0        0 eth1

補充:刪除路由使用route del -net *.*.*.*/#

    <7>啟用路由功能

# centos6-1啟用
[root@centos6 network-scripts]# echo 1 > /proc/sys/net/ipv4/ip_forward
# centos6-2啟用
[root@centos6 network-scripts]# echo 1 > /proc/sys/net/ipv4/ip_forward

    <8>清空防火墻

# centos6-1清空
[root@centos6 network-scripts]# iptables -F
# centos6-1清空
[root@centos6 network-scripts]# iptables -F

    <8>測試

準備
#另一臺虛擬機
[root@centos7~]#ifconfig 
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.222  netmask 255.255.255.0  broadcast 192.168.0.255
        inet6 fe80::20c:29ff:fe44:5b8d  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:44:5b:8d  txqueuelen 1000  (Ethernet)
        RX packets 72946  bytes 6742470 (6.4 MiB)
        RX errors 0  dropped 10  overruns 0  frame 0
        TX packets 3002  bytes 488833 (477.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
# windows配置172.16.0.100/16
[root@centos7~]#ping 10.0.0.2
PING 10.0.0.2 (10.0.0.2) 56(84) bytes of data.
64 bytes from 10.0.0.2: icmp_seq=1 ttl=63 time=1.11 ms
64 bytes from 10.0.0.2: icmp_seq=2 ttl=63 time=0.357 ms    # ttl至已經改變,減少一跳
[root@centos7~]#ping 172.16.0.100
PING 172.16.0.100 (172.16.0.100) 56(84) bytes of data.
64 bytes from 172.16.0.100: icmp_seq=1 ttl=62 time=1.12 ms
64 bytes from 172.16.0.100: icmp_seq=2 ttl=62 time=0.783 ms
64 bytes from 172.16.0.100: icmp_seq=3 ttl=62 time=0.785 ms    # ttl減少2,成功

 完成。      

補充:

手動指定IP:ifconfig eth0 *.*.*.*/#

            ifconfig eth0 up/down

手動添加路由 route add defaults gw IP 

             route del -net *.*.*.*/#

原創文章,作者:mfwing,如若轉載,請注明出處:http://www.www58058.com/43612

(0)
mfwingmfwing
上一篇 2016-09-06 08:51
下一篇 2016-09-06 08:51

相關推薦

  • sed

    簡介     sed 是一種流編輯器,它一次處理一行內容。處理時,把當前處理的行存儲在臨時緩沖區中,稱為“模式空間”(pattern space),接著用sed命令處理緩沖區中的內容,處理完成后,把緩沖區的內容送往屏幕。接著處理下一行,這樣不斷重復,直到文件末尾。文件內容并沒有 改變,除非你使用重定向存儲輸出。Sed主…

    Linux干貨 2016-03-22
  • 磁盤管理、文件系統管理

    磁盤管理、文件系統管理 磁盤管理 磁盤設備 機械硬盤(非固態) I/O Ports —– I/O 設備地址 塊設備:Block 存取單位“塊”;(磁盤) 字符設備:char 存取單位“字符”;(鍵盤) 設備文件:關聯一個設備驅動程序,進而能夠跟與之對應的硬件設備進行通信; – 設備號碼: 主設備號 major number…

    Linux干貨 2016-07-22
  • 如何新增加磁盤分區掛載

    新增磁盤和分區掛載我們先要了解fdisk的參數以及菜單的操作說明,下面是fdisk的參數說明: ?fdisk參數說明: m :顯示菜單和幫助信息 a :活動分區標記/引導分區 d :刪除分區 l :顯示分區類型 n :新建分區 p :顯示分區信息 q :退出不保存 t :設置分區號 v :進行分區檢查 w :保存修改 x :擴展應用,高級功能 1 ?查看磁盤…

    Linux干貨 2018-03-26
  • htop使用詳解

        在管理進程時通常要借助一些工具,比較常用的就是ps和top了;不過CentOS還為我們提供了一個更加強大的工具htop,下面就來了解一下此工具的使用方法。 一、安裝htop         htop工具在epel源中提供,請自行配置epel源,也可以直…

    Linux干貨 2015-05-18
  • Linux高級文件系統管理

                                          &n…

    系統運維 2016-09-06
  • linux中擴展swap分區和移動設備使用掛載以及常用的工具free、df 、du、dd命令

    swap分區 swap分區作用:內存不足時充當系統內存,所以swap盡量放在告訴硬盤上也就是盡量使用磁盤的第一個分區 動手創建swap分區       1、創建分區,將分區類型轉換成swap分區格式           &nbsp…

    Linux干貨 2016-08-29

評論列表(1條)

  • 馬哥教育
    馬哥教育 2016-09-10 15:03

    文章整體思路清晰,從一個問題引入,到通過自己的實驗來論證自己的想法,最后希望作者也能將實驗結果單獨總結出來。

欧美性久久久久