Linux下常用命令

1 端口

1.1 查询端口是否使用,以80端口为例

  • netstat -tuln | grep :<端口号>
netstat -tuln | grep :80
  • lsof -i :<端口号>(推荐,会列出使用端口的进程PID)
lsof -i :80
  • ss -tulnp | grep :<端口号>(在一些新版本的Linux中)
sudo ss -tulnp | grep :80

2 进程

2.1 PID

2.1.1 根据PID查询启动进程的命令

  • /proc/<PID>/cmdline
cat /proc/1234/cmdline | tr '\0' ' '

2.1.2 根据服务id查询可执行文件位置

ls -l /proc/3371187/cwd

2.2 TID

首先通过 top 命令获取内存或者CPU占用异常的进程的 PID,然后使用下面的命令查看线程级别的占用

top -Hp <PID>

找到占用高的线程 TID → 转十六进制

printf "%x\n" <TID>

用 jstack 输出线程堆栈,txt名称随便起

jstack <PID> > jstack.txt

在 jstack.txt 中搜索该线程,nid是0x起头(十六进制)

grep -A 30 "nid=0x<hex_TID>" jstack.txt

一般如果是代码的问题,这里就能看到具体的堆栈信息了,但是如果是GC导致的,那么就得去打印GC日志定位了。

3 压缩

3.1 zip

  • 解压
unzip archive.zip
  • 压缩文件
zip archive.zip file1.txt file2.txt
  • 压缩整个目录
zip -r archive.zip directory_name

如果没有 unzip 命令,可以使用 JDK 来进行解压操作

jar -xvf yourfile.zip

同样的,也可以进行压缩操作

jar -cvf myapp.zip myapp/

4 rpm

rpm:Red Hat Package Manager,是用于管理 RPM 包的工具。

4.1 手动安装rpm包

5 SELinux

SELinux(Security-Enhanced Linux)

5.1 检查 SELinux 的当前状态

sestatus

5.2 临时设置为宽松模式

setenforce 0

6 时间

使用date

# 简单查询当前时间
date

# 修改,使用这个修改似乎可以绕开下面的那个自动同步的限制
date +%Y-%m-%d\ %H:%M:%S -s "2024-11-14 10:30:00"

使用timedatectl

# 查看当前时区
timedatectl

# 设置时区
timedatectl set-timezone Asia/Shanghai

# 设置当前时间
timedatectl set-time "2024-11-14 10:30:00"

注意,设置时间的时候可能会遇到这个错误
Failed to set time: NTP unit is active
如果在尝试修改时间时遇到错误消息 "Failed to set time: NTP unit is active",意味着系统时间同步服务(NTP)正在运行,并且它会自动调整时间,因此你不能手动设置系统时间。
解决办法是先关掉,修改后再打开。
不是特别老的系统都是chronyd,所以关闭重启的时候试试这2个

systemctl stop ntpd

systemctl stop chronyd

6.1 时区无法设置的临时解决方案

有时候可能会遇到服务器时区无法设置,又不能随便重启服务器,可以使用下面的命令来手动设置时间,减去时区

date --set "$(date --date='8 hours ago')"

7 grep日志查询

7.1 tail实时查询

实时查看日志并过滤特定内容:

tail -f /path/to/logfile | grep -i "关键字"
  • tail -f 会实时显示日志文件的内容。
  • grep "关键字" 会过滤出包含 "关键字" 的行。
  • -i 忽略大小写

查看特定行数的日志后开始过滤:

tail -n 100 /path/to/logfile | grep "关键字"
  • 这会显示文件的最后 100 行并过滤出包含 "关键字" 的内容。

查看搜索内容的上下文:

tail -f /var/log/syslog | grep -C 3 "error"
  • -B(before)
  • -A(after)
  • -C(Context)

组合多个 grep 筛选器:

tail -f /var/log/syslog | grep -E "error|warning" | grep -C 3 "error"

查找包含 "error" 和 "warning" 的日志,并且还会显示 "error" 前后各 3 行。

  • -E:扩展正则表达式(Extended Regular Expression,简称 ERE)

7.2 cat顺序查询(非实时)

使用 catgrep 组合查询,示例:

cat hil-ibss-itss.dev_info.log | grep -A 30 '告警恢复'

7.3 tac倒叙查询最后一个匹配

使用 tac 可以查询最后一个匹配,示例:

tac hil-ibss-itss.dev_info.log | grep -m1 -B30 '告警恢复' | tac

说明:

  • tac:倒序读取文件内容(即从最后一行开始看);
  • grep -m1:匹配“告警”的第一个(即倒序时的最后一个);
  • -B30:显示匹配行之前的30行(因为现在是倒序);
  • 再通过 tac 把结果正过来。

8 看文件和目录的详细信息

查看文件的年份

ll --time-style=long-iso

9 磁盘空间

9.1 磁盘空闲空间

  • df:disk free
  • -h:human-readable,以 KB/MB/GB 显示
df -h

9.2 磁盘使用空间

  • du:disk usage
  • --max-depth=1:只显示当前目录和它的直接子目录的大小(不递归更深)

✅ 查看当前目录下所有一级子目录占用空间(人类可读)

du -h --max-depth=1

10 系统和硬件

10.1 判断当前机器是物理机还是虚拟机

方法 1:dmidecode

sudo dmidecode -t system | grep -i "Manufacturer\|Product Name"

常见输出:

物理机(例如 Dell、HP、Lenovo):

Manufacturer: Dell Inc.
Product Name: PowerEdge R740


虚拟机:

Manufacturer: VMware, Inc.
Product Name: VMware Virtual Platform

Manufacturer: Microsoft Corporation
Product Name: Virtual Machine   # Hyper-V

Manufacturer: QEMU
Product Name: Standard PC (Q35 + ICH9, 2009)   # KVM/QEMU

方法 2:systemd-detect-virt

大多数新系统都有这个命令:

systemd-detect-virt

输出可能是:

kvm → KVM 虚拟机

vmware → VMware

microsoft → Hyper-V

oracle → VirtualBox

none → 物理机

方法 3:lscpu

lscpu | grep Hypervisor

如果有类似:

Hypervisor vendor:     KVM
Virtualization type:   full
``

→ 说明是虚拟机。

如果没有 Hypervisor 信息 → 多半是物理机。

### 方法 4:dmesg
```shell
dmesg | grep -i hypervisor

如果能看到 KVM, VMware, Hyper-V 等字样 → 虚拟机。

没有 → 大概率物理机。

📌 推荐组合:

dmidecode -t system | egrep -i "Manufacturer|Product"
systemd-detect-virt
lscpu | grep Hypervisor

三条结果对照一下,就能 100% 确认。

posted @ 2024-10-31 21:43  大唐冠军侯  阅读(77)  评论(0)    收藏  举报