系统优化的几个设置累积

# 以下都是临时生效,默认1000不需要更改
# 设置的是闲置账号的超时时间
export TMOUT=10 10秒后提示超时时间
# 设置终端history显示条数
export HISTSIZE=5 只显示最近5条信息
# 上面的终端显示对应的是 cat ~/.bash_history 
export HISTFILESIZE=5 该文件只保存5条信息
# 清空历史记录
history -c
# 指定条数删除
history -d 历史记录条属

1.历史命令的时间戳和执行用户添加HISTTIMEFORMAT

# 历史命令显示操作时间

if ! grep HISTTIMEFORMAT /etc/profile; then

echo 'export HISTTIMEFORMAT="%F %T `whoami`  "' >> /etc/profile

fi

2.ssh终端超时时间添加TMOUT

# SSH超时时间

if ! grep "TMOUT=600" /etc/profile &>/dev/null; then

echo "export TMOUT=600" >> /etc/profile

fi

3.关闭防火墙和禁用selinux

#!/bin/bash
#2.关闭selinux
selinuxset()
{
selinux_status=`grep "SELINUX=disabled" /etc/sysconfig/selinux | wc -l`
echo "========================禁用SELINUX========================"
if [ $selinux_status -eq 0 ];then
sed -i "s#SELINUX=enforcing#SELINUX=disabled#g" /etc/sysconfig/selinux
setenforce 0
echo '#grep SELINUX=disabled /etc/sysconfig/selinux'
grep SELINUX=disabled /etc/sysconfig/selinux
echo '#getenforce'
getenforce
else
echo 'SELINUX已处于关闭状态'
echo '#grep SELINUX=disabled /etc/sysconfig/selinux'
grep SELINUX=disabled /etc/sysconfig/selinux
echo '#getenforce'
getenforce
fi
echo "==========================================================="
sleep 2
}

#3.关闭firewalld
firewalldset()
{
echo "=======================禁用firewalld========================"
systemctl stop firewalld.service &> /dev/null
echo '#firewall-cmd --state'
firewall-cmd --state
systemctl disable firewalld.service &> /dev/null
echo '#systemctl list-unit-files | grep firewalld'
systemctl list-unit-files | grep firewalld
echo "==========================================================="
sleep 5
}
read check
case $check in
1)
selinuxset;;
2)
firewalldset;;
*)
exit 1
esac

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

# 禁用selinux

sed -i '/SELINUX/{s/permissive/disabled/}' /etc/selinux/config

# 关闭防火墙

if egrep "7.[0-9]" /etc/redhat-release &>/dev/null; then

systemctl stop firewalld

systemctl disable firewalld

elif egrep "6.[0-9]" /etc/redhat-release &>/dev/null; then

service iptables stop

chkconfig iptables off

fi

4.修改字符集

.字符编码优化
出现乱码的原因:

系统字符集设置有问题
远程软件字符集设置有问题
文件编写字符集和系统查看的字符集不统一
查看系统默认编码信息 echo $LANG


永久修改:
方法一:更加优先
在/etc/profile文件中添加export LANG='zh_CN.UTF-8
方法二:
在/etc/locale.conf文件中直接修改
补充:一条命令即临时设置,又永久设置
localectl set-locale LANG=zh_CN.GBK

以下是脚本:

#.修改字符集

localeset() {  

echo "========================修改字符集========================="  

cat > /etc/locale.conf << END
LANG="zh_CN.UTF-8"
#LANG="en_US.UTF-8"
SYSFONT="latarcyrheb-sun16"
END

source /etc/locale.conf  

echo "#cat /etc/locale.conf"  

cat /etc/locale.conf  

echo "完成修改字符集"  

echo "==========================================================="  

sleep 2

}

5.修改系统时间::

#!/bin/bash

package="ntpdate"

info=$(rpm -q ${package})

check_pkgs(){

if [ ! -e ${info} ]

then

  echo "ntpdate already exists!"

else

  echo "start installation!"

  yum clean all > /dev/null 2>&1

  yum update -y && yum install -y ${package} > /dev/null 2>&1

fi

}

modify_time(){

echo "开始修改时间"

rm -rf /etc/localtime && ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

/usr/sbin/ntpdate cn.pool.ntp.org > /dev/null 2>&1 && hwclock -w

}

check_pkgs

modify_time

#6.修改文件描述符

limitset() {  

echo "======================修改文件描述符======================="  

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

ulimit -SHn 65535  

echo "#cat /etc/security/limits.conf"  

cat /etc/security/limits.conf  

echo "#ulimit -Sn ; ulimit -Hn"  

ulimit -Sn ; ulimit -Hn 

echo  "完成修改文件描述符" 

echo "==========================================================="  

sleep 2

}

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

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

#ninth: setting max file num is 65535
if ! grep "* soft nofile 65535" /etc/security/limits.conf &> /dev/null;then
cat >> /etc/security/limits.conf << EOF
* soft nofile 65535
* hard nofile 65535
EOF
fi

调整Linux的最大文件打开数

在/etc/security/limit.conf中添加如下设置:

[brian@Master ~]$ sudo nano /etc/security/limit.conf
* soft nofile 65535
* hard nofile 65535
另外再去修改/etc/rc.local
[brian@Master ~]$ sudo nano /etc/rc.local
ulimit -SHn 65535

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

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

7.使Xshell软件远程 连接速度加快

第一个步骤:修改ssh服务配置文件
vi /etc/ssh/sshd_config
79 GSSAPIAuthentication no
115 UseDNS no

第二个步骤:修改hosts文件
[root@oldboyedu ~]# vi /etc/hosts
10.0.0.200 oldboyedu.com

第三个步骤:重启ssh远程服务
systemctl restart sshd

命令形式:

sed -i 's/#UseDNS yes/UseDNS no/g' /etc/ssh/sshd_config
grep 'UseDNS no' /etc/ssh/sshd_config
sed -i 's/GSSAPIAuthentication yes/GSSAPIAuthentication no/g' /etc/ssh/sshd_config
grep 'GSSAPIAuthentication no' /etc/ssh/sshd_config
systemctl restart sshd

 

8yum源


默认国外的yum源(软件仓库)比较慢,所以换成国内的。
备份
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
下载新的CentOS-Base.repo 到/etc/yum.repos.d/
CentOS 5
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo
或者
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo
CentOS 6
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
或者
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
CentOS 7
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
或者
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
之后运行yum makecache生成缓存,此步骤可以不执行。

9、锁定关键文件系统

复制代码
加锁,不可修改加锁文件
[root@jokerpro ~]# chattr +i /etc/passwd
[root@jokerpro ~]# lsattr /etc/passwd
----i--------e-- /etc/passwd
去锁,可以修改文件
[root@jokerpro ~]# chattr -i /etc/passwd 
[root@jokerpro ~]# lsattr /etc/passwd   
-------------e-- /etc/passwd
复制代码

使用chattr命令后,为了安全我们需要将其改名

mv /usr/bin/chattr /usr/bin/任意名称

chattr只有超级权限的用户才具有使用该命令的权限,通过chattr命令修改属性能够提高系统的安全性,但是它并不适合所有的目录。chattr命令不能保护/、/dev、/tmp、/var目录。lsattr命令是显示chattr命令设置的文件属性。

  • a即append,设定该参数后,只能向文件中添加数据,而不能删除,多用于服务器日志文 件安全,只有root才能设定这个属性。
  • i设定文件不能被删除、改名、设定链接关系,同时不能写入或新增内容。i参数对于文件 系统的安全设置有很大帮助。
#用chattr命令防止系统中某个关键文件被修改,可以通过+i来设置。 
chattr +i /etc/fstab 
#让某个文件只能往里面追加内容,不能删除,一些日志文件适用于这种操作
chattr +a /data1/user_act.log
#锁定关键系统文件开始
chattr +i /etc/passwd
chattr +i /etc/inittab
chattr +i /etc/group
chattr +i /etc/shadow
chattr +i /etc/gshadow
#锁定关键系统文件结束

去除linux版本信息

因为特定的版本存在特定的bug,有些黑客依据版本信息可以黑我们的电脑。所以最好把版本信息给删除掉。

#通过重定向的方法覆盖掉原来的内容
#基于debian系列
>/etc/debian_version
#基于centos版本
>/etc/redhat-release
>/etc/issue

 

 

 

 

10.FTP上传下载用户批量设置

11.主机禁ping的设置

12.ssh端口的修改及普通用户的提权设置及root登陆的限制

13.swap交换空间的设置:

现在一般1个G的内存可修改为10, 2个G的可改为5, 甚至是0。具体这样做:
  1.查看你的系统里面的swappiness
  $ cat /proc/sys/vm/swappiness
  不出意外的话,你应该看到是 60
  2.修改swappiness值为10
  $ sudo sysctl vm.swappiness=10
  但是这只是临时性的修改,在你重启系统后会恢复默认的60,为长治久安,还要更进一步:
  $ sudo gedit /etc/sysctl.conf
  在这个文档的最后加上这样一行:
  vm.swappiness=10
  然后保存,重启。ok,你的设置就生效了;也可以sysctl -p立即生效,不用重启

如果内存够大,应当告诉 linux 不必太多的使用 SWAP 分区, 可以通过修改 swappiness 的数值。swappiness=0的时候表示最大限度使用物理内存,然后才是 swap空间,swappiness=100的时候表示积极的使用swap分区,并且把内存上的数据及时的搬运到swap空间里面。

[root@zabbix-server ~]# swapoff -a  #禁用swap,就彻底不使用swap交换空间了,完全使用物理内存

[root@zabbix-server ~]# free -m
total used free shared buff/cache available
Mem: 1982 239 1548 10 194 1565
Swap: 0 0 0

14.关闭磁盘的io功能

关闭写磁盘I/O功能

Linux文件默认有三个时间,分别如下:

  • atime 对此文件的访问时间
  • ctime 此文件inode发生变化时间
  • mtime 此文件的修改时间

如果一个linux服务器很多小文件和琐碎的资源文件,通常没有你要记录文件的访问时间,这样可以减少写磁盘的I/O。通过如下配置:

[brian@Master ~]$ sudo nano /etc/fstab
#在包含大量小文件的分区使用noatime和nodiratime
/dev/pics ext4 noatime,nodiratime 0 0

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

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

Linux基础优化与安全重点小结


不用root登录管理系统,而以普通用户登录通过sudo授权管理。
更改默认的远程连接SSH服务端口,禁止root用户远程连接,甚至要更改SSH服务只监听内网IP。
定时自动更新服务器的时间,使其和互联网时间同步。
配置yum更新源,从国内更新源下载安装软件包。
关闭SELinux及iptables(在工作场景中,如果有外部IP一般要打开iptables,高并发高流量的服务器可能无法开启)。
调整文件描述符的数量,进程及文件的打开都会消耗文件描述符数量。
定时自动清理邮件临时目录垃圾文件,防止磁盘的inodes数被小文件占满(注意Centos6和Centos5要清除的目录不同)。
精简并保留必要的开机自启动服务(如crond、sshd、network、rsyslog、sysstat)。
Linux内核参数优化/etc/sysctl.conf,执行sysctl -p生效。
更改系统字符集为“zh_CN.UTF-8”,使其支持中文,防止出现乱码问题。
锁定关键系统文件如/etc/passwd、/etc/shadow、/etc/group、/etc/gshadow、/etc/inittab,处理以上内容后把chattr、lsattr改名为luffy,转移走,这样就安全多了。
清空/etc/issue、/etc/issue.net,去除系统及内核版本登录前的屏幕显示。
清除多余的系统虚拟用户账号。
为grub引导菜单加密码。
禁止主机被ping。
打补丁并升级有已知漏洞的软件。 新系统 yum –y install 已经在线上用的服务器 web服务器能够停止。

posted @ 2023-03-24 14:56  往事已成昨天  阅读(74)  评论(0编辑  收藏  举报