1 Ubuntu网络配置

1.1 主机名

修改主机名

root@jiangmin:~# hostnamectl set-hostname jiangfeng
root@jiangmin:~# cat /etc/hostname 
jiangfeng
root@jiangmin:~# hostname
jiangfeng
root@jiangmin:~# echo $HOSTNAME
jiangmin

1.2 网卡名称

默认ubuntu的网卡名称和 CentOS 7 类似,如:ens33,ens38等 修改网卡名称为传统命名方式:

#修改配置文件为下面形式
root@jiangmin:~# vi /etc/default/grub
GRUB_CMDLINE_LINUX="net.ifnames=0"

#或者sed修改
root@jiangmin:~# sed -i.bak '/^GRUB_CMDLINE_LINUX=/s#"$#net.ifnames=0"#' /etc/default/grub


#生效新的grub.cfg文件
root@jiangmin:~# grub-mkconfig -o /boot/grub/grub.cfg 
root@jiangmin:~# grep net.ifnames /boot/grub/grub.cfg
        linux	/vmlinuz-4.15.0-151-generic root=UUID=fc608d33-14db-44f5-9934-deacf25fee55 ro net.ifnames=0 maybe-ubiquity
	        linux	/vmlinuz-4.15.0-151-generic root=UUID=fc608d33-14db-44f5-9934-deacf25fee55 ro net.ifnames=0 maybe-ubiquity
	        linux	/vmlinuz-4.15.0-151-generic root=UUID=fc608d33-14db-44f5-9934-deacf25fee55 ro recovery nomodeset dis_ucode_ldr net.ifnames=0
	        linux	/vmlinuz-4.15.0-147-generic root=UUID=fc608d33-14db-44f5-9934-deacf25fee55 ro net.ifnames=0 maybe-ubiquity
	        linux	/vmlinuz-4.15.0-147-generic root=UUID=fc608d33-14db-44f5-9934-deacf25fee55 ro recovery nomodeset dis_ucode_ldr net.ifnames=0


#重启生效
root@jiangfeng:~# reboot

1.3 ubuntu网卡

1.3.1 配置自动获取IP

网卡配置文件采用YAML格式,必须以/etc/netplan/XXX.yaml文件命名方式存放可以每个网卡对应一个单独的配置文件,也可以将所有网卡都放在一个配置文件里

root@jiangfeng:~# cat /etc/netplan/01-netcfg.yaml
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: yes

#修改网卡配置文件后需执行命令生效:
root@jiangfeng:~# netplan apply
1.3.2 配置静态IP
root@jiangfeng:~# cat /etc/netplan/01-netcfg.yaml
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      addresses: [192.168.8.10/24,10.0.0.10/8]   #或者用下面两行,两种格式不能混用
      - 192.168.8.10/24
      - 10.0.0.10/8
      gateway4: 10.0.0.2
      nameservers:
        search: [www.baidu.com]
        addresses: [180.76.76.76,114.114.114.114, 223.223.223.5]


#查看ip和网关
root@jiangfeng:~# ip a
root@jiangfeng:~# ip r

#查看DNS
root@jiangfeng:~# ls -l /etc/resolv.conf
1.3.3 配置多⽹卡静态IP和静态路由(生产建议一个网卡各自配置一个文件)
root@jiangfeng:~# cat /etc/netplan/01-netcfg.yaml
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: no
      dhcp6: no
      addresses: [10.0.0.100/16]
      gateway4: 10.0.0.2
      nameservers:
        addresses: [223.5.5.5]
    eth1:
      dhcp4: no
      dhcp6: no
      addresses: [10.20.0.100/16]
      routes:
      - to: 10.30.0.0/16
        via: 10.20.0.2
      - to: 10.40.0.0/16
        via: 10.20.0.2
      - to: 10.50.0.0/16
        via: 10.20.0.2
      - to: 10.60.0.0/16
        via: 10.20.0.2

root@jiangfeng:~# netplan apply
1.3.4 双网卡绑定
root@jiangfeng:~# cat /etc/netplan/01-netcfg.yaml
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: no
      dhcp6: no
    eth1:
      dhcp4: no
      dhcp6: no

  bonds:
    bond0:
      interfaces:
        - eth0
        - eth1
      addresses: [10.0.0.100/16]
      gateway4: 10.0.0.2
      nameservers:
        addresses: [223.6.6.6,223.5.5.5]
      parameters:
        mode: active-backup
        mii-monitor-interval: 100


root@jiangfeng:~# netplan apply
root@jiangfeng:~# ifconfig bond0