Linux 下常用命令纪要,便用便查。

98 篇文章 1 订阅
82 篇文章 3 订阅

查询系统准确的启动时间

[root@test ~]# 
[root@test ~]# date -d "$(awk -F. '{print $1}' /proc/uptime) second ago" +"%Y-%m-%d %H:%M:%S"
2018-01-19 14:45:35
[root@test ~]# who -b
         system boot  2017-11-22 06:04
[root@test ~]# 

查询所有进程的准确启动时间和运行时长

  • -e 表示查询所有进程
  • -o 表示按指定的格式输出
  • lstart 为进程启动时间
  • etime可以显示已经运行了多长时间
  • args 为具体的命令行参数
[root@test ~]# ps -e -o pid,lstart,etime,args
   PID                  STARTED     ELAPSED COMMAND
     1 Fri Jan 19 14:45:34 2018  3-08:37:23 /usr/lib/systemd/systemd --switched-root --system --deserialize 24
     2 Fri Jan 19 14:45:34 2018  3-08:37:23 [kthreadd]
     3 Fri Jan 19 14:45:34 2018  3-08:37:23 [ksoftirqd/0]
     5 Fri Jan 19 14:45:34 2018  3-08:37:23 [kworker/0:0H]
     6 Fri Jan 19 14:45:34 2018  3-08:37:23 [kworker/u256:0]
     7 Fri Jan 19 14:45:34 2018  3-08:37:23 [migration/0]
     8 Fri Jan 19 14:45:34 2018  3-08:37:23 [rcu_bh]
     9 Fri Jan 19 14:45:34 2018  3-08:37:23 [rcuob/0]
    10 Fri Jan 19 14:45:34 2018  3-08:37:23 [rcuob/1]

设置时间

date -s “dd/mm/yyyy hh:mm:ss”
也可以在centos7下使用
timedatectl set-time “2012-10-30 18:17:16”
使用该timedatactl 进行时间设置时,需要先将ntp关闭,不然会报错:“Failed to set time: Automatic time synchronization is enabled”。
具体操作见下:

[root@test ~]# date -s "2018/01/22 23:29"
Mon Jan 22 23:29:00 CST 2018
[root@test ~]# timedatectl set-time "2018-01-22 23:29"
Failed to set time: Automatic time synchronization is enabled
[root@test ~]# timedatectl set-ntp 0
[root@test ~]# timedatectl set-time "2018-01-22 23:29"
[root@test ~]# timedatectl set-ntp 1
[root@test ~]# timedatectl set-time "2018-01-22 23:29"
Failed to set time: Automatic time synchronization is enabled
[root@test ~]# 

同步OS与硬件时间

将OS系统时间同步到硬件(RTC)
hwclock –-systohc
将硬件时间同步到OS
hwclock –hctosys

[root@test ~]# 
[root@test ~]# hwclock --systohc
[root@test ~]# hwclock --hctosys
[root@test ~]# 

查看硬件信息

  • lscpu #查看CPU信息
  • lspci #查看PCI信息
  • lsusb #查看外设USB
  • lsscsi #查看SCSI设备
  • lsblk #查看块设备
  • dmidecode -t memory #查内存槽位信息. 不跟 -t , 查全量processor, Memory, BIOS



[root@test ~]# lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                1
On-line CPU(s) list:   0
Thread(s) per core:    1
Core(s) per socket:    1
Socket(s):             1
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 63
Model name:            Intel(R) Xeon(R) CPU E5-2680 v3 @ 2.50GHz
Stepping:              2
CPU MHz:               2494.280
BogoMIPS:              4988.56
Hypervisor vendor:     Xen
Virtualization type:   full
L1d cache:             32K
L1i cache:             32K
L2 cache:              256K
L3 cache:              30720K
NUMA node0 CPU(s):     0
[root@test ~]# lsusb
Bus 002 Device 002: ID 0627:0001 Adomax Technology Co., Ltd 
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
[root@test ~]# lsscsi
-bash: lsscsi: command not found
[root@test ~]# yum -y install lsscsi
[root@test ~]# lsscsi
[root@test ~]# lsblk 
NAME    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
xvda    202:0    0   40G  0 disk 
├─xvda1 202:1    0  500M  0 part /boot
└─xvda2 202:2    0 39.5G  0 part /
[root@test ~]# 
[root@test ~]# dmidecode -t memory
# dmidecode 2.12
SMBIOS 2.4 present.

Handle 0x1000, DMI type 16, 15 bytes
Physical Memory Array
    Location: Other
    Use: System Memory
    Error Correction Type: Multi-bit ECC
    Maximum Capacity: 1016 MB
    Error Information Handle: Not Provided
    Number Of Devices: 1

Handle 0x1100, DMI type 17, 21 bytes
Memory Device
    Array Handle: 0x1000
    Error Information Handle: 0x0000
    Total Width: 64 bits
    Data Width: 64 bits
    Size: 1016 MB
    Form Factor: DIMM
    Set: None
    Locator: DIMM 0
    Bank Locator: Not Specified
    Type: RAM
    Type Detail: None

[root@test ~]# 
[root@test ~]# dmidecode  memory
# dmidecode 2.12
SMBIOS 2.4 present.
11 structures occupying 346 bytes.
Table at 0xFC001000.

Handle 0x0000, DMI type 0, 24 bytes
BIOS Information
    Vendor: Xen
    Version: Unknown
    Release Date: 09/26/2017
    Address: 0xE8000
    Runtime Size: 96 kB
    ROM Size: 64 kB
    Characteristics:
        PCI is supported
        EDD is supported
        Targeted content distribution is supported
    BIOS Revision: 0.0

Handle 0x0100, DMI type 1, 27 bytes
System Information
    Manufacturer: Xen
    Product Name: HVM domU
    Version: Unknown
    Serial Number: 3e56659c-96ac-4c5c-b4f8-fd48d730e3e7
    UUID: 3E56659C-96AC-4C5C-B4F8-FD48D730E3E7
    Wake-up Type: Power Switch
    SKU Number: Not Specified
    Family: Not Specified

Handle 0x0300, DMI type 3, 13 bytes
Chassis Information
    Manufacturer: Xen
    Type: Other
    Lock: Not Present
    Version: Not Specified
    Serial Number: Not Specified
    Asset Tag: Not Specified
    Boot-up State: Safe
    Power Supply State: Safe
    Thermal State: Safe
    Security Status: Unknown

Handle 0x0401, DMI type 4, 26 bytes
Processor Information
    Socket Designation: CPU 1
    Type: Central Processor
    Family: Other
    Manufacturer: Intel
    ID: F2 06 03 00 FF FB 89 17
    Version: Intel(R) Xeon(R) CPU E5-2680 v3 @ 2.50GHz
    Voltage: Unknown
    External Clock: Unknown
    Max Speed: 2494 MHz
    Current Speed: 2494 MHz
    Status: Populated, Enabled
    Upgrade: Other

Handle 0x0B00, DMI type 11, 5 bytes
OEM Strings
    String 1: Xen

Handle 0x1000, DMI type 16, 15 bytes
Physical Memory Array
    Location: Other
    Use: System Memory
    Error Correction Type: Multi-bit ECC
    Maximum Capacity: 1016 MB
    Error Information Handle: Not Provided
    Number Of Devices: 1

Handle 0x1100, DMI type 17, 21 bytes
Memory Device
    Array Handle: 0x1000
    Error Information Handle: 0x0000
    Total Width: 64 bits
    Data Width: 64 bits
    Size: 1016 MB
    Form Factor: DIMM
    Set: None
    Locator: DIMM 0
    Bank Locator: Not Specified
    Type: RAM
    Type Detail: None

Handle 0x1300, DMI type 19, 15 bytes
Memory Array Mapped Address
    Starting Address: 0x00000000000
    Ending Address: 0x0003F7FFFFF
    Range Size: 1016 MB
    Physical Array Handle: 0x1000
    Partition Width: 1

Handle 0x1400, DMI type 20, 19 bytes
Memory Device Mapped Address
    Starting Address: 0x00000000000
    Ending Address: 0x0003F7FFFFF
    Range Size: 1016 MB
    Physical Device Handle: 0x1100
    Memory Array Mapped Address Handle: 0x1300
    Partition Row Position: 1

Handle 0x2000, DMI type 32, 11 bytes
System Boot Information
    Status: No errors detected

Handle 0x7F00, DMI type 127, 4 bytes
End Of Table

锁定文件,不被任何程序修改

chattr 可以改变文件扩展属性, chattr +i 可以防止任何程序修改该文件. 即使有root权限.
常用于锁定dns配置,防止dhcp程序自动修改. lsattr 查看当前的扩展属性. man chattr 查看所有参数含义.
chattr +i /etc/resolv.conf

[root@test ~]# lsattr
-------------e-- ./anaconda-ks.cfg
-------------e-- ./epel-release-latest-7.noarch.rpm
[root@test ~]# lsattr /etc/resolv.conf 
-------------e-- /etc/resolv.conf
[root@test ~]# chattr +i /etc/resolv.conf 
[root@test ~]# lsattr /etc/resolv.conf 
----i--------e-- /etc/resolv.conf
[root@test ~]# chattr -i /etc/resolv.conf 
[root@test ~]# lsattr /etc/resolv.conf 
-------------e-- /etc/resolv.conf
[root@test ~]# 

stat 查看文件详细元数据信息, 特别是inode

$ stat messages
File: ‘messages’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd01h/64769d Inode: 528438 Links: 1
Access: (0600/-rw——-) Uid: ( 0/ root) Gid: ( 0/ root)
Context: system_u:object_r:var_log_t:s0
Access: 2018-01-14 21:32:01.977218590 +0800
Modify: 2018-01-14 21:32:01.977218590 +0800
Change: 2018-01-14 21:32:01.977218590 +0800
Birth: -

[root@test ~]# stat /var/log/messages 
  File: ‘/var/log/messages’
  Size: 367390      Blocks: 728        IO Block: 4096   regular file
Device: ca02h/51714d    Inode: 2093357     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2018-01-22 23:40:02.118066621 +0800
Modify: 2018-01-22 23:40:01.118001621 +0800
Change: 2018-01-22 23:40:01.118001621 +0800
 Birth: -
[root@test ~]# 

xxd查看二进制文件

-s 指定起始位置(不指定则从0开始), -l 指定打印多少个字节
xxd -s 1 -l 5 /etc/hosts
当将js,或其他文本类信息嵌入到C程序时, xxd -i 非常实用.
xxd a.js
0000000: 7661 7220 6120 3d20 310a var a = 1.
xxd -i a.js
unsigned char a_js[] = {
0x76, 0x61, 0x72, 0x20, 0x61, 0x20, 0x3d, 0x20, 0x31, 0x0a
};
unsigned int a_js_len = 10;

[root@test ~]# 
[root@test ~]# xxd /etc/resolv.conf 
0000000: 2320 4765 6e65 7261 7465 6420 6279 204e  # Generated by N
0000010: 6574 776f 726b 4d61 6e61 6765 720a 7365  etworkManager.se
0000020: 6172 6368 206f 7065 6e73 7461 636b 6c6f  arch openstacklo
0000030: 6361 6c0a 6e61 6d65 7365 7276 6572 2031  cal.nameserver 1
0000040: 3134 2e31 3134 2e31 3134 2e31 3134 0a6e  14.114.114.114.n
0000050: 616d 6573 6572 7665 7220 3131 342e 3131  ameserver 114.11
0000060: 342e 3131 352e 3131 350a                 4.115.115.
[root@test ~]# xxd -s 1 -l 5  /etc/resolv.conf 
0000001: 2047 656e 65                              Gene
[root@test ~]# 

批量修改文件名

用法: rename 原字符串 目标字符串 文件
含义:
原字符串:将文件名需要替换的字符串;
目标字符串:将文件名中含有的原字符替换成目标字符串;
文件:指定要改变文件名的文件列

rename支持通配符:

  • ? 可替代单个字符
  • *可替代多个字符
  • [charset] 可替代charset集中的任意单个字符

将所有已 yum 开头的文件的后缀名从 .yumtx 改为 .txt
rename .yumtx .txt yum*

[root@test ~]# touch a.txt
[root@test ~]# touch b.txt
[root@test ~]# touch c.txt
[root@test ~]# rename .txt .yumtxt *.txt
[root@test ~]# ls
anaconda-ks.cfg  a.yumtxt  b.yumtxt  c.yumtxt  epel-release-latest-7.noarch.rpm
[root@test ~]# 

df 查询文件系统挂载点信息

df -hT
df -hi

-h 使容量的大小显示更人性化 -T 显示文件系统类型
-i 显示inode容量

[root@test ~]# df -hT
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/xvda2     ext4       39G  1.4G   36G   4% /
devtmpfs       devtmpfs  478M     0  478M   0% /dev
tmpfs          tmpfs     486M     0  486M   0% /dev/shm
tmpfs          tmpfs     486M   49M  437M  11% /run
tmpfs          tmpfs     486M     0  486M   0% /sys/fs/cgroup
/dev/xvda1     ext4      477M  148M  300M  34% /boot
tmpfs          tmpfs      98M     0   98M   0% /run/user/0
[root@test ~]# df -hi
Filesystem     Inodes IUsed IFree IUse% Mounted on
/dev/xvda2       2.5M   43K  2.5M    2% /
devtmpfs         120K   331  120K    1% /dev
tmpfs            122K     1  122K    1% /dev/shm
tmpfs            122K   389  122K    1% /run
tmpfs            122K    13  122K    1% /sys/fs/cgroup
/dev/xvda1       126K   341  125K    1% /boot
tmpfs            122K     1  122K    1% /run/user/0
[root@test ~]# 

du 命令

单位是KB 如下获取/tmp下所有文件的大小, 并按降序排列
du -xs /tmp/* | sort -rn -k1

[root@test ~]# du -xs /var/log/*
12  /var/log/boot.log
5584    /var/log/btmp
240 /var/log/cloud-init.log
136 /var/log/cron
40  /var/log/dmesg
40  /var/log/dmesg.old
320 /var/log/maillog
424 /var/log/messages
4676    /var/log/secure
12  /var/log/tuned
32  /var/log/wtmp
4   /var/log/yum.log
[root@test ~]# du -xs /var/log/* | sort -rn -k1
5584    /var/log/btmp
4676    /var/log/secure
424 /var/log/messages
320 /var/log/maillog
240 /var/log/cloud-init.log
136 /var/log/cron
40  /var/log/dmesg.old
40  /var/log/dmesg
32  /var/log/wtmp
12  /var/log/tuned
12  /var/log/boot.log
4   /var/log/yum.log

find删除查找到的文件

find命令经常会用到, -type f 表示只返回文件, -exec 可将已找到的结果作为标准输入执行其他命令.

find <PATH> -type f -exec rm {} \; # 删除查找到的文件, 将 path 改为实际要查找的目录

find [PATH] [option] [action]
-mtime n : n为数字,意思为在n天之前的“一天内”被更改过的文件;
-mtime +n : 列出在n天之前(不含n天本身)被更改过的文件名;
-mtime -n : 列出在n天之内(含n天本身)被更改过的文件名;
-newer file : 列出比file还要新的文件名
例如:
find /root -mtime 0 # 在当前目录下查找今天之内有改动的文件
与文件权限及名称有关的参数:
-name filename :找出文件名为filename的文件
-size [+-]SIZE :找出比SIZE还要大(+)或小(-)的文件
-tpye TYPE :查找文件的类型为TYPE的文件,TYPE的值主要有:一般文件(f)、设备文件(b、c)、
目录(d)、连接文件(l)、socket(s)、FIFO管道文件(p);
-perm mode :查找文件权限刚好等于mode的文件,mode用数字表示,如0755-perm -mode :查找文件权限必须要全部包括mode权限的文件,mode用数字表示
-perm +mode :查找文件权限包含任一mode的权限的文件,mode用数字表示

例如:
find / -name passwd # 查找文件名为passwd的文件
find . -perm 0755 # 查找当前目录中文件权限的0755的文件
find . -size +12k # 查找当前目录中大于12KB的文件,注意c表示byte

xargs 命令

该命令可以将一个命令的输出作为参数传递给另一个命令。
区别于管道符 | 是将将输出作为另一个命令的标准输入传递.

find /tmp -name *.png -type f | xargs tar -cvzf images.tar.gz

默认是将内容放到参数的最后面, 如果要放到指定位置,需要使用 -i 和 {}
如下所示,将第一个命令的输出放到 {} 出现的位置

ls /etc/*.conf | xargs -i cp {} /home/likegeeks/Desktop/out

[root@test ~]# find /var/log/ -name *.log -type f | xargs tar -cvzf logs.tar.gz
tar: Removing leading `/' from member names
/var/log/yum.log
/var/log/tuned/tuned.log
/var/log/boot.log
/var/log/cloud-init.log
[root@test ~]# ls logs.tar.gz 
logs.tar.gz
[root@test ~]# 
[root@test ~]# ls /etc/*.conf | xargs -i cp {} /tmp/xargs.files
[root@test ~]# ls /tmp/xargs.files 
/tmp/xargs.files
[root@test ~]# 

grep查询文本

在文件中查找字符串(不区分大小写)
grep -i “the” /etc/hosts
输出成功匹配的行,以及该行之后的三行. -B 表示前三行. -C 指前后三行
grep -A 3 “localhost” /etc/hosts
在一个文件夹中递归查询包含指定字符串的文件
grep -r “abc” /etc
查找不包含 127 的所有行
grep -v “127” /etc/hosts

[root@test ~]# grep -i "root" /etc/passwd 
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@test ~]# grep -A 3 "localhost" /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@test ~]# grep -r "abc" /etc
/etc/services:abcvoice-port   3781/tcp                # ABCvoice server port
/etc/services:abcvoice-port   3781/udp                # ABCvoice server port
/etc/services:abcsoftware     3996/tcp                # abcsoftware-01
/etc/services:abcsoftware     3996/udp                # abcsoftware-01
/etc/services:omabcastltkm    4359/tcp                # OMA BCAST Long-Term Key Messages
/etc/services:omabcastltkm    4359/udp                # OMA BCAST Long-Term Key Messages
Binary file /etc/udev/hwdb.bin matches
Binary file /etc/selinux/targeted/policy/policy.30 matches
Binary file /etc/selinux/targeted/contexts/files/file_contexts.homedirs.bin matches
/etc/selinux/targeted/contexts/files/file_contexts.homedirs:/home/[^/]+/abc --  unconfined_u:object_r:mozilla_home_t:s0
Binary file /etc/selinux/targeted/active/policy.kern matches
Binary file /etc/selinux/targeted/active/modules/100/zabbix/cil matches
/etc/selinux/targeted/active/homedir_template:HOME_DIR/abc  --  system_u:object_r:mozilla_home_t:s0
Binary file /etc/selinux/targeted/active/policy.linked matches
[root@test ~]# grep -v "127" /etc/hosts
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@test ~]# 

tail 查看文件指定行信息

显示最后3行记录
tail -3 /var/log/yum.log-20180101
从第770行开始显示
tail -n +770 /var/log/yum.log-20180101

[root@test ~]# tail -3 /var/log/messages 
Jan 23 22:01:01 test systemd: Starting Session 740 of user root.
Jan 23 22:10:01 test systemd: Started Session 741 of user root.
Jan 23 22:10:01 test systemd: Starting Session 741 of user root.
[root@test ~]# 
[root@test ~]# tail -n +770 /var/log/messages 
Jan 18 06:00:01 test systemd: Stopping User Slice of root.
Jan 18 06:01:01 test systemd: Created slice User Slice of root.
Jan 18 06:01:01 test systemd: Starting User Slice of root.
Jan 18 06:01:01 test systemd: Started Session 94 of user root.
Jan 18 06:01:01 test systemd: Starting Session 94 of user root.
Jan 18 06:01:01 test systemd: Removed slice User Slice of root.
Jan 18 06:01:01 test systemd: Stopping User Slice of root.
Jan 18 06:10:01 test systemd: Created slice User Slice of root.
Jan 18 06:10:01 test systemd: Starting User Slice of root.
Jan 18 06:10:01 test systemd: Started Session 95 of user root.

批量创建目录

mkdir -p new_folder/{folder_1,folder_2,folder_3,folder_4,folder_5}

[root@test ~]# mkdir -p new_folder/{folder_1,folder_2,folder_3,folder_4,folder_5}
[root@test ~]# ls new_folder/
folder_1  folder_2  folder_3  folder_4  folder_5
[root@test ~]# 

echo 相关命令

显示当前使用哪个shell
echo 0bashecho 0 − b a s h 显 示 最 近 一 个 命 令 执 行 的 结 果 码 e c h o ?
0

[root@test ~]# echo $0
-bash
[root@test ~]# echo $?
0
[root@test ~]# 

nohup与标准输出,标准错误输出

nohup 配和 & 可以让进程在后台运行, 如果没有显示指定, 默认将标准输出和错误输出重定向到 nohup.out
1>/dev/null 首先表示标准输出重定向到空设备文件,也就是不输出任何信息到终端,不显示任何信息
2>&1 表示标准错误输出重定向等同于标准输出,因为之前标准输出已经重定向到了空设备文件,所以标准错误输出
也重定向到空设备文件
常用使用方式为:
nohup COMMAND >output.log 2>&1 &

[root@test ~]# nohup yum -y install mysql-server > output.log 2>&1 &
[1] 31228
[root@test ~]# ls output.log 
output.log
[root@test ~]# cat output.log 
nohup: ignoring input
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.tuna.tsinghua.edu.cn
 * epel: mirrors.sohu.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.tuna.tsinghua.edu.cn
No package mysql-server available.
Error: Nothing to do
[1]+  Exit 1                  nohup yum -y install mysql-server > output.log 2>&1
[root@test ~]# 

Linux模块的安装和卸载

lsmod #显示当前装入的内核模块
modinfo module_name #显示模块信息
modprobe -c #显示模块的配置信息
modprobe –show-depends module_name #显示模块的依赖信息
modprobe module_name #手动加载模块
rmmod module_name #卸载模块

[root@test ~]# lsmod 
Module                  Size  Used by
dm_mirror              22135  0 
dm_region_hash         20862  1 dm_mirror
dm_log                 18411  2 dm_region_hash,dm_mirror
dm_mod                104038  2 dm_log,dm_mirror
intel_rapl             18773  0 
crct10dif_pclmul       14289  0 
crct10dif_common       12595  1 crct10dif_pclmul
crc32_pclmul           13113  0 
crc32c_intel           22079  0 

systemd 读取 /etc/modules-load.d/ 中的配置加载额外的内核模块。配置文件名称通常为 /etc/modulesload.
d/.conf 。如:
cat /etc/modules-load.d/bonding.conf
bonding
使用 /etc/modprobe.d/中的文件来配置传递参数,如:
/etc/modprobe.d/bonding.conf
options bonding mode=1
别名

$ cat /etc/modprobe.d/myalias.conf
# Lets you use 'mymod' in MODULES, instead of 'really_long_module_name'
alias mymod really_long_module_name

如果模块直接编译进内核, 也可以通过启动管理器(GRUB, LILO 或 Syslinux) 的内核行加入参数:
modname.parametername=parametercontents

column格式化输出

可以让一些命令的输出看起来更舒服些. 例如 blkid , mount , cat /etc/fstab .
[root@test ~]# blkid
[root@test ~]# blkid | column -t

-s 参数指定可以指定分隔符. 默认是空格

Usage:
column [options] [file …]

Options:
-c, –columns width of output in number of characters
-t, –table create a table
-s, –separator possible table delimiters
-o, –output-separator
table output column separator, default is two spaces
-x, –fillrows fill rows before columns

-h, –help display this help and exit
-V, –version output version information and exit

通过pid查看进程的环境变量信息

使用strings, 可以格式化打印
$ strings /proc/1158/environ
LANG=en_US.UTF-8
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
NOTIFY_SOCKET=/run/systemd/notify
SSH_USE_STRONG_RNG=0

通过pid查看进程对应可执行文件的绝对路径

readlink /proc/[pid]/exe

通过pid查看进程的当前工作目录

pwdx [pid]

lsof 一切皆文件

-p [PID] 只显示该进程打开的所有文件. 不带参数显示所有已打开的文件
-d 对 FD 有效, 用于筛选文件列表. ^txt 显示除txt 其他所有类型的文件. 1 显示所有fd为1的文件. 可以使用 , 逗号
连接多个选择
-a 表示两个参数都必须满足 (AND)。如果没有 -a 标志,缺省的情况是显示匹配任何一个参数 (OR) 的文件
-n 阻止网络地址转换
-P 阻止端口号到端口名的转换
-i protocol:@ip:port protocol 包括 tcp 和 udp. 显示符合该地址的文件列表 -u s s为用户名或者用户ID, 选择该用户下的文件

lsof [name]
name是 mount point或者文件系统对应的设备文件, 则显示在该文件系统上打开的所有文件列表
name是 文件夹(非mount point), 则显示所有将该文件夹作为正常文件打开的列表. 例如 cwd, rtd. 如果 +d 打印所有
在该目录下已打开的文件,但不递归查找子目录. +D 则允许递归查找

谁在使用 /var/log/messages
[root@test ~]# lsof /var/log/messages
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
abrt-watc 712 root 4r REG 202,2 555975 2093357 /var/log/messages
rsyslogd 714 root 3w REG 202,2 555975 2093357 /var/log/messages

显示文件系统 / 下所有已打开的文件列表. 和 fuser / 效果一样
$ lsof /
所有在/var下已打开的文件

[root@test ~]# lsof +D /var/log/
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF    NODE NAME
abrt-watc 712 root    4r   REG  202,2   555975 2093357 /var/log/messages
rsyslogd  714 root    3w   REG  202,2   555975 2093357 /var/log/messages
rsyslogd  714 root    6w   REG  202,2  6985172 2094508 /var/log/secure
rsyslogd  714 root    7w   REG  202,2   153861 2093173 /var/log/cron
tuned     732 root    3w   REG  202,2     7708 2093361 /var/log/tuned/tuned.log
[root@test ~]# 

列出root这个用户打开的所有文件

lsof -u root

dd 测试磁盘或者文件读写

高危命令, of 一定要指向正确的文件, 不要指 / , /dev/vda , /dev/vda1 等系统重要设备.
该命令要在测试环境验证充分.
if 表示从哪个设备/文件读
of 表示写到哪个设备/文件
bs 表示一次读写多少字节. 也可以使用 1K, 1M这样带单位的
count 表示最多读写多少次. 总的读写量为 bs * count
/dev/zero 可以无限读取 \0
dd if=/dev/zero of=/tmp/abc.txt bs=1M count=10

rpm 安装/更新/卸载软件包

查询系统已安装的所有软件包
rpm -qa
查询某个文件所属的软件包名. 文件必须是绝对路径
rpm -qf /etc/ssh/sshd_config
rpm -qf which strace
查询包所含有的文件
rpm -ql strace
查询包里的配置文件
rpm -qc openssh-server
查询软件包里的脚本信息
rpm -q –scripts openssh-server
查询当前包的changelog. 常用于查看已解决的CVE列表
rpm -q –changelog openssh-server
查询包所能提供的CAPABILITY
rpm -q –provides glibc
查询哪些包依赖某个CAPABILITY
rpm -q –whatrequires [CAPABILITY]
查询包所依赖的CAPABILITY
rpm -q -R glibc
查询包所有提供的CAPABILITY
rpm -q –whatprovides [CAPABILITY]
校验当前包与原始状态的差别.
5 – MD5 校验和
S – 文件长度
L – 符号链接
T – 文件修改日期
D – 设备
U – 用户
G – 用户组
M – 模式 (包含许可和文件类型)
? – 不可读文件
如下标志文件的md5, 文件长度, 修改日志有变化
$ rpm -V openssh-server
S.5….T. c /etc/ssh/sshd_config
安装本地包 -vh 获得一个详细的安装进程 –nodeps 忽略依赖关系
rpm -ivh abc.rpm
卸载包
rpm -e abc.rpm
升级包
rpm -Uvh abc.rpm

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值