Production security policy and system performance optimization server environment assessment

Production security policy and system performance optimization server environment assessment

1. operation and maintenance experience to share with the idea of ​​Linux troubleshooting

1.1 online server installation and basic strategy experience

Compact installation strategy:

  • Installation requires only, on-demand installation, do not install
  • Development kits, basic network packets, basic application package

1.1.1 CentOS-6.x

image.png-93kB

image.png-99.3kB

1.1.2 CentOS-7.x

image.png-133.5kB

Disc system - according to the default partitioning
data disk mounted separately

1.2 online server network setup experience and skills

1.2.1 Centos7.x the best service shut down

image.png-66.7kB

#关闭NetworkManager服务,并关闭开机启动
[root@yangwenbo ~]# systemctl stop NetworkManager
[root@yangwenbo ~]# systemctl disable NetworkManager
Removed symlink /etc/systemd/system/multi-user.target.wants/NetworkManager.service.
Removed symlink /etc/systemd/system/dbus-org.freedesktop.NetworkManager.service.
Removed symlink /etc/systemd/system/dbus-org.freedesktop.nm-dispatcher.service.

[root@yangwenbo ~]# systemctl status NetworkManager
● NetworkManager.service - Network Manager
   Loaded: loaded (/usr/lib/systemd/system/NetworkManager.service; disabled; vendor preset: enabled)
   Active: inactive (dead) since Mon 2018-10-08 02:01:14 EDT; 18s ago
     Docs: man:NetworkManager(8)
 Main PID: 1299 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/NetworkManager.service
           └─1344 /sbin/dhclient -d -q -sf /usr/libexec/nm-dhcp-helper -pf /var/run/dhclient-ens32.pid -lf...

If you do not close the service, then the service will be taken over Linux's network settings. Sometimes the situation can lead to modify the network card configuration file IP, but IP network card unchanged

开启NetworkManager服务,打开开机自启动的命令
[root@yangwenbo ~]# systemctl start NetworkManager
[root@yangwenbo ~]# systemctl enable NetworkManager

1.2.2 DNS settings on the

(1) temporarily modify DNS settings, the changes take effect immediately, restart the server or restart network recovery

[root@yangwenbo ~]# cat /etc/resolv.conf
; generated by /usr/sbin/dhclient-script
search localdomain
nameserver 192.168.200.2         #修改此条配置,DNS即可被修改。立即生效

Modify the file /etc/resolv.conf domain name server, DNS can be modified, effective immediately
but the network restart or reboot the server in the /etc/resolv.conf domain name server settings will be overwritten card configuration settings file

[root@yangwenbo ~]# vim /etc/resolv.conf
[root@yangwenbo ~]# cat /etc/resolv.conf
; generated by /usr/sbin/dhclient-script
search localdomain
nameserver 192.168.200.3      #修改了此行配置
[root@yangwenbo ~]# systemctl restart network
#中间有可能会掉线

[root@yangwenbo ~]# cat /etc/resolv.conf
; generated by /usr/sbin/dhclient-script
search localdomain
nameserver 192.168.200.2      #配置还原了

(2) permanently modify the DNS settings

[root@yangwenbo ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens32 
TYPE=Ethernet
BOOTPROTO=static
DEVICE=ens32
ONBOOT=yes
NM_CONTROLLED=yes
IPADDR=192.168.200.141
NETMASK=255.255.255.0
GATEWAY=192.168.200.2
DNS1=192.168.200.2     #永久修改需要修改网卡配置文件本行
DNS2=202.106.0.20

1.2.3 modify the server host name on its own

Centos7.x

#永久修改主机名
[root@yangwenbo ~]# cat /etc/hostname
localhost.localdomain
[root@yangwenbo ~]# vim /etc/hostname
[root@yangwenbo ~]# cat /etc/hostname
Centos7.5
[root@yangwenbo ~]# hostname Centos7.5
[root@yangwenbo ~]# bash
[root@Centos7 ~]# 

1.2.4 Mapping on their own server hostname

#映射服务器自身的主机名
[root@Centos7 ~]# echo "127.0.0.1 Centos7" >> /etc/hosts
[root@Centos7 ~]# tail -1 /etc/hosts
127.0.0.1 Centos7

Note that the server host name mappings own, be sure not mapped to 127.0.0.1 mapped to IP NIC This is because many run their own services must verify that the host name is mapped, or will lead to unknown trouble

1.3 online server Selinux, iptables policy settings

1.3.1 selinux configuration (how to turn off selinux)

[root@Centos7 ~]# sestatus
SELinux status:                 enabled
#以下省略。。。

#永久关闭selinux开机自启动
[root@Centos7 ~]# cat /etc/selinux/config 

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled   #当然selinux开启自启动不能(enforcing开启;disabled关闭)
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

#临时关闭selinux
[root@Centos7 ~]# setenforce 0

1.3.2 iptables configuration

If our room no hardware firewall, then we must do the iptables secure server with a public card

#防火墙配置文件/etc/sysconfig/iptables
#推荐配置
iptables -P INPUT ACCEPT
iptables -F
iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
iptables -A INPUT -s 1.1.1.0/24 -p tcp -m tcp --dport 22 -j ACCEPT
iptables -A INPUT -s 2.2.2.2/32 -p tcp -m tcp --dport 22 -j ACCEPT
iptables -A INPUT -i eth1 -j ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -j DROP
iptables -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -j DROP
iptables -A INPUT -p tcp -m tcp --tcp-flags SYN,RST SYN,RST -j DROP
iptables -A INPUT -p tcp -m tcp --tcp-flags FIN,RST FIN,RST -j DROP
iptables -A INPUT -p tcp -m tcp --tcp-flags FIN,ACK FIN -j DROP
iptables -A INPUT -p tcp -m tcp --tcp-flags PSH,ACK PSH -j DROP
iptables -A INPUT -p tcp -m tcp --tcp-flags ACK,URG URG -j DROP
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP

1.4 online server ssh login security policy

1.4.1 ssh landing strategy

(1) landing strategy

[root@Centos7 ~]# cp /etc/ssh/sshd_config{,.bak}   #备份

#修改前
[root@Centos7 ~]# cat -n /etc/ssh/sshd_config | sed -n '17p;38p;43p;47p;65p;79p;115p'
    17  #Port 22                       #修改ssh连接端口
    38  #PermitRootLogin yes           #是否允许root账号远程登陆
    43  #PubkeyAuthentication yes      #是否开启公钥连接认证
    47  AuthorizedKeysFile  .ssh/authorized_keys    #公钥文件的放置位置
    65  PasswordAuthentication yes     #是否开启密码验证登陆
    79  GSSAPIAuthentication yes       #是否关闭GSSAPI认证
   115  #UseDNS yes                    #是否关闭DNS反向解析
   
#修改后
[root@Centos7 ~]# cat -n /etc/ssh/sshd_config | sed -n '17p;38p;43p;47p;65p;79p;115p'
    17  Port 22221                  #工作中需要设定到1万以上的端口,避免被扫描出来。
    38  PermitRootLogin yes         #如果不是超大规模的服务器,为了方便我们可以暂时开启root远程登录
    43  PubkeyAuthentication yes    #开启公钥认证模式
    47  AuthorizedKeysFile  .ssh/authorized_keys    #公钥放置位置
    65  PasswordAuthentication no   #因为我们开启了root远程登录,因此为了安全我们关闭密码认证的方式
    79  GSSAPIAuthentication no     #关闭GSSAPI认证,极大提高ssh连接速度
   115  UseDNS no                   #关闭DNS反向解析,极大提高ssh连接速度

(2) Set XSHELL private landing for Linux

#查看服务器端IP
[root@Centos7 ~]# hostname -I
192.168.200.141

#在Linux服务器端生成rsa密钥对
[root@Centos7 ~]# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:niDwzrv1Xq4er0zH0FqfHpvRiCJfPUcnL3vT89ZEt0M [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|                 |
|                 |
|  .              |
|   o     .     Eo|
|    o . S o   +.+|
|   o . o * + = *.|
|    o o B * O +.=|
|     o * O . B ==|
|    o. oOoo + .o=|
+----[SHA256]-----+
#将生成的公钥导入到服务器端的~/.ssh/authorized_keys文件里
[root@Centos7 ~]# cd .ssh/
[root@Centos7 .ssh]# ls
id_rsa  id_rsa.pub
[root@Centos7 .ssh]# cat id_rsa.pub > authorized_keys
[root@Centos7 .ssh]# ls
authorized_keys  id_rsa  id_rsa.pub
[root@Centos7 .ssh]# cat authorized_keys 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDmNGBvYnNojir7tfB9l7N2DplsoRHeUB4747xT2q5Z3g9CvM/D5AsyFogcCPFyfXIZuNFiH2IEQOS8ZXjpNU/1jy6sUxpwld2sMXHYiP+PtQJimS568ASVS1pzhXksHcPk8yXenPId73vQX8p8H9nD5/y94UwMksC8YsnoDSW7tOUUG9vdtjZw06lUwXfAkUapT6tEb3Mq6mA2LZMDUck1NicrdbmpAdcdsFaL3mzCHqxTvt3sNIruTiE8DhtLGpYCEBpWVOJuoZ8hRQTzaMHJaF7XHf4Yw5d0m937KY16RQnTziJOEVfHEJaUmV875SUsEacHjggj5PJfxJhq6d/P [email protected]
#将私钥文件id_rsa改名为rd_rsa_root并导出到宿主机桌面上
[root@Centos7 .ssh]# ls
authorized_keys  id_rsa  id_rsa.pub
[root@Centos7 .ssh]# mv id_rsa id_rsa_root
[root@Centos7 .ssh]# ls
authorized_keys  id_rsa.pub  id_rsa_root

View into private key file on the desktop

image.png-2.2kB

image.png-39.1kB

image.png-40kB

而后XSHELL显示登陆成功!
其实这个私钥id_rsa_root,其他发给人作为金钥就都可以登陆服务器端的了
也。可以发给其他的Linux服务器使得他们可以SSH登陆金钥到192.168.200.141服务器端的
具体操作过程如下

#将桌面上的id_rsa_root私钥文件拷贝到任意Linux服务器上
[root@yangwenbo .ssh]# ls
id_rsa_root

#在Linux服务器上就不能随意改名字了。将id_rsa_root改名id_rsa
[root@yangwenbo .ssh]# mv id_rsa_root id_rsa

#授权600权限
[root@yangwenbo .ssh]# chmod 600 id_rsa

#进行登陆测试
[root@yangwenbo .ssh]# ssh [email protected] -p 22222
Last login: Mon Oct  8 04:52:42 2018 from 192.168.200.143
[root@Centos7 ~]# hostname -I
192.168.200.141 
[root@Centos7 ~]# exit
logout
Connection to 192.168.200.141 closed.
[root@yangwenbo .ssh]# hostname -I
192.168.200.143 

XSHELL密钥登陆服务器端的普通用户yunjisuan

#创建用户
[root@Centos7 ~]# useradd yunjisuan

#创建普通用户云计算下的.ssh密钥目录
[root@Centos7 ~]# mkdir -p /home/yunjisuan/.ssh

#授权普通用户属主属组
[root@Centos7 ~]# chown yunjisuan.yunjisuan /home/yunjisuan/.ssh

#.ssh目录必须700权限
[root@Centos7 ~]# chmod 700 /home/yunjisuan/.ssh
[root@Centos7 .ssh]# pwd
/root/.ssh
[root@Centos7 .ssh]# ll
total 12
-rw-r--r--. 1 root root  396 Oct  8 03:09 authorized_keys
-rw-r--r--. 1 root root  396 Oct  8 03:07 id_rsa.pub
-rw-------. 1 root root 1679 Oct  8 03:07 id_rsa_root

#将之前root下的authorized_keys文件拷贝过去,然后修改属主属组
[root@Centos7 .ssh]# cp -p authorized_keys /home/yunjisuan/.ssh/
[root@Centos7 .ssh]# chown yunjisuan.yunjisuan /home/yunjisuan/.ssh/authorized_keys
[root@Centos7 .ssh]# ll /home/yunjisuan/.ssh/authorized_keys
-rw-r--r-- 1 yunjisuan yunjisuan 396 Oct  8 03:09 /home/yunjisuan/.ssh/authorized_keys

最后在XSHELL端用同样的方法远程登录yunjisuan用户选择密钥认证方式即可。

(3)用户权限策略

禁止根用户远程登录系统,授权仅普通用户登陆系统,需要管理员权限执行须藤即可,避免根用户之间登陆

#以root账号授权普通用户yunjisuan所有权限并免输入密码
[root@Centos7 ~]# sed -n '93p' /etc/sudoers
yunjisuan   ALL=(ALL)   NOPASSWD: ALL
#以yunjisuan用户测试提权
[yunjisuan@Centos7 ~]$ sudo -l
Matching Defaults entries for yunjisuan on Centos7:
    !visiblepw, always_set_home, match_group_by_gid, env_reset, env_keep="COLORS DISPLAY HOSTNAME HISTSIZE
    KDEDIR LS_COLORS", env_keep+="MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE",
    env_keep+="LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES", env_keep+="LC_MONETARY LC_NAME
    LC_NUMERIC LC_PAPER LC_TELEPHONE", env_keep+="LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY",
    secure_path=/sbin\:/bin\:/usr/sbin\:/usr/bin

User yunjisuan may run the following commands on Centos7:
    (ALL) NOPASSWD: ALL
[yunjisuan@Centos7 ~]$ ls /root/      #权限不够
ls: cannot open directory /root/: Permission denied
[yunjisuan@Centos7 ~]$ sudo ls /root/
anaconda-ks.cfg

#如果ssh设置了不然root用户远程登录的配置
#那么我们可以通过此普通用户面密码方式切换成root账户
[yunjisuan@Centos7 ~]$ sudo su -
Last login: Mon Oct  8 04:54:17 EDT 2018 from 192.168.200.143 on pts/1
[root@Centos7 ~]# ls
anaconda-ks.cfg
[root@Centos7 ~]# exit
logout
[yunjisuan@Centos7 ~]$ ls
[yunjisuan@Centos7 ~]$ 

1.5 线上服务器更新yum源及必要软件安装以及NTP时钟服务设置

1.5.1 更新常用的yum源及必要软件包的安装

CentOS7.x服务器

#在CentOS7.x服务器上
#测试一下本机是否能上网
[root@Centos7 ~]# ping -c 1 www.baidu.com
PING www.a.shifen.com (119.75.213.61) 56(84) bytes of data.
64 bytes from 127.0.0.1 (119.75.213.61): icmp_seq=1 ttl=128 time=7.47 ms

--- www.a.shifen.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 7.475/7.475/7.475/0.000 ms
#删除原yum本地源
[root@Centos7 ~]# cd /etc/yum.repos.d/
[root@Centos7 yum.repos.d]# ls
CentOS-Base.repo  CentOS-Debuginfo.repo  CentOS-Media.repo    CentOS-Vault.repo
CentOS-CR.repo    CentOS-fasttrack.repo  CentOS-Sources.repo
[root@Centos7 yum.repos.d]# rm -rf *
#安装epel源
[root@Centos7 yum.repos.d]# yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[root@Centos7 yum.repos.d]# ls
epel.repo  epel-testing.repo
#下载并安装repoforge源
[root@Centos7 yum.repos.d]# yum -y install http://repository.it4i.cz/mirrors/repoforge/redhat/el7/en/x86_64/rpmforge/RPMS/rpmforge-release-0.5.3-1.el7.rf.x86_64.rpm
[root@Centos7 yum.repos.d]# ls
epel.repo          mirrors-rpmforge         mirrors-rpmforge-testing
epel-testing.repo  mirrors-rpmforge-extras  rpmforge.repo
#清空旧yum缓存,创建新yum缓存
[root@Centos7 yum.repos.d]# yum -y clean all
[root@Centos7 yum.repos.d]# yum makecache

#更新系统中已经安装的软件包
[root@Centos7 yum.repos.d]# yum -y update

CentOS6.x服务器

#在CentOS6.x服务器上
[root@Centos6 ~]# ls /etc/yum.repos.d/
bak  CentOS-Media.repo

#测试一下服务器是否能上网
[root@Centos6 ~]# ping -c 1 www.baidu.com
PING www.a.shifen.com (119.75.213.61) 56(84) bytes of data.
64 bytes from 127.0.0.1 (119.75.213.61): icmp_seq=1 ttl=128 time=15.2 ms

--- www.a.shifen.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 1463ms
rtt min/avg/max/mdev = 15.251/15.251/15.251/0.000 ms
#安装epel源
[root@Centos6 ~]# cd /etc/yum.repos.d/
[root@Centos6 yum.repos.d]# yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm

[root@Centos6 yum.repos.d]# ls
bak  CentOS-Media.repo  epel.repo  epel-testing.repo
#下载并安装repoforge源
[root@Centos6 yum.repos.d]# yum -y install http://repository.it4i.cz/mirrors/repoforge/redhat/el6/en/x86_64/rpmforge/RPMS/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm

[root@Centos6 yum.repos.d]# ls
bak                epel.repo          mirrors-rpmforge         mirrors-rpmforge-testing
CentOS-Media.repo  epel-testing.repo  mirrors-rpmforge-extras  rpmforge.repo
#清空旧yum缓存,创建新yum缓存
[root@Centos6 yum.repos.d]# yum -y clean all
[root@Centos6 yum.repos.d]# yum makecache

#更新系统中已经安装的软件包
[root@Centos6 yum.repos.d]# yum -y update

1.5.2 修改时区与定时自动更新服务器时间

推荐时间服务器:ntp.sjtu.edu.cn ntp1.aliyun.com

#安装ntpdate时间同步客户端
[root@Centos6 ~]# yum -y install ntpdate
[root@Centos6 ~]# rpm -qa ntpdate
ntpdate-4.2.6p5-1.el6.centos.x86_64

#修改时区
[root@Centos6 ~]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

#进行时间同步
[root@Centos6 ~]# ntpdate ntp1.aliyun.com
 8 Oct 17:45:36 ntpdate[1573]: adjust time server 120.25.115.20 offset -0.000011 sec
 
#将时间同步加入定时任务
[root@Centos6 ~]# echo '*/5 * * * * /usr/sbin/ntpdate ntp.sjtu.edu.cn >> /var/log/ntp.log 2>&1;/sbin/hwclock -w' >> /var/spool/cron/root
[root@Centos6 ~]# crontab -l
*/5 * * * * /usr/sbin/ntpdate ntp.sjtu.edu.cn >> /var/log/ntp.log 2>&1;/sbin/hwclock -w

#说明:/sbin/hwclock -w :将时钟信息刷新到bios里

1.6 精简开机服务,删除无关用户,清理垃圾文件,重要文件安全策略

1.6.1 线上服务器必须开启的五个服务

crond的,网络,系统日志,sshd的,SYSSTAT

1.6.2 删除无关用户

1.6.3 定时自动清理垃圾文件

(1)查找大文件的方法

[root@Centos7 /]# du -sh ./*
0   ./bin
97M ./boot
0   ./dev
31M ./etc
16K ./home
0   ./lib
0   ./lib64
4.2G    ./media
0   ./mnt
0   ./opt
du: cannot access ‘./proc/1486/task/1486/fd/4’: No such file or directory
du: cannot access ‘./proc/1486/task/1486/fdinfo/4’: No such file or directory
du: cannot access ‘./proc/1486/fd/4’: No such file or directory
du: cannot access ‘./proc/1486/fdinfo/4’: No such file or directory
0   ./proc
48K ./root
7.6M    ./run
0   ./sbin
0   ./srv
0   ./sys
0   ./tmp
998M    ./usr
759M    ./var
[root@Centos7 /]# cd /usr/
[root@Centos7 usr]# du -sh ./*
65M ./bin
0   ./etc
0   ./games
36K ./include
437M    ./lib
168M    ./lib64
12M ./libexec
0   ./local
42M ./sbin
276M    ./share
0   ./src
0   ./tmp

(2)定时任务清理的关键目录路径

/var/spool/mail/              #邮件路径
/var/spool/postfix/maildrop   #小碎片路径
1.6.4 重要文件安全策略
  • chattr + i / etc / sudoers
  • chattr + i / etc / shadow
  • chattr + i / etc / passwd
  • chattr + i /etc/grub.conf

1.7 线上服务器系统内核参数优化策略

1.7.1 显示当前所有系统资源使用限制

[root@Centos7 ~]# ulimit -a 
core file size          (blocks, -c) 0    #core文件的最大值为100blocks
data seg size           (kbytes, -d) unlimited   #进程的数据段可以任意大
scheduling priority             (-e) 0           #调度优先级
file size               (blocks, -f) unlimited   #文件可以任意大
pending signals                 (-i) 3802        #最多有3802个待处理的信号
max locked memory       (kbytes, -l) 64          #一个任务锁住的物理内存的最大值为64KB
max memory size         (kbytes, -m) unlimited   #一个任务的常驻物理内存的最大值
open files                      (-n) 1024        #一个任务最多可以同时打开1024个文件
pipe size            (512 bytes, -p) 8           #管道的最大空间为4096(512*8)字节
POSIX message queues     (bytes, -q) 819200      #POSIX的消息队列的最大值为819200字节
real-time priority              (-r) 0           #real-time调度优先级
stack size              (kbytes, -s) 8192        #进程的栈的最大值为8192字节
cpu time               (seconds, -t) unlimited   #进程使用的CPU时间
max user processes              (-u) 3802        #当前用户同时打开的进程(包括线程)的最大个数为3802
virtual memory          (kbytes, -v) unlimited   #没有限制进程的最大地址空间
file locks                      (-x) unlimited   #所能锁住的文件的最大个数没有限制

需要重点关注的内核参数(1):ulimit -c

[root@Centos7 ~]# ulimit -c     #查看core file size大小,默认0是关闭状态,unlimited是没有限制状态
0

说明:
系统调试时用,当系统的某些进程出现问题,内部会生成一些core文件,我们通过查看这个core文件我们就可以知道发生了什么问题。具体使用在C和C++程序中,它们利用这些文件进行调试
#打开core file size无限制状态
[root@Centos7 ~]# ulimit -c unlimited
[root@Centos7 ~]# ulimit -c
unlimited

需要重点关注的内核参数(2):ulimit -f

[root@Centos7 ~]# ulimit -f     #系统文件的最大大小,默认unlimited是没有限制
unlimited

说明:
控制操作系统中文本文件的大小,有时候,我们的一些应用程序的日志文件,如果我们想控制日志文件的最大的量值的话,我们就需要对这个参数做一些限制。这个参数具体到底限制还是不限制,要具体思考。因为如果限制文件的大小。那么一旦到了文件设定的最大大小,应用程序就不能再写入日志文件了。

需要重点关注的内核参数(3):ulimit -n

[root@Centos7 ~]# ulimit -n
1024

说明:
这个参数是我们必须要调整的参数,在生产中1024这个数值基本是不够的。1024就代表系统的内存中同时只能放1024个文件的句柄(打开文件)。一般来说65536就够用了。
[root@Centos7 ~]# ulimit -n 65536
[root@Centos7 ~]# ulimit -n
65536

需要重点关注的内核参数(4):ulimit -u

[root@Centos7 ~]# ulimit -u      #系统用户同时打开的进程(线程)的最大数
3802

说明:
很多程序都是用普通用户运行的。用户能够同时并发启用的进程(线程)个数,就代表了我们程序的性能,很多程序都是支持高并发的。因此,这个参数我们是需要修改大的。一般来说65536就够用了
[root@Centos7 ~]# ulimit -u 65536
[root@Centos7 ~]# ulimit -u
65536

1.7.2 修改内核参数的几种修改方法

  • / etc / profile:所有用户生效,永久有效;
  • 〜/ .bash_profile中:当前用户生效,永久有效;
  • 直接在控制台输入,当前用户有效,临时生效;
  • 在/etc/security/limits.conf:指定用户或用户组生效,永久生效;
#在/etc/security/limits.conf中设定对内核的修改,永久生效
[root@Centos7 ~]# tail -12 /etc/security/limits.conf
#<domain>      <type>  <item>         <value>
#

#*               soft    core            0
#*               hard    rss             10000
#@student        hard    nproc           20
#@faculty        soft    nproc           20
#@faculty        hard    nproc           50
#ftp             hard    nproc           0
#@student        -       maxlogins       4

# End of file

说明:
<domain> 指定匹配参数修改的用户和用户组,*代表所有用户;
<type> 限制的类型soft软限制,hard硬限制;
<item> nproc代表最大进程数;nofile代表最大文件打开数;core代表限制内核文件大小;maxlogins代表此用户允许登陆的最大数目
<value> 具体的限制数值
hard硬限制:用户在任何时候都可以活动的进程的最大数量,这是上限。没有任何非root进程能够增加hard ulimit;
soft软限制:是对会话或进程实际执行的限制,但任何进程都可以将其增加到hard ulimit的最大值
#增加对系统内核参数的限制,修改完毕,退出登陆在进入即可生效
[root@Centos7 ~]# tail -4 /etc/security/limits.conf
* soft nofile 65536
* hard nofile 65536
* soft nproc 65536
* hard nproc 65536
#退出登录后,再验证
[root@Centos7 ~]# ulimit -a
core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 3802
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 65536        #已经改变
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 65536        #已经改变
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

1.8 线上服务器系统故障排查思路与关注点

(1)tail -f /usr/local/nginx/logs/error.log#服务应用类日志查询

(2)tail -f / var / log / messages#系统应用类日志查询

(3)tail -f / var / log / secure#登录日志查询

(4)dmesg#系统日志查询

(5)/ var / tmp,/ tmp#容易被攻击的点查询

(6)crontab -l,/ etc / crontab#计划任务查询(经常攻击对象)

[root@Centos7 ~]# tail -1000 /var/log/secure | grep Accepted
Oct  8 01:48:27 localhost sshd[1235]: Accepted password for root from 192.168.200.1 port 50704 ssh2
Oct  8 02:19:58 localhost sshd[1832]: Accepted password for root from 192.168.200.1 port 50882 ssh2
Oct  8 03:12:44 localhost sshd[11788]: Accepted password for root from 192.168.200.1 port 51149 ssh2
Oct  8 03:18:29 Centos7 sshd[907]: Accepted password for root from 192.168.200.1 port 51185 ssh2
Oct  8 03:32:35 Centos7 sshd[1103]: Accepted publickey for root from 192.168.200.1 port 51258 ssh2: RSA SHA256:niDwzrv1Xq4er0zH0FqfHpvRiCJfPUcnL3vT89ZEt0M
Oct  8 03:41:10 Centos7 sshd[905]: Accepted publickey for root from 192.168.200.1 port 51326 ssh2: RSA SHA256:niDwzrv1Xq4er0zH0FqfHpvRiCJfPUcnL3vT89ZEt0M
Oct  8 03:41:46 Centos7 sshd[1107]: Accepted publickey for root from 192.168.200.1 port 51331 ssh2: RSA SHA256:niDwzrv1Xq4er0zH0FqfHpvRiCJfPUcnL3vT89ZEt0M
Oct  8 03:42:38 Centos7 sshd[1127]: Accepted publickey for root from 192.168.200.1 port 51341 ssh2: RSA SHA256:niDwzrv1Xq4er0zH0FqfHpvRiCJfPUcnL3vT89ZEt0M
Oct  8 03:42:59 Centos7 sshd[1146]: Accepted publickey for root from 192.168.200.1 port 51352 ssh2: RSA SHA256:niDwzrv1Xq4er0zH0FqfHpvRiCJfPUcnL3vT89ZEt0M
Oct  8 03:44:46 Centos7 sshd[1166]: Accepted publickey for root from 192.168.200.1 port 51355 ssh2: RSA SHA256:niDwzrv1Xq4er0zH0FqfHpvRiCJfPUcnL3vT89ZEt0M
Oct  8 04:52:42 Centos7 sshd[1239]: Accepted publickey for root from 192.168.200.143 port 49638 ssh2: RSA SHA256:niDwzrv1Xq4er0zH0FqfHpvRiCJfPUcnL3vT89ZEt0M
Oct  8 04:54:17 Centos7 sshd[1257]: Accepted publickey for root from 192.168.200.143 port 49640 ssh2: RSA SHA256:niDwzrv1Xq4er0zH0FqfHpvRiCJfPUcnL3vT89ZEt0M
Oct  8 05:09:39 Centos7 sshd[1318]: Accepted publickey for yunjisuan from 192.168.200.1 port 51755 ssh2: RSA SHA256:niDwzrv1Xq4er0zH0FqfHpvRiCJfPUcnL3vT89ZEt0M

2. Linux的系统性能优化思路和方法

2.1 影响Linux性能的CPU,内存,磁盘,网络等因素分析

2.1.1 CPU

如何判断多核CPU与超线程

  • 顶部命令按数字1,可以看到CPU一共有多少核
  • 在/ PROC内/ cpuinfo查看并过滤

image.png-45.2kB

#查看/proc/cpuinfo里部分信息
[root@Centos7 ~]# cat /proc/cpuinfo
processor   : 0         #CPU的线程号,过滤这个获得服务器的总线程数
vendor_id   : GenuineIntel
cpu family  : 6
model       : 61
model name  : Intel(R) Core(TM) i5-5200U CPU @ 2.20GHz
stepping    : 4
microcode   : 0x11
cpu MHz     : 2194.917
cache size  : 3072 KB
physical id : 0     #CPU的物理ID号。过滤这个获得服务器的物理CPU数
siblings    : 1
core id     : 0     #表示当前的信息出自physical id为0的CPU上的第一个核心
cpu cores   : 1     #表示当前物理physical id为0的CPU上有4个核心
apicid      : 0
initial apicid  : 0
fpu     : yes
fpu_exception   : yes
cpuid level : 20
wp      : yes
flags       : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc aperfmperf eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch epb fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid rdseed adx smap xsaveopt dtherm ida arat pln pts
bogomips    : 4389.83
clflush size    : 64
cache_alignment : 64
address sizes   : 42 bits physical, 48 bits virtual
power management:
#查看物理服务器有几个物理CPU
[root@Centos7 ~]# cat /proc/cpuinfo | grep "physical id"
physical id : 0    #物理id号

[root@Centos7 ~]# cat /proc/cpuinfo | grep "physical id" | uniq 
physical id : 0
[root@Centos7 ~]# cat /proc/cpuinfo | grep "physical id" | uniq  | wc -l     #服务器一共有一个物理CPU
1

#查看物理服务器的CPU的总线程数
[root@Centos7 ~]# cat /proc/cpuinfo | grep "processor" | uniq  | wc -l
1

#查看物理服务器的CPU的总核心数
[root@Centos7 ~]# cat /proc/cpuinfo | grep "core id" | wc -l
1

主要消耗CPU的业务:动态Web服务,邮件服务

2.1.2 内存

  • 物理内存与交换的取舍
  • 选择64位的Linux操作系统

如果物理内存大于16G,那么交换等于物理内存或者是物理内存两倍都是可以的。
如果物理内存小于16G,那么交换和内存大小保持一致。

消耗内存的业务:内存数据库(redis的/ HBase的/ mongodb的)

2.1.3 磁盘I / O.

  • RAID技术(RAID 0/1/5/10)
  • SSD磁盘

消耗磁盘的业务:数据库服务器

2.1.4 网络带宽

  • 网卡/交换机的选择:起码千兆网卡/千兆普通交换机/万兆核心交换机
  • 操作系统双网卡绑定:通过绑定提高网卡带宽吞吐量

消耗带宽的业务:分布式文件系统,视频业务平台

2.2 影响Linux性能的操作系统相关资源分析

2.2.1 系统安装优化

磁盘分区,RAID设置,交换设置

2.2.2 内核参数优化

  • ulimit -n(文件最大打开数)
  • ulimit -u(最大用户的进程数)

2.2.3 文件系统优化

  • EXT4:Linux的原生态文件格式
  • XFS:Centos7开始默认支持

应用建议:
读操作频繁,同时小文件众多的应用:首选EXT4系统-文件
写操作频繁的应用,首选的xfs。

2.2.4 程序问题

此类问题需要开发人员查看代码,介入处理。但作为运维人员需要给出程序问题的有力证据。

2.3 系统性能调优之CPU性能评估工具与优化经验

2.3.1 vmstat(系统默认自带)

利用vmstat的命令可以对操作系统的内存信息,进程状态,CPU活动等进行监视。

[root@Centos7 ~]# which vmstat
/usr/bin/vmstat

[root@Centos7 ~]# vmstat 3 5          #每3秒刷新一次,输出5次数据
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 2  0      0 810276   2124 108028    0    0    13     1   41   63  0  0 99  0  0
 1  0      0 810276   2124 108060    0    0     0     0   66   82  0  0 100  0  0
 0  0      0 810276   2124 108060    0    0     0     0   65   78  0  0 100  0  0
 0  0      0 810276   2124 108060    0    0     0     0   64   78  0  0 100  0  0
 0  0      0 810276   2124 108060    0    0     0     0   65   76  0  0 100  0  0

特效:

  • r列表示运行和等待cpu时间片的进程数,这个值如果长期大于系统CPU个数,说明CPU不足,需要增加CPU
  • b列表示在等待资源的进程数,比如正在等待I / O,或者内存交换等。长期大于0,那么说明CPU不足

记忆:

  • swpd列表示切换到内存交换区的内存数量(以k为单位)。如果swpd的值不为0,或者比较大,只要si,so的值长期为0,这种情况下一般不用担心,不会影响系统性能。
  • 免费列表示当前空闲的物理内存数量(以k为单位)
  • buff列表示缓存的内存数量,一般对块设备的读写才需要缓冲。
  • 缓存列表示页面缓存的内存数量,一般作为文件系统缓存,频繁访问的文件都会被缓存,如果缓存值较大,说明缓存的文件数较多,如果此时IO中bi比较小,说明文件系统效率比较好。

交换

  • si列表示由磁盘调入内存,也就是内存进入内存交换区的数量
  • 所以列表示由内存调入磁盘,也就是内存交换区进入内存的数量

一般情况下,SI,SO的值都为0,如果SI,SO的值长期不为0,则表示系统内存不足。需要增加系统内存。


IO项显示磁盘读写状况

  • 双列表示从块设备读入数据的总量(即读磁盘)(每秒KB)
  • 柏列表示写入到块设备的数据重量(即写磁盘)(每秒KB)

这里我们设置的双+博参考值为1000,如果超过1000,而且WA值较大,则表示系统磁盘IO有问题,应该考虑提高磁盘的读写性能。


system显示采集间隔内发生的中断数

  • 在列表示在某一时间间隔中观测到的每秒设备中断数
  • cs列表示每秒产生的上下文切换次数

上边这两个值越大,会看到由内核消耗的CPU时间会越多


CPU项显示了CPU的使用状态,此列是我们关注的重点

  • 我们列显示了用户进程消耗的CPU时间百分比.us的值比较高时,说明用户进程消耗的cpu时间多,但是如果长期大于50%,就需要考虑优化程序或算法。
  • sy列显示了内核进程消耗的CPU时间百分比.Sy的值比较高时,说明内核消耗的CPU资源很多。
  • 美国+ SY的参考值为80%,如果我们+ SY大于80%说明可能存在CPU资源不足。
  • d列显示了CPU处于空闲状态的时间百分比。
  • wa列显示了IO等待所占用的CPU时间百分比。
  • WA值越高,说明IO等待越严重,根据经验,WA的参考值为20%时,如果WA超过20%,说明IO等待严重,引起IO等待的原因可能是磁盘大量随机读写造成的,也可能是磁盘或者磁盘控制器的带宽瓶颈造成的(主要是块操作)

综上所述:
在对CPU的评估中,需要重点注意的是特效项[R列的值和CPU项中我们,SY和ID列的值

2.3.2 iostat

[root@Centos7 ~]# iostat -c 3 5
Linux 3.10.0-862.3.3.el7.x86_64 (Centos7.5)     2018年09月01日     _x86_64_    (8 CPU)
avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.03    0.00    0.02    0.00    0.00   99.95
avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.00    0.00    0.00    0.00    0.00  100.00
avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.00    0.00    0.00    0.00    0.00  100.00
avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.00    0.00    0.04    0.00    0.00   99.96
avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.00    0.00    0.00    0.00    0.00  100.00

2.3.3 正常运行时间和w

[root@Centos7 ~]# uptime
 00:40:01 up  2:01,  2 users,  load average: 0.03, 0.05, 0.05
 
[root@Centos7 ~]# w
 00:40:03 up  2:01,  2 users,  load average: 0.03, 0.05, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1                      00:22   16:11   0.03s  0.03s -bash
root     pts/0    192.168.200.1    00:25    3.00s  0.03s  0.00s w

2.4 系统性能调优之内存性能,磁盘性能评估工具与优化经验

2.4.1 free -m(评估内存)

#查看Centos6.x的内存情况
[root@Centos6 ~]# free -m
             total       used       free     shared    buffers     cached
Mem:           980        300        680          0         17        187
-/+ buffers/cache:         96        884
Swap:         1983          0       1983

说明:
系统可使用的剩余内存容量:884M   ===> 系统剩余内存 680M + buffers 17M + cached 187M
通过swap可以看出来,交换分区使用量为0,说明系统内存资源还非常充足。
#查看Centos7.x的内存情况
[root@Centos7 ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:            974          76         710           7         187         737
Swap:          2047           0        2047

说明:
系统剩余内存容量:710M
buffers+cache容量:187M
系统可用剩余内存容量为:available 737M

一般情况我们可以这样去判断内存:

  • 系统可用剩余内存总量/系统物理内存重量> 70%时,表示系统内存资源非常充足,不影响系统性能;
  • 系统可用剩余内存总量/系统物理内存重量<20%时,表示系统内存资源紧缺,需要增加系统内存;
  • 20%<系统可用剩余内存总量/系统物理内存重量<70%时,表示系统内存资源基本能满足应用需求,暂时不影响系统性能。

2.4.2 sar(主要评估内存)

This is mainly the case of two commands used to monitor all or specified processes consume system resources, such as CPU, memory, and device I / O

Three common parameters: -u (CPU status acquisition), - R (acquisition memory status), - d (Disk Get)

[root@Centos7 ~]# sar -u 3       #每3秒获取一次CPU状态信息
Linux 3.10.0-862.3.3.el7.x86_64 (Centos7.5)     2018年09月02日     _x86_64_    (8 CPU)
15时46分38秒     CPU     %user     %nice   %system   %iowait    %steal     %idle
15时46分41秒     all      0.00      0.00      0.04      0.00      0.00     99.96
15时46分44秒     all      0.00      0.00      0.00      0.00      0.00    100.00

[root@Centos7 ~]# sar -r 3      #每3秒获取一次MEM状态信息
Linux 3.10.0-862.3.3.el7.x86_64 (Centos7.5)     2018年09月02日     _x86_64_    (8 CPU)
15时47分57秒 kbmemfree kbmemused  %memused kbbuffers  kbcached  kbcommit   %commit  kbactive   kbinact   kbdirty
15时48分00秒    586576    411396     41.22      2684    256608    193496      6.25    191660    103324         0
15时48分03秒    586584    411388     41.22      2684    256608    193496      6.25    191660    103324         0

说明:
kbmemfree:表示空闲物理内存大小
kbmemused:表示已经使用的物理内存大小
%memused:表示已经使用内存占总内存百分比
kbbuffers和kbcached:表示buffers和cache占用的大小
kbcommit和%commit分别表示应用程序当前使用的内存大小和使用百分比

2.4.3 iostat -d combinations (primarily to assess disk)

[root@Centos7 ~]# iostat -d 2 3
Linux 3.10.0-862.3.3.el7.x86_64 (Centos7.5)     2018年09月02日     _x86_64_    (8 CPU)
Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda               0.62        16.00         6.85     207832      89039
scd0              0.02         0.23         0.00       2978          0
dm-0              0.48        15.20         6.70     197510      86991
dm-1              0.01         0.17         0.00       2228          0
Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda               0.00         0.00         0.00          0          0
scd0              0.00         0.00         0.00          0          0
dm-0              0.00         0.00         0.00          0          0
dm-1              0.00         0.00         0.00          0          0
Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda               0.00         0.00         0.00          0          0
scd0              0.00         0.00         0.00          0          0
dm-0              0.00         0.00         0.00          0          0
dm-1              0.00         0.00         0.00          0          0

说明:
DEV:表示磁盘设备名称
tps:表示每秒到物理磁盘的传送数,也就是每秒的I/O流量。一个传送就是一个I/O请求,多个逻辑请求可以被合并为一个物理I/O请求。
kB_read/s:每秒读取的数据块
kB_wrtn/s:每秒写入的数据块
kB_read:读取的所有数据块总数
kB_wrtn:写入的所有数据块总数

2.5 System tuning of network performance evaluation tool

2.5.1 ping command

[root@Centos7 ~]# ping 127.0.0.1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.016 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.049 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.051 ms
^C
--- 127.0.0.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.016/0.038/0.051/0.017 ms

说明:
     在这个输出中,time值表示了两台主机之间的网络延时情况,如果值很大,则表示网络的延时很大,单位为毫秒。在这个输出的最后,还有一个统计总结。packet loss表示网络丢包率,值越小,网络质量越高
2.5.2 netstat command
  • netstat -rn (see routing case) <====> route -n
  • netstat -i (see the network interface status)
[root@Centos7 ~]# netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         192.168.200.2   0.0.0.0         UG        0 0          0 ens32
169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 ens32
192.168.200.0   0.0.0.0         255.255.255.0   U         0 0          0 ens32

[root@Centos7 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.200.2   0.0.0.0         UG    0      0        0 ens32
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 ens32
192.168.200.0   0.0.0.0         255.255.255.0   U     0      0        0 ens32

[root@Centos7 ~]# netstat -i
Kernel Interface table
Iface      MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
ens32     1500     1189      0      0 0           868      0      0      0 BMRU
lo       65536       70      0      0 0            70      0      0      0 LRU

2.5.3 mtr / traceroute command

Tracking network routing status, it is recommended to use the subway, dynamic tracking network routing for troubleshooting network problems is very convenient.

#安装命令
[root@Centos6 ~]# yum -y install traceroute mtr
[root@Centos6 ~]# rpm -qa traceroute mtr
traceroute-2.0.14-2.el6.x86_64
mtr-0.75-5.el6.x86_64

#traceroute追踪到www.baidu.com的路由器
[root@Centos6 ~]# traceroute www.baidu.com
traceroute to www.baidu.com (111.13.100.92), 30 hops max, 60 byte packets
 1  192.168.200.2 (192.168.200.2)  0.327 ms  0.206 ms  0.172 ms
 2  * * *
 3  * * *
 4  * * *
 5  * * *
 6  * * *
 7  * * *
#中间省略。。。
29  * * *
30  * * *

Although traceroute packets to track, but not Intuitively
mtr -c command -n not resolve the host transmits the number of packets --report results show, no dynamic

image.png-44.2kB

2.6 Linux operating system performance analysis criteria

Factors affecting performance Criteria Criteria Criteria
it is good Bad bad
CPU User% + SYS% <70% User% + SYS% = 85% User% + SYS%> = 90%
RAM Switch input (si) = 0; switching output (so) = 0 Each CPU 10 pages / sec More exchange and exchange
Disk Iowa% <20% iowait%= 35% iowait%> = 50%
  • User%: indicates the percentage of time the CPU user mode
  • % SYS: indicates the percentage of time the CPU is in the system mode
  • % IOWAIT: indicates the percentage of time the CPU waits for completion of input and output
  • Exchange: i.e. si, represents the virtual memory page introduction, i.e. from the exchange to the RAM SWAP DISK

Guess you like

Origin www.cnblogs.com/ywb123/p/11124120.html