CentOS Linux在公司服务器上广泛被使用,本文总结了一些常见的加固方法。

基本原则:

  • 最小的权限+最小的服务=最大的安全
  • 操作之前先备份;
  • 为避免配置错误无法登录主机,请始终保持有一个终端已用root登录并不退出,在另一个终端中做配置修改。这样即使改错,也不至于因系统无法登录导致永远无法登录或恢复配置。

操作项:

一、注释掉系统不需要的用户和用户组

注意:不建议直接删除,当你需要某个用户时,自己重新添加会很麻烦。
   cp  /etc/passwd  /etc/passwdbak   #修改之前先备份
   vi /etc/passwd  #编辑用户,在前面加上#注释掉此行 
   #adm:x:3:4:adm:/var/adm:/sbin/nologin
   #lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
   #sync:x:5:0:sync:/sbin:/bin/sync
   #shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
   #halt:x:7:0:halt:/sbin:/sbin/halt
   #uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
   #operator:x:11:0perator:/root:/sbin/nologin
   #games:x:12:100:games:/usr/games:/sbin/nologin
   #gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
   #ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin    #注释掉ftp匿名账号 
  cp /etc/group   /etc/groupbak   #修改之前先备份
  vi /etc/group  #编辑用户组,在前面加上#注释掉此行
  #adm:x:4:root,adm,daemon
  #lp:x:7:daemon,lp
  #uucp:x:14:uucp
  #games:x:20:
  #dip:x:40:

二、关闭系统不需要的服务

使用命令 chkconfig|grep on 检查开机自启动的服务;

abrt-ccpp       0:off   1:off   2:off   3:on    4:off   5:on    6:off
abrtd           0:off   1:off   2:off   3:off   4:off   5:on    6:off
acpid           0:off   1:off   2:on    3:on    4:on    5:on    6:off
auditd          0:off   1:off   2:on    3:on    4:on    5:on    6:off
autofs          0:off   1:off   2:off   3:on    4:on    5:on    6:off
blk-availability        0:off   1:on    2:on    3:on    4:on    5:on    6:off
certmonger      0:off   1:off   2:off   3:on    4:on    5:on    6:off
cgconfig        0:off   1:off   2:off   3:off   4:off   5:off   6:off
cpuspeed        0:off   1:on    2:on    3:on    4:on    5:on    6:off
crond           0:off   1:off   2:on    3:on    4:on    5:on    6:off
cups            0:off   1:off   2:on    3:on    4:on    5:on    6:off
haldaemon       0:off   1:off   2:off   3:on    4:on    5:on    6:off
ip6tables       0:off   1:off   2:on    3:off   4:on    5:on    6:off
iptables        0:off   1:off   2:on    3:on    4:on    5:on    6:off
irqbalance      0:off   1:off   2:off   3:on    4:on    5:on    6:off
kdump           0:off   1:off   2:off   3:off   4:on    5:on    6:off
lvm2-monitor    0:off   1:on    2:on    3:off   4:on    5:on    6:off
mcelogd         0:off   1:off   2:off   3:on    4:off   5:on    6:off
mdmonitor       0:off   1:off   2:on    3:on    4:on    5:on    6:off
messagebus      0:off   1:off   2:on    3:on    4:on    5:on    6:off
netconsole      0:off   1:off   2:off   3:off   4:off   5:off   6:off
netfs           0:off   1:off   2:off   3:on    4:on    5:on    6:off
network         0:off   1:off   2:on    3:on    4:on    5:on    6:off
nfslock         0:off   1:off   2:off   3:on    4:on    5:on    6:off
portreserve     0:off   1:off   2:on    3:on    4:on    5:on    6:off
postfix         0:off   1:off   2:on    3:on    4:on    5:on    6:off
restorecond     0:off   1:off   2:off   3:off   4:off   5:off   6:off
rpcbind         0:off   1:off   2:on    3:on    4:on    5:on    6:off
rpcgssd         0:off   1:off   2:off   3:on    4:on    5:on    6:off
rsyslog         0:off   1:off   2:on    3:on    4:on    5:on    6:off
sshd            0:off   1:off   2:on    3:on    4:on    5:on    6:off
sysstat         0:off   1:on    2:on    3:on    4:on    5:on    6:off
udev-post       0:off   1:on    2:on    3:on    4:on    5:on    6:off

服务说明:

auditd

The audit package contains the user space utilities for storing and searching the audit records generate by the audit subsystem in the Linux 2.6 kernel.

blk-availability

Controls availability of LVM2 block devices.

cgconfig

Start cgconfigparser.

cgred

This is a daemon for automatically classifying processes into cgroups based on UID/GID.

cpuspeed

This program monitors the system's idle percentage and reduces or raises the CPUs' clock speeds and voltages accordingly to minimize power consumption when idle and maximize perfor-mance when needed.

crond

The daemon to execute scheduled commands.

ip6tables

It's IPv6 packet filter administration.

iptables

It's administration tool for IPv4 packet filtering and NAT.

lvm2-monitor

Monitor LVM2 with using dmeventd.

netconsole

It sends printk message from kernel on UDP.

netfs

NFS clinet.

network

Network system.

postfix

SMTP service daemon.

rdisc

Detect routers in local subnets.

restorecond

It's the daemon that watches for file creation and then sets the default SELinux file context.

rsyslog

It reads and/or clears kernel message ring buffer; set console_loglevel.

saslauthd

saslauthd is a daemon process that handles plaintext authentication requests on behalf of the SASL library.

sshd

OpenSSH sshd daemon.

udev-post

Moves the generated persistent udev rules to /etc/udev/rules.d.

(见http://www.server-world.info/en/note?os=CentOS_6&p=initial_conf&f=4

关闭不必要的服务

service acpid stop;chkconfig acpid off    #电源进阶设定,常用在 Laptop 上
  service cpuspeed stop;chkconfig  cpuspeed  off   #停用控制CPU速度主要用来省电
  service cups stop;chkconfig cups off    #停用 Common UNIX Printing System 使系统支援印表机
  service ip6tables stop;chkconfig ip6tables off   #禁止IPv6

三、禁止非root用户执行/etc/rc.d/init.d/下的系统命令
  chmod -R 700 /etc/rc.d/init.d/* (chmod -R 755 /etc/rc.d/init.d/*    #恢复默认设置)

四、修改history命令记录

cp /etc/profile   /etc/profilebak
   vi /etc/profile
   找到 HISTSIZE=1000 改为 HISTSIZE=50

五、调整文件描述符大小

echo "ulimit -SHn 65535" >> /etc/rc.local

/etc/security/limits.conf

*     soft    nofile          65535

*     hard    nofile          65535
   *     soft    stack           unlimited
   *     hard    stack           unlimited

六、同步系统时间

cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

crontab -e
      0-59/10 * * * * /usr/sbin/ntpdate us.pool.ntp.org | logger -t NTP

chkconfig --list | grep ntp #看下服务情况

chkconifg ntpd on

service ntpd start

七、内核参数优化

vi /etc/sysctl.conf    #末尾添加如下参数  (备注:内核参数优化 根据不同服务器不同业务进行调整)

net.ipv4.tcp_max_tw_buckets = 6144 
net.ipv4.tcp_sack = 1 
net.ipv4.tcp_window_scaling = 1 
net.ipv4.tcp_rmem = 4096 87380 4194304 
net.ipv4.tcp_wmem = 4096 16384 4194304

net.core.wmem_default = 8388608 
net.core.rmem_default = 8388608 
net.core.rmem_max = 16777216 
net.core.wmem_max = 16777216 
net.core.netdev_max_backlog = 262144 
net.core.somaxconn = 262144

net.ipv4.tcp_max_orphans = 3276800 
net.ipv4.tcp_max_syn_backlog = 262144 
net.ipv4.tcp_timestamps = 1 
net.ipv4.tcp_synack_retries = 1 
net.ipv4.tcp_syn_retries = 1 
net.ipv4.tcp_tw_recycle = 1 
net.ipv4.tcp_tw_reuse = 1 
net.ipv4.tcp_mem = 94500000 915000000 927000000 
net.ipv4.tcp_fin_timeout = 10 
net.ipv4.tcp_keepalive_time = 600 
net.ipv4.ip_local_port_range = 1024 65000

vm.swappiness = 5 
vm.zone_reclaim_mode = 0 
net.ipv4.tcp_max_syn_backlog = 5000 
vm.dirty_ratio = 60 
vm.dirty_background_ratio = 5

八、CentOS 系统优化    
      cp  /etc/profile  /etc/profilebak2
      vi /etc/profile      #在文件末尾添加以下内容
      ulimit -c unlimited
      ulimit -s unlimited
      ulimit -SHn 65535   
      source  /etc/profile    #使配置立即生效
      ulimit -a    #显示当前的各种用户进程限制

九、去除系统相关信息

事先备份到安全的地方哈:)

echo "Welcome to Server" >/etc/issue

echo "Welcome to Server" >/etc/issue.net

echo "Welcome to Server" >/etc/redhat-release

十、SSH服务安全配置

vi /etc/ssh/sshd_config

1. 禁用root远程登录

PermitRootLoginno

PermitEmptyPasswords no #禁止空密码登录

2.不要支持闲置会话,并配置 Idle Log Out Timeout 间隔:
  ClientAliveInterval 600 # (Set to 600 seconds = 10 minutes)
  ClientAliveCountMax 0

3.禁用用户的 .rhosts 文件:

IgnoreRhosts yes

4.PasswordAuthentication no

6.ssh安全技巧之更改 SSH 端口
  建议改成 10000 以上,以降低扫描到端口的机率。

Port 36000

7.使用iptables允许特定的主机连接
  作为TCP wrappers的一个代替品,你可以使用iptables来限制SSH访问(但可以同时使用这个两个的),这里有一个简单的例子,指出了如何允许一个特定的主机连接到你的SSH服务:
  iptables -A INPUT -p tcp -m state --state NEW --source 193.180.177.13 --dport 22 -j ACCEPT

保存后重启ssh服务。

十一、添加普通用户并进行sudo授权管理

useradd -d /home/goal user (指定home目录与用户名不同)
  passwd user  #设置密码  
  vi /etc/sudoers  #或visudo打开,添加user用户所有权 限  
  root    ALL=(ALL)       ALL
  user    ALL=(ALL)       ALL

十二、安装安全补丁(经常更新为好)

yum install yum-security

检查安全更新
yum –security check-update
只安装安全更新
yum update –security
检查特定软件有无安全更新
yum list-security software_name
列出更新的详细信息
yum info-security software_name

十三、将 SSH 程序包和需要的库保持为最新:
    yum update openssh-server openssh openssh-clients -y

十四、清空防火墙并设置规则 配置防火墙以接受仅来自已知网段的 SSH 连接:

iptables -t filter -P INPUT ACCEPT

iptables -t filter -F INPUT

iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

iptables -A INPUT -i lo -j ACCEPT

iptables -A INPUT -s 10.0.0.0/8 -j ACCEPT

iptables -A INPUT -s 192.168.0.0/16 -j ACCEPT

iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT

iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 36000 -j ACCEPT

iptables -A INPUT -p icmp -j ACCEPT

iptables -A INPUT -j REJECT --reject-with icmp-host-prohibited

iptables -A FORWARD -j REJECT --reject-with icmp-host-prohibited

十五、锁定关键文件系统

chattr +i /etc/passwd

chattr +i /etc/inittab

chattr +i /etc/group

chattr +i /etc/shadow

chattr +i /etc/gshadow

centos系统组件优化的更多相关文章

  1. CentOS系统基础优化16条知识汇总

    1.不用root管理,以普通用户的名义通过sudo授权管理: 2.更改默认的远程连接服务端,禁止root用户远程连接,甚至要更改只监听内网ip: 3.定时自动更新服务器时间,使其和互联网时间同步: 4 ...

  2. CentOS系统Apache服务器优化详解

    1.Apache优化 Apache能够在CentOS系统正常运行.但是,对于访问量稍大的站点,Apache的这些默认配置是无法满足需求的,我们仍需调整Apache的一些参数,使Apache能够在大访问 ...

  3. CentOS系统-常用组件安装

    1,安装系统后,补装包组yum groupinstall "Compatibility libraries" "Base" "Development ...

  4. Centos 7.x 系统基础优化

    Centos 7.x 系统基础优化 1.更换国内yum源 删除系统带的centos官方yum源 rm -rf /etc/yum.repos.d/* 使用国内阿里云源 curl -o /etc/yum. ...

  5. centos 6.x 系统基础优化简版

    Centos 6.x 系统基础优化 1.更换国内yum源 删除系统带的centos官方yum源 rm -rf /etc/yum.repos.d/* 使用国内阿里云源 curl -o /etc/yum. ...

  6. CentOS系统下Tomcat的优化

    一.JVM内存优化(线程优化) vim ./bin/catalina.sh 在catalina.sh文件中添加以下配置: JAVA_OPTS="-server -Xms128m -Xmx12 ...

  7. 6步骤实现CentOS系统环境精简优化

    6步骤实现CentOS系统环境精简优化 发布时间:2014-11-03 14:59:27   编辑:AHLinux.com 第一步.删除不必要的自带软件包yum remove Deployment_G ...

  8. 生产环境下Centos 6.5优化配置 (装载)

    本文 centos 6.5 优化 的项有18处: 1.centos6.5最小化安装后启动网卡 2.ifconfig查询IP进行SSH链接 3.更新系统源并且升级系统 4.系统时间更新和设定定时任 5. ...

  9. Linux Centos 系统上安装BT客户端 Transmission

    Linux Centos 系统上安装BT客户端 Transmission   Transmission是一种BitTorrent客户端,特点是一个跨平台的后端和其上的简洁的用户界面,以MIT许可证和G ...

随机推荐

  1. 【转】c#中数组赋值方法

    C#中数组复制有多种方法,数组间的复制 ,,,};int [] alias = pins; 这里出了错误,也是错误的根源,以上代码并没有出错,但是根本不是复制,因为pins和alias都是引用,存在于 ...

  2. Android/Unity大乱斗-完整双方集成交互指南

    这是一个很长很长的story!-芝麻粒儿创作 开篇 源码地址:GitHub 本文目的,将Unity集成到Android端,学完本文后你可以做到 Android任意布局加载Unity 3D场景 任意操作 ...

  3. C#调用Matlab生成的Dll

    问题描述:最近开发需要调用matlab生成的DLL,在New MWNumericArray 对象的时候报错,提示未将对象引用到对象的实例. 问题分析:因为MWArray.dll是Matlab提供的DL ...

  4. windows服务搭建(VS2019创建Windows服务不显示安装组件)

    1.创建windows服务应用 2.右键查看代码 3.写个计时器Timer  using System.Timers; 如上图,按tab键快速操作  会自动创建一个委托 改为下边的方式,打印日志来记录 ...

  5. 让VS2019支持.NET Core WinForms和WPF设计器的临时办法(转)

    .net core 3.0 Preview 6,vs2019 16.1.4 暂时还不支持Winform designer 解决方案: 将Form1修改为其他名字 WinForm   编辑项目文件  加 ...

  6. Window下安装并使用InfluxDB可视化工具 —— InfluxDBStudio

    下载 直接访问: https://github.com/CymaticLabs/InfluxDBStudio/releases/tag/v0.2.0-beta.1 创建or编辑InfluxDB 这个软 ...

  7. 鉴于崔庆才大大的对于 beautifulsoup 的再理解

    源地址看 soups = BeautifulSoup(html) soup = BeautifulSoup(open('index.html')) print soup.prettify() Tag通 ...

  8. java jvm jre jdk三者的关系

    jvm:java虚拟机器(跨平台的关键) jre:java运行环境 jdk:java 开发工具包(kit) jdk>jre>jvm 环境变量配置 https://www.cnblogs.c ...

  9. KALI美化-设置CONKY开机启动

    简介 Conky 是一个应用于桌面环境的系统监视软件,可以在桌面上监控系统运行状态.网络状态等一系列参数 https://github.com/brndnmtthws/conky/ 详细配置文档:ht ...

  10. c#移位运算符("<<"及">>")

    C#是用<<(左移) 和 >>(右移) 运算符是用来执行移位运算. 左移 (<<) 将第一个操作数向左移动第二个操作数指定的位数,空出的位置补0.  左移相当于乘. ...