linux必学的60个命令实例:日常9个Linux常用命令
linux必学的60个命令实例:日常9个Linux常用命令-e 表示查询所有进程```[root@test ~]# who -b system boot 2017-11-22 06:04[root@test ~]#
##查询系统准确的启动时间```
[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
具体操作见下:
```Shell
[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/51714dInode: 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 ~]#
```