Ubuntu网络配置

Posted by wsxq2 on 2018-12-10
TAGS:  网络UbuntuLinux

Solving “RTNETLINK answers: File exists” when running ifup

查看路由(网关)设置?

1
2
ip route
ip route help
1
route

查看 DNS 设置?

  1. resolv.conf: Linux 中 DNS 配置的主要文件。只要它配置了正确的 namesever IP 即可正确使用 DNS。相关的 manual 为man resolv.conf
    1
    2
    3
    4
    5
    
       $ cat /etc/resolv.conf
       # 显示可能如下
       # Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
       #     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
       nameserver 202.117.112.3
    
  2. nslookup: Linux 中用于检查 DNS 是否正确配置的命令,基本使用如下:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    $ nslookup www.baidu.com
    Server:         202.117.112.3
    Address:        202.117.112.3#53
       
    Non-authoritative answer:
    www.baidu.com   canonical name = www.a.shifen.com.
    Name:   www.a.shifen.com
    Address: 180.149.131.98
    Name:   www.a.shifen.com
    Address: 180.149.132.151
    

设置 DNS?

  1. /etc/network/interface中设置如下:
    1
    2
    3
    4
    5
    
    auto enp0s9
    iface enp0s9 inet static
        address 192.168.2.16/24
        gateway 192.168.2.1
        dns-nameservers 202.117.112.3 # 重点是此行
    

    参见man 8 resolvconf。此外,Linux 还有可能会通过网关自动设置 DNS

  2. 修改/etc/resolvconf/resolv.conf.d/base(这个文件默认是空的)在里面插入:
    1
    2
    
    nameserver 202.117.112.3
    nameserver 8.8.8.8
    

    修改好保存,然后执行

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    
    $ resolvconf -u
    $ cat /etc/resolv.conf
    # Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
    #     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
    nameserver 202.117.112.3
    nameserver 8.8.8.8
    $ ping baidu.com
    PING baidu.com (220.181.57.216) 56(84) bytes of data.
    64 bytes from 220.181.57.216: icmp_seq=1 ttl=52 time=25.7 ms
    64 bytes from 220.181.57.216: icmp_seq=2 ttl=52 time=25.8 ms
    64 bytes from 220.181.57.216: icmp_seq=3 ttl=52 time=26.1 ms
    ^C
    --- baidu.com ping statistics ---
    3 packets transmitted, 3 received, 0% packet loss, time 2004ms
    rtt min/avg/max/mdev = 25.741/25.918/26.153/0.253 ms
    

    参考自:Ubuntu下修改DNS重启也能用的方法

查看 物理(MAC)地址?

  1. ip: Linux 中用于取代传统的 ifconfig命令,是iproute2软件包的一部分。iproute2软件包中另一个常用的命令是ss,用于取代传统的netstat。更多相关信息,请参考试试Linux下的ip命令,ifconfig已经过时了
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    
    $ dpkg -S `which ip` # 查看 ip 命令属于哪个软件包
    iproute2: /sbin/ip
    $ dpkg -L iproute2|grep bin/ # 查看 iproute2 软件包中所有的命令
    /usr/bin/routef
    /usr/bin/routel
    /usr/bin/lnstat
    /usr/bin/nstat
    /usr/sbin/arpd
    /usr/sbin/genl
    /bin/ss
    /bin/ip
    /sbin/rtacct
    /sbin/tc
    /sbin/rtmon
    /sbin/bridge
    /sbin/tipc
    /usr/bin/rtstat
    /usr/bin/ctstat
    /sbin/ip
    $ ip help
    Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }
           ip [ -force ] -batch filename
    where  OBJECT := { link | address | addrlabel | route | rule | neighbor | ntable |
                       tunnel | tuntap | maddress | mroute | mrule | monitor | xfrm |
                       netns | l2tp | fou | tcp_metrics | token | netconf }
           OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |
                        -h[uman-readable] | -iec |
                        -f[amily] { inet | inet6 | ipx | dnet | mpls | bridge | link } |
                        -4 | -6 | -I | -D | -B | -0 |
                        -l[oops] { maximum-addr-flush-attempts } | -br[ief] |
                        -o[neline] | -t[imestamp] | -ts[hort] | -b[atch] [filename] |
                        -rc[vbuf] [size] | -n[etns] name | -a[ll] | -c[olor]}
    $ ip ad help
    Usage: ip address {add|change|replace} IFADDR dev IFNAME [ LIFETIME ]
                                                          [ CONFFLAG-LIST ]
           ip address del IFADDR dev IFNAME [mngtmpaddr]
           ip address {show|save|flush} [ dev IFNAME ] [ scope SCOPE-ID ]
                                [ to PREFIX ] [ FLAG-LIST ] [ label LABEL ] [up]
           ip address {showdump|restore}
    IFADDR := PREFIX | ADDR peer PREFIX
              [ broadcast ADDR ] [ anycast ADDR ]
              [ label IFNAME ] [ scope SCOPE-ID ]
    SCOPE-ID := [ host | link | global | NUMBER ]
    FLAG-LIST := [ FLAG-LIST ] FLAG
    FLAG  := [ permanent | dynamic | secondary | primary |
               [-]tentative | [-]deprecated | [-]dadfailed | temporary |
               CONFFLAG-LIST ]
    CONFFLAG-LIST := [ CONFFLAG-LIST ] CONFFLAG
    CONFFLAG  := [ home | nodad | mngtmpaddr | noprefixroute | autojoin ]
    LIFETIME := [ valid_lft LFT ] [ preferred_lft LFT ]
    LFT := forever | SECONDS
    

    简单用法:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    
    $ ip ad # 查看各接口的启用与否、IP地址、MAC地址信息
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
           valid_lft forever preferred_lft forever
        inet6 ::1/128 scope host
           valid_lft forever preferred_lft forever
    2: enp0s3: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
        link/ether 08:00:27:b2:80:2e brd ff:ff:ff:ff:ff:ff
    3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
        link/ether 08:00:27:c6:4e:bf brd ff:ff:ff:ff:ff:ff
        inet 192.168.56.16/24 brd 192.168.56.255 scope global enp0s8
           valid_lft forever preferred_lft forever
        inet6 fe80::a00:27ff:fec6:4ebf/64 scope link
           valid_lft forever preferred_lft forever
    4: enp0s9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
        link/ether 08:00:27:4b:67:b7 brd ff:ff:ff:ff:ff:ff
        inet 192.168.2.16/24 brd 192.168.2.255 scope global enp0s9
           valid_lft forever preferred_lft forever
        inet6 fe80::a00:27ff:fe4b:67b7/64 scope link
           valid_lft forever preferred_lft forever
    $ sudo ip link set enp0s3 up # 启用接口 enp0s3
    

    关于该命令的具体用法,请参考man ip

    关于ss命令,可通过ss -h查看用法,常用的用法为sudo ss -laut4p

查看 IP 地址?

  1. ip: 参见前述

开机启动时依然禁用某个网络接口?

  1. https://serverfault.com/questions/585852/disable-a-network-interface-on-booting-ubuntu

    You can add the interfaces that should not come up on boot on a new file /etc/network/interfaces.noboot, then bring them up when needed with:

    sudo ifup -i /etc/network/interfaces.noboot -v eth1

  2. /etc/network/interfaces文件中注释掉auto <interface_name>及相应的内容,如下所示:

    1
    2
    3
    
    # The primary network interface
    # auto enp0s3
    # iface enp0s3 inet dhcp
    

2 用作路由器,作网关?

Ubuntu下双网卡简单静态内网路由器

链接