赞
踩
路由器把IP数据包的目的IP地址路由表对比,找到匹配的路由表项之后,根据这个路由表项的指示将数据包转发给下一个网络设备或PC
[root@Centos-A1 ~]# route -n #查看路由信息
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
192.168.196.0 0.0.0.0 255.255.255.0 U 425 0 0 br0
[root@Centos-A1 ~]# ip route #查看路由的对应关系
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1
192.168.196.0/24 dev br0 proto kernel scope link src 192.168.196.200 metric 425
[root@Centos-A1 ~]# ip route #查看路由表
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1
[root@Centos-A1 ~]# ip route add 172.25.254.110 via 192.168.196.2 #添加路由,ip route add ip地址[/掩码] via 网关
[root@Centos-A1 ~]# ip route del 172.25.254.110 #删除路由
路由转发使用firewalld(防火墙)来实现管理与配置
[root@Centos-A1 ~]# systemctl status firewalld.service #查看防火墙状态
[root@Centos-A1 ~]# firewall-cmd --list-all #查看防护墙信息
public (active) #[默认使用的区域]
target: default #对于区域所采用的默认策略(Accept(允许)、reject(拒绝)、 drop(完全拒绝))
icmp-block-inversion: no :#ICMP黑白名单开关
interfaces: br0 ens33 ##关联的网卡
sources: #来源,可以是ip,也可以是mac地址
services: dhcpv6-client ssh #允许的服务
ports: #允许的端口
protocols: #允许通过的协议
masquerade: no #ip地址伪装,将接受到的源地址转发请求网卡的地址(路由器)
forward-ports: #端口转发
source-ports: #允许的来源端口
icmp-blocks: #可以添加的CMP类型
rich rules: #富规则集,优先级在所有防火墙中最高
防火墙操作
[root@Centos-A1 ~]# systemctl stop firewalld.service #关闭防火墙
[root@Centos-A1 ~]# systemctl restart firewalld.service #重启防火墙
[root@Centos-A1 ~]# systemctl start firewalld.service #开启防火墙
[root@Centos-A1 ~]# firewall-cmd --permanent --add-forward-port=port=888:proto=tcp:toport=80 #将访问本机888端口的流量转到80端口
success
#此处安装httpd服务器进行测试
root@Centos-A1 ~]# yum install httpd -y #安装httpd服务
[root@Centos-A1 ~]# firewall-cmd --add-service=http #放行http服务
[root@Centos-A1 ~]# firewall-cmd --add-port=888/tcp #方向转换端口888
[root@Centos-A1 ~]# echo hello centos7 > /var/www/html/index.html #给http网站写入内容
[root@Centos-A1 ~]# systemctl restart httpd #重新启动http服务
[root@Centos-A1 ~]# semanage port -l| grep http #查看http服务使用的selinux标签 //如果能够访问则不需要更改
[root@Centos-A1 ~]# semanage port -a -t http_port_t -p tcp 888 #给888端口打上http服务标签
使用物理机访问 http://192.168.196.137:888,即http://虚拟机ip地址:转换端口,能够访问,说明端口转发成功
实验中为了简单,可以关闭selinux
[root@Centos-A1 ~]# setenforce 0 #临时关闭selinux
[root@Centos-A1 ~]# vim /etc/selinux/config #永久关闭
SELINUX=enforcing
将enforcing修改为disabled
首先,Centos-A1也需要开启防护墙的地址伪装
[root@Centos-A1 ~]# firewall-cmd --add-masquerade --permanent #开启地址伪装,--permanent表示永久生效,不添加重启后配置失效
success
[root@Centos-A1 ~]# firewall-cmd --reload #重新载入防火墙配置
success
如果是接着上面端口转发后做的,先删除之前的端口转发,避免冲突
[root@Centos-A1 ~]# firewall-cmd --remove-forward-port=port=888:proto=tcp:toport=80
success
打开另一台服务器Cenos -A2,安装httpd服务
[root@centos-a2 ~]# yum install httpd -y
[root@centos-a2 ~]# firewall-cmd --permanent --add-service=http
success
[root@centos-a2 ~]# echo hello Centos-A2 > /var/www/html/index.html
[root@centos-a2 ~]# systemctl restart httpd
[root@centos-a2 ~]# firewall-cmd --permanent --add-service=http #防火墙放行httpd服务
[root@centos-a2 ~]# firewall-cmd --reload #重新载入服务
success
[root@centos-a2 ~]# firewall-cmd --list-all
services: ssh dhcpv6-client http #已经放行http服务
在Centos-A1上配置包转发,将访问Centos-A1的数据转到Centos-A2上
[root@Centos-A1 ~]# sysctl -w net.ipv4.ip_forward=1 #开启地包转发功能,1表示开启,0表示未开启
net.ipv4.ip_forward = 1
[root@Centos-A1 ~]# firewall-cmd --permanent --add-forward-port=port=80:proto=tcp:toport=80:toaddr=192.168.196.131 #将访问本机80端口的流量转发到192.168.196.131的80端口
本机访问测试,192.168.196.137为Centos-A1的ip地址
富规则实现
首先也需要开启地址伪装与地址转发
[root@Centos-A1 ~]# firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.196.0/24" forward-port port=80 protocol=tcp to-port=80 to-addr=192.168.196.131" #配置富规则,将192.168.192.0/24网段的主机访问本机80端口的流量转到192.168.196.131的80端口的流量
访问测试,即可看到转发成功
[root@centos-a2 ~]# man firewall-cmd #查看防火墙的使用帮助
/-add-forward #即可查看到配置端口转发的示例
[root@centos-a2 ~]# man firewalld.richlanguage #查看防火墙富规则配置
/Forward-Port #搜索即可查看防火墙富贵则配置地址转发的案例
关于yum源的配置
首先连接镜像
[root@centos-a2 yum.repos.d]# mount /dev/cdrom /mnt/ #挂载镜像
[root@centos-a2 yum.repos.d]# df -Th| grep mnt #查看挂载
/dev/sr0 iso9660 4.5G 4.5G 0 100% /mnt
[root@centos-a2 yum.repos.d]# cp -p CentOS-Base.repo CentOS-Base.repo.BAK #修改配置前备份
[root@centos-a2 yum.repos.d]# vim CentOS-Base.repo #修改镜像源为本地
[base]
name=centos_base
baseurl=file:///mnt
gpgcheck=0 #不做校验,为1表示做校验,此时需要添加一行:gpgkey=密钥路径
[root@centos-a2 yum.repos.d]# yum clean all #清除缓存
[root@centos-a2 yum.repos.d]# yum repolis #重新生成缓
对于本地镜像源,存在epel扩展源的情况下,repolist缓存1万多为正常,且其他源都有缓存
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo #下载镜像源到服务器
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo #两种方式选择一个即可,都是下载镜像源到本地
[root@centos-a2 yum.repos.d]# yum clean all #清除缓存
[root@centos-a2 yum.repos.d]# yum repolis #重新生成缓存
在有epel扩展源的情况下,repolist的值大于2万为正常,其中每个源都有缓存
centos 8需要配置两个镜像源
[root@centos-a2 yum.repos.d]# cp -p CentOS-Base.repo CentOS-Base.repo.BAK #修改配置前备份
[root@centos-a2 yum.repos.d]# vim CentOS-Base.repo #修改镜像源为本地
[BaseOS]
name=BaseOS
baseurl=本地路径或者网络镜像仓库/BaseOS
gpgcheck=0 #不做校验,为1表示做校验,此时需要添加一行:gpgkey=密钥路径
enabled=1 # 开启仓库
[AppStream]
name=AppStream
baseurl=本地路径或者网络镜像仓库/AppStream
gpgcheck=0 #不做校验,为1表示做校验,此时需要添加一行:gpgkey=密钥路径
enabled=1 # 开启仓库
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。