跳至主要內容

CentOS7系统初始化设置

知识库编程技巧CentOSLinux大约 4 分钟

镜像地址

# 官方镜像:http://isoredirect.centos.org/centos/
# 官方镜像:http://isoredirect.centos.org/centos/7/isos/x86_64/
wget http://mirrors.163.com/centos/7.9.2009/isos/x86_64/CentOS-7-x86_64-DVD-2009.iso

网络设置

静态 IP 地址:192.16.18.100
[root@hub ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
#BOOTPROTO=dhcp
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=b3d1393d-059d-49ea-a12b-9aa9e4c9bc2c
DEVICE=ens33
ONBOOT=yes
NM_CONTROLLED=no
IPADDR=192.16.18.100
NETMASK=255.255.255.0
GATEWAY=192.16.18.2
DNS1=114.114.114.114
DNS2=8.8.8.8





 












 
 
 


系统参数调整

# 关闭SELINUX 临时关闭
setenforce 0
# 关闭SELINUX 永久关闭需要修改SELINUX的配置文件
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

# 打开文件数调整 (按需调整)
echo "* soft nofile 65536" >> /etc/security/limits.conf
echo "* hard nofile 65536" >> /etc/security/limits.conf

更改 YUM 源

yum install -y vim  wget
## 备份
# 备份全部
mkdir -p /etc/yum.repos.d/bak && mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak
# 或者只备份 CentOS-Base.repo 文件
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

## 下载新的CentOS-Base.repo 到/etc/yum.repos.d/
# CentOS 7  (二选一)
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.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 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

## 配置epel源
# epel(RHEL 7)
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
# epel(RHEL 6)
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
# epel(RHEL 5)
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-5.repo


## 非阿里云ECS用户无法解析主机"mirrors.cloud.aliyuncs.com"
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
## 清除缓存 | 生成缓存 | 更新源
yum clean all && yum makecache && yum update && yum repolist 




 



 










 






 
 
 

基本软件工具安装

# jp:json格式的工具 | bash-completion: tab键补全命令
yum install -y vim curl wget telnet lrzsz unzip jq gcc tree sysstat libseccomp bash-completion yum-utils bzip2
yum install -y policycoreutils-python openssh-server openssh-clients cronie
yum install -y iptables conntrack ipvsadm ipset net-tools lsof iproute bridge-utils bind-utils
yum install -y epel-release xorg-x11-xauth htop dstat glances lftp
yum install -y ntpdate ntp

升级 Linux 内核版本

rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-4.el7.elrepo.noarch.rpm
# 安装内核  kernel官网:https://kernel.org/
yum --enablerepo=elrepo-kernel install -y kernel-lt
# 查询已安装的内核
rpm -qa | grep kernel
# 查看默认启动项
awk -F\' '$1=="menuentry " {print $2}' /etc/grub2.cfg
# 设置开机从新内核启动
#grub2-set-default 'CentOS Linux (4.4.189-1.el7.elrepo.x86_64) 7 (Core)'
# 上述命令不生效,可执行下面的命令设置默认启动
# 默认启动的顺序是从0开始(CentOS Linux (3.10.0-1127.el7.x86_64) 7),新内核是从头插入,所以需要选择0
grub2-set-default 0
# 注意:设置完内核后,需要重启服务器才会生效。
reboot
# 查询内核 4.4.249-1.el7.elrepo.x86_64
uname -r
## 通过 yum remove 命令
# 查看系统安装了哪些内核包
rpm -qa | grep kernel
# 使用yum remove 或rpm -e 删除无用内核
yum remove kernel-tools-libs-3.10.0-1127.el7.x86_64

## 通过 yum-utils 工具
# 如果安装的内核不多于 3 个,yum-utils 工具不会删除任何一个。只有在安装的内核大于 3 个时,才会自动删除旧内核。
yum install yum-utils
package-cleanup --oldkernels

时间同步

## 手动下载安装包地址
# http://mirror.centos.org/centos/7/os/x86_64/Packages/ntp-4.2.6p5-29.el7.centos.2.x86_64.rpm
# http://mirror.centos.org/centos/7/os/x86_64/Packages/ntpdate-4.2.6p5-29.el7.centos.2.x86_64.rpm
yum -y install ntp ntpdate
systemctl start ntpd && systemctl enable ntpd
# 时区不对的更改时区
timedatectl set-timezone Asia/Shanghai
# ntpdate cn.pool.ntp.org
ntpdate time1.aliyun.com
#将当前的 UTC 时间写入硬件时钟
timedatectl set-local-rtc 0
hwclock --systohc
hwclock -w
date
## 查看同步结果
ntpq -pn
## 重启依赖于系统时间的服务
systemctl restart rsyslog
systemctl restart crond

修改 hostname

# 查看机器的hostname
hostname
# 设置hostname
hostnamectl set-hostname dev.alot.pw
# 设置hosts文件
echo '192.16.18.100 dev.alot.pw' >> /etc/hosts

扩展安装

# axel安装:多线程下载工具
wget http://downloadib01.fedoraproject.org/pub/epel/7/x86_64/Packages/a/axel-2.4-9.el7.x86_64.rpm
rpm -ivh axel-2.4-9.el7.x86_64.rpm

全局环境变量

vi /etc/profile
# 添加如下内容, 注意修改自己的安装路径
export NODE_HOME=/usr/local/lib/nodejs
export JAVA_HOME=/usr/local/lib/jdk1.8.0_261
export MAVEN_HOME=/usr/local/lib/maven-3.6.3
export PATH=$JAVA_HOME/bin:$NODE_HOME/bin:$MAVEN_HOME/bin:$PATH
export CLASSPATH=$JAVA_HOME/jre/lib/ext:$JAVA_HOME/lib/toos.jar
# 配置生效
source /etc/profile

卸载 CentOS7 系统自带 mariadb

# 查看系统自带的Mariadb (mariadb-libs-5.5.68-1.el7.x86_64)
rpm -qa|grep -i mariadb
# 卸载系统自带的Mariadb
rpm -qa | grep mariadb | xargs rpm -e --nodeps
# 删除etc目录下的my.cnf
rm /etc/my.cnf

升级 git 版本

git version
## 如果已安装1.8版本的git请卸载
yum remove git
yum remove git-*

## 通过`End Point`库,参考 'https://packages.endpointdev.com'
yum install https://packages.endpointdev.com/rhel/7/os/x86_64/endpoint-repo.x86_64.rpm
yum install git
## git version 2.36.0
git --version


## 编译安装
yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel
yum install gcc perl-ExtUtils-MakeMaker
yum -y install wget
mkdir ~/downloads && cd ~/downloads && wget https://github.com/git/git/archive/v2.36.0.tar.gz
tar -xvf v2.36.0.tar.gz && rm -f v2.36.0.tar.gz && cd git-2.36.0
make configure && ./configure --prefix=/usr && make && make install
git --version