IT备忘录

您现在的位置是:首页 > 运维 > Linux

Linux

debian网络配置文件 debian网络配置命令【详解】

2023-04-23 11:04:22Linux
很多人不清楚Linux Debian系统网卡配置文件在哪?如何在命令行下配置网卡?下面IT备忘录小编就给大家分享详细的配置方法。debian网络配置命令Debian系列的网卡配置和RH系列的不

很多人不清楚Linux Debian系统网卡配置文件在哪?如何在命令行下配置网卡?下面IT备忘录小编就给大家分享详细的配置方法。

debian网络配置文件 debian网络配置命令【详解】

debian网络配置命令

Debian系列的网卡配置和RH系列的不一样,这里说一下。 配置文件是/etc/network/interfaces,不管有多少网卡,只有这一个配置文件。

1、查看要配置的网卡名称

ip link
或者
ip a

2、nano编辑器打开网络接口的配置文件"/etc/network/interfaces"

sudo nano /etc/network/interfaces

默认的配置:

auto lo     #开机启动loopback网卡
iface lo inet loopback  #网卡名称是lo, 类型是loopback

3、添加网卡的配置,比如eth0的配置
 
auto eth0
iface eth0 inet static
address 192.168.0.129
network 192.168.0.0
netmask 255.255.255.0
broadcast 192.168.0.255
gateway 192.168.0.1
dns-nameservers 192.168.1.1 8.8.8.8

4、重启网络服务

systemctl restart networking
或者
/etc/init.d/networking restart

其它配置案例分享:

更复杂的应用,包括添加和删除路由。

up 就是当这个网卡启用时,反之是 down。

up route add -net 192.168.0.128 netmask 255.255.255.0 gw 192.168.0.2
up route add default gw 192.168.0.200
down route del default gw 192.168.0.200
down route del -net 192.168.0.128 netmask 255.255.255.0 gw 192.168.0.200

一个物理网卡上多个接口的配置方法。

这种方法在配置一块网卡多个网址的时候很常见,有几个地址就配几个接口,冒号后面的数字是随意写的,只要不重复即可。

auto eth0 eth0:1
iface eth0 inet static
address 192.168.0.100
network 192.168.0.0
netmask 255.255.255.0
broadcast 192.168.0.255
gateway 192.168.0.1
iface eth0:1 inet static
address 192.168.0.200
network 192.168.0.0
netmask 255.255.255.0
broadcast 192.168.0.255

pre-up和post-down命令时间。

这是一组命令(pre-up/up/post-up/pre-down/down/post-down),分别定义在对应的时刻需要执行的命令。

auto eth0
iface eth0 inet dhcp
pre-up [ -f /etc/network/local-network-ok ]
# 这条命令的作用是在激活eth0之前检查该文件是否存在,如果不存在则不会激活网卡。

更复杂的例子

auto eth0 eth1
iface eth0 inet static
address 192.168.42.1
netmask 255.255.255.0
pre-up /path/to/check-mac-address.sh eth0 11:22:33:44:55:66
pre-up /usr/local/sbin/enable-masq
pre-up /path/to/check-mac-address.sh eth1 AA:BB:CC:DD:EE:FF
pre-up /usr/local/sbin/enable-masq

这个用法是为了避免两块网卡的识别顺序变化导致的MAC地址交换,这个可以用下面的方法来解决。

auto eth0 eth1
mapping eth0 eth1
script /path/to/get-mac-address.sh
map 11:22:33:44:55:66 lan
map AA:BB:CC:DD:EE:FF internet
iface lan inet static
address 192.168.42.1
netmask 255.255.255.0
pre-up /usr/local/sbin/enable-masq lan
iface internet inet dhcp
pre-up /usr/local/sbin/firewall internet

上面的代码中所用到的脚本都在 /usr/share/doc/ifdown/examples/ 目录下。

用一个网卡,但不对这个网卡设置任何IP,而是交由外部程序来设置它

auto eth0
iface eth0 inet manual
up ifconfig $IFACE 0.0.0.0 up
up /usr/local/bin/myconfigscript
down ifconfig $IFACE down

配置混杂模式,用作监听接口。

auto eth0
iface eth0 inet manual
up ifocnfig $IFACE 0.0.0.0 up
up ip link set $IFACE promisc on
down ip link set $IFACE promisc off
down ifconfig $IFACE down

配置无线网络接口

auto wlan0
iface wlan0 inet dhcp

注意
auto:开机启动,设置了2个网卡都开机启动
ip地址获取方式,static是静态IP地址,dhcp是DHCP获取地址。
设置两个网卡同时启动的时候,需要将一个网卡的网管置空,否则需要手动修改路由,删除其中的一条默认路由。
------------------------------------------------------------------------------
auto与allow-hotplug的区别

/etc/network/interfaces文件中一般用auto或者allow-hotplug来定义接口的启动行为。

auto

语法:
auto <interface_name>
含义:
在系统启动的时候启动网络接口,无论网络接口有无连接(插入网线),如果该接口配置了DHCP,则无论有无网线,系统都会去执行DHCP,如果没有插入网线,则等该接口超时后才会继续。

allow-hotplug

语法:
allow-hotplug <interface_name>

含义:
只有当内核从该接口检测到热插拔事件后才启动该接口。如果系统开机时该接口没有插入网线,则系统不会启动该接口,系统启动后,如果插入网线,系统会自动启动该接口。也就是将网络接口设置为热插拔模式。

手动重新启动网络

一般修改了网络配置文件后,会用以下命令重新启动网络
# /etc/init.d/networking restart
但从squeeze开始,此命令会有如下提示:

Running /etc/init.d/networking restart is deprecated because it may not enable again some interfaces … (warning).
Reconfiguring network interfaces…done.

如果设置接口为auto,虽然会有如此提示,但接口仍然会正确的启动。
如果接口设置为allow-hotplug则没有这么走运了,网络接口不会正确启动。这种情况下必须使用如下命令启动网络接口:

#ifup <interface_name>
而命令
#ifconfig <interface_name> up
也无法正确启动接口

所以allow-hotplug设置的接口最好如下方式重新启动网络接口,当然auto方式的接口也没问题:

#ifdown <interface_name> && ifup <interface_name>

特别是在ssh登录远程主机的情况下,一定要像上面这样在一条命令里执行ifdown和ifup,否则,如果先执行ifdown,则再也没有机会执行ifup了。

看来大多数情形下,网络接口还是用auto方式比较省心。

相关内容标签

文章评论