1006  
查询码:00000155
安装linux系统后调优及安全设置
作者: wyasw 于 2020年03月29日 发布在分类 / Linux / 系统相关 下,并于 2020年03月29日 编辑
linux

  安装linux系统后调优及安全设置

1.1 关闭SElinux功能

1.1.1 永久关闭

[root@master1 ~]# sed -i 's#SELINUX=enforcing#SELINUX=disabled#'  /etc/selinux/config

[root@master1 ~]# grep SELINUX=disabled /etc/selinux/config

SELINUX=disabled

#注意:修改配置文件以后需要重启

1.1.2 临时关闭

[root@master1 ~]# setenforce

usage:  setenforce [ Enforcing | Permissive | 1 | 0 ]

[root@master1 ~]# setenforce 0

setenforce: SELinux is disabled

[root@master1 ~]# getenforce

Disabled

#注意:如果系统不能重启,则选择临时关闭

1.2 设定系统运行级别为3(文本模式)

1.2.1  centos6系统

[root@dsf ~]# runlevel

N 3

[root@dsf ~]# grep 3:initdefault /etc/inittab

id:3:initdefault:

[root@dsf ~]# init 5

[root@dsf ~]# runlevel

3 5

#runlevel:查看当前系统运行级别

[root@dsf ~]# runlevel

3 5    #第一个数字表示为之前的运行级别为3,第二个数字表示现在的运行级别为5

#init 切换运行级别,后接对应级别的数字

1.2.2  centos7系统

1、运行级别对应表

init 级别

systemctl target

0

shutdown.target

1

emergency.target

2

rescure.target

3

multi-user.target

4

5

graphical.target

6

2、设置运行级别

命令格式:

systemctl [command] [unit.target]

参数详解:

command:

  • get-default :取得当前的target
  • set-default :设置指定的target为默认的运行级别
  • isolate :切换到指定的运行级别
  • unit.target :5.1表中列出的运行级别

systemctl

命令  说明

systemctl get-default

获得当前的运行级别

systemctl set-default multi-user.target

设置默认的运行级别为 mulit-user

systemctl isolate multi-user.target

在不重启的情况下,切换到运行级别 mulit-user

systemctl isolate graphical.target

在不重启的情况下,切换到图形界面下


注:该表前两行是设置默认运行级别,后两行是设置当前运行级别。

1.3 精简开机系统自启动

重要的必须开机自启动服务:1sshd 2rsyslog 3network 4crond 5sysstat

1.3.1  centos6系统

[root@dsf ~]# LANG=en

[root@dsf ~]# for i in `chkconfig --list|grep "3:on"|awk '{print $1}'|grep -vE "crond|network|sshd|rsyslog|systat|mysqld|nginx|tomcat|redis"`;do chkconfig $i off;done

或者

[root@dsf ~]# chkconfig --list|grep "3:on"|awk '{print $1}'|grep -vE "crond|network|sshd|rsyslog|systat|mysqld|nginx|tomcat|redis"|awk '{print "chkconfig " $1 " off"}'|bash

[root@dsf ~]# chkconfig --list|grep 3:on

crond                0:off      1:off      2:on       3:on       4:on       5:on       6:off

mysqld              0:off      1:off      2:on       3:on       4:on       5:on       6:off

network              0:off      1:off      2:on       3:on       4:on       5:on       6:off

nginx                0:off      1:off      2:on       3:on       4:on       5:on       6:off

redis          0:off      1:off      2:on       3:on       4:on       5:on       6:off

redis-server       0:off      1:off      2:on       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

tomcat               0:off      1:off      2:on       3:on       4:on       5:on       6:off

1.3.2  centos7系统

##感谢朋友给的脚本,直接运行即可

#!/bin/bash

#Author:

#Time:2017-07-11 00:40:21

#Name:chkconfig.sh

#Version:V1.0

####################################

for i in `systemctl list-unit-files |awk '{if ($2=="enabled") print $1}'`

do

systemctl disable $i &>/dev/null

done

cat >/tmp/log<< EOF

crond

dbus-org.freedesktop.NetworkManager.service

dbus-org.freedesktop.nm-dispatcher.service

getty@.service

irqbalance.service

NetworkManager-dispatcher.service

NetworkManager.service

rsyslog.service

sshd.service

systemd-readahead-collect.service

systemd-readahead-drop.service

systemd-readahead-replay.service

tuned.service

default.target

multi-user.target

remote-fs.target

EOF

while read line

do

systemctl enable $line &>/dev/null

done </tmp/log

rm /tmp/log -f

1.4 关闭防火墙

在企业环境中,一般只有配置外网IPlinux服务器才需要开启防火墙,但即使有外网IP,高并发、高流量的业务服务器仍然不能开启防火墙,因为开启后会有较大性能损失,导致网站访问速度很慢,这种情况下只能在前端加更好的硬件防火墙了。

#如果是iptables

[root@master1 ~]# systemctl stop iptables.service

[root@master1 ~]# systemctl disable iptables.service

[root@master1 ~]# systemctl list-unit-files |grep iptables

如果是firewalld

[root@master1 ~]# systemctl stop firewalld

[root@master1 ~]# systemctl disable firewalld

[root@master1 ~]# systemctl list-unit-files |grep firewalld

1.5  linux系统安全最小化

l  安装linux系统最小化,即选包最小化,yum安装软件包也要最小化

l  开机自启动服务最小化

l  操作命令最小化

l  登录linux用户最小化

l  普通用户授权权限最小化,即只给用户必须的管理系统的命令

linux系统文件及目录权限最小化,禁止随意创建、更改、删除文件

l  程序服务运行最小化,即程序服务运行精良不用root身份运行

1.6  Yum源优化并更新系统

yum update -y

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

1.7 提权dsf可以利用sudo

useradd dsf

echo 123456|passwd --stdin dsf

\cp /etc/sudoers /etc/sudoers.ori

echo "dsf  ALL=(ALL) NOPASSWD: ALL " >>/etc/sudoers

tail -1 /etc/sudoers

visudo -c

1.8 更改ssh服务端远程登录的配置

一定哟啊养成操作前备份的好习惯:\cp /etc/ssh/sshd_config /etc/ssh/sshd_config.`date +%F`

1.8.1 更改ssh端口

vim /etc/ssh/sshd_config

Port 52013

systemctl reload sshd.service

1.8.2  ssh连接速度慢优化

sed -i.bak 's@#UseDNS yes@UseDNS no@g;s@^GSSAPIAuthentication yes@GSSAPIAuthentication no@g' /etc/ssh/sshd_config

systemctl reload sshd.service

1.8.3 是否禁止root远程登录

vim /etc/ssh/sshd_config

PermitRootLogin no

#yes(默认允许 ,no表示禁止

1.9  linux中文显示设置

vim /etc/locale.conf

#LANG="en_US.UTF-8"

LANG="zh_CN.UTF-8"

[root@master1 ~]# echo $LANG

zh_CN.UTF-8

1.10 设置linux服务器时间同步

echo '#crond-id-001:time sync by dsf' >>/var/spool/cron/root

echo "*/5 * * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1">>/var/spool/cron/root

crontab -l

##如果机器数量大,那么最好在内网部署一个时间服务器ntp server,然后让自己的网内服务器的时间都与ntp server同步就可以了。

1.11 调整linux系统文件描述符数量

echo '*               -       nofile          65535 ' >>/etc/security/limits.conf

tail -1 /etc/security/limits.conf

ulimit -SHn   65535

ulimit -n

1.12  linux服务器内核参数优化

cat >>/etc/sysctl.conf<<EOF

net.ipv4.tcp_fin_timeout = 2

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_tw_recycle = 1

net.ipv4.tcp_syncookies = 1

net.ipv4.tcp_keepalive_time = 600

net.ipv4.ip_local_port_range = 4000    65000

net.ipv4.tcp_max_syn_backlog = 16384

net.ipv4.tcp_max_tw_buckets = 36000

net.ipv4.route.gc_timeout = 100

net.ipv4.tcp_syn_retries = 1

net.ipv4.tcp_synack_retries = 1

net.core.somaxconn = 16384

net.core.netdev_max_backlog = 16384

net.ipv4.tcp_max_orphans = 16384

#以下参数是对iptables防火墙的优化,防火墙不开会提示,可以忽略不理。

net.nf_conntrack_max = 25000000

net.netfilter.nf_conntrack_max = 25000000

net.netfilter.nf_conntrack_tcp_timeout_established = 180

net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120

net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60

net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120

net.core.wmem_default = 8388608

net.core.rmem_default = 8388608

net.core.wmem_max = 16777216

net.core.rmem_max = 16777216

EOF

sysctl -p

##参数详解

  • file-max:这个参数表示进程可以同时打开的最大句柄数,这个参数直接限制最大并发连接数。
  • tcp_tw_reuse:这个参数设置为1,表示允许将TIME-WAIT状态的socket重新用于新的TCP链接。这个对服务器来说很有意义,因为服务器上总会有大量TIME-WAIT状态的连接。
  • tcp_keepalive_time:这个参数表示当keepalive启用时,TCP发送keepalive消息的频度。默认是7200 seconds,意思是如果某个TCP连接在idle 2小时后,内核才发起probe。若将其设置得小一点,可以更快地清理无效的连接。
  • tcp_fin_timeout:这个参数表示当服务器主动关闭连接时,socket保持在FIN-WAIT-2状态的最大时间。
  • tcp_max_tw_buckets:这个参数表示操作系统允许TIME_WAIT套接字数量的最大值,如果超过这个数字,TIME_WAIT套接字将立刻被清除并打印警告信息。默认是i180000,过多TIME_WAIT套接字会使Web服务器变慢。
  • tcp_max_syn_backlog:这个参数表示TCP三次握手建立阶段接受WYN请求队列的最大长度,默认1024,将其设置大一些可以使出现Nginx繁忙来不及accept新连接的情况时,Linux不至于丢失客户端发起的连接请求。
  • ip_local_port_range:这个参数定义了在UDPTCP连接中本地端口的取值范围。
  • net.ipv4.tcp_rmem:这个参数定义了TCP接受缓存(用于TCP接收滑动窗口)的最小值,默认值,最大值。
  • net.ipv4.tcp_wmem:这个参数定义了TCP发送缓存(用于TCP发送滑动窗口)的最小值,默认值,最大值。
  • netdev_max_backlog:当网卡接收数据包的速度大于内核处理的速度时,会有一个队列保存这些数据包。这个参数表示该队列的最大值。
  • rmem_default:这个参数表示内核套接字接收缓存区默认的大小。
  • wmem_default:这个参数表示内核套接字发送缓存区默认的大小。
  • rmem_max:这个参数表示内核套接字接收缓存区默认的最大大小。
  • wmem_max:这个参数表示内核套接字发送缓存区默认的最大大小。

1.13 隐藏linux版本信息显示

[root@master1 ~]# cat /etc/issue

\S

Kernel \r on an \m

[root@master1 ~]# cat /etc/issue.net

\S

Kernel \r on an \m

执行如下命令清除linux系统版本及内核信息

[root@master1 ~]# > /etc/issue

[root@master1 ~]# > /etc/issue.net

1.14 锁定关键系统文件,防止被提权篡改

要锁定关键系统文件,必须度账号密码文件及启动文件加锁,防止被篡改。命令如下:

chattr +i /etc/passwd /etc/shadow /etc/group /etc/gshadow /etc/inittab

#上锁后,所有用户都不能对文件修改删除

解锁命令如下:

chattr -i /etc/passwd /etc/shadow /etc/group /etc/gshadow /etc/inittab

#如果想要更加安全,可以把chattr改名转移,防止被黑客利用。命令如下

[root@master1 ~]# mv /usr/bin/chattr /usr/bin/dsf

[root@master1 ~]# dsf +i /etc/passwd /etc/shadow /etc/group /etc/gshadow /etc/inittab

[root@master1 ~]# lsattr /etc/inittab   ##查看被上锁文件属性

----i----------- /etc/inittab

1.15 清除多余的系统虚拟账号

因为虚拟用户本身也是无法登录的,因此,此项优化不是必须的。

1.16 grub菜单加密码

grub菜单加密码的目的是防止他人修改grub进行内核等启动设置,以及用单用户模式启动进行破解root密码等操作。实际上此步骤可以在安装系统的过程中设定,安装系统后的具体设定步骤如下。

百度经验:

https://jingyan.baidu.com/article/851fbc3792b9133e1f15ab84.html

1.17 禁止linux系统被ping

[root@master1 ~]# echo "net.ipv4.icmp_echo_ignore_all=1" >> /etc/sysctl.conf

[root@master1 ~]# tail -1 /etc/sysctl.conf

net.ipv4.icmp_echo_ignore_all=1

[root@master1 ~]# sysctl -p

#禁止ping后,我们自己也无法通过ping检查了。还原上述禁止ping 的操作,因为工作中用的比较少。命令如下:

删除/etc/sysctl.conf中的net.ipv4.icmp_echo_ignore_all=1,保存后,并执行如下命令

echo 0 > /proc/sys/net/ipv4/icmp_echo_ignore_all

比较好的策略是通过iptables设置让特定的IP可以ping,如让内网用户ping

iptables -t filter -I INPUT -p icmp --icmp-type 8 -i eth0 -s 10.0.0.0/24 -j ACCEPT

使10.0.0.0/24网段允许ping

1.18 修改环境变量,让有颜色

echo "PS1='\[\e[32;1m\][\u@\h \W]\\$ \[\e[0m\]'"   >>/etc/profile && . /etc/profile



 推荐知识

 历史版本

修改日期 修改人 备注
2020-03-29 21:20:08[当前版本] wyasw 创建版本

  目录
    文艺知识分享平台 -V 4.9.5 -wcp
    京公网安备100012199188号 京ICP备2021030911号