发现一个问题,当真实主机开启火墙时,虚拟机中的dnf会无法通过网络软件仓库访问dnf源,每次都需要在真实主机中关闭火墙。关闭火墙后,地址伪装也关闭,虚拟机就无法上网
这是因为火墙中没有设置允许httpd服务。在真机执行
[root@westos Desktop]# firewall-cmd --permanent --add-service=http
success
[root@westos Desktop]# firewall-cmd --reload
success
一.环境配置
selinux就是内核级别的火墙
安装vsftpd
[root@server8 ~]# rm -rf /etc/vsftpd/
[root@server8 ~]# dnf install vsftpd
[root@server8 ~]# chmod 775 /var/ftp/pub/
[root@server8 ~]# chgrp ftp /var/ftp/pub/
[root@server8 ~]# ls -ld /var/ftp/pub/
drwxrwxr-x. 2 root ftp 6 Feb 17 2020 /var/ftp/pub/
[root@server8 ~]# vim /etc/vsftpd/vsftpd.conf
匿名用户的登录和上传都允许
[root@server8 ~]# systemctl restart vsftpd.service
二.selinux对系统功能的影响
[root@server8 ~]# vim /etc/selinux/config
首先将selinux状态修改为disabled,重启系统才会生效
getenforce
可以查看selinux的状态
[root@server8 ~]# getenforce
Disabled
[root@server8 mnt]# touch test
[root@server8 mnt]# mv test /var/ftp/
在服务端建立一个测试文件,移动到ftp的发布目录中
[root@client8 ~]# lftp 172.25.254.244
lftp 172.25.254.244:~> ls
drwxrwxr-x 2 0 50 6 Feb 17 2020 pub
-rw-r--r-- 1 0 0 0 Apr 26 08:18 test
在客户端可以查看到
当开启selinux的时候,匿名用户不能上传任何文件
这是selinux的安全上下文不匹配,安全上下文类似于一个认证
sebool SE波尔值
[root@server8 ~]# getsebool -a |grep ftp
ftpd_anon_write --> off
ftpd_connect_all_unreserved --> off
ftpd_connect_db --> off
ftpd_full_access --> off
ftpd_use_cifs --> off
ftpd_use_fusefs --> off
ftpd_use_nfs --> off
ftpd_use_passive_mode --> off
httpd_can_connect_ftp --> off
httpd_enable_ftp_server --> off
tftp_anon_write --> off
tftp_home_dir --> off
查看ftp的波尔值
[root@server8 ~]# setsebool -P ftpd_anon_write 1
设置ftp的安全上下文为允许,on也可以
三.SElinux的基本控制
```powershell
vim /etc/selinux/config
修改selinux的状态为此文件
disabled关闭
enforcing强制模式
permissive警告模式
修改完毕以后都需要重启
四.selinux的安全上下文的管理
ls -Z //查看文件的安全上下文
ls -Zd //查看目录的安全上下文
ps axZ //查看进程的安全上下文
临时修改安全上下文
[root@server8 mnt]# mkdir testdir1
[root@server8 mnt]# touch testdir1/haha
[root@server8 mnt]# ls -Zd testdir1/
unconfined_u:object_r:mnt_t:s0 testdir1/
[root@server8 mnt]# ls -Zd testdir1/haha
unconfined_u:object_r:mnt_t:s0 testdir1/haha
[root@server8 mnt]# chcon -t public_content_t testdir1/
[root@server8 mnt]# ls -Zd testdir1/
unconfined_u:object_r:public_content_t:s0 testdir1/
chcon
[root@server8 mnt]# chcon -Rt public_content_t testdir1/
[root@server8 mnt]# ls -Z testdir1/haha
unconfined_u:object_r:public_content_t:s0 testdir1/haha
加R递归改变安全上下文
[root@server8 mnt]# touch /.autorelabel
创建此文件相当于把selinux重启了一次
[root@server8 ~]# semanage fcontext -l |grep /var/ftp
/var/ftp(/.*)? all files system_u:object_r:public_content_t:s0
/var/ftp/bin(/.*)? all files system_u:object_r:bin_t:s0
/var/ftp/etc(/.*)? all files system_u:object_r:etc_t:s0
/var/ftp/lib(/.*)? all files system_u:object_r:lib_t:s0
/var/ftp/lib/ld[^/]*\.so(\.[^/]*)* regular file system_u:object_r:ld_so_t:s0
查看安全上下文列表
[root@server8 mnt]# semanage fcontext -a -t public_content_t '/mnt/testdir1(/.*)?'
把测试的目录添加到安全上下文列表里,现在存在的和将来生成的安全上下文都会改变
[root@server8 testdir1]# semanage fcontext -l |grep /mnt/testdir1
/mnt/testdir1(/.*) all files system_u:object_r:public_content_t:s0
/mnt/testdir1(/.*)? all files system_u:object_r:public_content_t:s0
此时查看就能查到
[root@server8 testdir1]# restorecon -RvvF /mnt/testdir1
Relabeled /mnt/testdir1 from unconfined_u:object_r:mnt_t:s0 to system_u:object_r:public_content_t:s0
Relabeled /mnt/testdir1/haha from unconfined_u:object_r:public_content_t:s0 to system_u:object_r:public_content_t:s0
Relabeled /mnt/testdir1/hehe from unconfined_u:object_r:mnt_t:s0 to system_u:object_r:public_content_t:s0
vv详细,F刷新,R递归
永久固定安全上下文
[root@server8 testdir1]# touch xixi
[root@server8 testdir1]# ls -Z xixi
unconfined_u:object_r:public_content_t:s0 xixi
新创建一个文件,查看它的安全上下文发现已经改变
五.selinux波尔值的管理
[root@server8 testdir1]# getsebool -a |grep ftp
查看波尔值
[root@server8 testdir1]# setsebool -P ftpd_anon_write on
更改波尔值,-p表示永久
六.selinux端口管理
selinux会限制端口的改变
[root@server8 ~]# setenforce 0
把selinux更改成警告模式
[root@server8 ~]# getenforce
Permissive
下一步改变sshd的端口
[root@server8 ~]# vim /etc/ssh/sshd_config
[root@server8 ~]# netstat -antlupe|grep sshd
tcp 0 0 0.0.0.0:1111 0.0.0.0:* LISTEN 0 46241 3049/sshd
tcp 0 0 172.25.254.244:22 172.25.254.44:47424 ESTABLISHED 0 41472 2540/sshd: root [pr
tcp6 0 0 :::1111 :::* LISTEN 0 46243 3049/sshd
[root@server8 ~]# setenforce 1
再把selinux的状态改成强制
[root@server8 ~]# systemctl restart sshd
Job for sshd.service failed because the control process exited with error code.
See "systemctl status sshd.service" and "journalctl -xe" for details.
此时sshd的重启会出现问题
[root@server8 ~]# semanage port -l |grep ssh
ssh_port_t tcp 22
[root@server8 ~]# semanage port -a -t ssh_port_t 1111 -p tcp
[root@server8 ~]# semanage port -l |grep ssh
ssh_port_t tcp 1111, 22
[root@server8 ~]# systemctl restart sshd
[root@server8 ~]# semanage port -d -t ssh_port_t 1111 -p tcp
[root@server8 ~]# semanage port -l |grep ssh
ssh_port_t tcp 22
[root@server8 ~]# vim /etc/ssh/sshd_config
[root@server8 ~]# systemctl restart sshd
[root@server8 ~]#
此时要给selinux增加一个端口1111 -a添加
-d删除
七.selinux 的排错
[root@server8 ~]# > /var/log/messages
[root@server8 ~]# > /var/log/audit/audit.log
清空系统日志和selinux的警告信息日志
[root@server8 ~]# less /var/log/messages
此日志会产生selinux报错的解决方案