Linux-基础教程

Linux

基础篇

liunx 概述

liunx 是一个开源、免费的操作系统

linux 主要发行版: Ubuntu、RedHat、CentOS、Debain

官网地址:https://www.kernel.org/

linux 应用领域最多的还是服务器领域

Liunx 和 Unix 的关系

  • Linux 和 Unix 是两种操作系统,但它们之间存在一定的关系。
  • Unix 是最早的操作系统之一,诞生于 1969 年,由 AT&T 贝尔实验室开发。
  • Linux 是一个类 Unix 操作系统,由 Linus Torvalds 在 1991 年创建,旨在为个人计算机提供一个免费的、可自由修改的操作系统内核。
  • Linux 内核与 Unix 的设计哲学非常相似,支持多任务、多用户,具有强大的网络功能和稳定性。
  • 许多 Linux 发行版不仅包含 Linux 内核,还集成了大量的 GNU 工具,这些工具的设计和实现也受到了 Unix 的影响。
  • 因此,可以说 Linux 是 Unix 的精神继承者,尽管它们不是同一个操作系统。
  • Unix 只针对大型主机或服务器

安装 VMware 虚拟机

主机 >>> 虚拟机 >>> 虚拟机内的系统
安装教程:https://blog.csdn.net/weixin_44828720/article/details/144188644

安装 CentOS

下载地址:http://mirrors.aliyun.com/centos/7.9.2009/isos/x86_64/

使用迅雷比较快

安装教程:https://blog.csdn.net/weixin_44828720/article/details/144192974

网络连接的三种模式

  1. 桥接模式,虚拟系统可以和外部系统通讯,但是容易造成 IP 冲突
  2. NAT 模式,网络地址转换模式,虚拟系统可以和外部系统通讯,不造成 IP 冲突
  3. 主机模式,独立的系统

虚拟机克隆

在虚拟机中已经安装好一台操作系统,如果还想要更多的,就克隆就行了

注:克隆的时候, 要先关闭系统

两种方式:

  1. 直接拷贝一份安装好的虚拟机文件
  2. 使用 VMware 的克隆操作

对应虚拟机,右键—> 管理—> 克隆

虚拟机快照

如果你在使用虚拟机系统的时候(比如 linux),你想回到原先的某一个状态,也就是说你担心可能有些误操作

造成系统异常,需要回到原先某个正常运行的状态,Vmware 也提供了这样的功能,就叫快照管理。

1.安装好系统以后,先做一个快照 A

2.进入到系统。创建一个文件夹,再保存一个快照 B

3.回到系统刚刚安装好的状态,即快照 A

4.试试看,是否还能再次回到快照 B

快照管理-----> 选择对应的快照-------> 转到

虚拟机迁移和删除

两种方式:

  1. 直接对虚拟机文件拷贝或者剪切或删除
  2. 使用 VMware 进行移除,选择虚拟机然后移除,然后再去目录删除

安装 vmtools

  1. vmtools 安装后,可以让我们在 windows 下更好的管理 vm 虚拟机
  2. 可以设置 windows 和 centos 的共享文件夹

安装 vmtools 的步骤:

1.进入 centos

2.点击 vm 菜单的-> installvmwaretools

3.centos 会出现一个 vm 的安装包,xx.tar.gz

4.拷贝到/opt

5.使用解压命令 tar,得到一个安装文件

[root@zjm009 ~]# cd /opt
[root@zjm009 opt]# ls
rh  VMwareTools-10.3.25-20206839.tar.gz
[root@zjm009 opt]# tar -zxvf VMwareTools-10.3.25-20206839.tar.gz 

6.进入该 vm 解压的目录,/opt 目录下

[root@zjm009 opt]# cd vmware-tools-distrib/
[root@zjm009 vmware-tools-distrib]# ls
bin  doc  etc  FILES  INSTALL  installer  lib  vgauth  vmware-install.pl
[root@zjm009 vmware-tools-distrib]# 

7.安装/vmware-install.pl

./vmware-install.pl

8.全部使用默认设置即可,就可以安装成功

9.注意:安装 vmtools 需要有 gcc

设置共享文件夹

  • 主机中创建一个共享文件夹
  • 虚拟机右键----> 设置-----> 选项-----> 共享文件夹-------> 文件夹共享:总是启用----> 添加主机创建的共享文件夹
  • 共享文件夹在虚拟机 centOS 系统下:/mnt/hgfs/

开发的时候使用远程登录的方式

Linux 目录结构

Linux 里一切皆文件

Linux 目录结构是一种 树形结构 ,从根目录 / 开始,向下延伸出多个子目录, 每个子目录都有特定的功能和用途 。以下是一些常见的顶级目录及其功能:

  • /: 根目录,整个文件系统的起点。
  • /bin : (常用)存放系统启动和运行基本命令所需的二进制文件。(/usr/bin、/usr/local/bin)
  • /boot : (常用)存放启动 Linux 时使用的静态文件,包括内核和启动加载程序的配置文件。
  • /dev: 存放设备文件,表示系统中的硬件设备。
  • /etc : (常用)存放系统配置文件,如网络配置、服务配置等。
  • /home : (常用)用户主目录的位置,每个用户都有一个自己的目录。一般通过用户名命名
  • /lib: 存放系统启动和运行基本命令所需的库文件。
  • /lost+found:这个目录一般情况下是空的,当系统非法关机后,这里就存放了一些文件
  • /media : (常用)可移动设备(如 U 盘、光盘)挂载点。
  • /mnt : (常用)临时挂载文件系统的挂载点。
  • /opt: 用于安装可选的应用程序包。
  • /proc : (不能动)虚拟文件系统,提供有关系统内核和进程的信息。
  • /root :(常用) 系统管理员(root 用户)的主目录。
  • /run: 运行时变量数据,如进程 ID 文件、锁文件等。
  • /sbin: 存放系统管理员使用的系统管理命令。(/usr/sbin、/usr/local/sbin)
  • /srv : (不能动)服务数据,存放服务相关的数据文件。
  • /sys : (不能动)虚拟文件系统,提供有关系统设备和内核参数的信息。
  • /tmp: 临时文件存储,重启后会被清空。
  • /usr : (常用)用户程序和文件,包含大量用户应用程序和库文件,类似与 windows 的 Program Files。
  • /usr/local : (常用)这是另一个给主机额外安装软件所安装的目录。一般是通过编译源码方式安装的程序
  • /var : (常用)可变数据文件,如日志文件、邮件队列等

实践篇

远程登录 Liunx

远程登录客户端:Xshell7, Xftp7

家庭学校免费版:https://www.xshell.com/zh/free-for-home-school/

查看 IP

[root@zjm009 /]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.18.129  netmask 255.255.255.0  broadcast 192.168.18.255
        inet6 fe80::7c6e:1844:f901:2eb2  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:2e:23:45  txqueuelen 1000  (Ethernet)
        RX packets 555  bytes 255542 (249.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 425  bytes 37851 (36.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 32  bytes 2592 (2.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 32  bytes 2592 (2.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:39:51:7a  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@zjm009 /]# 

自己的电脑使用 ping 命令测试

C:\Users\ZJM>ping 192.168.18.129

正在 Ping 192.168.18.129 具有 32 字节的数据:
来自 192.168.18.129 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.18.129 的回复: 字节=32 时间=2ms TTL=64
来自 192.168.18.129 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.18.129 的回复: 字节=32 时间=1ms TTL=64

192.168.18.129 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
    最短 = 0ms,最长 = 2ms,平均 = 0ms

C:\Users\ZJM>

远程上传下载文件:Xftp7

处理中文乱码:

会话属性------> 选项------------> 编码-----------> Unicode (UTF-8)

vi 和 vim 编辑器

Linux 系统会内置 vi 文本编辑器

Vim 具有程序编辑的能力,可以看做是 Vi 的增强版、具有语法高亮、代码补全、错误跳转等功能

vi 和 vim 常用的三种模式

正常模式
  • 进入方式: 启动 Vi/Vim 后,默认进入命令模式。
  • 用途:用于执行各种命令,如移动光标、删除文本、复制粘贴等。
  • 常用快捷键:
    • h:左移光标
    • j:下移光标
    • k:上移光标
    • l:右移光标
    • 0:移动到行首
    • $:移动到行尾
    • w:向前移动到下一个单词的开头
    • b:向后移动到上一个单词的开头
    • dd:删除当前行,删除当前行向下 5 行 (5yy)
    • dw:删除当前单词
    • yy:复制当前行, 拷贝当前行向下 5 行 (5yy)
    • p:粘贴已复制或删除的文本
    • u:撤销上一次操作
    • Ctrl + r:重做上一次撤销的操作
    • 输入 /关键字,回车查找,输入 n 就是查找下一个
    • G:跳转到文档最末行
    • gg:跳转到文档最首行
    • 跳转到对应行:输入 行数,再输入 shirt+g
插入模式
  • 进入方式:
    • i:在当前光标位置插入文本
    • I:在当前行的行首插入文本
    • a:在当前光标位置的下一个字符处插入文本
    • A:在当前行的行尾插入文本
    • o:在当前行下方插入一个新行
    • O:在当前行上方插入一个新行
  • 用途:用于输入和编辑文本。
  • 退出方式: 按 Esc 键返回正常模式。
命令行模式
  • 进入方式 :在正常模式下按 : 键。
  • 用途:用于执行一些高级命令,如保存文件、退出编辑器、搜索和替换等。
  • 常用命令:
    • :w:保存文件
    • :q:退出编辑器
    • :wq:x:保存并退出
    • :q!:不保存并强制退出
    • :e filename:打开另一个文件
    • :set number:显示行号 , 简写 :set nu
    • :set nonumber:取消显示行号 , 简写 :set nonu
    • :s/old/new/g:替换当前行中的所有 oldnew
    • :%s/old/new/g:替换整个文件中的所有 oldnew

关机、重启命令

1.不管是重启系统还是关闭系统, 首先要运行 sync 命令, 把内存中的数据写到磁盘中

2.目前的 shutdown/reboot/halt 等命令均已经在关机前进行了 sync

  • shutdown -h now :立即关机
  • shutdown -h 1:1 分钟后会关机
  • halt:关机
  • shutdown -r now:立即重启
  • shutdown -r +10:10 分钟后重启
  • reboot:立即重启
  • sync:把内存的数据同步到磁盘

用户登录和注销

切换用户命令

su - root

注销命令

logout

用户管理

Linux 用户管理是系统管理员的一项重要任务,涉及创建、修改、删除用户账户以及管理用户权限。以下是一些常用的 Linux 用户管理命令和操作步骤。

需要使用管理员账号

创建用户

使用 useradd 命令可以创建新用户。默认该用户的家目录在/home/username

useradd username

# 例如
useradd john

用户创建成功后,会自动的创建和用户同名的家目录。

也可以通过 useradd -d 指定目录 新的用户名 给新创建的用户指定家目录

设置用户密码

使用 passwd 命令为新用户设置密码。

passwd 用户名

# 例如
passwd john
[root@zjm009 home]# passwd tom
更改用户 tom 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。

密码输两次

删除用户

使用 userdel 命令可以删除用户。

  • 删除用户但保留主目录

  • userdel username
    
    # 例如
    userdel tom
    
  • 删除用户及其主目录

  • userdel -r username
    
    # 例如
    userdel -r tom
    

一般建议保留主目录

查询用户信息指令

使用 id 命令

id 用户名
[root@zjm009 ~]# id root
uid=0(root) gid=0(root) 组=0(root)
[root@zjm009 ~]# id zjm
uid=1000(zjm) gid=1000(zjm) 组=1000(zjm)
[root@zjm009 ~]# 

当用户不存在时,返回无此用户

切换用户

使用 su 命令

su - username

从权限高的用户切换到权限低用户,不需要输入密码,反之需要。

exit 或者 logout 退出登录

查看当前用户(登录用户)

使用 whoami 命令

whoami

使用 who 命令

who am i

使用 id 命令

id -un

用户组

类似于角色,系统可以对有共性的多个用户进行统一管理

创建用户组
groupadd 组名
删除用户组
groupdel 组名
创建用户时直接加上组
groupadd 组名
useradd -g 组名 用户名
修改用户的组
usermod -g 组名 用户名

用户和组相关文件

/etc/passwd 文件

用户(user)的配置文件,记录用户的各种信息

每行的含义: 用户名:口令:用户标识号:组标识号:注释性描述:主目录:登录 Shell

/etc/shadow 文件

口令的配置文件

每行的含义: 登录名:加密口令:最后一次修改时间:最小时间间隔:最大时间间隔:警告时间:不活动时

间:失效时间:标志

/etc/group 文件

组(group)的配置文件,记录 Linux 包含的组的信息

每行含义: 组名:口令:组标识号:组内用户列表

运行级别

常见的运行级别

  • 0 - 关机
    • 系统关闭,所有服务停止。
  • 1 - 单用户模式
    • 也称为维护模式,通常用于系统维护和修复。只有 root 用户可以登录,没有网络服务。
  • 2 - 多用户模式,无网络文件系统(NFS)
    • 多用户模式,但不支持网络文件系统(NFS)。适用于不需要网络服务的多用户环境。
  • 3 - 完全多用户模式,命令行界面(CLI)
    • 完全多用户模式,支持网络服务,但不启动图形用户界面(GUI)。适用于服务器环境。
  • 4 - 未定义
    • 通常不使用,可以自定义。
  • 5 - 完全多用户模式,图形用户界面(GUI)
    • 完全多用户模式,支持网络服务和图形用户界面(GUI)。适用于桌面环境。
  • 6 - 重启
    • 系统重启,所有服务停止并重新启动。

常用 3 和 5

查看当前运行级别

runlevel

#或者

who -r

更改运行级别

init 3

#或者

telinit 3

找回 root 密码

  1. 重启系统,开机界面按“e”进入编辑界面
  2. 进入编辑界面,光标移动到 Linux16 开头内容所在行数,在行的最后面输入:init =/bin/sh
  3. 输入完成后,直接按快捷键:ctrl+x 进入单用户模式
  4. 在光标闪烁的位置中输入:mount -o remount,rw / ,完成后按键盘回车键
  5. 在新的一行最后面输入:passwd,完成后按键盘的回车。输入密码,然后确认密码
  6. 显示 passwd…的字样,说明密码修改成功
  7. 在光标闪烁的位置中输入:touch /.autorelabel 回车
  8. 在光标闪烁的位置中输入: exec /sbin/init 回车
  9. 等待系统自动修改密码,完成后,系统会自动重启,新密码生效

帮助指令

1.man 命令

man 命令

#例如
man ls
  1. help 命令

    help 命令
    
    #例如
    help ls
    
  2. --help 选项

许多命令都支持 --help 选项,可以显示命令的基本用法和常用选项。

command --help

例如,查看 ls 命令的帮助信息:

ls --help

文件目录指令

  • pwd:显示当前工作目录的绝对路径

  • ls:基本语法: ls [选项] [目录或文件]

    • 常用选项:
    • -a:显示当前目录所有的文件和目录,包括隐藏文件
    • -l:以列表的方式显示信息
  • cd:基本语法:cd [参数](功能描述:切换到指定目录)

    • cd ~ 或者 cd :回到自己的家目录,比如你是 root,cd~到/root
    • cd .. 回到当前目录的上一级目录
  • mkdir:用于创建目录

    • -p:创建多级目录
  • rmdir:删除空目录,如果目录下面有内容时无法删除。

  • rm -rf: 删除非空目录,谨慎使用

  • touch:创建空文件

  • cp:拷贝文件到指定目录

    • 基本语法:cp [选项] source dest
    • 常用选项:-r : 递归复制整个文件夹
    • 强制覆盖不提示的方法:\cp , 如 \cp -r /home/java/ /opt/
  • rm:移除文件或目录

    • 基本语法:rm [选项] 要删除的文件或目录
    • 常用选项:
    • -r:递归删除整个文件夹
    • -f:强制删除不提示
  • mv:移动文件与目录或重命名

    • mv 旧文件名 新文件名:重命名
    • mv /temp/movefile /targetFolder:移动文件
    • mv 源目录 目标目录:移动目录
  • cat:查看文件内容

    • 基本语法:cat [选项] 要查看的文件
    • 常用选项:-n:显示行号
    • 配置管道命令使用:cat -n 文件 | more
  • more:more 指令是一个基于 VI 编辑器的文本过滤器,它以全屏幕的方式按页显示文本文件的内容。more
    指令中内置了若干快捷键(交互的指令)

    • 基本语法:more 要查看的文件
    • 操作说明:
      • 空格键:代表向下翻一页,
      • enter:代表向下翻一行
      • q:离开 more
      • ctrl+F:向下滚动一屏
      • ctrl+B:返回上一屏
      • =:输出当前行的行号
      • :f:输出文件名和当前行的行号
  • less:less 指令用来分屏查看文件内容,它的功能与 more 指令类似,但是比 more 指令更加强大,支持各
    种显示终端。less 指令在显示文件内容时,并不是一次将整个文件加载之后才显示,而是根据显示
    需要加载内容, 对于显示大型文件具有较高的效率 。

    • 基本语法:less 要查看的文件
    • 操作说明:
      • 空白键:向下翻动一页;
      • [pagedown]:向下翻动一页
      • [pageup]:向上翻动一页;
      • /字串:向下搜寻『字串】的功能;n:向下查找;N:向上查找;
      • ?字串:向上搜寻『字串】的功能;n:向上查找;N:向下查找;
      • q:离开 less
  • echo:输出内容到控制台

    • 基本语法:echo [选项] [输出内容]

    • [root@zjm009 /]# echo $PATH
      /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
      [root@zjm009 /]# echo $HOSTNAME
      zjm009
      [root@zjm009 /]# echo "hello,world"
      hello,world
      
  • head:head 用于显示文件的开头部分内容,默认情况下 head 指令显示文件的前 10 行内容

    • 基本语法:
    • head 文件(功能描述:查看文件头 10 行内容)
    • head -n 5 文件(功能描述:查看文件头 5 行内容,5 可以是任意行数)
  • tail:tail 用于输出文件中尾部的内容,默认情况下 tail 指令显示文件的前 10 行内容。

    • 基本语法:
    • tail 文件(功能描述:查看文件尾 10 行内容)
    • tail - n 5 文件(功能描述:查看文件尾 5 行内容,5 可以是任意行数)
    • tail -f 文件(功能描述:实时追踪该文档的所有更新)
  • > 指令和 >> 指令 : > 输出重定向和 >> 追加

    • 基本语法:
    • ls -l > 文件(功能描述:列表的内容写入文件 a.txt 中(覆盖写))
    • ls -al >> 文件(功能描述:列表的内容追加到文件 aa.txt 的末尾)
    • cat 文件1 > 文件2(功能描述:将文件 1 的内容覆盖到文件 2)
    • echo "内容" >> 文件 (追加)
  • ln 指令:软链接也称为符号链接,类似于 windows 里的快捷方式,主要存放了链接其他文件的路径

    • 基本语法:ln -s [原文件或目录] [软链接名](功能描述:给原文件创建一个软链接)

    • 当我们使用 pwd 指令查看目录时,仍然看到的是软链接所在目录。

    • [root@zjm009 home]# ln -s /root/ /home/myroot
      [root@zjm009 home]# ls -l
      总用量 20
      -rw-r--r--.  1 root root     11 12月  6 09:46 hello.txt
      -rw-r--r--.  1 root root    426 12月  6 14:40 info.txt
      drwxr-xr-x.  2 root root   4096 12月  6 13:36 java
      lrwxrwxrwx.  1 root root      6 12月  6 14:47 myroot -> /root/
      drwx------.  3 tom  zjm001 4096 12月  6 09:21 tom
      drwx------. 15 zjm  zjm    4096 12月  4 16:03 zjm
      [root@zjm009 home]# cd myroot/
      [root@zjm009 myroot]# ll
      总用量 48
      -rw-------. 1 root root 1881 12月  3 10:06 anaconda-ks.cfg
      -rw-r--r--. 1 root root  306 12月  4 15:20 Hello.java
      -rw-r--r--. 1 root root 1929 12月  3 10:11 initial-setup-ks.cfg
      -rw-r--r--. 1 root root    0 12月  6 13:50 pig.txt
      -rw-r--r--. 1 root root  106 12月  4 15:04 q
      drwxr-xr-x. 2 root root 4096 12月  3 10:30 公共
      drwxr-xr-x. 2 root root 4096 12月  3 10:30 模板
      drwxr-xr-x. 2 root root 4096 12月  3 10:30 视频
      drwxr-xr-x. 2 root root 4096 12月  3 10:30 图片
      drwxr-xr-x. 2 root root 4096 12月  3 10:30 文档
      drwxr-xr-x. 2 root root 4096 12月  3 10:30 下载
      drwxr-xr-x. 2 root root 4096 12月  3 10:30 音乐
      drwxr-xr-x. 2 root root 4096 12月  3 13:48 桌面
      [root@zjm009 myroot]# 
      
    • 删除软连接:rm /home/myroot

  • history:查看已经执行过历史命令,也可以执行历史指令

  • 基本语法:history(功能描述:查看已经执行过历史命令)

  • 显示所有的历史命令: history

  • 显示最近使用过的 10 个指令:history 10

  • 执行历史编号为 5 的指令: !5

时间日期类

  • date 指令显示当前日期:

    • date(功能描述:显示当前时间)

    • date +%Y(功能描述:显示当前年份)

    • date +%m(功能描述:显示当前月份)

    • date +%d(功能描述:显示当前是哪一天)

    • date "+%Y-%m-%d%H:%M:%S"(功能描述:显示年月日时分秒)

    • [root@zjm009 home]# date
      2024年 12月 06日 星期五 15:00:08 CST
      [root@zjm009 home]# date +%Y
      2024
      [root@zjm009 home]# date +%m
      12
      [root@zjm009 home]# date +%d
      06
      [root@zjm009 home]# date "+%Y-%m-%d %H:%M:%S"
      2024-12-06 15:01:40
      [root@zjm009 home]# 
      
  • date 指令设置日期

    • 基本语法:date -s 字符串时间

    • [root@zjm009 home]# date -s "2022-12-06 11:11:11"
      2022年 12月 06日 星期二 11:11:11 CST	
      
  • cal 指令:

    • cal:查看当前日历
    • cal [年份]:查看指定年份的日历

搜索查找类

  • find:将从指定目录向下递归地遍历其各个子目录,将满足条件的文件或者目录显示在终端。

    • 基本语法:find [搜索范围] [选项]
    • 选项说明:
    • -name 查询方式:按照指定的文件名查找模式查找文件
    • -user 用户名:查找属于指定用户名所有文件
    • -size 文件大小:按照指定的文件大小查找文件,(+n:大于 n,-n:小于 n,n:等于 n,单位 k, M, G)
  • locate:可以快速定位文件路径。locate 指令利用事先建立的系统中所有文件名称及路径的 locate 数

    据库实现快速定位给定的文件。Locate 指令无需遍历整个文件系统,查询速度较快。为了保证查询结果

    的准确度,管理员必须定期更新 locate 时刻。

    • 基本语法:locate 搜索文件
    • 由于 locate 指令基于数据库进行查询,所以第一次运行前,必须使用 updatedb 指令创建 locate 数据库。
  • which:可以查看某个指令在哪个目录下,比如:which ls

  • grep:过滤查找,表示将前一个命令的处理结果输出传递给后面的命令处理。

    • 基本语法:grep [选项] 查找内容 源文件

    • 常用选项:-n 显示匹配行及行号

    • -i 忽略字母大小写

    • [root@zjm009 home]# cat /home/hello.txt | grep "java" 
      hello java
      [root@zjm009 home]# cat /home/hello.txt | grep -n  "java" 
      1:hello java
      [root@zjm009 home]# 
      

压缩和解压类

  • gzip/gunzip:gzip 用于压缩文件,gunzip 用于解压的

    • 基本语法:
    • gzip 文件(功能描述:压缩文件,只能将文件压缩为*.gz 文件)
      gunzip 文件.gz(功能描述:解压缩文件命令)
  • zip/unzip:zip 用于压缩文件,unzip 用于解压的,这个在项目打包发布中很有用的

    • 基本语法:

      • zip [选项] XXX.zip 将要压缩的内容(功能描述:压缩文件和目录的命令)
      • unzip [选项] XXX.zip(功能描述:解压缩文件)
    • zip 常用选项:-r:递归压缩,记压缩目录

    • unzip 的常用选项:-d 目录:指定解压后文件的存放目录

    • [root@zjm009 home]# zip -r myhome.zip /home/
      [root@zjm009 home]# ls
      hello.txt  info.txt  java  myhome.zip  tom  zjm
      [root@zjm009 home]# mkdir /opt/tmp
      [root@zjm009 home]# unzip -d /opt/tmp /home/myhome.zip 
      [root@zjm009 home]# cd /opt/tmp
      [root@zjm009 tmp]# ls
      home
      [root@zjm009 tmp]# cd home
      [root@zjm009 home]# ls
      hello.txt  info.txt  java  tom  zjm
      [root@zjm009 home]# 
      
  • tar:是打包指令,最后打包后的文件是.tar.gz 的文件。

    • 基本语法:tar [选项] XXX.tar.gz 打包的内容(功能描述:打包目录,压缩后的文件格式.tar.gz)

    • 选项说明:

      • -c:产生.tar 打包文件
      • -v:显示详细信息
      • -f:指定压缩后的文件名
      • -z:打包同时压缩
      • -x:解包 tar 文件
    • 压缩多个文件:

    • [root@zjm009 home]# tar -zcvf myhome.tar.gz hello.txt info.txt
      hello.txt
      info.txt
      [root@zjm009 home]# ls
      hello.txt  info.txt  java  myhome.tar.gz  tom  zjm
      [root@zjm009 home]#
      
    • 压缩目录:

    • [root@zjm009 home]# tar -zcvf myhome1.tar.gz /home/
      
    • 解压到当前目录:

    • [root@zjm009 home]# tar -zxvf myhome.tar.gz  
      
    • 解压到指定目录:

    • [root@zjm009 home]# tar -zxvf myhome1.tar.gz -C /opt/tmp2 
      

Linux 组

概念

在 linux 中的每个用户必须属于一个组,不能独立于组外。在 linux 中每个文件有所有者、所在组、其它组的概念。

  • 所有者
  • 所在组
  • 其它组
  • 改变用户所在的组

文件、目录所有者

​ 一般为文件的创建者,谁创建了该文件,谁就是该文件的所有者。

ls -ahl:查看文件的所有者

chown 用户名 文件名:修改文件所有者

组的创建

groupadd 组名

创建一个组

groupadd monster

创建一个用户 fox,并放入到 monster 组中

useradd -g monster fox

文件、目录所在组

ls -ahl:查看文件的所有组

chgrp 组名 文件名:修改文件所在组

其它组

除文件的所有者和所在组的用户外,系统的其它用户都是文件的其它组

改变用户所在组

在添加用户时,可以指定将该用户添加到哪个组中,同样的用 root 的管理权限可以改变某个用户所在的组

改变用户所在组

usermod -g 组名 用户名

usermod -d 目录名 用户名 改变该用户登陆的初始目录。

权限基本

drwx------. 15 zjm  zjm    4096 12月  4 16:03 zjm

0-9 位说明

  • 第 0 位确定文件类型(d,-,l,c,b)

    • l 是链接,相当于 windows 的快捷方式

    • d 是目录,相当于 windows 的文件夹

    • c 是字符设备文件,鼠标,键盘

    • b 是块设备,比如硬盘

    • - 是普通文件

    • p 命名管道(FIFO)

    • s 套接字文件

  • 第 1-3 位确定所有者(该文件的所有者)拥有该文件的权限。—User

  • 第 4-6 位确定所属组(同用户组的)拥有该文件的权限,—Group

  • 第 7-9 位确定其他用户拥有该文件的权限,—Other

rwx 权限详解

rwx 作用到文件:

  • [ r ] 代表可读(read):可以读取,查看

  • [ w ] 代表可写(write):可以修改,但是不代表可以删除该文件,删除一个文件的前提条件是对该文件所

    在的目录有写权限,才能删除该文件。

  • [ x ] 代表可执行(execute):可以被执行。

rwx 作用到目录:

  • [ r ] 代表可读(read):可以读取,Is 查看目录内容
  • [ w ] 代表可写(write):可以修改,对目录内创建+删除+重命名目录
  • [ x ] 代表可执行(execute):可以进入该目录

文件及目录权限

drwxr-xr-x.  2 root root   4096 12月  6 13:36 java

10 个字符确定不同用户能对文件干什么

第一个字符代表文件类型:-I d c b

其余字符每 3 个一组(rwx)读(r)写(w)执行(x)

第一组 rwx: 文件拥有者 的权限是读、写和执行

第二组 rw-: 与文件拥有者同一组的用户 的权限是读、写但不能执行

第三组 r–: 不与文件拥有者同组的其他用户 的权限是读不能写和执行

可用数字表示为:r = 4, w = 2, x = 1 因此 rwx = 4+2+1 = 7

其它说明:
2:文件:硬连接数或目录:子目录数
root :用户
root :组
4096:文件大小(字节),如果是文件夹,显示 4096 字节
12月 6 13:36:最后修改日期
java:文件名

修改文件或目录权限

通过 chmod 指令,可以修改文件或者目录的权限。

文件类型 || 属主权限 || || 属组权限 || || 其他用户权限 ||
0 1 2 3 4 5 6 7 8 9
d r w x r - x r - x
目录 执行 执行 执行

第一种方式:+增加权限、-除去权限、= 变更权限

u: 所有者 g:所有组 o:其他人 a:所有人(u、g、o 的总和)

  • chmod u = rwx, g = rx, o = x 文件/目录名
  • chmod o+w 文件/目录名
  • chmod a-x 文件/目录名

案例:

给文件的所有者读写执行权限,给所在组读执行权限,给其他组读执行权限。

chmod u=rwx,g=rx,o=rx 文件目录

给文件的所有者除去执行的权限,增加组写的权限

chmod u-x,g+w 文件目录

给文件的所有用户添加读的权限

chmod a+r  文件目录

第二种方式:通过数字变更权限

r=4,w=2,x=1 rwx=4+2+1=7

chmod u=rwx,g=rx,o=x 文件目录名 相当于 chmod 751 文件目录名

案例:

文件的权限修改成rwxr-xr-x,使用给数字的方式实现:

chmod 755 文件目录名

修改文件目录所有者

# 改变所有者
chown newowner 文件/目录  
# 改变所有者和所在组 
chown newowner:newgroup 文件/目录

-R 如果是目录则使其下所有子文件或目录递归生效

案例:

将/home/abc.txt文件的所有者修改成tom

chown tom /home/abc.txt

将/home/java目录下所有的文件和目录的所有者都修改成tom

chown -R tom /home/java

修改文件目录所在组

# 改变所在组
chgrp newgroup 文件/目录

案例:

将/home/abc.txt文件的所在组修改成tom001

chgrp tom001 /home/abc.txt 

将/home/java目录下所有的文件和目录的所在组都修改成tom001

chgrp -R tom001 /home/java

权限管理应用案例

创建组

groupadd group01
groupadd group02

创建用户

useradd -g group01 jc1
useradd -g group01 jc2

useradd -g group02 bf1
useradd -g group02 bf2

设置用户密码

passwd jc1
passwd jc2
passwd bf1
passwd bf2

创建文件

r w x
4 2 1

jc1创建一个文件,自己可以读写,本组人可以读,其他人没任何权限

vim jc1.txt
chmod 640 jc1.txt

jc1修改该文件权限,本组人可以写,其他人可以读

chmod g=w,o=r jc1.txt

bf1来到group01组,看看是否可以读写该文件

usermod -g group01 bf1

# 验证是否修改成功
id bf1

如果要操作文件目录内的文件,要先有文件目录对应的权限

任务调度

crond 任务调度

crontab进行定时任务的设置

任务调度:是指系统在某个时间执行的特定的命令或程序。
任务调度分类:

系统工作:有些重要的工作必须周而复始地执行。如病毒扫描等。

个别用户工作:个别用户可能希望执行某些程序,比如对mysql数据库的备份。

crond基本语法

crontab [选项]

常用选项

-e:编辑crontab定时任务

-l:查询crontab任务

-r:删除当前用户所有的crontab任务

入门

  1. 设置任务调度文件:/etc/crontab
  2. 设置个人任务调度。执行crontab -e 命令。
  3. 接着输入任务调度文件
  4. */1 * * * * ls -l /etc/ > /tmp/zjm.txt 意思:每小时的每分钟执行

crond时间规则

五个参数占位符说明

项目 含义 范围
第一个“*” 一小时当中的第几分钟 0-59
第二个“*" 一天当中的第几小时 0-23
第三个“*” 一个月当中的第几天 1-31
第四个“*” 一年当中的第几月 1-12
第五个“*“ 一周当中的星期几 0-7(0和7都代表星期日)

特殊符号说明

特殊符号 含义
* 代表任何时间。比如第一个“*”就代表一小时中每分钟都执行一次的意思。
代表不连续的时间。比如“0 8,12,16 * * *命令”,就代表在每天的8点0分,12点0分,16点0分都执行一次命令
- 代表连续的时间范围。比如“0 5 * * 1-6命令”,代表在周一到周六的凌晨5点0分执行命令
*/n 代表每隔多久执行一次。比如“ */10 * * * *命令”,代表每隔10分钟就执行一遍命令

特定时间执行任务案例

crond时间表达式 含义
45 22 * * * 在22点45分执行命令
0 17 * * 1 每周1的17点0分执行命令
0 5 1,15 * * 每月1号和15号的凌晨5点0分执行命令
40 4 * * 1-5 每周一到周五的凌晨4点40分执行命令
*/10 4 * * * 每天的凌晨4点,每隔10分钟执行一次命令, 每天早上 4 点开始,每隔 10 分钟执行一次任务,直到 4 点 59 分为止。具体执行时间点为 4:00, 4:10, 4:20, 4:30, 4:40, 4:50。
0 0 1,15 * 1 每月1号和15号,每周1的0点0分都会执行命令。

注意:星期几和几号最好不要同时出现,因为他们定义的都是天。非常容易让管理员混乱。

crond应用实例

1.每隔1分钟,就将当前的日期信息,追加到/tmp/mydate文件中

*/1 * * * * date >> /tmp/mydate

2.每隔1分钟,将当前日期和日历都追加到/home/mycal文件中

步骤:

  • vim /home/my.sh 写入内容date >> /home/mycal 和cal >> /home/mycal。创建脚本my.sh
  • 给my.sh增加执行权限,chmod u+x /home/my.sh
  • crontab -e 增加*/1 * * * * /home/my.sh

3.每天凌晨2:00将mysql数据库testdb,备份到文件中。提示:指令为
mysqldump -u root -p密码 数据库 > /home/db.bak

步骤:

  • crontab -e
  • 0 2 * * * mysqldump -u root -p testdb > /home/db.bak

crond相关指令

conrtab -r:终止任务调度。

crontab -l:列出当前有那些任务调度。

service crond restart [重启任务调度]。

at任务调度

at概述

  • at命令是一次性定时计划任务,at的守护进程atd会以后台模式运行,检查作业队列来运行。
  • 默认情况下,atd守护进程每60秒检查作业队列,有作业时,会检查作业运行时间,如果时间与当前时间匹配,则运行此作业。
  • at命令是一次性定时计划任务,执行完一个任务后不再执行此任务了
  • 在使用at命令的时候,一定要保证atd进程的启动,可以使用相关指令来查看
  • ps -ef | grep atd 检测atd是否在运行

at命令格式

at [选项] [时间]

Ctrl + D 结束at命令的输入

选项:

选项 描述 示例
-f <文件> 从指定文件读取命令 at -f script.sh now + 1 hour
-m 在任务完成后发送邮件通知 at -m now + 1 day
-v 显示任务队列中的作业号 at -v
-q <队列> 指定任务队列(默认为 a-z 中的一个字母) at -q b now + 2 days
-latq 列出当前所有待执行的任务(等同于 atq at -l
-datrm 删除指定的任务(等同于 atrm at -d 12345
-c <作业号> 查看指定任务的内容 at -c 12345
-t <时间> 使用特定格式的时间字符串(如 202310101200 表示 2023-10-10 12:00) at -t 202310101200
now 立即执行任务 at now
now + <时间> 从现在起延迟一段时间后执行任务 at now + 5 minutes
<具体时间> 指定具体的时间点执行任务 at 14:30
noonmidnight 在中午或午夜执行任务 at noon
teatime 在下午 4 点执行任务 at teatime
todaytomorrow 在今天或明天的某个时间执行任务 at tomorrow 10:00

at时间定义

  • 受在当天的hh:mm(小时:分钟)式的时间指定。假如该时间已过去,那么就放在第二天执行。例如:

    04:00

  • 使用midnight(深夜),noon(中午),teatime(饮茶时间,一般是下午4点)等比较模糊的词语来指

    定时间。

  • 采用12小时计时制,即在时间后面加上AM(上午)或PM(下午)来说明是上午还是下午。例如:12pm

  • 指定命令执行的具体日期,指定格式为month day(月日)或mm/dd/yy(月/日/年)或dd.mm.yy(日.月.年),指定的日期必须跟在指定时间的后面。例如:04:00 2021-03-1

  • 使用相对计时法。指定格式为:now + count time-units,now就是当前时间,time-units是时间单位,

    这里能够是minutes(分钟)、hours(小时)、days(天)、weeks(星期)。count是时间的数量,

    几天,几小时。例如:now + 5 minutes

  • 直接使用today(今天)、tomorrow(明天)来指定完成命令的时间。

at任务调度应用实例

1.at命令设置任务

at 5pm + 2 days /bin/ls /home

2.使用atq命令列出当前所有待执行的任务

atq

3.使用atrm删除任务

atrm 任务编号

磁盘分区

linux分区

原理介绍:

  • Linux来说无论有几个分区,分给哪一目录使用,它归根结底就只有一个根目录,一个独立且唯一的

    文件结构,Linux中每个分区都是用来组成整个文件系统的一部分。

  • Linux采用了一种叫“载入”的处理方法,它的整个文件系统中包含了一整套的文件和目录,且将一

    个分区和一个目录联系起来。这时要载入的一个分区将使它的存储空间在一个目录下获得。

查看所有设备挂载情况:

[root@zjm009 ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   20G  0 disk 
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0    2G  0 part [SWAP]
└─sda3   8:3    0   17G  0 part /
sr0     11:0    1  4.4G  0 rom  /run/media/root/CentOS 7 x86_64
[root@zjm009 ~]# 
[root@zjm009 ~]# lsblk -f
NAME   FSTYPE  LABEL           UUID                                 MOUNTPOINT
sda                                                                 
├─sda1 ext4                    8a1b9aa6-a7b1-49cb-9478-bd70edbc00df /boot
├─sda2 swap                    4c6457ec-1677-45d5-b30d-07e46b216eed [SWAP]
└─sda3 ext4                    427ee905-1f53-4182-993c-27a45e504a19 /
sr0    iso9660 CentOS 7 x86_64 2020-11-04-11-36-43-00               /run/media/r
[root@zjm009 ~]# 

硬盘说明

  • inux硬盘分IDE硬盘和SCSI硬盘,目前基本上是SCSI硬盘
  • 对于IDE硬盘,驱动器标识符为“hdx~”,其中“hd”表明分区所在设备的类型,这里是指IDE硬盘
    了。“x”为盘号(a为基本盘,b为基本从属盘,c为辅助主盘,d为辅助从属盘),“~”代表分区,
    前四个分区用数字1到4表示,它们是主分区或扩展分区,从5开始就是逻辑分区。例,hda3表示为
    第一个IDE硬盘上的第三个主分区或扩展分区,hdb2表示为第二个IDE硬盘上的第二个主分区或扩展
    分区。
  • 对于SCSI硬盘则标识为“sdx~"”,SCSI硬盘是用“sd”来表示分区所在设备的类型的,其余则和
    IDE硬盘的表示方法一样。

挂载案例

以增加一块硬盘为例来熟悉下磁盘的相关指令和深入理解磁盘分区、挂载、卸载的概念。

虚拟机增加一块硬盘:

  • 虚拟机设置中添加硬盘,然后重启
[root@zjm009 ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   20G  0 disk 
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0    2G  0 part [SWAP]
└─sda3   8:3    0   17G  0 part /
sdb      8:16   0    1G  0 disk 
sr0     11:0    1  4.4G  0 rom  /run/media/root/CentOS 7 x86_64
[root@zjm009 ~]# 

  • 分区

分区命令:fdisk /dev/sdb

开始对/sdb分区

m显示命令列表

p显示磁盘分区同fdisk-1

n新增分区

d删除分区

w写入并退出

说明:开始分区后输入n,新增分区,然后选择p,分区类型为主分区。两次回车默认剩余全部空间。最

后输入w写入分区并退出,若不保存退出输入q。

[root@zjm009 ~]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。

Device does not contain a recognized partition table
使用磁盘标识符 0xef3eff6e 创建新的 DOS 磁盘标签。

命令(输入 m 获取帮助):m
命令操作
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

命令(输入 m 获取帮助):n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
分区号 (1-4,默认 1):1
起始 扇区 (2048-2097151,默认为 2048):
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-2097151,默认为 2097151):
将使用默认值 2097151
分区 1 已设置为 Linux 类型,大小设为 1023 MiB

命令(输入 m 获取帮助):w
The partition table has been altered!

Calling ioctl() to re-read partition table.
正在同步磁盘。
[root@zjm009 ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   20G  0 disk 
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0    2G  0 part [SWAP]
└─sda3   8:3    0   17G  0 part /
sdb      8:16   0    1G  0 disk 
└─sdb1   8:17   0 1023M  0 part 
sr0     11:0    1  4.4G  0 rom  /run/media/root/CentOS 7 x86_64
[root@zjm009 ~]# 

  • 格式化磁盘

分区命令:mkfs -t ext4 /dev/sdb1,其中ext4是分区类型

[root@zjm009 ~]# mkfs -t ext4 /dev/sdb1
mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
65536 inodes, 261888 blocks
13094 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376

Allocating group tables: 完成                            
正在写入inode表: 完成                            
Creating journal (4096 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

[root@zjm009 ~]# 

[root@zjm009 ~]# lsblk -f
NAME   FSTYPE  LABEL           UUID                                 MOUNTPOINT
sda                                                                 
├─sda1 ext4                    8a1b9aa6-a7b1-49cb-9478-bd70edbc00df /boot
├─sda2 swap                    4c6457ec-1677-45d5-b30d-07e46b216eed [SWAP]
└─sda3 ext4                    427ee905-1f53-4182-993c-27a45e504a19 /
sdb                                                                 
└─sdb1 ext4                    35a37ae2-4f3c-4bb9-a5ee-fe45508f4bef 
sr0    iso9660 CentOS 7 x86_64 2020-11-04-11-36-43-00               /run/media/r
[root@zjm009 ~]# 

  • 挂载:将一个分区与一个目录联系起来

mount 设备名称 挂载目录

例如:mount /dev/sdb1 /newdisk

[root@zjm009 /]# mount /dev/sdb1 /newdisk
[root@zjm009 /]# lsblk -f
NAME   FSTYPE  LABEL           UUID                                 MOUNTPOINT
sda                                                                 
├─sda1 ext4                    8a1b9aa6-a7b1-49cb-9478-bd70edbc00df /boot
├─sda2 swap                    4c6457ec-1677-45d5-b30d-07e46b216eed [SWAP]
└─sda3 ext4                    427ee905-1f53-4182-993c-27a45e504a19 /
sdb                                                                 
└─sdb1 ext4                    35a37ae2-4f3c-4bb9-a5ee-fe45508f4bef /newdisk
sr0    iso9660 CentOS 7 x86_64 2020-11-04-11-36-43-00               /run/media/r
[root@zjm009 /]# 

  • 卸载

umount 设备名称或者挂载目录
例如:umount /dev/sdb1或者umount /newdisk

注:用命令行挂载重启后会失效

  • 设置可以自动挂载
  • 永久挂载

永久挂载:通过修改/etc/fstab实现挂载

添加完成后执行mount -a即刻生效

磁盘情况查询

查询系统整体磁盘使用情况

[root@zjm009 ~]# df -h
文件系统        容量  已用  可用 已用% 挂载点
devtmpfs        976M     0  976M    0% /dev
tmpfs           991M     0  991M    0% /dev/shm
tmpfs           991M   11M  980M    2% /run
tmpfs           991M     0  991M    0% /sys/fs/cgroup
/dev/sda3        17G  4.7G   12G   30% /
/dev/sda1       976M  139M  770M   16% /boot
tmpfs           199M   20K  199M    1% /run/user/0
/dev/sr0        4.4G  4.4G     0  100% /run/media/root/CentOS 7 x86_64
/dev/sdb1       991M  2.6M  922M    1% /newdisk
[root@zjm009 ~]# 

查询指定目录的磁盘占用情况

基本语法

du -h /目录

查询指定目录的磁盘占用情况,默认为当前目录

-s指定目录占用大小汇总

-h带计量单位

-a含文件

–max-depth=1子目录深度

-c列出明细的同时,增加汇总值

[root@zjm009 ~]# du -h --max-depth=1 /opt
155M	/opt/vmware-tools-distrib
7.7M	/opt/tmp2
4.0K	/opt/rh
7.8M	/opt/tmp
222M	/opt
[root@zjm009 ~]# 
[root@zjm009 ~]# du -ha --max-depth=1 /opt
155M	/opt/vmware-tools-distrib
7.7M	/opt/tmp2
52M	/opt/VMwareTools-10.3.25-20206839.tar.gz
4.0K	/opt/rh
7.8M	/opt/tmp
222M	/opt
[root@zjm009 ~]# du -hac --max-depth=1 /opt
155M	/opt/vmware-tools-distrib
7.7M	/opt/tmp2
52M	/opt/VMwareTools-10.3.25-20206839.tar.gz
4.0K	/opt/rh
7.8M	/opt/tmp
222M	/opt
222M	总用量
[root@zjm009 ~]# 

磁盘实用命令

统计/opt文件夹下文件的个数

[root@zjm009 opt]# ls -l /opt | grep "^-" |wc -l 
1
[root@zjm009 opt]# 

统计/opt文件夹下目录的个数

[root@zjm009 opt]# ls -l /opt | grep "^d" |wc -l 
4
[root@zjm009 opt]# 

统计/opt文件夹下文件的个数,包括子文件夹里的

[root@zjm009 opt]# ls -lR /opt | grep "^-" |wc -l 
1475
[root@zjm009 opt]# 

统计/opt文件夹下目录的个数,包括子文件夹里的

[root@zjm009 opt]# ls -lR /opt | grep "^d" |wc -l 
506
[root@zjm009 opt]# 

以树状显示目录结构

tree 目录

网络配置

网络配置命令

linux查看网络配置

ifconfig

ping命令

设置主机名和hosts映射

设置主机名

  1. 为了方便记忆,可以给linux系统设置主机名,也可以根据需要修改主机名
  2. 指令hostname:查看主机名
  3. 修改文件在/etc/hostname指定
  4. 修改后,重启生效

设置hosts映射

linux

在/etc/hosts文件指定

192.168.31.216 DESKTOP-3BL22EM

[root@zjm999 ~]# vim /etc/hosts
[root@zjm999 ~]# ping DESKTOP-3BL22EM
PING DESKTOP-3BL22EM (192.168.31.216) 56(84) bytes of data.
64 bytes from DESKTOP-3BL22EM (192.168.31.216): icmp_seq=1 ttl=128 time=0.554 ms
64 bytes from DESKTOP-3BL22EM (192.168.31.216): icmp_seq=2 ttl=128 time=4.09 ms
64 bytes from DESKTOP-3BL22EM (192.168.31.216): icmp_seq=3 ttl=128 time=0.612 ms
64 bytes from DESKTOP-3BL22EM (192.168.31.216): icmp_seq=4 ttl=128 time=0.707 ms
64 bytes from DESKTOP-3BL22EM (192.168.31.216): icmp_seq=5 ttl=128 time=0.842 ms
64 bytes from DESKTOP-3BL22EM (192.168.31.216): icmp_seq=6 ttl=128 time=0.741 ms
64 bytes from DESKTOP-3BL22EM (192.168.31.216): icmp_seq=7 ttl=128 time=2.19 ms
64 bytes from DESKTOP-3BL22EM (192.168.31.216): icmp_seq=8 ttl=128 time=2.44 ms
64 bytes from DESKTOP-3BL22EM (192.168.31.216): icmp_seq=9 ttl=128 time=3.65 ms
64 bytes from DESKTOP-3BL22EM (192.168.31.216): icmp_seq=10 ttl=128 time=1.19 ms
64 bytes from DESKTOP-3BL22EM (192.168.31.216): icmp_seq=11 ttl=128 time=0.569 ms
^C
--- DESKTOP-3BL22EM ping statistics ---
11 packets transmitted, 11 received, 0% packet loss, time 10011ms
rtt min/avg/max/mdev = 0.554/1.600/4.090/1.239 ms

主机名解析

进程管理

基本介绍

  1. 在LINUX中,每个执行的程序都称为一个进程。每一个进程都分配一个ID号(pid,进程号)。

  2. 每个进程都可能以两种方式存在的。前台与后台,所谓前台进程就是用户目前的屏幕上可以进行操作

    的。后台进程则是实际在操作,但由于屏幕上无法看到的进程,通常使用后台方式执行。

  3. 般系统的服务都是以后台进程的方式存在,而且都会常驻在系统中。直到关机才才结束。

显示系统执行的进程

ps:是用来查看目前系统中,有哪些正在执行,以及它们执行的状况。可以不加任何参数.

ps -a:显示当前终端的所有进程信息

ps -u:以用户的格式显示进程信息

ps -x:显示后台进程运行的参数

[root@zjm999 ~]# ps
   PID TTY          TIME CMD
  1865 pts/0    00:00:00 bash
  2280 pts/0    00:00:00 ps
[root@zjm999 ~]# 

  • USER: 进程所有者的用户名。

  • PID: 进程的 ID(Process ID)。

  • %CPU: 进程占用的 CPU 百分比。

  • %MEM: 进程占用的物理内存百分比。

  • VSZ: 进程的虚拟内存大小(Virtual Memory Size),以 KB 为单位。

  • RSS: 进程的常驻内存大小(Resident Set Size),即实际使用的物理内存大小,以 KB 为单位。

  • TTY: 进程关联的终端(Terminal)。? 表示进程没有关联的终端。

  • STAT
    进程的状态。常见的状态包括:
    • S: 睡眠(Sleeping)
    • R: 运行(Running)
    • Z: 僵尸进程(Zombie)
    • T: 停止(Stopped)
    • D: 不可中断睡眠(Uninterruptible Sleep)
  • START: 进程启动的时间或日期。

  • TIME: 进程占用的 CPU 时间。

  • COMMAND: 启动进程的命令及其参数。

[root@zjm999 ~]# ps -aux | more
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.1  0.3 194092  7284 ?        Ss   15:34   0:03 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
root          2  0.0  0.0      0     0 ?        S    15:34   0:00 [kthreadd]
root          4  0.0  0.0      0     0 ?        S<   15:34   0:00 [kworker/0:0H]
root          5  0.0  0.0      0     0 ?        S    15:34   0:00 [kworker/u256:0]
root          6  0.0  0.0      0     0 ?        S    15:34   0:00 [ksoftirqd/0]
root          7  0.1  0.0      0     0 ?        S    15:34   0:03 [migration/0]
root          8  0.0  0.0      0     0 ?        S    15:34   0:00 [rcu_bh]
root          9  0.0  0.0      0     0 ?        S    15:34   0:00 [rcu_sched]
root         10  0.0  0.0      0     0 ?        S<   15:34   0:00 [lru-add-drain]
root         11  0.0  0.0      0     0 ?        S    15:34   0:00 [watchdog/0]
root         12  0.0  0.0      0     0 ?        S    15:34   0:00 [watchdog/1]
root         13  0.1  0.0      0     0 ?        S    15:34   0:03 [migration/1]
root         14  0.0  0.0      0     0 ?        S    15:34   0:00 [ksoftirqd/1]
root         16  0.0  0.0      0     0 ?        S<   15:34   0:00 [kworker/1:0H]
root         17  0.0  0.0      0     0 ?        S    15:34   0:00 [watchdog/2]
root         18  0.1  0.0      0     0 ?        S    15:34   0:03 [migration/2]
root         19  0.0  0.0      0     0 ?        S    15:34   0:00 [ksoftirqd/2]
root         21  0.0  0.0      0     0 ?        S<   15:34   0:00 [kworker/2:0H]
root         22  0.0  0.0      0     0 ?        S    15:34   0:00 [watchdog/3]
root         23  0.1  0.0      0     0 ?        S    15:34   0:03 [migration/3]
root         24  0.0  0.0      0     0 ?        S    15:34   0:00 [ksoftirqd/3]
root         26  0.0  0.0      0     0 ?        S<   15:34   0:00 [kworker/3:0H]
root         27  0.0  0.0      0     0 ?        S    15:34   0:00 [watchdog/4]
root         28  0.1  0.0      0     0 ?        S    15:34   0:03 [migration/4]
root         29  0.0  0.0      0     0 ?        S    15:34   0:00 [ksoftirqd/4]
root         31  0.0  0.0      0     0 ?        S<   15:34   0:00 [kworker/4:0H]
root         32  0.0  0.0      0     0 ?        S    15:34   0:00 [watchdog/5]
root         33  0.1  0.0      0     0 ?        S    15:34   0:03 [migration/5]
root         34  0.0  0.0      0     0 ?        S    15:34   0:00 [ksoftirqd/5]
root         36  0.0  0.0      0     0 ?        S<   15:34   0:00 [kworker/5:0H]
root         37  0.0  0.0      0     0 ?        S    15:34   0:00 [watchdog/6]
root         38  0.1  0.0      0     0 ?        S    15:34   0:03 [migration/6]
root         39  0.0  0.0      0     0 ?        S    15:34   0:00 [ksoftirqd/6]
root         41  0.0  0.0      0     0 ?        S<   15:34   0:00 [kworker/6:0H]
root         42  0.0  0.0      0     0 ?        S    15:34   0:00 [watchdog/7]
[root@zjm999 ~]# 

指令:ps -aux | grep xxx,比如我看看有没有sshd服务

[root@zjm999 ~]# ps -aux | grep sshd
root       1115  0.0  0.2 112900  4308 ?        Ss   15:34   0:00 /usr/sbin/sshd -D
root       1853  0.0  0.2 160988  5636 ?        Ss   15:35   0:00 sshd: root@pts/0
root       2411  0.0  0.0 112828   976 pts/0    S+   16:18   0:00 grep --color=auto sshd

父子进程

ps -ef是以全格式显示当前所有的进程

[root@zjm999 ~]# ps -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 15:34 ?        00:00:03 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
root          2      0  0 15:34 ?        00:00:00 [kthreadd]
root          4      2  0 15:34 ?        00:00:00 [kworker/0:0H]
root          5      2  0 15:34 ?        00:00:00 [kworker/u256:0]
root          6      2  0 15:34 ?        00:00:00 [ksoftirqd/0]
root          7      2  0 15:34 ?        00:00:03 [migration/0]
root          8      2  0 15:34 ?        00:00:00 [rcu_bh]
root          9      2  0 15:34 ?        00:00:00 [rcu_sched]
root         10      2  0 15:34 ?        00:00:00 [lru-add-drain]
root         11      2  0 15:34 ?        00:00:00 [watchdog/0]
root         12      2  0 15:34 ?        00:00:00 [watchdog/1]
root         13      2  0 15:34 ?        00:00:03 [migration/1]
root         14      2  0 15:34 ?        00:00:00 [ksoftirqd/1]
root         16      2  0 15:34 ?        00:00:00 [kworker/1:0H]
root         17      2  0 15:34 ?        00:00:00 [watchdog/2]
root         18      2  0 15:34 ?        00:00:03 [migration/2]
root         19      2  0 15:34 ?        00:00:00 [ksoftirqd/2]
root         21      2  0 15:34 ?        00:00:00 [kworker/2:0H]
root         22      2  0 15:34 ?        00:00:00 [watchdog/3]
root         23      2  0 15:34 ?        00:00:03 [migration/3]
root         24      2  0 15:34 ?        00:00:00 [ksoftirqd/3]
root         26      2  0 15:34 ?        00:00:00 [kworker/3:0H]
root         27      2  0 15:34 ?        00:00:00 [watchdog/4]
root         28      2  0 15:34 ?        00:00:03 [migration/4]
root         29      2  0 15:34 ?        00:00:00 [ksoftirqd/4]
root         31      2  0 15:34 ?        00:00:00 [kworker/4:0H]
root         32      2  0 15:34 ?        00:00:00 [watchdog/5]
root         33      2  0 15:34 ?        00:00:03 [migration/5]
root         34      2  0 15:34 ?        00:00:00 [ksoftirqd/5]
root         36      2  0 15:34 ?        00:00:00 [kworker/5:0H]
root         37      2  0 15:34 ?        00:00:00 [watchdog/6]
root         38      2  0 15:34 ?        00:00:03 [migration/6]
root         39      2  0 15:34 ?        00:00:00 [ksoftirqd/6]
root         41      2  0 15:34 ?        00:00:00 [kworker/6:0H]
root         42      2  0 15:34 ?        00:00:00 [watchdog/7]
root         43      2  0 15:34 ?        00:00:03 [migration/7]
root         44      2  0 15:34 ?        00:00:00 [ksoftirqd/7]
root         46      2  0 15:34 ?        00:00:00 [kworker/7:0H]
root         48      2  0 15:34 ?        00:00:00 [kdevtmpfs]
root         49      2  0 15:34 ?        00:00:00 [netns]
root         50      2  0 15:34 ?        00:00:00 [khungtaskd]
root         51      2  0 15:34 ?        00:00:00 [writeback]
root         52      2  0 15:34 ?        00:00:00 [kintegrityd]
root         53      2  0 15:34 ?        00:00:00 [bioset]
root         54      2  0 15:34 ?        00:00:00 [bioset]
root         55      2  0 15:34 ?        00:00:00 [bioset]
root         56      2  0 15:34 ?        00:00:00 [kblockd]
root         57      2  0 15:34 ?        00:00:00 [md]
root         58      2  0 15:34 ?        00:00:00 [edac-poller]
root         59      2  0 15:34 ?        00:00:00 [watchdogd]
root         60      2  0 15:34 ?        00:00:00 [kworker/7:1]
root         65      2  0 15:34 ?        00:00:00 [kswapd0]
root         66      2  0 15:34 ?        00:00:00 [ksmd]
root         67      2  0 15:34 ?        00:00:00 [khugepaged]
root         68      2  0 15:34 ?        00:00:00 [crypto]
root         76      2  0 15:34 ?        00:00:00 [kthrotld]
root         78      2  0 15:34 ?        00:00:00 [kworker/6:1]
root         79      2  0 15:34 ?        00:00:00 [kmpath_rdacd]
root         80      2  0 15:34 ?        00:00:00 [kaluad]
root         82      2  0 15:34 ?        00:00:00 [kpsmoused]
root         84      2  0 15:34 ?        00:00:00 [ipv6_addrconf]
root         85      2  0 15:34 ?        00:00:02 [kworker/1:1]
root         98      2  0 15:34 ?        00:00:00 [deferwq]
root         99      2  0 15:34 ?        00:00:00 [kworker/5:1]
root        136      2  0 15:34 ?        00:00:00 [kauditd]
root        183      2  0 15:34 ?        00:00:00 [kworker/4:1]
root        219      2  0 15:34 ?        00:00:00 [kworker/5:2]
root        247      2  0 15:34 ?        00:00:00 [kworker/2:1]
root        253      2  0 15:34 ?        00:00:00 [kworker/3:1]
root        274      2  0 15:34 ?        00:00:00 [kworker/6:2]
root        298      2  0 15:34 ?        00:00:00 [mpt_poll_0]
root        300      2  0 15:34 ?        00:00:00 [mpt/0]
root        303      2  0 15:34 ?        00:00:00 [nfit]
root        305      2  0 15:34 ?        00:00:00 [scsi_eh_0]
root        306      2  0 15:34 ?        00:00:00 [scsi_tmf_0]
root        307      2  0 15:34 ?        00:00:00 [ata_sff]
root        308      2  0 15:34 ?        00:00:00 [kworker/u256:2]
root        309      2  0 15:34 ?        00:00:00 [scsi_eh_1]
root        310      2  0 15:34 ?        00:00:00 [scsi_tmf_1]
root        311      2  0 15:34 ?        00:00:00 [scsi_eh_2]
root        312      2  0 15:34 ?        00:00:00 [scsi_tmf_2]
root        315      2  0 15:34 ?        00:00:00 [irq/16-vmwgfx]
root        316      2  0 15:34 ?        00:00:00 [ttm_swap]
root        331      2  0 15:34 ?        00:00:00 [kworker/4:2]
root        334      2  0 15:34 ?        00:00:00 [kworker/2:2]
root        338      2  0 15:34 ?        00:00:00 [kworker/7:2]
root        346      2  0 15:34 ?        00:00:00 [jbd2/sda3-8]
root        347      2  0 15:34 ?        00:00:00 [ext4-rsv-conver]
root        431      1  0 15:34 ?        00:00:00 /usr/lib/systemd/systemd-journald
root        459      1  0 15:34 ?        00:00:00 /usr/sbin/lvmetad -f
root        474      1  0 15:34 ?        00:00:03 /usr/lib/systemd/systemd-udevd
root        500      2  0 15:34 ?        00:00:00 [kworker/0:1H]
root        501      2  0 15:34 ?        00:00:00 [kworker/1:1H]
root        598      2  0 15:34 ?        00:00:00 [jbd2/sda1-8]
root        599      2  0 15:34 ?        00:00:00 [ext4-rsv-conver]
root        637      1  0 15:34 ?        00:00:00 /sbin/auditd
root        639    637  0 15:34 ?        00:00:00 /sbin/audispd
root        641    639  0 15:34 ?        00:00:00 /usr/sbin/sedispatch
root        643      2  0 15:34 ?        00:00:00 [rpciod]
root        644      2  0 15:34 ?        00:00:00 [xprtiod]
dbus        666      1  0 15:34 ?        00:00:00 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activa
rpc         667      1  0 15:34 ?        00:00:00 /sbin/rpcbind -w
libstor+    669      1  0 15:34 ?        00:00:00 /usr/bin/lsmd -d
root        670      1  0 15:34 ?        00:00:01 /sbin/rngd -f
root        672      1  0 15:34 ?        00:00:00 /usr/lib/systemd/systemd-logind
root        673      1  0 15:34 ?        00:00:00 /usr/sbin/smartd -n -q never
root        674      1  0 15:34 ?        00:00:00 /usr/sbin/alsactl -s -n 19 -c -E ALSA_CONFIG_PATH=/etc/alsa/alsactl.conf --initfile=/l
root        677      1  0 15:34 ?        00:00:00 /usr/libexec/udisks2/udisksd
root        680      1  0 15:34 ?        00:00:00 /usr/bin/VGAuthService -s
root        681      1  0 15:34 ?        00:00:08 /usr/bin/vmtoolsd
rtkit       683      1  0 15:34 ?        00:00:00 /usr/libexec/rtkit-daemon
root        686      1  0 15:34 ?        00:00:00 /usr/sbin/ModemManager
root        688      1  0 15:34 ?        00:00:00 /usr/libexec/accounts-daemon
root        690      1  0 15:34 ?        00:00:00 /usr/sbin/abrtd -d -s
root        692      1  0 15:34 ?        00:00:00 /usr/bin/abrt-watch-log -F Backtrace /var/log/Xorg.0.log -- /usr/bin/abrt-dump-xorg -x
root        698      1  0 15:34 ?        00:00:00 /usr/bin/abrt-watch-log -F BUG: WARNING: at WARNING: CPU: INFO: possible recursive loc
chrony      701      1  0 15:34 ?        00:00:00 /usr/sbin/chronyd
root        702      1  0 15:34 ?        00:00:00 /usr/sbin/irqbalance --foreground
polkitd     704      1  0 15:34 ?        00:00:00 /usr/lib/polkit-1/polkitd --no-debug
root        708      1  0 15:34 ?        00:00:00 /usr/sbin/gssproxy -D
avahi       713      1  0 15:34 ?        00:00:00 avahi-daemon: running [zjm999.local]
avahi       735    713  0 15:34 ?        00:00:00 avahi-daemon: chroot helper
root        742      1  0 15:34 ?        00:00:00 /bin/bash /usr/sbin/ksmtuned
root        756      2  0 15:34 ?        00:00:00 [kworker/2:1H]
root        760      1  0 15:34 ?        00:00:00 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid
root        764      2  0 15:34 ?        00:00:00 [kworker/0:5]
root        765      2  0 15:34 ?        00:00:00 [kworker/0:6]
root        788      1  0 15:34 ?        00:00:00 /usr/sbin/NetworkManager --no-daemon
root        918    788  0 15:34 ?        00:00:00 /sbin/dhclient -d -q -sf /usr/libexec/nm-dhcp-helper -pf /var/run/dhclient-ens33.pid -
root        956      2  0 15:34 ?        00:00:00 [kworker/7:1H]
root        957      2  0 15:34 ?        00:00:00 [kworker/5:1H]
root       1114      1  0 15:34 ?        00:00:00 /usr/bin/python2 -Es /usr/sbin/tuned -l -P
root       1115      1  0 15:34 ?        00:00:00 /usr/sbin/sshd -D
root       1116      1  0 15:34 ?        00:00:00 /usr/sbin/cupsd -f
root       1119      1  0 15:34 ?        00:00:00 /usr/sbin/rsyslogd -n
root       1122      1  0 15:34 ?        00:00:00 /usr/sbin/libvirtd
root       1128      2  0 15:34 ?        00:00:00 [kworker/3:2]
root       1131      1  0 15:34 ?        00:00:01 /usr/sbin/crond -n
root       1132      1  0 15:34 ?        00:00:00 /usr/sbin/gdm
root       1133      1  0 15:34 ?        00:00:00 /usr/sbin/atd -f
root       1300      1  0 15:34 ?        00:00:00 /usr/libexec/postfix/master -w
postfix    1305   1300  0 15:34 ?        00:00:00 pickup -l -t unix -u
postfix    1306   1300  0 15:34 ?        00:00:00 qmgr -l -t unix -u
root       1378   1132  0 15:34 tty1     00:00:00 /usr/bin/X :0 -background none -noreset -audit 4 -verbose -auth /run/gdm/auth-for-gdm-
root       1398   1132  0 15:34 ?        00:00:00 gdm-session-worker [pam/gdm-launch-environment]
gdm        1402   1398  0 15:34 ?        00:00:00 /usr/libexec/gnome-session-binary --autostart /usr/share/gdm/greeter/autostart
gdm        1407      1  0 15:34 ?        00:00:00 dbus-launch --exit-with-session /usr/libexec/gnome-session-binary --autostart /usr/sha
gdm        1408      1  0 15:34 ?        00:00:00 /usr/bin/dbus-daemon --fork --print-pid 5 --print-address 7 --session
gdm        1420      1  0 15:34 ?        00:00:00 /usr/libexec/at-spi-bus-launcher
gdm        1425   1420  0 15:34 ?        00:00:00 /usr/bin/dbus-daemon --config-file=/usr/share/defaults/at-spi2/accessibility.conf --no
gdm        1428      1  0 15:34 ?        00:00:00 /usr/libexec/at-spi2-registryd --use-gnome-session
gdm        1465   1402  0 15:34 ?        00:00:09 /usr/bin/gnome-shell
root       1473      1  0 15:34 ?        00:00:00 /usr/libexec/upowerd
gdm        1496      1  0 15:34 ?        00:00:00 /usr/bin/pulseaudio --start --log-target=syslog
gdm        1527   1465  0 15:34 ?        00:00:00 ibus-daemon --xim --panel disable
gdm        1530   1527  0 15:34 ?        00:00:00 /usr/libexec/ibus-dconf
gdm        1533      1  0 15:34 ?        00:00:00 /usr/libexec/ibus-x11 --kill-daemon
gdm        1535      1  0 15:34 ?        00:00:00 /usr/libexec/ibus-portal
gdm        1546      1  0 15:34 ?        00:00:00 /usr/libexec/xdg-permission-store
root       1554      1  0 15:34 ?        00:00:00 /usr/libexec/boltd
root       1559      1  0 15:34 ?        00:00:00 /usr/sbin/wpa_supplicant -u -f /var/log/wpa_supplicant.log -c /etc/wpa_supplicant/wpa_
root       1564      1  0 15:34 ?        00:00:00 /usr/libexec/packagekitd
gdm        1570   1402  0 15:34 ?        00:00:00 /usr/libexec/gsd-xsettings
gdm        1571   1402  0 15:34 ?        00:00:00 /usr/libexec/gsd-a11y-settings
gdm        1573   1402  0 15:34 ?        00:00:00 /usr/libexec/gsd-clipboard
gdm        1576   1402  0 15:34 ?        00:00:00 /usr/libexec/gsd-color
gdm        1577   1402  0 15:34 ?        00:00:00 /usr/libexec/gsd-datetime
gdm        1578   1402  0 15:34 ?        00:00:00 /usr/libexec/gsd-housekeeping
gdm        1579   1402  0 15:34 ?        00:00:00 /usr/libexec/gsd-keyboard
gdm        1584   1402  0 15:34 ?        00:00:00 /usr/libexec/gsd-media-keys
gdm        1588   1402  0 15:34 ?        00:00:00 /usr/libexec/gsd-mouse
gdm        1591   1402  0 15:34 ?        00:00:00 /usr/libexec/gsd-power
gdm        1593   1402  0 15:34 ?        00:00:00 /usr/libexec/gsd-print-notifications
gdm        1598   1402  0 15:34 ?        00:00:00 /usr/libexec/gsd-rfkill
gdm        1603   1402  0 15:34 ?        00:00:00 /usr/libexec/gsd-screensaver-proxy
gdm        1607   1402  0 15:34 ?        00:00:00 /usr/libexec/gsd-sharing
gdm        1611   1402  0 15:34 ?        00:00:00 /usr/libexec/gsd-smartcard
gdm        1613   1402  0 15:34 ?        00:00:00 /usr/libexec/gsd-sound
gdm        1620   1402  0 15:34 ?        00:00:00 /usr/libexec/gsd-wacom
colord     1640      1  0 15:34 ?        00:00:00 /usr/libexec/colord
gdm        1680   1527  0 15:34 ?        00:00:00 /usr/libexec/ibus-engine-simple
root       1734      2  0 15:34 ?        00:00:00 [kworker/3:1H]
nobody     1786      1  0 15:34 ?        00:00:00 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --d
root       1787   1786  0 15:34 ?        00:00:00 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --d
root       1844      2  0 15:35 ?        00:00:00 [kworker/4:1H]
root       1853   1115  0 15:35 ?        00:00:00 sshd: root@pts/0
root       1865   1853  0 15:35 pts/0    00:00:00 -bash
root       1909      2  0 15:36 ?        00:00:00 [kworker/6:1H]
root       2307      2  0 16:09 ?        00:00:00 [kworker/1:0]
root       2361      2  0 16:14 ?        00:00:00 [kworker/1:2]
root       2438      2  0 16:20 ?        00:00:00 [kworker/u256:1]
root       2474    742  0 16:23 ?        00:00:00 sleep 60
root       2475   1865  0 16:23 pts/0    00:00:00 ps -ef
[root@zjm999 ~]# 

ps -ef | grep sshd:查看sshd的父进程信息

[root@zjm999 ~]# ps -ef | grep sshd
root       1115      1  0 15:34 ?        00:00:00 /usr/sbin/sshd -D
root       1853   1115  0 15:35 ?        00:00:00 sshd: root@pts/0
root       2494   1865  0 16:26 pts/0    00:00:00 grep --color=auto sshd
[root@zjm999 ~]# 
  • UID: 进程所有者的用户 ID(User ID)。
  • PID: 进程的 ID(Process ID)。
  • PPID: 父进程的 ID(Parent Process ID)。
  • C: 进程的 CPU 使用率(通常表示进程的 CPU 时间百分比,但在某些工具中可能表示其他信息)。
  • STIME: 进程启动的时间。
  • TTY: 进程关联的终端(Terminal)。? 表示进程没有关联的终端。
  • TIME: 进程占用的 CPU 时间。
  • CMD: 启动进程的命令及其参数。

终止进程

若是某个进程执行一半需要停止时,或是已消了很大的系统资源时,此时可以考虑停止该进程。使用kill
命令来完成此项任务。

kill [选项] 进程号(功能描述:通过进程号杀死/终止进程)

killall 进程名称(功能描述:通过进程名称杀死进程,也支持通配符,这在系统因负载过大而变得很慢时很有用)

常用选项
-9:表示强迫进程立即停止

实践:

案例1:踢掉某个非法登录用户

[root@zjm999 ~]# ps -aux | grep sshd
root       1115  0.0  0.2 112900  4308 ?        Ss   15:34   0:00 /usr/sbin/sshd -D
root       1853  0.0  0.2 160988  5636 ?        Ss   15:35   0:00 sshd: root@pts/0
root       3499  0.2  0.2 160988  5568 ?        Ss   16:55   0:00 sshd: zjm [priv]
zjm        3503  0.0  0.1 160988  2376 ?        S    16:56   0:00 sshd: zjm@pts/1
root       3584  0.0  0.0 112828   976 pts/0    S+   16:57   0:00 grep --color=auto sshd
[root@zjm999 ~]# kill 3499

案例2:终止远程登录服务sshd,在适当时候再次重启sshd服务

[root@zjm999 ~]# ps -aux | grep sshd
root       1115  0.0  0.2 112900  4308 ?        Ss   15:34   0:00 /usr/sbin/sshd -D
root       1853  0.0  0.2 160988  5636 ?        Ss   15:35   0:00 sshd: root@pts/0
root       3620  0.0  0.0 112828   976 pts/0    S+   17:00   0:00 grep --color=auto sshd
[root@zjm999 ~]# kill 1115
[root@zjm999 ~]# ps -aux | grep sshd
root       1853  0.0  0.2 160988  5636 ?        Ss   15:35   0:00 sshd: root@pts/0
root       3631  0.0  0.0 112828   976 pts/0    S+   17:00   0:00 grep --color=auto sshd
[root@zjm999 ~]# 
[root@zjm999 ~]# /bin/systemctl start sshd.service
[root@zjm999 ~]# 

案例3:终止多个gedit

[root@zjm999 ~]# killall gedit
[root@zjm999 ~]# 

案例4:强制杀掉一个终端

[root@zjm999 ~]# ps -aux |grep bash
root        742  0.0  0.0 115408   948 ?        S    15:34   0:00 /bin/bash /usr/sbin/ksmtuned
root       1865  0.0  0.1 118844  3364 pts/0    Ss+  15:35   0:00 -bash
root       2782  0.0  0.0  72472   776 ?        Ss   16:37   0:00 /usr/bin/ssh-agent /bin/sh -c exec -l /bin/bash -c "env GNOME_SHELL_SESSION_MODE=classic gnome-session --session gnome-classic"
root       3792  0.1  0.1 116976  3344 pts/1    Ss   17:07   0:00 bash
root       3846  0.0  0.0 112828   976 pts/1    S+   17:07   0:00 grep --color=auto bash
[root@zjm999 ~]# kill -9 3792

查看进程树pstree

pstree [选项] 可以更加直观的来看进程信息

-p:显示进程的PID

-u:显示进程的所属用户

案例1:请你树状的形式显示进程的pid

pstree -p

案例2:请你树状的形式进程的用户id

pstree -u
[root@zjm999 ~]# pstree
systemd─┬─ModemManager───2*[{ModemManager}]
        ├─NetworkManager─┬─dhclient
        │                └─2*[{NetworkManager}]
        ├─VGAuthService
        ├─2*[abrt-watch-log]
        ├─abrtd
        ├─accounts-daemon───2*[{accounts-daemon}]
        ├─alsactl
        ├─at-spi-bus-laun─┬─dbus-daemon───{dbus-daemon}
        │                 └─3*[{at-spi-bus-laun}]
        ├─at-spi2-registr───2*[{at-spi2-registr}]
        ├─atd
        ├─auditd─┬─audispd─┬─sedispatch
        │        │         └─{audispd}
        │        └─{auditd}
        ├─avahi-daemon───avahi-daemon
        ├─boltd───2*[{boltd}]
        ├─chronyd
        ├─colord───2*[{colord}]
        ├─crond
        ├─cupsd
        ├─2*[dbus-daemon───{dbus-daemon}]
        ├─dbus-launch
        ├─dconf-service───2*[{dconf-service}]
        ├─dnsmasq───dnsmasq
        ├─evolution-addre─┬─evolution-addre───5*[{evolution-addre}]
        │                 └─4*[{evolution-addre}]
        ├─evolution-calen─┬─evolution-calen───8*[{evolution-calen}]
        │                 └─4*[{evolution-calen}]
        ├─evolution-sourc───3*[{evolution-sourc}]
        ├─firewalld───{firewalld}
        ├─fwupd───4*[{fwupd}]
        ├─gdm─┬─X───9*[{X}]
        │     ├─gdm-session-wor─┬─gnome-session-b─┬─abrt-applet───2*[{abrt-applet}]
        │     │                 │                 ├─gnome-shell─┬─ibus-daemon─┬─ibus-dconf───3*[{ibus-dconf}]
        │     │                 │                 │             │             ├─ibus-engine-lib───2*[{ibus-engine-lib}]
        │     │                 │                 │             │             ├─ibus-engine-sim───2*[{ibus-engine-sim}]
        │     │                 │                 │             │             └─2*[{ibus-daemon}]
        │     │                 │                 │             └─32*[{gnome-shell}]
        │     │                 │                 ├─gnome-software───3*[{gnome-software}]
        │     │                 │                 ├─gsd-a11y-settin───3*[{gsd-a11y-settin}]
        │     │                 │                 ├─gsd-account───3*[{gsd-account}]
        │     │                 │                 ├─gsd-clipboard───2*[{gsd-clipboard}]
        │     │                 │                 ├─gsd-color───3*[{gsd-color}]
        │     │                 │                 ├─gsd-datetime───3*[{gsd-datetime}]
        │     │                 │                 ├─gsd-disk-utilit───2*[{gsd-disk-utilit}]
        │     │                 │                 ├─gsd-housekeepin───3*[{gsd-housekeepin}]
        │     │                 │                 ├─gsd-keyboard───3*[{gsd-keyboard}]
        │     │                 │                 ├─gsd-media-keys───3*[{gsd-media-keys}]
        │     │                 │                 ├─gsd-mouse───3*[{gsd-mouse}]
        │     │                 │                 ├─gsd-power───3*[{gsd-power}]
        │     │                 │                 ├─gsd-print-notif───2*[{gsd-print-notif}]
        │     │                 │                 ├─gsd-rfkill───2*[{gsd-rfkill}]
        │     │                 │                 ├─gsd-screensaver───2*[{gsd-screensaver}]
        │     │                 │                 ├─gsd-sharing───3*[{gsd-sharing}]
        │     │                 │                 ├─gsd-smartcard───4*[{gsd-smartcard}]
        │     │                 │                 ├─gsd-sound───3*[{gsd-sound}]
        │     │                 │                 ├─gsd-wacom───2*[{gsd-wacom}]
        │     │                 │                 ├─gsd-xsettings───3*[{gsd-xsettings}]
        │     │                 │                 ├─nautilus-deskto───3*[{nautilus-deskto}]
        │     │                 │                 ├─seapplet
        │     │                 │                 ├─ssh-agent
        │     │                 │                 ├─tracker-extract───13*[{tracker-extract}]
        │     │                 │                 ├─tracker-miner-a───3*[{tracker-miner-a}]
        │     │                 │                 ├─tracker-miner-f───3*[{tracker-miner-f}]
        │     │                 │                 ├─tracker-miner-u───3*[{tracker-miner-u}]
        │     │                 │                 └─3*[{gnome-session-b}]
        │     │                 └─2*[{gdm-session-wor}]
        │     └─3*[{gdm}]
        ├─gnome-keyring-d───3*[{gnome-keyring-d}]
        ├─gnome-shell-cal───5*[{gnome-shell-cal}]
        ├─goa-daemon───3*[{goa-daemon}]
        ├─goa-identity-se───3*[{goa-identity-se}]
        ├─gsd-printer───2*[{gsd-printer}]
        ├─gssproxy───5*[{gssproxy}]
        ├─gvfs-afc-volume───3*[{gvfs-afc-volume}]
        ├─gvfs-goa-volume───2*[{gvfs-goa-volume}]
        ├─gvfs-gphoto2-vo───2*[{gvfs-gphoto2-vo}]
        ├─gvfs-mtp-volume───2*[{gvfs-mtp-volume}]
        ├─gvfs-udisks2-vo───2*[{gvfs-udisks2-vo}]
        ├─gvfsd─┬─gvfsd-burn───2*[{gvfsd-burn}]
        │       ├─gvfsd-trash───2*[{gvfsd-trash}]
        │       └─2*[{gvfsd}]
        ├─gvfsd-fuse───5*[{gvfsd-fuse}]
        ├─gvfsd-metadata───2*[{gvfsd-metadata}]
        ├─ibus-portal───2*[{ibus-portal}]
        ├─ibus-x11───2*[{ibus-x11}]
        ├─imsettings-daem───3*[{imsettings-daem}]
        ├─irqbalance
        ├─ksmtuned───sleep
        ├─libvirtd───16*[{libvirtd}]
        ├─lsmd
        ├─lvmetad
        ├─master─┬─pickup
        │        └─qmgr
        ├─mission-control───3*[{mission-control}]
        ├─packagekitd───2*[{packagekitd}]
        ├─polkitd───6*[{polkitd}]
        ├─pulseaudio───2*[{pulseaudio}]
        ├─rngd
        ├─rpcbind
        ├─rsyslogd───2*[{rsyslogd}]
        ├─rtkit-daemon───2*[{rtkit-daemon}]
        ├─smartd
        ├─sshd───bash───pstree
        ├─sshd
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        ├─tracker-store───7*[{tracker-store}]
        ├─tuned───4*[{tuned}]
        ├─udisksd───4*[{udisksd}]
        ├─upowerd───2*[{upowerd}]
        ├─vmtoolsd───{vmtoolsd}
        ├─vmtoolsd───3*[{vmtoolsd}]
        ├─wpa_supplicant
        └─xdg-permission-───2*[{xdg-permission-}]
[root@zjm999 ~]# 

服务管理

介绍

服务(service)本质就是进程,但是是运行在后台的,通常都会监听某个端口,等待其它程序的请求,比如(mysqld,sshd防火墙等),因此我们又称为守护进程,是Linux中非常重要的知识点。

service管理指令

1.service 服务名[start|stop|restart|reload|status]

2.在Centos7.0后很多服务不再使用service,而是systemctl(后面专门讲)

3.service指令管理的服务在/etc/init.d查看

[root@zjm999 ~]# ls -l /etc/init.d/
总用量 40
-rw-r--r--. 1 root root 18281 5月  22 2020 functions
-rwxr-xr-x. 1 root root  4569 5月  22 2020 netconsole
-rwxr-xr-x. 1 root root  7928 5月  22 2020 network
-rw-r--r--. 1 root root  1160 10月  2 2020 README
[root@zjm999 ~]# 
[root@zjm999 ~]# service network status
已配置设备:
lo ens33
当前活跃设备:
lo ens33 virbr0
[root@zjm999 ~]# 

service管理指令案例

请使用service指令,查看,关闭,启动network[注意:在虚拟系统演示,因为网络连接会关闭]

[root@zjm999 ~]# service network status
已配置设备:
lo ens33
当前活跃设备:
lo ens33 virbr0
[root@zjm999 ~]# service network stop
Stopping network (via systemctl):                          [  确定  ]
[root@zjm999 ~]# service network status
已配置设备:
lo ens33
当前活跃设备:
ens33 virbr0
[root@zjm999 ~]# service network start
Starting network (via systemctl):                          [  确定  ]
[root@zjm999 ~]# service network status
已配置设备:
lo ens33
当前活跃设备:
lo ens33 virbr0
[root@zjm999 ~]# 

查看服务名

方式1:使用setup->系统服务就可以看到全部。
setup

方式2:/etc/init.d看到service指令管理的服务

Is -l /etc/init.d

服务的运行级别(runlevel)

Linux系统有7种运行级别(runlevel):常用的是级别3和5

运行级别0:系统停机状态,系统默认运行级别不能设为0,否则不能正常启动

运行级别1:单用户工作状态,root权限,用于系统维护,禁止远程登陆

运行级别2:多用户状态(没有NFS),不支持网络

运行级别3:完全的多用户状态(有NFS),登陆后进入控制台命令行模式

运行级别4:系统未使用,保留

运行级别5:X11控制台,登陆后进入图形GUI模式

运行级别6:系统正常关闭并重启,默认运行级别不能设为6,否则不能正常启动

开机的流程说明:

开机—>BIOS—>/boot—>systemd进程1—>运行级别–>运行级对应的服务

[root@zjm999 ~]# systemctl get-default
graphical.target
[root@zjm999 ~]# 

chkconfig指令

介绍

1.通过chkconfig命令可以给服务的各个运行级别设置自启动/关闭

2.chkconfig指令管理的服务在/etc/init.d查看

3.注意:Centos7.0后,很多服务使用systemctl管理(后面马上讲)

chkconfig基本语法

查看服务chkconfig --list [| grep xxx]

chkconfig 服务名 --list

chkconfig --level 5 服务名 on/off

案例演示:对network服务进行各种操作,把network在3运行级别,关闭自启动

chkconfig --level 3 network off

chkconfig --level 3 network on

使用细节

chkconfig重新设置服务后自启动或关闭,需要重启机器reboot生效.

[root@zjm999 ~]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

netconsole     	0:关	1:关	2:关	3:关	4:关	5:关	6:关
network        	0:关	1:关	2:开	3:开	4:开	5:开	6:关
[root@zjm999 ~]# chkconfig --list | grep network

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

network        	0:关	1:关	2:开	3:开	4:开	5:开	6:关
[root@zjm999 ~]# 

systemctl管理指令

1.基本语法:systemctl [start|stop|restart|status] 服务名

2.systemctl指令管理的服务在/usr/lib/systemd/system查看

[root@zjm999 ~]# ls -l /usr/lib/systemd/system
总用量 1612
-rw-r--r--. 1 root root  275 10月  2 2020 abrt-ccpp.service
-rw-r--r--. 1 root root  380 10月  2 2020 abrtd.service
-rw-r--r--. 1 root root  361 10月  2 2020 abrt-oops.service
-rw-r--r--. 1 root root  266 10月  2 2020 abrt-pstoreoops.service
-rw-r--r--. 1 root root  262 10月  2 2020 abrt-vmcore.service
-rw-r--r--. 1 root root  311 10月  2 2020 abrt-xorg.service
-rw-r--r--. 1 root root  729 4月   1 2020 accounts-daemon.service
-rw-r--r--. 1 root root  569 8月   6 2019 alsa-restore.service
-rw-r--r--. 1 root root  465 8月   6 2019 alsa-state.service
-rw-r--r--. 1 root root  682 10月  2 2020 anaconda-direct.service
-rw-r--r--. 1 root root  185 10月  2 2020 anaconda-nm-config.service
-rw-r--r--. 1 root root  660 10月  2 2020 anaconda-noshell.service
-rw-r--r--. 1 root root  585 10月  2 2020 anaconda-pre.service
-rw-r--r--. 1 root root  442 10月  2 2020 anaconda.service
-rw-r--r--. 1 root root  532 10月  2 2020 [email protected]
-rw-r--r--. 1 root root  574 10月  2 2020 anaconda-sshd.service
-rw-r--r--. 1 root root  415 10月  2 2020 anaconda.target
-rw-r--r--. 1 root root  498 10月  2 2020 [email protected]
-rw-r--r--. 1 root root  275 8月   9 2019 arp-ethers.service
-rw-r--r--. 1 root root  222 10月 31 2018 atd.service
-rw-r--r--. 1 root root 1384 8月   8 2019 auditd.service
-rw-r--r--. 1 root root  646 10月  1 2020 auth-rpcgss-module.service
-rw-r--r--. 1 root root  484 9月  30 2020 autofs.service
lrwxrwxrwx. 1 root root   14 12月  3 09:55 [email protected] -> [email protected]
-rw-r--r--. 1 root root 1044 4月   1 2020 avahi-daemon.service
-rw-r--r--. 1 root root  874 4月   1 2020 avahi-daemon.socket
-rw-r--r--. 1 root root  517 10月  2 2020 basic.target
drwxr-xr-x. 2 root root 4096 12月  3 10:00 basic.target.wants
-r--r--r--. 1 root root  429 10月  1 2020 blk-availability.service
-rw-r--r--. 1 root root  424 9月  30 2020 bluetooth.service
-rw-r--r--. 1 root root  379 10月  2 2020 bluetooth.target
-rw-r--r--. 1 root root  401 8月   9 2019 bolt.service
-rw-r--r--. 1 root root  160 5月  22 2020 brandbot.path
-rw-r--r--. 1 root root  116 5月  22 2020 brandbot.service
-rw-r--r--. 1 root root  144 4月  11 2018 brltty.service
-rw-r--r--. 1 root root  491 4月   1 2020 canberra-system-bootup.service
-rw-r--r--. 1 root root  509 4月   1 2020 canberra-system-shutdown-reboot.service
-rw-r--r--. 1 root root  466 4月   1 2020 canberra-system-shutdown.service
-rw-r--r--. 1 root root  349 9月  30 2020 certmonger.service
-rw-r--r--. 1 root root  479 8月   9 2019 cgconfig.service
-rw-r--r--. 1 root root  135 11月  5 2016 cgdcbxd.service
-rw-r--r--. 1 root root  232 8月   9 2019 cgred.service
-rw-r--r--. 1 root root  209 8月   6 2019 [email protected]
-rw-r--r--. 1 root root  138 8月   6 2019 [email protected]
-rw-r--r--. 1 root root  495 8月   8 2019 chronyd.service
-rw-r--r--. 1 root root  472 9月  19 2018 chrony-wait.service
-rw-r--r--. 1 root root  169 4月   1 2020 [email protected]
-rw-r--r--. 1 root root  295 4月   1 2020 colord.service
-rw-r--r--. 1 root root  154 4月   1 2020 [email protected]
-rw-r--r--. 1 root root  787 10月  2 2020 console-getty.service
-rw-r--r--. 1 root root  749 10月  2 2020 console-shell.service
-rw-r--r--. 1 root root  808 10月  2 2020 [email protected]
-rw-r--r--. 1 root root  294 10月 20 2020 cpupower.service
-rw-r--r--. 1 root root  318 8月   9 2019 crond.service
-rw-r--r--. 1 root root  425 10月  2 2020 cryptsetup-pre.target
-rw-r--r--. 1 root root  372 10月  2 2020 cryptsetup.target
lrwxrwxrwx. 1 root root   13 12月  3 09:55 ctrl-alt-del.target -> reboot.target
-rw-r--r--. 1 root root  234 2月  25 2019 cups-browsed.service
-r--r--r--. 1 root root  126 9月  30 2020 cups.path
-r--r--r--. 1 root root  213 9月  30 2020 cups.service
-r--r--r--. 1 root root  131 9月  30 2020 cups.socket
lrwxrwxrwx. 1 root root   25 12月  3 09:55 dbus-org.freedesktop.hostname1.service -> systemd-hostnamed.service
lrwxrwxrwx. 1 root root   23 12月  3 09:55 dbus-org.freedesktop.import1.service -> systemd-importd.service
lrwxrwxrwx. 1 root root   23 12月  3 09:55 dbus-org.freedesktop.locale1.service -> systemd-localed.service
lrwxrwxrwx. 1 root root   22 12月  3 09:55 dbus-org.freedesktop.login1.service -> systemd-logind.service
lrwxrwxrwx. 1 root root   24 12月  3 09:55 dbus-org.freedesktop.machine1.service -> systemd-machined.service
lrwxrwxrwx. 1 root root   25 12月  3 09:55 dbus-org.freedesktop.timedate1.service -> systemd-timedated.service
-rw-r--r--. 1 root root  366 9月  30 2020 dbus.service
-rw-r--r--. 1 root root  102 9月  30 2020 dbus.socket
drwxr-xr-x. 2 root root 4096 10月  2 2020 dbus.target.wants
-rw-r--r--. 1 root root 1014 10月  2 2020 debug-shell.service
lrwxrwxrwx. 1 root root   16 12月  3 09:55 default.target -> graphical.target
drwxr-xr-x. 2 root root 4096 10月  2 2020 default.target.wants
-rw-r--r--. 1 root root  670 10月  2 2020 dev-hugepages.mount
-rw-r--r--. 1 root root  590 10月  2 2020 dev-mqueue.mount
-r--r--r--. 1 root root  345 10月  1 2020 dm-event.service
-r--r--r--. 1 root root  248 10月  1 2020 dm-event.socket
-r--r--r--. 1 root root  355 11月  6 2016 dmraid-activation.service
-rw-r--r--. 1 root root  140 10月 13 2020 dnsmasq.service
lrwxrwxrwx. 1 root root   55 12月  3 09:55 dracut-cmdline.service -> ../../dracut/modules.d/98systemd/dracut-cmdline.service
lrwxrwxrwx. 1 root root   57 12月  3 09:55 dracut-initqueue.service -> ../../dracut/modules.d/98systemd/dracut-initqueue.service
lrwxrwxrwx. 1 root root   53 12月  3 09:55 dracut-mount.service -> ../../dracut/modules.d/98systemd/dracut-mount.service
lrwxrwxrwx. 1 root root   57 12月  3 09:55 dracut-pre-mount.service -> ../../dracut/modules.d/98systemd/dracut-pre-mount.service
lrwxrwxrwx. 1 root root   57 12月  3 09:55 dracut-pre-pivot.service -> ../../dracut/modules.d/98systemd/dracut-pre-pivot.service
lrwxrwxrwx. 1 root root   59 12月  3 09:55 dracut-pre-trigger.service -> ../../dracut/modules.d/98systemd/dracut-pre-trigger.service
lrwxrwxrwx. 1 root root   56 12月  3 09:55 dracut-pre-udev.service -> ../../dracut/modules.d/98systemd/dracut-pre-udev.service
lrwxrwxrwx. 1 root root   56 12月  3 09:55 dracut-shutdown.service -> ../../dracut/modules.d/98systemd/dracut-shutdown.service
-rw-r--r--. 1 root root  208 4月  11 2018 ebtables.service
-rw-r--r--. 1 root root  976 10月  2 2020 emergency.service
-rw-r--r--. 1 root root  431 10月  2 2020 emergency.target
-rw-r--r--. 1 root root  282 3月  14 2019 fcoe.service
-rw-r--r--. 1 root root  440 10月  2 2020 final.target
-rw-r--r--. 1 root root  657 10月  1 2020 firewalld.service
-rw-r--r--. 1 root root  469 6月  17 2016 firstboot-graphical.service
-rw-r--r--. 1 root root  263 3月  18 2020 flatpak-system-helper.service
-rw-r--r--. 1 root root  368 10月 31 2018 fprintd.service
-rw-r--r--. 1 root root   95 10月  1 2020 fstrim.service
-rw-r--r--. 1 root root  174 10月  1 2020 fstrim.timer
-rw-r--r--. 1 root root  121 7月  29 2020 fwupdate-cleanup.service
-rw-r--r--. 1 root root  211 4月   1 2020 fwupd-offline-update.service
-rw-r--r--. 1 root root  398 4月   1 2020 fwupd.service
-rw-r--r--. 1 root root 1012 10月  1 2020 gdm.service
-rw-r--r--. 1 root root  146 10月 31 2018 geoclue.service
-rw-r--r--. 1 root root  466 10月  2 2020 getty-pre.target
-rw-r--r--. 1 root root 1553 10月  2 2020 [email protected]
-rw-r--r--. 1 root root  460 10月  2 2020 getty.target
-rw-r--r--. 1 root root  558 10月  2 2020 graphical.target
drwxr-xr-x. 2 root root 4096 12月  3 09:55 graphical.target.wants
-rw-r--r--. 1 root root  479 10月  1 2020 gssproxy.service
-rw-r--r--. 1 root root  565 10月  2 2020 halt-local.service
-rw-r--r--. 1 root root  487 10月  2 2020 halt.target
drwxr-xr-x. 2 root root 4096 12月  3 09:56 halt.target.wants
-rw-r--r--. 1 root root  447 10月  2 2020 hibernate.target
-rw-r--r--. 1 root root  468 10月  2 2020 hybrid-sleep.target
-rw-r--r--. 1 root root  145 10月 30 2018 hypervfcopyd.service
-rw-r--r--. 1 root root  172 10月 30 2018 hypervkvpd.service
-rw-r--r--. 1 root root  139 10月 30 2018 hypervvssd.service
-rw-r--r--. 1 root root  783 9月   9 2019 initial-setup-graphical.service
-rw-r--r--. 1 root root 1096 9月   9 2019 initial-setup-reconfiguration.service
-rw-r--r--. 1 root root 1071 9月   9 2019 initial-setup.service
-rw-r--r--. 1 root root  773 9月   9 2019 initial-setup-text.service
-rw-r--r--. 1 root root  634 10月  2 2020 initrd-cleanup.service
-rw-r--r--. 1 root root  553 10月  2 2020 initrd-fs.target
-rw-r--r--. 1 root root  802 10月  2 2020 initrd-parse-etc.service
-rw-r--r--. 1 root root  526 10月  2 2020 initrd-root-fs.target
-rw-r--r--. 1 root root  630 10月  2 2020 initrd-switch-root.service
-rw-r--r--. 1 root root  737 10月  2 2020 initrd-switch-root.target
drwxr-xr-x. 2 root root 4096 12月  3 09:56 initrd-switch-root.target.wants
-rw-r--r--. 1 root root  671 10月  2 2020 initrd.target
drwxr-xr-x. 2 root root 4096 12月  3 09:55 initrd.target.wants
-rw-r--r--. 1 root root  668 10月  2 2020 initrd-udevadm-cleanup-db.service
-rw-r--r--. 1 root root  163 10月  2 2020 instperf.service
-rw-r--r--. 1 root root  184 4月   1 2020 iprdump.service
-rw-r--r--. 1 root root  143 4月   1 2020 iprinit.service
-rw-r--r--. 1 root root  147 4月   1 2020 iprupdate.service
-rw-r--r--. 1 root root  173 4月   1 2020 iprutils.target
-rw-r--r--. 1 root root 1344 10月 19 2019 ipsec.service
-rw-r--r--. 1 root root  209 8月   9 2019 irqbalance.service
-rw-r--r--. 1 root root  337 10月  1 2020 iscsid.service
-rw-r--r--. 1 root root  175 10月  1 2020 iscsid.socket
-rw-r--r--. 1 root root  366 10月  1 2020 iscsi-onboot.service
-rw-r--r--. 1 root root  564 10月  1 2020 iscsi.service
-rw-r--r--. 1 root root  425 10月  1 2020 iscsi-shutdown.service
-rw-r--r--. 1 root root  350 10月  1 2020 iscsiuio.service
-rw-r--r--. 1 root root  165 10月  1 2020 iscsiuio.socket
-rw-r--r--. 1 root root  349 10月  2 2020 kdump.service
-rw-r--r--. 1 root root  501 10月  2 2020 kexec.target
drwxr-xr-x. 2 root root 4096 12月  3 09:56 kexec.target.wants
-rw-r--r--. 1 root root  679 10月  2 2020 kmod-static-nodes.service
-rw-r--r--. 1 root root  213 4月   1 2020 kpatch.service
-rw-r--r--. 1 root root  270 9月  30 2020 ksm.service
-rw-r--r--. 1 root root  229 9月  30 2020 ksmtuned.service
-rw-r--r--. 1 root root  198 4月   1 2020 libstoragemgmt.service
-rw-r--r--. 1 root root 1470 10月 14 2020 libvirtd.service
-rw-r--r--. 1 root root  243 8月   9 2019 lldpad.service
-rw-r--r--. 1 root root   99 8月   9 2019 lldpad.socket
-rw-r--r--. 1 root root  395 10月  2 2020 local-fs-pre.target
-rw-r--r--. 1 root root  507 10月  2 2020 local-fs.target
drwxr-xr-x. 2 root root 4096 12月  3 09:55 local-fs.target.wants
-r--r--r--. 1 root root  337 10月  1 2020 lvm2-lvmetad.service
-r--r--r--. 1 root root  241 10月  1 2020 lvm2-lvmetad.socket
-r--r--r--. 1 root root  327 10月  1 2020 lvm2-lvmpolld.service
-r--r--r--. 1 root root  239 10月  1 2020 lvm2-lvmpolld.socket
-r--r--r--. 1 root root  682 10月  1 2020 lvm2-monitor.service
-r--r--r--. 1 root root  411 10月  1 2020 [email protected]
-rw-r--r--. 1 root root  405 10月  2 2020 machine.slice
-rw-r--r--. 1 root root  531 10月  2 2020 machines.target
-rw-r--r--. 1 root root  485 10月  1 2020 [email protected]
-rw-r--r--. 1 root root  214 10月  1 2020 [email protected]
-rw-r--r--. 1 root root  179 10月  1 2020 [email protected]
-rw-r--r--. 1 root root  571 10月  1 2020 mdcheck_continue.service
-rw-r--r--. 1 root root  400 10月  1 2020 mdcheck_continue.timer
-rw-r--r--. 1 root root  521 10月  1 2020 mdcheck_start.service
-rw-r--r--. 1 root root  426 10月  1 2020 mdcheck_start.timer
-rw-r--r--. 1 root root  525 10月  1 2020 mdmonitor-oneshot.service
-rw-r--r--. 1 root root  398 10月  1 2020 mdmonitor-oneshot.timer
-rw-r--r--. 1 root root  382 10月  1 2020 mdmonitor.service
-rw-r--r--. 1 root root 1038 10月  1 2020 [email protected]
lrwxrwxrwx. 1 root root   12 12月  3 09:55 messagebus.service -> dbus.service
-rw-r--r--. 1 root root  284 10月  1 2020 microcode.service
-rw-r--r--. 1 root root  258 10月  1 2020 ModemManager.service
-rw-r--r--. 1 root root  623 9月  30 2020 multipathd.service
-rw-r--r--. 1 root root  492 10月  2 2020 multi-user.target
drwxr-xr-x. 2 root root 4096 12月  3 09:56 multi-user.target.wants
-rw-r--r--. 1 root root  134 4月   1 2020 ndctl-monitor.service
-rw-r--r--. 1 root root  350 8月   3 2017 netcf-transaction.service
-rw-r--r--. 1 root root  353 10月  1 2020 NetworkManager-dispatcher.service
-rw-r--r--. 1 root root 1445 10月  1 2020 NetworkManager.service
-rw-r--r--. 1 root root  303 10月  1 2020 NetworkManager-wait-online.service
-rw-r--r--. 1 root root  464 10月  2 2020 network-online.target
-rw-r--r--. 1 root root  461 10月  2 2020 network-pre.target
-rw-r--r--. 1 root root  480 10月  2 2020 network.target
-rw-r--r--. 1 root root  350 10月  1 2020 nfs-blkmap.service
-rw-r--r--. 1 root root  413 10月  1 2020 nfs-client.target
-rw-r--r--. 1 root root  375 10月  1 2020 nfs-config.service
-rw-r--r--. 1 root root  330 10月  1 2020 nfs-idmapd.service
lrwxrwxrwx. 1 root root   18 12月  3 09:59 nfs-idmap.service -> nfs-idmapd.service
lrwxrwxrwx. 1 root root   17 12月  3 09:59 nfs-lock.service -> rpc-statd.service
lrwxrwxrwx. 1 root root   17 12月  3 09:59 nfslock.service -> rpc-statd.service
-rw-r--r--. 1 root root  395 10月  1 2020 nfs-mountd.service
lrwxrwxrwx. 1 root root   19 12月  3 09:59 nfs-rquotad.service -> rpc-rquotad.service
lrwxrwxrwx. 1 root root   16 12月  3 09:59 nfs-secure.service -> rpc-gssd.service
-rw-r--r--. 1 root root 1044 10月  1 2020 nfs-server.service
lrwxrwxrwx. 1 root root   18 12月  3 09:59 nfs.service -> nfs-server.service
-rw-r--r--. 1 root root  567 10月  1 2020 nfs-utils.service
-rw-r--r--. 1 root root  514 10月  2 2020 nss-lookup.target
-rw-r--r--. 1 root root  473 10月  2 2020 nss-user-lookup.target
-rw-r--r--. 1 root root  272 11月 28 2019 ntpdate.service
-rw-r--r--. 1 root root  249 11月 28 2019 ntpd.service
-rw-r--r--. 1 root root  232 12月  3 2012 numad.service
-rw-r--r--. 1 root root  271 3月   6 2015 oddjobd.service
-rw-r--r--. 1 root root  375 4月   2 2020 packagekit-offline-update.service
-rw-r--r--. 1 root root  364 4月   2 2020 packagekit.service
-rw-r--r--. 1 root root  354 10月  2 2020 paths.target
-rw-r--r--. 1 root root  381 10月  2 2020 plymouth-halt.service
-rw-r--r--. 1 root root  396 10月  2 2020 plymouth-kexec.service
-rw-r--r--. 1 root root  393 10月  2 2020 plymouth-poweroff.service
-rw-r--r--. 1 root root  235 10月  2 2020 plymouth-quit.service
-rw-r--r--. 1 root root  243 10月  2 2020 plymouth-quit-wait.service
-rw-r--r--. 1 root root  282 10月  2 2020 plymouth-read-write.service
-rw-r--r--. 1 root root  386 10月  2 2020 plymouth-reboot.service
-rw-r--r--. 1 root root  546 10月  2 2020 plymouth-start.service
-rw-r--r--. 1 root root  295 10月  2 2020 plymouth-switch-root.service
-rw-r--r--. 1 root root  172 4月   1 2020 polkit.service
-rw-r--r--. 1 root root  463 4月   1 2020 postfix.service
-rw-r--r--. 1 root root  552 10月  2 2020 poweroff.target
drwxr-xr-x. 2 root root 4096 12月  3 09:56 poweroff.target.wants
-rw-r--r--. 1 root root  377 10月  2 2020 printer.target
-rw-r--r--. 1 root root   98 10月  1 2020 proc-fs-nfsd.mount
-rw-r--r--. 1 root root  705 10月  2 2020 proc-sys-fs-binfmt_misc.automount
-rw-r--r--. 1 root root  615 10月  2 2020 proc-sys-fs-binfmt_misc.mount
-rw-r--r--. 1 root root  303 8月   3 2017 psacct.service
-rw-r--r--. 1 root root  523 8月   8 2019 qemu-guest-agent.service
-rw-r--r--. 1 root root  644 10月  2 2020 quotaon.service
-rw-r--r--. 1 root root  279 10月 31 2018 radvd.service
-rw-r--r--. 1 root root  632 10月  2 2020 rc-local.service
-rw-r--r--. 1 root root  243 8月   4 2017 rdisc.service
-rw-r--r--. 1 root root  585 10月 14 2020 rdma-hw.target
-rw-r--r--. 1 root root  886 10月 14 2020 [email protected]
-rw-r--r--. 1 root root  843 10月 14 2020 rdma-ndd.service
-rw-r--r--. 1 root root  370 10月 14 2020 rdma.service
-rw-r--r--. 1 root root  164 10月  1 2020 realmd.service
-rw-r--r--. 1 root root  543 10月  2 2020 reboot.target
drwxr-xr-x. 2 root root 4096 12月  3 09:56 reboot.target.wants
-rw-r--r--. 1 root root  509 10月  2 2020 remote-cryptsetup.target
-rw-r--r--. 1 root root  396 10月  2 2020 remote-fs-pre.target
-rw-r--r--. 1 root root  482 10月  2 2020 remote-fs.target
-rw-r--r--. 1 root root  976 10月  2 2020 rescue.service
-rw-r--r--. 1 root root  486 10月  2 2020 rescue.target
drwxr-xr-x. 2 root root 4096 12月  3 09:55 rescue.target.wants
-rw-r--r--. 1 root root  410 5月  22 2020 rhel-autorelabel-mark.service
-rw-r--r--. 1 root root  459 5月  22 2020 rhel-autorelabel.service
-rw-r--r--. 1 root root  408 5月  22 2020 rhel-configure.service
-rw-r--r--. 1 root root  217 5月  22 2020 rhel-dmesg.service
-rw-r--r--. 1 root root  331 5月  22 2020 rhel-domainname.service
-rw-r--r--. 1 root root  450 5月  22 2020 rhel-import-state.service
-rw-r--r--. 1 root root  437 5月  22 2020 rhel-loadmodules.service
-rw-r--r--. 1 root root  401 5月  22 2020 rhel-readonly.service
-rw-r--r--. 1 root root  129 4月   1 2020 rngd.service
-rw-r--r--. 1 root root  385 4月   1 2020 rpcbind.service
-rw-r--r--. 1 root root  132 4月   1 2020 rpcbind.socket
-rw-r--r--. 1 root root  500 10月  2 2020 rpcbind.target
-rw-r--r--. 1 root root  402 10月  1 2020 rpc-gssd.service
lrwxrwxrwx. 1 root root   16 12月  3 09:59 rpcgssd.service -> rpc-gssd.service
lrwxrwxrwx. 1 root root   18 12月  3 09:59 rpcidmapd.service -> nfs-idmapd.service
-rw-r--r--. 1 root root   80 10月  1 2020 rpc_pipefs.target
-rw-r--r--. 1 root root  324 8月   6 2019 rpc-rquotad.service
-rw-r--r--. 1 root root  558 10月  1 2020 rpc-statd-notify.service
-rw-r--r--. 1 root root  486 10月  1 2020 rpc-statd.service
-rw-r--r--. 1 root root  237 4月   1 2020 rsyncd.service
-rw-r--r--. 1 root root  220 4月   1 2020 [email protected]
-rw-r--r--. 1 root root  138 4月   1 2020 rsyncd.socket
-rw-r--r--. 1 root root  465 10月 14 2020 rsyslog.service
-rw-r--r--. 1 root root 1053 3月   6 2015 rtkit-daemon.service
lrwxrwxrwx. 1 root root   15 12月  3 09:55 runlevel0.target -> poweroff.target
lrwxrwxrwx. 1 root root   13 12月  3 09:55 runlevel1.target -> rescue.target
drwxr-xr-x. 2 root root 4096 12月  3 09:55 runlevel1.target.wants
lrwxrwxrwx. 1 root root   17 12月  3 09:55 runlevel2.target -> multi-user.target
drwxr-xr-x. 2 root root 4096 12月  3 09:55 runlevel2.target.wants
lrwxrwxrwx. 1 root root   17 12月  3 09:55 runlevel3.target -> multi-user.target
drwxr-xr-x. 2 root root 4096 12月  3 09:55 runlevel3.target.wants
lrwxrwxrwx. 1 root root   17 12月  3 09:55 runlevel4.target -> multi-user.target
drwxr-xr-x. 2 root root 4096 12月  3 09:55 runlevel4.target.wants
lrwxrwxrwx. 1 root root   16 12月  3 09:55 runlevel5.target -> graphical.target
drwxr-xr-x. 2 root root 4096 12月  3 09:55 runlevel5.target.wants
lrwxrwxrwx. 1 root root   13 12月  3 09:55 runlevel6.target -> reboot.target
-rw-r--r--. 1 root root  469 9月  30 2020 run-vmblock\x2dfuse.mount
-rw-r--r--. 1 root root  430 10月 31 2018 [email protected]
-rw-r--r--. 1 root root  132 10月 31 2018 saned.socket
-rw-r--r--. 1 root root  295 4月  11 2018 saslauthd.service
-rw-r--r--. 1 root root  527 9月  30 2020 [email protected]
-rw-r--r--. 1 root root 1055 10月  2 2020 [email protected]
-rw-r--r--. 1 root root  402 10月  2 2020 shutdown.target
drwxr-xr-x. 2 root root 4096 12月  3 09:55 shutdown.target.wants
-rw-r--r--. 1 root root  362 10月  2 2020 sigpwr.target
-rw-r--r--. 1 root root  420 10月  2 2020 sleep.target
-rw-r--r--. 1 root root  403 10月  2 2020 -.slice
-rw-r--r--. 1 root root  409 10月  2 2020 slices.target
-rw-r--r--. 1 root root  380 10月  2 2020 smartcard.target
-rw-r--r--. 1 root root  329 4月   1 2020 smartd.service
-rw-r--r--. 1 root root  356 10月  2 2020 sockets.target
drwxr-xr-x. 2 root root 4096 12月  3 09:55 sockets.target.wants
-rw-r--r--. 1 root root  380 10月  2 2020 sound.target
drwxr-xr-x. 2 root root 4096 12月  3 09:57 sound.target.wants
-rw-r--r--. 1 root root  221 1月  27 2014 speech-dispatcherd.service
-rw-r--r--. 1 root root  351 8月   9 2019 spice-vdagentd.service
-rw-r--r--. 1 root root  132 8月   9 2019 spice-vdagentd.socket
-rw-r--r--. 1 root root  313 8月   9 2019 sshd-keygen.service
-rw-r--r--. 1 root root  373 8月   9 2019 sshd.service
-rw-r--r--. 1 root root  260 8月   9 2019 [email protected]
-rw-r--r--. 1 root root  181 8月   9 2019 sshd.socket
-rw-r--r--. 1 root root  472 10月 16 2020 sssd-autofs.service
-rw-r--r--. 1 root root  371 10月 16 2020 sssd-autofs.socket
-rw-r--r--. 1 root root  351 10月 16 2020 sssd-nss.service
-rw-r--r--. 1 root root  420 10月 16 2020 sssd-nss.socket
-rw-r--r--. 1 root root  460 10月 16 2020 sssd-pac.service
-rw-r--r--. 1 root root  362 10月 16 2020 sssd-pac.socket
-rw-r--r--. 1 root root  443 10月 16 2020 sssd-pam-priv.socket
-rw-r--r--. 1 root root  481 10月 16 2020 sssd-pam.service
-rw-r--r--. 1 root root  391 10月 16 2020 sssd-pam.socket
-rw-r--r--. 1 root root  299 10月 16 2020 sssd-secrets.service
-rw-r--r--. 1 root root  173 10月 16 2020 sssd-secrets.socket
-rw-r--r--. 1 root root  420 10月 16 2020 sssd.service
-rw-r--r--. 1 root root  460 10月 16 2020 sssd-ssh.service
-rw-r--r--. 1 root root  362 10月 16 2020 sssd-ssh.socket
-rw-r--r--. 1 root root  465 10月 16 2020 sssd-sudo.service
-rw-r--r--. 1 root root  364 10月 16 2020 sssd-sudo.socket
-rw-r--r--. 1 root root  441 10月  2 2020 suspend.target
-rw-r--r--. 1 root root  267 9月  30 2020 svnserve.service
-rw-r--r--. 1 root root  353 10月  2 2020 swap.target
-rw-r--r--. 1 root root  681 10月  2 2020 sys-fs-fuse-connections.mount
-rw-r--r--. 1 root root  518 10月  2 2020 sysinit.target
drwxr-xr-x. 2 root root 4096 12月  3 09:56 sysinit.target.wants
-rw-r--r--. 1 root root  719 10月  2 2020 sys-kernel-config.mount
-rw-r--r--. 1 root root  662 10月  2 2020 sys-kernel-debug.mount
-rw-r--r--. 1 root root 1235 10月  2 2020 syslog.socket
drwxr-xr-x. 2 root root 4096 10月  2 2020 syslog.target.wants
-rw-r--r--. 1 root root  421 4月   1 2020 sysstat.service
-rw-r--r--. 1 root root  646 10月  2 2020 systemd-ask-password-console.path
-rw-r--r--. 1 root root  657 10月  2 2020 systemd-ask-password-console.service
-rw-r--r--. 1 root root  419 10月  2 2020 systemd-ask-password-plymouth.path
-rw-r--r--. 1 root root  400 10月  2 2020 systemd-ask-password-plymouth.service
-rw-r--r--. 1 root root  574 10月  2 2020 systemd-ask-password-wall.path
-rw-r--r--. 1 root root  689 10月  2 2020 systemd-ask-password-wall.service
-rw-r--r--. 1 root root  799 10月  2 2020 [email protected]
-rw-r--r--. 1 root root 1027 10月  2 2020 systemd-binfmt.service
-rw-r--r--. 1 root root  654 10月  2 2020 systemd-bootchart.service
-rw-r--r--. 1 root root  826 10月  2 2020 systemd-firstboot.service
-rw-r--r--. 1 root root  682 10月  2 2020 systemd-fsck-root.service
-rw-r--r--. 1 root root  702 10月  2 2020 [email protected]
-rw-r--r--. 1 root root  548 10月  2 2020 systemd-halt.service
-rw-r--r--. 1 root root  635 10月  2 2020 [email protected]
-rw-r--r--. 1 root root  505 10月  2 2020 systemd-hibernate.service
-rw-r--r--. 1 root root  714 10月  2 2020 systemd-hostnamed.service
-rw-r--r--. 1 root root  838 10月  2 2020 systemd-hwdb-update.service
-rw-r--r--. 1 root root  523 10月  2 2020 systemd-hybrid-sleep.service
-rw-r--r--. 1 root root  693 10月  2 2020 systemd-importd.service
-rw-r--r--. 1 root root  484 10月  2 2020 systemd-initctl.service
-rw-r--r--. 1 root root  524 10月  2 2020 systemd-initctl.socket
-rw-r--r--. 1 root root  738 10月  2 2020 systemd-journal-catalog-update.service
-rw-r--r--. 1 root root 1156 10月  2 2020 systemd-journald.service
-rw-r--r--. 1 root root  833 10月  2 2020 systemd-journald.socket
-rw-r--r--. 1 root root  735 10月  2 2020 systemd-journal-flush.service
-rw-r--r--. 1 root root  561 10月  2 2020 systemd-kexec.service
-rw-r--r--. 1 root root  695 10月  2 2020 systemd-localed.service
-rw-r--r--. 1 root root 1130 10月  2 2020 systemd-logind.service
-rw-r--r--. 1 root root  819 10月  2 2020 systemd-machined.service
-rw-r--r--. 1 root root  682 10月  2 2020 systemd-machine-id-commit.service
-rw-r--r--. 1 root root 1029 10月  2 2020 systemd-modules-load.service
-rw-r--r--. 1 root root  676 10月  2 2020 [email protected]
-rw-r--r--. 1 root root  557 10月  2 2020 systemd-poweroff.service
-rw-r--r--. 1 root root  690 10月  2 2020 systemd-quotacheck.service
-rw-r--r--. 1 root root  777 10月  2 2020 systemd-random-seed.service
-rw-r--r--. 1 root root  845 10月  2 2020 systemd-readahead-collect.service
-rw-r--r--. 1 root root  642 10月  2 2020 systemd-readahead-done.service
-rw-r--r--. 1 root root  635 10月  2 2020 systemd-readahead-done.timer
-rw-r--r--. 1 root root  555 10月  2 2020 systemd-readahead-drop.service
-rw-r--r--. 1 root root  757 10月  2 2020 systemd-readahead-replay.service
-rw-r--r--. 1 root root  552 10月  2 2020 systemd-reboot.service
-rw-r--r--. 1 root root  828 10月  2 2020 systemd-remount-fs.service
-rw-r--r--. 1 root root  813 10月  2 2020 [email protected]
-rw-r--r--. 1 root root  479 10月  2 2020 systemd-shutdownd.service
-rw-r--r--. 1 root root  528 10月  2 2020 systemd-shutdownd.socket
-rw-r--r--. 1 root root  501 10月  2 2020 systemd-suspend.service
-rw-r--r--. 1 root root  711 10月  2 2020 systemd-sysctl.service
-rw-r--r--. 1 root root  659 10月  2 2020 systemd-timedated.service
-rw-r--r--. 1 root root  669 10月  2 2020 systemd-tmpfiles-clean.service
-rw-r--r--. 1 root root  450 10月  2 2020 systemd-tmpfiles-clean.timer
-rw-r--r--. 1 root root  774 10月  2 2020 systemd-tmpfiles-setup-dev.service
-rw-r--r--. 1 root root  754 10月  2 2020 systemd-tmpfiles-setup.service
-rw-r--r--. 1 root root  595 10月  2 2020 systemd-udevd-control.socket
-rw-r--r--. 1 root root  570 10月  2 2020 systemd-udevd-kernel.socket
-rw-r--r--. 1 root root  812 10月  2 2020 systemd-udevd.service
-rw-r--r--. 1 root root  827 10月  2 2020 systemd-udev-settle.service
-rw-r--r--. 1 root root  751 10月  2 2020 systemd-udev-trigger.service
-rw-r--r--. 1 root root  701 10月  2 2020 systemd-update-done.service
-rw-r--r--. 1 root root  761 10月  2 2020 systemd-update-utmp-runlevel.service
-rw-r--r--. 1 root root  829 10月  2 2020 systemd-update-utmp.service
-rw-r--r--. 1 root root  581 10月  2 2020 systemd-user-sessions.service
-rw-r--r--. 1 root root  716 10月  2 2020 systemd-vconsole-setup.service
-rw-r--r--. 1 root root  433 10月  2 2020 system.slice
-rw-r--r--. 1 root root  652 10月  2 2020 system-update.target
drwxr-xr-x. 2 root root 4096 12月  3 09:57 system-update.target.wants
-rw-r--r--. 1 root root  222 11月  6 2019 targetclid.service
-rw-r--r--. 1 root root  168 10月  1 2020 targetclid.socket
-rw-r--r--. 1 root root  331 10月  1 2020 target.service
-rw-r--r--. 1 root root  148 8月   4 2017 tcsd.service
-rw-r--r--. 1 root root  244 12月  9 2018 [email protected]
-rw-r--r--. 1 root root  405 10月  2 2020 timers.target
drwxr-xr-x. 2 root root 4096 12月  3 09:55 timers.target.wants
-rw-r--r--. 1 root root  395 10月  2 2020 time-sync.target
-rw-r--r--. 1 root root  703 10月  2 2020 tmp.mount
-rw-r--r--. 1 root root  376 3月  22 2019 tuned.service
-rw-r--r--. 1 root root  207 4月   1 2020 udisks2.service
-rw-r--r--. 1 root root  417 10月  2 2020 umount.target
-rw-r--r--. 1 root root  273 11月 28 2019 unbound-anchor.service
-rw-r--r--. 1 root root  346 11月 28 2019 unbound-anchor.timer
-rw-r--r--. 1 root root  215 10月 31 2018 upower.service
-rw-r--r--. 1 root root  149 4月  11 2018 [email protected]
-rw-r--r--. 1 root root  184 8月   7 2017 usbmuxd.service
-rw-r--r--. 1 root root  392 10月  2 2020 user.slice
-rw-r--r--. 1 root root  191 10月  1 2020 var-lib-nfs-rpc_pipefs.mount
-rw-r--r--. 1 root root  278 10月  1 2020 vdo.service
-rw-r--r--. 1 root root  241 10月  1 2020 [email protected]
-rw-r--r--. 1 root root  263 9月  30 2020 vgauthd.service
-rw-r--r--. 1 root root   77 10月 14 2020 virt-guest-shutdown.target
-rw-r--r--. 1 root root  223 10月 14 2020 virtlockd-admin.socket
-rw-r--r--. 1 root root  695 10月 14 2020 virtlockd.service
-rw-r--r--. 1 root root  185 10月 14 2020 virtlockd.socket
-rw-r--r--. 1 root root  214 10月 14 2020 virtlogd-admin.socket
-rw-r--r--. 1 root root  828 10月 14 2020 virtlogd.service
-rw-r--r--. 1 root root  183 10月 14 2020 virtlogd.socket
-rw-r--r--. 1 root root  366 9月  30 2020 vmtoolsd.service
-rw-r--r--. 1 root root  167 8月   9 2019 [email protected]
-rw-r--r--. 1 root root  365 10月 31 2018 wpa_supplicant.service
-rw-r--r--. 1 root root  275 10月  2 2020 zram.service
[root@zjm999 ~]# 

systemctl设置服务的自启动状态

systemctl list-unit-files [|grep 服务名](查看服务开机启动状态,grep可以进行过滤)

systemctl enable 服务名(设置服务开机启动)

systemctl disable 服务名(关闭服务开机启动)
systemctl is-enabled 服务名(查询某个服务是否是自启动的)

应用案例:

查看当前防火墙的状况,关闭防火墙和重启防火墙。

[root@zjm999 ~]# ls -l /usr/lib/systemd/system | grep fire
-rw-r--r--. 1 root root  657 10月  1 2020 firewalld.service
[root@zjm999 ~]# systemctl list-unit-files | grep firewalld.service
firewalld.service                             enabled 
[root@zjm999 ~]# 
[root@zjm999 ~]# systemctl is-enabled firewalld
enabled
[root@zjm999 ~]# 
[root@zjm999 ~]# systemctl is-enabled sshd
enabled
[root@zjm999 ~]# 
[root@zjm999 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since 四 2025-02-06 15:10:28 CST; 53min ago
     Docs: man:firewalld(1)
 Main PID: 785 (firewalld)
    Tasks: 2
   CGroup: /system.slice/firewalld.service
           └─785 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid

2月 06 15:10:27 zjm999 systemd[1]: Starting firewalld - dynamic firewall daemon...
2月 06 15:10:28 zjm999 systemd[1]: Started firewalld - dynamic firewall daemon.
2月 06 15:10:28 zjm999 firewalld[785]: WARNING: AllowZoneDrifting is enabled. This is considered an insecure configuration op...it now.
Hint: Some lines were ellipsized, use -l to show in full.
[root@zjm999 ~]# 

细节讨论:

1.关闭或者启用防火墙后,立即生效。[telnet 测试某个端口即可]

2.这种方式只是临时生效,当重启系统后,还是回归以前对服务的设置。

3.如果希望设置某个服务自启动或关闭永久生效,要使用systemctl [enable | disable] 服务名.

打开或者关闭指定端口

在真正的生产环境,往往需要将防火墙打开,但问题来了,如果我们把防火墙打开,那么外部请求数据包就不能跟服务器监听端口通讯。这时,需要打开指定的端口。比如80、22、8080等,这个又怎么做呢?

firewall指令
打开端口:firewall-cmd --permanent --add-port=端口号/协议

关闭端口:firewall-cmd --permanent --remove-port=端口号/协议

重新载入,才能生效:firewalli-cmd --reload

查询端口是否开放:firewall-cmd --query-port=端口/协议

应用案例:

1.启用防火墙,测试111端口是否能telnet

2.开放111端口

3.再次关闭111端口

[root@zjm999 ~]# firewall-cmd --permanent --add-port=111/tcp
success
[root@zjm999 ~]# firewall-cmd --reload
success
[root@zjm999 ~]# firewall-cmd --query-port=111/tcp
yes
[root@zjm999 ~]# 

测试

C:\Users\ZJM>telnet 192.168.18.129 111

关闭

[root@zjm999 ~]# firewall-cmd --permanent --remove-port=111/tcp
success
[root@zjm999 ~]# firewall-cmd --reload
success
[root@zjm999 ~]# firewall-cmd --query-port=111/tcp
no
[root@zjm999 ~]# 

动态监控进程

top与ps命令很相似。它们都用来显示正在执行的进程。Top与ps最大的不同之处,在于top在

执行一段时间可以更新正在运行的的进程。

基本语法
top [选项]

选项 功能
-d 秒数 : 指定top命令每隔几秒更新。默认是3秒
-i : 使top不显示任何闲置或者僵死进程。
-p:通过指定监控进程ID来仅仅监控某个进程的状态。

[root@zjm999 ~]# top
top - 16:34:15 up  1:24,  4 users,  load average: 0.01, 0.03, 0.05
Tasks: 236 total,   1 running, 235 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.0 sy,  0.0 ni, 99.7 id,  0.2 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  2027896 total,   395124 free,   836028 used,   796744 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.   993488 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                           
   671 root      20   0  295564   5268   4036 S   1.0  0.3   0:14.04 vmtoolsd                                                          
  2345 root      20   0  608676  25460  18800 S   0.7  1.3   0:13.52 vmtoolsd                                                          
  3485 root      20   0  160988   5632   4276 S   0.3  0.3   0:00.81 sshd                                                              
     1 root      20   0  194244   7420   4232 S   0.0  0.4   0:04.82 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:00.17 ksoftirqd/0                                                       
     7 root      rt   0       0      0      0 S   0.0  0.0   0:02.87 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:01.80 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:00.03 watchdog/0                                                        
    12 root      rt   0       0      0      0 S   0.0  0.0   0:00.06 watchdog/1                                                        
    13 root      rt   0       0      0      0 S   0.0  0.0   0:02.83 migration/1                                                       
    14 root      20   0       0      0      0 S   0.0  0.0   0:00.09 ksoftirqd/1                                                       
    15 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kworker/1:0                                                       
    16 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/1:0H                                                      
    17 root      rt   0       0      0      0 S   0.0  0.0   0:00.02 watchdog/2                                                        
    18 root      rt   0       0      0      0 S   0.0  0.0   0:02.74 migration/2                                                       
    19 root      20   0       0      0      0 S   0.0  0.0   0:00.07 ksoftirqd/2                                                       
    20 root      20   0       0      0      0 S   0.0  0.0   0:00.16 kworker/2:0                                                       
    21 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/2:0H                                                      
    22 root      rt   0       0      0      0 S   0.0  0.0   0:00.02 watchdog/3                                                        
    23 root      rt   0       0      0      0 S   0.0  0.0   0:02.71 migration/3                                                       
    24 root      20   0       0      0      0 S   0.0  0.0   0:00.06 ksoftirqd/3                                                       
    26 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/3:0H                                                      
    27 root      rt   0       0      0      0 S   0.0  0.0   0:00.03 watchdog/4                                                        
    28 root      rt   0       0      0      0 S   0.0  0.0   0:02.74 migration/4                                                       
    29 root      20   0       0      0      0 S   0.0  0.0   0:00.17 ksoftirqd/4                                                       
    31 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/4:0H                                                      
    32 root      rt   0       0      0      0 S   0.0  0.0   0:00.04 watchdog/5 

交互操作说明

操作功能

P:以CPU使用率排序,默认就是此项

M:以内存的使用率排序

N:以PID排序

q:退出top

应用实例
案例1.监视特定用户

top:输入此命令,按回车键,查看执行的进程。

u:然后输入“u”回车,再输入用户名,即可

案例2:终止指定的进程。

top:输入此命令,按回车键,查看执行的进程。

k:然后输入“k”回车,再输入要结束的进程ID号

监控网络状态

查看系统网络情况netstat

基本语法

netstat [选项]

选项说明
-an 按一定顺序排列输出
-p 显示哪个进程在调用

[root@zjm999 ~]# netstat -an
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN     

[root@zjm999 ~]# netstat -anp | more
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1140/sshd           
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1144/cupsd          
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1326/master         
tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN      2622/sshd: root@pts 
tcp        0      0 127.0.0.1:6011          0.0.0.0:*               LISTEN      3485/sshd: root@pts 
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      670/rpcbind         
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      1815/dnsmasq        
tcp        0      0 192.168.18.129:22       192.168.18.1:59961      ESTABLISHED 3485/sshd: root@pts 
tcp        0      0 192.168.18.129:22       192.168.18.1:57726      ESTABLISHED 2622/sshd: root@pts 
tcp6       0      0 :::22                   :::*                    LISTEN      1140/sshd           
tcp6       0      0 ::1:631                 :::*                    LISTEN      1144/cupsd          
tcp6       0      0 ::1:25                  :::*                    LISTEN      1326/master         
tcp6       0      0 ::1:6010                :::*                    LISTEN      2622/sshd: root@pts 
tcp6       0      0 ::1:6011                :::*                    LISTEN      3485/sshd: root@pts 
tcp6       0      0 :::111                  :::*                    LISTEN      670/rpcbind         
udp        0      0 0.0.0.0:67              0.0.0.0:*                           1815/dnsmasq        
udp        0      0 0.0.0.0:68              0.0.0.0:*                           3335/dhclient       
udp        0      0 0.0.0.0:111             0.0.0.0:*                           670/rpcbind         
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           692/avahi-daemon: r 
udp        0      0 127.0.0.1:323           0.0.0.0:*                           716/chronyd         
udp        0      0 0.0.0.0:840             0.0.0.0:*                           670/rpcbind         
udp        0      0 0.0.0.0:47020           0.0.0.0:*                           692/avahi-daemon: r 
udp        0      0 192.168.122.1:53        0.0.0.0:*                           1815/dnsmasq        
udp6       0      0 :::111                  :::*                                670/rpcbind         
udp6       0      0 ::1:323                 :::*                                716/chronyd         
udp6       0      0 :::840                  :::*                                670/rpcbind         
raw6       0      0 :::58                   :::*                    7           816/NetworkManager  
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   PID/Program name     Path
unix  2      [ ACC ]     STREAM     LISTENING     24358    1894/gnome-session-  @/tmp/.ICE-unix/1894
unix  2      [ ACC ]     STREAM     LISTENING     773      1/systemd            /run/lvm/lvmpolld.socket
unix  2      [ ACC ]     SEQPACKET  LISTENING     798      1/systemd            /run/udev/control
unix  2      [ ACC ]     STREAM     LISTENING     27928    1406/X               @/tmp/.X11-unix/X0
unix  2      [ ACC ]     STREAM     LISTENING     30506    1876/gnome-keyring-  /run/user/0/keyring/pkcs11
unix  2      [ ACC ]     STREAM     LISTENING     30510    1876/gnome-keyring-  /run/user/0/keyring/ssh
--More--

应用案例
请查看服务名为sshd的服务的信息。

[root@zjm999 ~]# netstat -anp | grep sshd
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1140/sshd           
tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN      2622/sshd: root@pts 
tcp        0      0 127.0.0.1:6011          0.0.0.0:*               LISTEN      3485/sshd: root@pts 
tcp        0     36 192.168.18.129:22       192.168.18.1:59961      ESTABLISHED 3485/sshd: root@pts 
tcp        0      0 192.168.18.129:22       192.168.18.1:57726      ESTABLISHED 2622/sshd: root@pts 
tcp6       0      0 :::22                   :::*                    LISTEN      1140/sshd           
tcp6       0      0 ::1:6010                :::*                    LISTEN      2622/sshd: root@pts 
tcp6       0      0 ::1:6011                :::*                    LISTEN      3485/sshd: root@pts 
unix  2      [ ]         DGRAM                    34360    2622/sshd: root@pts  
unix  2      [ ]         DGRAM                    43628    3485/sshd: root@pts  
unix  3      [ ]         STREAM     CONNECTED     27744    1140/sshd            
[root@zjm999 ~]# 

检测主机连接命令ping:
是一种网络检测检测工具,它主要是用检测远程主机是否正常,或是两部主机间的网线或网卡故障。
如:ping对方ip地址

rpm包的管理

rpm 用于互联网下载包的打包及安装工具,它包含在某些 Linux 分发版中。它生成具有.RPM 扩展名的文件。RPM 是

RedHatPackageManager(RedHat 软件包管理工具)的缩写,类似 windows 的 setup.exe,这一文件格式名称虽

然打上了 RedHat 的标志,但理念是通用的。

Linux 的分发版本都有采用(suse, redhat,centos 等等),可以算是公认的行业标准了。

rpm 包的简单查询指令

查询已安装的 rpm 列表 rpm -qa | grep xx

[root@zjm999 ~]# rpm -qa
iscsi-initiator-utils-iscsiuio-6.2.0.874-19.el7.x86_64
libss-1.42.9-19.el7.x86_64
libvirt-daemon-config-network-4.5.0-36.el7.x86_64
btrfs-progs-4.9.1-1.el7.x86_64
alsa-plugins-pulseaudio-1.1.6-1.el7.x86_64
telepathy-filesystem-0.0.2-6.el7.noarch
libstoragemgmt-python-clibs-1.8.1-1.el7.x86_64
slang-2.2.4-11.el7.x86_64
libsoup-2.62.2-2.el7.x86_64
[root@zjm999 ~]# rpm -qa | grep firefox
firefox-68.10.0-1.el7.centos.x86_64
[root@zjm999 ~]# 

rpm 包名基本格式

一个 rpm 包名:firefox-60.2.2-1.el7.centos.x86_64

名称:firefox

版本号:60.2.2-1

适用操作系统:el7.centos.x86_64

表示 centos7.x 的 64 位系统

如果是 i686、i386 表示 32 位系统,noarch 表示通用

rpm 包的其它查询指令

rpm -qa:查询所安装的所有 rpm 软件包

rpm -qa | more

rpm -qa | grep X [rpm -qa | grep firefox ]

rpm -q 软件包名:查询软件包是否安装

[root@zjm999 ~]# rpm -q firefox
firefox-68.10.0-1.el7.centos.x86_64
[root@zjm999 ~]# 

rpm -qi 软件包名:查询软件包信息

[root@zjm999 ~]# rpm -qi firefox
Name        : firefox
Version     : 68.10.0
Release     : 1.el7.centos
Architecture: x86_64
Install Date: 2024年12月03日 星期二 10时00分00秒
Group       : Unspecified
Size        : 241030932
License     : MPLv1.1 or GPLv2+ or LGPLv2+
Signature   : RSA/SHA256, 2020年07月09日 星期四 00时21分14秒, Key ID 24c6a8a7f4a80eb5
Source RPM  : firefox-68.10.0-1.el7.centos.src.rpm
Build Date  : 2020年07月08日 星期三 02时51分10秒
Build Host  : x86-01.bsys.centos.org
Relocations : (not relocatable)
Packager    : CentOS BuildSystem 
Vendor      : CentOS
URL         : https://www.mozilla.org/firefox/
Summary     : Mozilla Firefox Web browser
Description :
Mozilla Firefox is an open-source web browser, designed for standards
compliance, performance and portability.
[root@zjm999 ~]# 

rpm- ql 软件包名:查询软件包中的文件

[root@zjm999 ~]# rpm -ql firefox
/etc/firefox
/etc/firefox/pref
/usr/bin/firefox
/usr/lib64/firefox
/usr/lib64/firefox/LICENSE
/usr/lib64/firefox/application.ini
/usr/lib64/firefox/browser/blocklist.xml
/usr/lib64/firefox/browser/chrome
/usr/lib64/firefox/browser/chrome.manifest
/usr/lib64/firefox/browser/chrome/icons
/usr/lib64/firefox/browser/chrome/icons/default
/usr/lib64/firefox/browser/chrome/icons/default/default128.png
/usr/lib64/firefox/browser/chrome/icons/default/default16.png
/usr/lib64/firefox/browser/chrome/icons/default/default32.png
/usr/lib64/firefox/browser/chrome/icons/default/default48.png
/usr/lib64/firefox/browser/chrome/icons/default/default64.png
/usr/lib64/firefox/browser/defaults/preferences
/usr/lib64/firefox/browser/features/[email protected]
/usr/lib64/firefox/browser/features/[email protected]
/usr/lib64/firefox/browser/features/[email protected]
/usr/lib64/firefox/browser/features/[email protected]
/usr/lib64/firefox/browser/features/[email protected]
/usr/lib64/firefox/browser/omni.ja
/usr/lib64/firefox/chrome.manifest
/usr/lib64/firefox/defaults/pref/channel-prefs.js
/usr/lib64/firefox/defaults/preferences/all-redhat.js
/usr/lib64/firefox/dependentlibs.list
/usr/lib64/firefox/dictionaries
/usr/lib64/firefox/distribution/distribution.ini
/usr/lib64/firefox/distribution/extensions
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/distribution/extensions/[email protected]
/usr/lib64/firefox/firefox
/usr/lib64/firefox/firefox-bin
/usr/lib64/firefox/fonts/TwemojiMozilla.ttf
/usr/lib64/firefox/gmp-clearkey
/usr/lib64/firefox/gmp-clearkey/0.1
/usr/lib64/firefox/gmp-clearkey/0.1/libclearkey.so
/usr/lib64/firefox/gmp-clearkey/0.1/manifest.json
/usr/lib64/firefox/gtk2/libmozgtk.so
/usr/lib64/firefox/liblgpllibs.so
/usr/lib64/firefox/libmozavcodec.so
/usr/lib64/firefox/libmozavutil.so
/usr/lib64/firefox/libmozgtk.so
/usr/lib64/firefox/libmozsandbox.so
/usr/lib64/firefox/libmozsqlite3.so
/usr/lib64/firefox/libmozwayland.so
/usr/lib64/firefox/libxul.so
/usr/lib64/firefox/omni.ja
/usr/lib64/firefox/pingsender
/usr/lib64/firefox/platform.ini
/usr/lib64/firefox/plugin-container
/usr/lib64/firefox/run-mozilla.sh
/usr/lib64/mozilla/extensions/{ec8030f7-c20a-464f-9b0e-13a3a9e97384}
/usr/share/appdata/firefox.appdata.xml
/usr/share/applications/firefox.desktop
/usr/share/icons/hicolor/16x16/apps/firefox.png
/usr/share/icons/hicolor/22x22/apps/firefox.png
/usr/share/icons/hicolor/24x24/apps/firefox.png
/usr/share/icons/hicolor/256x256/apps/firefox.png
/usr/share/icons/hicolor/32x32/apps/firefox.png
/usr/share/icons/hicolor/48x48/apps/firefox.png
/usr/share/icons/hicolor/symbolic/apps/firefox-symbolic.svg
/usr/share/man/man1/firefox.1.gz
/usr/share/mozilla/extensions/{ec8030f7-c20a-464f-9b0e-13a3a9e97384}
[root@zjm999 ~]# 

rpm -qf 文件全路径名 查询文件所属的软件包
rpm -qf /etc/passwd
rpm -qf /root/install.log

[root@zjm999 ~]# rpm -qf /etc/passwd
setup-2.8.71-11.el7.noarch
[root@zjm999 ~]# 

卸载 rpm 包

基本语法: rpm -e RPM包的名称//erase

删除 firefox 软件包
rpm -e firefox

细节
1.如果其它软件包依赖于您要卸载的软件包,卸载时则会产生错误信息。

如:rpm -e foo

removing these packageswould break dependencies: foo is needed by bar-1.0-1

1.如果我们就是要删除 foo 这个 rpm 包,可以增加参数–nodeps,就可以强制删除,但是一般不推荐这样做,因为依赖于该软件包的程序可能无法运行

如:rpm -e --nodeps foo

[root@zjm999 ~]# rpm -e firefox
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions/[email protected]: 移除失败: 没有那个文件或目录
警告:文件 /usr/lib64/firefox/distribution/extensions: 移除失败: 没有那个文件或目录
[root@zjm999 ~]# rpm -q firefox
未安装软件包 firefox 
[root@zjm999 ~]# 

安装 rpm 包

基本语法:rpm -ivh RPM包全路径名称

参数说明

i = install 安装

v = verbose 提示

h = hash 进度条

[root@zjm999 ~]# rpm -ivh /opt/firefox-68.10.0-1.el7.centos.x86_64.rpm 
警告:/opt/firefox-68.10.0-1.el7.centos.x86_64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID f4a80eb5: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:firefox-68.10.0-1.el7.centos     ################################# [100%]
[root@zjm999 ~]# 

yum 管理

Yum 是一个 Shell 前端软件包管理器。基于 RPM 包管理,能够从指定的服务器自动下载 RPM 包并且安装,可以自动

处理依赖性关系,并且一次安装所有依赖的软件包。

yum 的基本指令

查询 yum 服务器是否有需要安装的软件

yum list | grep xx软件列表

安装指定的 yum 包
yum install xxx

使用 yum 安装 firefox

yum list | grep firefox
yum install firefox

安装 JavaEE 环境

jdk、tomcat、mysql

Shell 编程

Shell 介绍

Shell 是一种命令行解释器,它为用户提供了一个与操作系统内核交互的界面。在 Linux 和 Unix 系统中,Shell 不仅是用户与系统之间的桥梁,还是一种强大的编程环境,允许用户编写脚本来自动化任务。

脚本格式要求

1.脚本以#!/bin/bash 开头

2.脚本需要有可执行权限

编写第一个 Shell 脚本

需求说明:创建一个 Shell 脚本,输出 helloworld!

vim hello.sh
#!/bin/bash
echo"hello,world~"

脚本的常用执行方式

方式 1(输入脚本的绝对路径或相对路径)

说明:首先要赋予 hello.sh 脚本的+x 权限,再执行脚本

方式 2(sh+脚本)

说明:不用赋予脚本+x 权限,直接执行即可。

比如 sh hello.sh

Shell 变量

Shell 变量介绍

1.Linux Shell 中的变量分为,系统变量和用户自定义变量。

2.系统变量: H O M E 、 HOME、 HOME PWD、 S H E L L 、 SHELL、 SHELL USER 等等,比如:echo $HOME 等等…

3.显示当前 shell 中所有变量:set

[root@zjm999 sh_code]# set | more
ABRT_DEBUG_LOG=/dev/null
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:expand_aliases:extglob:extquote:force_fignore:histappend:interactive_comments:login_shell:pro
gcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_COMPLETION_COMPAT_DIR=/etc/bash_completion.d
BASH_LINENO=()
BASH_REMATCH=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="4" [1]="2" [2]="46" [3]="2" [4]="release" [5]="x86_64-redhat-linux-gnu")
BASH_VERSION='4.2.46(2)-release'
COLUMNS=123
COMP_WORDBREAKS=$' \t\n"\'><=;|&(:'
DIRSTACK=()
DISPLAY=localhost:10.0
EUID=0

Shell 变量的定义

基本语法

1.定义变量:变量名 = 值

2.撤销变量:unset 变量

3.声明静态变量:readonly 变量,注意:不能 unset

快速入门

案例 1:定义变量 A

案例 2:撤销变量 A

案例 3:声明静态的变量 B = 2,不能 unset

案例 4:可把变量提升为全局环境变量,可供其他 shel 程序使用 [该案例后面讲]

#!/bin/bash
#案例1:定义变量A
A=100
#输出变量要加上$
echo A=$A
#案例2:撤销变量A
unset A
echo "A=$A"
#案例3:声明静态的变量B=2,不能unset
readonly B=200
echo "B=$B"

定义变量的规则

1.变量名称可以由字母、数字和下划线组成,但是不能以数字开头。

2.等号两侧不能有空格

3.变量名称一般习惯为大写

将命令的返回值赋给变量

1.A = date 反引号,运行里面的命令,并把结果返回给变量 A

2.A =$(date)等价于反引号

Shell 设置环境变量

基本语法:

export 变量名 = 变量值(功能描述:将 shell 变量输出为环境变量/全局变量)

source 配置文件(功能描述:让修改后的配置信息立即生效)

echo $变量名(功能描述:查询环境变量的值)

shell 脚本的多行注释

:<<!
内容
!

Shell 位置参数变量

n (功能描述: n 为数字, n(功能描述:n为数字, n(功能描述:n为数字, 0 代表命令本身, 1 − 1- 1 9 代表第一到第九个参数,十以上的参数,十以上的参数需要用大括号包含,如${10})

KaTeX parse error: Undefined control sequence: \* at position 1: \̲*̲(功能描述:这个变量代表命令行…*把所有的参数看成一个整体)

@ (功能描述:这个变量也代表命令行中所有的参数,不过 @(功能描述:这个变量也代表命令行中所有的参数,不过 @(功能描述:这个变量也代表命令行中所有的参数,不过@把每个参数区分对待)

$#(功能描述:这个变量代表命令行中所有参数的个数)

#!/bin/bash
echo 1=$1 2=$2 3=$3
echo $*
[root@zjm009 sh_code]# sh position.sh 10 20 30
1=10 2=20 3=30
10 20 30

Shell 预定义变量

$$(功能描述:当前进程的进程号(PID))

$!(功能描述:后台运行的最后一个进程的进程号(PID))

$?(功能描述:最后一次执行的命令的返回状态。如果这个变量的值为 0,证明上一个命令正确执行;

如果这个变量的值为非 0(具体是哪个数,由命令自己来决定),则证明上一个命令执行不正确了。)

#!/bin/bash
echo "当前执行的进程ID=$$"
echo "最后一个后台方式运行的进程id=$!"
echo "执行的结果是=$?"

Shell 运算符

  1. ( ( 运算式 ) ) ”或“ ((运算式))”或“ (运算式)) [运算式]”或者 expr m+n //expression 表达式
  2. 注意 expr 运算符间要有空格,如果希望将 expr 的结果赋给某个变量,使用``
  3. expr m - n
  4. expr,*,/,% 乘 ,除,取余
#!/bin/bash
RES1=$(((2+3)*4))
echo "res1=$RES1"
RES2=$[(2+3)*4]
echo "res2=$RES2"
RES3=`expr 2 + 3`
echo "res3=$RES3"

Shell 条件判断

判断语句

基本语法

[ condition ] 注意condition前后要有空格

#非空返回true,可使用$?验证(0为true,>1为false)

[ condition ] && echo OK || echo notok 条件满足,执行后面的语句

用判断条件

1)=字符串比较

2)两个整数的比较

-lt小于

-le小于等于

-eq等于

-gt大于

-ge大于等于

-ne不等于
3)按照文件权限进行判断
-r有读的权限
-w有写的权限
-x有执行的权限
4)按照文件类型进行判断
-f文件存在并且是一个常规的文件
-e文件存在
-d文件存在并是一个目录

应用实例:

#!/bin/bash
#案例1:“ok"是否等于"ok"
#判断语句:使用=
if [ "ok" = "ok" ]
then
        echo "equal"
fi

#案例2:23是否大于等于22
#判断语句:使用-ge
if [ 23 -ge 22 ]
then
        echo "大于等于"
fi


#案例3:root/shcode/aaa.txt目录中的文件是否存在
#判断语句:使用-f
if [ -f /root/shcode/aaa.txt ]
then
        echo "存在"
fi

单分支多分支
基本语法

if [ 条件判断式 ]
then
代码
fi  #反着写表示结束   
if [ 条件判断式 ]
then
代码
elif [ 条件判断式 ]
then
代码
fi #反着写表示结束

注意事项:[ 条件判断式 ],中括号和条件判断式之间必须有空格

#!/bin/bash
#案例:请编写一个shell程序,如果输入的参数,大于等于60,则输出“及格了”,如果小于60,则输出“不及格”

if [ $1 -ge 60 ]
then
        echo "及格了"
elif [ $1 -lt 60 ]
then
        echo "不及格"
fi

case语句

case $变量名in
"值1")
如果变量的值等于值1,则执行程序1
;;
"值2")
如果变量的值等于值2,则执行程序2
;;
...省略其他分支..
*)
如果变量的值都不是以上的值,则执行此程序
;;
esac #反着写表示结束
#!/bin/bash
# 案例1:当命令行参数是1时,输出“周一”,是2时,就输出“周二",其它情况输出“other”
case $1 in
"1")
echo "周一"
;;
"2")
echo "周二"
;;
*)
echo "other"
;;
esac

Shell 循环语句

for循环

# 基本语法1

for 变量 in 值1 值2 值3...
do
程序/代码
done
#!/bin/bash
#案例1:打印命令行输入的参数[这里可以看出$*和$@的区别]
#注意$*是把输入的参数,当做一个整体,所以,只会输出一句
for i in "$*"
do
        echo "num is $i"
done

echo "================================================"

#使用$@来获取输入的参数,注意,这时是分别对待,所以有几个参数,就输出几句
for j in "$@"
do
        echo "num is $j"
done

# 基本语法2
for((初始值;循环控制条件;变量变化))
do
程序
done
#!/bin/bash
# 案例1:从1加到100的值输出显示
# 定义一个变量 SUM
SUM=0
for(( i=1; i<= 100;  i++))
do
#业务代码
        SUM=$[$SUM+$i]
done
echo "SUM的总和为:$SUM"

while循环

# 基本语法
while [ 条件判断式 ]
do
程序
done
# 注意:while和[有空格,条件判断式和[也有空格
#!/bin/bash
# 案例1:从命令行输入一个数n,统计从1+..+n的值是多少
SUM=0
i=0
while [ $i -le $1 ]
do
        SUM=$[$SUM+$i]
        # i自增
        i=$[$i+1]
done
echo "执行结果=$SUM"

Shell read获取输入

基本语法
read(选项)(参数)
选项:
-p:指定读取值时的提示符;
-t:指定读取值时等待的时间(秒),如果没有在指定的时间内输入,就不再等待了。。
参数
变量:指定读取值的变量名。

#!/bin/bash
# 案例1:读取控制台输入一个NUM1值
read -p "请输入一个数NUM1:" NUM1
echo "你输入的NUM1=$NUM1"
# 案例2:读取控制台输入一个NUM2值,在10秒内输入。
read -t 10 -p "请在10秒内输入一个数NUM2:" NUM2
echo "你输入的NUM2=$NUM2"

Shell 函数

shell编程和其它编程语言一样,有系统函数,也可以自定义函数。

系统函数

basename基本语法
功能:返回完整路径最后/的部分,常用于获取文件名
basename [pathname] [suffix]
basename [string] [suffix]
(功能描述:basename命令会删掉所有的前缀包括最后一个('/')字符,然后将字符串显示出来。


选项:suffix为后缀,如果suffix被指定了,basename会将pathname或string中的suffix去掉。

[root@zjm009 sh_code]# basename /home/aaa/test.txt
test.txt
[root@zjm009 sh_code]# basename /home/aaa/test.txt .txt
test
[root@zjm009 sh_code]# 

dirname基本语法
功能:返回完整路径最后/的前面的部分,常用于返回路径部分

dirname 文件绝对路径
功能描述:从给定的包含绝对路径的文件名中去除文件名(非目录的部分),然后返回剩下的路径(目录的部分))
[root@zjm009 sh_code]# dirname /home/aaa/text.txt
/home/aaa
[root@zjm009 sh_code]# dirname /home/aaa/bbb/ccc/ddd/text.txt
/home/aaa/bbb/ccc/ddd
[root@zjm009 sh_code]# 

自定义函数

[ function ] funname[()]
{
	Action
	[return int;]
}

# 调用直接写函数名:funname[值]
#!/bin/bash
# 案例1:计算输入两个参数的和(动态的获取),getSum

# 定义函数 getSum
function getSum() {

        SUM=$[$n1+$n2]
        echo "和是=$SUM"
}

#输入两个值
read -p "请输入一个数n1:" n1
read -p "请输入一个数n2:" n2


#调用自定义函数
getSum $n1 $n2

Shell 应用

定时备份数据库

#!/bin/bash
# 备份目录
BACKUP=/data/backup/db
# 当前时间
DATETIME=$(date +%Y-%m-%d_%H%M%S)
echo $DATETIME
# 数据库的地址
HOST=localhost
# 数据库用户名
DB_USER=root
# 数据库密码
DB_PW=zjm123456
# 备份的数据库名
DATABASE=test1

# 创建备份目录,如果不存在,就创建
[ ! -d "${BACKUP}/${DATETIME}" ] && mkdir -p "${BACKUP}/${DATETIME}"

# 备份数据库
mysqldump -u${DB_USER} -p${DB_PW} --host=${HOST} -q -R --database ${DATABASE} | gzip > ${BACKUP}/${DATETIME}/$DATETIME.sql.gz

# 将文件处理成 tar.gz
cd ${BACKUP}
tar -zcvf $DATETIMR.tar.gz ${DATETIME}

# 删除对应的备份目录
rm -rf ${BACKUP}/${DATETIME}

# 删除10天前的备份文件
find ${BACKUP} -atime +11 -name "*.tar.gz" -exec rm -rf {} \;
echo "备份数据库${DATABASE}成功~"

定时执行

[root@zjm009 sbin]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
[root@zjm009 sbin]# crontab -l
30 2 * * * /usr/sbin/mysql_db_backup.sh
[root@zjm009 sbin]# 
30 2 * * * /usr/sbin/mysql_db_backup.sh

你可能感兴趣的:(Linux,linux,运维,服务器)