Logout

通用

当前路径 pwd
修改密码passwd timedatectl status查看时区 timedatectl set-timezone Asia/Shanghai 修改时区为上海

进程相关

ps -aux | grep进程名
au(x) 输出格式 :USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND ps -ef | grep aria top shift+p cpu使用率排序 shift+m 内存使用率排序, 键盘b加亮效果;y运行态进程;x排序列加亮 通过shift + >或shift + <可以向右或左改变排序列 ps auxw|head -1;ps auxw|sort -rn -k4|head -50 列出消耗内存最多的前50个进程

网络相关

netstat -alnp|grep 6800端口占用 netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n 已建立连接的ip连接进程数

文件控制

llls -l罗列出当前文件或目录的详细信息 ll /proc/进程号 pwdx进程号 显示进程工作路径、用户等信息 ls -lh -h human-readable 人性化阅读,文件夹不显示总用量 df -h 挂载的磁盘空间 du --max-depth=1 -h 查看当前每个文件夹大小 du --max-depth=1 -h |sort -n -r |head -n 10目录下占用最大的10个文件 yum clean all清理yum缓存 * 查找文件 find / -name error.log 在/路径下查找名字为error的文件 find / -name "error.log*" 通配符模式 * 文件名乱码 centos文件名乱码导致不能直接删除文件 1. 换到要删除的目录,查看文件的inode号,ls -li 2. 将文件用find找出来操作

rm -rf `find . -inum 3207` 
mv `find . -inum 655374` 123
find . -inum 655376 |xargs -i mv {} 12345 
find . -inum 655376 |xargs -i mv {} "12345"
#更有用,如果重新命名的文件带有#等字符,使用""

ssh

修改默认ssh端口!

/etc/ssh/sshd_config port 22

service

注册新服务 systemd restart sshd cp -f v2-ui.service /etc/systemd/system/ systemctl daemon-reload systemctl enable v2-ui systemctl start v2-ui


系统服务目录:/usr/lib/systemd/system/ 用户服务目录:/usr/lib/systemd/user/

systemctl

Systemctl接受服务(.service),挂载点(.mount),套接口(.socket)和设备(.device)作为单元 systemctl is-active httpd.service 检查服务是否运行 systemctl enable httpd.service 开机自启 systemctl list-unit-files --type=service | grep syncthing 检查指定服务的开机自启状态 systemctl list-unit-files --type=service 列出所有服务 systemctl的start,restart,stop和reload 同service 控制服务的CPU利用率(分配额)

日志记录

修改log位置

php-fpm: etc/opt/remi/php72/ php-fpm.conf: error_log = /zym/z-log/php-fpm/error.log php-fpm.-D/www.conf: access.log = /zym/z-log/php-fpm/$pool.access.log (超过10秒执行的php被记录) slowlog = /zym/z-log/php-fpm/www-slow.log request_slowlog_timeout = 10
进程日志 php_flag[display_errors] = on php_admin_value[error_log] =/zym/z-log/php-fpm/www-error.log /zym/z-log/php-fpm/www-error.log php_admin_flag[log_errors] = on nginx: etc/nginx/nginx.conf access_log /zym/z-log/nginx/access.log; error_log /zym/z-log/nginx/error.log notice;

系统日志工具:

journalctl journalctl -k 查看内核日志 journalctl --since="2018-09-21 10:21:00" --until="2018-09-21 10:22:00" journalctl -u kubelet.service journalctl --disk-usage 查看日志磁盘用量 /etc/systemd/journald.conf 参数配置 SystemMaxUse=16M 日志工具rsyslog 记录iptables iptables -A INPUT -j LOG --log-prefix "one: "
此处的"one: "自行定义,有意义即可。可以理解为符合此规则的数据包防火墙都给打上一个标示"one: " 在给防火墙添加规则的过程中给特殊的数据包打标示,下面的配置中将用到。iptables.log中将记录防火有标示的数据包的处理细节。 vim /etc/rsyslog.d/iptables.conf (此文件默认是不从在的) 添加如下内容

:msg, contains, "one: " -/zym/z-log/iptables.log
& ~
iptables -A INPUT -d 46.17.41.6 -p tcp -m multiport --dport 50527:50530 -j LOG --log-prefix "iptables"

iptables

注意ip6tables即IPv6默认常开

service iptables status 
yum install -y iptables
yum update iptables  
yum install iptables-services
systemctl stop firewalld                                                systemctl mask firewalld
iptables-save > /etc/sysconfig/iptables.backup
iptables-restore < /etc/sysconfig/iptables.backup
service iptables save   //save以后restart
systemctl enable/start/status iptables.service

清空所有默认规则、自定义规则、计数器归0 iptables -F
iptables -X iptables -Z iptables -A INPUT -s 192.168.122.0/23 -p tcp -m multiport --dport 21:23,2227:2230 -j ACCEPT -A 追加规则在规则后,-I增加规则在规则前 iptables -I INPUT -s 124.78.23.155 -j DROP 封禁IP iptables -L INPUT --line-numbers
列出INPUT 链所有的规则 iptables -D INPUT 4 删除指定的第4行规则:

防止端口扫描

-A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -j REJECT --reject-with icmp-port-unreachable
-A INPUT -p tcp -m tcp --tcp-flags SYN,RST SYN,RST -j REJECT --reject-with icmp-port-unreachable
-A INPUT -p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -j REJECT --reject-with icmp-port-unreachable

Fail2ban

fail2ban防止暴力破解 http://www.361way.com/fail2ban-nginx/1825.html 参考-匹配RUL规则。 https://www.cnblogs.com/wangxiaoqiangs/p/5630325.html 参考-安装教程 https://blog.csdn.net/shuchengzhang/article/details/50931123 参考-相关配置文件 https://blog.csdn.net/w958660278/article/details/80592928 参考-fail2ban从黑名单中移除IP http://www.zsythink.net/archives/1199 参考-iptables配置规则 http://www.fail2ban.org/wiki/index.php/Main_Page 参考-官网文档

Fail2ban 能够监控系统日志,匹配日志中的错误信息(使用正则表达式),执行相应的屏蔽动作(支持多种,一般为调用 iptables ),是一款很实用、强大的软件。 如:攻击者不断尝试穷举 SSH 、SMTP 、FTP 密码等,只要达到预设值,fail2ban 就会调用防火墙屏蔽此 IP ,并且可以发送邮件通知系统管理员。

功能、特性: 1、支持大量服务:sshd 、apache 、qmail 等 2、支持多作动作:iptables 、tcp-wrapper 、shorewall 、mail notifications 等 3、logpath 选项中支持通配符 4、需要 Gamin 支持(Gamin 用于监控文件和目录是否更改) 5、如果需要邮件通知,则系统事先要确保能够正常发送邮件

# 安装epel-release
yum -y install epel-release
# 安装fail2ban
yum -y install fail2ban

/etc/fail2ban ## fail2ban 服务配置目录 /etc/fail2ban/action.d ## iptables 、mail 等动作文件目录 /etc/fail2ban/filter.d ## 条件匹配文件目录,过滤日志关键内容 /etc/fail2ban/jail.conf ## fail2ban 防护配置文件 /etc/fail2ban/fail2ban.conf ## fail2ban 配置文件,定义日志级别、日志、sock 文件位置等

官方的文档写到:在配置时,我们应该避免修改由fail2ban安装创建的文件,我们应该去编写具有.local扩展名的新文件。在.local新文件里配置的内容会覆盖jail.conf内容里相同的值。当我们的配置发生改变了我们可以使用 fail2ban-client reload ,来加载新的配置。 修改/etc/fail2ban/fail2ban.conf文件,设定日志文件等

[Definition]
loglevel = 3                              ## 定义日志级别,默认
logtarget = /zym/z-log/fail2ban/fail2ban.log         ## 定义 fail2ban 日志文件
socket = /var/run/fail2ban/fail2ban.sock  ## sock 文件存放位置,默认
pidfile = /var/run/fail2ban/fail2ban.pid  ## pid 文件存放位置,默认

添加/etc/fail2ban/jail.d/sshd.local文件

# defalut设定全局设置,会覆盖jail.conf文件中的[defalut]部分
[DEFAULT]
# 用于指定哪些地址ip可以忽略 fail2ban 防御,以空格间隔。
ignoreip = 127.0.0.1/8
# ssh客户端主机被禁止的时长(默认单位为秒)
bantime  = 3600
# 过滤的时长(秒)
findtime  = 600
# 匹配到的阈值(允许失败次数)
maxretry = 3
#覆盖jail.conf文件中的[sshd]部分
[sshd]
# 是否开启
enabled  = true
# 过滤规则
filter   = sshd
# 动作,如果我们更改了sshd服务的端口号修改port=22,
action   = iptables[name=SSH, port=ssh, protocol=tcp]
# 日志文件的路径
logpath  = /var/log/secure
# 匹配到的阈值(次数)
maxretry = 3

命令:yum安装会自动添加服务 fail2ban-client reload fail2ban-client status sshd #查看sshd封的IP情况 fail2ban-client set 服务规则 unbanip IP地址 解禁 fail2ban-client set sshd unbanip 8.8.8.8


PHP

https://segmentfault.com/a/1190000015781413 https://www.mf8.biz/rhel-centos-8-install-php/ 安装 PHP7.2 安装 EPEL 软件包: yum install epel-release 安装 remi 源: yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm 安装 yum 扩展包:yum install yum-utils 启用 remi 仓库: yum-config-manager --enable remi-php72 yum update 安装 PHP7.2 yum install php72 安装 php-fpm 和一些其他模块
yum install php72-php-fpm php72-php-gd php72-php-json php72-php-mbstring php72-php-mysqlnd php72-php-xml php72-php-xmlrpc php72-php-opcache 输入 php72 -v 查看安装结果 php72 -i查看目录 php执行文件路径: whereis php72which php72 php.ini配置文件路径:php72 -i | grep php.ini php72-php-fpm.service 服务

/etc/opt/remi/php72/php.ini /etc/opt/remi/php72/php-fpm.conf

禁用高危函数

disable_functions =chroot,chgrp,chown,ini_set,ini_alter,ini_restore,get_cfg_var popen,exec,passthru,system,shell_exec,popen,dl,symlink
disable_functions = passthru,exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,fsocket,popen

disable_functions = pcntl_alarm, pcntl_fork, pcntl_waitpid, pcntl_wait, pcntl_wifexited, pcntl_wifstopped, pcntl_wifsignaled, pcntl_wexitstatus, pcntl_wtermsig, pcntl_wstopsig, pcntl_signal, pcntl_signal_dispatch, pcntl_get_last_error, pcntl_strerror, pcntl_sigprocmask, pcntl_sigwaitinfo, pcntl_sigtimedwait, pcntl_exec, pcntl_getpriority, pcntl_setpriority, eval, popen, passthru, exec, system, shell_exec, proc_open, proc_get_status, chroot, chgrp, chown, ini_alter, ini_restore, dl, pfsockopen, openlog, syslog, readlink, symlink, popepassthru, stream_socket_server, fsocket, chdir 让php-fpm以root的权限运行:php72-php-fpm.service中在ExecStart的 --nodaemonize前面加上 --allow-to-run-as-root

PHP自带web服务

[Unit]
Description=php-web

[Service]
WorkingDirectory=/root/
ExecStart=/usr/bin/php72 -S 0.0.0.0:50525 -t /
ExecStop=eval $(ps -ef | grep php | grep -v grep | awk '{print "kill "$2}')
Restart=always
RestartSec=3

[Install]
WantedBy=multi-user.target

BBR锐速

https://ssr.tools/533 CentOS7 内核更换为: 3.10.0-229.1.2.el7.x86_64 rpm -ivh http://soft.91yun.pw/ISO/Linux/CentOS/kernel/kernel-3.10.0-229.1.2.el7.x86_64.rpm --force 查看内核是否安装成功rpm -qa | grep kernel 安装脚本 wget -N --no-check-certificate https://github.com/91yun/serverspeeder/raw/master/serverspeeder.sh && bash serverspeeder.sh 重启锐速: /serverspeeder/bin/serverSpeeder.sh restart 锐速状态: /serverspeeder/bin/serverSpeeder.sh status 卸载锐速: chattr -i /serverspeeder/etc/apx* && /serverspeeder/bin/serverSpeeder.sh uninstall –f


v2ray

https://www.v2ray.com bash <(curl -L -s https://install.direct/go.sh) /usr/bin/v2ray/v2ray:V2Ray 程序; /usr/bin/v2ray/v2ctl:V2Ray 工具; /etc/v2ray/config.json:配置文件; /usr/bin/v2ray/geoip.dat:IP 数据文件 /usr/bin/v2ray/geosite.dat:域名数据文件 /etc/systemd/system/v2ray.service: Systemd 1.编辑 /etc/v2ray/config.json 文件来配置你需要的代理方式; 2.运行 service v2ray start 来启动 V2Ray 进程; 3.之后可以使用 service v2ray start|stop|status|reload|restart|force-reload 控制 V2Ray 的运行。


torrent-cloud

https://github.com/jpillora/cloud-torrent curl https://i.jpillora.com/cloud-torrent! | bash

vi /usr/lib/systemd/system/cloud-torrent.service
[Unit]
Description=filebrowser

[Service]
WorkingDirectory=/root/
ExecStart=/usr/local/bin/cloud-torrent --host 0.0.0.0 --port 2000 --auth "[email protected]:XXXX" --config-path /root/cloud-torrent.json --title "Cloud Torrent"
ExecStop=eval $(ps -ef | grep cloud-torrent | grep -v grep | awk '{print "kill "$2}')
Restart=always
RestartSec=3

[Install]
WantedBy=multi-user.target

/root/cloud-torrent.json


tiny file maneger

https://github.com/prasathmani/tinyfilemanager 配置文件就是自身 ,需要网页服务器

PHP默认服务器

vi /usr/lib/systemd/system/php-web.service

[Unit]
Description=php-web

[Service]
WorkingDirectory=/root/
ExecStart=/usr/bin/php72 -S 0.0.0.0:5000 -t /
ExecStop=eval $(ps -ef | grep php | grep -v grep | awk '{print "kill "$2}')
Restart=always
RestartSec=3

[Install]
WantedBy=multi-user.target

syncthing

https://www.moerats.com/archives/642/ https://docs.syncthing.net/ 开启 ./syncthing 编辑/root/.config/syncthing/config.xml文件,将127.0.0.1:8384改成0.0.0.0:8384 服务器端: 开启TLS 协议监听地址tcp://0.0.0.0:22000 启动NAT、全球发现、本地发现、开启中继都关闭 客户机: 开启TLS 高级-地址列表 tcp://46.17.41.6:5000


Replace “myuser” with the actual Syncthing user after the @: systemctl enable [email protected] systemctl start [email protected]

v2-ui

https://github.com/sprov065/v2-ui/releases/download/版本/v2-ui-linux.tar.gz tar zxvf v2-ui-linux.tar.gz cd v2-ui chmod +x v2-ui systemctl start v2-ui

Nginx设置:

location /v2-ui {
    proxy_pass http://127.0.0.1:65432/v2-ui;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

面板所有数据包括账号信息等都存在 /etc/v2-ui/v2-ui.db 中

aria2

yum安装,自定义.conf,session,DHT.dat文件 https://github.com/P3TERX/aria2.conf 此处下载DHT.dat文件和tracker.sh(添加tracker脚本)

[Unit]
Description=Aria2
Requires=network.target
After=network.target

[Service]
Type=forking
#PIDFile=/run/aria2.pid
ExecStart=/usr/bin/aria2c --conf-path=/zym/test/aria2-conf/aria2.conf -D
ExecReload=/usr/bin/kill -HUP $MAINPID
RestartSec=10
Restart=on-failure

[Install]
WantedBy=multi-user.target

logrotate

https://www.cnblogs.com/kevingrace/p/6307298.html 默认配置文件路径: /etc/logrotate.conf /etc/logrotate.d/ ogrotate.conf 才主要的配置文件,logrotate.d 是一个目录,该目录里的所有文件都会被主动的读入/etc/logrotate.conf中执行。 另外,如果 /etc/logrotate.d/ 里面的文件中没有设定一些细节,则会以/etc/logrotate.conf这个文件的设定来作为默认值。 如果是yum方式安装的nginx,系统默认会自动通过logrotate这个日志管理软件,按天进行分割。 Logrotate是基于CRON来运行的,其脚本是/etc/cron.daily/logrotate,日志轮转是系统自动完成的。 实际运行时,Logrotate会调用配置文件/etc/logrotate.conf。 可以在/etc/logrotate.d目录里放置自定义好的配置文件,用来覆盖Logrotate的缺省值。

cat /etc/cron.daily/logrotate
#!/bin/sh

/usr/sbin/logrotate /etc/logrotate.conf >/dev/null 2>&1
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0

如果等不及cron自动执行日志轮转,想手动强制切割日志,需要加-f参数;不过正式执行前最好通过Debug选项来验证一下(-d参数),这对调试也很重要 # /usr/sbin/logrotate -f /etc/logrotate.d/nginx # /usr/sbin/logrotate -d -f /etc/logrotate.d/nginx

cat /etc/logrotate.d/nginx 
/var/log/nginx/*log {    #为nginx日志的存储目录,可以根据实际情况进行修改
    daily                #日志文件将按天轮循
    rotate 10            #一次存储10个日志文件。对于第11个日志文件,时间最久的那个日志文件将被删除
    missingok            #在日志轮循期间,任何错误将被忽略,例如“文件无法找到”之类的错误
    notifempty           #如果是空文件的话,不进行转储
    compress             #在轮循任务完成后,已轮循的归档将使用gzip进行压缩
    sharedscripts        #运行postrotate脚本,作用是在所有日志都轮转后统一执行一次脚本。如果没有配置这个,那么每个日志轮转后都会执行一次脚本
    postrotate           #在所有其它指令完成后,postrotate和endscript里面指定的命令将被执行。在这种情况下,rsyslogd进程将立即再次读取其配置并继续运行。注意:这两个关键字必须单独成行    
        /bin/kill -USR1 $(cat /var/run/nginx.pid 2>/dev/null) 2>/dev/null || :
    endscript
}

daily 指定转储周期为每天 weekly 指定转储周期为每周 monthly 指定转储周期为每月 rotate count 指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份 dateext 使用当期日期作为命名格式 dateformat .%s 配合dateext使用,紧跟在下一行出现,定义文件切割后的文件名,必须配合dateext使用,只支持 %Y %m %d %s 这四个参数 size(或minsize) log-size 当日志文件到达指定的大小时才转储,log-size能指定bytes(缺省)及KB (sizek)或MB(sizem). 当日志文件 >= log-size 的时候就转储。 以下为合法格式:(其他格式的单位大小写没有试过) size = 5 或 size 5 (>= 5 个字节就转储) size = 100k 或 size 100k size = 100M 或 size 100M size 仅以大小作为分割依据 minsize 超过minsize才按日期分割,否则忽略

compress 通过gzip 压缩转储以后的日志 nocompress 不做gzip压缩处理 delaycompress 和compress 一起使用时,转储的日志文件到下一次转储时才压缩 nodelaycompress 覆盖 delaycompress 选项,转储同时压缩。

copytruncate 用于还在打开中的日志文件,把当前日志备份并截断;是先拷贝再清空的方式,拷贝和清空之间有一个时间差,可能会丢失部分日志数据。 nocopytruncate 备份日志文件不过不截断 create mode owner group 轮转时指定创建新文件的属性,如create 0777 nobody nobody nocreate 不建立新的日志文件

missingok 如果日志丢失,不报错继续滚动下一个日志 ifempty 即使日志文件为空文件也做轮转,这个是logrotate的缺省选项。 notifempty 当日志文件为空时,不进行轮转 olddir directory 转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统 noolddir 转储后的日志文件和当前日志文件放在同一个目录下

sharedscripts 运行postrotate脚本,作用是在所有日志都轮转后统一执行一次脚本。如果没有配置这个,那么每个日志轮转后都会执行一次脚本 prerotate 在logrotate转储之前需要执行的指令,例如修改文件的属性等动作;必须独立成行 postrotate 在logrotate转储之后需要执行的指令,例如重新启动 (kill -HUP) 某个服务!必须独立成行