目录
CentOS系统可以运行初始化函数脚本:centos_init.sh
Ubuntu系统可以运行初始化函数脚本:Ubuntu_init.sh
CentOS系统可以运行初始化函数脚本:centos_init.sh
#关闭SELinux
sed -i '/^SELINUX=/c SELINUX=disabled' /etc/selinux/config
#getenforce命令可以查看
#关闭防火墙
systemctl disable --now firewalld (centos7和8)
systemctl status firewalld命令可以查看
centos6:
chkconfig iptables off
service iptables status 查看防火墙状态
service iptables stop 临时关闭防火墙
service iptables start 开启防火墙
service iptables restart 重启防火墙
#支持光盘,/misc/cd对应就是光盘内容
yum -y install autofs
systemctl enable --now autofs
centos6: service autofs start
#配置仓库(此步可做可不做)
系统源,EPEL源
yum clean all清除原有yum缓存
yum makecache生成新的缓存
yum repolist all查看拥有仓库
EPEL源:
wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/epel-7.repo
加载:yum repolist
#yum源放置的位置:cd /etc/yum.repos.d/
这里我是先备份 cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup(最好不要删除)然后去使用阿里或者其他的centos源
yum clean all 清除缓存
yum makecache 生成新的缓存
yum update 更新软件包
#修改网卡名称(centos6不适用)
sed -ri.bak '/^GRUB_CMDLINE_LINUX=/s#(.*)"$#\1 net.ifnames=0"#' /etc/default/grub
or
sed -ri.bak '/^GRUB_CMDLINE_LINUX=/s@"$@ net.ifnames=0"@' /etc/default/grub
or
sed -ri.bak '/^GRUB_CMDLINE_LINUX=/s#"$# net.ifnames=0"#' /etc/default/grub
#修改后执行:
centos:
grub2-mkconfig -o /boot/grub2/grub.cfg; reboot
ubuntu:
grub2-mkconfig -o /boot/grub2/grub.cfg >& /dev/null; reboot
#配置网卡IP
cat > /etc/sysconfig/network-scripts/ifcfg-eth0 <<EOF
DEVICE=eth0
NAME=eth0
BOOTPROTO=static
IPADDR=10.0.0.x
PREFIX=24
GATEWAY=10.0.0.x
DNS1=x.x.x.x
DNS2=180.76.76.76
DNS3=223.6.6.6
ONBOOT=yes
EOF
#此步之后可能会有网络无法连接,查看
[root@localhost ~]#systemctl restart network
Job for network.service failed because the control process exited with error code. See "systemctl status network.service" and "journalctl -xe" for details.
[root@localhost ~]#nmcli con sh
NAME UUID TYPE DEVICE
eth0 5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03 ethernet eth0
ens33 47b3f401-d904-48b1-aef2-2d6af9c88bee ethernet --
[root@localhost ~]nmcli connection up eth0
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/2)
[root@localhost ~]nmcli connection show
[root@localhost ~]nmcli dev show eth0
[root@localhost ~]ping www.baidu.com
或者删除源网卡信息
[root@localhost ~]#nmcli con del ens33
Connection 'ens33' (47b3f401-d904-48b1-aef2-2d6af9c88bee) successfully deleted.
[root@localhost ~]#systemctl restart network
[root@localhost ~]#systemctl status network
#最小化安装系统后,建议安装常用软件
yum -y install autofs vim-enhanced tcpdump chrony lrzsz tree telnet ftp lftp redhat-lsb-core bash-completion net-tools postfix wget bzip2 zip unzip xz lsof mlocate man-pages rsync install gcc gcc-c++ kernel-devel pcsc-lite-libs elfutils-libelf-devel make open-vm-tools
#修改提示符和环境变量
cat > /etc/profile.d/env.sh <<EOF
PS1='\[\e[1;32m\][\u@\h \W]\\$\[\e[0m\] '
export EDITOR=vim
export HISTTIMEFORMAT="%F %T "
EOF
cat > ~/.vimrc <<EOF
set ignorecase
set autoindent
autocmd BufNewFile *.sh exec ":call SetTitle()"
func SetTitle()
if expand("%:e") == 'sh'
call setline(1,"#!/bin/bash")
call setline(2,"#")
call setline(3,"#********************************************************************")
call setline(4,"#Author: xxxxx")
call setline(5,"#QQ: xxxxxxxx")
call setline(6,"#Date: ".strftime("%Y-%m-%d"))
call setline(7,"#FileName: ".expand("%"))
call setline(8,"#URL: www.zhangxingshu.com")
call setline(9,"#Description: My test script")
call setline(10,"#Copyright (C): ".strftime("%Y")." All rights reserved")
call setline(11,"#********************************************************************")
call setline(12,"")
endif
endfunc
autocmd BufNewFile * normal G
EOF
# 更改提示符颜色在vi .bashrc 中添加
echo 'PS1="\[\e[1;40;32m\][\u@\h \w]\\$\[\e[0m\]"' > ~/.bashrc
#或者在ls /etc/profile.d/目录下创建新文件添加
echo 'PS1="\[\e[1;40;32m\][\u@\h \w]\\$\[\e[0m\]"' > /etc/profile.d/xxx
reboot
保存快照
Ubuntu系统可以运行初始化函数脚本:Ubuntu_init.sh
防火墙默认关闭
开启root 远程登录功能
sudo -i
passwd root
sed -i '/PermitRootLogin/c PermitRootLogin yes' /etc/ssh/sshd_config
systemctl restart sshd
#修改网卡名称
vi /etc/default/grub
GRUB_CMDLINE_LINUX="net.ifnames=0"
或者
sed -i '/GRUB_CMDLINE_LINUX=/c GRUB_CMDLINE_LINUX="net.ifnames=0"' /etc/default/grub
生成新的grub.cfg文件
grub-mkconfig -o /boot/grub/grub.cfg
reboot
或者
update-grub
grep net.ifnames /boot/grub/grub.cfg
建议安装常用软件
apt update
apt -y install autofs vim tcpdump chrony lrzsz tree telnet ftp lftp bash-completion net-tools postfix wget bzip2 zip unzip lsof mlocate rsync network-manager
消除日志错误
vim /etc/multipath.conf
#添加下面内容
blacklist {
devnode "^sda"
}
service multipath-tools restart
ubuntu默认时区是UTC,改时区:
timedatectl set-timezone Asia/Shanghai
reboot
做成快照
Linux系统优化配置
1.内核参数优化(新)
vi /etc/sysctl.conf #生产环境中将服务器以下内核参数调大
kernel.shmall = 2097152
kernel.shmmax = 2147483648
kernel.shmmni = 4096
kernel.sem = 250 32000 100 128
fs.file-max = 6553560
net.ipv4.ip_local_port_range = 1024 65000
net.core.rmem_default=262144
net.core.wmem_default=262144
net.core.rmem_max=262144
net.core.wmem_max=262144
所有的TCP/IP调优参数都位于/proc/sys/net/目录。例如, 下面是最重要的一些调优参数, 后面是它们的含义:
1. /proc/sys/net/core/rmem_max — 最大的TCP数据接收缓冲。
2. /proc/sys/net/core/wmem_max — 最大的TCP数据发送缓冲。
3. /proc/sys/net/ipv4/tcp_timestamps — 时间戳在(请参考RFC 1323)TCP的包头增加12个字节。
4. /proc/sys/net/ipv4/tcp_sack — 有选择的应答。
5. /proc/sys/net/ipv4/tcp_window_scaling — 支持更大的TCP窗口. 如果TCP窗口最大超过65535(64KB), 必须设置该数值为1。
6. rmem_default — 默认的接收窗口大小。
7. rmem_max — 接收窗口的最大大小。
8. wmem_default — 默认的发送窗口大小。
9. wmem_max — 发送窗口的最大大小。
/proc目录下的所有内容都是临时性的, 所以重启动系统后任何修改都会丢失。
建议在系统启动时自动修改TCP/IP参数:
把下面代码增加到/etc/rc.local文件, 然后保存文件, 系统重新引导的时候会自动修改下面的TCP/IP参数:
echo 256960 > /proc/sys/net/core/rmem_default
echo 256960 > /proc/sys/net/core/rmem_max
echo 256960 > /proc/sys/net/core/wmem_default
echo 256960 > /proc/sys/net/core/wmem_max
echo 0 > /proc/sys/net/ipv4/tcp_timestamps
echo 1 > /proc/sys/net/ipv4/tcp_sack
echo 1 > /proc/sys/net/ipv4/tcp_window_scaling
TCP/IP参数都是自解释的, TCP窗口大小设置为256960, 禁止TCP的时间戳(取消在每个数据包的头中增加12字节), 支持更大的TCP窗口和TCP有选择的应答。
上面数值的设定是根据互连网连接和最大带宽/延迟率来决定。
注: 上面实例中的数值可以实际应用, 但它只包含了一部分参数。
另外一个方法: 使用 /etc/sysctl.conf 在系统启动时把参数配置成您所设置的值:
net.core.rmem_default = 256960
net.core.rmem_max = 256960
net.core.wmem_default = 256960
net.core.wmem_max = 256960
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_sack =1
net.ipv4.tcp_window_scaling = 1
设置Linux内核参数
配置 Linux 内核参数(2种方法),修改后不用重启动更新: /sbin/sysctl -p
第一种:打开/etc/sysctl.conf 复制如下内容
kernel.shmall = 8388608
kernel.shmmax = 34359738367
kernel.shmmni = 4096
kernel.sem = 250 32000 100 128
fs.file-max = 6553560
net.ipv4.ip_local_port_range = 1024 65000
net.core.rmem_default=262144
net.core.wmem_default=262144
net.core.rmem_max=4194304
net.core.wmem_max=1048586
第二种:打开终端
cat >> /etc/sysctl.conf<
kernel.shmall = 8388608
kernel.shmmax = 34359738367
kernel.shmmni = 4096
kernel.sem = 250 32000 100 128
fs.file-max = 6553560
net.ipv4.ip_local_port_range = 1024 65000
net.core.rmem_default=262144
net.core.wmem_default=262144
net.core.rmem_max=4194304
net.core.wmem_max=1048586
EOF
这里,对每个参数值做个简要的解释和说明。
(1)shmmax:该参数定义了共享内存段的最大尺寸(以字节为单位)。缺省为32M,对于Oracle来说,该缺省值太低了,通常将其设置为2G。
(2)shmmni:这个内核参数用于设置系统范围内共享内存段的最大数量。该参数的默认值是 4096 。通常不需要更改。
(3)shmall:该参数表示系统一次可以使用的共享内存总量(以页为单位)。缺省值就是2097152,通常不需要修改。
(4)sem:该参数表示设置的信号量。
(5)file-max:该参数表示文件句柄的最大数量。文件句柄设置表示在linux系统中可以打开的文件数量。
修改好内核以后,执行下面的命令使新的配置生效。
[root @linux1 /root]# /sbin/sysctl -p
以 root 用户身份运行以下命令来验证您的设置:
/sbin/sysctl -a | grep shm
/sbin/sysctl -a | grep sem
/sbin/sysctl -a | grep file-max
/sbin/sysctl -a | grep ip_local_port_range
例如:
# /sbin/sysctl -a | grep shm
kernel.shmmni = 4096
kernel.shmall = 2097152
kernel.shmmax = 2147483648
kernel.shm-use-bigpages = 0
# /sbin/sysctl -a | grep sem
kernel.sem = 250 32000 100 128
# /sbin/sysctl -a | grep file-max
fs.file-max = 6553560
# /sbin/sysctl -a | grep ip_local_port_range
net.ipv4.ip_local_port_range = 1024 65000
如果系统的参数设置的比上述参数值小,则编辑 /etc/sysctl.conf 文件,添加或更改这些参数。完成后,运行以下命令激活更改:
/sbin/sysctl -p
2.安全模块配置文件limits.conf
vi /etc/security/limits.conf
* - nofile 100000
* - nproc 100000
* - memlock 60000
vi /etc/security/limits.conf #生产环境设置参考limits.conf文件
# End of file
* soft core unlimited
* hard core unlimited
* soft nproc 1000000
* hard nproc 1000000
* soft nofile 1000000
* hard nofile 1000000
* soft memlock 32000
* hard memlock 32000
* soft msgqueue 8192000
* hard msgqueue 8192000
3.网络连接最大追踪连接数
4.mysql最大连接数
5.ssh优化
提示符格式颜色调整:
CentOS提示符修改:
echo 'PS1="\[\e[1;32m\][\t \[\e[1;33m\]\u\[\e[35m\]@\h\[\e[1;31m\] \W\[\e[1;32m\]]\[\e[0m\]\\$ "' > /etc/profile.d/env.sh
echo "PS1='\[\e[1;33m\][\u@\h \W]\\$\[\e[0m\] '" > /etc/profile.d/env.sh
Ubuntu提示符修改:
echo "PS1='\[\e[1;35m\][\u@\h \W]\\$\[\e[0m\] '" >> .bashrc
恢复原格式:PS1="[\u@\h \W]\\$\[\e[0m\]"
30黑色;31红色;32绿色;33黄色;34蓝色;35紫色;36青色;37白色
修改提示符文件:Ubuntu,追加到~/.bashrc
rocky,添加到/etc/profile.d/env.sh,或者是追加到/etc/profile或者/etc/bashrc
修改提示符和环境变量
cat > /etc/profile.d/env.sh <<EOF
PS1='\[\e[1;32m\][\u@\h \W]\\$\[\e[0m\] '
export EDITOR=vim
export HISTTIMEFORMAT="%F %T "
EOF
时间同步:
CentOS7系统默认使用chrony
systemctl start chronyd
systemctl stataus chronyd
在chrony配置文件中添加NTP服务器地址,之后重启
更改硬件时区
echo 'LANG=zh_CN.UTF-8' > /etc/locale.conf 中国中文版格式
echo 'LANG=en_CN.UTF-8' > /etc/locale.conf 中国英文版格式
以系统时间为基准同步硬件时间 hwclock -w
以硬件时间为基准同步系统时间 hwclock -s
查看时间同步状态
date;clock