Linux配置ip地址的两种方法,实验环境为centos7.6
方法1:nmcli工具配置(centos7以下版本不支持该方法)
第一步,通过nmcliconnection查看网卡名称
[root@localhost~]#nmcliconnection
NAMEUUIDTYPEDEVICE
eth009be0948-faf1-43b6-a5a4-c19efab0bb48etherneteth0
第二步,配置ip,网关,dns,并设置网卡开机自动启动,最后开启网卡
[root@localhost~]#nmcliconnectionmodifyeth0ipv4.addresses"192.168.1.201/24"
说明:配置地址和掩码
想要获取更多技术干货和资料,可以加群752160765一起学习哦!
[root@localhost~]#nmcliconnectionmodifyeth0ipv4.gateway"192.168.1.1"
说明:配置网关
[root@localhost~]#nmcliconnectionmodifyeth0ipv4.dns"180.76.76.76"
说明:配置dns
[root@localhost~]#nmcliconnectionmodifyeth0ipv4.methodmanual
说明:地址获取的方法为手动配置而不是dhcp
[root@localhost~]#nmcliconnectionmodifyeth0autoconnectyes
说明:开机自动打开网卡
[root@localhost~]#nmcliconnectionupeth0
说明:立即打开网卡
Connectionsuccessfullyactivated(D-Busactivepath:/org/freedesktop/NetworkManager/ActiveConnection/4)
第三步,查看结果(这里使用ipaddr命令查看,较新版本Linux系统支持该命令)
[root@localhost~]#ipaddr
1:lo:<LOOPBACK,UP,LOWER_UP>mtu65536qdiscnoqueuestateUNKNOWNgroupdefaultqlen1000
link/loopback00:00:00:00:00:00brd00:00:00:00:00:00
inet127.0.0.1/8scopehostlo
valid_lftforeverpreferred_lftforever
inet6::1/128scopehost
valid_lftforeverpreferred_lftforever
2:eth0:<BROADCAST,MULTICAST,UP,LOWER_UP>mtu1500qdiscpfifo_faststateUPgroupdefaultqlen1000
link/ether00:0c:29:84:23:62brdff:ff:ff:ff:ff:ff
inet192.168.1.201/24brd192.168.1.255scopeglobalnoprefixrouteeth0
valid_lftforeverpreferred_lftforever
inet6fe80::b7ad:e2ed:832e:99a9/64scopelinknoprefixroute
valid_lftforeverpreferred_lftforever
测试通信
[root@localhost~]#pingwww.baidu.com
PINGwww.a.shifen.com(61.135.169.121)56(84)bytesofdata.
64bytesfrom61.135.169.121(61.135.169.121):icmp_seq=1ttl=53time=34.7ms
64bytesfrom61.135.169.121(61.135.169.121):icmp_seq=2ttl=53time=27.9ms
64bytesfrom61.135.169.121(61.135.169.121):icmp_seq=3ttl=53time=24.1ms
64bytesfrom61.135.169.121(61.135.169.121):icmp_seq=4ttl=53time=25.2ms
64bytesfrom61.135.169.121(61.135.169.121):icmp_seq=5ttl=53time=24.2ms
^C
---www.a.shifen.compingstatistics---
5packetstransmitted,5received,0%packetloss,time4005ms
rttmin/avg/max/mdev=24.177/27.277/34.718/3.970ms
方法2:通过vi编辑网卡配置文件(最新版rhel8或centos8不推荐该方法,老版本rhel6及以下推荐该方法)
第一步,通过vi或vim打开配置文件
[root@localhost~]#vim/etc/sysconfig/network-scripts/ifcfg-eth0
第二步,编辑相关的参数
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
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=eth0
UUID=09be0948-faf1-43b6-a5a4-c19efab0bb48
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.1.202
PREFIX=24
GATEWAY=192.168.1.1
DNS1=180.76.76.76
PEERDNS=no
vi的编辑方法是,输入字母i,进行编辑,编辑完成后,按esc,再按:wq保存退出。如果不保存,则是:q!退出
第三步,重启网络服务
[root@localhost~]#servicenetworkrestart
Restartingnetwork(viasystemctl):[OK]
第四步,查看结果并测试通信(这里用ifconfig命令来查看,各种版本Linux均支持该命令)
[root@localhost~]#ifconfigeth0
eth0:flags=4163<UP,BROADCAST,RUNNING,MULTICAST>mtu1500
inet192.168.1.202netmask255.255.255.0broadcast192.168.1.255
inet6fe80::b7ad:e2ed:832e:99a9prefixlen64scopeid0x20<link>
ether00:0c:29:84:23:62txqueuelen1000(Ethernet)
RXpackets1117bytes127303(124.3KiB)
RXerrors0dropped0overruns0frame0
TXpackets564bytes69559(67.9KiB)
TXerrors0dropped0overruns0carrier0collisions0
[root@localhost~]#pingwww.baidu.com
PINGwww.a.shifen.com(61.135.169.125)56(84)bytesofdata.
64bytesfrom61.135.169.125(61.135.169.125):icmp_seq=1ttl=53time=28.2ms
64bytesfrom61.135.169.125(61.135.169.125):icmp_seq=2ttl=53time=30.9ms
^C
---www.a.shifen.compingstatistics---
3packetstransmitted,2received,33%packetloss,time2003ms
rttmin/avg/max/mdev=28.228/29.590/30.953/1.373ms
[root@localhost~]#
配置地址的两种方法就介绍到这里。
但是查看地址时,我们并没有看到网关和dns,那么网关和dns怎么看呢,用以下两条命令即可
[root@localhost~]#route-n通过查看路由表来知道网关
KernelIProutingtable
DestinationGatewayGenmaskFlagsMetricRefUseIface
0.0.0.0192.168.1.10.0.0.0UG10000eth0
[root@localhost~]#cat/etc/resolv.conf通过查看rsolv.conf文件来查看dns
#GeneratedbyNetworkManager
nameserver180.76.76.76