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的快捷键:
开始时创建的分区:
- /boot 引导分区
- /swap 交换分区
- / 根分区
第二章 文件管理
根目录下的目录
| /bin | 二进制可执行文件 常用命令 |
| /etc | 系统管理和配置文件 |
| /home | 用户文件根目录 |
| /usr | 用户安装的程序和系统程序 |
| /tmp | 临时文件 |
| /root | 系统管理员目录 |
| /var | 运行时需要改变数据的文件/大文件的溢出区 |
| /sbin | 二进制可执行文件,只有root可以访问 |
文件路径
-
相对路径
-
绝对路径
目录与文件的操作
目录操作
-
创建目录 -p递归创建目录 mkdir
-
查看目录 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连同上层空目录一起删除 |
文件操作
创建文件
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/bashrc3.less命令 分页显示
pageUp——向上看文件
pageDown——向下看文件 q键退出
[root@localhost ~]# less /etc/profile4.head命令 看文件的前几行 -n
[root@localhost ~]# head -3 /etc/profile # /etc/profile# System wide environment and startup programs, for login setup5.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/bash6.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 dos2unixVim编辑器
[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.c1023ll /home查看文件的所有者 第三列为文件的所有者
[root@localhost ~]# ll /home 总用量 4 drwx------. 16 shigen shigen 4096 3月 16 20:24 shigenps 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/nologin9个字段 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
修改用户密码
任何用户都可以用 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 SHA512su/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
| U | owner | 属主 |
| G | group | 属组 |
| O | other | 其他用户 |
| r | read | 4 |
| w | write | 2 |
| x | execute | 1 |
设置文件属性与权限
三个修改权限的命令
| 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.txtchmod 赋值的功能格式
| u | + | r——可读取文件内容 列出目录内容 | ||
| chmod | g | - | 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.txtmask 的作用和特性
临时降低用户/组的权限
[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 默认的目录和文件的权限
- 把 unamsk 的默认权限设置成0777,创建的文件、目录的权限基本为000
- 可以在用户目录下的 /home/etc/login.defs 文件更改 60行 UMASK 的权限值
第五章 进程管理
初始进程
进程: 系统进行资源分配和调度的一个独立单位
系统的四大核心资源: Disk IO, Memory, CPU , Network
查看进程
静态查看进程
- ps aux 命令查看当前目录进程
- ps aux --sort -%cpu 按照CPU占用率降序排列
- ps -ef 命令查看UID,PID,PPID等信息
- ps axo 命令自定义显示字段
- 查看指定进程的PID
2.pidof 命令
[root@localhost ~]# pidof sshd 6812 2710 11503.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——内存占用率降序排序
- P——CPU占用率降序排序
- N——PID数值大小排序
- R——对排序进行反转 F——显示自定义显示字段 ,上下键移动,空格选中 q——退出 W——保存自定义显示字段
- 按 1 显示CPU负载
- top -d 1 设置每秒刷新一次
- 添加 -p 查看一个到多个进程信息
- 添加 -u 查看指定用户进程, -n 设置刷新次数,完成后自动退出
信号控制进程
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 查看全部信号
- 教材案例
- killall vsftpd 终止某个指令名称的服务所对应的全部进程
进程的优先级
优先级决定了进程被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重定向
- 显示当前时间,并指向文件
文件描述符的定义**
| 0 | stdin | 标准输入 | 键盘 | 只读 |
| 1 | stdout | 输出 | 终端 | 只写 |
| 2 | stderr | 错误 | 终端 | 只写 |
| 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,一共写入两次
- 输入重定向与输出重定向实现相同的功能
- at命令添加计划任务,Ctrl + d 结束
- 输入重定向同时添加多个用户
重定向综合案例
建立多行文件 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 结束
- 输入重定向同时添加多个用户
重定向综合案例
建立多行文件 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的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 学习verilog的经典好教材与资料
- 下一篇: linux终端文件名前特殊符号,Linu