欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

Linux服务与管理-step1

发布时间:2023/12/16 50 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Linux服务与管理-step1 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

文章目录

    • Linux服务与管理
      • vuritual machine的快捷键:
      • 开始时创建的分区:
    • 第二章 文件管理
      • 根目录下的目录
      • 文件路径
          • 相对路径
          • 绝对路径
      • 目录与文件的操作
        • 目录操作
          • 创建目录 ==-p递归创建目录== `mkdir`
          • 查看目录 `pwd` `ls`
          • 删除目录 `rmdir`
        • 文件操作
          • 创建文件
          • 查看文件
          • 复制文件
          • 移动文件
          • 删除文件
        • 常见的问题
        • Vim编辑器
          • 常用命令
            • 光标定位命令
            • 文本编辑命令
            • 从命令模式进入其他模式
            • 末行模式常用的命令
      • 文件时间
      • 文件类型
    • 带三章 用户管理
      • 用户/组
        • UID和GID
        • 用户/组相关文件
          • 创建用户/组
          • 删除用户/组
          • 修改用户密码
          • 配置文件
          • su/sudo 命令
    • 第四章 文件权限
      • 基本权限UGO
        • 设置文件属性与权限
          • 修改属主和属组
          • 只修改文件属组
          • 所有子目录或文件同时修改属主或属组,`chown 和 chgrp 后边添加 -R`
          • `chmod` 赋值的功能格式
          • 为一个文件插入权限
          • 同时给所有对象添加权限
          • 同时给所有用户删除权限
          • 同时删除所有对象的全部权限
          • 一次给不同的对象增加或删除不同的权限
          • 使用递归参数 `-R`
          • 使用数字权限 4+2+1
      • 基本权限ACL
        • ACL的基本用法
          • `-m`修改当前文件的ACL权限
          • 新建用户 添加ACL权限
          • 为组添加权限
          • `-` 对文件没有任何权限
          • `-x` 刪除用户对文件的所有权限,此时用户属于other
          • `-b` 删除所有的扩展ACL权限,回到UGO基本权限
      • ACL高级特性
        • 最大有限权限 `mask`
        • `mask` 的作用和特性
        • `default` 继承
      • 高级权限
        • SUID权限
        • SGID权限
        • Sticky权限
      • 文件属性 chattr
      • 进程掩码 umask
    • 第五章 进程管理
      • 初始进程
      • 查看进程
        • 静态查看进程
        • 动态查看进程
      • 信号控制进程
        • kill命令 终止指定进程的运行
      • 进程的优先级
        • 使用top命令查看nice级别
        • 使用ps命令查看nice级别
      • 作业控制
    • 第六章 I/O重定向与管道
      • I/O重定向
        • 输出重定向
          • 错误输出重定向
          • 正确结果与错误结果都输出到相同的位置
          • 正确结果与错误结果都输出**重定向**到相同的位置
          • 正确的保存在 `right.txt`, 错误的保存在 null.txt`
        • 输入重定向
        • 重定向综合案例
          • 建立多行文件 `cat` 在键盘上输入文本按回车换行,Ctrl+d 结束
          • 用户自定义结束符号
          • 重定向建立脚本操纵Shell
          • 多条命令输出重定向
            • 两条命令重定向,添加括号
            • 让命令在后台运行,输出重定向到文件,终止后台程序
        • 重定向综合案例
          • 建立多行文件 `cat` 在键盘上输入文本按回车换行,Ctrl+d 结束
          • 用户自定义结束符号
          • 重定向建立脚本操纵Shell
          • 多条命令输出重定向
            • 两条命令重定向,添加括号

Linux服务与管理

vuritual machine的快捷键:

  • Ctrl+Alt 虚拟环境切换到物理环境
  • Ctrl +G 物理→虚拟
  • 开始时创建的分区:

    • /boot 引导分区
    • /swap 交换分区
    • / 根分区

    第二章 文件管理

    根目录下的目录

    目录说明
    /bin二进制可执行文件 常用命令
    /etc系统管理和配置文件
    /home用户文件根目录
    /usr用户安装的程序和系统程序
    /tmp临时文件
    /root系统管理员目录
    /var运行时需要改变数据的文件/大文件的溢出区
    /sbin二进制可执行文件,只有root可以访问

    文件路径

    • 相对路径
    / 根目录
    • 绝对路径
    ../ 当前目录的上级目录 ./当前目录

    目录与文件的操作

    目录操作

    • 创建目录 -p递归创建目录 mkdir
    [root@localhost /]# cd /temp [root@localhost temp]# mkdir 111 [root@localhost temp]# cd 111 [root@localhost 111]# mkdir -p 222/333/444 [root@localhost 111]# cd 222/333 [root@localhost 333]# ll 总用量 0 drwxr-xr-x. 2 root root 6 3月 16 22:23 444
    • 查看目录 pwd ls
    命令备注
    [root@localhost 333]# pwd
    /temp/111/222/333
    显示当前所在的目录
    [root@localhost 333]# pwd -P
    /temp/111/222/333
    -P显示实际工作目录
    [root@localhost 333]# ls -a
    . … 444
    查看隐藏的目录文件
    [root@localhost 333]# ls -l
    总用量 0
    drwxr-xr-x. 2 root root 6 3月 16 22:23 444
    查看目录与文件的属性
    [root@localhost 333]# cd
    [root@localhost ~]#
    cd直接回到根目录
    [root@localhost 333]# cd
    [root@localhost ~]# cd /temp/111/222/333
    [root@localhost 333]# cd -
    /root
    ==cd -==回到上次的目录
    • 删除目录 rmdir
    命名备注
    [root@localhost ~]# cd /temp/111/222
    [root@localhost 222]# rmdir 333
    rmdir: 删除 “333” 失败: 目录非空
    [root@localhost 222]# cd 333
    [root@localhost 333]# rmdir 444
    [root@localhost 333]#
    删除目录,非空会报错
    [root@localhost 222]# cd /temp/111/222/333
    [root@localhost 333]# cd …/
    [root@localhost 222]# cd …/
    [root@localhost 111]# rmdir 222
    rmdir: 删除 “222” 失败: 目录非空
    [root@localhost 111]# rmdir -p 222
    rmdir: 删除 “222” 失败: 目录非空
    [root@localhost 111]# rmdir -p 222/333
    -p连同上层空目录一起删除

    文件操作

    创建文件
  • touch filename 创建一个空白的文件
  • echo "content">>filename 写入新的内容到filename中
  • [root@localhost 111]# cd /temp/111 [root@localhost 111]# touch test.txt [root@localhost 111]# ll 总用量 0 -rw-r--r--. 1 root root 0 3月 16 22:47 test.txt [root@localhost 111]# echo "hello world" >>test.txt [root@localhost 111]# ll 总用量 4 -rw-r--r--. 1 root root 12 3月 16 22:48 test.txt

    echo设置文件、属性

    [root@localhost 111]# touch -d "2022-03-16 22:50" test.txt [root@localhost 111]# ll 总用量 4 -rw-r--r--. 1 root root 12 3月 16 2022 test.txt
    查看文件

    1.cat命令

    -n 显示行号

    -A 显示不可显示的换行符/制表符

    [root@localhost ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 [root@localhost ~]# cat -n /etc/hosts1 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain42 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 [root@localhost ~]# cat -A /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4$ ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6$

    2.more命令 以逐页的方式显示文件的内容

    空格键——下一页

    b——上一页

    [root@localhost ~]# more /etc/profile # /etc/profile# System wide environment and startup programs, for login setup # Functions and aliases go in /etc/bashrc

    3.less命令 分页显示

    pageUp——向上看文件

    pageDown——向下看文件 q键退出

    [root@localhost ~]# less /etc/profile

    4.head命令 看文件的前几行 -n

    [root@localhost ~]# head -3 /etc/profile # /etc/profile# System wide environment and startup programs, for login setup

    5.tail命令 看文件的后几行 -n

    -f ——查看动态文件

    [root@localhost ~]# tail -3 /etc/passwd ntp:x:38:38::/etc/ntp:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin shigen:x:1000:1000:shigen:/home/shigen:/bin/bash

    6.grep命令 对文件的内容过滤、搜索关键词,快速找到内容

    [root@localhost ~]# grep 'shigen' /etc/passwd shigen:x:1000:1000:shigen:/home/shigen:/bin/bash [root@localhost ~]# grep '^shigen' /etc/passwd shigen:x:1000:1000:shigen:/home/shigen:/bin/bash [root@localhost ~]# grep 'shigen$' /etc/passwd
    复制文件

    cp sourcefile targetfile

    [root@localhost ~]# cd /temp [root@localhost temp]# touch aa.txt [root@localhost temp]# echo 'hello shigen'>>aa.txt [root@localhost temp]# cat -n aa.txt1 hello shigen [root@localhost temp]# touch bb.txt [root@localhost temp]# echo 'hello world'>>bb.txt [root@localhost temp]# cat -n bb.txt1 hello world [root@localhost temp]# cp aa.txt bb.txt cp:是否覆盖"bb.txt"? y [root@localhost temp]# cat aa.txt hello shigen [root@localhost temp]# cat bb.txt hello shigen [root@localhost temp]# ll 总用量 8 drwxr-xr-x. 2 root root 22 3月 16 22:47 111 -rw-r--r--. 1 root root 13 3月 16 23:08 aa.txt -rw-r--r--. 1 root root 13 3月 16 23:10 bb.txt

    文件目录较长时,花括号括起不同的部分

    [root@localhost temp]# touch cc.txt [root@localhost temp]# echo 'hello cc'>>cc.txt [root@localhost temp]# cat -n cc.txt1 hello cc [root@localhost temp]# cd [root@localhost ~]# cp -rf /temp/{aa.txt,cc.txt} cp:是否覆盖"/temp/cc.txt"? y [root@localhost ~]# cat -n cc.txt cat: cc.txt: 没有那个文件或目录 [root@localhost ~]# cat -n /temp/cc.txt1 hello shigen
    移动文件

    mv sourcefile targetfile

    [root@localhost 111]# ll 总用量 4 -rw-r--r--. 1 root root 12 3月 16 2022 test.txt [root@localhost 111]# mv /temp/aa.txt /temp/111 [root@localhost 111]# ll 总用量 8 -rw-r--r--. 1 root root 13 3月 16 23:08 aa.txt -rw-r--r--. 1 root root 12 3月 16 2022 test.txt
    删除文件

    rm file2(目录文件或目录)

    rm -rf 删除目录和文件且不提示

    rm -rf / 进行递归操作

    [root@localhost 111]# cd /temp [root@localhost temp]# ll 总用量 8 drwxr-xr-x. 2 root root 36 3月 16 23:19 111 -rw-r--r--. 1 root root 13 3月 16 23:10 bb.txt -rw-r--r--. 1 root root 13 3月 16 23:13 cc.txt [root@localhost temp]# rm cc.txt rm:是否删除普通文件 "cc.txt"?y [root@localhost temp]# ll 总用量 4 drwxr-xr-x. 2 root root 36 3月 16 23:19 111 -rw-r--r--. 1 root root 13 3月 16 23:10 bb.txt

    常见的问题

    在Linux环境下正常打开Windows文件

    yum -y install dos2unix

    Vim编辑器

    [root@localhost ~]# which vim /usr/bin/vim [root@localhost ~]# yum -y install vim-enhanced 已加载插件:fastestmirror Loading mirror speeds from cached hostfile
    常用命令
    光标定位命令
    命令描述
    h,j,k,l光标上下左右移动 也可用方向键
    0 $光标移到行首 行尾
    gg G光标移到第一行首个字符的位置 最后一行首个字符的位置
    /字符串快速定位到字符串所在的行
    /^d定位首个字母为d的行
    /txt$定位结尾为txt的行
    文本编辑命令
    命令描述
    n yy复制当前行
    n dd删除当前行
    p粘贴
    x删除光标所在字符
    D从光标处删除到行尾
    u撤销
    ^r重做
    R进入替换模式
    从命令模式进入其他模式
    命令描述
    o进入编辑模式,光标下面另起一行
    a进入编辑模式 光标后一位
    i进入编辑模式 Esc 退出
    进入末行模式
    V进入可视行模式 多行会被选中
    v可视模式
    ^v可视块模式
    R替换模式
    末行模式常用的命令
    命令备注
    :w保存
    :q退出
    :wq保存并退出
    :w!强制保存
    :q!不保存强制退出
    :wq!
    :set nu显示行号
    :set nonu不显示行号
    :整数跳转到该行
    命令描述
    😒/abc/abd该行第一个abc替换成abd
    😒/abc/abd/g该行所有的abc替换成abd

    文件时间

    文件的三种时间: ①访问时间 ②修改时间 ③状态时间

    stat查看文件的详细信息

    [root@localhost ~]# stat /temp/111/a.txt文件:"/temp/111/a.txt"大小:1433 块:8 IO 块:4096 普通文件 设备:fd00h/64768d Inode:14464057 硬链接:1 权限:(0600/-rw-------) Uid:( 0/ root) Gid:( 0/ root) 环境:unconfined_u:object_r:default_t:s0 最近访问:2021-03-17 10:02:31.763021875 +0800 最近更改:2021-03-17 10:02:31.763021875 +0800 最近改动:2021-03-17 10:02:31.772021806 +0800 创建时间:-

    文件类型

    Linux 系统中的文件没有扩展名,修改扩展名无法修改文件的本质

    [root@localhost ~]# file /temp/111/a.txt /temp/111/a.txt: ASCII text [root@localhost ~]# ls -l /root 总用量 8 -rw-------. 1 root root 1794 3月 16 19:29 anaconda-ks.cfg -rw-r--r--. 1 root root 1842 3月 16 19:31 initial-setup-ks.cfg drwxr-xr-x. 2 root root 6 3月 16 20:25 公共 drwxr-xr-x. 2 root root 6 3月 16 20:25 模板'-' 开头为普通文件(文本,二进制,压缩文件) 'd' 目录文件 'b' 设备文件(块设备) 'c' 字符设备 's' 套接字文件 'p' 管道文件 'l' 链接文件

    带三章 用户管理

    用户/组

    UID和GID

    uid : 系统中唯一的能识别身份的

    gid:用户所属组的ID

    [root@localhost ~]# id uid=0(root) gid=0(root)=0(root) 环境=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

    ll /home查看文件的所有者 第三列为文件的所有者

    [root@localhost ~]# ll /home 总用量 4 drwx------. 16 shigen shigen 4096 3月 16 20:24 shigen

    ps aux|head 查看进程

    [root@localhost ~]# ps aux|head -4 USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.4 193968 4564 ? Ss 3月16 0:13 /usr/lib/systemd/systemd --switched-root --system --deserialize 22 root 2 0.0 0.0 0 0 ? S 3月16 0:00 [kthreadd] root 4 0.0 0.0 0 0 ? S< 3月16 0:00 [kworker/0:0H]

    ps aux|grep httpd查看httpd进程的用户名

    [root@localhost ~]# ps aux|grep httpd root 1469 0.3 0.5 224084 5048 ? Ss 12:52 0:00 /usr/sbin/httpd -DFOREGROUND apache 1470 0.0 0.3 226168 3100 ? S 12:52 0:00 /usr/sbin/httpd -DFOREGROUND apache 1471 0.0 0.3 226168 3100 ? S 12:52 0:00 /usr/sbin/httpd -DFOREGROUND apache 1472 0.0 0.3 226168 3100 ? S 12:52 0:00 /usr/sbin/httpd -DFOREGROUND apache 1473 0.0 0.3 226168 3100 ? S 12:52 0:00 /usr/sbin/httpd -DFOREGROUND apache 1474 0.0 0.3 226168 3100 ? S 12:52 0:00 /usr/sbin/httpd -DFOREGROUND root 1485 0.0 0.0 112824 976 pts/0 S+ 12:53 0:00 grep --color=auto httpd

    用户/组相关文件

    用户名和密码—— /etc/passwd /etc/shadow两个文件里面

    7个字段 username 密码占位符 UID GID 注释性描述 用户主目录 用户的Shell

    [root@localhost ~]# head -5 /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

    9个字段 username 加密后的密码 最近改动密码时间 密码不可变更时间(99999为没有限制) 密码重新变更时间 密码过期时间 密码过期宽恕时间 用户失效时间 保留

    [root@localhost ~]# head -5 /etc/shadow root:$6$7eyCQa06auzEltKU$N166xr.pllEgvYI0c3vm2rUPxqKGEi1UUuMdtHmGE.dIzCSZoZAoQNIeX4B.P52iYmH3o/60j2p0MjsOegsm31::0:99999:7::: bin:*:18353:0:99999:7::: daemon:*:18353:0:99999:7::: adm:*:18353:0:99999:7::: lp:*:18353:0:99999:7:::

    管理用户/组

    创建用户/组

    创建一个用户,grep过滤出该用户的信息

    若未指定任何组,系统默认给该用户创建一个和用户名相同的用户组作为用户主组

    [root@localhost ~]# useradd user1 [root@localhost ~]# grep 'user1' /etc/passwd /etc/shadow /etc/group /etc/passwd:user1:x:1001:1001::/home/user1:/bin/bash /etc/shadow:user1:!!:18703:0:99999:7::: /etc/group:user1:x:1001:

    -G 指定已存在的附加组

    [root@localhost ~]# groupadd group1 [root@localhost ~]# groupadd group2 [root@localhost ~]# useradd user2 -G group1 [root@localhost ~]# useradd user3 -G group1,group2 [root@localhost ~]# id user2 uid=1002(user2) gid=1004(user2)=1004(user2),1002(group1) [root@localhost ~]# id user3 uid=1003(user3) gid=1005(user3)=1005(user3),1002(group1),1003(group2) [root@localhost ~]# useradd user4 -G group2 [root@localhost ~]# gpasswd -d user4 group2 正在将用户“user4”从“group2”组中删除 [root@localhost ~]# id user4 uid=1004(user4) gid=1006(user4)=1006(user4) [root@localhost ~]# gpasswd -d user3 group2 正在将用户“user3”从“group2”组中删除 [root@localhost ~]# id user3 uid=1003(user3) gid=1005(user3)=1005(user3),1002(group1)

    查看组

    [root@localhost ~]# tail -10 /etc/group stapdev:x:158: tcpdump:x:72: shigen:x:1000: apache:x:48: user1:x:1001: group1:x:1002:user2,user3 group2:x:1003: user2:x:1004: user3:x:1005: user4:x:1006:

    useradd 命令的参数

    命令参数
    -d指定用户的主目录
    -u指定用户uid
    -g指定用户主组的名称或ID
    -G指定用户的附加组或列表
    -s指定用户登录的shell
    -m创建用户主目录
    -M不创建
    删除用户/组
    • 删除用户且删除 /home和 /var/spool/mail -r
    • 用户不存在删除 rm -rf
    [root@localhost ~]# id user4 uid=1004(user4) gid=1006(user4)=1006(user4) [root@localhost ~]# userdel user4 [root@localhost ~]# userdel user4 userdel:用户“user4”不存在 [root@localhost ~]# ls /home shigen user1 user2 user3 user4[root@localhost ~]# userdel -r user3 [root@localhost ~]# ls /home shigen user1 user2 user4 [root@localhost ~]# ls /var/spool/mail root rpc shigen user1 user2 user4
    修改用户密码

    任何用户都可以用 passwd 修改自己的密码 需要提供原密码

    [root@localhost ~]# tail -5 /etc/shadow tcpdump:!!:18702:::::: shigen:$6$gs9FX/fSSbFFBSVY$L8/D5kA4oX6WekeqmyzpX79ai7HLDDYRZzzgFt5/tNx/1MyYGkiCtAtiWMPC19j22bmqLpM8UFmPMrMzVRAx31:18702:0:99999:7::: apache:!!:18703:::::: user1:!!:18703:0:99999:7::: user2:!!:18703:0:99999:7::: [root@localhost ~]# passwd user2 更改用户 user2 的密码 。isbn978. 新的 密码: 重新输入新的 密码: passwd:所有的身份验证令牌已经成功更新。 [root@localhost ~]# su user2 [user2@localhost root]$ passwd 更改用户 user2 的密码 。 为 user2 更改 STRESS 密码。 (当前)UNIX 密码: 新的 密码: 无效的密码: 密码与原来的太相似 新的 密码: 重新输入新的 密码:.789nbsi passwd:所有的身份验证令牌已经成功更新。 [user2@localhost root]$

    安全用户

    useradd user -s /sbin/nologin 本地和远程都不能登录系统,无法管理系统

    查看可登录系统的用户

    [root@localhost ~]# grep 'bash$' /etc/passwd root:x:0:0:root:/root:/bin/bash shigen:x:1000:1000:shigen:/home/shigen:/bin/bash user1:x:1001:1001::/home/user1:/bin/bash user2:x:1002:1004::/home/user2:/bin/bash
    配置文件

    /etc/login.defs /etc/default/useradd

    是命令useradd 的配置文件 决定创建用户的默认参数

    [root@localhost ~]# cat -n /etc/login.defs1 #2 # Please note that the parameters in this configuration file control the3 # behavior of the tools from the shadow-utils component. None of these4 # tools uses the PAM mechanism, and the utilities that use PAM (such as the5 # passwd command) should therefore be configured elsewhere. Refer to6 # /etc/pam.d/system-auth for more information.7 #89 # *REQUIRED*10 # Directory where mailboxes reside, _or_ name of file, relative to the11 # home directory. If you _do_ define both, MAIL_DIR takes precedence.12 # QMAIL_DIR is for Qmail13 #14 #QMAIL_DIR Maildir15 MAIL_DIR /var/spool/mail16 #MAIL_FILE .mail1718 # Password aging controls:19 #20 # PASS_MAX_DAYS Maximum number of days a password may be used.21 # PASS_MIN_DAYS Minimum number of days allowed between password changes.22 # PASS_MIN_LEN Minimum acceptable password length.23 # PASS_WARN_AGE Number of days warning given before a password expires.24 #25 PASS_MAX_DAYS 9999926 PASS_MIN_DAYS 027 PASS_MIN_LEN 528 PASS_WARN_AGE 72930 #31 # Min/max values for automatic uid selection in useradd32 #33 UID_MIN 100034 UID_MAX 6000035 # System accounts36 SYS_UID_MIN 20137 SYS_UID_MAX 9993839 #40 # Min/max values for automatic gid selection in groupadd41 #42 GID_MIN 100043 GID_MAX 6000044 # System accounts45 SYS_GID_MIN 20146 SYS_GID_MAX 9994748 #49 # If defined, this command is run when removing a user.50 # It should remove any at/cron/print jobs etc. owned by51 # the user to be removed (passed as the first argument).52 #53 #USERDEL_CMD /usr/sbin/userdel_local5455 #56 # If useradd should create home directories for users by default57 # On RH systems, we do. This option is overridden with the -m flag on58 # useradd command line.59 #60 CREATE_HOME yes6162 # The permission mask is initialized to this value. If not specified,63 # the permission mask will be initialized to 022.64 UMASK 0776566 # This enables userdel to remove user groups if no members exist.67 #68 USERGROUPS_ENAB yes6970 # Use SHA512 to encrypt password.71 ENCRYPT_METHOD SHA512
    su/sudo 命令

    su命令切换用户

    普通用户→root 需要密码

    root→普通用户 不需要密码

    [root@localhost ~]# su user2 [user2@localhost root]$ su root 密码: [root@localhost ~]#

    加入wheel组的普通用户可以使用sudo 命令来执行系统的有关操作

    必须先输入密码 5分钟有效时间

    [root@localhost ~]# su user3 [user3@localhost root]$ id user3 uid=1003(user3) gid=1005(user3)=1005(user3),10(wheel) [user3@localhost root]$ useradd user10 useradd: Permission denied. useradd:无法锁定 /etc/passwd,请稍后再试。 [user3@localhost root]$ sudo useradd user10我们信任您已经从系统管理员那里了解了日常注意事项。 总结起来无外乎这三点:#1) 尊重别人的隐私。#2) 输入前要先考虑(后果和风险)。#3) 权力越大,责任越大。[sudo] user3 的密码: [user3@localhost root]$ sudo id user10 uid=1004(user10) gid=1006(user10)=1006(user10)

    第四章 文件权限

    允许一个用户或者用户组依规定的方式去访问某个文件

    基本权限UGO

    Uowner属主
    Ggroup属组
    Oother其他用户
    rread4
    wwrite2
    xexecute1
    [root@localhost ~]# ll /temp 总用量 4 drwxr-xr-x. 2 root root 60 3月 17 10:02 111 -rw-r--r--. 1 root root 13 3月 16 23:10 bb.txt

    设置文件属性与权限

    三个修改权限的命令

    chown修改文件的属主、属组
    chgrp修改文件属组
    chmod修改文件属性
    修改属主和属组
    [root@localhost ~]# chown user1.group2 premission.txt [root@localhost ~]# ll premission.txt -rw-r--r--. 1 user1 group2 0 3月 17 16:09 premission.txt
    只修改文件属组
    [root@localhost ~]# tail -5 /etc/group group1:x:1002: group2:x:1003:user10 user2:x:1004: user3:x:1005: user10:x:1006: [root@localhost ~]# ll premission.txt -rw-r--r--. 1 user1 group2 0 3月 17 16:09 premission.txt [root@localhost ~]# chgrp group1 premission.txt [root@localhost ~]# ll premission.txt -rw-r--r--. 1 user1 group1 0 3月 17 16:09 premission.txt
    所有子目录或文件同时修改属主或属组,chown 和 chgrp 后边添加 -R
    [root@localhost temp]# touch file{1..10}.txt [root@localhost temp]# ll 总用量 4 drwxr-xr-x. 2 root root 60 3月 17 10:02 111 -rw-r--r--. 1 root root 13 3月 16 23:10 bb.txt -rw-r--r--. 1 root root 0 3月 17 16:25 file10.txt -rw-r--r--. 1 root root 0 3月 17 16:25 file1.txt ...... -rw-r--r--. 1 root root 0 3月 17 16:25 file9.txt [root@localhost temp]# chown -R user10:group2 /temp [root@localhost temp]# ll 总用量 4 drwxr-xr-x. 2 user10 group2 60 3月 17 10:02 111 -rw-r--r--. 1 user10 group2 13 3月 16 23:10 bb.txt -rw-r--r--. 1 user10 group2 0 3月 17 16:25 file10.txt -rw-r--r--. 1 user10 group2 0 3月 17 16:25 file1.txt ...... -rw-r--r--. 1 user10 group2 0 3月 17 16:25 file9.txt
    chmod 赋值的功能格式
    命令对象赋值符权限类型目标文件
    u+r——可读取文件内容 列出目录内容
    chmodg-w——修改文件内容 创建或删除目录中的任意一文件file
    o=e——将文件作为命令执行 可访问目录的内容(取决于目录中文件权限)
    a
    为一个文件插入权限
    [root@localhost temp]# chmod g+w file4.txt [root@localhost temp]# ll file4.txt -rw-rw-r--. 1 user10 group2 0 3月 17 16:25 file4.txt
    同时给所有对象添加权限
    [root@localhost temp]# chmod a=rwx file5.txt [root@localhost temp]# ll file5.txt -rwxrwxrwx. 1 user10 group2 0 3月 17 16:25 file5.txt
    同时给所有用户删除权限
    [root@localhost temp]# ll file5.txt -rwxrwxrwx. 1 user10 group2 0 3月 17 16:25 file5.txt [root@localhost temp]# chmod a-x file5.txt [root@localhost temp]# ll file5.txt -rw-rw-rw-. 1 user10 group2 0 3月 17 16:25 file5.txt
    同时删除所有对象的全部权限
    [root@localhost temp]# chmod a=- file5.txt [root@localhost temp]# ll file5.txt ----------. 1 user10 group2 0 3月 17 16:25 file5.txt
    一次给不同的对象增加或删除不同的权限
    [root@localhost temp]# ll file6.txt -rw-r--r--. 1 user10 group2 0 3月 17 16:25 file6.txt [root@localhost temp]# chmod u-x,g+x,o=w file6.txt [root@localhost temp]# ll file6.txt -rw-r-x-w-. 1 user10 group2 0 3月 17 16:25 file6.txt
    使用递归参数 -R
    [root@localhost temp]# chmod -R u=rwx,g-x,o=r 111 [root@localhost temp]# ll 111 总用量 16 -rwxr--r--. 1 user10 group2 13 3月 16 23:08 aa.txt -rwx---r--. 1 user10 group2 1433 3月 17 10:02 a.txt -rwxr--r--. 1 user10 group2 12 3月 16 2022 test.txt -rwxr--r--. 1 user10 group2 1440 3月 17 09:55 :w!
    使用数字权限 4+2+1
    [root@localhost temp]# chmod 000 file8.txt [root@localhost temp]# ll file8.txt ----------. 1 user10 group2 0 3月 17 16:25 file8.txt

    基本权限ACL

    ACL(Access Control List) 提供传统的UGO r,w,x之外的权限设置

    ACL的基本用法

    [root@localhost temp]# getfacl test.txt # file: test.txt # owner: root # group: root user::rw- group::r-- other::r--[root@localhost temp]# ll test.txt -rw-r--r--. 1 root root 0 3月 18 12:35 test.txt
    -m修改当前文件的ACL权限
    [root@localhost temp]# setfacl -m u:user1:rw test.txt [root@localhost temp]# ll test.txt -rw-rw-r--+ 1 root root 0 3月 18 12:35 test.txt[root@localhost temp]# getfacl test.txt # file: test.txt # owner: root # group: root user::rw- user:user1:rw- group::r-- mask::rw- other::r--
    新建用户 添加ACL权限
    [root@localhost temp]# useradd user9 -G user10 [root@localhost temp]# id user9 uid=1005(user9) gid=1007(user9)=1007(user9),1006(user10) [root@localhost temp]# setfacl -m u:user9:rwx test.txt [root@localhost temp]# getfacl test.txt # file: test.txt # owner: root # group: root user::rw- user:user1:rw- user:user9:rwx group::r-- mask::rwx other::r--
    为组添加权限
    [root@localhost temp]# setfacl -m g:user10:rw test.txt [root@localhost temp]# getfacl test.txt # file: test.txt # owner: root # group: root user::rw- user:user1:rw- user:user9:rwx group::r-- group:user10:rw- mask::rwx other::r--
    - 对文件没有任何权限
    [root@localhost temp]# useradd test9 [root@localhost temp]# setfacl -m u:test9:- test.txt [root@localhost temp]# ll test.txt -rw-rwxr--+ 1 root root 0 3月 18 12:35 test.txt [root@localhost temp]# getfacl test.txt # file: test.txt # owner: root # group: root user::rw- user:user1:rw- user:user9:rwx user:test9:--- group::r-- group:user10:rw- mask::rwx other::r-- # 已经无法读取文件 [root@localhost temp]# echo 'hello world' >>test.txt [root@localhost temp]# cat test.txt hello world [root@localhost temp]# su test9 [test9@localhost temp]$ cat test.txt cat: test.txt: 权限不够
    -x 刪除用户对文件的所有权限,此时用户属于other
    [root@localhost temp]# getfacl test.txt # file: test.txt # owner: root # group: root user::rw- user:user1:rw- user:user9:rwx user:test9:--- group::r-- group:user10:rw- mask::rwx other::r--[root@localhost temp]# setfacl -x u:test9 test.txt [root@localhost temp]# getfacl test.txt # file: test.txt # owner: root # group: root user::rw- user:user1:rw- user:user9:rwx group::r-- group:user10:rw- mask::rwx other::r--[root@localhost temp]# su test9 [test9@localhost temp]$ cat test.txt hello world
    -b 删除所有的扩展ACL权限,回到UGO基本权限
    [root@localhost temp]# setfacl -b test.txt [root@localhost temp]# getfacl test.txt # file: test.txt # owner: root # group: root user::rw- group::r-- other::r--

    ACL高级特性

    最大有限权限 mask

    系统给用户赋予的ACL权限和mask的权限逻辑“相与”,之后的权限才是用户真正的权限

    [root@localhost temp]# setfacl -m u:user10:r,u:user9:rw,g:group2:rwx test.txt [root@localhost temp]# getfacl test.txt # file: test.txt # owner: root # group: root user::rw- user:user10:r-- user:user9:rw- group::r-- group:group2:rwx mask::rwx other::r-- [root@localhost temp]# setfacl -m mask::r test.txt [root@localhost temp]# getfacl test.txt # file: test.txt # owner: root # group: root user::rw- user:user10:r-- user:user9:rw- #effective:r-- group::r-- group:group2:rwx #effective:r-- mask::r-- other::r--[user10@localhost temp]$ echo 'aaa' >>test.txt bash: test.txt: 权限不够

    将other权限置空,方便管理

    [root@localhost temp]# setfacl -m o::- test.txt [root@localhost temp]# ll test.txt -rwxr-----+ 1 root root 12 3月 18 13:13 test.txt

    mask 的作用和特性

    临时降低用户/组的权限

    [root@localhost 222]# setfacl -m mask:- file10.txt [root@localhost 222]# getfacl file10.txt # file: file10.txt # owner: user # group: group1 user::rw- user:aa:r-- #effective:--- group::r-x #effective:--- group:g1:rw- #effective:--- mask::--- other::--- # ACL修改后,mask会自动还原 [root@localhost 222]# setfacl -m u:user:rwx file10.txt [root@localhost 222]# getfacl file10.txt # file: file10.txt # owner: user # group: group1 user::rw- user:user:rwx user:aa:r-- group::r-x group:g1:rw- mask::rwx other::---

    default 继承

    d 可以继承上一个目录的权限

    [root@localhost ~]# mkdir /temp [root@localhost ~]# setfacl -m u:user:rwx /temp [root@localhost ~]# su user [user@localhost root]$ cd /temp [user@localhost temp]$ touch a.txt [user@localhost temp]$ exit exit [root@localhost ~]# mkdir /temp/111 [root@localhost ~]# su user [user@localhost root]$ cd /temp/111 [user@localhost 111]$ touch b.txt touch: 无法创建"b.txt": 权限不够 # 切换到user账户,无法创建文件# 使用==default==命令可以继承上一个目录的权限 [root@localhost ~]# mkdir /temp/111 [root@localhost ~]# cd /temp/111 [root@localhost 111]# su user [user@localhost 111]$ touch a.txt [user@localhost 111]$ ll 总用量 0 -rw-rw-r--+ 1 user user 0 3月 21 16:39 a.txt [user@localhost 111]$ getfacl /temp getfacl: Removing leading '/' from absolute path names # file: temp # owner: root # group: root user::rwx group::r-x other::r-x default:user::rwx default:user:user:rwx default:group::r-x default:mask::rwx default:other::r-x

    高级权限

    SUID权限

    SGID权限

    Sticky权限

    文件属性 chattr

    为了保护系统文件,Linux系统会用 chattr 命令改变文件的隐藏属性

    [root@localhost temp]# touch file1 file2 file3 # lsattr 查看三个文件的隐藏属性 [root@localhost temp]# lsattr file1 file2 file3 ---------------- file1 ---------------- file2 ---------------- file3 # man 工具查看chattr 命令的使用方法 [root@localhost temp]# man chattr # 为file1文件增加‘a’属性,此时Vim编辑器不能写入文本,需要使用echo命令追加写入-----用于日志文件,不能删除,文本只能追加 [root@localhost temp]# chattr +a file1 [root@localhost temp]# lsattr file1 -----a---------- file1 [root@localhost temp]# vim file1[root@localhost temp]# echo 'hello world'>>file1 [root@localhost temp]# cat file1 hello world [root@localhost temp]# echo 'hello world'>file1 bash: file1: 不允许的操作 [root@localhost temp]# rm -rf file1 rm: 无法删除"file1": 不允许的操作

    给文件增加 i 属性之后,文件不支持任何形式的修改,只能读取

    [root@localhost temp]# lsattr ---------------- ./111 -----a---------- ./file1 ---------------- ./file2 ---------------- ./file3 ---------------- ./file1~ ---------------- ./filez~ [root@localhost temp]# chattr +i file2 [root@localhost temp]# echo 'hello world'>>file2 bash: file2: 权限不够 [root@localhost temp]# rm -rf file2 rm: 无法删除"file2": 不允许的操作 [root@localhost temp]#

    进程掩码 umask

    用户创建目录或者文件时,系统赋予一个默认的权限, umask 指定权限的默认值,表示要减掉的权限

    • 创建目录,创建文件,查看 umask 默认的目录和文件的权限
    [root@localhost temp]# mkdir file01 [root@localhost temp]# cd file01 [root@localhost file01]# touch a.txt [root@localhost file01]# umask 0022 [root@localhost file01]# ll -d /temp/file01 drwxrwxr-x+ 2 root root 19 3月 21 17:25 /temp/file01 [root@localhost file01]# ll a.txt -rw-rw-r--+ 1 root root 0 3月 21 17:25 a.txt
    • 把 unamsk 的默认权限设置成0777,创建的文件、目录的权限基本为000
    • 可以在用户目录下的 /home/etc/login.defs 文件更改 60行 UMASK 的权限值
    [root@localhost file01]# umask 0777 [root@localhost file01]# mkdir dir1 [root@localhost file01]# cd dir1 [root@localhost dir1]# touch file.txt[root@localhost file01]# ll -d dir1 dir1/file.txt drwxrwxr-x+ 2 root root 22 3月 21 17:35 dir1 -rw-rw-r--+ 1 root root 0 3月 21 17:35 dir1/file.txt

    第五章 进程管理

    初始进程

    进程: 系统进行资源分配和调度的一个独立单位

    系统的四大核心资源: Disk IO, Memory, CPU , Network

    查看进程

    静态查看进程

    • ps aux 命令查看当前目录进程
    [root@localhost ~]# ps aux # 运行进程的用户 进程ID CPU利用率 内存占用率 占用虚拟内存 占用实际内存 进程运行的终端 进程状态 累计占用CPU的时间 进程发起者 # STAT状态 R——运行 S——休眠 D——不可中断休眠 T——停止的进程 Z——僵死的进程 X——死掉的进程 USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.4 193832 4440 ? Ss 14:28 0:06 /usr/lib/systemd/systemd --switched-root --sys root 2 0.0 0.0 0 0 ? S 14:28 0:00 [kthreadd] root 4 0.0 0.0 0 0 ? S< 14:28 0:00 [kworker/0:0H] root 6 0.0 0.0 0 0 ? S 14:28 0:01 [ksoftirqd/0] root 7 0.0 0.0 0 0 ? S 14:28 0:00 [migration/0] root 8 0.0 0.0 0 0 ? S 14:28 0:00 [rcu_bh] root 9 0.0 0.0 0 0 ? R 14:28 0:03 [rcu_sched] root 10 0.0 0.0 0 0 ? S< 14:28 0:00 [lru-add-drain] root 11 0.0 0.0 0 0 ? S 14:28 0:00 [watchdog/0] root 13 0.0 0.0 0 0 ? S 14:28 0:00 [kdevtmpfs]
    • ps aux --sort -%cpu 按照CPU占用率降序排列
    [root@localhost ~]# ps aux --sort -%cpu USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 2032 0.5 16.9 2784232 170752 ? Sl 14:29 1:35 /usr/bin/gnome-shell root 712 0.4 0.1 221832 1688 ? Ssl 14:28 1:08 /usr/bin/vmtoolsd root 2284 0.3 0.8 608676 8864 ? Rl 14:29 0:59 /usr/bin/vmtoolsd -n vmusr root 404 0.1 0.0 0 0 ? S 14:28 0:22 [xfsaild/dm-0] root 1262 0.1 3.6 342604 37044 tty1 Ssl+ 14:28 0:20 /usr/bin/X :0 -background none -noreset -audit root 2279 0.1 12.9 1216376 130332 ? Sl 14:29 0:21 /usr/bin/gnome-software --gapplication-service root 6595 0.1 0.0 0 0 ? S 18:45 0:01 [kworker/0:2] root 6812 0.1 0.5 160988 5784 ? Ss 19:01 0:00 sshd: root@pts/1
    • ps -ef 命令查看UID,PID,PPID等信息
    [root@localhost ~]# ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 14:28 ? 00:00:06 /usr/lib/systemd/systemd --switched-root --system --deserialize root 2 0 0 14:28 ? 00:00:00 [kthreadd] root 4 2 0 14:28 ? 00:00:00 [kworker/0:0H] root 6 2 0 14:28 ? 00:00:01 [ksoftirqd/0] root 7 2 0 14:28 ? 00:00:00 [migration/0] root 8 2 0 14:28 ? 00:00:00 [rcu_bh] root 9 2 0 14:28 ? 00:00:03 [rcu_sched] root 10 2 0 14:28 ? 00:00:00 [lru-add-drain]
    • ps axo 命令自定义显示字段
    [root@localhost ~]# ps axo pid,ppid,user,%cpu,commandPID PPID USER %CPU COMMAND1 0 root 0.0 /usr/lib/systemd/systemd --switched-root --system --deserialize 222 0 root 0.0 [kthreadd]4 2 root 0.0 [kworker/0:0H]6 2 root 0.0 [ksoftirqd/0]7 2 root 0.0 [migration/0]8 2 root 0.0 [rcu_bh]9 2 root 0.0 [rcu_sched]10 2 root 0.0 [lru-add-drain]
    • 查看指定进程的PID
  • cat 命令
  • [root@localhost ~]# cat /run/sshd.pid 1150

    2.pidof 命令

    [root@localhost ~]# pidof sshd 6812 2710 1150

    3.pgrep 命令

    [root@localhost ~]# pgrep sshd 1150 2710 6812

    动态查看进程

    上边部分为整体信息,下边部分为每一个进程信息。默认更新的时为3S,enter键立即更新

    # top: 当前时间 # up: 启动后的运行时间 # users: 用户数 # load average : CPU在最近1min 5min 15min 平均负载值 # Tasks: 进程数 # Kib Men: 内存使用情况[root@localhost ~]# top top - 19:22:26 up 4:54, 3 users, load average: 0.00, 0.01, 0.05 Tasks: 212 total, 1 running, 211 sleeping, 0 stopped, 0 zombie %Cpu(s): 0.3 us, 0.3 sy, 0.0 ni, 99.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem : 1005408 total, 77700 free, 743924 used, 183784 buff/cache KiB Swap: 1048572 total, 908540 free, 140032 used. 81204 avail MemPID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND7106 root 20 0 162100 2340 1576 R 1.6 0.2 0:05.47 top2284 root 20 0 608676 8864 2204 S 0.7 0.9 1:01.70 vmtoolsd404 root 20 0 0 0 0 S 0.3 0.0 0:23.20 xfsaild/dm-01 root 20 0 193832 4440 2456 S 0.0 0.4 0:06.28 systemd2 root 20 0 0 0 0 S 0.0 0.0 0:00.03 kthreadd
    • M——内存占用率降序排序
    PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND2032 root 20 0 2784232 170752 26260 S 0.0 17.0 1:36.87 gnome-shell2279 root 20 0 1216376 130332 24404 S 0.0 13.0 0:21.58 gnome-software1262 root 20 0 342604 37044 17228 S 0.0 3.7 0:20.35 X3058 root 20 0 1069884 27476 12596 S 0.0 2.7 0:11.79 nautilus2263 root 20 0 1041680 17472 5968 S 0.0 1.7 0:02.05 nautilus-deskto2341 root 20 0 525612 11132 2388 S 0.0 1.1 0:00.49 tracker-store2301 root 39 19 736712 10812 3264 S 0.0 1.1 0:00.72 tracker-extract2284 root 20 0 608676 8864 2204 S 0.3 0.9 1:02.90 vmtoolsd
    • P——CPU占用率降序排序
    PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND7106 root 20 0 162100 2340 1576 R 3.4 0.2 0:11.39 top712 root 20 0 221832 1688 1280 S 0.7 0.2 1:12.71 vmtoolsd6812 root 20 0 160988 5784 4432 S 0.7 0.6 0:01.62 sshd7104 root 20 0 0 0 0 S 0.7 0.0 0:00.57 kworker/0:31 root 20 0 193832 4440 2456 S 0.0 0.4 0:06.35 systemd2 root 20 0 0 0 0 S 0.0 0.0 0:00.03 kthreadd4 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
    • N——PID数值大小排序
    PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND7212 root 20 0 108052 352 280 S 0.0 0.0 0:00.00 sleep7198 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kworker/0:27189 postfix 20 0 91896 4092 3080 S 0.0 0.4 0:00.02 pickup7171 root 20 0 0 0 0 S 0.0 0.0 0:00.27 kworker/0:17106 root 20 0 162100 2340 1576 R 1.6 0.2 0:12.21 top7104 root 20 0 0 0 0 S 0.3 0.0 0:00.67 kworker/0:3
    • R——对排序进行反转 F——显示自定义显示字段 ,上下键移动,空格选中 q——退出 W——保存自定义显示字段
    • 按 1 显示CPU负载
    top - 19:40:34 up 5:12, 3 users, load average: 0.01, 0.06, 0.05 Tasks: 213 total, 1 running, 212 sleeping, 0 stopped, 0 zombie %Cpu0 : 0.3 us, 0.3 sy, 0.0 ni, 99.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem : 1005408 total, 74924 free, 746128 used, 184356 buff/cache KiB Swap: 1048572 total, 908540 free, 140032 used. 78676 avail Mem
    • top -d 1 设置每秒刷新一次
    [root@localhost ~]# top -d 1top - 19:42:32 up 5:14, 3 users, load average: 0.45, 0.18, 0.09 Tasks: 213 total, 2 running, 211 sleeping, 0 stopped, 0 zombie %Cpu(s): 9.4 us, 4.2 sy, 0.0 ni, 85.4 id, 0.0 wa, 0.0 hi, 1.0 si, 0.0 st KiB Mem : 1005408 total, 69940 free, 721424 used, 214044 buff/cache KiB Swap: 1048572 total, 898556 free, 150016 used. 97360 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 2032 root 20 0 2786696 183632 39920 S 6.9 18.3 1:46.19 gnome-shell 1262 root 20 0 348592 44132 18288 S 5.9 4.4 0:22.71 X 7513 root 20 0 162100 2340 1596 R 4.9 0.2 0:00.53 top 7466 root 20 0 687948 27376 16524 S 2.0 2.7 0:01.47 gnome-terminal- 1151 root 20 0 574288 904 336 S 1.0 0.1 0:06.67 tuned 1 root 20 0 193832 3152 1168 S 0.0 0.3 0:06.84 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
    • 添加 -p 查看一个到多个进程信息
    [root@localhost ~]# top -d 1 -p 10636,1 top - 19:46:23 up 5:18, 3 users, load average: 0.09, 0.13, 0.09 Tasks: 1 total, 0 running, 1 sleeping, 0 stopped, 0 zombie %Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem : 1005408 total, 75088 free, 695856 used, 234464 buff/cache KiB Swap: 1048572 total, 898556 free, 150016 used. 128856 avail MemPID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND1 root 20 0 193832 4316 2332 S 0.0 0.4 0:06.93 systemd
    • 添加 -u 查看指定用户进程, -n 设置刷新次数,完成后自动退出
    [root@localhost ~]# top -d 1 -u user -n 3top - 19:53:46 up 5:25, 3 users, load average: 0.10, 0.07, 0.06 Tasks: 217 total, 1 running, 216 sleeping, 0 stopped, 0 zombie %Cpu(s): 5.6 us, 1.1 sy, 0.0 ni, 93.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem : 1005408 total, 72744 free, 716176 used, 216488 buff/cache KiB Swap: 1048572 total, 894972 free, 153600 used. 103456 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 4304 user 20 0 116548 2572 1348 S 0.0 0.3 0:00.14 bash 5104 user 20 0 116548 2624 1396 S 0.0 0.3 0:00.13 bash 7761 user 20 0 116552 2924 1692 S 0.0 0.3 0:00.07 bash

    信号控制进程

    kill命令 终止指定进程的运行

    常用信号:

    HUP 1 终端断线 INT 2 中断(同 Ctrl + C) QUIT 3 退出(同 Ctrl + \) TERM 15 终止 KILL 9 强制终止 CONT 18 继续(与STOP相反, fg/bg命令) STOP 19 暂停(同 Ctrl + Z)
    • kill -l 查看全部信号
    [root@localhost ~]# kill -l1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8 43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2 63) SIGRTMAX-1 64) SIGRTMAX
    • 教材案例
    # 安装vsftpd服务 yum -y install vsftpd # 启动服务 systemctl start vsftpd # pidof 查看进程PID pidof vsftpd # 重新加载进程 kill -1 11847 # 终止服务 systemcal stop vsftpd
    • killall vsftpd 终止某个指令名称的服务所对应的全部进程
    [root@localhost ~]# systemctl start vsftpd [root@localhost ~]# ps aux|grep vsftpd root 10570 0.0 0.0 53288 580 ? Ss 11:34 0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf root 10578 0.0 0.0 112824 980 pts/1 S+ 11:35 0:00 grep --color=auto vsftpd [root@localhost ~]# pidof vsftpd 10570 [root@localhost ~]# killall vsftpd [root@localhost ~]# pidof vsftpd [root@localhost ~]#

    进程的优先级

    优先级决定了进程被CPU处理的顺序

    使用top命令查看nice级别

    NI列表示的是实际nice级别,PR将nice级别映射到更大优先级队列

    PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 2032 root 20 0 2786696 100020 30912 S 1.7 9.9 2:37.59 gnome-shell 10798 root 20 0 162100 2316 1576 R 1.3 0.2 0:00.98 top 404 root 20 0 0 0 0 S 0.3 0.0 0:39.13 xfsaild/dm-0 712 root 20 0 221832 1648 1240 S 0.3 0.2 1:59.21 vmtoolsd 1262 root 20 0 348592 39616 17584 S 0.3 3.9 0:38.49 X 2284 root 20 0 608676 4600 1924 S 0.3 0.5 1:43.28 vmtoolsd 9238 root 20 0 0 0 0 S 0.3 0.0 0:12.32 kworker/u128:0 10629 root 20 0 0 0 0 S 0.3 0.0 0:00.27 kworker/0:2 1 root 20 0 194128 5276 2956 S 0.0 0.5 0:10.03 systemd

    使用ps命令查看nice级别

    [root@localhost ~]# ps axo pid,command,nice,cls --sort=-nicePID COMMAND NI CLS32 [khugepaged] 19 TS2301 /usr/libexec/tracker-extrac 19 TS2305 /usr/libexec/tracker-miner- - IDL2310 /usr/libexec/tracker-miner- 19 TS2318 /usr/libexec/tracker-miner- - IDL31 [ksmd] 5 TS700 /usr/libexec/rtkit-daemon 1 TS

    作业控制

    允许一个shell实例运行和管理多个命令

    # 创建后台进程,&为后台符 [root@localhost ~]# sleep 7000 & [1] 10944 # 创建前台进程 Ctrl+c 停止进程 [root@localhost ~]# sleep 8000 ^Z [2]+ 已停止 sleep 8000[root@localhost ~]# ps aux|grep sleep root 10944 0.0 0.0 108052 356 pts/1 S 11:47 0:00 sleep 7000 root 10951 0.0 0.0 108052 352 pts/1 T 11:48 0:00 sleep 8000 root 10959 0.0 0.0 112824 980 pts/1 S+ 11:48 0:00 grep --color=auto sleep # jobs 显示当前shell作业列表和作业状态 [root@localhost ~]# jobs [1]- 运行中 sleep 7000 & [2]+ 已停止 sleep 8000 # bg 2 将作业从前台--->后台 [root@localhost ~]# bg 2 [2]+ sleep 8000 & # fg 将作业后台-->前台 [root@localhost ~]# fg 2 sleep 8000 ^C [root@localhost ~]# jobs [1]+ 运行中 sleep 7000 & # kill 命令终止 [root@localhost ~]# kill %1 [1]+ 已终止 sleep 7000 [root@localhost ~]#

    使用 fg 命令默认调用最近一次的进程

    [root@localhost ~]# sleep 1234 & [1] 11336 [root@localhost ~]# sleep 4321 & [2] 11343 [root@localhost ~]# jobs [1]- 运行中 sleep 1234 & [2]+ 运行中 sleep 4321 & [root@localhost ~]# fg sleep 4321

    第六章 I/O重定向与管道

    I/O重定向

    • 显示当前时间,并指向文件
    [root@localhost ~]# cd /temp/111 [root@localhost 111]# ll 总用量 0 -rw-rw-r--+ 1 user user 0 3月 21 16:39 a.txt [root@localhost 111]# date 2021年 03月 24日 星期三 10:49:03 CST [root@localhost 111]# date>a.txt [root@localhost 111]# ll a.txt -rw-rw-r--+ 1 user user 43 3月 24 10:49 a.txt [root@localhost 111]# [root@localhost ~]# cd /temp/111/ # date默认将结果输出到终端,此时文件描述符为1 改变输出的方向--->a.txt——重定向 [root@localhost 111]# date 1>a.txt # 输出方向 结果在终端显示 [root@localhost 111]# date 2>a.txt 2021年 03月 24日 星期三 11:02:19 CST # 输入命令是错误的,执行结果输出到终端上,重定向到a.txt [root@localhost 111]# linux 2>a.txt [root@localhost 111]# cat a.txt bash: linux: 未找到命令... # 输入重定向把键盘输入改为文件输入 [root@localhost 111]# cat 0</etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

    文件描述符的定义**

    文件描述符通道名通道名说明默认连接用法
    0stdin标准输入键盘只读
    1stdout输出终端只写
    2stderr错误终端只写
    3+filename其他文件rw/w/r

    输出重定向

    写法: 1> ——覆盖 1>>——追加

    [root@localhost 111]# cd /temp/111 [root@localhost 111]# ll 总用量 0 -rw-rw-r--+ 1 user user 0 3月 24 11:25 a.txt [root@localhost 111]# cat a.txt [root@localhost 111]# date>a.txt [root@localhost 111]# date>a.txt [root@localhost 111]# date>a.txt [root@localhost 111]# cat a.txt 2021年 03月 24日 星期三 12:09:32 CST [root@localhost 111]# date>>a.txt [root@localhost 111]# date>>a.txt [root@localhost 111]# date>>a.txt [root@localhost 111]# cat a.txt 2021年 03月 24日 星期三 12:09:32 CST 2021年 03月 24日 星期三 12:09:48 CST 2021年 03月 24日 星期三 12:09:51 CST 2021年 03月 24日 星期三 12:09:52 CST
    错误输出重定向
    [root@localhost 111]# ls /home/linux ls: 无法访问/home/linux: 没有那个文件或目录 [root@localhost 111]# ls /home/linux 2>error.txt [root@localhost 111]# ll 总用量 8 -rw-rw-r--+ 1 user user 172 3月 24 12:09 a.txt -rw-rw-r--+ 1 root root 57 3月 24 12:15 error.txt [root@localhost 111]# cat error.txt ls: 无法访问/home/linux: 没有那个文件或目录
    正确结果与错误结果都输出到相同的位置
    [root@localhost 111]# ll /home /linux &>error.txt [root@localhost 111]# cat error.txt ls: 无法访问/linux: 没有那个文件或目录 /home: 总用量 8 drwx------. 3 bb bb 78 3月 19 11:44 a drwx------. 3 aa aa 78 3月 21 15:19 aa drwx------. 3 cc cc 78 3月 19 11:44 b .......
    正确结果与错误结果都输出重定向到相同的位置
    [root@localhost 111]# ll /home /linux >a.txt 2>&1 [root@localhost 111]# cat a.txt ls: 无法访问/linux: 没有那个文件或目录 /home: 总用量 8 drwx------. 3 bb bb 78 3月 19 11:44 a drwx------. 3 aa aa 78 3月 21 15:19 aa drwx------. 3 cc cc 78 3月 19 11:44 b drwx------. 3 bb bb 78 3月 21 15:19 bb drwx------. 3 1008 1008 78 3月 19 11:44 c drwx------. 3 cc cc 78 3月 21 15:19 cc drwx------. 15 fsg fsg 4096 3月 19 11:54 fsg drwx------. 16 shigen shigen 4096 3月 16 20:24 shigen drwx------. 5 user user 128 3月 21 19:53 user drwx------. 5 1001 1001 128 3月 18 23:14 user1
    正确的保存在 right.txt, 错误的保存在 null.txt`
    [root@localhost 111]# ls /home /linux >right.txt 2>null.txt [root@localhost 111]# cat right.txt /home: a aa b bb c cc fsg ...... [root@localhost 111]# cat null.txt ls: 无法访问/linux: 没有那个文件或目录

    输入重定向

    • 将数据写入到文件中,每次1MB,一共写入两次
    [root@localhost 111]# dd if=/dev/zero of=aa.txt bs=1M count=2 记录了2+0 的读入 记录了2+0 的写出 2097152字节(2.1 MB)已复制,0.0065069 秒,322 MB/秒
    • 输入重定向与输出重定向实现相同的功能
    [root@localhost 111]# dd </dev/zero >bb.txt bs=1M count=3 记录了3+0 的读入 记录了3+0 的写出 3145728字节(3.1 MB)已复制,0.0107413 秒,293 MB/秒
    • at命令添加计划任务,Ctrl + d 结束
    [root@localhost 111]# at now +1 min at> useradd li at> <EOT> job 1 at Thu Mar 25 11:17:00 2021 [root@localhost 111]# id li uid=1008(li) gid=1008(li)=1008(li) 您在 /var/spool/mail/root 中有新邮件 [root@localhost 111]# tail -5 /etc/passwd ...... li:x:1008:1008::/home/li:/bin/bash
    • 输入重定向同时添加多个用户
    [root@localhost 111]# touch user.txt [root@localhost 111]# vim user.txt [root@localhost 111]# cat user.txt useradd aa useradd bb useradd cc [root@localhost 111]# at now +1 min < user.txt job 2 at Thu Mar 25 11:21:00 2021 [root@localhost 111]# tail -5 /etc/passwd user:x:1003:30002::/home/user:/bin/bash aa:x:1005:1005::/home/aa:/bin/bash bb:x:1006:1006::/home/bb:/bin/bash cc:x:1007:1007::/home/cc:/bin/bash li:x:1008:1008::/home/li:/bin/bash

    重定向综合案例

    建立多行文件 cat 在键盘上输入文本按回车换行,Ctrl+d 结束
    [root@localhost 111]# cat>aaa.txt this is a line 1234 [root@localhost 111]# cat aaa.txt this is a line 1234 [root@localhost 111]#
    用户自定义结束符号
    [root@localhost 111]# cat>aaa.txt<<EOF > 1234 > 5678 > EOF
    重定向建立脚本操纵Shell
    # vim编辑器中写入脚本 [root@localhost 111]# vim create_file.sh [root@localhost 111]# [root@localhost 111]# cat create_file.shcat >file000.txt<<EOF 111 222 333 EOF # bash 执行该脚本文件 [root@localhost 111]# bash create_file.sh [root@localhost 111]# cat file000.txt 111 222 333
    多条命令输出重定向
    两条命令重定向,添加括号
    [root@localhost 111]# (ls;date)&>bb.txt [root@localhost 111]# cat bb.txt aaa.txt ...... user.txt 2021年 03月 25日 星期四 11:50:07 CST
    让命令在后台运行,输出重定向到文件,终止后台程序
    - 输入重定向与输出重定向实现相同的功能```shell [root@localhost 111]# dd </dev/zero >bb.txt bs=1M count=3 记录了3+0 的读入 记录了3+0 的写出 3145728字节(3.1 MB)已复制,0.0107413 秒,293 MB/秒
    • at命令添加计划任务,Ctrl + d 结束
    [root@localhost 111]# at now +1 min at> useradd li at> <EOT> job 1 at Thu Mar 25 11:17:00 2021 [root@localhost 111]# id li uid=1008(li) gid=1008(li)=1008(li) 您在 /var/spool/mail/root 中有新邮件 [root@localhost 111]# tail -5 /etc/passwd ...... li:x:1008:1008::/home/li:/bin/bash
    • 输入重定向同时添加多个用户
    [root@localhost 111]# touch user.txt [root@localhost 111]# vim user.txt [root@localhost 111]# cat user.txt useradd aa useradd bb useradd cc [root@localhost 111]# at now +1 min < user.txt job 2 at Thu Mar 25 11:21:00 2021 [root@localhost 111]# tail -5 /etc/passwd user:x:1003:30002::/home/user:/bin/bash aa:x:1005:1005::/home/aa:/bin/bash bb:x:1006:1006::/home/bb:/bin/bash cc:x:1007:1007::/home/cc:/bin/bash li:x:1008:1008::/home/li:/bin/bash

    重定向综合案例

    建立多行文件 cat 在键盘上输入文本按回车换行,Ctrl+d 结束
    [root@localhost 111]# cat>aaa.txt this is a line 1234 [root@localhost 111]# cat aaa.txt this is a line 1234 [root@localhost 111]#
    用户自定义结束符号
    [root@localhost 111]# cat>aaa.txt<<EOF > 1234 > 5678 > EOF
    重定向建立脚本操纵Shell
    # vim编辑器中写入脚本 [root@localhost 111]# vim create_file.sh [root@localhost 111]# [root@localhost 111]# cat create_file.shcat >file000.txt<<EOF 111 222 333 EOF # bash 执行该脚本文件 [root@localhost 111]# bash create_file.sh [root@localhost 111]# cat file000.txt 111 222 333
    多条命令输出重定向
    两条命令重定向,添加括号
    [root@localhost 111]# (ls;date)&>bb.txt [root@localhost 111]# cat bb.txt aaa.txt ...... user.txt 2021年 03月 25日 星期四 11:50:07 CST

    总结

    以上是生活随笔为你收集整理的Linux服务与管理-step1的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。