首先查看網口地址ifconfig
[root@shijie ~]# ifconfig
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.10 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::20c:29ff:fe53:ce07 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:53:ce:07 txqueuelen 1000 (Ethernet)
RX packets 489 bytes 48089 (46.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 191 bytes 34403 (33.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
方法1.編輯配置文件
網口配置文件目錄:/etc/sysconfig/network-script/ifcfg-eno16777736
vim /etc/sysconfig/network-script/ifcfg-eno16777736
TYPE=Ethernet # 設備類型
BOOTPROTO=static # 地址分配模式
NAME=eno16777736 # 網卡名稱
**ONBOOT=yes # 是否啓動 重要:開機後是否自啓**
IPADDR0=192.168.10.10 # IP地址
NETMASK=255.255.255.0 # 子網掩碼 或者 PREFIX0=24
GATEWAY=192.168.10.1 # 網關地址
DNS1=192.168.10.1 # DNS地址
方法2.ifconfig ethx x.x.x.x netmask x.x.x.x
實時生效,不需要重啓網絡服務,重啓後失效。
ifconfig eno16777736 192.168.10.10 netmask 255.255.255.0
方法3.nmtui網卡編輯工具
方法4:圖形界面中設置網絡
網卡模板功能
在不同場景下使用不同的網絡設置進行快速切換
添加company和house兩個網絡配置模板
[root@shijie ~]# nmcli connection add con-name company ifname eno16777736 autoconnect no type ethernet ip4 192.168.10.10/24 gw4 192.168.10.1
Connection 'company' (86c71220-0057-419e-b615-38f4014cfdee) successfully added.
[root@shijie ~]# nmcli connection add con-name house type ethernet ifname eno16777736
Connection 'house' (44acf0a7-07e2-40b4-94ba-69ea973090fb) successfully added.
查看網路會話模板
[root@shijie ~]# nmcli connection show
名稱 UUID 類型 設備
house df19ad40-e2fb-4ffb-b0f8-14aa60cf86e1 802-3-ethernet --
company 67746b28-e089-4603-b3e2-3d41152d5b31 802-3-ethernet --
eno16777736 d6dca6f7-2aca-4c7d-8c36-c6cb19059141 802-3-ethernet eno16777736
啓用house網絡地址切換,實時生效不需要重啓網絡服務
nmcli connection up house
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/4)
重啓網絡服務
systemctl restart network