Linux系統(tǒng)初始化配置指南
1.禁用SELinux
sed -ri.bak 's/^(SELINUX=).*/\1disabled/' /etc/selinux/config
2.關(guān)閉防火墻
Centos系統(tǒng)
rpm -q firewalld &> /dev/null && systemctl disable --now firewalld &> /dev/null
Ubuntu系統(tǒng)
dpkg -s ufw &> /dev/null && systemctl disable --now ufw &> /dev/null
3.設(shè)置主機名
hostnamectl set-hostname ${HOST}4.修改網(wǎng)卡名
Centos系統(tǒng)
#修改網(wǎng)卡名稱配置文件
sed -ri.bak '/^GRUB_CMDLINE_LINUX=/s@"$@ net.ifnames=0"@' /etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg >& /dev/null
#修改網(wǎng)卡文件名
ETHNAME=`ip addr | awk -F"[ :]" '/^2/{print $3}'`
mv /etc/sysconfig/network-scripts/ifcfg-${ETHNAME} /etc/sysconfig/network-scripts/ifcfg-eth0Ubuntu系統(tǒng)
#修改網(wǎng)卡名稱配置文件 sed -ri.bak '/^GRUB_CMDLINE_LINUX=/s@"$@ net.ifnames=0"@' /etc/default/grub grub-mkconfig -o /boot/grub/grub.cfg >& /dev/null
5.修改IP地址和網(wǎng)關(guān)地址
Centos系統(tǒng)
cat > /etc/sysconfig/network-scripts/ifcfg-eth0 <<-EOF
DEVICE=eth0
NAME=eth0
BOOTPROTO=none
ONBOOT=yes
IPADDR=${IP}
PREFIX=24
GATEWAY=${GATEWAY}
DNS1=223.5.5.5
DNS2=180.76.76.76
EOFUbuntu系統(tǒng)
cat > /etc/netplan/01-network-manager-all.yaml <<-EOF
network:
version: 2
renderer: networkd
ethernets:
eth0:
addresses: [${IP}/24]
gateway4: ${GATEWAY}
nameservers:
search: [neteagles.cn, neteagles.com]
addresses: [223.5.5.5, 180.76.76.76]
EOF臨時設(shè)置DNS
cat > /etc/resolv.conf <<EOF nameserver 223.5.5.5 # 阿里云 DNS nameserver 180.76.76.76 # 百度 DNS nameserver 119.29.29.29 # 騰訊云 DNS EOF
6.設(shè)置軟件包倉庫
Centos8系統(tǒng)
#!/bin/bash
mkdir /etc/yum.repos.d/backup
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup
cat > /etc/yum.repos.d/base.repo <<EOF
[BaseOS]
name=BaseOS
baseurl=https://mirrors.aliyun.com/centos/\$releasever/BaseOS/\$basearch/os/
https://mirrors.huaweicloud.com/centos/\$releasever/BaseOS/\$basearch/os/
https://mirrors.cloud.tencent.com/centos/\$releasever/BaseOS/\$basearch/os/
https://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/BaseOS/\$basearch/os/
http://mirrors.163.com//centos/\$releasever/BaseOS/\$basearch/os/
http://mirrors.sohu.com/centos/\$releasever/BaseOS/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
[AppStream]
name=AppStream
baseurl=https://mirrors.aliyun.com/centos/\$releasever/AppStream/\$basearch/os/
https://mirrors.huaweicloud.com/centos/\$releasever/AppStream/\$basearch/os/
https://mirrors.cloud.tencent.com/centos/\$releasever/AppStream/\$basearch/os/
https://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/AppStream/\$basearch/os/
http://mirrors.163.com/centos/\$releasever/AppStream/\$basearch/os/
http://mirrors.sohu.com/centos/\$releasever/AppStream/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
[EPEL]
name=EPEL
baseurl=https://mirrors.aliyun.com/epel/\$releasever/Everything/\$basearch/
https://mirrors.huaweicloud.com/epel/\$releasever/Everything/\$basearch/
https://mirrors.cloud.tencent.com/epel/\$releasever/Everything/\$basearch/
https://mirrors.tuna.tsinghua.edu.cn/epel/\$releasever/Everything/\$basearch/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-\$releasever
[extras]
name=extras
baseurl=https://mirrors.aliyun.com/centos/\$releasever/extras/\$basearch/os/
https://mirrors.huaweicloud.com/centos/\$releasever/extras/\$basearch/os/
https://mirrors.cloud.tencent.com/centos/\$releasever/extras/\$basearch/os/
https://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/extras/\$basearch/os/
http://mirrors.163.com/centos/\$releasever/extras/\$basearch/os/
http://mirrors.sohu.com/centos/\$releasever/extras/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
enabled=1
[centosplus]
name=centosplus
baseurl=https://mirrors.aliyun.com/centos/\$releasever/centosplus/\$basearch/os/
https://mirrors.huaweicloud.com/centos/\$releasever/centosplus/\$basearch/os/
https://mirrors.cloud.tencent.com/centos/\$releasever/centosplus/\$basearch/os/
https://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/centosplus/\$basearch/os/
http://mirrors.163.com/centos/\$releasever/centosplus/\$basearch/os/
http://mirrors.sohu.com/centos/\$releasever/centosplus/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
EOF
dnf clean all
dnf repolistCentos7系統(tǒng)
#!/bin/bash
mkdir /etc/yum.repos.d/backup
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup
cat > /etc/yum.repos.d/base.repo <<EOF
[base]
name=base
baseurl=https://mirrors.aliyun.com/centos/\$releasever/os/\$basearch/
https://mirrors.huaweicloud.com/centos/\$releasever/os/\$basearch/
https://mirrors.cloud.tencent.com/centos/\$releasever/os/\$basearch/
https://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/os/\$basearch/
http://mirrors.163.com/centos/\$releasever/os/\$basearch/
http://mirrors.sohu.com/centos/\$releasever/os/\$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-\$releasever
[epel]
name=epel
baseurl=https://mirrors.aliyun.com/epel/\$releasever/\$basearch/
https://mirrors.huaweicloud.com/epel/\$releasever/\$basearch/
https://mirrors.cloud.tencent.com/epel/\$releasever/\$basearch/
https://mirrors.tuna.tsinghua.edu.cn/epel/\$releasever/\$basearch/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-\$releasever
[extras]
name=extras
baseurl=https://mirrors.aliyun.com/centos/\$releasever/extras/\$basearch/
https://mirrors.huaweicloud.com/centos/\$releasever/extras/\$basearch/
https://mirrors.cloud.tencent.com/centos/\$releasever/extras/\$basearch/
https://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/extras/\$basearch/
http://mirrors.163.com/centos/\$releasever/extras/\$basearch/
http://mirrors.sohu.com/centos/\$releasever/extras/\$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-\$releasever
[updates]
name=updates
baseurl=https://mirrors.aliyun.com/centos/\$releasever/updates/\$basearch/
https://mirrors.huaweicloud.com/centos/\$releasever/updates/\$basearch/
https://mirrors.cloud.tencent.com/centos/\$releasever/updates/\$basearch/
https://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/updates/\$basearch/
http://mirrors.163.com/centos/\$releasever/updates/\$basearch/
http://mirrors.sohu.com/centos/\$releasever/updates/\$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-\$releasever
[centosplus]
name=centosplus
baseurl=https://mirrors.aliyun.com/centos/\$releasever/centosplus/\$basearch/
https://mirrors.huaweicloud.com/centos/\$releasever/centosplus/\$basearch/
https://mirrors.cloud.tencent.com/centos/\$releasever/centosplus/\$basearch/
https://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/centosplus/\$basearch/
http://mirrors.163.com/centos/\$releasever/centosplus/\$basearch/
http://mirrors.sohu.com/centos/\$releasever/centosplus/\$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-\$releasever
EOF
yum clean all
yum repolistUbuntu18.04
#!/bin/bash mv /etc/apt/sources.list /etc/apt/sources.list.bak cat > /etc/apt/sources.list <<EOF deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse EOF apt update
Ubuntu20.04
#!/bin/bash mv /etc/apt/sources.list /etc/apt/sources.list.bak cat > /etc/apt/sources.list <<EOF deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse EOF apt update
其他參考阿里巴巴開源鏡像站-OPSX鏡像站-阿里云開發(fā)者社區(qū)
7.Minimal安裝建議安裝軟件包
Centos系統(tǒng)
yum -y install gcc make autoconf gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel zlib-devel vim lrzsz tree tmux lsof tcpdump wget net-tools iotop bc bzip2 zip unzip nfs-utils man-pages bash-completion chrony &> /dev/null
Ubuntu系統(tǒng)
apt -y install iproute2 ntpdate tcpdump telnet traceroute nfs-kernel-server nfs-common lrzsz tree openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev gcc openssh-server iotop unzip zip bash-completion chrony
8.配置NTP時間同步
Centos系統(tǒng)
# 移除或注釋原有的 server 配置 sed -i '/^server /d' /etc/chrony.conf sed -i 's/^pool /#pool /' /etc/chrony.conf # 添加阿里云時間服務(wù)器 sed -i '3i server ntp.aliyun.com iburst' /etc/chrony.conf # 重啟并設(shè)置開機自啟 systemctl restart chronyd systemctl enable chronyd # 驗證時間同步 chronyc sources -v
Ubuntu系統(tǒng)
# 移除或注釋原有的 server 配置 sed -i '/^server /d' /etc/chrony/chrony.conf sed -i 's/^pool /#pool /' /etc/chrony/chrony.conf # 添加阿里云時間服務(wù)器 sed -i '3i server ntp.aliyun.com iburst' /etc/chrony/chrony.conf # 重啟并設(shè)置開機自啟 systemctl restart chrony systemctl enable chrony # 驗證時間同步 chronyc sources -v
# 使用國內(nèi)NTP服務(wù)器源 server ntp.aliyun.com iburst server ntp.tencent.com iburst server ntp.ntsc.ac.cn iburst # 局域網(wǎng)內(nèi)的NTP服務(wù)器需要增加如下配置 # 允許內(nèi)網(wǎng)客戶端同步時間(如果不使用allow指令明確指定允許訪問的網(wǎng)段,默認行為是拒絕所有外部客戶端同步請求) allow 192.168.0.0/16 # 允許本地時鐘作為后備 local stratum 10
9.優(yōu)化SSH
sed -i.bak -e 's/#UseDNS no/UseDNS no/' -e 's/GSSAPIAuthentication yes/GSSAPIAuthentication no/' /etc/ssh/sshd_config
10.優(yōu)化資源限制
cat >> /etc/security/limits.conf <<-EOF root soft core unlimited root hard core unlimited root soft nproc 1000000 root hard nproc 1000000 root soft nofile 1000000 root hard nofile 1000000 root soft memlock 32000 root hard memlock 32000 root soft msgqueue 8192000 root hard msgqueue 8192000 EOF
11.優(yōu)化內(nèi)核
cat > /etc/sysctl.conf <<-EOF # Controls source route verification net.ipv4.conf.default.rp_filter = 1 net.ipv4.ip_nonlocal_bind = 1 net.ipv4.ip_forward = 1 # Do not accept source routing net.ipv4.conf.default.accept_source_route = 0 # Controls the System Request debugging functionality of the kernel kernel.sysrq = 0 # Controls whether core dumps will append the PID to the core filename. # Useful for debugging multi-threaded applications. kernel.core_uses_pid = 1 # Controls the use of TCP syncookies net.ipv4.tcp_syncookies = 1 # Disable netfilter on bridges. net.bridge.bridge-nf-call-ip6tables = 0 net.bridge.bridge-nf-call-iptables = 0 net.bridge.bridge-nf-call-arptables = 0 # Controls the default maxmimum size of a mesage queue kernel.msgmnb = 65536 # # Controls the maximum size of a message, in bytes kernel.msgmax = 65536 # Controls the maximum shared segment size, in bytes kernel.shmmax = 68719476736 # # Controls the maximum number of shared memory segments, in pages kernel.shmall = 4294967296 # TCP kernel paramater net.ipv4.tcp_mem = 786432 1048576 1572864 net.ipv4.tcp_rmem = 4096 87380 4194304 net.ipv4.tcp_wmem = 4096 16384 4194304 net.ipv4.tcp_window_scaling = 1 net.ipv4.tcp_sack = 1 # socket buffer net.core.wmem_default = 8388608 net.core.rmem_default = 8388608 net.core.rmem_max = 16777216 net.core.wmem_max = 16777216 net.core.netdev_max_backlog = 262144 net.core.somaxconn = 20480 net.core.optmem_max = 81920 # TCP conn net.ipv4.tcp_max_syn_backlog = 262144 net.ipv4.tcp_syn_retries = 3 net.ipv4.tcp_retries1 = 3 net.ipv4.tcp_retries2 = 15 # tcp conn reuse net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_tw_recycle = 0 net.ipv4.tcp_fin_timeout = 30 net.ipv4.tcp_timestamps = 0 net.ipv4.tcp_max_tw_buckets = 20000 net.ipv4.tcp_max_orphans = 3276800 net.ipv4.tcp_synack_retries = 1 net.ipv4.tcp_syncookies = 1 # keepalive conn net.ipv4.tcp_keepalive_time = 300 net.ipv4.tcp_keepalive_intvl = 30 net.ipv4.tcp_keepalive_probes = 3 net.ipv4.ip_local_port_range = 10001 65000 # swap vm.overcommit_memory = 0 vm.swappiness = 10 #net.ipv4.conf.eth1.rp_filter = 0 #net.ipv4.conf.lo.arp_ignore = 1 #net.ipv4.conf.lo.arp_announce = 2 #net.ipv4.conf.all.arp_ignore = 1 #net.ipv4.conf.all.arp_announce = 2 EOF sysctl -p &> /dev/null
12.配置郵件
# Centos安裝郵箱 yum install s-nail -y # Ubuntu安裝郵箱 sudo apt install s-nail -y cat > /etc/s-nail.rc <<-EOF set v15-compat #啟用 v15-compat 兼容性選項 set from="1726340645@qq.com" #發(fā)件人信息 set mta=smtps://1726340645:onrjmfvxckikfcja@smtp.qq.com:465 set smtp-auth=login EOF # 發(fā)送郵件 echo "測試內(nèi)容" | s-nail -s "測試主題" recipient@example.com s-nail -s "郵件主題" xx@163.com < /etc/passwd #xx@163.com為收件人信息
13.總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Ubuntu 16.04 LTS下安裝MATLAB 2014B的方法教程
這篇文章主要介紹了Ubuntu 16.04 LTS下安裝MATLAB 2014B的方法教程,文中介紹的很詳細,相信對大家具有一定的參考價值,有需要的朋友們下面來一起看看吧。2017-02-02
Linux下SVN服務(wù)器同時支持Apache的http和svnserve獨立服務(wù)器兩種模式且使用相同的訪問權(quán)限賬號
這篇文章主要介紹了Linux下SVN服務(wù)器同時支持Apache的http和svnserve獨立服務(wù)器兩種模式且使用相同的訪問權(quán)限賬號,需要的朋友可以參考下2015-07-07
Linux安全地更新系統(tǒng)軟件與內(nèi)核的完整指南
在Linux生態(tài)中,系統(tǒng)升級與內(nèi)核更新是保障安全、提升性能、兼容新硬件的核心操作,無論是服務(wù)器管理員還是桌面用戶,掌握規(guī)范的升級流程與風(fēng)險控制方法,都是系統(tǒng)穩(wěn)定運行的基石,本文將結(jié)合主流發(fā)行版的實踐案例給大家詳細介紹一下,需要的朋友可以參考下2025-10-10
K8s使用kubectl訪問api-server失敗,node“XXX“?not?found的解決方案
K8s中kubectl訪問api-server失敗,報錯node?not?found,需檢查連接、核對kubelet與宿主機IP,修正/etc/hosts或相關(guān)配置,確保節(jié)點信息匹配2025-07-07
sersync2 完全安裝配置說明(三) 插件基本配置和使用
sersync2 完全安裝配置說明(三) 插件基本配置和使用,需要的朋友可以參考下。2011-11-11

