[笔记] Linux的CLI笔录

Linux - CLI笔录


Linux的CLI笔录

Linux CentOS及Redhat的firewall-cmd使用

[root@localhost ~]# ls /etc/firewalld/zones/				# 查看firewalld的zone区域配置文件
docker.xml  public.xml  public.xml.old
[root@localhost ~]# cat /etc/firewalld/zones/public.xml		# firewalld的public区域配置文件
<?xml version="1.0" encoding="utf-8"?>
<zone>
  <short>Public</short>
  <description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
  <port protocol="udp" port="587"/>
  ...


firewall-cmd --help		# 帮助信息
### firewall-cmd的 查看|查询
firewall-cmd --list-all-zones	# 查看所有zones区域,常用并激活的是public区域和docker区域
firewall-cmd --list-all			# 查看当前激活区域的所有配置信息
firewall-cmd --zone=public --list-port		# 指定public区域,查看port端口信息
firewall-cmd --zone=public --list-services	# 指定public区域,查看services服务信息
firewall-cmd --zone=public --list-all		# 指定public区域,查看所有配置信息
firewall-cmd --state			# 查看当前防火墙状态
firewall-cmd --reload			# 重新加载当前配置
systemctl status firewalld		# 查看防火墙服务状态

### firewall-cmd的 增删改查
# -permanent: 写入配置文件, 永久设置.

firewall-cmd --zone=public --add-port=80/tcp --permanent		# 放通public区域的tcp80端口
firewall-cmd --zone=public --add-service=nfs --permanent		# 放通public区域的nfs服务端口

# 以完整规则的定义,放通public区域的源X.X.X.0/24访问本机的tcp2049端口
firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="X.X.X.0/24" port port="2049" protocol="tcp" accept'

firewall-cmd --zone=public --add-rule=xxx			# 根据rule规则的定义,放通public区域的指定rule
firewall-cmd --zone=public --add-chain=xxx			# 根据chain规则的定义,放通public区域的指定chain
firewall-cmd --zone=public --remove-xxx=yyy			# 删除某条规则,以remove为前缀
firewall-cmd --zone=public --remove-rich-rule="xxx"	# 删除某条rich-rule规则,以remove为前缀

Linux CentOS及Redhat的iptables使用

iptables --help		# 帮助信息
# -n: 显示端口号
# -v: 显示详细信息
# -L: 显示规则列表

### iptables的 查看|查询
iptables -nvL				# 默认显示filter表的所有链规则
iptables -t filter -nvL INPUT	# 默认显示filter表的INPUT链规则
iptables -t nat -nvL		# 显示nat表的所有链规则
systemctl status firewalld	# 查看防火墙服务状态

### iptables的 增删改查
#
# 往INPUT链第一行插入规则,放通源1.1.1.0/24访问1.1.2.1的tcp22端口
iptables -I  INPUT 1 -p tcp --dport 22 -s 1.1.1.0/24 -d 1.1.2.1 -j ACCEPT

# 往INPUT链第一行插入规则,放通源1.1.1.0/24访问1.1.2.1的多个tcp端口(20-22,111,389,636,2000-2100)
iptables -I INPUT 1 -p tcp -m multiport --dport 20:22,111,389,636,2000:2100 -s 1.1.1.0/24 -d 1.1.2.1 -j ACCEPT

# 往OUTPUT链添加规则到最后行,拒绝源1.1.2.1的udp111端口访问1.1.1.0/24
iptables -A OUTPUT -p udp --sport 111 -s 1.1.2.1 -d 1.1.1.0/24 -j DROP

# 往FORWARD链添加规则到最后行,允许状态为establish和related的数据包
iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT

# 删除INPUT链的第一条规则
iptables -D INPUT 1

# 修改INPUT链的第一条规则,修改动作为DROP
iptables -R INPUT 1 -j DROP

# 清除INPUT链的所有规则
iptables -F INPUT

# 修改INPUT、OUTPUT、FORWARD链的默认动作为DROP
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP

# iptables保存规则到开机自启动
service iptables save

Linux的tcpdump使用

# -i: 接口
# -s0: 不截断包, 抓完整的数据包
# -X: 完整显示协议头和包内容
# -n: 域名转ip
# -nn: 域名转ip, 应用名称转端口号
# -v: 输出详细报文信息, 明细级别最多 vvv
# -e: 打印包头部信息, 包括源目mac和协议
# -w: 写到文件
# host: 主机
# net: 网络
# port: 端口
# src: 源
# dst: 目

tcpdump -i any host 1.1.1.1 port 80 -X -s0 -nnvvve	# 抓取任意接口, 主机1.1.1.1的80端口的数据包

Linux的vim使用

vim ~/.vimrc
set cuc		# 设置vim高亮光标
set number	# 设置vim显示行数

Linux创建systemctl系统服务

### 参考链接: https://blog.csdn.net/qq_40903527/article/details/127678795

systemctl list-units --type=service		# 查看 service 类型的服务列表

# 在 /etc/systemd/system 目录创建存放自定义服务的目录
[root@nginx ~]# mkdir -p /etc/systemd/system/custom.target.wants/

# 创建自定义服务
[root@nginx ~]# touch /etc/systemd/system/custom.target.wants/custom.service

# 链接自定义服务到系统服务的执行目录 /usr/lib/systemd/system
[root@nginx ~]# ln -s /etc/systemd/system/custom.target.wants/custom.service /usr/lib/systemd/system/custom.service

# 查看自定义服务文件的权限列表
[root@nginx ~]# ll /usr/lib/systemd/system/custom.service
lrwxrwxrwx. 1 root root 74 Sep 19 09:21 /usr/lib/systemd/system/custom.service -> /etc/systemd/system/custom.target.wants/custom.service

[root@nginx ~]# ll /etc/systemd/system/custom.target.wants/custom.service 
-rw-r--r--. 1 root root 306 Jul 24 11:26 /etc/systemd/system/custom.target.wants/custom.service

# 编辑自定义服务内容
[root@nginx ~]# vim /etc/systemd/system/custom.target.wants/custom.service
[Unit]
Description=desc
Requires=network.service

[Service]
ExecStart=/usr/bin/python3 /tmp/custom.py
Type=simple
KillMode=mixed

[Install]
WantedBy=multi-user.target
:x

# 查看新创建的系统服务
systemctl status custom.service
systemctl is-enabled custom.service

# 开启新创建的系统服务
systemctl enable custom.service

Linux扫描全部磁盘

ls /sys/class/scsi_device		# 检查 scsi 设备的名称

# 可以使用下面脚本一键扫描全部磁盘
for i in `ls /sys/class/scsi_device`;do echo 1 > /sys/class/scsi_device/$i/device/rescan;done

OpenWrt的CLI

vim /etc/rc.d/K15addroute
#!/bin/sh /etc/rc.common

START=99
STOP=15

start() {
        route add -net 1.1.0.0/16 gw 1.1.1.2 metric 10
}
:x

vim /etc/rc.d/S99addroute 
#!/bin/sh /etc/rc.common

START=99
STOP=15

start() {
        route add -net 1.1.0.0/16 gw 1.1.1.2 metric 10
}
:x

cd /etc/rc.d/
ll *addroute
lrwxrwxrwx    1 root     root            18 Aug 17  2022 K15addroute -> ../init.d/addroute*
lrwxrwxrwx    1 root     root            18 Aug 17  2022 S99addroute -> ../init.d/addroute*



参考来源

  1. (建议收藏)systemd(systemctl命令)运行服务的配置文件详解
  2. Linux扩容虚拟磁盘后不显示新增磁盘或扩容后的磁盘大小
  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

歪果仨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值