linux中的网络配置
查看网卡配置文件中参数的帮助:三要素:IP 网关 dns一. ip二. 网关路由器1.开启内核路由功能2.打开地址伪装功能解决问题:真机和虚拟机上网:将真机作为一个路由器三.dns本地解析默认先解析/etc/hosts文件dns 的指向文件解析修改解析优先级查看网卡配置文件中参数的帮助:
less /usr/share/doc/initscripts/sysconfig.txt
三要素:IP 网关 dns
一. ip
设置ip:图形 命令行 (见上篇博客)/ninimino/article/details/107645973二. 网关
网关:配置文件(如上图)图形方式(见上篇博客1vim /etc/sysconfig/network
##全局网关,针对所有没有设定网关的网卡生效
GATEWAY=172.25.254.20
2vim /etc/sysconfig/network-scripts/ifcfg-ens160
GATEWAY0=172.25.254.20
#当网卡中设定的IP有多个时,指定对于那个IP生效
GATEWAY=172.25.254.20
#当网卡中设定的IP只有一个时
3临时修改
ip route add default via 172.25.254.20
route add default gw 172.25.254.20
路由器
双网卡 单网卡SNAT单网卡172 ---->1721.1. -----> 1.1.1.204
route 双网卡主机
1.开启内核路由功能
2.打开地址伪装功能
[root@localhost network-scripts]# cat ifcfg-westos
DEVICE=enp1s0
NAME=enp1s0
ONBOOT=yes
BOOTPROTO=none
IPADDR=172.25.254.104
NETMASK=255.255.255.0
[root@localhost network-scripts]# cat ifcfg-enp7s0
DEVICE=enp7s0
NAME=enp7s0
ONBOOT=yes
BOOTPROTO=none
IPADDR=1.1.1.104
PREFIX=24
[root@localhost network-scripts]# sysctl -a | grep ip_forward
##查看内核路由功能
net.ipv4.ip_forward = 1 ####=1
net.ipv4.ip_forward_update_priority = 1
net.ipv4.ip_forward_use_pmtu = 0
###不等于1时,vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
sysctl -P 使文件中的信息生效
打开地址伪装功能
systemctl enable --now firewalld
firewall-cmd --permanent --add-masquerade
##打开地址伪装功能
firewall-cmd --reload
1.1.1.204 1.1.1.104 172.25.254.103 172.25.254.3
单网卡中主机的设置
(当有了路由器的同网段ip,就可以通过路由器的其他网段的ip连接路由器所在主机的不同网段的主机)
[root@localhost network-scripts]# cat ifcfg-westos
DEVICE=enp1s0
ONBOOT=yes
BOOTPROTO=none
NETMASK=255.255.255.0
IPADDR=1.1.1.204
NAME=westos
设置全局网关
vim /etc/sysconfig/network
GATEWAY=1.1.1.104
[root@localhost network-scripts]# ip route add default via 1.1.1.104##添加网关--->双网卡主机中同网段的ip
[root@localhost network-scripts]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 1.1.1.104 0.0.0.0 UG 0 0 0 enp1s0
1.1.1.0 0.0.0.0 255.255.255.0 U 100 0 0 enp1s0
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
[root@localhost network-scripts]# ping 1.1.1.104
PING 1.1.1.104 (1.1.1.104) 56(84) bytes of data.
64 bytes from 1.1.1.104: icmp_seq=1 ttl=64 time=0.389 ms
64 bytes from 1.1.1.104: icmp_seq=2 ttl=64 time=0.321 ms
^C
[root@localhost network-scripts]# ping 172.25.254.104
###可以ping不同网段的
PING 172.25.254.104 (172.25.254.104) 56(84) bytes of data.
64 bytes from 172.25.254.104: icmp_seq=1 ttl=64 time=0.438 ms
64 bytes from 172.25.254.104: icmp_seq=2 ttl=64 time=0.399 ms
^C
— 172.25.254.104 ping statistics —
2 packets transmitted, 2 received, 0% packet loss, time 30ms
在1.1.1.204主机连接一个172网段的主机
[root@localhost network-scripts]# ifconfig
enp1s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 1.1.1.204 netmask 255.255.255.0 broadcast 1.1.1.255
[root@localhost network-scripts]# ssh root@172.25.254.3
The authenticity of host ‘172.25.254.3 (172.25.254.3)’ can’t be established.
ECDSA key fingerprint is SHA256:qpgSfCxNbbQYFMESw7CTuIoNg+88unEhtRKvSzigQV0.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added ‘172.25.254.3’ (ECDSA) to the list of known hosts.
root@172.25.254.3’s password:
[root@westos_student3 ~]#w -i
###可以看到其实是由路由器的172网段的ip访问的
14:38:04 up 6:05, 2 users, load average: 0.91, 0.86, 0.82
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
westos :0 :0 08:32 ?xdm? 1:29m 0.03s /usr/libexec/gd
root pts/2172.25.254.104
14:38 4.00s 0.04s 0.00s w -i
[root@westos_student3 ~]# exit
logout
Connection to 172.25.254.3 closed.
解决问题:真机和虚拟机上网:将真机作为一个路由器
真机中
虚拟机中
三.dns
dns 地址解析#系统操作者对字符敏感,系统网络通信需要通过ip地址这个数字
,当操作着输入网址,这个网址不是一个可以通信的IP地址,于是必须要在系统中把变成百度服务器的IP地址.这样的过程叫做地址解析
domain name server == 域名解析服务 ##解析就是把域名变成IP
1vim /etc/sysconfig/network-scripts/ifcfg-xxxx
DNS1=114.114.114.114
需要重启网络,当网络重新启动
此参数会修改/etc/resolv.conf
本地解析
vim /etc/hosts
本地解析文件,此文件中直接提供地址解析
ip 域名220.181.111.188
默认先解析/etc/hosts文件
dns 的指向文件解析
vim /etc/resolv.conf
dns的指向文件 不需要重新启动网络立即生效
nameserver 114.114.114.114
当需要某个域名的IP地址时去问114.114.114.114
修改解析优先级
vim /etc/nsswitch.conf
#示例
示例:编辑配置文件 /etc/sysconfig/network-scripts/ifcfg-ens160 如下图所示设置
[root@localhost network-scripts]# vim ifcfg-ens160 需重新启动网络接口使被修改的配置文件生效。[root@localhost network-scripts]# nmcli connection down ens160 关掉网络接口Connection 'ens160' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/1)[root@localhost network-scripts]# nmcli connection up ens160 启用网络接口Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/3)[root@localhost network-scripts]# ip addr show ens160 查看ip是否设置成功2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000link/ether 00:0c:29:24:c5:b9 brd ff:ff:ff:ff:ff:ffinet 192.168.3.22/24 brd 192.168.3.255 scope global noprefixroute ens160valid_lft forever preferred_lft foreverinet6 fe80::20c:29ff:fe24:c5b9/64 scope link valid_lft forever preferred_lft forever[root@localhost network-scripts]# route -n 查看网关设置是否成功Kernel IP routing tableDestinationGateway Genmask Flags Metric Ref Use Iface0.0.0.0 192.168.3.10.0.0.0 UG 100 0 0 ens160192.168.3.00.0.0.0 255.255.255.0 U100 0 0 ens160192.168.122.0 0.0.0.0 255.255.255.0 U00 0 virbr0[root@localhost network-scripts]# cat /etc/resolv.conf 查看设置dns是否成功# Generated by NetworkManagernameserver 114.114.114.114