Linux中的网络配置(二) ---地址解析(DNS)、设定解析的优先级、网关(设置双网卡为路由器,单网卡添加网关)、lo回环接口(转载)

一、设定DNS ##地址解析
#系统操作者对字符敏感
#系统网络通信需要通过ip地址这个数字
#当操作者输入网址www.baidu.com,这个网址不是一个可以通信的IP地址
#于是必须要在系统中把www.baidu.com变成百度服务器的IP地址
#这样的过程叫做地址解析
DNS服务器:domain name server == 域名解析服务 ##解析就是把域名变成IP
vim /etc/resolv.conf ##dns的指向文件 不需要重新启动网络立即生效
nameserver 114.114.114.114 ##当需要某个域名的IP地址时去问 114.114.114.114
nameserver 8.8.8.8 ##问谷歌的ip

vim /etc/sysconfig/network-scripts/ifcfg-xxxx
DNS1=114.114.114.114 ##需要重启网络,当网络重新启动
​ ##此参数会修改/etc/resolv.conf

vim /etc/hosts ##本地解析文件,此文件中直接提供地址解析

注意: 当网络工作模式为dhcp时 系统会自动获得ip 网关 dns

         那么/etc/resolv.conf会被获得到的信息修改

         如果不需要获得dns信息  ,在网卡配置文件中加入 PREEDNS=no

二、设定解析的优先级
系统默认:
/etc/hosts > /etc/resolv.conf
vim /etc/nsswitch.conf 39 hosts: files dns ##/etc/hosts优先
vim /etc/nsswitch.conf 39 hosts: dns files ##/etc/resolv.conf dns指向优先

三、网关
1.环境设定
实验环境:
在Horizon_carry单网卡主机中:
ens160:172.25.254.10 #不可ping通
在carry双网卡主机中
先生成桥接网卡ens224
ens160:172.25.254.20
ens224:192.168.0.20 #可以ping 通

2.把carry双网卡主机变成路由器
<1>开启地址伪装功能
[root@carry ~]# systemctl start firewalld #打开火墙
[root@carry ~]# firewall-cmd --add-masquerade #临时地址伪装功能
success
[root@carry ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens160 ens224
sources:
services: cockpit dhcpv6-client ssh
ports:
protocols:
masquerade: yes <<<地址伪装功能开启,真实主机变成路由器
forward-ports:
source-ports:
icmp-blocks:
rich rules:

<2>过滤确认地址伪装功能开启
[root@carry ~]# sysctl -a | grep ip_forward #过滤一遍
net.ipv4.ip_forward = 1 #地址伪装功能开启
net.ipv4.ip_forward_use_pmtu = 0

<3> 内核路由开启地址伪装功能
[root@carry ~]# vim /etc/sysctl.conf

sysctl settings are defined through files in

/usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.

Vendors settings live in /usr/lib/sysctl.d/.

To override a whole file, create a new file with the same in

/etc/sysctl.d/ and put new settings there. To override

only specific settings, add a file with a lexically later

name in /etc/sysctl.d/ and put new settings there.

For more information, see sysctl.conf(5) and sysctl.d(5).

net.ipv4.ip_forward=1

[root@carry ~]# sysctl -p
net.ipv4.ip_forward = 1

3.设定单网卡主机Horizon_carry的网关
<1>确认实验环境
[root@Horizion_carry Desktop]# route -n ##查看网关
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
172.25.254.0 0.0.0.0 255.255.255.0 U 101 0 0 ens160
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
[root@Horizion_carry Desktop]# ping 192.168.0.20 ##pingWindows不通
connect: Network is unreachable

<2>临时添加网关
[root@Horizion_carry Desktop]# ip route add default via 172.25.254.20 dev ens160
##临时添加网关
[root@Horizion_carry Desktop]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 172.25.254.20 0.0.0.0 UG 0 0 0 ens160
172.25.254.0 0.0.0.0 255.255.255.0 U 101 0 0 ens160
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
重启服务后网关就没有了

<3>永久设定网关
vim /etc/sysconfig/network ##全局网关,针对所有没有设定网关的网卡生效
GATEWAY=172.25.254.20

四、lo回环接口
回环接口------人的神经----127.0.0.1-----localhost

原文链接:https://blog.csdn.net/Horizon_carry/article/details/106007510

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值