谷动谷力

 找回密码
 立即注册
查看: 1152|回复: 0
打印 上一主题 下一主题
收起左侧

Debian Linux系统网络配置文件详解

[复制链接]
跳转到指定楼层
楼主
发表于 2022-3-4 00:17:36 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
Debian Linux系统网络配置文件详解


Debian管理方法和linux系统一样,说白了数据采集器(通讯管理机)其实就是一个阉割版的linux。之前的老版本都是基于centos和RH的,新版的这款基于Debian系统。网络配置方面有点不熟。所以整合网络上的资源和教程,整理了一下。

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

    auto lo                  #开机启动loopback网卡
    iface lo inet loopback   #lo接口设置为一个本地回环(loopback)地址
                             #不同的接口之间配置部分必须留有一个空格
    auto eth0                #开机启动eth0网卡
    iface eth0 inet static   #指出eth0接口具有一个静态的(static)IP配置
    address 192.168.0.129    #IP地址
    network 192.168.0.0      #网段
    netmask 255.255.255.0    #子掩码
    broadcast 192.168.0.255  #广播
    gateway 192.168.0.1      #网关

第二、高级配置
1、静态路由

    auto eth0
    iface eth0 inet static
    address 192.168.1.42
    network 192.168.1.0
    netmask 255.255.255.128
    broadcast 192.168.1.0
    up route add -net 192.168.1.128 netmask 255.255.255.128 gw 192.168.1.2    #启动时添加本条静态路由
    up route add default gw 192.168.1.200                                     #启动时添加默认路由
    down route del default gw 192.168.1.200                                   #禁用时删除默认路由
    down route del -net 192.168.1.128 netmask 255.255.255.128 gw 192.168.1.2  #禁用时删除本条静态路由

up就是当这个网卡启用时;

down网卡禁用时。

配置路由的写法,请自行查阅百度。
2、单网卡配置

/etc/network/interfaces

    auto eth0
    #iface eth0 inet dhcp(默认是DHCP被注释掉)
    iface eth0 inet static
    address 192.168.8.110
    netmask 255.255.255.0
    gateway 192.168.8.1

3、双网卡配置

/etc/network/interfaces

    auto eth0
    #iface eth0 inet dhcp(默认是DHCP被注释掉)
    iface eth0 inet static
    address 192.168.8.110
    netmask 255.255.255.0
    gateway 192.168.8.1
     
    auto eth1
    iface eth1 inet static
    address 192.168.0.110
    netmask 255.255.255.0

(注意eth1没有gateway )
4、单网卡配置多ip

有几个地址就配几个接口,冒号后面的数字是随意写的,只要不重复即可。

    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

5、pre-up和post-down命令时间

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

    auto eth0
    iface eth0 inet dhcp
    pre-up [ -f /etc/local-network-ok ]    #在激活eth0之前检查/etc/local-network-ok文件是否存在,如果不存在,则不会激活eth0。

6、通过MAC检测控制网卡、并在不同网卡上执行不同的命令

    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    #检测网卡的MAC地址是否为11:22:33:44:55:66,如果是则启动网卡。check-mac-address.sh放在/usr/share/doc/ifupdown/examples/目录中,使用的时候需要给它加上可执行权限。
        pre-up /usr/local/sbin/enable-masq    #假定在这执行的命令,你可以把它们替换成你想要的任何玩意
    iface eth1 inet dhcp
        pre-up /path/to/check-mac-address.sh eth1 AA:BB:CCD:EE:FF    #检测两块网卡的MAC地址是否为AA:BB:CCD:EE:FF,如果是则启动网卡。check-mac-address.sh放在/usr/share/doc/ifupdown/examples/目录中,使用的时候需要给它加上可执行权限。
        pre-up /usr/local/sbin/firewall       #假定在这执行的命令,你可以把它们替换成你想要的任何玩意

手册上说,这种方法主要是用来检测两块网卡的MAC地址交换(If their MAC addresses get swapped),其实就是两块网卡名互换了,这种情况在debian系统上再常见不过了,主要是因为内核识别网卡的顺序发生了变化。这个问题可以用下面 的这种方法来避免。
7、配置逻辑接口

这段配置首先配置了两个逻辑接口(这个名词的定义请参见debian参考手册 http://www.debian.org/doc/manuals/reference/ch-gateway.zh-cn.html)lan和internet,然后根据网卡的MAC地址,将逻辑接口映射(mapped)到物理接口上去。

    auto eth0 eth1
        mapping eth0 eth1
            script /path/to/get-mac-address.sh   #启动时执行脚本get-mac-address.sh,脚本的作用就是获得每块网卡的MAC地址。
            map 11:22:33:44:55:66 lan            #指定MAC为11:22:33:44:55:66的网卡为lan口
            map AA:BB:CCD:EE:FF internet       #指定MAC为AA:BB:CCD:EE:FF的网卡为internet口,和WAN口差不多
    iface lan inet static
        address 192.168.42.1
        netmask 255.255.255.0
        pre-up /usr/local/sbin/enable-masq $IFACE
    iface internet inet dhcp
        pre-up /usr/local/sbin/firewall $IFACE

8、外部程序来设置ip

这段配置只是启用一个网卡,但是ifupdown不对这个网卡设置任何ip,而是由外部程序来设置ip。

    57 auto eth0  
    58 iface eth0 inet manual  
    59 up ifconfig $IFACE 0.0.0.0 up  
    60 up /usr/local/bin/myconfigscript  
    61 down ifconfig $IFACE down

9、网卡的混杂模式

这段配置启用了网卡的混杂模式,用来当监听接口。

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

第三、添加DNS

编辑文件vim /etc/resolv.conf

nameserver 114.114.114.114 #替换为自己的DNS服务器地址

还可以在interfaces文件中添加dns服务器

    # 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
    auto eth0
    iface eth0 inet static
          address 10.112.18.106
          network 10.112.18.0
          netmask 255.255.255.0
          broadcast 10.112.18.255
          gateway 10.112.18.254
          dns-nameservers 10.112.18.1

最后一行dns-nameservers,可以添加多个,用空格分开。
第四、重启网卡服务

    debian8
    systemctl restart network
     
    debian9
    service networking restart

sudo systemctl restart networking.service


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|深圳市光明谷科技有限公司|光明谷商城|Sunshine Silicon Corpporation ( 粤ICP备14060730号|Sitemap

GMT+8, 2024-4-25 14:42 , Processed in 0.077752 second(s), 35 queries .

Powered by Discuz! X3.2 Licensed

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表