當前位置:首頁 » 操作系統 » linuxip路由

linuxip路由

發布時間: 2022-08-31 12:58:37

linux更改ip後,為什麼路由會自動刪除

因為linux中路由默認都是動態路由,如果想他不變的話,可以手工配置靜態路由。但是要注意,靜態路由優先順序是高於動態路由的,如果靜態路由配置不正確的話,可能會導致不能上網

⑵ LINUX靜態路由配置

你已經在route2指定了#route
add
-net
192.168.4.0/24
gw
192.168.3.100,這樣下一跳指向route3
192.168.3.100
,數據包被轉發到route3,通過192.168.4.33
這個地址播發到PC2(一個網段)

⑶ Linux怎麼添加靜態路由

平時工作經常需要配置路由,接下來總結一下linux伺服器配置路由的方法。

1、首先看一下本機的網路環境網路信息及路由信息:ip addr;route -n。

⑷ 如何理解ip路由和操作linux的路由表

路由其實就是選擇一條數據包傳輸路徑的過程,也就是說主機怎麼向它的目的地順利發送數據的過程。當TCP/IP主機發送IP數據包時,便出現了路由,且當到達IP路由器時還會再次出現。路由器是從一個物理網向另一個物理網發送數據包的裝置,路由器通常被稱為網關,它承但著分發數據包的任務。對於發送的主機和路由器而言,必須決定向哪裡轉發數據包。在決定路由時,IP層查詢位於內存中的路由表,然後根據查詢規則,進行ip路由。是不是很抽象?用我的話說吧,也就是下面要討論的問題。
先說如何選路:
(1)當一個主機試圖與另一個主機通信時,IP首先決定目的主機是一個內網還是外網,怎麼確定?當然使用網路號。
(2)如果是是同一內網,那就就是直接發送了,這個最簡答不過了。
(3)如果目的主機是和發送主機不在同一個內網,也就是在外網了,^_^很啰嗦,IP將查詢路由表來為外網主機或外網選擇一個路由,所以一般情況下有可能為某個外網指定特定的路由,具體問題稍後分析。
(4)若未找到明確的路由,此時在路由表中還會有默認網關,也可稱為預設網關,IP用預設的網關地址將一個數據傳送給下一個指定的路由器,所以網關也可能是路由器,也可能只是內網向特定路由器傳輸數據的網關。
(4)在該路由器收到數據後,它再次為遠程主機或網路查詢路由,若還未找到路由,該數據包將發送到該路由器的預設網關地址。
每發現一條路由,數據包被轉送下一級路由器,稱為一次「跳步」,按照同樣的方式進行轉發,並最終發送至目的主機。若未發現任何一個路由可以接受此次ip包的地址,發送主機將收到一個出錯信息,也就是說其中任何一跳出現了不可到達,最後的結果也就是報錯了。
上面只是個人的一些理解,事實上當時我們做的實驗不少,但是看看tcp/ip詳解後,就可以一目瞭然了,現在以linux下的路由表為例說明一下:
$ sudo route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.100.0 * 255.255.255.0 U 0 0 0 eth1
192.168.10.0 * 255.255.254.0 U 0 0 0 eth0
default 192.168.10.1 0.0.0.0 UG 0 0 0 eth0
--------------------------------------------------------------------------------------
IP路由選擇是逐跳地(hop-by-hop)進行的。從這個路由表信息可以看出,IP並不知道到達任何目的的完整路徑(當然,除了那些與主機直接相連的目的)。所有的IP路由選擇只為數據報傳輸提供下一站路由器的IP地址。它假定下一站路由器比發送數據報的主機更接近目的,而且下一站路由器與該主機是直接相連的。
IP路由選擇主要完成以下這些功能:
1)搜索路由表,尋找能與目的IP地址完全匹配的表目(網路號和主機號都要匹配)。如果找到,則把報文發送給該表目指定的下一站路由器或直接連接的網路介面(取決於標志欄位的值)。
2) 搜索路由表,尋找能與目的網路號相匹配的表目。如果找到,則把報文發送給該表目指定的下一站路由器或直接連接的網路介面(取決於標志欄位的值)。目的網路上的所有主機都可以通過這個表目來處置。例如,一個乙太網上的所有主機都是通過這種表目進行尋徑的。這種搜索網路的匹配方法必須考慮可能的子網掩碼。關於這一點我們在下一節中進行討論。
3)搜索路由表,尋找標為「默認(default)」的表目。如果找到,則把報文發送給該表目指定的下一站路由器。如果上面這些步驟都沒有成功,那麼該數據報就不能被傳送。如果不能傳送的數據報來自本機,那麼一般會向生成數據報的應用程序返回一個「主機不可達」或 「網路不可達」的錯誤。
完整主機地址匹配在網路號匹配之前執行。只有當它們都失敗後才選擇默認路由。默認路由,以及下一站路由器發送的ICMP間接報文(如果我們為數據報選擇了錯誤的默認路由),是IP路由選擇機制中功能強大的特性。
以上的解釋都很清楚了,不用再詳細解釋怎麼走了,如果你實在還不能理解的話,去看tcp/ip詳解吧。順便解釋一下上面的output內容:
The output of the kernel routing table is organized in the following columns
Destination
The destination network or destination host.
Gateway
The gateway address or '*' if none set.
Genmask
The netmask for the destination net; '255.255.255.255' for a host destination and '0.0.0.0' for the default route.
Flags Possible flags include
U (route is up)
H (target is a host)
G (use gateway)
R (reinstate route for dynamic routing)
D (dynamically installed by daemon or redirect)
M (modified from routing daemon or redirect)
A (installed by addrconf)
C (cache entry)
! (reject route)
Metric The 'distance' to the target (usually counted in hops). It is not used by recent kernels, but may be needed by
routing daemons.
Ref Number of references to this route. (Not used in the Linux kernel.)
Use Count of lookups for the route. Depending on the use of -F and -C this will be either route cache misses (-F) or
hits (-C).
Iface Interface to which packets for this route will be sent.
MSS Default maximum segment size for TCP connections over this route.
Window Default window size for TCP connections over this route.
irtt Initial RTT (Round Trip Time). The kernel uses this to guess about the best TCP protocol parameters without wait-
ing on (possibly slow) answers.
HH (cached only)
The number of ARP entries and cached routes that refer to the hardware header cache for the cached route. This
will be -1 if a hardware address is not needed for the interface of the cached route (e.g. lo).
Arp (cached only)
Whether or not the hardware address for the cached route is up to date
同時在freebsd上使用netstat -nr列印路由表,也順便提示一下。
現在問題在如何操作路由表,因為ip的選路全靠它吃飯了,怎麼為一個特定的網路或者主機選擇一條特定的路由?如何刪除路由?這些問題對於網路維護和系統管理也尤為重要:
還是以上面的路由表為例,建議使用前先man route查看方法,或許各個系統略有差異,下面是以debian為例,由於是測試,所以使用sudo,而不是root許可權:
1、為某主機添加路由
$ sudo route add –host 192.168.10.58 dev eth1
//所有通向192.168.10.58的數據都是用eth1網卡
$ sudo route add –host 192.168.11.58 gw 192.168.10.1
//通向192.168.11.58的數據使用網關192.168.10.1
不經意間做了一個驗證實驗:由於eth1是沒有啟動的,所以添加這個路由後,192.168.10.58不可ping通
初始路由表
sudo route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.100.0 * 255.255.255.0 U 0 0 0 eth1
192.168.10.0 * 255.255.254.0 U 0 0 0 eth0
default 192.168.10.1 0.0.0.0 UG 0 0 0 eth0
----------------------------------------------------------------------------
可以ping通192.168.10.58
$ ping 192.168.10.58
PING 192.168.10.58 (192.168.10.58) 56(84) bytes of data.
64 bytes from 192.168.10.58: icmp_seq=1 ttl=64 time=0.188 ms
-----------------------------------------------------------------------------
添加192.168.10.58的路由
$ sudo route add -host 192.168.10.58 dev eth1
$ sudo route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.10.58 * 255.255.255.255 UH 0 0 0 eth1
192.168.100.0 * 255.255.255.0 U 0 0 0 eth1
192.168.10.0 * 255.255.254.0 U 0 0 0 eth0
default 192.168.10.1 0.0.0.0 UG 0 0 0 eth0
----------------------------------------------------------------------------
由於eth0無法使用了,所以發給eth1,就等於丟入黑洞之中
$ ping 192.168.10.58
PING 192.168.10.58 (192.168.10.58) 56(84) bytes of data.
--- 192.168.10.58 ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 1010ms
----------------------------------------------------------------------------
指定給網關處理
$ sudo route add -host 192.168.10.58 gw 192.168.10.1
$ sudo route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.10.58 192.168.10.1 255.255.255.255 UGH 0 0 0 eth0
192.168.10.58 * 255.255.255.255 UH 0 0 0 eth1
192.168.100.0 * 255.255.255.0 U 0 0 0 eth1
192.168.10.0 * 255.255.254.0 U 0 0 0 eth0
default 192.168.10.1 0.0.0.0 UG 0 0 0 eth0
----------------------------------------------------------------------------
要經過網關了,驗證了路由表的作用
$ ping 192.168.10.58
PING 192.168.10.58 (192.168.10.58) 56(84) bytes of data.
From 192.168.10.1: icmp_seq=1 Redirect Network(New nexthop: 192.168.10.58)
64 bytes from 192.168.10.58: icmp_seq=1 ttl=64 time=1.02 ms
----------------------------------------------------------------------------
廢話少說了,同理為某網路添加路由和刪除路由如下所示:經過測試有效:
2、為某網路的添加路由
$ sudo route add –net 220.181.8.0/24 dev eth0
$ sudo route add –net 220.181.8.0/24 gw 192.168.10.1
3、添加默認網關
$ sudo route add default gw 192.168.10.1
4、刪除路由,
$ sudo route del –host 192.168.168.110 dev eth0
可能你會遇到刪除時候語法錯誤,請參看路由表的Flags,路由上面的第一條,G表示設定了網關,H表示操作了主機,所以就用$ sudo route del -host 192.168.10.58 gw 192.168.10.1刪除,更詳細的請man。
使用route 命令添加的路由,機器重啟或者網卡重啟後路由就失效了,和iptables一樣,需要永久添加的話,也是使用開機執行,所以可以用以下方法添加永久路由:
1.在/etc/rc.local里添加執行命令,進行開機執行,因為是root許可權,所以不用sudo了:
route add –net 220.181.8.0/24 dev eth0
route add –net 220.181.9.0/24 gw 192.168.10.1
2.在/etc/sysconfig/network里添加到末尾
方法:GATEWAY=gw-ip 或者 GATEWAY=gw-dev
3./etc/sysconfig/static-router :
any net x.x.x.x/24 gw y.y.y.y

⑸ linux如何查看無線路由IP

運行 route 命令,你會看到一組路由設置的輸出
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.0.0 * 255.255.255.0 U 0 0 0 eth0

169.254.0.0 * 255.255.0.0 U 0 0 0 eth0

default 192.168.0.1 0.0.0.0 UG 0 0 0 eth0

其中,default那條路由對應的IP地址 -- 本例中是 192.168.0.1就是無線路由器地址。

⑹ linux如何配置路由

linux下添加路由的方法:
一:使用 route 命令添加
使用route 命令添加的路由,機器重啟或者網卡重啟後路由就失效了,方法:
//添加到主機的路由
# route add –host 192.168.168.110 dev eth0
# route add –host 192.168.168.119 gw 192.168.168.1
//添加到網路的路由
# route add –net IP netmask MASK eth0
# route add –net IP netmask MASK gw IP
# route add –net IP/24 eth1
//添加默認網關
# route add default gw IP
//刪除路由
# route del –host 192.168.168.110 dev eth0

二:在linux下設置永久路由的方法:
1.在/etc/rc.local里添加
方法:
route add -net 192.168.3.0/24 dev eth0
route add -net 192.168.2.0/24 gw 192.168.3.254

2.在/etc/sysconfig/network里添加到末尾
方法:GATEWAY=gw-ip 或者 GATEWAY=gw-dev

3./etc/sysconfig/static-router :
any net x.x.x.x/24 gw y.y.y.y

⑺ linux 如何查看無線路由ip

運行 route 命令,你會看到一組路由設置的輸出
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.0.0 * 255.255.255.0 U 0 0 0 eth0

169.254.0.0 * 255.255.0.0 U 0 0 0 eth0

default 192.168.0.1 0.0.0.0 UG 0 0 0 eth0

其中,default那條路由對應的IP地址 -- 本例中是 192.168.0.1就是無線路由器地址。

熱點內容
自製演算法表 發布:2025-05-14 11:05:18 瀏覽:356
android獲取手機聯系人 發布:2025-05-14 10:59:30 瀏覽:141
電腦怎麼選擇配置 發布:2025-05-14 10:46:12 瀏覽:326
電腦怎麼不顯示手機連接伺服器失敗 發布:2025-05-14 10:42:28 瀏覽:10
安卓如何下載lv手游 發布:2025-05-14 10:35:45 瀏覽:384
pythondict添加key 發布:2025-05-14 10:33:59 瀏覽:382
柱子箍筋加密區長度 發布:2025-05-14 10:18:29 瀏覽:352
雲伺服器和內網穿透哪個好 發布:2025-05-14 10:16:41 瀏覽:627
安徽新能源網路配置是什麼 發布:2025-05-14 10:06:24 瀏覽:631
pinode搭建伺服器 發布:2025-05-14 10:04:23 瀏覽:4