Linux之SELINUX

  • 身份识别(Identify)
root        --- 表示root的账号身份
system_u    --- 表示系统程序方面的识别,通常就是程序
user_u      --- 代表一般用户账号相关的身份
  • 角色(Role)
object_r    --- 代表的是文件或目录等文件资源,这是最常见的
system_r    --- 代表的就是程序,不过一般用户也会被指定为system_r
  • 类型(Type)
Type        --- 在文件资源(object)中称为类型(Type)
Domain      --- 在主体程序(Subject)中则称为域(Domain)
  • 当你在单纯地复制时,SELinux的Type字段会继承自目标目录,如果是移动的话就会连同SELinux的类型也会被移动过去
  • 常用命令以及一些参数:
chcon       --- 改变SELinux的状态
    -R              --- 连同该目录下的子目录也同时修改
    -t              --- 后面接安全性环境的类型字段
    -u              --- 后面接身份识别
    -r              --- 后面接角色
    --reference     --- 拿某个文件当范例来修改后续接的文件类型
restorecon [-Rv] file|dir   --- 恢复成原有的SELinux Type类型
    -R          --- 连同子目录一起修改
    -v          --- 将过程显示到屏幕上 
semanage fcontext -l -{a|d|m} [-frst] file_spec --- 如果没有semanage这个命令,需要先用"yum install policycoreutils-python"
    -l      --- 表示查询的意思
    -a      --- 增加一个默认类型
    -m      --- 修改一个默认配置
    -d      --- 删除一个默认的配置
seinfo [-Atrub]         --- 查看策略提供的规则,如果没有这个命令,需要使用命令"yum  install setools-console"安装
    -A      --- 列出SELinux的状态、规则布尔值、身份识别、角色。类别等所有信息
    -t      --- 列出SELinux的所有类别(type)的种类
    -r      --- 列出SELinux的所有角色(role)种类
    -u      --- 列出SELinux的所有身份识别(user)种类
    -b      --- 列出所有规则的种类(布尔值)
    sesearch [--all] [-s 主体类别] [-t 目标类型] [-b 布尔值]   --- 查询详细的规则
    --all   --- 列出该类别或布尔值的所有相关信息
getsebool -a        --- 查询系统内所有的布尔值设置状况
setsebool [-P] 布尔值=[0|1]        --- 设置布尔值
    -P      --- 直接将设置写入配置文件,重启后依然有效
setrouobleshoot ---这个服务会将关于SELinux的错误信息与解决方法记录到/var/log/messages与/var/log/setroubleshoot/*中,使用以下命令进行安装:yum install setroubleshoot setroubleshoot-server.排错的步骤就是根据/var/log/messages中的setroubleshoot错误信息,按照提示进行就可以了
/etc/init.d/auditd restart
  • 一些示例
semanage fcontext -l | grep '/var/www'      --- 查询/var/www目录下的默认类型有哪些
mkdir /opt/web  --- 我们在/srv下创建了一个目录并且通过下面这个命令把他的默认类型设置为public_content_t类型
semanage fcontext -a -t public_content_t "/opt/web(/.*)?"

PS:如果从disable转到启动SELinux的模式时,由于系统必须要针对文件写入安全性环境的信息,因此开机过程会花费不少时间来等待重新写入SELinux安全性环境(有时候也称为SELinux Label),而且在写完之后还需要再次重新启动一次,这需要等待很长一段时间;如果在重新写入SELinux Type(Relable)时出错,可以使用”restorecon -Rv /”重新还原SELinux的类型,就可以处理了

MAC OS安装virtualenv

pip3 install --upgrade pip  ## 升级pip
pip3 install virtualenv
pip3 install virtualenvwrapper
which virtualenvwrapper.sh ## 查找virtualenvwrapper.sh位置,下面需要写入到.bash_profile文件中
which python3  ## ## 查找python3位置,下面需要写入到.bash_profile文件中
vi ~/.bash_profile  ## 将下面三行写入到.bash_profile中,第二行和第三行的路径是通过前面两个命令找到的
export WORKON_HOME='~/.virtualenvs'
export VIRTUALENVWRAPPER_PYTHON='/Library/Frameworks/Python.framework/Versions/3.7/bin/python3'
source /Library/Frameworks/Python.framework/Versions/3.7/bin/virtualenvwrapper.sh
## 一些常用的命令
workon   会列出所有的虚拟环境
workon  [name]  会进入指定的虚拟环境
deactivate   退出当前的虚拟环境
mkvirtualenv  [name] 创建虚拟环境
rmvirtualenv  [name] 删除虚拟环境

gitlab安装脚本

  • 安装脚本
#!/bin/bash
## 系统为CENTOS7
setenforce 0
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
curl -s https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
systemctl start postfix && systemctl enable postfix
yum install gitlab-ce -y
## 生成ssl证书,这儿使用的是自生成证书,也可以在阿里上面使用免费的ssl证书
mkdir -p /etc/gitlab/ssl
cd /etc/gitlab/ssl
openssl req -new -nodes -sha256 -newkey rsa:2048 -keyout your_domai_name.key -out your_domai_name.csr -subj "/C=CN/ST=CHONGQING/0=CHONGQING/OU=CHONGQING/CN=your_domai_name/emailAddress=111111@qq.com"
openssl x509 -req -days 3650 -in your_domai_name.csr -signkey your_domai_name.key -out your_domai_name.crt
openssl dhparam -out dhparams.pem 2048
chmod 600 *
  • 需要修改的配置,编辑”/etc/gitlab/gitlab.rb”。以下几项需要打开,并且证书需要改成你自己设置的位置
##域名与ssl相关的配置
external_url 'https://your_domai_name'
nginx['redirect_http_to_https'] = true
nginx['ssl_certificate'] = "/etc/gitlab/ssl/#{node['fqdn']}.crt"
nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/#{node['fqdn']}.key"
nginx['ssl_dhparam'] = /etc/gitlab/ssl/dhparams.pem
##备份相关得设置
gitlab_rails['manage_backup_path'] = true
gitlab_rails['backup_path'] = "/opt/backup/gitlab"    ##gitlab备份目录
gitlab_rails['backup_archive_permissions'] = 0644       ##生成的备份文件权限
gitlab_rails['backup_keep_time'] = 7776000
  • 根据配置文件启动gitlab
gitlab-ctl reconfigure

jenkins安装脚本

  • jenkins安装脚本
#!/bin/bash
setenforce 0
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
yum install wget git curl -y
wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
yum install java jenkins -y
systemctl start jenkins
## 查看jenkins默认生成的密码
cat /var/lib/jenkins/secrets/initialAdminPassword

SHELL脚本之case

  • 一般形式
case variable in
    pattern1 | pattern2)
        command;;
    pattern3)
        command;;
    *)
        default command;;
esac
  • 例子
#!/bin/bash
case $USER in
    root | user)
        echo "Welcome.$USER"
        echo "Please enjoy your visit";;
    testing)
        echo "Special testing acount";;
    *)
    echo "Sorry,you are not allowed here";;
esac