vim方式
- 先用
ifconfig
获取自己的网卡号
[root@master lyf]# ifconfig
#执行结果
#其中:ens33是网卡名字
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.64.128 netmask 255.255.255.0 broadcast 192.168.64.255
inet6 fe80::2376:f203:5e45:6ec2 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:25:b4:45 txqueuelen 1000 (Ethernet)
RX packets 432 bytes 250302 (244.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 417 bytes 35102 (34.2 KiB)
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 48 bytes 4080 (3.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 48 bytes 4080 (3.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:c1:f2:c0 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33 表示第一块网卡,
inet addr 用来表示网卡的IP地址,此网卡的IP地址是192.168.64.128
,广播地址Bcast:192.168.64.255
,掩码地址Mask:255.255.255.0
。
ether后面是网卡物理地址,此网卡物理地址是 00:0c:29:25:b4:45
- 切换到
/etc/sysconfig/network-script/
目录中(存放网卡配置的文件)
[root@master lyf]# cd /etc/sysconfig/network-scripts/
- 使用vim编辑器修改网卡
ifcfg-ens33
(网卡名称),写入下面的配置参数并保存退出。
/*
设备类型:TYPE=Ethernet
地址分配模式:BOOTPROTO=static
网卡名称:NAME=ens33
是否启动:ONBOOT=yes
IP地址:IPADDR=192.168.64.129
子网掩码:NETMASK=255.255.255.0
网关地址:GATEWAY=192.168.64.1
DNS地址:DNS1=192.168.64.1
*/
vim ifcfg-ens33
- 配置好后保存并退出,然后输入
nmcli connection reload ens33
重启网卡
- 测试使用ping命令测试网络是否连通
[root@master network-scripts]# ping baidu.com
PING baidu.com (110.242.68.66) 56(84) bytes of data.
64 bytes from 110.242.68.66 (110.242.68.66): icmp_seq=1 ttl=128 time=39.4 ms
64 bytes from 110.242.68.66 (110.242.68.66): icmp_seq=2 ttl=128 time=45.2 ms
64 bytes from 110.242.68.66 (110.242.68.66): icmp_seq=3 ttl=128 time=37.3 ms
64 bytes from 110.242.68.66 (110.242.68.66): icmp_seq=4 ttl=128 time=37.7 ms
^C
--- baidu.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3011ms
rtt min/avg/max/mdev = 37.386/39.942/45.217/3.148 ms
以上2、3步命令课可以直接写为:
[root@master network-scripts]# vi /etc/sysconfig/network-scripts/ifcfg-ens33