当前位置:   article > 正文

网安等保-国产Linux操作系统银河麒麟KylinOS-V10SP3常规配置、系统优化与安全加固基线实践文档_麒麟系统调优

麒麟系统调优

本章目录:

0x00 前言简述

0x01 常规配置

  • 1.主机IP地址与网关设置

  • 2.主机DNS配置

  • 3.镜像源配置

  • 4.常规运维工具安装及系统升级

  • 5.系统时间时区同步配置

0x02 系统优化

  • 1.创建swap系统分区配置

  • 2.系统资源句柄数优化配置

  • 3.系统常规内核参数优化配置

  • 4.系统服务优化配置

0x03 安全加固

  • 1.远程登录主机提示信息

  • 2.远程登录主机系统信息

  • 3.远程登录sshd服务安全策略配置

  • 4.系统账户安全策略配置

  • 5.系统账户密码更改及过期策略配置

  • 6.系统用户密码复杂性策略配置

  • 7.系统用户登录失败策略配置

  • 8.系统用户su/sudo权限策略配置

  • 8.系统文件权限策略配置

  • 9.系统grub引导安全策略配置

  • 10.系统用户历史命令记录策略配置

  • 11.系统安全日志事件记录策略配置

  • 12.系统审计规则安全策略配置

  • 13.配置禁用系统非必须别名策略

  • 14.配置禁用桌面系统策略

  • 15.配置禁用Ctrl+Alt+Del重启系统

  • 16.配置rm删除回收站策略

  • 17.配置清除临时文件策略

  • 18.配置系统防火墙策略

  • 19.配置重启服务器策略

0x00 前言简述

描述: 随着国家要求各政府部门及事企业单位服务器系统国产化,越来越多的的企业单位逐步引进国产化Linux操作系统(大趋势),在众多国产操作系统中银河麒麟(KylinOS)、中科方德、统信UOS,此三家持续版本迭代超15年的其生态市场及占有率最高, 除此之外红旗Linux、共创Linux、凝思磐石、新支点、深度Linux、Start OS、思普操作系统、云针OS、鸿蒙OS、YunOS、OpenCloudOS等国产操作系统。

此处由于我们企业中是试用的银河麒麟(KylinOS)V10 SP3 版本的国产系统,为了试用该系统是否可以承载现有业务,以及满足网络安全等保2.0主机安全配置要求,遂针对该系统进行安全加固及常规初始化操作,设置安全基线镜像,以保证基础业务运行环境安全。

这里作者就不在针对银河麒麟(KylinOS)的国产系统进行详细介绍与下载安装讲解,有兴趣的朋友可以参照【1.国产银河麒麟V10服务器操作系统基础知识与安装实践】( https://blog.weiyigeek.top/2023/3-21-725.html ) 此文。

好的废话不多说,此处我将其分为三个章节,第一个章节是初始化运维常规配置,第二个章节是系统内核优化,第三个章节安全加固,此处我已经将其写成shell脚本可以直接运行加固大大的节省了我们运维人的时间,最后我会将安全加固shell脚本(部分适用于CentOS7操作系统)放在文章末尾, 以供各位看友使用实践参考,若有错误欢迎在【全栈工程师修炼指南】公众号留言。

若需观看视频实践演示,请在【全栈工程师修炼指南】公众号中回复【kylinos安全加固】或【10002】关键字获得脚本下载链接。

温馨提示: 在进行操作时请注意备份操作文件,以便于异常时及时回退。

温馨提示: 此处为了防止伸手党,以及尊重作者编写脚本, 实践成果,象征性的设置为收费文章,希望大家理解支持!

首发地址: https://mp.weixin.qq.com/s/eBF_Q-WkiZHKGdEG1MODNQ

0x01 常规配置

1.主机IP地址与网关设置

描述: 一台新安装的主机必须配置IP地址才能方便我们通过远程连接,所以第一步肯定是把网络打通,主要根据配置的IP地址与网络地址环境变量进行对应设置,例如下述部分脚本片段。

  1. # Modify the IP/MASK and Gateway
  2. VAR_NETINTERFACE=ens192
  3. VAR_IP=192.168.4.201/24
  4. VAR_GATEWAY=192.168.4.1
  5. if [ ! -f /opt/init/ ];then
  6. mkdir -vp /opt/init/
  7. sudo tee /opt/init/network.sh <<'EOF'
  8. #!/bin/bash
  9. # @Author: WeiyiGeek
  10. # @Description: Configure KylinOS / CentOS Linux Server Network
  11. # @E-mail: master@weiyigeek.top
  12. # @Blog: https://www.weiyigeek.top
  13. if [[ $# -lt 4 ]];then
  14. echo "Usage: $0 NetInterface IP/NETMASK GATEWAY DNS"
  15. echo "Example: $0 ens192 192.168.12.12/24 192.168.12.1 223.6.6.6"
  16. echo "@Author: WeiyiGeek"
  17. echo "@Blog: https://blog.weiyigeek.top"
  18. exit
  19. fi
  20. echo "Setting Network interface card: ${1}, IP: ${2} , GATEWAY: ${3}"
  21. CURRENT_IP=$(hostname -I | cut -f 1 -d " ")
  22. CURRENT_GATEWAY=$(hostname -I | cut -f 1,2,3,4 -d ".")
  23. CURRENT_FILE=/etc/sysconfig/network-scripts/ifcfg-${1}
  24. CONFIG_IP=${2%%/*}
  25. CONFIG_PREFIX=${2##*/}
  26. echo "Original Network info: IP: ${CURRENT_IP} , GATEWAY: ${CURRENT_GATEWAY}"
  27. echo "Setting Network interface card: ${1}, IP/NETMASK: ${2} , GATEWAY: ${3}, DNS: ${4}"
  28. if [[ -f ${CURRENT_FILE} ]];then
  29. # 已存在网卡配置文件的情况下
  30. egrep -q "^\s*ONBOOT=.*$" ${CURRENT_FILE} && sed -ri "s/^\s*ONBOOT=.*$/ONBOOT=yes/" ${CURRENT_FILE}|| echo "ONBOOT=yes" >> ${CURRENT_FILE}
  31. egrep -q "^\s*BOOTPROTO=.*$" ${CURRENT_FILE} && sed -ri "s/^\s*BOOTPROTO=.*$/BOOTPROTO=static/" ${CURRENT_FILE}|| echo "BOOTPROTO=static" >> ${CURRENT_FILE}
  32. egrep -q "^\s*IPADDR=.*$" ${CURRENT_FILE} && sed -ri "s/^\s*IPADDR=.*$/IPADDR=${CONFIG_IP}/" ${CURRENT_FILE}|| echo "IPADDR=${CONFIG_IP}" >> ${CURRENT_FILE}
  33. egrep -q "^\s*PREFIX=.*$" ${CURRENT_FILE} && sed -ri "s/^\s*PREFIX=.*$/PREFIX=${CONFIG_PREFIX}/" ${CURRENT_FILE}|| echo "PREFIX=${CONFIG_PREFIX}" >> ${CURRENT_FILE}
  34. egrep -q "^\s*GATEWAY=.*$" ${CURRENT_FILE} && sed -ri "s/^\s*GATEWAY=.*$/GATEWAY=${3}/" ${CURRENT_FILE}|| echo "GATEWAY=${3}" >> ${CURRENT_FILE}
  35. egrep -q "^\s*DNS1=.*$" ${CURRENT_FILE} && sed -ri "s/^\s*DNS1=.*$/DNS1=${4}/" ${CURRENT_FILE}|| echo "DNS1=${4}" >> ${CURRENT_FILE}
  36. else
  37. nmcli dev show ${1}
  38. nmcli conn add connection.id ${1}-staic connection.interface-name ${1} connection.autoconnect yes type Ethernet ifname ${1} ipv4.method manual ipv4.address ${2} ipv4.gateway ${3} ipv4.dns ${4} ipv4.ignore-auto-dns true
  39. fi
  40. sudo nmcli c reload
  41. read -t 5 -p "Heavy load network card, It is recommended to enter N during initialization (Y/N): " VERTIFY
  42. if [[ ${VERIFY:="N"} == "Y" || ${VERIFY:="N"} == "y" ]]; then
  43. sudo nmcli c up ${1}
  44. sudo nmcli d reapply ${1}
  45. else
  46. echo "Please reload the network card manually, run sudo nmcli d reapply ${1}."
  47. fi
  48. EOF
  49. # 权限赋予
  50. sudo chmod +x /opt/init/network.sh
  51. /opt/init/network.sh ${VAR_NETINTERFACE} ${VAR_IP} ${VAR_GATEWAY} ${VAR_DNS_SERVER}

2.主机DNS配置

描述: 完成IP地址的配置后,我便需要为主机配置私有DNS或者公共的DNS,以便可以解析外部域名。

  1. # Show Script Execute result (Y/N)
  2. VAR_VERIFY_RESULT=Y
  3. # Modify the DNS server
  4. # DNSPod: 119.29.29.29 Alidns: 223.5.5.5 223.6.6.6
  5. # Google: 8.8.8.8 8.8.4.4 Cloudflare: 1.1.1.1 1.0.0.1
  6. # Baidu: 114.114.114.114
  7. # Internal : Your intranet domain name resolution server
  8. VAR_DNS_SERVER=( "223.5.5.5" "114.114.114.114" "192.168.4.254")
  9. local flag
  10. # 此处配置的是百度IPV4 DNS与阿里云IPV6 DNS
  11. sed -i -e "s/^#FallbackDNS=.*/FallbackDNS=114.114.114.114 2400:3200::1 2400:3200:baba::1/" -e "s/^#DNSSEC=.*/DNSSEC=allow-downgrade/" -e "s/^#DNSOverTLS=.*/DNSOverTLS=opportunistic/" /etc/systemd/resolved.conf
  12. for dns in ${VAR_DNS_SERVER[@]};do
  13. grep -q "${dns}" /etc/systemd/resolved.conf
  14. if [ $? != 0 ];then
  15. echo "nameserver ${dns}"
  16. sed -i "/#DNS=/i DNS=${dns}" /etc/systemd/resolved.conf;
  17. fi
  18. done
  19. systemctl restart systemd-resolved && systemctl enable systemd-resolved
  20. find /etc/resolv.conf -delete
  21. ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf
  22. if [[ ${VERIFY:="N"} == "Y" || ${VERIFY:="N"} == "y" ]]; then
  23. grep -Ev '^#|^$' /etc/resolv.conf | uniq
  24. echo
  25. grep -Ev '^#|^$' /etc/systemd/resolved.conf | uniq
  26. fi

3.镜像源配置

描述: 使用国外的操作系统,例如CentOS、Ubuntu、Debian、Alpine等操作系统,通常为了加快Linux系统中下载安装软件的速度,我们是需要配置软件镜像源,此处由于我们是国产操作系统,其软件更新源也肯定是在国内,所以通常无需调整。

但此处为了防止小伙伴们更改过该镜像源,我也将各发行版镜像源配置罗列出来。

  1. local release
  2. cp /etc/yum.repos.d/kylin_x86_64.repo ${BACKUPDIR}
  3. # 1.根据主机发行版设置
  4. # (Tercel) 版本是 麒麟 V10 SP1 版本,
  5. # (Sword) 版本是 麒麟 V10 SP2 版本,
  6. # (Lance) 版本是 麒麟 V10 SP3 版本,
  7. release=$(grep -e "^VERSION=" /etc/os-release | cut -f 2 -d "=" | tr -d '[:punct:][:space:]')
  8. if [ ${release} == "V10Lance" ];then
  9. sudo tee /etc/yum.repos.d/kylin_x86_64.repo <<'EOF'
  10. ### Kylin Linux Advanced Server 10 (SP3) - os repo ###
  11. [ks10-adv-os]
  12. name = Kylin Linux Advanced Server 10 - Os
  13. baseurl = https://update.cs2c.com.cn/NS/V10/V10SP3/os/adv/lic/base/$basearch/
  14. gpgcheck = 1
  15. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-kylin
  16. enabled = 1
  17. [ks10-adv-updates]
  18. name = Kylin Linux Advanced Server 10 - Updates
  19. baseurl = https://update.cs2c.com.cn/NS/V10/V10SP3/os/adv/lic/updates/$basearch/
  20. gpgcheck = 1
  21. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-kylin
  22. enabled = 1
  23. [ks10-adv-addons]
  24. name = Kylin Linux Advanced Server 10 - Addons
  25. baseurl = https://update.cs2c.com.cn/NS/V10/V10SP3/os/adv/lic/addons/$basearch/
  26. gpgcheck = 1
  27. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-kylin
  28. enabled = 0
  29. EOF
  30. # echo "7" > /etc/yum/vars/centos_version
  31. # wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
  32. elif [ ${release} == "V10Sword" ];then
  33. echo "暂未使用麒麟 V10 Sword SP2 版本,请自行百度搜索,镜像源!"
  34. elif [ ${release} == "V10Tercel" ];then
  35. echo "暂未使用麒麟 V10 Tercel SP1 版本,请自行百度搜索,镜像源!"
  36. else
  37. echo "暂未使用麒麟除 V10 以外的系统版本,请自行百度搜索,镜像源!"
  38. fi
  39. sudo yum clean all -y && sudo yum makecache
  40. read -t ${VAR_VERIFY_TIMEOUT} -p "Please input, Perform system software update and upgrade. (Y/N) : " VERIFY
  41. if [[ ${VERIFY:="N"} == "Y" || ${VERIFY:="N"} == "y" ]]; then
  42. sudo yum update -y && sudo yum upgrade -y
  43. fi

PS: 虽然银河麒麟(KylinOS)V10 SP3 系统中可以使用CentOS7的镜像源,但是并不建议这样否则在镜像软件更新安装时,将会出现莫名错误。

4.常规运维工具安装及系统升级

描述: 完成软件镜像源配置后我们便可进行系统更新以及,常规的运维工具安装了。

  1. # 1.系统更新
  2. echo "[-] 系统软件源更新."
  3. sudo yum update && sudo yum upgrade -y && dnf repolist
  4. # 2.安装系统所需的常规软件
  5. echo "[-] 安装系统所需的常规软件."
  6. sudo dnf install -y gcc make
  7. sudo dnf install -y nano vim git unzip unrar ftp wget ntpdate dos2unix net-tools tree htop sysstat psmisc bash-completion jq rpcbind dialog nfs-utils
  8. # 补充:代理方式进行更新
  9. # echo "proxy=http://127.0.0.1:8080/" >> /etc/yum.conf
  10. # sudo yum clean all -y && sudo yum update -y && sudo yum upgrade -y
  11. # sudo yum install -y 软件包

5.系统时间时区同步配置

描述: 更新系统及对应工具后,我们需要针对系统时间时区做同步配置,此步骤非常重要往往会影响应用程序时间,建议在服务器中必须进行配置。

  1. # Show Script Execute result (Y/N)
  2. VAR_VERIFY_RESULT=Y
  3. # Modify the NTP server
  4. # PS: "192.168.4.254" 为内部NTP服务器,若需要搭建NTP服务器请参考,此篇文章: https://blog.weiyigeek.top/2020/1-29-112.html
  5. VAR_NTP_SERVER=( "ntp.aliyun.com" "ntp.tencent.com" "192.168.10.254")
  6. # 安装配置 chrony 时间同步服务器
  7. # 方式1.安装 Chrony 客户端配置
  8. if [[ $(rpm -qa | grep -c "chrony") -eq 0 ]];then
  9. dnf install -y chrony
  10. fi
  11. cp /etc/chrony.conf ${BACKUPDIR}
  12. grep -E -q "^server" /etc/chrony.conf | sed -i 's/^server/# server/g' /etc/chrony.conf
  13. grep -E -q "^pool" /etc/chrony.conf | sed -i 's/^pool/# pool/g' /etc/chrony.conf
  14. for ntp in ${VAR_NTP_SERVER[@]};do
  15. echo "ntp server => ${ntp}"
  16. if [[ ${ntp} =~ "ntp" ]];then
  17. echo "pool ${ntp} iburst maxsources 4" >> /etc/chrony.conf;
  18. else
  19. echo "pool ${ntp} iburst maxsources 1" >> /etc/chrony.conf;
  20. fi
  21. done
  22. systemctl enable chronyd.service && systemctl restart chronyd.service
  23. # chrony.conf 配置示例
  24. # sudo tee /etc/chrony.conf <<'EOF'
  25. # confdir /etc/conf.d
  26. # server ntp.aliyun.com iburst maxsources 4
  27. # server ntp.tencent.com iburst maxsources 4
  28. # pool 192.168.10.254 iburst maxsources 1
  29. # pool 192.168.12.254 iburst maxsources 2
  30. # pool 192.168.4.254 iburst maxsources 3
  31. # sourcedir /run/chrony-dhcp
  32. # sourcedir /etc/sources.d
  33. # keyfile /etc/chrony.keys
  34. # driftfile /var/lib/chrony/chrony.drift
  35. # ntsdumpdir /var/lib/chrony
  36. # logdir /var/log/chrony
  37. # maxupdateskew 100.0
  38. # rtcsync
  39. # makestep 1 3
  40. # leapsectz right/UTC
  41. # EOF
  42. # 方式2.使用 ntpdate 工具定时同步
  43. # sudo ntpdate 192.168.10.254 || sudo ntpdate 192.168.12.254 || sudo ntpdate ntp1.aliyun.com
  44. # 方式3.使用系统 systemd-timesyncd
  45. # echo 'NTP=192.168.10.254 192.168.4.254' >> /etc/systemd/timesyncd.conf
  46. # echo 'FallbackNTP=ntp.aliyun.com' >> /etc/systemd/timesyncd.conf
  47. # systemctl restart systemd-timesyncd.service
  48. if [[ ${VAR_VERIFY_RESULT} == "Y" ]];then systemctl status chronyd.service -l --no-pager;fi

主机时间同步校准与时区设置

  1. # Modify the timezone
  2. VAR_TIMEZONE=Asia/Shanghai
  3. # 1.时区设置
  4. sudo timedatectl set-timezone ${VAR_TIMEZONE}
  5. # sudo dpkg-reconfigure tzdata # 修改确认
  6. # sudo bash -c "echo 'Asia/Shanghai' > /etc/timezone" # 与上一条命令一样
  7. # 2.将当前的 UTC 时间写入硬件时钟 (硬件时间默认为UTC)
  8. sudo timedatectl set-local-rtc 0
  9. # 3.启用NTP时间同步:
  10. sudo timedatectl set-ntp yes
  11. # 4.校准时间服务器-时间同步(推荐使用chronyc进行平滑同步)
  12. sudo chronyc tracking
  13. # 5.手动校准-强制更新时间
  14. # chronyc -a makestep
  15. # 6.系统时钟同步硬件时钟
  16. # sudo hwclock --systohc
  17. sudo hwclock -w
  18. echo "设置时间同步与时区后: $(date)"
  19. # 7.重启依赖于系统时间的服务
  20. sudo systemctl restart rsyslog.service crond.service

0x02 系统优化

1.创建swap系统分区配置

描述: 当服务器系统内存过小时,我们可以划分一块磁盘空间作为swap交换分区以补充内存过小,无法运行某些程序,通常情况下会出现在VPS上,针对于企业中的服务器基本都是在64G以上,请根据业务需求划分,我们由于使用了K8S云原生通常情况下需要禁用SWAP交换分区,不过此处作者还是将方法其罗列出来以供需要的朋友使用。

  1. # Show Script Execute result (Y/N)
  2. VAR_VERIFY_RESULT=Y
  3. # Modify Script vertify timeout (unit s)
  4. VAR_VERIFY_TIMEOUT=8
  5. echo "[${COUNT}] Create system swap partition."
  6. read -t ${VAR_VERIFY_TIMEOUT} -p "Please input, Create swap partition. (Y/N) : " VERIFY
  7. if [[ ${VERIFY:="N"} == "Y" || ${VERIFY:="N"} == "y" ]]; then
  8. # 1.验证当前内存大小
  9. MEM=$(free -m | awk '/Mem:/{print $2}')
  10. if [ "$MEM" -le 1280 ]; then
  11. MEM_LEVEL=1G
  12. elif [ "$MEM" -gt 1280 ] && [ "$MEM" -le 2500 ]; then
  13. MEM_LEVEL=2G
  14. elif [ "$MEM" -gt 2500 ] && [ "$MEM" -le 3500 ]; then
  15. MEM_LEVEL=3G
  16. elif [ "$MEM" -gt 3500 ] && [ "$MEM" -le 4500 ]; then
  17. MEM_LEVEL=4G
  18. elif [ "$MEM" -gt 4500 ] && [ "$MEM" -le 8000 ]; then
  19. MEM_LEVEL=6G
  20. elif [ "$MEM" -gt 8000 ]; then
  21. MEM_LEVEL=8G
  22. fi
  23. # 2.根据内存大小划分对应的swap分区并自动挂载
  24. if [ "$(free -m | awk '/Swap:/{print $2}')" == '0' ]; then
  25. fallocate -l "${MEM_LEVEL}" /swapfile
  26. chmod 600 /swapfile
  27. mkswap /swapfile >/dev/null 2>&1
  28. swapon /swapfile
  29. sed -i "/swap/d" /etc/fstab
  30. echo "/swapfile swap swap defaults 0 0" >> /etc/fstab
  31. fi
  32. # 3.swap分区内核参数调整
  33. egrep -q "^\s*vm.swappiness.*$" /etc/sysctl.conf && sed -ri "s/^\s*vm.swappiness.*$/vm.swappiness = 10/" /etc/sysctl.conf || echo "vm.swappiness = 10" >> /etc/sysctl.conf
  34. egrep -q "^\s*vm.vfs_cache_pressure.*$" /etc/sysctl.conf && sed -ri "s/^\s*vm.vfs_cache_pressure.*$/vm.vfs_cache_pressure = 501/" /etc/sysctl.conf || echo "vm.vfs_cache_pressure = 50" >> /etc/sysctl.conf
  35. sysctl -p >/dev/null 2>&1
  36. if [[ $VAR_VERIFY_RESULT == "Y" ]]; then
  37. swapon --show
  38. echo .
  39. free -h
  40. echo .
  41. grep -Ev '^#|^$' /etc/fstab | uniq
  42. fi
  43. fi

2.系统资源句柄数优化配置

描述: 为了提高系统的高并发以及防止程序报 Too many open file 错误,通常需要针对系统资源句柄数进行优化配置。

  1. echo "[-] Linux 系统的最大进程数和最大文件打开数限制."
  2. cp -a /etc/security/limits.conf ${BACKUPDIR}
  3. egrep -q "^\s*ulimit -HSn\s+\w+.*$" /etc/profile && sed -ri "s/^\s*ulimit -HSn\s+\w+.*$/ulimit -HSn 655350/" /etc/profile || echo "ulimit -HSn 655350" >> /etc/profile
  4. egrep -q "^\s*ulimit -HSu\s+\w+.*$" /etc/profile && sed -ri "s/^\s*ulimit -HSu\s+\w+.*$/ulimit -HSu 655350/" /etc/profile || echo "ulimit -HSu 655350" >> /etc/profile
  5. if ! grep -qi "# OS Resources Limits Config" /etc/security/limits.conf; then
  6. sed -i 's/^# End of file*//' /etc/security/limits.conf
  7. {
  8. echo '# OS Resources Limits Config'
  9. echo '* soft nofile 655350'
  10. echo '* hard nofile 655350'
  11. echo '* soft nproc unlimited'
  12. echo '* hard nproc unlimited'
  13. echo '* soft core unlimited'
  14. echo '* hard core unlimited'
  15. echo '# End of file'
  16. } >> /etc/security/limits.conf
  17. fi
  18. if [[ $VAR_VERIFY_RESULT == "Y" ]]; then grep -Ev '^#|^$' /etc/security/limits.conf | uniq;fi

3.系统常规内核参数优化配置

描述: 服务器内核参数的优化有助于系统以及应用程序提供更好的性能,但是通常需要针对应用程序特点以及应用场景进行相应配置,下述只是常规配置有侧重点的朋友们,可根据实际情况进行调整。

  1. # Show Script Execute result (Y/N)
  2. VAR_VERIFY_RESULT=Y
  3. # Modify Script vertify timeout (unit s)
  4. VAR_VERIFY_TIMEOUT=8
  5. # 1.系统内核参数的配置文件/etc/sysctl.conf
  6. echo "[-] 系统内核参数的优化配置 /etc/sysctl.conf"
  7. # 启用IPV4数据包转发(业务需要)
  8. egrep -q "^(#)?net.ipv4.ip_forward.*" /etc/sysctl.conf && sed -ri "s|^(#)?net.ipv4.ip_forward.*|net.ipv4.ip_forward = 1|g" /etc/sysctl.conf || echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
  9. # egrep -q "^(#)?net.bridge.bridge-nf-call-ip6tables.*" /etc/sysctl.conf && sed -ri "s|^(#)?net.bridge.bridge-nf-call-ip6tables.*|net.bridge.bridge-nf-call-ip6tables = 1|g" /etc/sysctl.conf || echo "net.bridge.bridge-nf-call-ip6tables = 1" >> /etc/sysctl.conf
  10. # egrep -q "^(#)?net.bridge.bridge-nf-call-iptables.*" /etc/sysctl.conf && sed -ri "s|^(#)?net.bridge.bridge-nf-call-iptables.*|net.bridge.bridge-nf-call-iptables = 1|g" /etc/sysctl.conf || echo "net.bridge.bridge-nf-call-iptables = 1" >> /etc/sysctl.conf
  11. egrep -q "^(#)?net.ipv6.conf.all.disable_ipv6.*" /etc/sysctl.conf && sed -ri "s|^(#)?net.ipv6.conf.all.disable_ipv6.*|net.ipv6.conf.all.disable_ipv6 = 1|g" /etc/sysctl.conf || echo "net.ipv6.conf.all.disable_ipv6 = 1" >> /etc/sysctl.conf
  12. egrep -q "^(#)?net.ipv6.conf.default.disable_ipv6.*" /etc/sysctl.conf && sed -ri "s|^(#)?net.ipv6.conf.default.disable_ipv6.*|net.ipv6.conf.default.disable_ipv6 = 1|g" /etc/sysctl.conf || echo "net.ipv6.conf.default.disable_ipv6 = 1" >> /etc/sysctl.conf
  13. egrep -q "^(#)?net.ipv6.conf.lo.disable_ipv6.*" /etc/sysctl.conf && sed -ri "s|^(#)?net.ipv6.conf.lo.disable_ipv6.*|net.ipv6.conf.lo.disable_ipv6 = 1|g" /etc/sysctl.conf || echo "net.ipv6.conf.lo.disable_ipv6 = 1" >> /etc/sysctl.conf
  14. egrep -q "^(#)?net.ipv6.conf.all.forwarding.*" /etc/sysctl.conf && sed -ri "s|^(#)?net.ipv6.conf.all.forwarding.*|net.ipv6.conf.all.forwarding = 1|g" /etc/sysctl.conf || echo "net.ipv6.conf.all.forwarding = 1" >> /etc/sysctl.conf
  15. # 2.系统内核参数扩展优化配置
  16. if ! grep -qi "# OS Resources Limits Config" /etc/sysctl.conf; then
  17. tee -a /etc/sysctl.conf <<'EOF'
  18. # Configuration of system kernel parameters
  19. # 禁止 icmp 重定向报文
  20. net.ipv4.conf.all.accept_redirects = 0
  21. net.ipv4.conf.default.accept_redirects = 0
  22. # 忽略 icmp echo 请求广播
  23. net.ipv4.icmp_echo_ignore_broadcasts = 1
  24. # 禁止 icmp 源路由
  25. net.ipv4.conf.all.accept_source_route = 0
  26. net.ipv4.conf.default.accept_source_route = 0
  27. # 禁止发送重定向 (若非必须建议设置 0)
  28. net.ipv4.conf.all.send_redirects = 0
  29. net.ipv4.conf.default.send_redirects = 0
  30. # 禁止对主机进行 IP 伪装
  31. net.ipv4.conf.all.rp_filter = 1
  32. net.ipv4.conf.default.rp_filter = 1
  33. # 限制一个进程可以拥有的VMA(虚拟内存区域)的数量
  34. vm.max_map_count = 262144
  35. # 设置内存分配策略,使用0表示内核将检查是否有足够的可用内存。
  36. vm.overcommit_memory = 0
  37. # 调整提升服务器负载能力之外,还能够防御小流量的Dos、CC和SYN攻击
  38. net.ipv4.tcp_syncookies = 1
  39. net.ipv4.tcp_tw_reuse = 1
  40. # net.ipv4.tcp_tw_recycle = 1
  41. net.ipv4.tcp_fin_timeout = 60
  42. net.ipv4.tcp_synack_retries = 1
  43. net.ipv4.tcp_syn_retries = 1
  44. net.ipv4.tcp_fastopen = 3
  45. # 优化TCP的可使用端口范围及提升服务器并发能力(注意一般流量小的服务器上没必要设置如下参数)
  46. net.ipv4.tcp_keepalive_time = 7200
  47. net.ipv4.tcp_max_syn_backlog = 8192
  48. net.ipv4.tcp_max_tw_buckets = 16384
  49. net.ipv4.ip_local_port_range = 1024 65535
  50. # 优化核套接字TCP的缓存区
  51. net.core.netdev_max_backlog = 8192
  52. net.core.somaxconn = 32768
  53. net.core.rmem_max = 12582912
  54. net.core.rmem_default = 6291456
  55. net.core.wmem_max = 12582912
  56. net.core.wmem_default = 6291456
  57. # 内存缓存IO优化
  58. vm.dirty_background_ratio = 5
  59. vm.dirty_ratio = 10
  60. EOF
  61. fi
  62. if [[ ${VAR_VERIFY_RESULT} == "Y" ]];then sysctl -p;fi

4.系统服务优化配置

描述: 针对我们新安装的KylinOS服务器中往往存在许多非必须服务,此处我们可以根据需求禁用相关服务。

  1. # 1.用于关闭与禁用某些服务端口
  2. echo "[-] 用于关闭与禁用某些服务端口。."
  3. local VAR_APP_SERVICE VAR_SYSTEM_SERVICE
  4. VAR_APP_SERVICE="telnet.socket printer sendmail nfs kshell lpd tftp ident time ntalk bootps klogin ypbind daytime nfslock echo discard chargen debug-shell.service"
  5. VAR_SYSTEM_SERVICE="chargen-dgram daytime-stream echo-streamklogin tcpmux-server chargen-stream discard-dgram eklogin krb5-telnet tftp cvs discard-stream ekrb5-telnet kshell time-dgram daytime-dgram echo-dgram gssftp rsync time-stream"
  6. for i in ${VAR_APP_SERVICE};do
  7. echo "Status and Disable APP ${i} Service!"
  8. # systemctl status ${i}
  9. systemctl stop ${i};systemctl disable ${i};
  10. done
  11. for i in ${VAR_SYSTEM_SERVICE};do
  12. echo "Status and Disable System ${i} Service!"
  13. # systemctl status ${i}
  14. systemctl stop ${i};systemctl disable ${i};
  15. done
  16. # 2.禁用烦人的apport错误报告
  17. if [ -f /etc/default/apport ]; then
  18. cp /etc/default/apport ${BACKUPDIR}
  19. sed -i 's/enabled=.*/enabled=0/' /etc/default/apport
  20. systemctl stop apport.service
  21. systemctl disable apport.service
  22. systemctl mask apport.service >/dev/null 2>&1
  23. fi
  24. read -t ${VAR_VERIFY_TIMEOUT} -p "Please input, is service verificating (Y/N) : " VERIFY
  25. if [[ ${VERIFY:="N"} == "Y" || ${VERIFY:="N"} == "y" ]]; then
  26. systemctl status apport.service --no-pager
  27. else
  28. log::success "[${COUNT}] This operation is completed!"
  29. fi
  30. # 3.非云的环境下禁用或者卸载多余的cloud-init软件及其服务
  31. sudo systemctl stop cloud-init.target cloud-init.service cloud-config.service cloud-init-local.service cloud-final.service
  32. sudo systemctl disable cloud-init.target cloud-init.service cloud-config.service cloud-init-local.service cloud-final.service
  33. sudo systemctl mask cloud-init.service cloud-config.service cloud-init-local.service cloud-final.service >/dev/null 2>&1
  34. # 禁用 Ubuntu 中的 cloud-init, 在 /etc/cloud 目录下创建 cloud-init.disable 文件(重启后生效)
  35. if [ ! -f /etc/cloud/cloud-init.disable ];then sudo touch /etc/cloud/cloud-init.disable;fi
  36. read -t ${VAR_VERIFY_TIMEOUT} -p "Please input, is Remove cloud-init related files and their directories (Y/N) : " VERIFY
  37. if [[ ${VERIFY:="N"} == "Y" || ${VERIFY:="N"} == "y" ]]; then
  38. sudo apt purge cloud-init -y
  39. sudo rm -rf /etc/cloud && sudo rm -rf /var/lib/cloud/
  40. fi
  41. sudo systemctl daemon-reload
  42. # 4.在系统启动时禁用debug-shell服务
  43. systemctl stop debug-shell.service
  44. systemctl mask debug-shell.service >/dev/null 2>&1
  45. if [[ $VAR_VERIFY_RESULT == "Y" ]]; then
  46. systemctl status debug-shell.service --no-pager
  47. fi

0x03 安全加固

1.远程登录主机提示信息

描述: 配置提示信息可以提示运维人员以及恶意人员,在非权限授权时禁止访问。

  1. # 1.设置SSH登录前警告Banner提示
  2. egrep -q "^\s*(banner|Banner)\s+\W+.*$" /etc/ssh/sshd_config && sed -ri "s/^\s*(banner|Banner)\s+\W+.*$/Banner \/etc\/issue.net/" /etc/ssh/sshd_config || echo "Banner /etc/issue.net" >> /etc/ssh/sshd_config
  3. sudo tee /etc/issue <<'EOF'
  4. ************************* [ 安全登陆 (Security Login) ] ************************
  5. Authorized users only. All activity will be monitored and reported.By WeiyiGeek Security Center.
  6. Author: WeiyiGeek
  7. blog: https://blog.weiyigeek.top
  8. EOF
  9. sudo tee /etc/issue.net <<'EOF'
  10. ************************* [ 安全登陆 (Security Login) ] *************************
  11. Authorized users only. All activity will be monitored and reported.By WeiyiGeek Security Center.
  12. Author: WeiyiGeek
  13. blog: https://blog.weiyigeek.top
  14. EOF
  15. # 2.本地控制台与SSH登录后提示自定义提示信息
  16. tee /etc/motd <<'EOF'
  17. Welcome to KylinOS Private Cloud Computer Service!
  18. If the server is abnormal, please add WX weiyigeeker (WeiyiGeek-Security-Center)
  19. _ooOoo_
  20. o8888888o
  21. 88" . "88
  22. (| -_- |)
  23. O\ = /O
  24. ____/`---'\____
  25. .' \\| |// `.
  26. / \\||| : |||// \
  27. / _||||| -:- |||||- \
  28. | | \\\ - /// | |
  29. | \_| ''\---/'' | |
  30. \ .-\__ `-` ___/-. /
  31. ___`. .' /--.--\ `. . __
  32. ."" '< `.___\_<|>_/___.' >'"".
  33. | | : `- \`.;`\ _ /`;.`/ - ` : | |
  34. \ \ `-. \_ __\ /__ _/ .-` / /
  35. ======`-.____`-.___\_____/___.-`____.-'======
  36. `=---='
  37. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  38. 佛祖保佑 永不死机
  39. 心外无法 法外无心
  40. EOF

脚本执行效果:

2.远程登录主机系统信息

描述: 在登录到系统后及时的显示服务器系统相关信息,包括但不限于系统资源信息、登录时间、失败信息,以及各分区磁盘使用率。

后续完整文章请访问获取,https://mp.weixin.qq.com/s/eBF_Q-WkiZHKGdEG1MODNQ

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/825549
推荐阅读
相关标签
  

闽ICP备14008679号