用于ssh后上传下载工具
yum -y install lrzsz
rz 上传 sz ** 下载指定文件
2、fail2ban用于登陆防密码爆破
yum -y install fail2ban
修改配置文件保存
cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.conf.bak #备份源文件 vi /etc/fail2ban/jail.conf #修改配置文件
[DEFAULT] # 以空格分隔的列表,可以是 IP 地址、CIDR 前缀或者 DNS 主机名 # 用于指定哪些地址可以忽略 fail2ban 防御 ignoreip = 10.62.191.2 # 客户端主机被禁止的时长(秒) bantime = 900 # 客户端主机被禁止前允许失败的次数 maxretry = 3 # 查找失败次数的时长(秒) findtime = 600 mta = sendmail [ssh-iptables] enabled = true filter = sshd action = iptables[name=SSH, port=ssh, protocol=tcp] #sendmail-whois[name=SSH-Fail2ban, dest=example@mail.com, sender=fail2ban@email.com] # Red Hat 系的发行版 logpath = /var/log/secure # ssh 服务的最大尝试次数 maxretry = 3
重启服务
systemctl restart fail2ban3、htop或glances
yum -y install htop yum -y install glances
调用命令为
htop
或
glances4、tmux
yum -y install tmux
命令启用 tumx
左右分屏ctrl+b “ 上下分屏 ctrl+b % 分屏切换ctrl+b 上下左右键切换
5、net-toolsyum install net-tools -y
查看网卡
ifconfig
查看监控端口
netstat -tunlp #用于显示 tcp,udp 的端口和进程等相关情况。
netstat 查看端口占用语法格式:
netstat -tunlp | grep 端口号
- -t (tcp) 仅显示tcp相关选项
- -u (udp)仅显示udp相关选项
- -n 拒绝显示别名,能显示数字的全部转化为数字
- -l 仅列出在Listen(监听)的服务状态
- -p 显示建立相关链接的程序名
例如查看 8000 端口的情况,使用以下命令:
# netstat -tunlp | grep 8000 tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 26993/nodejs
更多命令:
netstat -ntlp //查看当前所有tcp端口 netstat -ntulp | grep 80 //查看所有80端口使用情况 netstat -ntulp | grep 3306 //查看所有3306端口使用情况
6、iptables
yum -y install iptables常用配置
vi /etc/sysconfig/iptables
# Generated by iptables-save v1.4.21 on Mon Oct 25 21:27:57 2021 *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [228:45504] :DOCKER - [0:0] :syn-flood - [0:0] -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -i docker0 -j ACCEPT -A INPUT -i docker0 -p tcp -m tcp --dport 3306 -j ACCEPT -A INPUT -i docker0 -p tcp -m tcp --dport 6379 -j ACCEPT -A INPUT -i docker0 -p tcp -m tcp --dport 8088 -j ACCEPT -A INPUT -s 172.17.0.0/24 -p tcp -m tcp --dport 3306 -j ACCEPT -A INPUT -s 172.17.0.0/24 -p tcp -m tcp --dport 6379 -j ACCEPT -A INPUT -s 172.17.0.0/24 -p tcp -m tcp --dport 8088 -j ACCEPT -A INPUT -s 10.0.0.0/9 -p tcp -m state --state NEW -m tcp --dport 6081 -j ACCEPT -A INPUT -s 10.62.191.0/27 -p tcp -m tcp --dport 9000 -j ACCEPT -A INPUT -s 192.168.15.0/24 -p tcp -m tcp --dport 9000 -j ACCEPT -A INPUT -s 10.62.191.0/27 -p tcp -m tcp --dport 3306 -j ACCEPT -A INPUT -s 10.62.248.0/21 -p tcp -m tcp --dport 3306 -j ACCEPT -A INPUT -s 10.62.248.0/21 -p tcp -m tcp --dport 22 -j ACCEPT -A INPUT -s 10.62.191.0/27 -p tcp -m tcp --dport 8088 -j ACCEPT -A INPUT -s 10.62.191.0/27 -p tcp -m tcp --dport 22 -j ACCEPT -A INPUT -s 10.62.248.0/21 -p tcp -m tcp --dport 8088 -j ACCEPT -A INPUT -s 10.62.248.0/21 -p tcp -m tcp --dport 6379 -j ACCEPT -A INPUT -j REJECT --reject-with icmp-host-prohibited -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j syn-flood -A INPUT -i ens192 -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -m connlimit --connlimit-above 15 --connlimit-mask 32 --connlimit-saddr -j DROP -A INPUT -p tcp -m state --state RELATED,ESTABLISHED -j ACCEPT -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -m limit --limit 12/sec --limit-burst 24 -j ACCEPT -A FORWARD -i docker0 -o ens192 -j ACCEPT -A FORWARD -i ens192 -o docker0 -j ACCEPT -A FORWARD -i docker0 -o docker0 -j ACCEPT -A FORWARD -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -m limit --limit 1/sec -j ACCEPT -A OUTPUT -p tcp -m tcp --sport 80 -m state --state NEW,ESTABLISHED -j ACCEPT -A DOCKER -d 172.17.0.6/32 ! -i docker0 -o docker0 -p tcp -m tcp --dport 2222 -j ACCEPT -A DOCKER -d 172.17.0.6/32 ! -i docker0 -o docker0 -p tcp -m tcp --dport 443 -j ACCEPT -A DOCKER -d 172.17.0.6/32 ! -i docker0 -o docker0 -p tcp -m tcp --dport 80 -j ACCEPT -A syn-flood -p tcp -m limit --limit 3/sec --limit-burst 6 -j RETURN -A syn-flood -j REJECT --reject-with icmp-port-unreachable COMMIT # Completed on Mon Oct 25 21:27:57 2021 # Generated by iptables-save v1.4.21 on Mon Oct 25 21:27:57 2021 *nat :PREROUTING ACCEPT [11:932] :INPUT ACCEPT [10:600] :OUTPUT ACCEPT [10:520] :POSTROUTING ACCEPT [10:520] :DOCKER - [0:0] -A PREROUTING -m addrtype --dst-type LOCAL -j DOCKER -A OUTPUT ! -d 127.0.0.0/8 -m addrtype --dst-type LOCAL -j DOCKER -A POSTROUTING -s 172.17.0.5/32 -j MASQUERADE -A POSTROUTING -s 172.17.0.0/16 ! -o docker0 -j MASQUERADE -A POSTROUTING -s 172.17.0.5/32 -o bridge0 -j MASQUERADE -A POSTROUTING -s 172.17.0.5/32 -o docker0 -j MASQUERADE -A POSTROUTING -s 172.17.0.6/32 -d 172.17.0.6/32 -p tcp -m tcp --dport 2222 -j MASQUERADE -A POSTROUTING -s 172.17.0.6/32 -d 172.17.0.6/32 -p tcp -m tcp --dport 443 -j MASQUERADE -A POSTROUTING -s 172.17.0.6/32 -d 172.17.0.6/32 -p tcp -m tcp --dport 80 -j MASQUERADE -A DOCKER -i docker0 -j RETURN -A DOCKER ! -i docker0 -p tcp -m tcp --dport 2222 -j DNAT --to-destination 172.17.0.6:2222 -A DOCKER ! -i docker0 -p tcp -m tcp --dport 10443 -j DNAT --to-destination 172.17.0.6:443 -A DOCKER ! -i docker0 -p tcp -m tcp --dport 8088 -j DNAT --to-destination 172.17.0.6:80 COMMIT7、ssh 密钥设置
Linux Public Key 配置
使用一种被称为 “公私钥” 认证的方式来进行 ssh 登录. “公私钥” 认证方式简单的解释:首先在客户端上创建一对公私钥 (公钥文件:/.ssh/id_rsa.pub; 私钥文件:/.ssh/id_rsa)
然后把公钥放到服务器上(~/.ssh/authorized_keys), 自己保留好私钥。在使用 ssh 登录时,ssh 程序会发送私钥去和服务器上的公钥做匹配。如果匹配成功就可以登录了。
步骤如下:先要在本地生成一个 rsa 的公共 key,然后 copy 到远程你要认证的服务器创建 key)
-
本地
SYSIN-SRV# ssh-keygen -t rsa -b 4096 #可选参数 -b 2048,CentOS7 中默认 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:MfZHl/b8XhGTU59fhLHptmzNFnhCrorySvGojyb69dM root@SYSIN-SRV #root@SYSIN-SRV 这里是当前用户名@主机名,公钥文件中可以删除 The key's randomart image is: +---[RSA 2048]----+ |+= = . . .... | |O.E + o . | |*+ o . . | |+o. . . o. . . | |+ o o .SBo . | | . + oo++=+ | | o +. o.+. | | . o.. o . | | . ... .. | +----[SHA256]-----+
直接三次回车即可,这里会提示输入密码 passphrase,这个密码与与远程 ssh 登录密码无关。Public-key 证机制比密码要安全,因为密码不在网络上传输,而且可以是用加密的方式存储的。为了安全,也可以设置一个 passphrase,这样,别人拿到密钥也没有用。
# cd .ssh/ # ls id_rsa id_rsa.pub known_hosts # cp id_rsa.pub authorized_keys //本机公钥配置,允许 ssh localhost 免密登录 //id_rsa 文件复制到其他机器上复用,权限需要设置为600 # chmod 600 id_rsa #ssh 对目录的权限有要求,建议设置如下 chmod 700 ~/.ssh chmod -R 600 ~/.ssh/*
| Element | Permission |
| .ssh directory | 700 ((drwx——) |
| public keys: id_rsa_pub (=authorized_keys) | 644 (-rw-r–r–) |
| private keys: id_rsa | 600 (-rw——-) |
| authorized_keys | 600 (-rw——-) |
| known_hosts | 600 (-rw——-) |
| config | 600 (-rw——-) |
Correct file permissions on ~/.ssh folder and its content:
(2)将公钥文件复制到远程服务器上:
ssh-copy-id root@目标节点IP
这里 root 是在目标节点上登录的用户,@ 符号后面接目标节点 IP 即可,之后会提示输入目标节点 root 用户密码,输入即可。
添加认证信息后,目标节点主机的 ~/.ssh/ 目录下将会出现一个 authorized_keys 文件,里面包含了管理节点的公钥信息,可以检查一下是否存在。
保存好上述生成的公钥和私钥文件后,可以上传到其他主机(或者网络设备)上直接使用:
安装上传工具 yum install -y lrzsz
rz上传 sz下载
## 直接上传已有的 Public key mkdir ~/.ssh cd ~/.ssh 上传 id_rsa 和 id_rsa.pub 到当前目录 mv id_rsa.pub authorized_keys chmod 700 ~/.ssh chmod -R 600 ~/.ssh/* ## 测试访问是否正常 ssh localhost
(3)修改 sshd 配置文件:
启用 Public key 认证并关闭传统密码认证,重启 sshd 服务生效。
## CentOS 7 默认配置如下 ## 取消注释#PubkeyAuthentication yes sed -i 's/^#(PubkeyAuthentication yes)/1/' /etc/ssh/sshd_config ## 修改为PasswordAuthentication no,即替换行 sed -i '/^PasswordAuthentication yes/c PasswordAuthentication no' /etc/ssh/sshd_config ## 写入文件需要添加 -i 参数 ## 重启sshd服务 systemctl restart sshd8、ifstat
介绍
ifstat工具是个网络接口监测工具,比较简单看网络流量
实例
默认使用
#ifstat
eth0 eth1
KB/s in KB/s out KB/s in KB/s out
0.07 0.20 0.00 0.00
0.07 0.15 0.58 0.00
默认ifstat不监控回环接口,显示的流量单位是KB。
监控所有网络接口
# ifstat -a
lo eth0 eth1
KB/s in KB/s out KB/s in KB/s out KB/s in KB/s out
0.00 0.00 0.28 0.58 0.06 0.06
0.00 0.00 1.41 1.13 0.00 0.00
0.61 0.61 0.26 0.23 0.00 0.00
ifstat比较简单看网络流量概况。
9、iftop介绍
iftop是一款实时流量监控工具,监控TCP/IP连接等,缺点就是无报表功能。必须以root身份才能运行。
实例
默认是监控第一块网卡的流量
iftop
监控eth1
iftop -i eth1
直接显示IP, 不进行DNS反解析
iftop -n
直接显示连接埠编号, 不显示服务名称:
iftop -N
显示某个网段进出封包流量
iftop -F 192.168.1.0/24 or 192.168.1.0/255.255.255.010、修改yum 源 和 NTP时间同步
#Yum源更换为国内阿里源 yum install wget -y mv -f /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 #添加阿里的epel源 #add the epel wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo #yum重新建立缓存 yum clean all yum makecache #同步时间 yum -y install ntp /usr/sbin/ntpdate ntp1.aliyun.com echo "* 4 * * * /usr/sbin/ntpdate ntp1.aliyun.com > /dev/null 2>&1" >> /var/spool/cron/root systemctl restart crond.service11、杀毒clamav
#install clamav yum -y install epel-release yum -y install clamav-server clamav-data clamav-update clamav-filesystem clamav clamav-scanner-systemd clamav-devel clamav-lib clamav-server-systemd #update databases freshclam #auto start update databases chown -R clamupdate:clamupdate /var/lib/clamav chmod 775 /var/lib/clamav/ systemctl enable clamav-freshclam systemctl start clamav-freshclam #config sed -ri 's/^User(.*)$/User root/' /etc/clamd.d/scan.conf #扫描日志文件/var/log/clamd.scan sed -ri 's~^#LogFile /var/log/clamd.scan~LogFile /var/log/clamd.scan~' /etc/clamd.d/scan.conf sed -ri 's~^#PidFile /run/clamd.scan/clamd.pid~PidFile /run/clamd.scan/clamd.pid~' /etc/clamd.d/scan.conf sed -ri 's~^#DatabaseDirectory /var/lib/clamav~DatabaseDirectory /var/lib/clamav~' /etc/clamd.d/scan.conf sed -ri 's~^#LocalSocket /run/clamd.scan/clamd.sock~LocalSocket /run/clamd.scan/clamd.sock~' /etc/clamd.d/scan.conf #auto start clamd server systemctl enable clamd@scan systemctl start clamd@scan
扫描杀毒
用clamdscan命令扫描,需要开启服务才能使用。速度快,默认会递归扫描子目录
病毒测试代码:将下面的内容保存为txt,通过ClamAV扫描
X5O!P%@AP[4PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*
1、扫描某个目录
clamdscan /opt/test #扫描/opt/test目录
如下图扫描/opt/test/,找到两个异常文件
扫描完成后,会将扫描详细信息列出
2、扫描并处理病毒文件
clamdscan test/ --remove
添加参数: --remove #将扫描到的病毒文件从系统删除。 --remove /Quarantine #将扫描到的病毒文件移到/Quarantine目录下
3、扫描日志文件 /var/log/clamd.scan
三、周期定时更新和定时杀毒
crontab -e
crontab文件如下:
1 3 * * * /usr/bin/freshclam --quiet #让服务器每天晚上3点定时更新 20 3 * * * /usr/bin/clamdscan /home --remove #让服务器每天晚上3点20定时扫描/home目录,保存杀毒日志12 、批量脚本参考
#!/bin/bash #author linus.lin #this script is only for CentOS 7.x #check the OS platform=`uname -i` if [ $platform != "x86_64" ];then echo "this script is only for 64bit Operating System !" exit 1 fi echo "the platform is ok" cat << EOF +---------------------------------------+ | your system is CentOS 7 x86_64 | | start optimizing....... | +--------------------------------------- EOF #添加公网DNS地址 cat >> /etc/resolv.conf << EOF nameserver 114.114.114.114 EOF #Yum源更换为国内阿里源 yum install wget -y mv -f /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 #添加阿里的epel源 #add the epel wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo #yum重新建立缓存 yum clean all yum makecache #同步时间 yum -y install ntp /usr/sbin/ntpdate ntp1.aliyun.com echo "* 4 * * * /usr/sbin/ntpdate ntp1.aliyun.com > /dev/null 2>&1" >> /var/spool/cron/root systemctl restart crond.service #设置主机名 hostnamectl set-hostname linus #设置字符集 #设置最大打开文件描述符数 echo "ulimit -SHn 102400" >> /etc/rc.local cat >> /etc/security/limits.conf << EOF * soft nofile 655350 * hard nofile 655350 EOF #禁用selinux sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config setenforce 0 #set ssh sed -i 's/^GSSAPIAuthentication yes$/GSSAPIAuthentication no/' /etc/ssh/sshd_config sed -i 's/#UseDNS yes/UseDNS no/' /etc/ssh/sshd_config sed -i "s/#MaxAuthTries 6/MaxAuthTries 5/" /etc/ssh/sshd_config sed -i "s/#ClientAliveInterval 0/ClientAliveInterval 900/" /etc/ssh/sshd_config systemctl restart sshd.service #内核参数优化 cat >> /etc/sysctl.conf << EOF #CTCDN系统优化参数 #关闭ipv6 net.ipv6.conf.all.disable_ipv6 = 1 net.ipv6.conf.default.disable_ipv6 = 1 #决定检查过期多久邻居条目 net.ipv4.neigh.default.gc_stale_time=120 #使用arp_announce / arp_ignore解决ARP映射问题 net.ipv4.conf.default.arp_announce = 2 net.ipv4.conf.all.arp_announce=2 net.ipv4.conf.lo.arp_announce=2 # 避免放大攻击 net.ipv4.icmp_echo_ignore_broadcasts = 1 # 开启恶意icmp错误消息保护 net.ipv4.icmp_ignore_bogus_error_responses = 1 #开启路由转发 net.ipv4.ip_forward = 1 net.ipv4.conf.all.send_redirects = 1 net.ipv4.conf.default.send_redirects = 1 #开启反向路径过滤 net.ipv4.conf.all.rp_filter = 1 net.ipv4.conf.default.rp_filter = 1 #处理无源路由的包 net.ipv4.conf.all.accept_source_route = 0 net.ipv4.conf.default.accept_source_route = 0 #关闭sysrq功能 kernel.sysrq = 0 #core文件名中添加pid作为扩展名 kernel.core_uses_pid = 1 # 开启SYN洪水攻击保护 net.ipv4.tcp_syncookies = 1 #修改消息队列长度 kernel.msgmnb = 65536 kernel.msgmax = 65536 #设置最大内存共享段大小bytes kernel.shmmax = 68719476736 kernel.shmall = 4294967296 #timewait的数量,默认180000 net.ipv4.tcp_max_tw_buckets = 6000 net.ipv4.tcp_sack = 1 net.ipv4.tcp_window_scaling = 1 net.ipv4.tcp_rmem = 4096 87380 4194304 net.ipv4.tcp_wmem = 4096 16384 4194304 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 #限制仅仅是为了防止简单的DoS 攻击 net.ipv4.tcp_max_orphans = 3276800 #未收到客户端确认信息的连接请求的最大值 net.ipv4.tcp_max_syn_backlog = 262144 net.ipv4.tcp_timestamps = 0 #内核放弃建立连接之前发送SYNACK 包的数量 net.ipv4.tcp_synack_retries = 1 #内核放弃建立连接之前发送SYN 包的数量 net.ipv4.tcp_syn_retries = 1 #启用timewait 快速回收 #net.ipv4.tcp_tw_recycle = 0 #开启重用。允许将TIME-WAIT sockets 重新用于新的TCP 连接 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_mem = 94500000 915000000 927000000 net.ipv4.tcp_fin_timeout = 1 #当keepalive 起用的时候,TCP 发送keepalive 消息的频度。缺省是2 小时 net.ipv4.tcp_keepalive_time = 1800 net.ipv4.tcp_keepalive_probes = 3 net.ipv4.tcp_keepalive_intvl = 15 #允许系统打开的端口范围 net.ipv4.ip_local_port_range = 1024 65000 #修改防火墙表大小,默认65536 net.netfilter.nf_conntrack_max=655350 net.netfilter.nf_conntrack_tcp_timeout_established=1200 # 确保无人能修改路由表 net.ipv4.conf.all.accept_redirects = 0 net.ipv4.conf.default.accept_redirects = 0 net.ipv4.conf.all.secure_redirects = 0 net.ipv4.conf.default.secure_redirects = 0 EOF /sbin/sysctl -p #vim定义退格键可删除最后一个字符类型 sed -i 's/^HISTSIZE=1000/HISTSIZE=50000/g' /etc/profile yum -y install vim echo 'alias vi=vim' >> /etc/profile echo 'stty erase ^H' >> /etc/profile echo "export TMOUT=900" >> /etc/profile echo 'curl whatismyip.akamai.com&&echo' >> /etc/profile #设置vi编辑器 cat >> /root/.vimrc << EOF set tabstop=4 set shiftwidth=4 set expandtab syntax on set number EOF grep -q "* - nofile" /etc/security/limits.conf || cat >> /etc/security/limits.conf << EOF ######################################## * - nofile 1048576 * - nproc 65536 * - stack 1024 EOF # grep -q "ulimit -n" /etc/profile || cat >> /etc/profile << EOF ######################################## ulimit -n 1048576 ulimit -u 65536 ulimit -s 1024 ######################################## alias grep='grep --color=auto' export HISTTIMEFORMAT="[%Y-%m-%d %H:%M:%S] " EOF #update soft yum -y update cat << EOF +-------------------------------------------------+ | 优 化 已 完 成 | | 5s 后 重启 这台服务器 ! | +-------------------------------------------------+ EOF # sleep 5 echo -e "n 33[31m请重启机器 使内核修改生效!!! 33[0mn" ##重启加载内核修改 rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm yum --enablerepo=elrepo-kernel install kernel-ml -y&& sed -i s/saved/0/g /etc/default/grub&& grub2-mkconfig -o /boot/grub2/grub.cfg && reboot #重启生效!



