您的位置:首页 > 运维架构 > Linux

linux工程师技术-01 -SElinux、配置高级连接、防火墙管理策略

2017-11-01 23:50 501 查看
Security-Enhanced Linux(SElinux)

– 美国NSA国家安全局主导开发,一套增强Linux系统安全的强制访问控制体系
目的:强制访问控制体系

– 集成到Linux内核(2.6及以上)中运行
– RHEL7基于SELinux体系针对用户、进程、目录和文件提供了预设的保护策略,以及管理工具

SELinux的运行模式
– enforcing(强制)、permissive(宽松)
– disabled(彻底禁用)

切换运行模式
-临时切换(当前切换):setenforce 1/0 #0表示降级,1表示升级
-固定配置(下次重启才生效):vim /etc/selinux/config

[root@server0 ~]# getenforce #查看当前SELinux状态
Enforcing
[root@server0 ~]# setenforce 0 #设置当前SELinux状态
[root@server0 ~]# getenforce
Permissive

enforcing、permissive与disabled之间的切换必须到vim固定配置重启才能生效

固定配置:
[root@server0 ~]# vim /etc/selinux/config
.. ..
SELINUX=permissive

[root@server0 ~]#reboot #重启系统切换模式

补充:vim 命令模式
C(大写):删除光标之后到行尾,并且进入插入模式
-------------------------------------------------------------------------------------------
配置高级连接
配置聚合连接(网卡绑定)
和HSRP相似
HSRP:备份网关设备

路由器1 路由器2
192.168.1.254 192.168.1.253
活跃 备份

虚拟路由器
192.168.1.200

聚合连接:备份网卡设备

eth1 eth2
192.168.1.1/24 192.168.1.2/24
team
192.168.1.10

team,聚合连接(也称为链路聚合)
– 由多块网卡(team-slave)一起组建而成的虚拟网卡,即“组队”
– 作用1:轮询式(roundrobin)的流量负载均衡
– 作用2:热备份(activebackup)连接冗余

热备份配置: {"runner":{"name":"activebackup"}}
热备份配置字符复杂可以用:man帮助辅助记忆

输入man teamd.conf进入帮助界面
输入 /example #全文查找example
再按n #按n 跳转下一个匹配
找到并复制 {"runner":{"name":"activebackup"}}

[root@server0 ~]# man teamd.conf
/example #全文查找example
#按n 跳转下一个匹配

一、添加team团队设备
# nmcli connection add type team
con-name team0 ifname team0
config '{"runner": {"name": "activebackup"}}'

# cat /etc/sysconfig/network-scripts/ifcfg-team0
# ifconfig team0

二、添加成员
# nmcli connection add type team-slave
ifname eth1 master team0

# nmcli connection add type team-slave
ifname eth2 master team0

三、配置team0的IP地址
# nmcli connection modify team0
ipv4.method manual
ipv4.addresses 192.168.1.1/24
connection.autoconnect yes

四、激活team0
# nmcli connection up team-slave-eth1 #激活从设备eth1
# nmcli connection up team-slave-eth2 #激活从设备eth2
# nmcli connection up team0 #激活主设备team0

五、验证
# teamdctl team0 state #专用于查看team信息
setup:
runner: activebackup
ports:
eth1
link watches:
link summary: up
instance[link_watch_0]:
name: ethtool
link: up #状态已激活
eth2
link watches:
link summary: up
instance[link_watch_0]:
name: ethtool
link: up #状态已激活
runner:
active port: eth1 #指定活跃网卡为eth1

注意:前面有输入错误的地方不要重新输一遍正确的,不会覆盖,最好删除网卡组成员
删除命令:
# nmcli connection delete team-slave-eth1
# nmcli connection delete team-slave-eth2
# nmcli connection delete team0

在客户端desktop虚拟机上按照server0重新配置一遍,配置IP地址为192.168.1.2/24
都配置完后可以Ping验证
若将其中一个网卡eth1或eth2端口关闭,命令:ifconfig eth1/eth2 down也会ping通,因为有备份,两个都关闭则ping失败

-------------------------------------------------------------------------------------------
配置IPv6地址
IPv6 地址表示

– 128个二进制位,冒号分隔的十六进制数
– 每段内连续的前置 0 可省略、连续的多个 : 可简化为 ::

# nmcli connection modify 'System eth0'
ipv6.method manual
ipv6.addresses 2003:ac18::305/64
connection.autoconnect yes

# nmcli connection up 'System eth0'

# ifconfig eth0

# ping6 2003:ac18::305

-------------------------------------------------------------------------------------------
alias别名设置

查看已设置的别名
– alias [别名名称]
定义新的别名
– alias 别名名称= '实际执行的命令行'
取消已设置的别名
– unalias [别名名称]

用户个性化配置文件
影响指定用户的 bash 解释环境
– ~/.bashrc,每次开启 bash 终端时生效

全局环境配置
影响所有用户的 bash 解释环境
– /etc/bashrc,每次开启 bash 终端时生效

[root@server0 ~]# vim /root/.bashrc #影响root文件
.. ..
alias hello='echo hello'

[root@server0 ~]# vim /home/student/.bashrc #影响student文件
.. ..
alias hi='echo hi'

[root@server0 ~]# vim /etc/bashrc #全局配置文件
.. ..
alias haha='echo xixi'

退出远程登陆,重新远程server0验证
[root@server0 ~]# hello #成功
[root@server0 ~]# hi #失败
[root@server0 ~]# haha #成功
[root@server0 ~]# su - student
[student@server0 ~]$ hello #失败
[student@server0 ~]$ hi #成功
[student@server0 ~]$ haha #成功
[student@server0 ~]$ exit
-------------------------------------------------------------------------------------------
防火墙策略管理(firewall)

一、搭建基本Web服务
利用服务端的软件(httpd)为客户端提供Web服务

服务端: httpd(软件)由Apache组织开发
1.server0上安装httpd软件
2.server0启动httpd服务,设置开机自起
默认情况下:Apache没有提供任何页面
虚拟机登陆firefox页面必须将真机的所有firefox页面程序关闭

默认Apache网页文件存放路径:/var/www/html
默认Apache网页文件名称:index.html

[root@server0 ~]# yum -y install httpd #安装软件
[root@server0 ~]# systemctl restart httpd #设置开机自启
[root@server0 ~]# systemctl enable httpd

[root@server0 ~]# vim /var/www/html/index.html #进入编辑页面

<marquee><font color=green><h1>My First Web
#字体滚动,字体颜色,字体大小后面输入页面显示内容

[root@server0 ~]# firefox 172.25.0.11
前面没写服务就默认http服务

二、FTP服务的搭建 ftp:文件传输协议
服务端: vsftpd(软件)
1.server0上安装 vsftpd软件
2.server0启动 vsftpd服务,设置开机自起
默认共享的位置:/var/ftp

可以创建文档文件:
e.g:
[root@server0 ~]# touch /var/ftp/1.txt
[root@server0 ~]# touch /var/ftp/2.txt

测试
[root@server0 ~]# firefox ftp://172.25.0.11
-------------------------------------------------------------------------------------------
防火墙策略管理(firewall)

作用:隔离外网和内网
阻止外网(WAN)入站,允许内网(LAN)出站

系统服务:firewalld
管理工具:firewall-cmd(命令)、firewall-config(图形)

查看防火墙服务状态
[root@server0 ~]# systemctl status firewalld.service

根据所在的网络场所区分,预设保护规则集
– public(默认的区域):仅允许访问本机的sshd等少数几个服务
– trusted:允许任何访问
– block:拒绝任何来访请求(客户端请求会回应并拒绝)
– drop:丢弃任何来访的数据包(直接拒绝没有任何回应)

防火墙判断的规则:匹配及停止
1.首先看请求(客户端)当中的源IP地址,所有区域中是否有对于该IP地址的策略,如果有则该请求进入该区域
2.进入默认区域

虚拟机desktop0:
# firefox http://172.25.0.11 #访问失败,因为没有添加http服务
# firefox ftp://172.25.0.11 #访问失败,因为没有添加ftp服务
虚拟机server0:
# firewall-cmd --get-default-zone #查看默认区域为public
# firewall-cmd --zone=public --list-all #查看区域规则信息
public (default, active)
interfaces: eth0 eth1 eth2 team0
sources:
services: dhcpv6-client ssh #支持的服务
ports:
masquerade: no
forward-ports:
icmp-blocks:
rich rules:
# firewall-cmd --zone=public --add-service=http #添加服务
# firewall-cmd --zone=public --list-all #查看区域规则信息
public (default, active)
interfaces: eth0 eth1 eth2 team0
sources:
services: dhcpv6-client http ssh #已添加http服务
ports:
masquerade: no
forward-ports:
icmp-blocks:
rich rules:
虚拟机desktop0:
# firefox http://172.25.0.11 #访问成功
# firefox ftp://172.25.0.11 #访问失败
虚拟机server0:
# firewall-cmd --zone=public --add-service=ftp
# firewall-cmd --zone=public --list-all
public (default, active)
interfaces: eth0 eth1 eth2 team0
sources:
services: dhcpv6-client ftp http ssh
ports:
masquerade: no
forward-ports:
icmp-blocks:
rich rules:
虚拟机desktop0:
# firefox ftp://172.25.0.11 #访问成功
-------------------------------------------------------------------------------------------
--permanent选项:实现永久设置

虚拟机server0:

# firewall-cmd --reload #重新加载防火墙,关闭之前添加的服务
# firewall-cmd --zone=public --list-all #重新后之前设置的服务会消失

# firewall-cmd --permanent --zone=public --add-service=ftp
# firewall-cmd --permanent --zone=public --add-service=http

# firewall-cmd --reload #重新加载防火墙
# firewall-cmd --zone=public --list-all

-------------------------------------------------------------------------------------------
修改默认的区域,不需要加上--permanent

虚拟机desktop0:

# ping 172.25.0.11 #可以通信
虚拟机server0:
# firewall-cmd --set-default-zone=block #修改默认区域为block
# firewall-cmd --get-default-zone #查看默认区域

虚拟机desktop0:
# ping 172.25.0.11 #不可以通信

虚拟机server0:
# firewall-cmd --set-default-zone=drop #修改默认区域为drop
# firewall-cmd --get-default-zone
虚拟机desktop0:
# ping 172.25.0.11 #通信无反馈

-------------------------------------------------------------------------------------------
虚拟机server0:
# firewall-cmd --permanent --zone=public --add-source=172.25.0.10 #添加客户端desktop源IP

# firewall-cmd --zone=public --list-all
# firewall-cmd --reload
# firewall-cmd --zone=public --list-all

虚拟机desktop0:
# firefox http://172.25.0.11
-------------------------------------------------------------------------------------------
实现本机的端口映射
本地应用的端口重定向(端口1 --> 端口2)
– 从客户机访问 端口1 的请求,自动映射到本机 端口2
– 比如,访问以下两个地址可以看到相同的页面:

虚拟机desktop0:
# firefox http://172.25.0.11:5423-------》172.25.0.11:80
虚拟机server0:
# firewall-cmd --permanent --zone=public
--add-forward-port=port=5423:proto=tcp:toport=80

# firewall-cmd --reload

# firewall-cmd --zone=public --list-all

虚拟机desktop0:
# firefox http://172.25.0.11:5423
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息