Linux的网络配置(二)
一、虚拟机的网络连通
1.建实验环境
主机端打开主机端路由功能,调整主机端路由配置。[[email protected] ~]$ su - root ##切换到真机的超级用户 Password: Last login: Wed Oct 24 18:16:04 CST 2018 on pts/0 [[email protected] ~]# firewall-cmd --list-all public (active) target: default icmp-block-inversion: no interfaces: br0 enp0s20f0u1c4i2 sources: services: dhcpv6-client ssh ports: protocols: masquerade: no ##路由功能是关闭的 forward-ports: sourceports: icmp-blocks: rich rules:
[[email protected] ~]# firewall-cmd --add-masquerade ##打开主机的路由功能 success [[email protected] ~]# sysctl -a | grep ip_forward ##查询内核路由功能是否开启 net.ipv4.ip_forward = 1 ##现实为1时内核路由打开 net.ipv4.ip_forward_use_pmtu = 0
注意:若:主机的ipv4显示为0,编辑/etc/sysctl.conf文件。
[[email protected] ~]# vim /etc/sysctl.conf
虚拟的网络配置
[[email protected] ~]# cd /etc/sysconfig/network-scripts/ [[email protected] network-scripts]# vim ifcfg-eth0 ##配置文件中设置参数 [[email protected] network-scripts]# systemctl restart network ##重新启动网关 [[email protected] network-scripts]# route -n ##查询网关是否设置成功 Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 172.25.254.69 0.0.0.0 UG 1024 0 0 eth0 1.1.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1 172.25.254.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
实验开始
主机ping百度
[[email protected] ~]$ ping -c 3 www.baidu.com PING www.a.shifen.com (220.181.112.244) 56(84) bytes of data. 64 bytes from 220.181.112.244 (220.181.112.244): icmp_seq=1 ttl=53 time=62.3 ms 64 bytes from 220.181.112.244 (220.181.112.244): icmp_seq=2 ttl=53 time=60.8 ms 64 bytes from 220.181.112.244 (220.181.112.244): icmp_seq=3 ttl=53 time=57.8 ms --- www.a.shifen.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2003ms rtt min/avg/max/mdev = 57.874/60.359/62.345/1.859 ms
主机可以ping通百度,且得到百度IP地址220.181.112.244。
虚拟机器因为没有配置DNS ,所以只能ping 220.181.112.244
[[email protected] network-scripts]# ping -c 3 220.181.112.244 PING 220.181.112.244 (220.181.112.244) 56(84) bytes of data. 64 bytes from 220.181.112.244: icmp_seq=1 ttl=52 time=224 ms 64 bytes from 220.181.112.244: icmp_seq=2 ttl=52 time=40.4 ms 64 bytes from 220.181.112.244: icmp_seq=3 ttl=52 time=59.0 ms --- 220.181.112.244 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2002ms rtt min/avg/max/mdev = 40.492/108.013/224.497/82.714 ms
实验完成
二、DNS的配置
1.什么是DNS
把域名翻译成IP地址的软件称为域名系统,即DNS。
DNS 是计算机域名系统 (Domain Name System 或 Domain Name Service) 的缩写,它是由域名解析器和域名服务器组成的。通过它可以把你需要访问的网址找到然后把信息送到你电脑上。2.临时配置虚拟机器的DNS
注意:主机去ping百度时间,会把域名转换成IP地址。而虚拟机不会,会出现UNKNOW
同过编辑本地解析文件我们将我们知道的域名和IP输入进去并保存。
[[email protected] ~]# vim /etc/hosts ##本地域名解析配置文件
由于手动的添加一个一个网址过于繁琐我们可以去添加一个DNS服务器,默认情况下主机会在本地寻找,若是找不到系统会将此域名交给DNS域名解析服务器。配置文件地址/etc/resolv.conf。
[[email protected] ~]# vim /etc/resolv.conf [[email protected] ~]# ping -c 3 www.taobao.com PING www.taobao.com.danuoyi.tbcache.com (171.8.242.186) 56(84) bytes of data. 64 bytes from 8.171.broad.ha.dynamic.163data.com.cn (171.8.242.186): icmp_seq=1 ttl=38 time=61.4 ms 64 bytes from 8.171.broad.ha.dynamic.163data.com.cn (171.8.242.186): icmp_seq=2 ttl=38 time=85.1 ms 64 bytes from 8.171.broad.ha.dynamic.163data.com.cn (171.8.242.186): icmp_seq=3 ttl=38 time=82.7 ms --- www.taobao.com.danuoyi.tbcache.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2003ms rtt min/avg/max/mdev = 61.497/76.468/85.114/10.631 ms
可以ping同为在本地添加的淘宝网。
注意:以上的设置都是临时设定,系统重新启动以后,所有的设置都无法保留
3.永久的设定DNS配置
(1)静态设定 (设定网络配置文件)
[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0 ##设置配置文件 [[email protected] ~]# systemctl restart network ##重新启动网关
一般DNS 可以设置两个,另一个为备用。
(1)动态设定 (设定网络配置文件)
[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0 [[email protected] ~]# vim /etc/resolv.conf [[email protected] ~]# systemctl restart network [[email protected] ~]# cat /etc/resolv.conf # Generated by NetworkManager domain ilt.example.com search ilt.example.com example.com nameserver 114.114.114.114
4.本地解析和DNS的优先级调整
系统默认情况下本地解析的优先级高于DNS的解析优先级,/etc/nsswitch.conf下调节优先级。
[[email protected] ~]# vim /etc/nsswitch.conf 39 hosts: files dns ##file 为本地解析 dns 为服务器解析
注意:调节file和dns的先后顺序就可以调整优先级。
三、nmcli命令
注意:nmcli命令需要 NetworkManager打开以后才可以用
[[email protected] ~]# systemctl stop NetworkManager [[email protected] ~]# nmcli device show Error: NetworkManager is not running.
nmcli device show eth0 ##显示eth0的信息 nmcli device status eth0 ##显示eth0的设备状态 nmcli device disconnect eth0 ##关闭名为eth0的设备 nmcli device connect eth0 ##开启名为eth0的设备
注意:以上服务主要用于企业操作,systemctl restart network 所有的网络接口包括网络都重新启动,造成不必要的系统问题,所在才使用此命令
[email protected] ~]# nmcli connection show ##显示所有网路连接 NAME UUID TYPE DEVICE System eth1 9c92fad9-6ecb-3e6c-eb4d-8a47c6f50c04 802-3-ethernet eth1 System eth0 5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03 802-3-ethernet eth0 [[email protected] ~]# nmcli connection delete 'System eth1' ##删除链接命为eth1的网络连接
[[email protected] ~]# nmcli connection add type ethernet con-name westos ifname eth1 autoconnect yes ##创建一个动态网口 Connection 'westos' (6a73517e-5218-4c87-a77b-92bb099b42fe) successfully added. ##westos创建成功 [[email protected] ~]# nmcli connection show NAME UUID TYPE DEVICE westos 6a73517e-5218-4c87-a77b-92bb099b42fe 802-3-ethernet eth1 System eth0 5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03 802-3-ethernet eth0
注意:此命令不能多次执行,记得删除之前的设定。
[[email protected] ~]# nmcli connection add con-name westos ifname eth1 type ethernet ip4 1.1.1.100/24 ##设置一个静态IP Connection 'westos' (d7644ffd-a451-4d72-b160-81fd6daa8736) successfully added. [[email protected] ~]# nmcli connection modify westos ipv4.method auto ##改为动态IPV4 [[email protected] ~]# nmcli connection up westos [[email protected] ~]# nmcli connection down westos