Linux配置ip地址的兩種方法
Linux配置ip地址的兩種方法,實(shí)驗(yàn)環(huán)境為centos7.6
方法1:nmcli工具配置 (centos7以下版本不支持該方法)
第一步,通過(guò)nmcli connection查看網(wǎng)卡名稱
- [root@localhost ~]# nmcli connection
- NAME UUID TYPE DEVICE
- eth0 09be0948-faf1-43b6-a5a4-c19efab0bb48 ethernet eth0
第二步,配置ip,網(wǎng)關(guān),dns,并設(shè)置網(wǎng)卡開(kāi)機(jī)自動(dòng)啟動(dòng),最后開(kāi)啟網(wǎng)卡
- [root@localhost ~]# nmcli connection modify eth0 ipv4.addresses "192.168.1.201/24"
說(shuō)明:配置地址和掩碼
想要獲取更多技術(shù)干貨和資料,可以加群752160765一起學(xué)習(xí)哦!
- [root@localhost ~]# nmcli connection modify eth0 ipv4.gateway "192.168.1.1"
說(shuō)明:配置網(wǎng)關(guān)
- [root@localhost ~]# nmcli connection modify eth0 ipv4.dns "180.76.76.76"
說(shuō)明:配置dns
- [root@localhost ~]# nmcli connection modify eth0 ipv4.method manual
說(shuō)明:地址獲取的方法為手動(dòng)配置而不是dhcp
- [root@localhost ~]# nmcli connection modify eth0 autoconnect yes
說(shuō)明:開(kāi)機(jī)自動(dòng)打開(kāi)網(wǎng)卡
- [root@localhost ~]# nmcli connection up eth0
說(shuō)明:立即打開(kāi)網(wǎng)卡
- Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/4)
第三步,查看結(jié)果(這里使用ip addr命令查看,較新版本Linux系統(tǒng)支持該命令)
- [root@localhost ~]# ip addr
- 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
- link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
- inet 127.0.0.1/8 scope host lo
- valid_lft forever preferred_lft forever
- inet6 ::1/128 scope host
- valid_lft forever preferred_lft forever
- 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
- link/ether 00:0c:29:84:23:62 brd ff:ff:ff:ff:ff:ff
- inet 192.168.1.201/24 brd 192.168.1.255 scope global noprefixroute eth0
- valid_lft forever preferred_lft forever
- inet6 fe80::b7ad:e2ed:832e:99a9/64 scope link noprefixroute
- valid_lft forever preferred_lft forever
測(cè)試通信
- [root@localhost ~]# ping www.baidu.com
- PING www.a.shifen.com (61.135.169.121) 56(84) bytes of data.
- 64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=1 ttl=53 time=34.7 ms
- 64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=2 ttl=53 time=27.9 ms
- 64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=3 ttl=53 time=24.1 ms
- 64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=4 ttl=53 time=25.2 ms
- 64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=5 ttl=53 time=24.2 ms
- ^C
- --- www.a.shifen.com ping statistics ---
- 5 packets transmitted, 5 received, 0% packet loss, time 4005ms
- rtt min/avg/max/mdev = 24.177/27.277/34.718/3.970 ms
方法2:通過(guò)vi編輯網(wǎng)卡配置文件(最新版rhel8或centos8不推薦該方法,老版本rhel6及以下推薦該方法)
第一步,通過(guò)vi或vim打開(kāi)配置文件
- [root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
第二步,編輯相關(guān)的參數(shù)
- 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,進(jìn)行編輯,編輯完成后,按esc,再按:wq 保存退出。如果不保存,則是:q!退出
第三步,重啟網(wǎng)絡(luò)服務(wù)
- [root@localhost ~]# service network restart
- Restarting network (via systemctl): [ OK ]
第四步,查看結(jié)果并測(cè)試通信(這里用ifconfig命令來(lái)查看,各種版本Linux均支持該命令)
- [root@localhost ~]# ifconfig eth0
- eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
- inet 192.168.1.202 netmask 255.255.255.0 broadcast 192.168.1.255
- inet6 fe80::b7ad:e2ed:832e:99a9 prefixlen 64 scopeid 0x20<link>
- ether 00:0c:29:84:23:62 txqueuelen 1000 (Ethernet)
- RX packets 1117 bytes 127303 (124.3 KiB)
- RX errors 0 dropped 0 overruns 0 frame 0
- TX packets 564 bytes 69559 (67.9 KiB)
- TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
- [root@localhost ~]# ping www.baidu.com
- PING www.a.shifen.com (61.135.169.125) 56(84) bytes of data.
- 64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=1 ttl=53 time=28.2 ms
- 64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=2 ttl=53 time=30.9 ms
- ^C
- --- www.a.shifen.com ping statistics ---
- 3 packets transmitted, 2 received, 33% packet loss, time 2003ms
- rtt min/avg/max/mdev = 28.228/29.590/30.953/1.373 ms
- [root@localhost ~]#
配置地址的兩種方法就介紹到這里。
但是查看地址時(shí),我們并沒(méi)有看到網(wǎng)關(guān)和dns,那么網(wǎng)關(guān)和dns怎么看呢,用以下兩條命令即可
- [root@localhost ~]# route -n 通過(guò)查看路由表來(lái)知道網(wǎng)關(guān)
- Kernel IP routing table
- Destination Gateway Genmask Flags Metric Ref Use Iface
- 0.0.0.0 192.168.1.1 0.0.0.0 UG 100 0 0 eth0
- [root@localhost ~]# cat /etc/resolv.conf 通過(guò)查看rsolv.conf文件來(lái)查看dns
- # Generated by NetworkManager
- nameserver 180.76.76.76