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