1.修改主机名
# 永久修改
root@nginx:~# hostnamectl set-hostname ubuntu
root@ubuntu:~#
# 查看主机名(用hostname命令确认)
hong@ubuntu:~$ hostname
ubuntu
hong@ubuntu:~$ hostname -I
10.0.0.4
# 查看主机名(通过配置文件确认)
hong@ubuntu:~$ cat /etc/hostname
ubuntu
# 查看主机名(通过环境变量确认)
hong@ubuntu:~$ echo $HOSTNAME
ubuntu
2.修改网卡名
- 修改网卡名称为传统命令方式:
范例:通过grub配置文件修改网卡名为eth
root@ubuntu:~# cat /etc/default/grub
GRUB_CMDLINE_LINUX="net.ifnames=0" # 网卡名修改项
范例:通过sed编辑grub配置文件
root@ubuntu:~# sed -i.bak ‘/^GRUB_CMDLINE_LINUX=/s#"net.ifnames=0"#‘ /etc/default/grub
- 让新的grub.cfg文件生效
范例:用命令生效
# 方式一:
root@ubuntu:~# grub-mkconfig -o /boot/grub/grub.cfg
# 方式二:
root@ubuntu:~# update-grub
root@ubuntu:~# grep net.ifnames /boot/grub/grub.cfg
范例:重启服务器生效
root@ubuntu:~# reboot
3.网卡配置
官文:
https://help.ubuntu.com/lts/serverguide/network-confifiguration.html.zh-CN
https://ubuntu.com/server/docs/network-confifiguration
3.1配置自动获取IP
- 配置网卡文件为DHCP自动获取IP
root@ubuntu:~# cat /etc/netplan/01-network-manager-all.yaml
# Let NetworkManager manage all devices on this system
network:
version: 2
renderer: NetworkManager
ethernets:
eth0:
dhcp4: yes
- 执行命令使其生效
root@ubuntu:~# netplan apply
3.2配置静态IP
- 修改配置文件
root@ubuntu:~# cat /etc/netplan/01-network-manager-all.yaml
# Let NetworkManager manage all devices on this system
network:
version: 2
renderer: NetworkManager
ethernets:
eth0:
dhcp4: no
addresses: [10.0.0.51/24]
gateway4: 10.0.0.2
nameservers:
addresses: [8.8.8.8,8.8.4.4]
- 查看IP和Gateway
root@ubuntu:~# ip addr # 查看IP 或者简写为 ip a
root@ubuntu:~# route -n # 查看路由
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.0.0.2 0.0.0.0 UG 100 0 0 eth0
10.0.0.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1000 0 0 eth0
- 查看DNS
root@ubuntu:~# ls -l /etc/resolv.conf
lrwxrwxrwx 1 root root 39 3月 11 13:47 /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf
root@ubuntu:~# systemd-resolve --status
3.3配置多网卡静态IP和静态路由
实例:
# 配置多网卡静态IP和静态路由
root@ubuntu:~# cat /etc/netplan/01-network-manager-all.yaml
# Let NetworkManager manage all devices on this system
network:
version: 2
renderer: NetworkManager
ethernets:
eth0:
dhcp4: no
addresses: [10.0.0.51/24]
gateway4: 10.0.0.2
nameservers:
addresses: [8.8.8.8,8.8.4.4]
eth1:
dhcp4: no
addresses: [192.168.40.245/24]
nameservers:
addresses: [223.6.6.6]
routes:
- to: 192.168.40.0/24
via: 192.168.40.1
# 使得配置立即生效
root@ubuntu:~# netplan apply
# 查看路由
root@ubuntu:~# route -n
范例:给每个网卡配置单独配置文件
root@ubuntu:~# cat /etc/netplan/01-eth0.yaml
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: yes
root@ubuntu:~# cat /etc/netplan/02-eth1.yaml
network:
version: 2
renderer: networkd
ethernets:
eth1:
addresses:
- 10.20.0.51/16
- 192.168.40.253/24
gateway4: 10.0.0.2
nameservers:
addresses: [233.6.6.6]
dhcp4: no
3.4单网卡桥接
root@ubuntu:~# apt install -y bridge-utils # apt相当于yum
root@ubuntu:~# dpkg -L bridge-utils # dpkg相当于rpm