Networking

引導時未配置環回設備

  • February 9, 2016

我對 linux 上的網路介面的了解充其量是有限的,所以我希望有人能幫助我找出我無意中引起的問題的最佳解決方案。

啟動後,我的工作站上的環回設備似乎不存在。需要環回地址的東西(例如在 Eclipse 中執行 JUnit 測試套件)無法正常工作。但是,直到最近,它一直都很好,在我擺弄 iptables 將埠 80 轉發到埠 8080 之後不久,所以這可能是我做過的事情,但我不知道如何修復它,並且到目前為止,所有Google搜尋都沒有出現任何指針(我可能不知道正確的搜尋詞……)

首先,內容/etc/network/interfaces

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
allow-hotplug eth0
pre-up iptables-restore /etc/iptables.conf
post-down /etc/iptables-flush-all

現在,啟動我的機器後:

$ sudo ifconfig
eth0      Link encap:Ethernet  HWaddr 00:24:e8:25:90:5d  
         inet addr:10.33.1.106  Bcast:10.33.1.255  Mask:255.255.255.0
         inet6 addr: fe80::224:e8ff:fe25:905d/64 Scope:Link
         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
         RX packets:500239 errors:0 dropped:0 overruns:0 frame:0
         TX packets:334565 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:1000 
         RX bytes:617321176 (588.7 MiB)  TX bytes:36019254 (34.3 MiB)
         Interrupt:26 Base address:0x8000 

如果我執行ifconfig lo 127.0.0.1,那麼一切都很好,並且輸出ifconfig更改為:

$ sudo ifconfig
eth0      Link encap:Ethernet  HWaddr 00:24:e8:25:90:5d  
         inet addr:10.33.1.106  Bcast:10.33.1.255  Mask:255.255.255.0
         inet6 addr: fe80::224:e8ff:fe25:905d/64 Scope:Link
         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
         RX packets:606221 errors:0 dropped:0 overruns:0 frame:0
         TX packets:407173 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:1000 
         RX bytes:767892736 (732.3 MiB)  TX bytes:42051623 (40.1 MiB)
         Interrupt:26 Base address:0x8000 

lo        Link encap:Local Loopback  
         inet addr:127.0.0.1  Mask:255.0.0.0
         inet6 addr: ::1/128 Scope:Host
         UP LOOPBACK RUNNING  MTU:16436  Metric:1
         RX packets:65 errors:0 dropped:0 overruns:0 frame:0
         TX packets:65 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:0 
         RX bytes:35668 (34.8 KiB)  TX bytes:35668 (34.8 KiB)

那麼,誰能幫我弄清楚我做了什麼來破壞我的環回地址?

編輯 0:內容/etc/iptables.conf

chris@PC:~$ sudo cat /etc/iptables.conf 
# Generated by iptables-save v1.4.8 on Thu Nov 10 17:01:44 2011
*nat
:PREROUTING ACCEPT [2:440]
:POSTROUTING ACCEPT [2:102]
:OUTPUT ACCEPT [1:58]
-A PREROUTING -d 127.0.0.1/32 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 8080 
-A PREROUTING -i eth0 -p tcp -m tcp --dport 80 -j DNAT --to-destination :8080 
-A OUTPUT -d 127.0.0.1/32 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 8080 
COMMIT
# Completed on Thu Nov 10 17:01:44 2011
# Generated by iptables-save v1.4.8 on Thu Nov 10 17:01:44 2011
*mangle
:PREROUTING ACCEPT [4787:2521834]
:INPUT ACCEPT [4544:2507689]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [4190:913164]
:POSTROUTING ACCEPT [4117:908160]
COMMIT
# Completed on Thu Nov 10 17:01:44 2011
# Generated by iptables-save v1.4.8 on Thu Nov 10 17:01:44 2011
*filter
:INPUT ACCEPT [2699:1514101]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [2525:612477]
COMMIT
# Completed on Thu Nov 10 17:01:44 2011

克里斯@PC:~$sudo iptables -L -t filter

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

克里斯@PC:~$sudo iptables -L -t mangle

Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         

克里斯@PC:~$sudo iptables -L -t nat

Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination   

編輯 1:註釋掉我的 pre-up 和 post-down 行eth0似乎/etc/network/interfaces可以消除問題。

更改為以下內容後,我的環回地址在引導期間已正確配置/etc/network/interfaces

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
allow-hotplug eth0
#pre-up iptables-restore /etc/iptables.conf
#post-down /etc/iptables-flush-all

這兩行旨在在啟用和禁用介面時載入和解除安裝 iptables 規則。我讀到這是在 Debian 中配置防火牆的更好方法,而不是在啟動時載入所有規則。

的內容/etc/iptables-flush-all是:

克里斯@PC:~$sudo cat /etc/iptables-flush-all

#!/bin/sh

iptables --flush
iptables -t nat --flush
iptables -t mangle --flush
iptables -t filter --flush

它在沒有載入防火牆的情況下工作嗎?

順便說一句,“ifconfig”已被棄用;最好將“ip”用於包括診斷在內的所有內容。並不是說我認為這與您的問題有關。

就我而言,這是正確配置 /etc/hosts 的問題。

使用命令hostname獲取您的主機名並以這種方式設置 /etc/hosts:

127.0.0.1 本地主機
127.0.1.1 主機名

引用自:https://serverfault.com/questions/333269