linux初始化
一、配置yum加速
备份并清空/etc/yum.repos.d目录下所有文件
1 2 3 4 5
| curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo yum clean all yum makecache fast yum update -y
|
二、修改hostname
1
| hostnamectl set-hostname s172010136016
|
三、设置时区
1
| timedatectl set-timezone Asia/Shanghai
|
四、磁盘挂载
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| fdisk -l parted /dev/vdb parted>mklabel gpt parted>mkpart primary 0% 100% parted>print parted>quit
fdisk -l
mkfs -t ext4 /dev/vdb1
mkdir -p /pldk cp /etc/fstab /etc/fstab.bak.$(date '+%Y%m%d%H%M%S')
cat >> /etc/fstab << EOF /dev/vdb1 /pldk ext4 defaults 1 2 EOF
|
五、密码安全设置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| vi /etc/login.defs PASS_MAX_DAYS 90 # 有效期90天 PASS_MIN_DAYS 2 # PASS_MIN_LEN 12 PASS_WARN_AGE 7
cat /etc/login.defs | grep PASS cat /etc/pam.d/system-auth | grep pam_tally2 vi /etc/pam.d/system-auth auth required pam_tally2.so onerr=fail deny=5 unlock_time=300 even_deny_root root_unlock_time=60 省总信创等保测评要求 引用pam.tally2.so模块,并配置登录失败次数3次,账户锁定10分钟 auth required pam_tally2.so onerr=fail deny=3 unlock_time=600 even_deny_root root_unlock_time=60
cat /etc/pam.d/system-auth | grep pam_cracklib vi /etc/pam.d/system-auth password requisite pam_cracklib.so difok=4 minlen=12 ucredit=-1 lcredit=-1 dcredit=-1
cat /etc/pam.d/system-auth | grep pam_pwquality vi /etc/pam.d/system-auth password requisite pam_pwquality.so try_first_pass local_users_only retry=3 authtok_type= enforce_for_root
cat /etc/pam.d/system-auth | grep pam_pwhistory vi /etc/pam.d/system-auth password required pam_pwhistory.so enforce_for_root remember=5
cat /etc/security/pwquality.conf | grep -E "minlen|minclass|credit" cat /etc/security/pwquality.conf | grep minclass cat /etc/security/pwquality.conf | grep credit
|
六、设置ssh连接时长
1 2 3 4 5 6 7
| cat /etc/profile | grep TMOUT
vi /etc/profile export TMOUT=300 readonly TMOUT
source /etc/profile
|
七、配置新用户并赋予sudo
1 2 3 4 5
| useradd -m ywfw
chmod -v u+w /etc/sudoers echo 'user1 ALL=(ALL) ALL'>>/etc/sudoers chmod -v u-w /etc/sudoers
|
八、实时同步时间
1 2 3
| yum install -y ntp */30 * * * * /usr/sbin/ntpdate 172.10.254.20 >> /var/log/ntpdate.log 2>&1 & */30 * * * * /usr/sbin/ntpdate ntp.aliyun.com >> /var/log/ntpdate.log 2>&1 &
|
九、禁止root远程登录
1 2 3
| vi /etc/ssh/sshd_config PermitRootLogin no systemctl restart sshd
|
十、关闭selinux
1 2 3 4 5 6 7 8
| #关闭selinux sed -i.bak 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config #检查是否有关闭成功 grep SELINUX=disabled /etc/selinux/config #临时修改 setenforce 0
|
十一、字符集设置
1 2 3 4 5 6 7 8 9 10 11
| #英文字符集 locale set-locale LANG="en_US>UTF-8" echo $LANG #设置系统的区域设置为美国英语(en_US),并指定字符编码为 UTF-8。 #并检查是否有设置成功 #PS:设置中文字符集可以安装中文语言包 yum install -y zh_CN.koi8u locale set-locale LANG="zh_CN.UTF-8"
|
CentOS 7 的优化可以包括多个方面,以下是一些基本的优化步骤:
更新系统:
sudo yum update -y
安装EPEL仓库:
sudo yum install epel-release -y
安装必要的工具:
sudo yum install vim htop net-tools -y
调整文件描述符限制:
编辑/etc/security/limits.conf,添加或修改以下行:
- soft nofile 65535
- hard nofile 65535
- soft nproc 65535
- hard nproc 65535
关闭不必要的服务:
sudo systemctl stop postfix
sudo systemctl disable postfix
设置时区:
sudo timedatectl set-timezone Asia/Shanghai
配置防火墙(如firewalld):
sudo systemctl start firewalld
sudo systemctl enable firewalld
sudo firewall-cmd –permanent –add-service=http
sudo firewall-cmd –reload
优化SSH配置:
编辑/etc/ssh/sshd_config,例如禁用DNS反向解析可以提高SSH登录性能:
UseDNS no
然后重启SSH服务:
sudo systemctl restart sshd
调整内核参数(可选,高级用户):
编辑/etc/sysctl.conf,添加或修改内核参数,例如:
net.ipv4.ip_local_port_range = 1024 65535
应用新的内核参数:
sudo sysctl -p
创建备份和清理系统:
sudo tar cvpzf /backup.tgz –exclude=/backup.tgz –one-file-system /
这些是基本的系统优化步骤,具体根据您的服务器用途和需求可能还需要进行更多的优化。