Linux服务与管理-step1
文章目錄
- Linux服務(wù)與管理
- vuritual machine的快捷鍵:
- 開(kāi)始時(shí)創(chuàng)建的分區(qū):
- 第二章 文件管理
- 根目錄下的目錄
- 文件路徑
- 相對(duì)路徑
- 絕對(duì)路徑
- 目錄與文件的操作
- 目錄操作
- 創(chuàng)建目錄 ==-p遞歸創(chuàng)建目錄== `mkdir`
- 查看目錄 `pwd` `ls`
- 刪除目錄 `rmdir`
- 文件操作
- 創(chuàng)建文件
- 查看文件
- 復(fù)制文件
- 移動(dòng)文件
- 刪除文件
- 常見(jiàn)的問(wèn)題
- Vim編輯器
- 常用命令
- 光標(biāo)定位命令
- 文本編輯命令
- 從命令模式進(jìn)入其他模式
- 末行模式常用的命令
- 文件時(shí)間
- 文件類型
- 帶三章 用戶管理
- 用戶/組
- UID和GID
- 用戶/組相關(guān)文件
- 創(chuàng)建用戶/組
- 刪除用戶/組
- 修改用戶密碼
- 配置文件
- su/sudo 命令
- 第四章 文件權(quán)限
- 基本權(quán)限UGO
- 設(shè)置文件屬性與權(quán)限
- 修改屬主和屬組
- 只修改文件屬組
- 所有子目錄或文件同時(shí)修改屬主或?qū)俳M,`chown 和 chgrp 后邊添加 -R`
- `chmod` 賦值的功能格式
- 為一個(gè)文件插入權(quán)限
- 同時(shí)給所有對(duì)象添加權(quán)限
- 同時(shí)給所有用戶刪除權(quán)限
- 同時(shí)刪除所有對(duì)象的全部權(quán)限
- 一次給不同的對(duì)象增加或刪除不同的權(quán)限
- 使用遞歸參數(shù) `-R`
- 使用數(shù)字權(quán)限 4+2+1
- 基本權(quán)限ACL
- ACL的基本用法
- `-m`修改當(dāng)前文件的ACL權(quán)限
- 新建用戶 添加ACL權(quán)限
- 為組添加權(quán)限
- `-` 對(duì)文件沒(méi)有任何權(quán)限
- `-x` 刪除用戶對(duì)文件的所有權(quán)限,此時(shí)用戶屬于other
- `-b` 刪除所有的擴(kuò)展ACL權(quán)限,回到UGO基本權(quán)限
- ACL高級(jí)特性
- 最大有限權(quán)限 `mask`
- `mask` 的作用和特性
- `default` 繼承
- 高級(jí)權(quán)限
- SUID權(quán)限
- SGID權(quán)限
- Sticky權(quán)限
- 文件屬性 chattr
- 進(jìn)程掩碼 umask
- 第五章 進(jìn)程管理
- 初始進(jìn)程
- 查看進(jìn)程
- 靜態(tài)查看進(jìn)程
- 動(dòng)態(tài)查看進(jìn)程
- 信號(hào)控制進(jìn)程
- kill命令 終止指定進(jìn)程的運(yùn)行
- 進(jìn)程的優(yōu)先級(jí)
- 使用top命令查看nice級(jí)別
- 使用ps命令查看nice級(jí)別
- 作業(yè)控制
- 第六章 I/O重定向與管道
- I/O重定向
- 輸出重定向
- 錯(cuò)誤輸出重定向
- 正確結(jié)果與錯(cuò)誤結(jié)果都輸出到相同的位置
- 正確結(jié)果與錯(cuò)誤結(jié)果都輸出**重定向**到相同的位置
- 正確的保存在 `right.txt`, 錯(cuò)誤的保存在 null.txt`
- 輸入重定向
- 重定向綜合案例
- 建立多行文件 `cat` 在鍵盤(pán)上輸入文本按回車換行,Ctrl+d 結(jié)束
- 用戶自定義結(jié)束符號(hào)
- 重定向建立腳本操縱Shell
- 多條命令輸出重定向
- 兩條命令重定向,添加括號(hào)
- 讓命令在后臺(tái)運(yùn)行,輸出重定向到文件,終止后臺(tái)程序
- 重定向綜合案例
- 建立多行文件 `cat` 在鍵盤(pán)上輸入文本按回車換行,Ctrl+d 結(jié)束
- 用戶自定義結(jié)束符號(hào)
- 重定向建立腳本操縱Shell
- 多條命令輸出重定向
- 兩條命令重定向,添加括號(hào)
Linux服務(wù)與管理
vuritual machine的快捷鍵:
開(kāi)始時(shí)創(chuàng)建的分區(qū):
- /boot 引導(dǎo)分區(qū)
- /swap 交換分區(qū)
- / 根分區(qū)
第二章 文件管理
根目錄下的目錄
| /bin | 二進(jìn)制可執(zhí)行文件 常用命令 |
| /etc | 系統(tǒng)管理和配置文件 |
| /home | 用戶文件根目錄 |
| /usr | 用戶安裝的程序和系統(tǒng)程序 |
| /tmp | 臨時(shí)文件 |
| /root | 系統(tǒng)管理員目錄 |
| /var | 運(yùn)行時(shí)需要改變數(shù)據(jù)的文件/大文件的溢出區(qū) |
| /sbin | 二進(jìn)制可執(zhí)行文件,只有root可以訪問(wèn) |
文件路徑
-
相對(duì)路徑
-
絕對(duì)路徑
目錄與文件的操作
目錄操作
-
創(chuàng)建目錄 -p遞歸創(chuàng)建目錄 mkdir
-
查看目錄 pwd ls
| [root@localhost 333]# pwd /temp/111/222/333 | 顯示當(dāng)前所在的目錄 |
| [root@localhost 333]# pwd -P /temp/111/222/333 | -P顯示實(shí)際工作目錄 |
| [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]# | 刪除目錄,非空會(huì)報(bào)錯(cuò) |
| [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連同上層空目錄一起刪除 |
文件操作
創(chuàng)建文件
echo設(shè)置文件、屬性
[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 顯示行號(hào)
-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命令 以逐頁(yè)的方式顯示文件的內(nèi)容
空格鍵——下一頁(yè)
b——上一頁(yè)
[root@localhost ~]# more /etc/profile # /etc/profile# System wide environment and startup programs, for login setup # Functions and aliases go in /etc/bashrc3.less命令 分頁(yè)顯示
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 ——查看動(dòng)態(tài)文件
[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命令 對(duì)文件的內(nèi)容過(guò)濾、搜索關(guān)鍵詞,快速找到內(nèi)容
[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復(fù)制文件
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文件目錄較長(zhǎng)時(shí),花括號(hào)括起不同的部分
[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: 沒(méi)有那個(gè)文件或目錄 [root@localhost ~]# cat -n /temp/cc.txt1 hello shigen移動(dòng)文件
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 / 進(jìn)行遞歸操作
[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常見(jiàn)的問(wèn)題
在Linux環(huán)境下正常打開(kāi)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常用命令
光標(biāo)定位命令
| h,j,k,l | 光標(biāo)上下左右移動(dòng) 也可用方向鍵 |
| 0 $ | 光標(biāo)移到行首 行尾 |
| gg G | 光標(biāo)移到第一行首個(gè)字符的位置 最后一行首個(gè)字符的位置 |
| /字符串 | 快速定位到字符串所在的行 |
| /^d | 定位首個(gè)字母為d的行 |
| /txt$ | 定位結(jié)尾為txt的行 |
文本編輯命令
| n yy | 復(fù)制當(dāng)前行 |
| n dd | 刪除當(dāng)前行 |
| p | 粘貼 |
| x | 刪除光標(biāo)所在字符 |
| D | 從光標(biāo)處刪除到行尾 |
| u | 撤銷 |
| ^r | 重做 |
| R | 進(jìn)入替換模式 |
從命令模式進(jìn)入其他模式
| o | 進(jìn)入編輯模式,光標(biāo)下面另起一行 |
| a | 進(jìn)入編輯模式 光標(biāo)后一位 |
| i | 進(jìn)入編輯模式 Esc 退出 |
| : | 進(jìn)入末行模式 |
| V | 進(jìn)入可視行模式 多行會(huì)被選中 |
| v | 可視模式 |
| ^v | 可視塊模式 |
| R | 替換模式 |
末行模式常用的命令
| :w | 保存 |
| :q | 退出 |
| :wq | 保存并退出 |
| :w! | 強(qiáng)制保存 |
| :q! | 不保存強(qiáng)制退出 |
| :wq! | |
| :set nu | 顯示行號(hào) |
| :set nonu | 不顯示行號(hào) |
| :整數(shù) | 跳轉(zhuǎn)到該行 |
| 😒/abc/abd | 該行第一個(gè)abc替換成abd |
| 😒/abc/abd/g | 該行所有的abc替換成abd |
文件時(shí)間
文件的三種時(shí)間: ①訪問(wèn)時(shí)間 ②修改時(shí)間 ③狀態(tài)時(shí)間
stat查看文件的詳細(xì)信息
[root@localhost ~]# stat /temp/111/a.txt文件:"/temp/111/a.txt"大小:1433 塊:8 IO 塊:4096 普通文件 設(shè)備:fd00h/64768d Inode:14464057 硬鏈接:1 權(quán)限:(0600/-rw-------) Uid:( 0/ root) Gid:( 0/ root) 環(huán)境:unconfined_u:object_r:default_t:s0 最近訪問(wèn):2021-03-17 10:02:31.763021875 +0800 最近更改:2021-03-17 10:02:31.763021875 +0800 最近改動(dòng):2021-03-17 10:02:31.772021806 +0800 創(chuàng)建時(shí)間:-文件類型
Linux 系統(tǒng)中的文件沒(méi)有擴(kuò)展名,修改擴(kuò)展名無(wú)法修改文件的本質(zhì)
[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 模板'-' 開(kāi)頭為普通文件(文本,二進(jìn)制,壓縮文件) 'd' 目錄文件 'b' 設(shè)備文件(塊設(shè)備) 'c' 字符設(shè)備 's' 套接字文件 'p' 管道文件 'l' 鏈接文件帶三章 用戶管理
用戶/組
UID和GID
uid : 系統(tǒng)中唯一的能識(shí)別身份的
gid:用戶所屬組的ID
[root@localhost ~]# id uid=0(root) gid=0(root) 組=0(root) 環(huán)境=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 查看進(jìn)程
[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進(jìn)程的用戶名
[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用戶/組相關(guān)文件
用戶名和密碼—— /etc/passwd /etc/shadow兩個(gè)文件里面
7個(gè)字段 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個(gè)字段 username 加密后的密碼 最近改動(dòng)密碼時(shí)間 密碼不可變更時(shí)間(99999為沒(méi)有限制) 密碼重新變更時(shí)間 密碼過(guò)期時(shí)間 密碼過(guò)期寬恕時(shí)間 用戶失效時(shí)間 保留
[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:::管理用戶/組
創(chuàng)建用戶/組
創(chuàng)建一個(gè)用戶,grep過(guò)濾出該用戶的信息
若未指定任何組,系統(tǒng)默認(rèn)給該用戶創(chuàng)建一個(gè)和用戶名相同的用戶組作為用戶主組
[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 命令的參數(shù)
| -d | 指定用戶的主目錄 |
| -u | 指定用戶uid |
| -g | 指定用戶主組的名稱或ID |
| -G | 指定用戶的附加組或列表 |
| -s | 指定用戶登錄的shell |
| -m | 創(chuàng)建用戶主目錄 |
| -M | 不創(chuàng)建 |
刪除用戶/組
- 刪除用戶且刪除 /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:所有的身份驗(yàn)證令牌已經(jīng)成功更新。 [root@localhost ~]# su user2 [user2@localhost root]$ passwd 更改用戶 user2 的密碼 。 為 user2 更改 STRESS 密碼。 (當(dāng)前)UNIX 密碼: 新的 密碼: 無(wú)效的密碼: 密碼與原來(lái)的太相似 新的 密碼: 重新輸入新的 密碼:.789nbsi passwd:所有的身份驗(yàn)證令牌已經(jīng)成功更新。 [user2@localhost root]$安全用戶
useradd user -s /sbin/nologin 本地和遠(yuǎn)程都不能登錄系統(tǒng),無(wú)法管理系統(tǒng)
查看可登錄系統(tǒng)的用戶
[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 的配置文件 決定創(chuàng)建用戶的默認(rèn)參數(shù)
[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 命令來(lái)執(zhí)行系統(tǒng)的有關(guān)操作
必須先輸入密碼 5分鐘有效時(shí)間
[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:無(wú)法鎖定 /etc/passwd,請(qǐng)稍后再試。 [user3@localhost root]$ sudo useradd user10我們信任您已經(jīng)從系統(tǒng)管理員那里了解了日常注意事項(xiàng)。 總結(jié)起來(lái)無(wú)外乎這三點(diǎn):#1) 尊重別人的隱私。#2) 輸入前要先考慮(后果和風(fēng)險(xiǎn))。#3) 權(quán)力越大,責(zé)任越大。[sudo] user3 的密碼: [user3@localhost root]$ sudo id user10 uid=1004(user10) gid=1006(user10) 組=1006(user10)第四章 文件權(quán)限
允許一個(gè)用戶或者用戶組依規(guī)定的方式去訪問(wèn)某個(gè)文件
基本權(quán)限UGO
| U | owner | 屬主 |
| G | group | 屬組 |
| O | other | 其他用戶 |
| r | read | 4 |
| w | write | 2 |
| x | execute | 1 |
設(shè)置文件屬性與權(quán)限
三個(gè)修改權(quán)限的命令
| 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所有子目錄或文件同時(shí)修改屬主或?qū)俳M,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——可讀取文件內(nèi)容 列出目錄內(nèi)容 | ||
| chmod | g | - | w——修改文件內(nèi)容 創(chuàng)建或刪除目錄中的任意一文件 | file |
| o | = | e——將文件作為命令執(zhí)行 可訪問(wèn)目錄的內(nèi)容(取決于目錄中文件權(quán)限) | ||
| a |
為一個(gè)文件插入權(quán)限
[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同時(shí)給所有對(duì)象添加權(quán)限
[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同時(shí)給所有用戶刪除權(quán)限
[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同時(shí)刪除所有對(duì)象的全部權(quán)限
[root@localhost temp]# chmod a=- file5.txt [root@localhost temp]# ll file5.txt ----------. 1 user10 group2 0 3月 17 16:25 file5.txt一次給不同的對(duì)象增加或刪除不同的權(quán)限
[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使用遞歸參數(shù) -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!使用數(shù)字權(quán)限 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基本權(quán)限ACL
ACL(Access Control List) 提供傳統(tǒng)的UGO r,w,x之外的權(quán)限設(shè)置
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修改當(dāng)前文件的ACL權(quán)限
[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權(quán)限
[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--為組添加權(quán)限
[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--- 對(duì)文件沒(méi)有任何權(quán)限
[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-- # 已經(jīng)無(wú)法讀取文件 [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: 權(quán)限不夠-x 刪除用戶對(duì)文件的所有權(quán)限,此時(shí)用戶屬于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 刪除所有的擴(kuò)展ACL權(quán)限,回到UGO基本權(quán)限
[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高級(jí)特性
最大有限權(quán)限 mask
系統(tǒng)給用戶賦予的ACL權(quán)限和mask的權(quán)限邏輯“相與”,之后的權(quán)限才是用戶真正的權(quán)限
[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: 權(quán)限不夠將other權(quán)限置空,方便管理
[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 的作用和特性
臨時(shí)降低用戶/組的權(quán)限
[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會(huì)自動(dòng)還原 [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 可以繼承上一個(gè)目錄的權(quán)限
[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: 無(wú)法創(chuàng)建"b.txt": 權(quán)限不夠 # 切換到user賬戶,無(wú)法創(chuàng)建文件# 使用==default==命令可以繼承上一個(gè)目錄的權(quán)限 [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高級(jí)權(quán)限
SUID權(quán)限
SGID權(quán)限
Sticky權(quán)限
文件屬性 chattr
為了保護(hù)系統(tǒng)文件,Linux系統(tǒng)會(huì)用 chattr 命令改變文件的隱藏屬性
[root@localhost temp]# touch file1 file2 file3 # lsattr 查看三個(gè)文件的隱藏屬性 [root@localhost temp]# lsattr file1 file2 file3 ---------------- file1 ---------------- file2 ---------------- file3 # man 工具查看chattr 命令的使用方法 [root@localhost temp]# man chattr # 為file1文件增加‘a(chǎn)’屬性,此時(shí)Vim編輯器不能寫(xiě)入文本,需要使用echo命令追加寫(xiě)入-----用于日志文件,不能刪除,文本只能追加 [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: 無(wú)法刪除"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: 權(quán)限不夠 [root@localhost temp]# rm -rf file2 rm: 無(wú)法刪除"file2": 不允許的操作 [root@localhost temp]#進(jìn)程掩碼 umask
用戶創(chuàng)建目錄或者文件時(shí),系統(tǒng)賦予一個(gè)默認(rèn)的權(quán)限, umask 指定權(quán)限的默認(rèn)值,表示要減掉的權(quán)限
- 創(chuàng)建目錄,創(chuàng)建文件,查看 umask 默認(rèn)的目錄和文件的權(quán)限
- 把 unamsk 的默認(rèn)權(quán)限設(shè)置成0777,創(chuàng)建的文件、目錄的權(quán)限基本為000
- 可以在用戶目錄下的 /home/etc/login.defs 文件更改 60行 UMASK 的權(quán)限值
第五章 進(jìn)程管理
初始進(jìn)程
進(jìn)程: 系統(tǒng)進(jìn)行資源分配和調(diào)度的一個(gè)獨(dú)立單位
系統(tǒng)的四大核心資源: Disk IO, Memory, CPU , Network
查看進(jìn)程
靜態(tài)查看進(jìn)程
- ps aux 命令查看當(dāng)前目錄進(jìn)程
- ps aux --sort -%cpu 按照CPU占用率降序排列
- ps -ef 命令查看UID,PID,PPID等信息
- ps axo 命令自定義顯示字段
- 查看指定進(jìn)程的PID
2.pidof 命令
[root@localhost ~]# pidof sshd 6812 2710 11503.pgrep 命令
[root@localhost ~]# pgrep sshd 1150 2710 6812動(dòng)態(tài)查看進(jìn)程
上邊部分為整體信息,下邊部分為每一個(gè)進(jìn)程信息。默認(rèn)更新的時(shí)為3S,enter鍵立即更新
# top: 當(dāng)前時(shí)間 # up: 啟動(dòng)后的運(yùn)行時(shí)間 # users: 用戶數(shù) # load average : CPU在最近1min 5min 15min 平均負(fù)載值 # Tasks: 進(jìn)程數(shù) # Kib Men: 內(nèi)存使用情況[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——內(nèi)存占用率降序排序
- P——CPU占用率降序排序
- N——PID數(shù)值大小排序
- R——對(duì)排序進(jìn)行反轉(zhuǎn) F——顯示自定義顯示字段 ,上下鍵移動(dòng),空格選中 q——退出 W——保存自定義顯示字段
- 按 1 顯示CPU負(fù)載
- top -d 1 設(shè)置每秒刷新一次
- 添加 -p 查看一個(gè)到多個(gè)進(jìn)程信息
- 添加 -u 查看指定用戶進(jìn)程, -n 設(shè)置刷新次數(shù),完成后自動(dòng)退出
信號(hào)控制進(jìn)程
kill命令 終止指定進(jìn)程的運(yùn)行
常用信號(hào):
HUP 1 終端斷線 INT 2 中斷(同 Ctrl + C) QUIT 3 退出(同 Ctrl + \) TERM 15 終止 KILL 9 強(qiáng)制終止 CONT 18 繼續(xù)(與STOP相反, fg/bg命令) STOP 19 暫停(同 Ctrl + Z)- kill -l 查看全部信號(hào)
- 教材案例
- killall vsftpd 終止某個(gè)指令名稱的服務(wù)所對(duì)應(yīng)的全部進(jìn)程
進(jìn)程的優(yōu)先級(jí)
優(yōu)先級(jí)決定了進(jìn)程被CPU處理的順序
使用top命令查看nice級(jí)別
NI列表示的是實(shí)際nice級(jí)別,PR將nice級(jí)別映射到更大優(yōu)先級(jí)隊(duì)列
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級(jí)別
[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作業(yè)控制
允許一個(gè)shell實(shí)例運(yùn)行和管理多個(gè)命令
# 創(chuàng)建后臺(tái)進(jìn)程,&為后臺(tái)符 [root@localhost ~]# sleep 7000 & [1] 10944 # 創(chuàng)建前臺(tái)進(jìn)程 Ctrl+c 停止進(jìn)程 [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 顯示當(dāng)前shell作業(yè)列表和作業(yè)狀態(tài) [root@localhost ~]# jobs [1]- 運(yùn)行中 sleep 7000 & [2]+ 已停止 sleep 8000 # bg 2 將作業(yè)從前臺(tái)--->后臺(tái) [root@localhost ~]# bg 2 [2]+ sleep 8000 & # fg 將作業(yè)后臺(tái)-->前臺(tái) [root@localhost ~]# fg 2 sleep 8000 ^C [root@localhost ~]# jobs [1]+ 運(yùn)行中 sleep 7000 & # kill 命令終止 [root@localhost ~]# kill %1 [1]+ 已終止 sleep 7000 [root@localhost ~]#使用 fg 命令默認(rèn)調(diào)用最近一次的進(jìn)程
[root@localhost ~]# sleep 1234 & [1] 11336 [root@localhost ~]# sleep 4321 & [2] 11343 [root@localhost ~]# jobs [1]- 運(yùn)行中 sleep 1234 & [2]+ 運(yùn)行中 sleep 4321 & [root@localhost ~]# fg sleep 4321第六章 I/O重定向與管道
I/O重定向
- 顯示當(dāng)前時(shí)間,并指向文件
文件描述符的定義**
| 0 | stdin | 標(biāo)準(zhǔn)輸入 | 鍵盤(pán) | 只讀 |
| 1 | stdout | 輸出 | 終端 | 只寫(xiě) |
| 2 | stderr | 錯(cuò)誤 | 終端 | 只寫(xiě) |
| 3+ | filename | 其他文件 | 無(wú) | rw/w/r |
輸出重定向
寫(xiě)法: 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錯(cuò)誤輸出重定向
[root@localhost 111]# ls /home/linux ls: 無(wú)法訪問(wèn)/home/linux: 沒(méi)有那個(gè)文件或目錄 [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: 無(wú)法訪問(wèn)/home/linux: 沒(méi)有那個(gè)文件或目錄正確結(jié)果與錯(cuò)誤結(jié)果都輸出到相同的位置
[root@localhost 111]# ll /home /linux &>error.txt [root@localhost 111]# cat error.txt ls: 無(wú)法訪問(wèn)/linux: 沒(méi)有那個(gè)文件或目錄 /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 .......正確結(jié)果與錯(cuò)誤結(jié)果都輸出重定向到相同的位置
[root@localhost 111]# ll /home /linux >a.txt 2>&1 [root@localhost 111]# cat a.txt ls: 無(wú)法訪問(wèn)/linux: 沒(méi)有那個(gè)文件或目錄 /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, 錯(cuò)誤的保存在 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: 無(wú)法訪問(wèn)/linux: 沒(méi)有那個(gè)文件或目錄輸入重定向
- 將數(shù)據(jù)寫(xiě)入到文件中,每次1MB,一共寫(xiě)入兩次
- 輸入重定向與輸出重定向?qū)崿F(xiàn)相同的功能
- at命令添加計(jì)劃任務(wù),Ctrl + d 結(jié)束
- 輸入重定向同時(shí)添加多個(gè)用戶
重定向綜合案例
建立多行文件 cat 在鍵盤(pán)上輸入文本按回車換行,Ctrl+d 結(jié)束
[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]#用戶自定義結(jié)束符號(hào)
[root@localhost 111]# cat>aaa.txt<<EOF > 1234 > 5678 > EOF重定向建立腳本操縱Shell
# vim編輯器中寫(xiě)入腳本 [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 執(zhí)行該腳本文件 [root@localhost 111]# bash create_file.sh [root@localhost 111]# cat file000.txt 111 222 333多條命令輸出重定向
兩條命令重定向,添加括號(hào)
[root@localhost 111]# (ls;date)&>bb.txt [root@localhost 111]# cat bb.txt aaa.txt ...... user.txt 2021年 03月 25日 星期四 11:50:07 CST讓命令在后臺(tái)運(yùn)行,輸出重定向到文件,終止后臺(tái)程序
- 輸入重定向與輸出重定向?qū)崿F(xiàn)相同的功能```shell [root@localhost 111]# dd </dev/zero >bb.txt bs=1M count=3 記錄了3+0 的讀入 記錄了3+0 的寫(xiě)出 3145728字節(jié)(3.1 MB)已復(fù)制,0.0107413 秒,293 MB/秒- at命令添加計(jì)劃任務(wù),Ctrl + d 結(jié)束
- 輸入重定向同時(shí)添加多個(gè)用戶
重定向綜合案例
建立多行文件 cat 在鍵盤(pán)上輸入文本按回車換行,Ctrl+d 結(jié)束
[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]#用戶自定義結(jié)束符號(hào)
[root@localhost 111]# cat>aaa.txt<<EOF > 1234 > 5678 > EOF重定向建立腳本操縱Shell
# vim編輯器中寫(xiě)入腳本 [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 執(zhí)行該腳本文件 [root@localhost 111]# bash create_file.sh [root@localhost 111]# cat file000.txt 111 222 333多條命令輸出重定向
兩條命令重定向,添加括號(hào)
[root@localhost 111]# (ls;date)&>bb.txt [root@localhost 111]# cat bb.txt aaa.txt ...... user.txt 2021年 03月 25日 星期四 11:50:07 CST總結(jié)
以上是生活随笔為你收集整理的Linux服务与管理-step1的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 学习verilog的经典好教材与资料
- 下一篇: 用python绘制万花筒_Python实