Skip to content

系统命令

shell
# 查看内核版本
uname -r
#把yum包更新到最新
yum update

ps:进程状态

# 显示所有正在运行的进程
ps -e

# 查看更多的进程的信息
ps -ef

# 以详细格式显示所有正在运行的进程
ps -aux 

# 查看PID是1333的详细信息
ps -p 1333 -f

# 查询java进程
ps -ef | grep java

cd,pwd,mkdir,touch

cd:切换目录

# cd命令
cd 目标目录路径

# 切换指定目录
cd /opt/games

# 切换上一级目录
cd ..

# 切换到主目录
cd
cd ~

# 切换到上一次的目录
cd -

pwd:打印工作目录

# 打印工作目录
pwd

# 打印实际路径
pwd -P

# 打印逻辑路径
pwd -L

mkdir:创建目录

# 创建多个目录
mkdir dir1 dir2 dir3

# 递归创建
mkdir -p /tmp/java/project

# 创建目录并增加权限
mkdir -m 700 importance

touch:

# 创建多个文件
touch f1.txt f2.txt f3.txt

# 不存在创建
touch -c f1.txt

# 查看文件信息
stat f1.txt

# 修改文件访问时间
touch -a f1.txt

# 修改文件的修改时间
touch -m f1.txt

mv,cp,tar,rm

mv:移动文件或目录、文件或目录重命名

# mv命令
mv 源文件或目录 目标文件或目标位置

# 移动文件到指定目录
mv car.ini /opt

# 重命名指定文件
mv car.ini boat.ini

# 重名名文件
mv config conf

cp:复制文件和目录

# cp命令
cp 源文件或目录 目标文件或目标位置

# 复制文件并重命名
cp car.ini /tmp/car2.ini

# 复制目录到指定位置
cp -r test /tmp

tar:处理文件和目录的归档和压缩

# 命令
tar [选项参数] 归档文件名 文件/目录名

# 创建gzip文件
tar -czf crack.tar.gz crack

# 解压gzip文件
tar -xzf redis.tar.gz

# 解压gzip文件到指定目录
tar -xzf redis.tar.gz -C /usr/local

# 查看归档文件中的内容
tar -tf redis.tar.gz

rm:删除指定的文件和目录

# rm命令
rm [选项参数] 文件或目录名

# 删除文件
rm file.txt

# 删除目录
rm -r directory

# 前置删除目录
rm -rf directory

find,cat,tail,grep,zgrep

命令功能
wc -l统计行数
uniq
排序
sort

find:用于在指定的目录下搜索文件和目录

# find命令
find 目录路径 [选项参数] [搜索条件]

# 在/opt目录下所有以 .txt 扩展名结尾的文件
find /opt -name "*.txt"

# 当前目录及其子目录中查找所有以.txt结尾的文本文件
find . -name "*.txt"

# /opt目录下的所有类型为目录的文件(d-目录,f-文件,l-链接)
find /opt -type d

# /boot目录及其子目录下所有大小大于1MB的文件(+表示大于,-表示小于)
find /boot -size +1M

# /home目录及其子目录下所有属于god23bin这个用户的文件
find /home -user god23bin

# /opt目录及其子目录下最近7天内发生过修改的文件(-7代表7天内,+7代表7天之前)
find /opt -mtime -7

cat:连接一个或多个文件,并将结果输出到终端或其他文件

# cat命令
cat [选项参数] 文件名
cat 文件名1, 文件名2

# 显示行号
cat -n show.txt

# 空行不显示行号
cat -b show.txt

# 多个空行合并成一行
cat -s show.txt

# 清空指定文件内容
cat /dev/null > show.txt

tail:

# tail命令
tail [选项参数] 文件名

# 指定显示文件尾部20行的内容
tail -n 20 readme.txt

# 持续跟踪文件尾部最新内容
tail -f sys-user.log

# 跟踪尾部最新的 1000 行的内容
tail -1000f sys-user.log

grep:索文件的内容

text
# grep命令
grep [选项参数] 文本模式 文件名

# 查找包含VI的行
grep VI readme.txt

# 查找包含vi的行,忽略大小写
grep -i "vi" readme.txt

# 查找http ,显示所在的行号:
grep -n http readme.txt

# 在show.txt中进行反向匹配,打印不包含文本模式“我”的行:
grep -v 我 show.txt

# 当前目录及其子目录中递归搜索包含关键字error的行
grep -r "error" .

# 日志中搜索
tail -2000f project-test.log | grep -i error

zgrep:用于在压缩 (.gz) 文件中搜索

# zgrep命令
zgrep [OPTIONS] PATTERN FILE.gz

# 使用
zgrep "error" logfile.gz  

# 多个压缩文件中搜索
zgrep "error" *.gz

# 大小写不敏感
zgrep -i "error" logfile.gz

# 显示行号
zgrep -n "error" logfile.gz

# 统计匹配到的行数
zgrep -c "error" logfile.gz

# 仅显示匹配的文件名
zgrep -l "error" *.gz

# 显示除包含“error”的行之外的所有行
zgrep -v "error" logfile.gz

# 递归搜索
zgrep -r "error" /var/log/

# 使用正则表达式
zgrep -E "error|warning|failed" logfile.gz
text
# 下载rpm包而不安装它
yum install yum-plugin-downloadonly
yum install --downloadonly --downloaddir=<directory> iptables

# 找出所有依赖项
yum deplist iptables

cp -r 

# 系统信息
lsb_release -a

# CPU信息
lscpu

# centos版本
cat /etc/centos-release
cat /etc/redhat-release

# 防火墙
systemctl status firewalld
systemctl start firewalld
systemctl stop firewalld

ps

more,less

less

功能快捷键
向下翻页空格键Page Down
向上翻页Page Upb(向后翻页)
逐行滚动/ 方向键 或 j/k
跳转到首行gHome
跳转到末行GEnd
搜索文本/(向下搜索)、?(向上搜索)

shell脚本

awk

$0:当前处理的完整行(包括行末的换行符 \n),当使用 print 不带参数时,默认输出 $0

比较运算符含义示例
~匹配正则$0 ~ /error/若行含 "error" 返回
!~不配正则$1 !~ /^A/若首字段不以 A 开头返回

网络测试

网络测试教程:https://cloud.tencent.com/developer/article/1468122 MTR工具使用说明:https://cloud.tencent.com/developer/article/1035350

修改服务器时间

text
# 时间状态
timedatectl status

# 尼日利亚时区
timedatectl set-timezone "Africa/Lagos"

# 上海时区
timedatectl set-timezone "Asia/Shanghai"

配置伪IP

text
ifconfig ens160:0 10.170.0.188 netmask 255.255.255.192 up

磁盘空间

text
du -h --max-depth=1 | sort -hr

iptables

参考地址:https://www.cloudiplc.com/index.php?rp=%2Fknowledgebase%2F9%2FiptablesIP.html& language=english 安装教程:centos下Iptables的安装(离线)_离线安装iptables-CSDN博客 IP转发:通过iptables实现IP端口数据包转发服务配置 - Knowledgebase - CloudIPLC centos下Iptables的安装(离线)_离线安装iptables-CSDN博客

text
# 可以删掉所有的规则
iptables -F

# 保存当前配置
iptables -save

# iptables状态
sysctl net.ipv4.ip_forward

# 查看目前正在NAT的规则
iptables -t nat -nL

# CentOS 7 开启方式:
echo "net.ipv4.ip_forward = 1" >> /usr/lib/sysctl.d/cloudiplc.conf
sysctl -p /usr/lib/sysctl.d/cloudiplc.conf

# CentOS 7 关闭方式:
echo "net.ipv4.ip_forward = 0" >> /usr/lib/sysctl.d/cloudiplc.conf
sysctl -p /usr/lib/sysctl.d/cloudiplc.conf

iptables -t nat -A PREROUTING -p tcp --dport 目标端口号 -j DNAT --to-destination 目标IP
iptables -t nat -A PREROUTING -p udp --dport 目标端口号 -j DNAT --to-destination 目标IP
iptables -t nat -A POSTROUTING -p tcp -d 目标IP --dport 目标端口号 -j SNAT --to-source 本地服务器IP:端口
iptables -t nat -A POSTROUTING -p udp -d 目标IP --dport 目标端口号 -j SNAT --to-source 本地服务器IP:端口

# tcp示例
iptables -t nat -A PREROUTING -p tcp --dport 17079 -j DNAT --to-destination 10.170.0.133
iptables -t nat -A POSTROUTING -p tcp -d 10.170.0.133 --dport 17079 -j SNAT --to-source 10.0.3.24:8998
# udp示例
iptables -t nat -A PREROUTING -p udp --dport 17079 -j DNAT --to-destination 10.170.0.133
iptables -t nat -A POSTROUTING -p udp -d 10.170.0.133 --dport 17079 -j SNAT --to-source 10.0.3.24:8998