新装CentOS7基础优化

信息查看

cat /etc/centos-release        #查看版本
uname -r                       #查看内核版本
hostnamectl set-hostname name  #改计算机名

配置网络

vi /etc/sysconfig/network-scripts/ifcfg-ens33


# 最小配置就这么多,其他的都可以不配置
TYPE=Ethernet			#设置网卡类型,“Ethernet”表示以太网
DEVICE=ens33			#设置网卡的名称
ONBOOT=yes				#设置网卡是否在 Linux 操作系统启动时激活
BOOTPROTO=static		#设置网卡的配置方式,“static”表示使用静态IP地址,“dhcp”时表示动态获取地址
IPADDR=192.168.80.3		#设置网卡的 IP 地址
NETMASK=255.255.255.0	#设置网卡的子网掩码
GATEWAY=192.168.80.2	#设置网卡的默认网关地址
DNS1=192.168.80.2		#设置DNS服务器的 IP 地址

系统源优化

首先备份源 /ect/yum.repos.d/CentOS-Base.repo,在安装之前我们先必须安装wget

yum -y install wget yum-utils epel-release
mv  /etc/yum.repos.d/CentOS-Base.repo  /etc/yum.repos.d/CentOS-Base.repo.bak

替换为阿里源

这里是centos7和epel的源,后续还可能用到python模块源

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

如果你需要使用docker

wget -O /etc/yum.repos.d/docker-ce.repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

安装python更改国内源

yum install python36 -y
mkdir ~/.pip
cd ~/.pip
vim pip.conf

[global]
trusted-host=mirrors.aliyun.com
index-url=http://mirrors.aliyun.com/pypi/simple/

清楚缓存生成新缓存

yum clean all && yum makecache

系统更新

只升级包

yum -y upgrade

升级所有包,不改变软件设置和系统设置,系统版本升级,内核不改变

全升级

yum -y update

升级所有包,改变软件设置和系统设置,系统版本内核都升级

生产服务器千万别这么干,总之不建议使用update。如果这么服务器上部署了好多服务,这么干你小则罚款记过,大则离职跑路。

详细请看这里:

必装依赖组件

以下是一些必装依赖、软件、服务,可以根据你的实际情况选择安装。

gcc glibc gcc-c++ make cmake net-tools screen vim lrzsz tree dos2unix lsof tcpdump bash-completion
wget ntp setuptool psmisc openssl openssl-devel bind-utils traceroute

一键安装

yum -y install gcc glibc gcc-c++ make cmake net-tools screen vim lrzsz tree dos2unix lsof tcpdump bash-completion wget ntp setuptool psmisc openssl openssl-devel bind-utils traceroute

图形化配置工具

图形化配置网络/服务/防火墙

yum -y install setuptool ntsysvsystem-config-securitylevel-tui networkManager-tui authconfig-gtk system-config-keyboard bind-utils

关闭防火墙

搭建的时候关掉,搭建号之后再开启,要不然很痛苦

systemctl disable firewalld
systemctl stop firewalld
sed -i 's/^ *SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

登录的优化

更改远程登录端口

vim /etc/ssh/sshd_config

Port 22     #之前的是22
Port 22222  #这行是新增的

SSH超时时间

vim /etc/ssh/sshd_config

ClientAliveInterval 60    #server每隔60秒发送一次请求给client,然后client响应,从而保持连接
ClientAliveCountMax 3     #server发出请求后,客户端没有响应得次数达到3,就自动断开连接,正常情况下,client不会不响应

禁止root远程登录

只能远程登录普通用户,然后su到root的权限,不能让root直接远程登录

vim /etc/ssh/sshd_config。
找到 PermitRootLogin
改为 PermitRootLogin no
service sshd restart

root用户密码输入错误三次,锁定账户一段时间

vim /etc/pam.d/sshd
增加
auth required pam_tally.so deny=3 unlock_time=5

设置时区并同步时间

timedatectl set-timezone 'Asia/Shanghai'
ntpdate ntp1.aliyun.com

历史命令显示操作时间

vim /etc/profile 
export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S  "
source /etc/profile

禁止定时任务向发送邮件

sed -i 's/^MAILTO=root/MAILTO=""/' /etc/crontab