分类 Linux 下的文章

CentOS 7.5 firewalld 常规使用(包括屏蔽IP、状态查询、端口开放/关闭等)

系统从6升级到7之后,默认的防火墙从iptables更换到了firewalld,使用起来差别很大,现列出常用的功能,便于查阅。

如果是新安装的系统,也没有开启过firewalld,那么建议使用firewall-offline-cmd命令开启ssh端口。因为firewall-cmd在firewalld未启动时不能进行设置,一旦开启(假如还未开启ssh端口),那么你将被断开连接。
#查看firewall状态;
firewall-cmd --state

#安装
yum install firewalld

#启动
systemctl start firewalld 

#设置开机启动
systemctl enable firewalld

#关闭
systemctl stop firewalld

#取消开机启动
systemctl disable firewalld

#禁止IP(123.44.55.66)访问机器,记得加 permanent 参数,否则系统重启后无效
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address="123.44.55.66" drop'

#禁止一个IP段,比如禁止116.255.*.*
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address="116.255.0.0/16" drop'

#禁止一个IP段,比如禁止116.255.196.*
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address="116.255.196.0/24" drop'

#禁止机器IP(123.44.55.66)从防火墙中删除
firewall-cmd --permanent --remove-rich-rule='rule family=ipv4 source address="123.44.55.66" drop'

#允许http服务(对应服务策略目录:/usr/lib/firewalld/services/)
firewall-cmd --permanent --add-service=http

#关闭http服务(对应服务策略目录:/usr/lib/firewalld/services/)
firewall-cmd --permanent --remove-service=http

#允许端口:3389
firewall-cmd --permanent --add-port=3389/tcp

#允许端口:1-3389
firewall-cmd --permanent --add-port=1-3389/tcp

#关闭放行中端口:3389
firewall-cmd --permanent --remove-port=3389/tcp

#查看防火墙规则(只显示/etc/firewalld/zones/public.xml中防火墙策略,在配置策略前,我一般喜欢先CP,以后方便直接还原)
firewall-cmd --list-all 

#查看所有的防火墙策略(即显示/etc/firewalld/zones/下的所有策略)
firewall-cmd --list-all-zones 

#查看防火墙所有开放的端口
firewall-cmd --zone=public --list-ports

#重新加载配置文件,更改配置后一定要重新加载配置文件
firewall-cmd --reload

Centos7.5 从Python2.7.5升级到Python2.7.16

Centos7 默认安装的python版本是2.7.5,由于某些平台提示建议更新版本,防止跨脚本攻击,所以现对其进行升级,更新至2.7.16版本(截至目前2019-8-12的最新版本)。

查看当前python版本

[root@csensix ~]# python -V

运行结果:Python 2.7.5

下载并安装最新版本 Python 2.7.16

下载

.tar.xz.tgz 压缩版本二选一,这里我下载的是体积较小的 .tar.xz 版本。

# tar.xz 版本
[root@csensix ~]# wget https://www.python.org/ftp/python/2.7.16/Python-2.7.16.tar.xz

# tgz 版本
[root@csensix ~]# wget https://www.python.org/ftp/python/2.7.16/Python-2.7.16.tgz

解压

[root@csensix ~]# tar -xf Python-2.7.16.tar.xz

# 或者分两步
[root@csensix ~]# xz -d Python-2.7.16.tar.xz
[root@csensix ~]# tar xvf Python-2.7.16.tar

编译安装

[root@csensix ~]# cd Python-2.7.16
[root@csensix ~]# ./configure --prefix=/usr/local
[root@csensix ~]# make
[root@csensix ~]# make altinstall

为避免覆盖系统自带python,故没有选择常规的make install,而是使用了 make altinstall

修改默认python版本

安装完之后执行 python -V,此时显示的还是老的 2.7.5 版本,因为 /usr/bin/python 指向的还是原来的版本,所以需要做相应的更改。

首先,备份老版本

[root@csensix ~]# mv /usr/bin/python /usr/bin/python2.7.5

链接新版本

[root@csensix ~]# ln -s /usr/local/bin/python2.7 /usr/bin/python

至此,新版本安装基本完成。现在,python -V 显示的结果是 Python 2.7.16,如果希望访问老版本,那么执行 python2.7.5 即可,如下:

[root@csensix ~]# python2.7.5
Python 2.7.5 (default, Apr 11 2018, 07:36:10) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

- 阅读剩余部分 -

apache按天生成访问日志,并指定存放位置

一、日志格式及按天分割

# 错误日志
ErrorLog "logs/error_log"

# 默认可用的访问日志格式,combined common referer ajent 是别名,CustomLog 时通过指定别名来使用相应的格式
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

# 默认访问日志
CustomLog "logs/access_log" common

# 按天生成日志
CustomLog "| /usr/sbin/rotatelogs /etc/httpd/logs/access_log-%Y%m%d 86400 480" combined

二 关闭日志
错误日志没有开启关闭设置,只能通过设定不同级别来减少错误日志的记录,达到减慢日志生成速度。
其中,LogLevel用于调整记于错误日志中的信息的详细程度。(参阅ErrorLog指令)。可以选择下列级别,依照重要性降序排列:
Level Description Example
emerg 紧急 – 系统无法使用。 “Child cannot open lock file. Exiting”
alert 必须立即采取措施。 “getpwuid: couldn’t determine user name from uid”
crit 致命情况。 “socket: Failed to get a socket, exiting child”
error 错误情况。 “remature end of script headers”
warn 警告情况。 “child process 1234 did not exit, sending another SIGHUP”
notice 一般重要情况。 “httpd: caught SIGBUS, attempting to dump core in …”
info 普通信息。 “Server seems busy, (you may need to increase StartServers, or Min/MaxSpareServers)…”
debug 出错级别信息 “Opening config file …”

默认级别是warn,那么warn级别以上的日志都会记录,会产生大量“文件不存在”的erro级别的错误日志。建议使用 crit 级别的设置,这样只记录致命级别以上的日志,有效减少日志数量。

访问日志关闭只需要注释 CustomLog 设置即可。

Linux删除文件后df -h显示目录大小无变化

情形:删除文件后df -h 文件目录大小未改变,du 查看目录大小却改变了

这是因为,df 会计算各自 meta 数据,当该目录的文件被删除了,却仍有进程hold住这个文件句柄,此时这个文件的block并未被释放,df仍会统计到该文件的block,du却不会。

遇到这种情况,就需要查hold进程,kill 掉进程就好了。

lsof | grep delete

#  找到 对应文件的hold进程 id
kill -9 $id

nginx 配置图片文件等过期时间,禁止日志记录

查看nginx日志文件,发现有大量图片请求的日志,其实这种日志我们并不需要,所以考虑禁止此类文件的日志文件生成,方法如下:

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
        error_log /dev/null;
        access_log off;
    }
    
    location ~ .*\.(js|css)?$
    {
        expires      12h;
        error_log /dev/null;
        access_log off; 
    }