准备工作
- 系统版本信息
cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
- 内核信息
uname -a
Linux x1 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
系统用户优化
- 创建用户
useradd dyp
清除用户
userdel 用户名
查看用户
id 用户名
- 创建用户密码
- root用户创建密码
passwd dyp
Changing password for user dyp.
New password: #输入新密码
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: #再次输入密码
passwd: all authentication tokens updated successfully.
- 普通用户创建密码
su - dyp #切换用户
passwd #修改密码
Changing password for user dyp.
Changing password for dyp.
(current) UNIX password:#输入旧密码
New password:#输入新密码
Retype new password:#再次输入新密码
passwd: all authentication tokens updated successfully.
- 免交互修改密码
echo 密码 | passwd --stdin 用户名
- 批量修改密码
for user(变量) in 用户名 用户名 用户名 ; do echo 123456(密码) | passwd --stdin $user ; done
系统命令提示符优化
- 优化提示符显示信息
PS1 #用于设置系统命令提示符
- 临时添加时间
echo $PS1
[\u@\h \W]\$
PS1="[\u@\h \t \W]\\$ "
[root@wy 12:29:02 ~]#
- 永久添加时间
vim /etc/profile #变量修改
source /etc/profile #重新加载
- 修改命令提示符颜色
vim /etc/profile
RED='\[\033[01;31m\]'
Yello='\[\033[01;33m\]'
Green='\[\033[01;32m\]'
End='\033[0m\]'
PS1="[$RED\u$End@$Yello\h$End $Green\W$End]\\$ "
source /etc/profile
重新加载
系统下载软件优化
- yum源优化
- Base yum源优化
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
- epel yum源优化 (企业扩展yum仓库) Extra Packages for Enterprise Linux
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
或者
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
- 下载
yum install -y vim wget tree telnet nc nmap net-tools bash-completion(补全参数软件)
系统安全优化(关闭)
- 安全服务: firewalld(防火墙)
关闭防火墙服务
systemctl stop firewalld.service #关闭firewalld防火墙服务
systemctl status firewalld.service #关闭开机启动firewalld防火墙服务
- 安全服务: selinux (限制root用户行为)
- 临时关闭
setenforce 0 #关闭selinux服务
getenforce #查看selinux服务
Permissive #关闭服务 0
Enforcing #开启服务 1
- 永久关闭
vim /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=enforcing #开启
# SELINUXTYPE= can take one of three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
修改
SELINUX=permissive — 警告
或
SELINUX=disabled — 关闭
- 重启后生效
时间优化
- 显示系统的当前时间和日期
timedatectl status
- 查看当前时区
timedatectl status | grep Time
- 查看可用时区
timedatectl list-timezones
- 设置本地时区
timedatectl set-timezone "Asia/Shanghai"
设置时区亚洲上海
- 推荐使用和设置协调世界时,即UTC。
timedatectl set-timezone UTC
- 启动关闭ntp
Network Time Protocol(网络时间协议)
用于同步系统时间
systemctl status chronyd.service
查看时间同步服务
yum install -y chrony
安装时间同步软件(没有的安装)
timedatectl set-ntp true
开启ntp服务
timedatectl set-ntp false
关闭ntp服务
- 修改时间日期
关闭ntp才可以修改时间日期
“-” 日期格式
“:” 时间格式
timedatectl set-time 07:07:07
修改时间
timedatectl set-time 2019-7-7
修改日期
timedatectl set-time "2019-7-7 7:7:7"
修改日期和时间
- 时间同步
开启ntp才可以使用
ntpdate ntp1.aliyun.com
- 设置硬件时间信息
timedatectl set-local-rtc 0
关闭rtc功能
timedatectl set-local-rtc 1
开启rtc功能
远程连接优化
修改远程服务配置文件:
vim /etc/ssh/sshd_config
:sed nu 显示行号
GSSAPIAuthentication no #78行 修改远程认证方式
UseDNS no #115行 修改远程访问时根据IP地址进行反向解析过程
systemctl restart sshd
重启sshd服务
文件描述符游湖
每个进程可以打开最大文件数
- 临时
[root@7 ~]# ulimit -n 777 # 最大65535
[root@7 ~]# ulimit -n
777
- 永久
[root@7 ~]# echo '* - nofile 65535 ' >>/etc/security/limits.conf
内核优化
[root@7 ~]# vim /etc/sysctl.conf # 配置文件
[root@7 ~]# sysctl -p # 加载