故障排查与优化深入:生产环境服务器安全策略与系统性能优化评估

本文分享了Linux运维经验,包括精简安装策略、网络设置、Selinux和iptables配置、SSH安全策略、系统更新与NTP时钟服务、内核参数优化、系统故障排查思路与性能优化方法。详细讲解了如何确保服务器安全并提升性能。
摘要由CSDN通过智能技术生成
Linux运维经验分享与故障排查思路
  1. 线上服务器安装基本策略和经验
    精简安装策略:
  • 仅安装需要的,按需安装,不用不装
  • 开发包,基本网络包,基本应用包
    CentOS6.x
    在这里插入图片描述在这里插入图片描述CentOS7.x
    在这里插入图片描述系统盘按照默认分区方式
    数据盘单独挂载
  1. 线上服务器网络设置经验和技巧

2.1 Centos7.x下最好关闭的服务
该服务专门用来管理网络,如果不关闭该服务,当我们修改IP地址后可能会出现IP地址为改变的情况
在这里插入图片描述

#关闭NetworkManager服务,并关闭开机启动
[root@localhost ~]# systemctl stop NetworkManager
[root@localhost ~]# 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@localhost ~]# systemctl status NetworkManager
● NetworkManager.service - Network Manager
   Loaded: loaded (/usr/lib/systemd/system/NetworkManager.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:NetworkManager(8)

如果不关闭此服务,那么此服务会接管Linux的网络设置。有时候会导致修改了网卡配置文件IP,但是网卡的IP不变的情况

2.2 关于DNS的设置
(1)临时修改DNS设置,修改立即生效,重启服务器或重启网络后恢复

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

修改/etc/resolv.conf文件里的nameserver,DNS即可被修改,立刻生效
但是重启网络或者重启服务器/etc/resolv.conf里的nameserver设置会被网卡配置文件的设置覆盖

[root@localhost ~]# vim /etc/resolv.conf
; generated by /usr/sbin/dhclient-script
search localdomain
nameserver 192.168.100.3			 #修改了此行配置
[root@localhost ~]# systemctl restart network
[root@localhost ~]# cat /etc/resolv.conf 
; generated by /usr/sbin/dhclient-script
search localdomain
nameserver 192.168.100.2				 #配置还原了

(2)永久修改DNS设置

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

2.3 关于服务器自身主机名的修改
Centos7.x

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

2.4 关于服务器对自身主机名的映射

#映射服务器自身的主机名
[root@Centos7 ~]# echo "127.0.0.1 Centos7" >> /etc/hosts
[root@Centos7 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 Centos7

请注意服务器映射自己的主机名,务必映射为127.0.0.1不要映射成网卡的IP
这是因为很多服务的运行都要验证自身的主机名是否被映射,不然会导致未知的故障

  1. 线上服务器Selinux,iptables策略设置

3.1 selinux配置(如何关闭selinux)

[root@Centos7 ~]# sestatus
SELinux status:                 disabled    #selinux 目前处于关闭状态

#永久关闭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

3.2 iptables配置
如果我们的机房没有硬件防火墙的话,那么我们必须通过iptables对拥有公网网卡的服务器做安全

#防火墙配置文件/etc/sysconfig/iptables-config
#推荐配置
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    		#放开公司内网环境 1.1.1.0/24网段的22端口给运维人员访问
iptables -A INPUT -s 2.2.2.2/32 -p tcp -m tcp --dport 22 -j ACCEPT			
iptables -A INPUT -i eth1 -j ACCEPT				#允许所用通过eth1进来的用户访问
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT				#以下是对web服务器的tcp连接做限制,防止tcp握手攻击
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
  • 保存防火墙规则:iptables_save=“”
  • 禁止某个IP地址或网段访问
  • [ ](1)防火墙iptables -A INPUT -s1.1.1.0/24 -j DROP
  • [ ](2)nginx配置文件中location拒绝
  • [ ](3) nginx,deny
  • 允许所用通过eth1进来的用户访问:iptables -A INPUT -i eth1 -j ACCEPT
  • 可能出现的情况(web服务器):
    (1)不允许外网通过eth0网卡访问,但不限制内网通过eth1网卡出去
    (2)eth1为心跳线的网卡,允许心跳包通过
  • 企业防火墙规则:
  1. 打开所有内网,运维和开发各留两个内网网段
  2. web服务器打开tcp的80(8080)端口
  3. 存在心跳线的话,放开心跳线网卡
  4. 对数据包进行转发,是数据包能够从内网出外网,对外网进内网的数据包做限制
  5. 对web服务器的tcp连接做限制(模板)
  1. 线上服务器ssh登陆安全策略

4.1 ssh登陆策略
(1)登陆策略
备份:cp /etc/ssh/sshd_config{,.bak}

[root@www ~]# cat -n /etc/ssh/sshd_config.bak | 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@www ~]# cat -n /etc/ssh/sshd_config | sed -n '17p;38p;43p;47p;65p;79p;115p'
    17  Port 22221                  #工作中需要设定到1万以上的端口,避免被扫描出来(生产环境),因为nmap能扫描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)设置xshell私钥登陆Linux

公钥在服务端,私钥在客户端,也就是说公钥只有一个,私钥可以有许多个;若想登陆对方服务器就需要把公钥给对方;
当用xshell登陆webA时,需要把私钥给xshell

#查看服务器端IP
[root@www .ssh]# hostname -I
192.168.200.174

#在Linux服务器端生成rsa密钥对
[root@www ~]# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
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:royhAEKx9bhe4jLZ3SzfZ/yvhkzPgToDIx+1gSxoOLM root@www
The keys randomart image is:
+---[RSA 2048]----+
| . .             |
|  + o            |
| o..... .        |
|.+ o.. o o       |
|o =o .. S o .    |
|oE= +.o= . o .   |
|.+ +.ooo= = + .  |
| .o. +oo.+ * +   |
|  . . o. .= ooo. |
+----[SHA256]-----+

#将生成的公钥导入到服务器端的~/.ssh/authorized_keys文件里
[root@www ~]# cd .ssh/
[root@www .ssh]# ls
authorized_keys  id_rsa  id_rsa.p
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值