Linux配置ip地址的两种方法

运维 系统运维
Linux配置ip地址的两种方法,实验环境为centos7.6:方法1:nmcli工具配置 (centos7以下版本不支持该方法);方法2:通过vi编辑网卡配置文件(最新版rhel8或centos8不推荐该方法,老版本rhel6及以下推荐该方法)。

Linux配置ip地址的两种方法,实验环境为centos7.6

方法1:nmcli工具配置 (centos7以下版本不支持该方法)

第一步,通过nmcli connection查看网卡名称

 

  1. [root@localhost ~]# nmcli connection  
  2. NAME UUID TYPE DEVICE  
  3. eth0 09be0948-faf1-43b6-a5a4-c19efab0bb48 ethernet eth0 

第二步,配置ip,网关,dns,并设置网卡开机自动启动,最后开启网卡

  1. [root@localhost ~]# nmcli connection modify eth0 ipv4.addresses "192.168.1.201/24" 

说明:配置地址和掩码

想要获取更多技术干货和资料,可以加群752160765一起学习哦!

  1. [root@localhost ~]# nmcli connection modify eth0 ipv4.gateway "192.168.1.1" 

说明:配置网关

  1. [root@localhost ~]# nmcli connection modify eth0 ipv4.dns "180.76.76.76" 

说明:配置dns

  1. [root@localhost ~]# nmcli connection modify eth0 ipv4.method manual 

说明:地址获取的方法为手动配置而不是dhcp

  1. [root@localhost ~]# nmcli connection modify eth0 autoconnect yes 

说明:开机自动打开网卡

  1. [root@localhost ~]# nmcli connection up eth0 

说明:立即打开网卡

  1. Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/4) 

第三步,查看结果(这里使用ip addr命令查看,较新版本Linux系统支持该命令)

 

  1. [root@localhost ~]# ip addr 
  2.  
  3.  
  4. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 
  5. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 
  6. inet 127.0.0.1/8 scope host lo 
  7. valid_lft forever preferred_lft forever 
  8. inet6 ::1/128 scope host 
  9. valid_lft forever preferred_lft forever 
  10.  
  11.  
  12. 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 
  13. link/ether 00:0c:29:84:23:62 brd ff:ff:ff:ff:ff:ff 
  14. inet 192.168.1.201/24 brd 192.168.1.255 scope global noprefixroute eth0 
  15. valid_lft forever preferred_lft forever 
  16. inet6 fe80::b7ad:e2ed:832e:99a9/64 scope link noprefixroute 
  17. valid_lft forever preferred_lft forever 

测试通信

 

  1. [root@localhost ~]# ping www.baidu.com 
  2. PING www.a.shifen.com (61.135.169.121) 56(84) bytes of data. 
  3. 64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=1 ttl=53 time=34.7 ms 
  4. 64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=2 ttl=53 time=27.9 ms 
  5. 64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=3 ttl=53 time=24.1 ms 
  6. 64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=4 ttl=53 time=25.2 ms 
  7. 64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=5 ttl=53 time=24.2 ms 
  8. ^C 
  9. --- www.a.shifen.com ping statistics --- 
  10. 5 packets transmitted, 5 received, 0% packet loss, time 4005ms 
  11. rtt min/avg/max/mdev = 24.177/27.277/34.718/3.970 ms 

方法2:通过vi编辑网卡配置文件(最新版rhel8或centos8不推荐该方法,老版本rhel6及以下推荐该方法)

第一步,通过vi或vim打开配置文件

  1. [root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0 

第二步,编辑相关的参数

 

  1. TYPE=Ethernet 
  2. PROXY_METHOD=none 
  3. BROWSER_ONLY=no 
  4. BOOTPROTO=none 
  5. DEFROUTE=yes 
  6. IPV4_FAILURE_FATAL=no 
  7. IPV6INIT=yes 
  8. IPV6_AUTOCONF=yes 
  9. IPV6_DEFROUTE=yes 
  10. IPV6_FAILURE_FATAL=no 
  11. IPV6_ADDR_GEN_MODE=stable-privacy 
  12. NAME=eth0 
  13. UUID=09be0948-faf1-43b6-a5a4-c19efab0bb48 
  14. DEVICE=eth0 
  15. ONBOOT=yes 
  16. IPADDR=192.168.1.202 
  17. PREFIX=24 
  18. GATEWAY=192.168.1.1 
  19. DNS1=180.76.76.76 
  20. PEERDNS=no 

vi的编辑方法是,输入字母i,进行编辑,编辑完成后,按esc,再按:wq 保存退出。如果不保存,则是:q!退出

第三步,重启网络服务

 

  1. [root@localhost ~]# service network restart  
  2. Restarting network (via systemctl): [ OK ] 

第四步,查看结果并测试通信(这里用ifconfig命令来查看,各种版本Linux均支持该命令)

 

  1. [root@localhost ~]# ifconfig eth0 
  2. eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 
  3. inet 192.168.1.202 netmask 255.255.255.0 broadcast 192.168.1.255 
  4. inet6 fe80::b7ad:e2ed:832e:99a9 prefixlen 64 scopeid 0x20<link> 
  5. ether 00:0c:29:84:23:62 txqueuelen 1000 (Ethernet) 
  6. RX packets 1117 bytes 127303 (124.3 KiB) 
  7. RX errors 0 dropped 0 overruns 0 frame 0 
  8. TX packets 564 bytes 69559 (67.9 KiB) 
  9. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0  
  10.  
  11.  
  12. [root@localhost ~]# ping www.baidu.com 
  13. PING www.a.shifen.com (61.135.169.125) 56(84) bytes of data. 
  14. 64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=1 ttl=53 time=28.2 ms 
  15. 64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=2 ttl=53 time=30.9 ms 
  16. ^C 
  17. --- www.a.shifen.com ping statistics --- 
  18. 3 packets transmitted, 2 received, 33% packet loss, time 2003ms 
  19. rtt min/avg/max/mdev = 28.228/29.590/30.953/1.373 ms 
  20. [root@localhost ~]# 

配置地址的两种方法就介绍到这里。

但是查看地址时,我们并没有看到网关和dns,那么网关和dns怎么看呢,用以下两条命令即可

 

  1. [root@localhost ~]# route -n 通过查看路由表来知道网关 
  2.  
  3. Kernel IP routing table 
  4. Destination Gateway Genmask Flags Metric Ref Use Iface 
  5. 0.0.0.0 192.168.1.1 0.0.0.0 UG 100 0 0 eth0 
  6. [root@localhost ~]# cat /etc/resolv.conf 通过查看rsolv.conf文件来查看dns 
  7. # Generated by NetworkManager 
  8. nameserver 180.76.76.76 

 

责任编辑:未丽燕 来源: 今日头条
相关推荐

2011-11-30 15:43:40

JavaJSP

2010-08-03 13:53:47

Flex+Java配置

2010-08-04 17:41:52

挂载NFS

2018-03-02 13:28:19

Linux开机自启动

2010-08-02 16:58:08

Flex配置文件

2009-08-06 19:37:17

2009-09-25 14:04:09

Hibernate eHibernate h

2011-03-30 17:04:24

MySQL添加用户

2010-04-13 09:50:44

Oracle跟踪

2010-11-24 14:36:25

修复mysql表

2010-02-06 14:35:36

ibmdwRUP迭代

2010-09-09 19:53:50

2009-06-18 11:09:42

2009-08-05 13:34:18

C#日期相减

2010-09-07 11:18:10

2019-09-15 17:35:28

Wireshark解密HTTPS

2010-11-12 11:44:37

SQL Server删

2010-05-24 16:22:43

Linux SNMP

2010-10-27 14:47:32

Windows 200远程证书

2009-11-06 09:48:40

WCF服务
点赞
收藏

51CTO技术栈公众号