1.概览
CentOS系统安装之后并不能立即投入生产环境使用,往往需要先经过我们运维人员的优化才行。在此写几点关于Linux系统安装后的基础优化操作。
注意:本次优化都是基于CentOS(7.7)
2.优化目录
配置yum源并安装基础软件
修改主机地址,主机名dns等信息
修改默认的端口,禁止root用户登录
编辑sudo,以普通用户管理
关闭selinux
配置时间同步
锁定系统关键文件,防止提权篡改
为Grub菜单增加密码
调整文件描述符数量,进程及文件打开都会消耗文件描述符
精简并保留必要的开机自启动服务
linux内核优化/etc/sysctl.conf
a.安装常用软件
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
#PS:yum repolist 列出yum源信息;把yum源换为阿里的源,当然也可以换成清华或者是别的源
例如:wget vim lrzsz bash-completion telnet nmap等等
yum -y install wget vim lrzsz bash-completion telnet nmap
b.修改主机地址,主机名dns等信息
[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0 #网卡名字
BOOTPROTO=static #静态IP地址获取状态 如:DHCP表示自动获取IP地址
IPADDR=172.16.1.113 #IP地址
NETMASK=255.255.255.0 #子网掩码
ONBOOT=yes #引导时是否激活
GATEWAY=172.16.1.1
[root@localhost ~]# vim /etc/hostname
test #这个是主机名
我们也可以用 hostnamectl set-hostname test 来永久修改主机名,重新登录生效
修改DNS
[root@localhost ~]# vi /etc/resolv.conf #修改DNS信息
nameserver 114.114.114.114
nameserver 8.8.8.8
[root@localhost ~]# cat /etc/resolv.conf #查看修改后的DNS信息
nameserver 114.114.114.114
nameserver 8.8.8.8
[root@localhost ~]# systemctl restart network #重启网卡,生效
重启网卡,也可以用下面的命令
[root@localhost ~]# /etc/init.d/network restart
c.修改默认的端口
默认ssh配置文件是/etc/ssh/sshd_config
大约在17行左右,有一个参数port 22,默认是注释的,我们打开注释,把22修改为别的端口即可
注意:端口不要冲突,先备份。
[root@test ~]# cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
[root@test ~]# vim /etc/ssh/sshd_config
Port 22022 #ssh连接默认的端口
PermitRootLogin no #root用户黑客都知道,禁止它远程登录
PermitEmptyPasswords no #禁止空密码登录
UseDNS no #不使用DNS
[root@test ~]# /etc/init.d/sshd reload #从新加载配置
[root@test ~]# netstat -lnt #查看端口信息
[root@test ~]# lsof -i tcp:22022
d.编辑sudo,以普通用户管理
[root@test ~]# useradd test
[root@test ~]# echo "123456"|passwd --stdin test
[root@test ~]# visudo
在root ALL=(ALL) ALL此行下,添加如下内容
test ALL=(ALL) ALL
e.关闭selinux
#临时关闭和查看
[root@test ~]# setenforce 0
[root@test ~]# getenforce
Disabled
#永久关闭
把selinux的值修改为disabled即可
[root@test ~]# cat /etc/selinux/config
SELINUX=disabled
f.配置时间同步
#设置每5分钟同步一次
[root@test ~]# echo '#time sync by dxy' >>/var/spool/cron/root
[root@test ~]# echo '*/5 * * * * /usr/sbin/ntpdate pool.ntp.org >/dev/null 2>&1' >>/var/spool/cron/root
[root@test ~]# crontab -l
#time sync by dxy
*/5 * * * * /usr/sbin/ntpdate pool.ntp.org >/dev/null 2>&1
g.锁定系统关键文件,防止提权篡改
[root@test ~]# chattr +i /etc/passwd
[root@test ~]# chattr +i /etc/inittab
[root@test ~]# chattr +i /etc/group
[root@test ~]# chattr +i /etc/shadow
[root@test ~]# chattr +i /etc/gshadow
h.为Grub菜单增加密码
在RHEL 7.2和Centos 7(及更高版本)上,GRUB 2使用grub2-setpassword命令提供密码保护。
[root@test ~]# grub2-setpassword
Enter password:
Confirm password:
这个过程创建了一个/boot/grub2/user.cfg配置文件,其中包含已经加密的密码。这个密码的用户是root,在/boot/grub2/grub.cfg文件中已经定义了。通过此更改,在引导期间修改引导条目需要指定root用户名和密码。
j.调整文件描述符数量,进程及文件打开都会消耗文件描述符
#修改编辑/etc/security/limits.conf 这个文件
echo '* - nofile 65535 ' >>/etc/security/limits.conf
tail -1 /etc/security/limits.conf
#查看ulimit
ulimit -n
k.精简并保留必要的开机自启动服务
服务 状态 是否保留 描述
abrt-ccpp.service enabled x
abrt-oops.service enabled x
abrt-vmcore.service enabled x
abrt-xorg.service enabled x
abrtd.service enabled x 调试相关的服务
auditd.service enabled x 一次性任务服务
autovt@.service enabled √ 登录相关
crond.service enabled √ 定时任务相关
getty@.service enabled √ tty窗口
irqbalance.service enabled √ 多核cpu调度
kdump.service enabled x 内核出现问题dump信息保存
microcode.service enabled x inter的cpu会提供一个外挂的微指令集提供系统运行
rsyslog.service enabled √ 日志服务
sshd.service enabled √ ssh服务
systemd-readahead-collect.service enabled √ 系统服务
systemd-readahead-drop.service enabled √ 系统服务
systemd-readahead-replay.service enabled √ 系统服务
tuned.service enabled √ 服务器能耗管理
default.target enabled √
multi-user.target enabled √
remote-fs.target enabled √
runlevel2.target enabled √
runlevel3.target enabled √
runlevel4.target enabled √