CentOS优化

一、CentOS6.x优化
CentOS时间管理命令
CentOS6:date、hwclock
CentOS7:timedatectl
CentOS6安装bash-completion,安装完成之后,注销终端,重新登录即可。
[root@CentOS6 ~]# wget http://mirrors.yun-idc.com/epel/6/i386/epel-release-6-8.noarch.rpm
[root@CentOS6 ~]# rpm -ivh epel-release-6-8.noarch.rpm
[root@CentOS6 ~]# yum clean all
[root@CentOS6 ~]# yum makecache
[root@CentOS6 ~]# yum install -y bash-completion
CenOS6可用的yum源
wget -O /etc/yum.repos.d/CentOS-Base.repo http://file.kangle.odata.cc/repo/Centos-6.repo
wget -O /etc/yum.repos.d/epel.repo http://file.kangle.odata.cc/repo/epel-6.repo
yum makecache 

#0、更改yum源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup &&\
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo 

#1、关闭selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
grep SELINUX=disabled /etc/selinux/config 
setenforce 0
getenforce

#2、关闭iptables 
/etc/init.d/iptables stop
/etc/init.d/iptables stop
chkconfig iptables off

#3、精简开机自启动服务
chkconfig|egrep -v "crond|sshd|network|rsyslog|sysstat"|awk '{print "chkconfig",$1,"off"}'|bash
export LANG=en
chkconfig --list|grep 3:on

#4、提权oldboy可以sudo
useradd oldboy
echo 123456|passwd --stdin oldboy
\cp /etc/sudoers /etc/sudoers.ori
echo "oldboy  ALL=(ALL) NOPASSWD: ALL " >>/etc/sudoers
tail -1 /etc/sudoers
visudo -c

#5、中文字符集
cp /etc/sysconfig/i18n /etc/sysconfig/i18n.ori
echo 'LANG="zh_CN.UTF-8"'  >/etc/sysconfig/i18n 
source /etc/sysconfig/i18n
echo $LANG

#6、时间同步
echo '#time sync by oldboy at 2010-2-1' >>/var/spool/cron/root
echo '*/5 * * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1' >>/var/spool/cron/root
crontab -l

#7、命令行安全
#echo 'export TMOUT=300' >>/etc/profile
#echo 'export HISTSIZE=5' >>/etc/profile
#echo 'export HISTFILESIZE=5' >>/etc/profile
#tail -3 /etc/profile
#. /etc/profile

#8、加大文件描述
echo '*               -       nofile          65535 ' >>/etc/security/limits.conf 
tail -1 /etc/security/limits.conf 

#9、内核优化
cat >>/etc/sysctl.conf<<EOF
net.ipv4.tcp_fin_timeout = 2
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_keepalive_time = 600
net.ipv4.ip_local_port_range = 4000    65000
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_max_tw_buckets = 36000
net.ipv4.route.gc_timeout = 100
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_synack_retries = 1
net.core.somaxconn = 16384
net.core.netdev_max_backlog = 16384
net.ipv4.tcp_max_orphans = 16384
#以下参数是对iptables防火墙的优化,防火墙不开会提示,可以忽略不理。
net.nf_conntrack_max = 25000000
net.netfilter.nf_conntrack_max = 25000000
net.netfilter.nf_conntrack_tcp_timeout_established = 180
net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120
net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60
net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120
EOF

#CentOS6 epel源
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
wget http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
rpm -ivh epel-release-6-8.noarch.rpm 
检查EPEL源,EPEL源的配置安装到了 /etc/yum.repos.d/epel.repo 文件
yum repolist
重建缓存
yum makecache

yum install lrzsz nmap tree dos2unix nc -y

#yum -y update
升级所有包,改变软件设置和系统设置,系统版本内核都升级
#yum -y upgrade
升级所有包,不改变软件设置和系统设置,系统版本升级,内核不改变
模板机特殊处理技巧:方便后续克隆改IP和主机名等
修改主机名和IP脚本
[root@backup ~]# cat /server/scripts/m.sh 
#!/bin/sh
if [ $# -ne 2 ];then
  echo "/bin/sh $0 hostname PartIP"
  exit 1
fi
sed  -i "s#oldboy#$1#g" /etc/sysconfig/network
hostname $1
sed -i "s#100#$2#g" /etc/sysconfig/network-scripts/ifcfg-eth0
sed -i "s#100#$2#g" /etc/sysconfig/network-scripts/ifcfg-eth1
调整克隆后网站正常配置
[root@backup ~]# tail -1 /etc/rc.local    
>/etc/udev/rules.d/70-persistent-net.rules
删除模板及网卡配置里mac所在行及uuid所在行。
B、模板机配置
a./etc/hosts
hosts解析
cat >/etc/hosts<<EOF
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.1.5      lb01
172.16.1.6      lb02
172.16.1.7      web02
172.16.1.8      web01
172.16.1.51     db01
172.16.1.31     nfs01
172.16.1.41     backup
172.16.1.61     m01
EOF
View Code

二、CentOS7.x优化
1、安装常用软件包
yum -y install wget vim bash-completion lrzsz nmap telnet tree net-tools bind-utils lsof

2、更改yum源(一般不需要改)
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
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

3、Manpages-zh中文包安装(翻译的命令太少,鸡肋)

# 安装epel源
yum -y install epel-release
# 安装中文语言包
yum -y install langpacks-zh_CN
# 设置系统语言环境为中文
vim /etc/locale.conf
#LANG="en_US.UTF-8"
LANG="zh_CN.UTF-8"
# 重新加载语言环境
source /etc/locale.conf
# 安装man-pages-zh-CN包
yum -y install man-pages-zh-CN
# 验证安装
man ls

4、更改网卡名称为eth(默认网卡名称随机分配)
在装系统时修改网卡名称:进入启动菜单后按Tab键,在quiet后添加net.ifnames=0 biosdevname=0,例:
> vmlinuz initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 quiet net.ifnames=0 biosdevname=0
在装完系统后修改:
[root@CentOS7 network-scripts]# cat ifcfg-eth0

TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
NAME=eth0
UUID=64d3ac01-c44b-42be-919e-677d6ae9f758
DEVICE=eth0
ONBOOT=yes
IPADDR=10.47.39.200
PREFIX=24
GATEWAY=10.47.39.254
DNS1=10.47.39.10
IPV4_ROUTE_METRIC=80    #指定默认路由metric值

[root@CentOS7 ~]# cat /etc/sysconfig/grub

GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="rhgb net.ifnames=0 biosdevname=0 quiet"
GRUB_DISABLE_RECOVERY="true"
[root@CentOS ~]# grub2-mkconfig -o /boot/grub2/grub.cfg    # 生成启动菜单(CentOS,Rocky都使用此命令)
[root@CentOS ~]# reboot
修改网卡名称:http://oldboy.blog.51cto.com/2561410/1722101

5、安装epel源
yum install -y epel-release    # 安装epel源
yum clean all                        # 清除系统所有的yum缓存
yum makecache                  # 生成yum缓存
yum repolist enabled           # 查看系统可用的yum源(yum repolist)
yum repolist all                    # 查看系统所有yum源
RHEL/CentOS 6:
# yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm
RHEL/CentOS 7:
# yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
RHEL/CentOS 8:
# yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
RHEL/CentOS 8修改yum源:
# sed -i -e 's|mirrorlist=|#mirrorlist=|g' /etc/yum.repos.d/CentOS-*
# sed -i -e 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*

6、常用命令
[root@CentOS7 ~]# cat /etc/os-release | /etc/redhat-release         #查看Centos7版本
[root@CentOS7 ~]# nmtui              #进入图形界面的网络管理
[root@CentOS7 ~]# vim /etc/hostname | hostnamectl set-hostname CentOS7    #修改主机名
[root@CentOS7 ~]# localectl set-locale LANG=zh_CN.UTF-8                                 #设置字符集
[root@CentOS7 ~]# systemctl set-default multi-user.target                                     #设置命令行启动
[root@CentOS7 ~]# ls -lh /usr/lib/systemd/system/runlevel*.target
[root@CentOS7 ~]# systemd-analyze time
[root@CentOS7 ~]# systemd-analyze blame
[root@CentOS7 ~]# systemd-analyze plot > bootime.svg
[root@CentOS7 ~]# systemctl disable <servicesname>          #关闭不需要的开机启动服务
[root@CentOS7 ~]# systemctl disable firewalld                      #关闭防火墙开机启动
[root@CentOS7 ~]# systemctl stop firewalld                         #关闭防火墙服务
[root@CentOS7 ~]# systemctl status firewalld                     #查看防火墙状态
[root@CentOS7 ~]# sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config   #关闭selinux
[root@CentOS7 ~]# systemctl list-unit-files|grep postfix        #查看单个服务状态
[root@CentOS7 ~]# systemctl list-unit-files|grep enabled     #查看所有enable的服务

abrt-ccpp.service                             enabled 
abrt-oops.service                             enabled 
abrt-vmcore.service                           enabled 
abrt-xorg.service                             enabled 
abrtd.service                                 enabled  #调试相关的服务
atd.service                                   enabled  #一次性任务服务
auditd.service                                enabled  #selinux相关
chronyd.service                               enabled  #时间同步服务,改名了,也不需要,用ntpdate
crond.service                                 enabled  #定时任务
dbus-org.fedoraproject.FirewallD1.service     enabled  
dbus-org.freedesktop.NetworkManager.service   enabled  #dbus相关,系统服务
dbus-org.freedesktop.nm-dispatcher.service    enabled  #dbus相关,系统服务
dmraid-activation.service                     enabled  #raid相关
firewalld.service                             enabled  #防火墙服务
getty@.service                                enabled  #tty窗口
irqbalance.service                            enabled  #多核CPU调度
kdump.service                                 enabled  #内核出问题dump保存信息
libstoragemgmt.service                        enabled  #用于存储管理的库
lvm2-monitor.service                          enabled  #不用lvm关闭
mdmonitor.service                             enabled  #软raid相关
microcode.service                             enabled  #Intel的CPU会提供一个外挂的微指令集提供系统运行
NetworkManager-dispatcher.service             enabled  #网络相关
NetworkManager.service                        enabled  #网络相关
postfix.service                               enabled  #邮件服务
rngd.service                                  enabled  #为内核提供随机数服务
rsyslog.service                               enabled  #系统日志
smartd.service                                enabled  #检测硬盘状态
sshd.service                                  enabled  #ssh服务
sysstat.service                               enabled  #系统检测服务
systemd-readahead-collect.service             enabled  #系统服务
systemd-readahead-drop.service                enabled  #系统服务 
systemd-readahead-replay.service              enabled  #系统服务
tuned.service                                 enabled  #服务器能耗管理
dm-event.socket                               enabled  #监控设备对应表(device mapper)
lvm2-lvmetad.socket                           enabled  #不用lvm时关闭
lvm2-lvmpolld.socket                          enabled  #不用lvm时关闭
default.target                                enabled 
multi-user.target                             enabled 
remote-fs.target                              enabled 
runlevel2.target                              enabled 
runlevel3.target                              enabled 
runlevel4.target                              enabled 

Linux基础优化与安全重点小结:
1)不用root登录管理系统,而以普通用户登录通过sudo授权管理。
2)更改默认的远程连接SSH服务端口,禁止root用户远程连接,甚至要更改SSH服务只监听内网IP。
3)定时自动更新服务器的时间,使其和互联网时间同步。
4)配置yum更新源,从国内更新源下载安装软件包。
5)关闭SELinux及iptables(在工作场景中,如果有外部IP一般要打开iptables,高并发高流量的服务器可能无法开启)。
6)调整文件描述符的数量,进程及文件的打开都会消耗文件描述符数量。
7)定时自动清理邮件临时目录垃圾文件,防止磁盘的inodes数被小文件占满(注意Centos6和Centos5要清除的目录不同)。
8)精简并保留必要的开机自启动服务(如crond、sshd、network、rsyslog、sysstat)。
9)Linux内核参数优化/etc/sysctl.conf,执行sysctl -p生效。
10)更改系统字符集为“zh_CN.UTF-8”,使其支持中文,防止出现乱码问题。
11)锁定关键系统文件如/etc/passwd、/etc/shadow、/etc/group、/etc/gshadow、/etc/inittab, 处理以上内容后把chattr、lsattr改名为oldboy,转移走,这样就安全多了。
12)清空/etc/issue、/etc/issue.net,去除系统及内核版本登录前的屏幕显示。
13)清除多余的系统虚拟用户账号。
14)为grub引导菜单加密码。
15)禁止主机被ping。
16)打补丁并升级有已知漏洞的软件。

三、CentOS7和ubuntu使用UEFI启动的分区方法:

四、自定义linux系统日志格式(messages,cron,ssh等日志格式)
[root@localhost ~]# vim /etc/rsyslog.conf

#定义自己的时间格式
$template myformat,"%$NOW% %TIMESTAMP:8:15% %hostname% %syslogtag% %msg%\n"

#Use default timestamp format
#把这行注释,系统的日志格式默认是由这行定义的
#$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat

#使用自定义的格式
$ActionFileDefaultTemplate myformat

[root@localhost ~]# systemctl restart rsyslog

五、CentOS添加永久路由

[root@CentOS7 /root]#ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: eth2: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc mq master bond1 state UP qlen 1000
    link/ether 74:3a:20:27:61:68 brd ff:ff:ff:ff:ff:ff
3: eth3: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc mq master bond1 state UP qlen 1000
    link/ether 74:3a:20:27:61:68 brd ff:ff:ff:ff:ff:ff
4: eth0: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc mq master bond0 state UP qlen 1000
    link/ether 74:3a:20:25:87:a0 brd ff:ff:ff:ff:ff:ff
5: eth1: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc mq master bond0 state UP qlen 1000
    link/ether 74:3a:20:25:87:a0 brd ff:ff:ff:ff:ff:ff
6: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
    link/ether 74:3a:20:25:87:a0 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.10/24 brd 99.99.0.191 scope global bond0
       valid_lft forever preferred_lft forever
7: bond1: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
    link/ether 74:3a:20:27:61:68 brd ff:ff:ff:ff:ff:ff
    inet 192.168.2.10/24 brd 99.99.25.7 scope global bond1
       valid_lft forever preferred_lft forever

[root@CenOS7 /root]#cat /etc/sysconfig/network-scripts/route-bond0
10.10.10.0/23 via 192.168.1.1
10.10.20.0/24 via 192.168.1.1
[root@CenOS7 /root]#route -n
Kernel IP routing table
Destination     Gateway          Genmask           Flags Metric Ref    Use Iface
10.10.10.0       192.168.1.1      255.255.254.0   UG    0          0           0  bond0
10.10.20.0       192.168.1.1      255.255.255.0   UG    0          0           0  bond0                                                                        

[root@localhost ~]# ip route show | column -t     #查看路由表
临时添加,重启系统失效
[root@localhost ~]# route add -net 1.1.1.0/24 gw 192.168.1.1
[root@localhost ~]# ip route add 2.2.2.0/24 via 192.168.1.1

六、添加多个loopback口(设置网卡别名)
[root@CentOS ~]# cp /etc/sysconfig/network-scripts/ifcfg-lo /etc/sysconfig/network-scripts/ifcfg-lo:1
[root@CentOS ~]# vim /etc/sysconfig/network-scripts/ifcfg-lo:1

DEVICE=lo:1
IPADDR=192.168.100.1
NETMASK=255.255.255.0
NETWORK=192.168.100.0
# If you're having problems with gated making 127.0.0.0/8 a martian,
# you can change this to something else (255.255.255.255, for example)
BROADCAST=192.168.100.255
ONBOOT=yes
NAME=loopback1

七、云虚拟主机系统磁盘扩容
cloud-utils-growpart-0.27-13.el7.centos.noarch.rpm
[root@CentOS ~]# wget https://cbs.centos.org/kojifiles/packages/cloud-utils-growpart/0.27/13.el7.centos/noarch/cloud-utils-growpart-0.27-13.el7.centos.noarch.rpm
[root@CentOS ~]# fdisk -l
[root@CentOS ~]# rpm -ivh cloud-utils-growpart-0.27-13.el7.centos.noarch.rpm
[root@CentOS ~]# growpart /dev/vda 1      #表示给系统盘的第一个分区扩容
[root@CentOS ~]# reboot
[root@CentOS ~]# blkid /dev/vda1             #文件系统格式检查
[root@CentOS ~]# resize2fs /dev/vda1      #ext4格式文件系统使用
[root@CentOS ~]# xfs_growfs /dev/vda1   #xfs格式文件系统使用(yum -y install xfsprogs)
[root@CentOS ~]# df -h

相关链接:
       https://oldqiang.com/
       https://www.dgstack.cn/
       https://man.linuxde.net/                       #Linux命令查询
       https://fedoraproject.org/wiki/EPEL     #epel源安装
       https://wiki.centos.org/Download        #centos各版本下载
       https://vault.centos.org/                      #centos各版本下载
       http://linuxsoft.cern.ch/centos-vault/
       https://archive.kernel.org/centos-vault/
       https://www.unixhot.com/page/ops     #运维知识体系
       http://www.jinbuguo.com/systemd/systemd.service.html      # systemd.service 中文手册
       http://caspian.dotconf.net/menu/Software/SendEmail/         # SendEmail下载
       https://blog.csdn.net/qq_46162321/article/details/115006096       # CentOS7配置本地和网络yum源
       https://mp.weixin.qq.com/s/Rey0gSnnj-zoAEwE6J-Gjw      # Linux系统性能优化
       https://github.com/zsh-users/zsh-completions        # zsh-completions Tab键补全

 

posted @ 2018-03-26 10:21  風£飛  阅读(476)  评论(0编辑  收藏  举报