由于最近 DIY 了一台主机,在新主机上创建Linux虚拟机做服务器集群需要服务器IP为静态IP,之前在Cent OS上设置过,这次使用Ubuntu 22.04.2 会有些区别,一并记录下设置方法。
设置静态IP重点是以下信息:
- 网段
- 网关
- IP地址
- dns服务器地址
- 禁用 dhcp 协议
虚拟机与宿主机之间使用了 NAT,宿主机网段和虚拟机网段不同。
ubuntu
ubuntu 我使用的是 ubuntu 22.04.2。
查看下本机使用的网卡:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
root@node1:/home/uphie# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.160.128 netmask 255.255.255.0 broadcast 192.168.160.255
inet6 fe80::20c:29ff:feab:fc16 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:ab:fc:16 txqueuelen 1000 (Ethernet)
RX packets 48251 bytes 69947833 (69.9 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 5109 bytes 362447 (362.4 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 217 bytes 27916 (27.9 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 217 bytes 27916 (27.9 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
得知网卡为 ens33
,子网掩码为24位(255.255.255.0)。
查看下路由表:
1
2
3
4
5
6
root@node1:/home/uphie# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.160.2 0.0.0.0 UG 100 0 0 ens33
192.168.160.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.160.2 0.0.0.0 255.255.255.255 UH 100 0 0 ens33
网关为 192.168.160.2
。
查看网卡配置文件:
1
2
3
4
5
root@node1:/home/uphie# ll /etc/netplan/
total 12
drwxr-xr-x 2 root root 4096 Jun 22 16:04 ./
drwxr-xr-x 97 root root 4096 Jun 26 15:20 ../
-rw-r--r-- 1 root root 116 Jun 22 16:04 00-installer-config.yaml
编辑 /etc/netplan/00-installer-config.yaml
,
原配置:
1
2
3
4
5
6
# This is the network config written by 'subiquity'
network:
ethernets:
ens33:
dhcp4: true
version: 2
改为下面这样:
1
2
3
4
5
6
7
8
9
10
11
# This is the network config written by 'subiquity'
network:
ethernets:
ens33:
addresses: [192.168.160.101/24] # 静态IP及掩码
gateway4: 192.168.160.2 # 网关
dhcp4: false # 禁用 dhcp
nameservers:
addresses: [192.168.160.2,114.114.114.114] # dns 服务器
version: 2
renderer: networkd
我们仍使用之前的网段、网关和掩码,dns服务器优先使用网关,其次 114.114.114.114
。
启用新配置:
1
root@node1:/home/uphie# netplan apply
重新查看IP,并检测网络连接情况:
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
root@node1:/home/uphie# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.160.101 netmask 255.255.255.0 broadcast 192.168.160.255
inet6 fe80::20c:29ff:feab:fc16 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:ab:fc:16 txqueuelen 1000 (Ethernet)
RX packets 51869 bytes 73460998 (73.4 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 5970 bytes 467493 (467.4 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 259 bytes 33608 (33.6 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 259 bytes 33608 (33.6 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
root@node1:/home/uphie# ping baidu.com
PING baidu.com (39.156.66.10) 56(84) bytes of data.
64 bytes from 39.156.66.10 (39.156.66.10): icmp_seq=1 ttl=128 time=44.1 ms
64 bytes from 39.156.66.10 (39.156.66.10): icmp_seq=3 ttl=128 time=250 ms
64 bytes from 39.156.66.10 (39.156.66.10): icmp_seq=4 ttl=128 time=44.3 ms
64 bytes from 39.156.66.10 (39.156.66.10): icmp_seq=5 ttl=128 time=45.0 ms
64 bytes from 39.156.66.10 (39.156.66.10): icmp_seq=6 ttl=128 time=45.6 ms
^C
--- baidu.com ping statistics ---
6 packets transmitted, 5 received, 16.6667% packet loss, time 5027ms
rtt min/avg/max/mdev = 44.108/85.718/249.637/81.961 ms
修改完成!
cent os
cent os 我使用的是cent os 7,cent os 8不再更新维护,建议使用 cent os 7 或 较新的 cent os stream。
首先查看网络信息:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@localhost uphie]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.160.130 netmask 255.255.255.0 broadcast 192.168.160.255
inet6 fe80::20c:29ff:fe9c:2de7 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:9c:2d:e7 txqueuelen 1000 (Ethernet)
RX packets 222962 bytes 328889321 (313.6 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 34000 bytes 3591093 (3.4 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 24 bytes 1992 (1.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 24 bytes 1992 (1.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
网卡为 ens33
。
查看路由:
1
2
3
4
5
6
[root@localhost uphie]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.160.2 0.0.0.0 UG 0 0 0 ens33
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 ens33
192.168.160.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33
网关为 192.168.160.2
。
和 Ubuntu 不同的是,centos 需要编辑 /etc/sysconfig/network-scripts/ifcfg-ens33
,ifcfg-ens33
中的 ens33
正是网卡名。
之前为:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=3a8c9789-79a8-4745-bd39-f0698404e35c
DEVICE=ens33
ONBOOT=yes
改为:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static # 改为静态
IPADDR="192.168.160.111" # 添加静态IP地址
GATEWAY="192.168.160.2" # 添加网关
NETMASK="255.255.255.0" # 添加子网掩码
DNS="192.168.160.2,114.114.114.114" # 添加dns服务器
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=3a8c9789-79a8-4745-bd39-f0698404e35c
DEVICE=ens33
ONBOOT=yes
重新启动network服务使生效:
1
[root@localhost uphie]# systemctl restart network
检查网络:
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
[root@localhost uphie]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.160.111 netmask 255.255.255.0 broadcast 192.168.160.255
inet6 fe80::20c:29ff:fe9c:2de7 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:9c:2d:e7 txqueuelen 1000 (Ethernet)
RX packets 223687 bytes 328944780 (313.7 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 34336 bytes 3646356 (3.4 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 24 bytes 1992 (1.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 24 bytes 1992 (1.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost uphie]# ping baidu.com
PING baidu.com (39.156.66.10) 56(84) bytes of data.
64 bytes from 39.156.66.10 (39.156.66.10): icmp_seq=1 ttl=128 time=44.8 ms
64 bytes from 39.156.66.10 (39.156.66.10): icmp_seq=2 ttl=128 time=49.4 ms
64 bytes from 39.156.66.10 (39.156.66.10): icmp_seq=3 ttl=128 time=45.1 ms
^C
--- baidu.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2004ms
rtt min/avg/max/mdev = 44.896/46.492/49.401/2.060 ms
修改完成!