跳轉到內容

Linux 網路/配置網路介面

來自華夏公益教科書

當您擁有所有需要的程式,以及您的地址和網路資訊,您可以配置您的網路介面。 當我們談論配置網路介面時,我們指的是將適當的地址分配給網路裝置,併為網路裝置的其他可配置引數設定適當的值。 最常用於此目的的程式是ip命令。

通常,您會使用類似於以下的命令

ip addr add 192.168.0.1/24 dev eth0
ip link set eth0 up

在這種情況下,我正在配置一個名為“eth0”的乙太網介面,其 IP 地址為 192.168.0.1,字首長度為 24 以指定網路大小。第二個命令告訴作業系統應將介面設定為活動狀態,但這通常可以省略,因為它是預設設定。要關閉介面,您只需呼叫`ip link set eth0 down`。

iproute2 在配置介面時會假設某些預設值。例如,如果您沒有指定字首長度,它將使用 /32。

ip link命令還有許多其他選項。其中最重要的有

  up
     this option activates an interface (and is the default).
  down
     this option deactivates an interface.
  arp on/off
     this option enables or disables use of the address resolution
     protocol on this interface
  allmulticast on/off
     this option enables or disables the reception of all hardware
     multicast packets. Hardware multicast enables groups of hosts to
     receive packets addressed to special destinations. This may be
     of importance if you are using applications like desktop
  mtu N
     this parameter allows you to set the MTU of this device.
  addr <addr>
     this parameter allows you to set the hardware address of certain
     types of network devices. This is not often useful for ethernet,
     but is useful for other network types such as AX.25.

對於ip addr,可以指定

  (ip addr add ...) brd addr
     this parameter allows you to enable and set the accepting of
     datagrams destined to the IPv4 broadcast address, or to disable
     reception of these datagrams. One can use `brd +` to automatically
     fill in the broadcast address.

您可以在任何網路介面上使用 ip 命令。某些使用者程式(如 pppd 和 dip)會在建立網路裝置時自動配置它們,因此無需手動使用 ifconfig。

華夏公益教科書