红联Linux门户
Linux帮助

管理IPv4网络连接

发布时间:2016-12-22 10:20:58来源:linux网站作者:北海悟空
1.使用 nmcli 检查所有连接
[root@desktop1 ~]# nmcli connection 
NAME                UUID                                  TYPE            DEVICE
eno33554960         5be7dba9-6baf-407d-9227-88c034fb7e9c  802-3-ethernet  --
Bridge eno16777736  d2d68553-f97e-7549-7a26-b34a26f29318  bridge          br0
System eno16777736  8863e6a4-94da-488b-9de7-160b3b5951c8  802-3-ethernet  eno16777736
virbr0              b50fb815-6e76-49d3-937b-bbe0b7b137c1  bridge          virbr0
eno50332184         8e064c29-9aad-433c-a75e-1f80e6051018  802-3-ethernet  --
[root@desktop1 ~]#
 
2.删除‘virbr0’外的所有 connection
[root@desktop1 ~]# nmcli connection delete 'Bridge eno16777736' 'System eno16777736' 'eno50332184'
 
3.使用 ip addr 和 ping 确认你的 desktop 无法访问网络
[root@desktop1 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
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: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:94:d3:7a brd ff:ff:ff:ff:ff:ff
3: eno33554960: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:94:d3:84 brd ff:ff:ff:ff:ff:ff
4: eno50332184: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:94:d3:8e brd ff:ff:ff:ff:ff:ff
6: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN 
link/ether a2:11:a8:b3:ca:97 brd ff:ff:ff:ff:ff:ff
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
valid_lft forever preferred_lft forever
[root@desktop1 ~]# ping 192.168.0.254
connect: Network is unreachable
[root@desktop1 ~]# 
 
4.创建一个新的 connection,名称为 net1,接口为你的 desktop 的第一个网卡(eno16777736),开启自动连接 (查看网卡信息:nmcli device status)
[root@desktop1 ~]# nmcli connection add con-name net1 type ethernet ifname eno16777736 autoconnect yes
Connection 'net1' (4e2cba1b-b8ff-4036-812f-9772d2a5cdd8) successfully added.
[root@desktop1 ~]# 
 
5.修改 net1,IPv4 地址为手动地址,地址是 192.168.0.1/24 ,网关和 dns 是 192.168.0.254 
[root@desktop1 ~]# nmcli connection modify net1 ipv4.addresses '192.168.0.1/24 192.168.0.254' ipv4.dns '192.168.0.254' ipv4.method manual
[root@desktop1 ~]# 
 
6.重新启动 net1,确认 IP 地址是静态 IP 地址 
[root@desktop1 ~]# nmcli connection up net1 
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/4)
[root@desktop1 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
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: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:94:d3:7a brd ff:ff:ff:ff:ff:ff
inet 192.168.0.1/24 brd 192.168.0.255 scope global eno16777736
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe94:d37a/64 scope link 
valid_lft forever preferred_lft forever
3: eno33554960: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:94:d3:84 brd ff:ff:ff:ff:ff:ff
4: eno50332184: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:94:d3:8e brd ff:ff:ff:ff:ff:ff
6: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN 
link/ether a2:11:a8:b3:ca:97 brd ff:ff:ff:ff:ff:ff
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
valid_lft forever preferred_lft forever
[root@desktop1 ~]# ip route 
default via 192.168.0.254 dev eno16777736  proto static  metric 1024 
192.168.0.0/24 dev eno16777736  proto kernel  scope link  src 192.168.0.1 
192.168.122.0/24 dev virbr0  proto kernel  scope link  src 192.168.122.1 
[root@desktop1 ~]# ping 192.168.0.254
PING 192.168.0.254 (192.168.0.254) 56(84) bytes of data.
64 bytes from 192.168.0.254: icmp_seq=1 ttl=64 time=0.688 ms
64 bytes from 192.168.0.254: icmp_seq=2 ttl=64 time=0.658 ms
^C
--- 192.168.0.254 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 0.658/0.681/0.698/0.027 ms
[root@desktop1 ~]# 
 
7.修改 net1,增加一个 dns 地址为 192.168.1.254
[root@desktop1 ~]# nmcli connection modify net1 +ipv4.dns '192.168.1.254'
[root@desktop1 ~]# nmcli connection up net1 
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5)
[root@desktop1 ~]# cat /etc/resolv.conf 
# Generated by NetworkManager
search example.com
nameserver 192.168.0.254
nameserver 192.168.1.254
[root@desktop1 ~]# 
 
8.修改 net1,删除上一步配置的 dns192.168.1.254,但保留 192.168.0.254
[root@desktop1 ~]# nmcli connection modify net1 -ipv4.dns '192.168.1.254'
[root@desktop1 ~]# nmcli connection up net1 
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/6)
[root@desktop1 ~]# cat /etc/resolv.conf 
# Generated by NetworkManager
search example.com
nameserver 192.168.0.254
[root@desktop1 ~]# 
 
9.编辑 hosts 文件,确保访问“myserver”会被解析为 192.168.0.(100+X )
[root@desktop1 ~]# echo '192.168.0.101 myserver' >> /etc/hosts
[root@desktop1 ~]# ping myserver
PING myserver (192.168.0.101) 56(84) bytes of data.
64 bytes from myserver (192.168.0.101): icmp_seq=1 ttl=64 time=0.679 ms
64 bytes from myserver (192.168.0.101): icmp_seq=2 ttl=64 time=0.509 ms
64 bytes from myserver (192.168.0.101): icmp_seq=3 ttl=64 time=0.595 ms
64 bytes from myserver (192.168.0.101): icmp_seq=4 ttl=64 time=0.773 ms
^C
--- myserver ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 0.509/0.639/0.773/0.097 ms
[root@desktop1 ~]#
 
本文永久更新地址:http://www.linuxdiyf.com/linux/27140.html