Linux 命令 常用总结(三)

Linux 命令 常用总结(三)

目录

Linux 命令 常用总结(三)

(41):zip

(42):unzip

(43):dmesg

(44):uptime

(45):stat

(46):du

(47):df

(48):top

(49):free

(50):date

(51):which

(52):whereis

(53):who

(54):w

(55):id

(56):su/sudo

(57):cal

(58):telnet

(59):ssh


(41):zip

命令简介

zip 命令可以用来压缩、打包文件。

#Debian/Ubuntu安装
apt-get install zip

#CentOS安装
[root@centos7 testdir]# zip
-bash: zip: command not found
[root@centos7 testdir]# yum install zip -y

zip 命令也可以用来解压缩文件,zip也是一个常用的压缩、解压应用程序,文件经它压缩后会产生一个新以.zip为扩展名的压缩包文件。

语法格式

zip [ OPTIONS ] [ NAME ..]

选项说明

-A  #调整可执行的自动解压缩文件
-b<工作目录>  #指定压缩文件的存放目录
-c  #给每个被压缩的文件加上注释信息
-d  #从压缩文件内删除指定的文件,也可以使用--delete参数
-D  #压缩文件内不建立目录名称
-f  #更新既有文件,将其它文件一并加入到压缩文件中
-F  #修复已损坏的压缩文件
-g  #将指定文件压缩至已存在的压缩文件中,不建立新文件
-h  #打印帮助信息
-i<范本样式>  #只压缩匹配指定条件的文件
-m  #将指定文件压缩打包后直接删除原始文件
-o  #将压缩文件的属性信息更改成与压缩文件中的最新文件的属性一致
-q  #安静模式
-r  #递归处理
-t<日期时间>  #把压缩文件的日期设成指定的日期
-T  #检查备份文件内的每个文件是否正确无误
-u  #更新较新的文件到压缩文件内
-v  #打印命令执行过程信息或版本信息
-x<范本样式>  #压缩时排除符合条件的文件
-z  #给压缩文件加上注释信息
-<压缩效率>  #指定压缩效率(1~9数字)

应用举例

在当前目录下创建压缩文件(自动创建以.zip的文件)

[root@centos7 testdir]# zip mingongge *
  adding: cest.txt.gz (stored 0%)
  adding: cuttest.txt.gz (stored 0%)
  adding: dir/ (stored 0%)
  adding: file (stored 0%)
  adding: mingongge1.txt.gz (stored 0%)
  adding: mingongge2.txt.gz (stored 0%)
  adding: mingongge.txt.md5.gz (stored 0%)
  adding: sort.cut.txt.gz (stored 0%)

分割一个大文件

[root@centos7 ~]# ls -lh
total 22M
-rw-------.  1 root root 1.3K Aug 20 10:39 anaconda-ks.cfg
-rw-r--r--   1 root root  140 Jan 16 11:36 dos_test.txt
drwxr-xr-x   3 root root   39 Aug 30 03:48 goinception
-rw-r--r--   1 root root  13M Aug 30 03:42 goInception-linux-amd64-v1.2.3.tar.gz
drwxr-sr-x  11 root   40 4.0K Dec 24 22:35 httpd-2.4.46
-rw-r--r--   1 root root 9.0M Aug  5 07:32 httpd-2.4.46.tar.gz
-rw-r--r--   1 root root    0 Jan 16 11:32 mingongge.file
drwxr-xr-x   3 root root  192 Jan 16 16:19 testdir
-rw-r--r--   1 root root  140 Jan 16 11:32 test.txt
[root@centos7 ~]# zip -s 4M -r mingongge.zip httpd-2.4.46.tar.gz
  adding: httpd-2.4.46.tar.gz (deflated 0%)
[root@centos7 ~]# ls -lh
total 31M
-rw-------.  1 root root 1.3K Aug 20 10:39 anaconda-ks.cfg
-rw-r--r--   1 root root  140 Jan 16 11:36 dos_test.txt
drwxr-xr-x   3 root root   39 Aug 30 03:48 goinception
-rw-r--r--   1 root root  13M Aug 30 03:42 goInception-linux-amd64-v1.2.3.tar.gz
drwxr-sr-x  11 root   40 4.0K Dec 24 22:35 httpd-2.4.46
-rw-r--r--   1 root root 9.0M Aug  5 07:32 httpd-2.4.46.tar.gz
-rw-r--r--   1 root root    0 Jan 16 11:32 mingongge.file
-rw-r--r--   1 root root 4.0M Jan 16 16:24 mingongge.z01
-rw-r--r--   1 root root 4.0M Jan 16 16:24 mingongge.z02
-rw-r--r--   1 root root 943K Jan 16 16:24 mingongge.zip
drwxr-xr-x   3 root root  192 Jan 16 16:19 testdir
#从结果可以看出会拆分成三个文件即:4M大小的mingongge.z01、4M大小的mingongge.z02和一个943k的mingongge.zip文件。



(42):unzip

 

昨日推荐:每天学一个 Linux 命令(41):zip

命令简介

unzip 命令用于解压由zip命令压缩的压缩包文件。

#Debian/Ubuntu安装
apt-get install unzip

#CentOS安装
[root@centos7 ~]# unzip 
-bash: unzip: command not found
[root@centos7 ~]# yum install unzip -y

语法格式

unzip [ OPTIONS ] file[.zip] [file(s) ...]

选项说明

-c  #将解压缩的结果输出,并对字符做适当的转换
-f  #更新现有的文件
-l  #列出压缩文件内所包含的文件
-p  #将解压缩的结果显示到屏幕上,但不执行任何的转换
-t  #检查压缩文件是否正确;
-u  #除了更新现有的文件外,也会将压缩文件中的其他文件解压缩到目录中
-v  #显示详细的信息
-z  #仅显示压缩文件的备注信息
-a  #对文本文件进行必要的字符转换
-b  #不对文本文件进行字符转换
-C  #压缩文件名称区分大小写
-j  #不处理压缩文件中原有的目录路径
-L  #将压缩文件中的全部文件名改为小写
-M  #将输出结果再交给more程序处理
-n  #解压缩时不覆盖原有的文件
-o  #unzip执行后覆盖原有的文件,不提示
-P<密码>  #使用zip的密码选项
-q  #不显示任何命令执行过程信息
-s  #将文件名中的空白字符转换为底线字符
-d<目录>  #将解压缩后存至指定的目录下
-x<文件>  #指定不要处理.zip压缩文件中的哪些文件
-Z  #unzip-Z相当于执行zipinfo命令

应用举例

解压一个文件

[root@centos7 testdir]# unzip mingongge.zip

查看一个压缩文件但不解压

[root@centos7 testdir]# unzip -v mingongge.zip 
Archive:  mingongge.zip
 Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- ----- --------  ----
      59  Stored       59   0% 01-16-2021 12:15 b32621da  cest.txt.gz
      57  Stored       57   0% 01-16-2021 12:12 cbda1ce8  cuttest.txt.gz
       0  Stored        0   0% 01-16-2021 16:18 00000000  dir/
       0  Stored        0   0% 01-16-2021 16:18 00000000  file
      81  Stored       81   0% 01-16-2021 09:55 da9f2476  mingongge1.txt.gz
      51  Stored       51   0% 01-16-2021 03:36 8fdf382e  mingongge2.txt.gz
      87  Stored       87   0% 01-16-2021 09:59 982ab7bb  mingongge.txt.md5.gz
      65  Stored       65   0% 01-16-2021 10:19 17350869  sort.cut.txt.gz
--------          -------  ---                            -------
     400              400   0%                            8 files

指定解压后的文件存放目录

[root@centos7 testdir]# unzip -n mingongge.zip -d /tmp/
Archive:  mingongge.zip
 extracting: /tmp/cest.txt.gz        
 extracting: /tmp/cuttest.txt.gz     
   creating: /tmp/dir/
 extracting: /tmp/file               
 extracting: /tmp/mingongge1.txt.gz  
 extracting: /tmp/mingongge2.txt.gz  
 extracting: /tmp/mingongge.txt.md5.gz  
 extracting: /tmp/sort.cut.txt.gz    
[root@centos7 testdir]# ll /tmp/
total 24
-rw-r--r-- 1 root root 59 Jan 16 12:15 cest.txt.gz
-rw-r--r-- 1 root root 57 Jan 16 12:12 cuttest.txt.gz
drwxr-xr-x 2 root root  6 Jan 16 16:18 dir
-rw-r--r-- 1 root root  0 Jan 16 16:18 file
-rw-r--r-- 1 root root 81 Jan 16 09:55 mingongge1.txt.gz
-rw-r--r-- 1 root root 51 Jan 16 03:36 mingongge2.txt.gz
-rw-r--r-- 1 root root 87 Jan 16 09:59 mingongge.txt.md5.gz
-rw-r--r-- 1 root root 65 Jan 16 10:19 sort.cut.txt.gz



(43):dmesg

命令简介

dmesg 命令用于显示系统开机信息,可用于诊断系统故障。

内核会将系统开机信息存储在ring buffer中,可以使用dmesg命令来查看,开机信息保存在/var/log/dmesg文件中。

命令语法

dmesg [options]

选项说明

-c  #显示信息后,清除ring buffer中的内容
-s<缓冲区大小>  #默认值为8196,刚好等于ring buffer的大小
-n  #设置记录信息的层级
-D  #禁用打印消息到控制台
-E  #启用打印消息到控制台
-h  #打印帮助文本并退出
-k  #打印内核消息
-n  #设置将消息记录到控制台的级别
-r  #打印原始消息缓冲区
-s  #使用多少大小的缓冲区来查询内核环缓冲区。 默认情况下为16392
-T  #打印人类可读时间戳
-t  #不打印内核的时间戳
-u  #打印用户空间消息
-V  #输出版本信息并退出
-x  #将设施和级别(优先级)编号解码为可读的前缀

应用举例

查看前20行开机信息

[root@centos7 ~]# dmesg | head -n 20
[    0.000000] Initializing cgroup subsys cpuset
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Initializing cgroup subsys cpuacct
[    0.000000] Linux version 3.10.0-1127.18.2.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) ) #1 SMP Sun Jul 26 15:27:06 UTC 2020
[    0.000000] Command line: BOOT_IMAGE=/vmlinuz-3.10.0-1127.18.2.el7.x86_64 root=/dev/mapper/centos-root ro crashkernel=auto spectre_v2=retpoline rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet LANG=en_US.UTF-8
[    0.000000] Disabled fast string operations
[    0.000000] e820: BIOS-provided physical RAM map:
[    0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009ebff] usable
[    0.000000] BIOS-e820: [mem 0x000000000009ec00-0x000000000009ffff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000000dc000-0x00000000000fffff] reserved
[    0.000000] BIOS-e820: [mem 0x0000000000100000-0x000000003fedffff] usable
[    0.000000] BIOS-e820: [mem 0x000000003fee0000-0x000000003fefefff] ACPI data
[    0.000000] BIOS-e820: [mem 0x000000003feff000-0x000000003fefffff] ACPI NVS
[    0.000000] BIOS-e820: [mem 0x000000003ff00000-0x000000003fffffff] usable
[    0.000000] BIOS-e820: [mem 0x00000000f0000000-0x00000000f7ffffff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000fec00000-0x00000000fec0ffff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000fee00000-0x00000000fee00fff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000fffe0000-0x00000000ffffffff] reserved
[    0.000000] NX (Execute Disable) protection: active
[    0.000000] SMBIOS 2.4 present.

查看与内存相关的开机信息

[root@centos7 ~]# dmesg | grep -i memory
[    0.000000] Base memory trampoline at [ffff9102c0098000] 98000 size 24576
[    0.000000] crashkernel=auto resulted in zero bytes of reserved memory.
[    0.000000] Early memory node ranges
[    0.000000] PM: Registered nosave memory: [mem 0x0009e000-0x0009efff]
[    0.000000] PM: Registered nosave memory: [mem 0x0009f000-0x0009ffff]
[    0.000000] PM: Registered nosave memory: [mem 0x000a0000-0x000dbfff]
[    0.000000] PM: Registered nosave memory: [mem 0x000dc000-0x000fffff]
[    0.000000] PM: Registered nosave memory: [mem 0x3fee0000-0x3fefefff]
[    0.000000] PM: Registered nosave memory: [mem 0x3feff000-0x3fefffff]
[    0.000000] Memory: 972140k/1048576k available (7784k kernel code, 524k absent, 75912k reserved, 5958k data, 1980k init)
[    0.000000] please try 'cgroup_disable=memory' option if you don't want memory cgroups
[    0.755288] Initializing cgroup subsys memory
[    1.619645] x86/mm: Memory block size: 128MB
[    3.669071] Freeing initrd memory: 20628k freed
[    3.933907] Non-volatile memory driver v1.3
[    3.935079] crash memory driver: version 1.1
[    4.000261] Freeing unused kernel memory: 1980k freed
[    4.001692] Freeing unused kernel memory: 396k freed
[    4.003171] Freeing unused kernel memory: 540k freed
[    5.956205] [drm] Max dedicated hypervisor surface memory is 0 kiB
[    5.956206] [drm] Maximum display memory size is 32768 kiB
[    5.968934] [TTM] Zone  kernel: Available graphics memory: 497842 kiB

查看与磁盘相关的开机信息

[root@centos7 ~]# dmesg | grep -i disk
[    0.000000] RAMDISK: [mem 0x357a7000-0x36bcbfff]
[    3.738914] VFS: Disk quotas dquot_6.5.2
[    4.013333] systemd[1]: Running in initial RAM disk.
[    6.295432] sd 0:0:0:0: [sda] Attached SCSI disk
[root@centos7 ~]# dmesg | grep -i sda
[    6.291698] sd 0:0:0:0: [sda] 41943040 512-byte logical blocks: (21.4 GB/20.0 GiB)
[    6.291830] sd 0:0:0:0: [sda] Write Protect is off
[    6.291831] sd 0:0:0:0: [sda] Mode Sense: 61 00 00 00
[    6.292032] sd 0:0:0:0: [sda] Cache data unavailable
[    6.292033] sd 0:0:0:0: [sda] Assuming drive cache: write through
[    6.294046]  sda: sda1 sda2
[    6.295432] sd 0:0:0:0: [sda] Attached SCSI disk
[   15.843965] XFS (sda1): Mounting V5 Filesystem
[   16.778908] XFS (sda1): Starting recovery (logdev: internal)
[   16.801659] XFS (sda1): Ending recovery (logdev: internal)



(44):uptime

命令简介

uptime命令用于显示系统运行时间及负载。

uptime 命令可以打印出系统总共运行了多长时间和系统的平均负载。uptime 命令显示的信息显示依次为:现在时间、系统已经运行了多长时间、目前有多少登陆用户、系统在过去的 1 分钟、5 分钟和 15 分钟内的平均负载。

[root@centos7 ~]# uptime
 19:48:14 up 21:02,  1 user,  load average: 0.00, 0.01, 0.05

命令语法

uptime [options]

选项说明

-p #以好看的格式显示正常运行时间
-s #自系统启动来的时间
-h #打印帮助信息
-V #打印版本信息。

应用举例

[root@centos7 ~]# uptime -V
uptime from procps-ng 3.3.10

[root@centos7 ~]# uptime 
 19:50:29 up 21:04,  1 user,  load average: 0.03, 0.03, 0.05
 
[root@centos7 ~]# uptime -p
up 21 hours, 4 minutes

[root@centos7 ~]# uptime -s
2021-01-15 22:46:06

[root@centos7 ~]# uptime -h

Usage:
 uptime [options]

Options:
 -p, --pretty   show uptime in pretty format
 -h, --help     display this help and exit
 -s, --since    system up since
 -V, --version  output version information and exit

For more details see uptime(1).

文件存放目录

/var/run/utmp #当前登录者的信息
[root@centos7 ~]# cat /var/run/utmp
~~~reboot3.10.0-1127.18.2.el7.x86_64󞞗/vtty1tty1LOGINv `S3~~~runlevel3.10.0-1127.18.2.el7.x86_64 `pts/0ts/0root192.168.1.93󂠻(]



(45):stat

命令简介

stat 命令用于显示文件或文件系统的状态。

命令语法

stat [OPTION]... FILE...

文件:指定要显示信息的普通文件或者文件系统对应的设备文件名

选项说明

-L  #支持符号连接
-f  #显示文件系统状态而非文件状态
-t  #以简洁方式输出信息
-c  #使用指定的格式而不是默认格式
-Z  #打印 SELinux 安全上下文
--help  #打印帮助信息
--version  #打印的版本信息

应用举例

查看文件test.txt详细信息

[root@centos7 ~]# stat test.txt
  File: ‘test.txt’
  Size: 140        Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d Inode: 33575001    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2021-01-16 11:34:32.000000000 -0500
Modify: 2021-01-16 11:32:55.000000000 -0500
Change: 2021-01-16 11:38:17.157156882 -0500
 Birth: -
 
[root@centos7 ~]# stat -f test.txt
  File: "test.txt"
    ID: fd0000000000 Namelen: 255     Type: xfs
Block size: 4096       Fundamental block size: 4096
Blocks: Total: 4452864    Free: 3984106    Available: 3984106
Inodes: Total: 8910848    Free: 8860529

[root@centos7 ~]# stat -t test.txt
test.txt 140 8 81a4 0 0 fd00 33575001 1 0 0 1610814872 1610814775 1610815097 0 4096

获取文件权限的数字

[root@centos7 ~]# stat test.txt |awk 'NR==4' |awk -F '[(0/]' '{print $3}'
644
[root@centos7 ~]# stat test.txt |awk 'NR==4' |cut -c 11-13 
644
[root@centos7 ~]# stat test.txt |sed -n '4p' |cut -c 11-13 
644
[root@centos7 ~]# stat -c %a test.txt
644

文件有效格式说明

The valid format sequences for files (without --file-system):

  %a   #显示8进制访问权限
  %A   #可读格式的访问权限
  %b   #可分配的块数
  %B   #每个块的字节大小
  %C   #SELinux安全上下文字符串
  %d   #设备编号(十进制)
  %D   #设备编号(十六进制)
  %f   #文件类型(十六进制)
  %F   #文件类型
  %g   #所有者的组ID
  %G   #所有者的组名
  %h   #硬链接数
  %i   #inode号
  %m   #挂载点
  %n   #文件名
  %N   #带引号的文件名,如果有软链接则取消引用
  %o   #IO块大小
  %s   #总大小(以字节为单位)
  %t   #十六进制的主要设备类型
  %T   #次设备类型(十六进制)
  %u   #所有者的用户ID
  %U   #所有者的用户
  %x   #最后访问时间
  %X   #最后访问时间(以秒为单位)
  %y   #最后修改时间
  %Y   #最后修改时间(以秒为单位)
  %z   #最后更改时间
  %Z   #最后更改时间(以秒为单位)

文件系统有效格式说明:

Valid format sequences for file systems:

  %a   #非超级用户可用的空闲块
  %b   #文件系统中的数据块总数
  %c   #文件系统中的文件节点总数
  %d   #文件系统中的空闲文件节点
  %f   #文件系统中的空闲块
  %i   #十六进制文件系统ID
  %l   #文件名的最大长度
  %n   #文件名
  %s   #最佳传输块大小
  %t   #十六进制的形式输入
  %T   #以易读的形式输入
[root@centos7 ~]# stat -f /dev/sda
  File: "/dev/sda"
    ID: 0        Namelen: 255     Type: tmpfs
Block size: 4096       Fundamental block size: 4096
Blocks: Total: 121517     Free: 121517     Available: 121517
Inodes: Total: 121517     Free: 121132

 




(46):du

命令简介

du 命令用于显示每个文件和目录使用磁盘空间大小,du 会显示指定的目录或文件所占用的磁盘空间大小。还可以利用 du 命令,可以快速的查找 Linux 下的大目录。

命令语法

du [OPTION]... [FILE]...

选项说明

-a  #显示目录中所有文件的大小
-b  #显示目录或文件大小时,以byte为单位。
-c  #除了显示个别目录或文件的大小外,同时也显示所有目录或文件的总和
-D  #显示指定符号连接的源文件大小
-h  #以K,M,G为单位,提高可读性
-H  #与-h参数相同,但以1000为换算单位
-k  #以1024 bytes为单位
-l  #重复计算硬件连接的文件
-L<符号连接>  #显示选项中所指定符号连接的源文件大小
-m  #以1MB为单位
-s  #只显示总和
-S  #显示指定目录的大小,但是不显示其子目录
--exclude=<目录或文件> #忽略指定的目录或文件
--max-depth=<目录层数> #超过指定层数的目录后,忽略
--help  #打印帮助信息
--version  #打印版本信息

应用举例

显示目录或者文件所占用的磁盘空间

[root@centos7 testdir]# du
0 ./dir
28 .
[root@centos7 testdir]# du -h *.gz
4.0K cest.txt.gz
4.0K cuttest.txt.gz
4.0K mingongge1.txt.gz
4.0K mingongge2.txt.gz
4.0K mingongge.txt.md5.gz
4.0K sort.cut.txt.gz
[root@centos7 testdir]# du -a
4 ./cest.txt.gz
4 ./cuttest.txt.gz
4 ./mingongge1.txt.gz
4 ./mingongge2.txt.gz
4 ./mingongge.txt.md5.gz
4 ./sort.cut.txt.gz
0 ./dir
0 ./file
4 ./mingongge.zip
28 .

显示指定文件所占的磁盘空间

[root@centos7 ~]# du test.txt
4 test.txt
[root@centos7 ~]# du -h test.txt
4.0K test.txt

查看大目录

[root@centos7 ~]# du -h --max-depth=1
0 ./.pki
38M ./goinception
46M ./httpd-2.4.46
28K ./testdir
114M .

[root@centos7 ~]# du -h --max-depth=1 |sort -nr
114M .
46M ./httpd-2.4.46
38M ./goinception
28K ./testdir
0 ./.pki



(47):df

命令简介

df 命令用于显示磁盘的相关信息。df(Disk Free)的首字母组合,用来显示文件系统磁盘空间的使用情况。

默认显示单位为KB。可以使用 df 命令来查看硬盘被占用了多少空间,目前还剩下多少空间。

命令语法

df [OPTION]... [FILE]...

选项说明

-a  #包括所有的文件系统
--block-size=<区块大小>  #以指定的区块大小来显示区块数目
-h  #以可读的方式显示信息
-i  #查看inode的信息
-k  #指定区块大小为1024字节
-l  #仅显示本地端的文件系统
-m  #指定区块大小为1048576字节
--no-sync  #在获取磁盘使用信息前,不要执行sync指令
-P  #使用POSIX的输出格式
--sync  #在获取磁盘使用信息前,先执行sync指令
-t<文件系统类型>或--type=<文件系统类型>  #仅显示指定文件系统类型的磁盘信息
-T  #查看文件系统的类型
--help  #打印帮助信息
--version  #打印版本信息

应用举例

查看整个文件系统磁盘空间使用情况

[root@centos7 ~]# df
Filesystem              1K-blocks    Used Available Use% Mounted on
devtmpfs                   486068       0    486068   0% /dev
tmpfs                      497840       0    497840   0% /dev/shm
tmpfs                      497840    7784    490056   2% /run
tmpfs                      497840       0    497840   0% /sys/fs/cgroup
/dev/mapper/centos-root  17811456 1875036  15936420  11% /
/dev/sda1                 1038336  170064    868272  17% /boot
tmpfs                       99572       0     99572   0% /run/user/0

Filesystem  #Linux 系统中的文件系统
1K-blocks   #文件系统的大小,用 1K 大小的块来表示
Used        #以1K大小的块所表示的已使用数量
Available   #以1K大小的块所表示的可用空间的数量
Use%        #文件系统中已使用的百分比
Mounted on  #文件系统的挂载点

以更可读的方式查看文件系统磁盘空间使用情况

[root@centos7 ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 475M     0  475M   0% /dev
tmpfs                    487M     0  487M   0% /dev/shm
tmpfs                    487M  7.7M  479M   2% /run
tmpfs                    487M     0  487M   0% /sys/fs/cgroup
/dev/mapper/centos-root   17G  1.8G   16G  11% /
/dev/sda1               1014M  167M  848M  17% /boot
tmpfs                     98M     0   98M   0% /run/user/0

以MB为单位显示

[root@centos7 ~]# df -m
Filesystem              1M-blocks  Used Available Use% Mounted on
devtmpfs                      475     0       475   0% /dev
tmpfs                         487     0       487   0% /dev/shm
tmpfs                         487     8       479   2% /run
tmpfs                         487     0       487   0% /sys/fs/cgroup
/dev/mapper/centos-root     17394  1832     15563  11% /
/dev/sda1                    1014   167       848  17% /boot
tmpfs                          98     0        98   0% /run/user/0

显示inode的信息

[root@centos7 ~]# df -i
Filesystem               Inodes IUsed   IFree IUse% Mounted on
devtmpfs                 121517   385  121132    1% /dev
tmpfs                    124460     1  124459    1% /dev/shm
tmpfs                    124460   726  123734    1% /run
tmpfs                    124460    16  124444    1% /sys/fs/cgroup
/dev/mapper/centos-root 8910848 50320 8860528    1% /
/dev/sda1                524288   332  523956    1% /boot
tmpfs                    124460     1  124459    1% /run/user/0

显示文件系统类型

[root@centos7 ~]# df -T
Filesystem              Type     1K-blocks    Used Available Use% Mounted on
devtmpfs                devtmpfs    486068       0    486068   0% /dev
tmpfs                   tmpfs       497840       0    497840   0% /dev/shm
tmpfs                   tmpfs       497840    7784    490056   2% /run
tmpfs                   tmpfs       497840       0    497840   0% /sys/fs/cgroup
/dev/mapper/centos-root xfs       17811456 1875036  15936420  11% /
/dev/sda1               xfs        1038336  170064    868272  17% /boot
tmpfs                   tmpfs        99572       0     99572   0% /run/user/0

显示指定文件系统类型的信息

[root@centos7 ~]# df -t xfs
Filesystem              1K-blocks    Used Available Use% Mounted on
/dev/mapper/centos-root  17811456 1875036  15936420  11% /
/dev/sda1                 1038336  170064    868272  17% /boot

排除指定文件系统,再显示其它文件系统的信息

[root@centos7 ~]# df -x xfs
Filesystem     1K-blocks  Used Available Use% Mounted on
devtmpfs          486068     0    486068   0% /dev
tmpfs             497840     0    497840   0% /dev/shm
tmpfs             497840  7784    490056   2% /run
tmpfs             497840     0    497840   0% /sys/fs/cgroup
tmpfs              99572     0     99572   0% /run/user/0



(48):top

命令简介

top 命令用于实时显示系统资源使用情况。它可以显示系统摘要信息,以及内核当前正在管理的进程或线程的列表。

top 命令可以实时动态地查看系统的整体运行情况,是一个非常实用的系统性能和运行信息的监测工具。通过 top 命令所提供的互动式界面,用热键可以管理。

语法格式

top [options]

选项说明

-b  #以批处理模式操作
-c  #显示完整的治命令
-d  #屏幕刷新间隔时间
-I  #忽略失效过程
-s  #保密模式
-S  #累积模式
-i<时间>  #设置间隔时间
-u<用户名>  #指定用户名
-p<进程号>  #指定进程
-n<次数>  #循环显示的次数

top交互命令

在 top 命令执行过程中会使用到一些交互命令,这些命令都是单字母,如下。

h  #显示帮助信息界面
k  #终止一个进程
i  #忽略闲置和僵死进程,这是一个开关式命令
q  #退出程序
r  #重新安排一个进程的优先级别
S  #切换到累计模式
s  #改变两次刷新之间的延迟时间(单位为s),默认值是5s
f或者F  #从当前显示中添加或者删除项目
o或者O  #改变显示项目的顺序
l  #切换显示平均负载和启动时间信息
m  #切换显示内存信息
t  #切换显示进程和CPU状态信息
c  #切换显示命令名称和完整命令行
M  #根据驻留内存大小进行排序
P  #根据CPU使用百分比大小进行排序
T  #根据时间/累计时间进行排序
w  #将当前设置写入~/.toprc文件中

应用举例

查看系统整体运行信息

[root@centos7 ~]# top
top - 05:59:56 up 1 day,  7:13,  1 user,  load average: 0.06, 0.03, 0.05
Tasks:  92 total,   2 running,  90 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.3 sy,  0.0 ni, 99.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :   995684 total,   473120 free,   124960 used,   397604 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.   698120 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                                                                               
   860 root      20   0  574304  17416   6112 S  0.3  1.7   1:01.09 tuned                                                                                                 
  2250 root      20   0  161536   6112   4720 S  0.3  0.6   0:02.82 sshd                                                                                                  
 18762 root      20   0       0      0      0 S  0.3  0.0   0:02.76 kworker/0:2                                                                                           
 21244 root      20   0  161996   2188   1552 R  0.3  0.2   0:00.19 top                                                                                                   
     1 root      20   0  125372   3840   2564 S  0.0  0.4   0:23.04 systemd                                                                                               
     2 root      20   0       0      0      0 S  0.0  0.0   0:00.03 kthreadd                                                                                              
     4 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kworker/0:0H                                                                                          
     6 root      20   0       0      0      0 S  0.0  0.0   0:17.53 ksoftirqd/0                                                                                           
     7 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 migration/0                                                                                           
     8 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcu_bh                                                                                                
     9 root      20   0       0      0      0 S  0.0  0.0   0:12.19 rcu_sched                                                                                             
    10 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 lru-add-drain                                                                                         
    11 root      rt   0       0      0      0 S  0.0  0.0   0:07.75 watchdog/0                                                                                            
    13 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kdevtmpfs                                                                                             
    14 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 netns                                                                                                 
    15 root      20   0       0      0      0 S  0.0  0.0   0:00.19 khungtaskd                                                                                            
    16 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 writeback                                                                                             
    17 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kintegrityd

上述结果信息详细说明

top - 05:59:56  #当前系统时间
up 1 day    #系统已经运行了1天
1 user    #当前登录用户个数 
load average: 0.06, 0.03, 0.05   #系统负载信息

Tasks:  
92 total  #总进程数 
2 running #正在运行的进程数
90 sleeping   #休眠的进程数
0 stopped     #停止的进程数
0 zombie      #冻结的进程数

%Cpu(s):  
0.0 us  #用户空间占用CPU的百分比 
0.3 sy  #内核空间占用CPU的百分比
0.0 ni  #用户进程空间内改变过优先级的进程占用CPU百分比
99.7 id #空闲CPU百分比
0.0 wa  #等待输入输出的CPU时间百分比
0.0 hi  #硬中断占用CPU的百分比 
0.0 si  #软中断占用CPU的百分比
0.0 st  #虚拟机占用百分比 

KiB Mem :   
995684 total  #物理内存的总量
473120 free   #剩余内存的总量
124960 used   #已使用内存的总量
397604 buff/cache  #内核缓存所使用内存的量

KiB Swap:  
2097148 total   #交换分区的总量
2097148 free    #交换分区剩余的总量    
0 used          #交换分区已使用的总量
698120 avail Mem  #可用内存总量

PID  #进程id
USER #进程所有者
PR  #任务的调度优先级,范围0-31,数值越低,优先级越高
NI  #nice值,范围-20到+19,用于调整进程优先级
VIRT #进程所使用的虚拟内存总量(单位 KB)
RES  #任务已使用的未交换物理内存(单位 KB)
SHR  #共享内存大小(单位 KB)
S    #进程状态
      ' D '=不间断的睡眠
      ' R '=运行
      ' S '=睡眠
      ' T '=被跟踪或停止的
      ' Z '=僵尸
%CPU #CPU的使用率
%MEM #内存使用率
TIME+ #CPU时间
COMMAND #进程名称(命令名/命令行),显示用于启动任务的命令行或关联程序的名称。



(49):free

命令简介

free 命令用于显示内存的使用情况,显示可用和已用物理内存和交换内存的总数,以及内核使用的缓冲区。

语法格式

free [options]

选项说明

-b #以Byte为单位显示内存使用情况
-k #以KB为单位显示内存使用情况
-m #以MB为单位显示内存使用情况
-g #以GB为单位显示内存使用情况
-o #不显示缓冲区调节列
-s<间隔秒数> #持续观察内存使用状况
-t #显示内存总和列
-V #显示版本信息

应用举例

查看内存使用的总和

[root@centos7 ~]# free -t
              total        used        free      shared  buff/cache   available
Mem:         995684      124784      473344        7784      397556      698328
Swap:       2097148           0     2097148
Total:      3092832      124784     2570492

周期性打印出内存使用信息

[root@centos7 ~]# free -s 10
              total        used        free      shared  buff/cache   available
Mem:         995684      124780      473344        7784      397560      698332
Swap:       2097148           0     2097148

              total        used        free      shared  buff/cache   available
Mem:         995684      124780      473344        7784      397560      698332
Swap:       2097148           0     2097148

              total        used        free      shared  buff/cache   available
Mem:         995684      124780      473344        7784      397560      698332
Swap:       2097148           0     2097148

              total        used        free      shared  buff/cache   available
Mem:         995684      124780      473344        7784      397560      698332
Swap:       2097148           0     2097148

显示内存使用情况

[root@centos7 ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:            972         121         462           7         388         682
Swap:          2047           0        2047

结果信息详细说明如下:
total #内存总和
used  #已经使用的内存
free  #空闲的内存
shared  #当前已经废弃不用
Buffer/cache  #缓存内存数
available     #实际可用内存数

Linux 在内存管理方面做的非常的好,系统在运行一段时间后,会将暂时用不到的内存转为 buff/cache,这样在程序使用到这一部分数据时,能够很快的取出,从而提高系统的运行效率。如果你看到服务器内存剩余的非常少,不必担心,真正剩余的内存是 free+buff/cache的和才是实际可用的内存。如果应用程序需要内存空间时,Linux 会将缓存让出给程序使用,从而使内存达到最大化的利用率。

当大量的缓存占用内存空间时,应用程序就会使用到sawp交换分区,这样会使得系统的运行变慢,从而影响整体运行效率。

所以,这个时候我们需要手动去释放内存,释放内存的时候,首先执行命令 sync 将所有正在内存中的缓冲区写到磁盘中,其中包括已经修改的文件 inode、已延迟的块 I/O 以及读写映射文件,从而确保文件系统的完整性。

sync
 
echo 1 > /proc/sys/vm/drop_caches
 
echo 0 > /proc/sys/vm/drop_caches

Swap 是指交换分区,当可用内存少于额定值的时候,就会开会进行交换。额定值信息如下

[root@centos7 ~]# cat /proc/meminfo
MemTotal:         995684 kB
MemFree:          473460 kB
MemAvailable:     698452 kB
Buffers:            2108 kB
Cached:           321280 kB
SwapCached:            0 kB
Active:           229996 kB
Inactive:         132264 kB
Active(anon):      39304 kB
Inactive(anon):     7352 kB
Active(file):     190692 kB
Inactive(file):   124912 kB
Unevictable:           0 kB
Mlocked:               0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Dirty:                 0 kB
Writeback:             0 kB
AnonPages:         38904 kB
Mapped:            20988 kB
Shmem:              7784 kB
Slab:              99432 kB
SReclaimable:      74176 kB
SUnreclaim:        25256 kB
KernelStack:        3728 kB
PageTables:         3280 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:     2594988 kB
Committed_AS:     230116 kB
VmallocTotal:   34359738367 kB
VmallocUsed:      177420 kB
VmallocChunk:   34359310332 kB
Percpu:            22528 kB
HardwareCorrupted:     0 kB
AnonHugePages:      8192 kB
CmaTotal:              0 kB
CmaFree:               0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
DirectMap4k:       69504 kB
DirectMap2M:      978944 kB
DirectMap1G:           0 kB



(50):date

命令简介

date 命令用于显示与设置系统时间。

语法格式

date [OPTION] [MMDDhhmm[[CC]YY][.ss]]

选项说明

-d<字符串>  #显示字符串所指的日期与时间。字符串前后必须加上双引号
-s<字符串>  #根据字符串来设置日期与时间。字符串前后必须加上双引号
-u  #显示GMT
--help     #打印在线帮助信息
--version  #打印版本信息

时间字符串列表

FORMAT controls the output.  Interpreted sequences are:
%% #字符%
%H #小时,24小时制(表示方式:00~23)
%I #小时,12小时制(表示方式:01~12)
%k #小时,24小时制(表示方式:0~23)
%l #小时,12小时制(表示方式:1~12)
%M #分(00~59)
%p #显示出上午或下午
%r #时间,12小时制
%s #从1970年1月1日0点到目前经历的秒数
%S #秒(00~59) 
%T #时间(24小时制)(hh:mm:ss)
%X #显示时间的格式(%H时%M分%S秒)
%Z #按字母表排序的时区缩写
%a #星期名缩写
%A #星期名全称
%b #月名缩写
%B #月名全称
%c #日期和时间
%d #按月计的日期(01~31)
%D #日期(mm/dd/yy) 
%h #和%b选项相同
%j #一年的第几天(001~366)
%m #月份(01~12)
%w #一个星期的第几天(0代表星期天)
%W #一年的第几个星期(00~53,星期一为第一天)
%x #显示日期的格式(mm/dd/yy)
%y #年份的最后两个数字(1999则是99)
%Y #年份(比如1970、1996等)
%C #世纪,通常为省略当前年份的后两位数字
%U #一年中的第几周,以周日为每星期第一天
%e #按月计的日期,添加空格,等于%_d

应用举例

格式化输出当前时间信息

[root@centos7 ~]# date +"%Y-%m-%d"
2021-01-17

打印昨天(1天前)日期

[root@centos7 ~]# date -d "1 day ago"
Sat Jan 16 07:31:22 EST 2021
[root@centos7 ~]# date -d "1 day ago" +"%Y-%m-%d"
2021-01-16

时间的加减

#当前时间
[root@centos7 ~]# date +"%Y-%m-%d %H:%M:%S"
2021-01-17 07:38:17

#下一天(明天)的时间
[root@centos7 ~]# date -d "+1 day" +"%Y-%m-%d %H:%M:%S"
2021-01-18 07:38:50
#前一天(昨天)的时间
[root@centos7 ~]# date -d "-1 day" +"%Y-%m-%d %H:%M:%S"
2021-01-16 07:38:56

#后一个月(下个月)的时间
[root@centos7 ~]# date -d "+1 month" +"%Y-%m-%d %H:%M:%S"
2021-02-17 07:39:21
#前一个月(上个月)的时间
[root@centos7 ~]# date -d "-1 month" +"%Y-%m-%d %H:%M:%S"
2020-12-17 07:39:35

#后一周的时间
[root@centos7 ~]# date -d "+1 week" +"%Y-%m-%d %H:%M:%S"
2021-01-24 07:39:52
#前一前的时间
[root@centos7 ~]# date -d "-1 week" +"%Y-%m-%d %H:%M:%S"
2021-01-10 07:39:59

#下一年(明年)的时间
[root@centos7 ~]# date -d "+1 year" +"%Y-%m-%d %H:%M:%S"
2022-01-17 07:40:11
#前一年(去年)的时间
[root@centos7 ~]# date -d "-1 year" +"%Y-%m-%d %H:%M:%S"
2020-01-17 07:40:19

设置时间

date -s      #设置当前时间,只有root权限才能设置
date -s 20201111   #设置成20201111,这样会把具体时间设置成空00:00:00
date -s 00:00:00   #设置具体时间,日期不会被更改

#设置全部时间
date -s "00:00:00 2020-11-11"  
date -s "01:01:01 20201111"    
date -s "2020-11-11 00:00:00"   
date -s "20201111 00:00:00"   

时间的转换

[root@centos7 ~]# date -d "2021-01-01 1234567890 seconds" +"%Y-%m-%d %H:%m:%S"
2060-02-14 23:02:30
[root@centos7 ~]# date -d "2021-01-01" +"%Y-%m-%d %H:%m:%S"
2021-01-01 00:01:00
[root@centos7 ~]# date -d "2021-01-01 7890 seconds" +"%Y-%m-%d %H:%m:%S"
2021-01-01 02:01:30



(51):which

命令简介

which 命令用于查找并显示指定的命令的绝对路径信息,按环境变量PATH路径查找。使用 which 命令,也可以看到某个系统命令是否存在。

语法格式

which [options] [--] programname [...]

选项说明

-a  #打印每个匹配文件名的所有匹配路径名
-V  #打印版本信息

which 退出状态

0 #找到了所有文件名,所有文件都是可执行的
1 #找不到一个或多个文件名,或者文件名不可执行
2 #指定的选项无效

应用举例

显示命令的完整路径

[root@centos7 ~]# which which
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
 /usr/bin/alias
 /usr/bin/which
[root@centos7 ~]# which ls
alias ls='ls --color=auto'
 /usr/bin/ls
[root@centos7 ~]# which pwd
/usr/bin/pwd
[root@centos7 ~]# which rz
/usr/bin/rz
[root@centos7 ~]# which ifconfig
/usr/sbin/ifconfig



(52):whereis

命令简介

whereis 命令查找二进制程序、代码等相关文件路径。

但是,和 find 相比,whereis 查找的速度非常快,因为,Linux 会将系统里的所有文件统一记录在一个数据库文件中,当用户使用 whereis 命令时,它就会直接从这个数据库文件中去查找。而find命令则是遍历硬盘来进行查找,故而效率比不上 whereis 命令。

语法格式

whereis [ OPTIONS ] file name...

选项说明

-b  #只查找二进制文件
-B<目录>  #只在指定的目录下查找二进制文件
-f  #不显示文件名前的路径名称
-m  #只查找说明文件
-M<目录>  #只在指定的目录下查找说明文件
-s  #只查找原始代码文件
-S<目录>  #只在指定的目录下查找原始代码文件
-u  #查找不包含指定类型的文件

应用举例

#将相关的所有文件都查找出来
[root@centos7 ~]# whereis ifconfig
ifconfig: /usr/sbin/ifconfig /usr/share/man/man8/ifconfig.8.gz
[root@centos7 ~]# whereis top
top: /usr/bin/top /usr/share/man/man1/top.1.gz

#只将二进制文件查找出来
[root@centos7 ~]# whereis -b ifconfig
ifconfig: /usr/sbin/ifconfig
[root@centos7 ~]# whereis -b top
top: /usr/bin/top



(53):who

命令简介

who 命令用于显示目前登录系统的用户信息。

who 命令用于显示当前系统登录的所有用户的信息,可以查看目前有那些用户登录到系统等信息。单独执行 who 命令时可以打印出登录的帐号,用户所使用的终端,登录时间以及从何处登录或正在使用哪些显示器等信息。

语法格式

who [ OPTION ]... [ FILE ] [ am i ]

选项说明

-a  #与使用选项-b -d --login -p -r -t -T -u相同。
-b  #显示上次系统引导的时间
-d  #显示无效进程
-H  #显示各栏位的标题信息列
-i  #显示闲置时间
-m  #此参数和指定"am i"字符串相同
-q  #只显示登入系统的帐号名称和总人数
-w  #显示用户的信息状态栏
--help  #打印在线帮助
--version  #显示版本信息

应用举例

[root@centos7 ~]# who
root     pts/0        2021-01-17 08:26 (192.168.1.93)
root     pts/1        2021-01-17 08:32 (192.168.1.93)
[root@centos7 ~]# who -b
         system boot  2021-01-14 05:41
[root@centos7 ~]# who -d
[root@centos7 ~]# who -m
root     pts/1        2021-01-17 08:32 (192.168.1.93)
[root@centos7 ~]# who -q
root root
# users=2
[root@centos7 ~]# who -aH
NAME       LINE         TIME             IDLE          PID COMMENT  EXIT
           system boot  2021-01-14 05:41
LOGIN      tty1         2021-01-14 05:42               630 id=tty1
           run-level 3  2021-01-14 05:42
root     + pts/0        2021-01-17 08:26 00:06       28432 (192.168.1.93)
root     + pts/1        2021-01-17 08:32   .         28737 (192.168.1.93)



(54):w

命令简介

w 命令是一个非常常用的命令,用来查看登录者的信息及他们的行为动作、系统负载等信息。

语法格式

w [OPTIONS][用户名称]

选项说明

-f  #开启或关闭显示用户从何处登录到系统
-h  #不显示各栏位的标题信息列
-l  #使用详细格式列表(默认)
-s  #使用简洁格式列表
-u  #忽略执行程序的名称,以及该程序耗费CPU时间的信息
-V  #打印版本信息

应用举例

#显示当前用户及系统负载信息
[root@centos7 ~]# w
 08:48:55 up 1 day, 10:02,  2 users,  load average: 0.09, 0.04, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.1.93     08:26   20:15   0.04s  0.04s -bash
root     pts/1    192.168.1.93     08:32    7.00s  0.25s  0.18s w

#不显示用户登录位置信息
[root@centos7 ~]# w -f
 08:48:58 up 1 day, 10:02,  2 users,  load average: 0.08, 0.04, 0.05
USER     TTY        LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0     08:26   20:18   0.04s  0.04s -bash
root     pts/1     08:32    2.00s  0.08s  0.01s w -f

#以精简格式显示
[root@centos7 ~]# w -s
 08:49:12 up 1 day, 10:03,  2 users,  load average: 0.06, 0.04, 0.05
USER     TTY      FROM              IDLE WHAT
root     pts/0    192.168.1.93     20:32  -bash
root     pts/1    192.168.1.93      0.00s w -s

#不显示列的标题信息
[root@centos7 ~]# w -h
root     pts/0    192.168.1.93     08:26   20:36   0.04s  0.04s -bash
root     pts/1    192.168.1.93     08:32    4.00s  0.07s  0.00s w -h



(55):id

命令简介

id 命令用于查看用户的 UID\GID 及所属的用户组等信息。

需要注意的就是:UID在系统中是唯一的,也是用户的身份标识,但GID可以对应多个UID(简单理解就是一个用户可以加入多个用户组)。

语法格式

id [OPTIONS][用户名称]

选项说明

-g  #显示用户所属群组的ID
-G   #显示用户所属附加群组的ID
-n  #显示用户,所属群组或附加群组的名称
-r  #显示实际ID
-u  #显示用户ID
-Z   #仅打印当前用户的安全上下文
-help   #打印帮助信息
-version      #打印版本信息

应用举例

[root@centos7 ~]# id
uid=0(root) gid=0(root) groups=0(root)
#root 的 UID = 0,GID = 0

[root@centos7 ~]# id -a
uid=0(root) gid=0(root) groups=0(root)
[root@centos7 ~]# id -g
0
[root@centos7 ~]# id -G
0
[root@centos7 ~]# id mingongge
uid=1000(mingongge) gid=1000(mingongge) groups=1000(mingongge)



(56):su/sudo

命令简介

su 命令用于切换当前用户身份到其他用户身份。

sudo 命令用于切换用户执行权限,这个命令可使其它非 root 用户具有 root 权限。默认情况下,sudo 要求用户使用密码进行身份验证,这是用户密码,不是 root 密码。

语法格式

su [ options ] [ username ]
sudo [ options ] [ command ]

选项说明

su 命令选项

-c<指令>  #执行完指定的指令后,即恢复原来的身份
-f  #使shell不用去读取启动文件
-l  #切换身份时,同时变更工作目录
-m  #切换身份时,不变更环境变量
-s  #指定要执行的shell
--help     #打印帮助信息
--version  #打印版本信息

sudo 命令选项

-b  #在后台执行指令
-h  #打印帮助信息
-H  #将HOME环境变量设为新身份的HOME环境变量
-k  #结束密码的有效期限,也就是下次再执行sudo时便需要输入密码
-l  #列出目前用户可执行与无法执行的指令
-s<shell>  #执行指定的shell
-u<用户>   #以指定的用户作为新的身份
-v  #延长密码有效期限5分钟
-V  #打印版本信息

应用举例

#切换用户到 mingongge
[root@centos7 ~]# su - mingongge
Last login: Sun Jan 17 08:08:46 EST 2021 on pts/0

#切换到root用户后执行pwd命令后再切换至原用户
[mingongge@centos7 ~]$ su -c pwd root
Password: 
/home/mingongge
[mingongge@centos7 ~]$ sudo -i
[sudo] password for mingongge: 
mingongge is not in the sudoers file.  This incident will be reported.
#普通用户如果没有在/etc/sudoers文件里配置相关的信息,则无法执行sudo这个命令
[mingongge@centos7 ~]$ sudo -l
[sudo] password for mingongge: 
Sorry, user mingongge may not run sudo on centos7.

用户需要执行 sudo 命令时就需要在/etc/sudoers配置文件中配置,然后直接使用sudo + 需要执行的命令 这种组合来让自己具有管理员权限。




(57):cal

命令简介

cal 命令用于显示当前日历或指定日期的日历。

语法格式

cal [month] [year] [-m month] [-y year] [-h] [-3] [-1] [-A num] [-B num] [-d YYYY-MM] [-j] [-N]

选项说明

-l  #显示单月输出
-3  #显示临近三个月的日历
-s  #将星期日作为月的第一天
-m  #将星期一作为月的第一天
-j  #显示“julian”日期
-y  #显示当前年的日历

应用举例

[root@centos7 ~]# cal
    January 2021    
Su Mo Tu We Th Fr Sa
                1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
[root@centos7 ~]# cal -s
    January 2021    
Su Mo Tu We Th Fr Sa
                1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
[root@centos7 ~]# cal -m
    January 2021    
Mo Tu We Th Fr Sa Su
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
[root@centos7 ~]# cal -3
    December 2020         January 2021          February 2021   
Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa
       1  2  3  4  5                  1  2      1  2  3  4  5  6
 6  7  8  9 10 11 12   3  4  5  6  7  8  9   7  8  9 10 11 12 13
13 14 15 16 17 18 19  10 11 12 13 14 15 16  14 15 16 17 18 19 20
20 21 22 23 24 25 26  17 18 19 20 21 22 23  21 22 23 24 25 26 27
27 28 29 30 31        24 25 26 27 28 29 30  28                  
                      31                                        
[root@centos7 ~]# cal -y
                               2021                               

       January               February                 March       
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                1  2       1  2  3  4  5  6       1  2  3  4  5  6
 3  4  5  6  7  8  9    7  8  9 10 11 12 13    7  8  9 10 11 12 13
10 11 12 13 14 15 16   14 15 16 17 18 19 20   14 15 16 17 18 19 20
17 18 19 20 21 22 23   21 22 23 24 25 26 27   21 22 23 24 25 26 27
24 25 26 27 28 29 30   28                     28 29 30 31
31
        April                   May                   June        
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
             1  2  3                      1          1  2  3  4  5
 4  5  6  7  8  9 10    2  3  4  5  6  7  8    6  7  8  9 10 11 12
11 12 13 14 15 16 17    9 10 11 12 13 14 15   13 14 15 16 17 18 19
18 19 20 21 22 23 24   16 17 18 19 20 21 22   20 21 22 23 24 25 26
25 26 27 28 29 30      23 24 25 26 27 28 29   27 28 29 30
                       30 31
        July                  August                September     
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
             1  2  3    1  2  3  4  5  6  7             1  2  3  4
 4  5  6  7  8  9 10    8  9 10 11 12 13 14    5  6  7  8  9 10 11
11 12 13 14 15 16 17   15 16 17 18 19 20 21   12 13 14 15 16 17 18
18 19 20 21 22 23 24   22 23 24 25 26 27 28   19 20 21 22 23 24 25
25 26 27 28 29 30 31   29 30 31               26 27 28 29 30

       October               November               December      
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                1  2       1  2  3  4  5  6             1  2  3  4
 3  4  5  6  7  8  9    7  8  9 10 11 12 13    5  6  7  8  9 10 11
10 11 12 13 14 15 16   14 15 16 17 18 19 20   12 13 14 15 16 17 18
17 18 19 20 21 22 23   21 22 23 24 25 26 27   19 20 21 22 23 24 25
24 25 26 27 28 29 30   28 29 30               26 27 28 29 30 31
31



(58):telnet

命令简介

telnet 命令用于使用 TELNET 协议与另一个主机进行交互通信。可以对另一主机进行远程登录、管理操作,同样也可以通过telnet来确认远程主机的某个端口是否开放?也是日常网络故障排错的重要一部分。

语法格式

telnet [OPTIONS] [host [port]
[host]远程主机 #指定要登录进行管理的远程主机;
[port]端口    #指定TELNET协议使用的端口号。

选项说明

-4  #强制IPv4地址解析
-6  #强制进行IPv6地址解析
-a  #尝试自动登录远端主机系统
-b<主机别名>  #使用指定远端主机名称
-c  #不读取用户专属目录里的.telnetrc文件
-d  #启动排错模式
-e<脱离字符>  #设置脱离字符
-E  #滤除脱离字符
-K  #不自动登录远端主机
-l<用户名称>  #指定要登录远端主机的用户名称
-L  #允许输出8位字符资料
-n<记录文件>  #指定文件记录相关信息
-x  #假设主机有支持数据加密的功能就用它
-X<认证形态>  #关闭指定的认证形态

telnet 服务配置

telnet 服务配置如下:

#通常参数配置,如下:
service telnet
{
    disable = no #启用
    flags = REUSE #socket可重用
    socket_type = stream #连接方式为TCP
    wait = no #为每个请求启动一个进程
    user = root #启动服务的用户为root
    server = /usr/sbin/in.telnetd #要激活的进程
    log_on_failure += USERID #登录失败时记录登录用户名
}
 
#配置允许登录的客户端列表
only_from = 10.0.0.2 #只允许10.0.0.2登录
 
#配置禁止登录的客户端列表
no_access = 10.0.0.{2,3,4}  #禁止10.08.0.2、10.0.0.3、10.0.0.4登录
 
#设置开放时段
access_times = 9:00-12:00 13:00-17:00 # 每天只有这两个时段开放服务

#配置用户只从某个地址登录telnet服务
bind = 10.0.0.2

应用举例

尝试打开与远程主机 baidu.com 的连接

[root@centos7 ~]# telnet www.baidu.com
Trying 36.152.44.96...

尝试使用登录名 mingongge 在端口 9999 上打开到远程主机 mingongget.com 的连接。如果连接成功,将会提示输入 mingongge 的密码

[root@centos7 ~]# telnet -l mingongge mingongge.com 9999
Trying 104.164.133.141...

查看某个端口是否开放

[root@centos7 ~]# telnet 127.0.0.1 22
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
SSH-2.0-OpenSSH_7.4
#出现上述信息则说明22端口已开放

#出现如下提示则说明相应的端口没有开放
[root@centos7 ~]# telnet 127.0.0.1 80
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused
[root@centos7 ~]# telnet 127.0.0.1 9999
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused



(59):ssh

命令简介

ssh ( Secure Shell )命令是用于安全登录到远程系统的协议,它可用于在远程服务器上记录或执行命令。

ssh(SSH 客户端)是用于登录到远程计算机并在远程计算机上执行命令的程序。可以在不安全的网络中于两个不受信任的主机之间提供安全的加密通信。

语法格式

ssh [OPTIONS] [-p PORT] [USER@]HOSTNAME [COMMAND]

选项说明

-4 #强制ssh协议只使用IPv4地址
-6 #强制ssh协议只使用IPv6地址
-A #启用来自身份验证代理的连接转发
-a  #禁用身份验证代理连接的转发
-B bind_interface  #绑定到的地址 bind_interface在尝试连接到目标主机之前
-b bind_address    #使用 bind_address在本地计算机上作为连接的源地址
-C  #请求压缩所有数据
-c cipher_spec  #指定用于加密会话的密码规范
-D [bind_address:] 端口  #指定本地“动态”应用程序级端口转发
-E log_file   #将调试日志附加到 log_file 而非标准错误
-e escape_char  #设置带有pty的会话的转义字符(默认值:' ~')
-F 配置文件  #指定每用户ssh的配置文件 
-f  #配置ssh在执行命令之前将请求转到后台 
-g  #允许远程主机连接到本地转发的端口
-i identity_file  #指定从这个文件中去读取用于公共密钥身份验证的标识(私有密钥)
-K  #启用基于GSSAPI的身份验证
-k  #禁用将GSSAPI凭据
-L local_socket:remote_socket  #指定将与本地(客户端)主机上给定的TCP端口或Unix套接字的连接转发到远程的给定主机和端口或Unix套接字。
-N  #禁止执行远程命令 
-p port  #指定SSH连接端口
-q  #静默模式
-s  #用于请求调用远程系统上的子系统
-T  #禁用分配伪终端
-t  #强制分配伪终端
-V  #打印SSH版本号并退出
-v  #详细模式(输出SSH连接的过程信息)
-X  #启用X11转发
-x  #禁用X11转发
-Y  #启用受信任的X11转发
-y  #指定发送日志信息 syslog(3)系统模块

应用举例

连接到远程主机

ssh username@remote_host

#ssh 使用特定身份(私钥)连接到远程主机
ssh -i path/to/key_file username@remote_host

使用特定端口连接到远程主机

ssh username@remote_host -p 9999

用SSH连接到远程服务器上再运行命令

ssh remote_host command
[root@centos7 ~]# ssh 192.168.1.199 ls
The authenticity of host '192.168.1.199 (192.168.1.199)' can't be established.
ECDSA key fingerprint is SHA256:mF2QLxkGH/mWhHu/NlaKOrx4nKkyVvhYV6BRPA8TdEk.
ECDSA key fingerprint is MD5:a1:91:03:6b:9a:91:f6:c3:cf:19:06:32:19:b9:85:8e.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.199' (ECDSA) to the list of known hosts.
root@192.168.1.199's password: 
anaconda-ks.cfg
dos_test.txt
goinception
goInception-linux-amd64-v1.2.3.tar.gz
httpd
httpd-2.4.46
httpd-2.4.46.tar.gz
mingongge.file
mingongge.z01
mingongge.z02
mingongge.zip
testdir
test.txt

查看一下SSH远程登录过程的详细信息

[root@centos7 ~]# ssh -v 192.168.1.199 -p 22
OpenSSH_7.4p1, OpenSSL 1.0.2k-fips  26 Jan 2017
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 58: Applying options for *
debug1: Connecting to 192.168.1.199 [192.168.1.199] port 22.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_rsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.4
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.4
debug1: match: OpenSSH_7.4 pat OpenSSH* compat 0x04000000
debug1: Authenticating to 192.168.1.199:22 as 'root'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: curve25519-sha256 need=64 dh_need=64
debug1: kex: curve25519-sha256 need=64 dh_need=64
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:mF2QLxkGH/mWhHu/NlaKOrx4nKkyVvhYV6BRPA8TdEk
debug1: Host '192.168.1.199' is known and matches the ECDSA host key.
debug1: Found key in /root/.ssh/known_hosts:1
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Next authentication method: gssapi-keyex
debug1: No valid Key exchange context
debug1: Next authentication method: gssapi-with-mic
debug1: Unspecified GSS failure.  Minor code may provide more information
No Kerberos credentials available (default cache: KEYRING:persistent:0)

debug1: Unspecified GSS failure.  Minor code may provide more information
No Kerberos credentials available (default cache: KEYRING:persistent:0)

debug1: Next authentication method: publickey
debug1: Trying private key: /root/.ssh/id_rsa
debug1: Trying private key: /root/.ssh/id_dsa
debug1: Trying private key: /root/.ssh/id_ecdsa
debug1: Trying private key: /root/.ssh/id_ed25519
debug1: Next authentication method: password
root@192.168.1.199's password: 
debug1: Authentication succeeded (password).
Authenticated to 192.168.1.199 ([192.168.1.199]:22).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: pledge: network
debug1: client_input_global_request: rtype hostkeys-00@openssh.com want_reply 0
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
Last login: Sun Jan 17 14:26:28 2021 from 192.168.1.93



 

 

 

 

 

 

 

 

 

 



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值