2

前言

检查网卡是否已插入网线

  • 直接查看文件

    # 1 表示有;0 表示没有
    $ cat /sys/class/net/enp2s0/carrier
    1
  • ip addrip link 命令
    image.png

查看网卡状态

  • mii-tool

    $ sudo mii-tool enp3s0
    enp3s0: no link
    walker@vip-055:~$ sudo mii-tool enp4s0
    enp4s0: negotiated 1000baseT-FD flow-control, link ok
  • ip link

    walker@vip-055:~$ ip link
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
      link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    2: enp3s0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN mode DEFAULT group default qlen 1000
      link/ether 00:e0:53:35:00:9f brd ff:ff:ff:ff:ff:ff
    3: enp4s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
      link/ether b4:2e:99:2b:c9:e7 brd ff:ff:ff:ff:ff:ff

查看物理网卡 ip

  • 在安装 docker 后,ip addr 显示的地址太多,想要只查看物理地址,可以先用命令过滤出接口再查看

    $ ll /sys/class/net/ | grep -v virtual
    total 0
    drwxr-xr-x  2 root root 0 May  5 14:42 ./
    drwxr-xr-x 71 root root 0 May  5 14:42 ../
    lrwxrwxrwx  1 root root 0 May  5 14:42 enp4s0 -> ../../devices/pci0000:00/0000:00:1c.5/0000:04:00.0/net/enp4s0/
    
    $ ip addr show dev enp4s0

重启指定网卡

# 关闭
$ sudo ip link set enp3s0 down
# 开启
$ sudo ip link set enp3s0 up

典型配置

  • /etc/netplan/******.yaml(yaml 不能 TAB 缩进)

    network:
      ethernets:
          # 静态IP
          enp2s0:
              dhcp4: false
              addresses:
                - 192.168.0.145/24
              gateway4: 192.168.0.1
              nameservers:
                  addresses:
                    - 223.5.5.5
                  search: []
              # 系统启动不用等待网络接口完全激活,常用与手动绑定ip的情况(dhcp4: false)
              optional: true 
          # 动态IP
          enp4s0:
              dhcp4: true            
              # 若没有下面这一句
              # 向服务器发送的“mac”地址会是类似
              # “5de26c1500020000ab1102df86200698a807”
              # 的奇怪字符串
              # 实际上这是 DUID
              dhcp-identifier: mac
              optional: true
      version: 2
  • 应用配置

    sudo netplan apply

多网卡下静态路由无效(不生效)的一种解决方案

  • How can I configure default route metric with dhcp and netplan?

    # 示例
    network:
    ethernets:
        enp2s0:
            dhcp4: false
            addresses:
              - 192.168.30.36/24
            # gateway4: 192.168.30.1
            routes:
              - to: 192.168.0.0/16
                via: 192.168.30.1
                metric: 50
            nameservers:
                addresses:
                  - 223.5.5.5
                search: []
            optional: true
        enp3s0:
            dhcp4: true
            dhcp-identifier: mac
            optional: true
    version: 2

DHCP

  • 查看 DHCP 信息

    cat /var/lib/dhcp/dhclient.leases 
  • 重新获取 ip

    dhclient

DNS

  • 查看 DNS 服务器地址

    resolvectl dns    # 推荐
    # OR
    systemd-resolve --status | grep Current
    # OR
    cat /run/systemd/resolve/resolv.conf
  • 查询域名 ip

    $ resolvectl query www.qq.com
    www.qq.com: 121.14.77.201                      -- link: enp5s0
              121.14.77.221                      -- link: enp5s0
              (ins-r23tsuuf.ias.tencent-cloud.net)
    
    -- Information acquired via protocol DNS in 38.5ms.
    -- Data is authenticated: no
  • 添加 DNS 服务器地址

    sudo resolvectl dns enp5s0 223.5.5.5
    # OR
    sudo systemd-resolve --interface enp5s0 --set-dns 223.5.5.5
    # OR 
    1、编辑 /etc/systemd/resolved.conf 文件
    2、systemctl restart systemd-resolved.service

PPPoE 拨号上网

# 安装 pppoeconf
sudo apt install pppoeconf
# 配置
sudo pppoeconf
# 手动连接
sudo pon dsl-provider
# 手动断开
sudo poff dsl-provider
# 查看状态
sudo plog
# 查看接口信息
sudo ip addr show ppp0

查看网络接口信息

sudo ifconfig -a
# or
sudo ip addr show

路由

  • ip route 命令
  • 添加路由报错 SIOCADDRT: Network is unreachable,是因为出口地址对主机来说广播不可达,具体来说有两种可能情况:1、出口地址与主机不在同一个网段;2、出口地址与主机在同一个网段,但广播路由被无意删除了。广播路由示例(红框内即为广播路由):
    route -n
    image.png
本文出自 qbit snap

qbit
268 声望279 粉丝