【概述】html
Ubuntu Server 18.04 中用 netplan 取代了 ifupdown,配置文件在 /etc/netplan/ 目录下,文件格式为 yaml,使配置文件生效的命令为 sudo netplan apply。linux
~ $ cat /etc/network/interfaces # ifupdown has been replaced by netplan(5) on this system. See # /etc/netplan for current configuration. # To re-enable ifupdown on this system, you can run: # sudo apt install ifupdown auto dsl-provider iface dsl-provider inet ppp pre-up /bin/ip link set enp3s0 up # line maintained by pppoeconf provider dsl-provider auto enp3s0 iface enp3s0 inet manual
【检查网卡是否已插入网线】ubuntu
# 1 表示有;0 表示没有 $ cat /sys/class/net/enp2s0/carrier 1
【查看网卡状态】bash
$ sudo mii-tool enp2s0 enp2s0: negotiated 1000baseT-HD flow-control, link ok
【重启指定网卡】服务器
# 关闭 $ sudo ifconfig enp3s0 down # 开启 $ sudo ifconfig enp3s0 up
【典型配置】网络
# /etc/netplan/50-cloud-init.yaml network: ethernets: # 静态IP enp2s0: dhcp4: false addresses: - 192.168.0.145/24 gateway4: 192.168.0.1 nameservers: addresses: - 223.5.5.5 search: [] optional: true # 动态IP enp4s0: dhcp4: true # 若没有下面这一句 # 向服务器发送的“mac”地址会是相似 # “5de26c1500020000ab1102df86200698a807” # 的奇怪字符串 # 实际上这是 DUID dhcp-identifier: mac optional: true version: 2
多网卡下静态路由无效(不生效)的一种解决方案:How can I configure default route metric with dhcp and netplan?app
# 示例 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
【DNS】ide
若是上面配置的 dns 并无什么卵用(多是受 iptables 防火墙规则的影响),能够清空 iptables 命令排查,也能够直接修改 /etc/resolv.conf 文件。this
nameserver 223.5.5.5
若是修改后又被改回 127.0.0.53,能够停用 systemd-resolved 服务。spa
sudo systemctl disable systemd-resolved
sudo 很慢?在 /etc/hosts 添加 hostname 到 127.0.0.1 的映射。
清空 iptables 命令
sudo iptables -F sudo iptables -X sudo iptables -t nat -F sudo iptables -t nat -X sudo iptables -t mangle -F sudo iptables -t mangle -X sudo iptables -P INPUT ACCEPT sudo iptables -P FORWARD ACCEPT sudo iptables -P OUTPUT ACCEPT
【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
【路由】
摘自:Linux下路由配置梳理
# 查看路由 route -n # 添加到主机的路由 route add -host 192.168.1.2 dev eth0:0 route add -host 10.20.30.148 gw 10.20.30.40 # 添加到网络的路由 route add -net 10.20.30.40 netmask 255.255.255.248 eth0 route add -net 10.20.30.48 netmask 255.255.255.248 gw 10.20.30.41 route add -net 192.168.1.0/24 eth1 route add -net 192.168.0.0/16 gw 192.168.30.1 # 添加默认路由 route add default gw 192.168.1.1 # 删除路由 route del -host 192.168.1.2 dev eth0:0 route del -host 10.20.30.148 gw 10.20.30.40 route del -net 10.20.30.40 netmask 255.255.255.248 eth0 route del -net 10.20.30.48 netmask 255.255.255.248 gw 10.20.30.41 route del -net 192.168.1.0/24 eth1 route del default gw 192.168.1.1 # route del default 删除全部的默认路由 # 添加一条默认路由 route add default gw 10.0.0.1 # 默认只在内存中生效 # 开机自启动能够追加到/etc/rc.local文件里 echo "route add default gw 10.0.0.1" >>/etc/rc.local # 添加一条静态路由 route add -net 192.168.2.0/24 gw 192.168.2.254 # 要永久生效的话要这样作: echo "any net 192.168.2.0/24 gw 192.168.2.254" >>/etc/sysconfig/static-routes # 添加到一台主机的静态路由 route add -host 192.168.2.2 gw 192.168.2.254 # 要永久生效的话要这样作: echo "any host 192.168.2.2 gw 192.168.2.254 " >>/etc/sysconfig/static-routes # 注:Linux 默认没有这个文件 ,得手动建立一个
添加路由报错 SIOCADDRT: Network is unreachable,是由于出口地址对主机来讲广播不可达,具体来讲有两种可能状况:一、出口地址与主机不在同一个网段;二、出口地址与主机在同一个网段,但广播路由被无心删除了。广播路由示例(红框内即为广播路由):
【相关阅读】
*** walker ***