nmcli 命令的方式
在我们的ens160网卡上添加ip地址、网关、dns、获取IP方式改为静态、最后connection.autoconnect 设为yes
我们在添加ip时注意添加我们的网段
dns可以是114.114.114.114或者8.8.8.8都可以
[root@redhat ~]# nmcli connection modify ens160 ipv4.addresses 192.168.12.130/24 ipv4.gateway 192.168.12.2 ipv4.dns 114.114.114.114 ipv4.method manual connection.autoconnect yes
使用ip a查看我们的网络信息,可以看见我们的ens网卡上的192.168.12.130/24的地址已经成功添加
[root@redhat ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope host valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000link/ether 00:0c:29:b0:ee:cd brd ff:ff:ff:ff:ff:ffinet 192.168.12.130/24 brd 192.168.12.255 scope global noprefixroute ens160valid_lft forever preferred_lft foreverinet 192.168.12.133/24 brd 192.168.12.255 scope global secondary dynamic ens160valid_lft 1460sec preferred_lft 1460secinet6 fe80::bef9:1972:a66:671d/64 scope link noprefixroute valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000link/ether 52:54:00:be:3f:6c brd ff:ff:ff:ff:ff:ffinet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0valid_lft forever preferred_lft forever
4: virbr0-nic: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel master virbr0 state DOWN group default qlen 1000link/ether 52:54:00:be:3f:6c brd ff:ff:ff:ff:ff:ff
我们在手动添加之后网络状态还为之前的状态,所以我们先关闭连接,再打开连接
[root@redhat ~]# nmcli connection down ens160
Connection 'ens160' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/13)
[root@redhat ~]# nmcli connection up ens160
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/14)
测试网络是否连接成功
此时的网络是已经通了
[root@redhat ~]# ping www.baidu.com
PING www.a.shifen.com (110.242.68.3) 56(84) bytes of data.
64 bytes from 110.242.68.3 (110.242.68.3): icmp_seq=1 ttl=128 time=61.7 ms
64 bytes from 110.242.68.3 (110.242.68.3): icmp_seq=2 ttl=128 time=114 ms
^C
--- www.a.shifen.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 2ms
rtt min/avg/max/mdev = 61.650/87.915/114.181/26.267 ms
添加多个ip
[root@redhat ~]# nmcli connection modify ens160 +ipv4.addresses 192.168.12.131/24 ipv4.gateway 192.168.12.2 ipv4.dns 114.114.114.114 ipv4.method manual connection.autoconnect yes
[root@redhat ~]# nmcli connection modify ens160 +ipv4.addresses 192.168.12.132/24 +ipv4.dns 8.8.8.8
一样添加之后还是先断开再重连
[root@redhat ~]# nmcli connection down ens160
Connection 'ens160' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/14)
[root@redhat ~]# nmcli connection up ens160
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/15)
[root@redhat ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope host valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000link/ether 00:0c:29:b0:ee:cd brd ff:ff:ff:ff:ff:ffinet 192.168.12.130/24 brd 192.168.12.255 scope global noprefixroute ens160valid_lft forever preferred_lft foreverinet 192.168.12.131/24 brd 192.168.12.255 scope global secondary noprefixroute ens160valid_lft forever preferred_lft foreverinet 192.168.12.132/24 brd 192.168.12.255 scope global secondary noprefixroute ens160valid_lft forever preferred_lft foreverinet6 fe80::bef9:1972:a66:671d/64 scope link noprefixroute valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000link/ether 52:54:00:be:3f:6c brd ff:ff:ff:ff:ff:ffinet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0valid_lft forever preferred_lft forever
4: virbr0-nic: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel master virbr0 state DOWN group default qlen 1000link/ether 52:54:00:be:3f:6c brd ff:ff:ff:ff:ff:ff
图形界面直接添加
首先在我们的虚拟机右上角的下拉按扭
先点击Wried Connected,再点击Wried Settings
点击设置按钮
点击ipv4,IPv4 Method 选择Manual ,在Addresses下添加IP地址,子网掩码,默认网关,DNS下添加dns地址
最后点击Apply按钮保存退出,
如需要添加多个ip,直接添加在下面即可,之后就不再需要输入默认网关
先断开之前的连接状态,再开启我们的网络状态
最后在Terminal窗口或则自带的火狐浏览器中测试网络即可