Linux基礎(chǔ)命令,用戶管理和文件系統(tǒng)總結(jié)
基礎(chǔ)命令
Linux系統(tǒng)的命令通常都是如下所示的格式:
- 命令名稱 [命名參數(shù)] [命令對象]
獲取登錄信息
獲取登錄信息 - 「w」 / 「who」 / 「last」/ 「lastb」。
- maoli@ubuntu:~$ w
- 08:07:38 up 2 min, 1 user, load average: 0.83, 0.64, 0.27
- USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
- maoli tty7 :0 08:06 2:38 2.29s 0.31s /sbin/upstart -
- maoli@ubuntu:~$ who
- maoli tty7 2020-05-28 08:06 (:0)
- maoli@ubuntu:~$ last
- maoli tty7 :0 Thu May 28 08:06 gone - no logout
- reboot system boot 4.15.0-99-generi Thu May 28 08:04 still running
- maoli tty7 :0 Fri May 1 15:20 - down (01:26)
- reboot system boot 4.15.0-99-generi Fri May 1 15:20 - 16:47 (01:26)
- maoli tty7 :0 Fri May 1 10:12 - crash (05:08)
- wtmp begins Fri May 1 10:12:22 2020
- maoli@ubuntu:~$ sudo lastb
- [sudo] maoli 的密碼:
- btmp begins Fri May 1 15:20:29 2020
查看自己使用的Shell
查看自己使用的Shell - 「ps」。
Shell也被稱為“殼”或“殼程序”,它是用戶與操作系統(tǒng)內(nèi)核交流的翻譯官,簡單的說就是人與計(jì)算機(jī)交互的界面和接口。目前很多Linux系統(tǒng)默認(rèn)的Shell都是bash(Bourne Again SHell),因?yàn)樗梢允褂胻ab鍵進(jìn)行命令和路徑補(bǔ)全、可以保存歷史命令、可以方便的配置環(huán)境變量以及執(zhí)行批處理操作。
- maoli@ubuntu:~$ ps
- PID TTY TIME CMD
- 3621 pts/1 00:00:00 bash
- 5082 pts/1 00:00:00 ps
查看命令的說明和位置
查看命令的說明和位置 - 「whatis」 / 「which」 / 「whereis」。
- maoli@ubuntu:~$ whatis ps
- ps (1) - report a snapshot of the current processes.
- maoli@ubuntu:~$ whatis python
- python (1) - an interpreted, interactive, object-oriented programmi...
- maoli@ubuntu:~$ whereis ps
- ps: /bin/ps /usr/share/man/man1/ps.1.gz
- maoli@ubuntu:~$ whereis python
- python: /usr/bin/python3.5m-config /usr/bin/python /usr/bin/python3.5 /usr/bin/python3.5m /usr/bin/python2.7 /usr/bin/python3.5-config /usr/lib/python3.5 /usr/lib/python2.7 /etc/python /etc/python3.5 /etc/python2.7 /usr/local/lib/python3.5 /usr/local/lib/python2.7 /usr/include/python3.5 /usr/include/python3.5m /usr/share/python /usr/share/man/man1/python.1.gz
- maoli@ubuntu:~$ which ps
- /bin/ps
- maoli@ubuntu:~$ which python
- /usr/bin/python
清除屏幕上顯示的內(nèi)容
清除屏幕上顯示的內(nèi)容 - 「clear」。
查看幫助文檔查看幫助文檔 - 「man」 / 「info」 / 「help」 / 「apropos」。
- maoli@ubuntu:~$ ps --help
- Usage:
- ps [options]
- Try 'ps --help <simple|list|output|threads|misc|all>'
- or 'ps --help <s|l|o|t|m|a>'
- for additional help text.
- For more details see ps(1).
- maoli@ubuntu:~$ man ps
- PS(1) User Commands PS(1)
- NAME
- ps - report a snapshot of the current processes.
- SYNOPSIS
- ps [options]
- DESCRIPTION
- ...
查看系統(tǒng)和主機(jī)名
查看系統(tǒng)和主機(jī)名 - 「uname」 / 「hostname」。
- maoli@ubuntu:~$ uname
- Linux
- maoli@ubuntu:~$ hostname
- ubuntu
時間和日期
時間和日期 - 「date」 / 「cal」。
- maoli@ubuntu:~$ date
- 2020年 05月 28日 星期四 08:13:25 CST
- maoli@ubuntu:~$ cal
- 五月 2020
- 日 一 二 三 四 五 六
- 1 2
- 3 4 5 6 7 8 9
- 10 11 12 13 14 15 16
- 17 18 19 20 21 22 23
- 24 25 26 27 28 29 30
- 31
- maoli@ubuntu:~$ cal 5 2020
- 五月 2020
- 日 一 二 三 四 五 六
- 1 2
- 3 4 5 6 7 8 9
- 10 11 12 13 14 15 16
- 17 18 19 20 21 22 23
- 24 25 26 27 28 29 30
- 31
重啟和關(guān)機(jī)
重啟和關(guān)機(jī) - 「reboot」 / 「shutdown」。
- maoli@ubuntu:~$ shutdown -h +5 #五分鐘關(guān)機(jī)
- Shutdown scheduled for 四 2020-05-28 08:19:24 CST, use 'shutdown -c' to cancel. [root ~]#
- maoli@ubuntu:~$ shutdown -c
- maoli@ubuntu:~$ shutdown -r 12:00
- Shutdown scheduled for 四 2020-05-28 12:00:00 CST, use 'shutdown -c' to cancel
- maoli@ubuntu:~$ shutdown -c
說明:在執(zhí)行shutdown命令時會向登錄系統(tǒng)的用戶發(fā)出警告,可以在命令后面跟上警告消息來替換默認(rèn)的警告消息,也可以在-h參數(shù)后通過now來表示立刻關(guān)機(jī)。
退出登錄
退出登錄 - 「exit」 / 「logout」。
查看歷史命令
查看歷史命令 - 「history」。
- maoli@ubuntu:~$ history
- ...
- 625 date
- 626 cal
- 627 cal 5 2020
- 628 shutdown -c
- 629 shutdown -r 12:00
- 630 shutdown -c
- 631 history
- maoli@ubuntu:~$ !631
說明:查看到歷史命令之后,可以用!歷史命令編號來重新執(zhí)行該命令;通過history -c可以清除歷史命令。
實(shí)用程序
文件和文件夾操作
創(chuàng)建/刪除空目錄
創(chuàng)建/刪除空目錄 - 「mkdir」 / 「rmdir」。
- [root ~]# mkdir runsen
- [root ~]# mkdir -p abc/runsen
- [root ~]# rmdir runsen
創(chuàng)建/刪除文件
創(chuàng)建/刪除文件 - 「touch」 / 「rm」。
- maoli@ubuntu:~$ touch readme.txt
- maoli@ubuntu:~$ rm readme.txt
- rm: remove regular empty file ‘readme.txt ’? y
- [root ~]# rm -rf ;xyz&amp;nbsp;
touch命令用于創(chuàng)建空白文件或修改文件時間。在Linux系統(tǒng)中一個文件有三種時間:
- 更改內(nèi)容的時間 - mtime。
- 更改權(quán)限的時間 - ctime。
- 最后訪問時間 - atime。
rm的幾個重要參數(shù):
- -i:交互式刪除,每個刪除項(xiàng)都會進(jìn)行詢問。
- -r:刪除目錄并遞歸的刪除目錄中的文件和目錄。
- -f:強(qiáng)制刪除,忽略不存在的文件,沒有任何提示。
切換和查看當(dāng)前工作目錄
切換和查看當(dāng)前工作目錄 - 「cd」 / 「pwd」。
說明:cd命令后面可以跟相對路徑(以當(dāng)前路徑作為參照)或絕對路徑(以/開頭)來切換到指定的目錄,也可以用cd ..來返回上一級目錄。返回到上上一級目錄應(yīng)該給cd ../../命令。
查看目錄內(nèi)容
查看目錄內(nèi)容 - 「ls」。
- -l:以長格式查看文件和目錄。
- -a:顯示以點(diǎn)開頭的文件和目錄(隱藏文件)。
- -R:遇到目錄要進(jìn)行遞歸展開(繼續(xù)列出目錄下面的文件和目錄)。
- -d:只列出目錄,不列出其他內(nèi)容。
- -S / -t:按大小/時間排序。
查看文件內(nèi)容
查看文件內(nèi)容 - 「cat」 / 「tac」 / 「head」 / 「tail」 / 「more」 / 「less」 / 「rev」 / 「od」。
- maoli@ubuntu:~$ wget https://www.csdn.net/
- --2020-05-28 08:25:01-- https://www.csdn.net/
- 正在解析主機(jī) www.csdn.net (www.csdn.net)... 47.95.164.112
- 正在連接 www.csdn.net (www.csdn.net)|47.95.164.112|:443... 已連接。
- 已發(fā)出 HTTP 請求,正在等待回應(yīng)... 200 OK
- 長度: 未指定 [text/html]
- 正在保存至: “index.html”
- index.html [ <=> ] 420.39K 1.53MB/s in 0.3s
- 2020-05-28 08:25:02 (1.53 MB/s) - “index.html” 已保存 [430482]
- maoli@ubuntu:~$ cat index.html
- <!DOCTYPE html>
- ...
- maoli@ubuntu:~$ head -10 index.html
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta http-equiv="X-UA-Compatible" content="IE=Edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
- <meta name="apple-mobile-web-app-status-bar-style" content="black">
- <meta name="referrer"content="always">
- <meta name="msvalidate.01" content="3189512127C34C46BC74BED5852D45E4" />
- <title>CSDN-專業(yè)IT技術(shù)社區(qū)</title>
- maoli@ubuntu:~$ tail -2 index.html
- <script src="https://g.csdnimg.cn/side-toolbar/2.0/side-toolbar.js"></script>
- </html>
- maoli@ubuntu:~$ less index.html #相當(dāng)于cat
- maoli@ubuntu:~$ cat -n index.html |more
- ...
說明:上面用到了一個名為wget的命令,它是一個網(wǎng)絡(luò)下載器程序,可以從指定的URL下載資源。
拷貝/移動文件
拷貝/移動文件 - 「cp」 / 「mv」。
- maoli@ubuntu:~$ mkdir backup
- maoli@ubuntu:~$ cp index.html backup/
- maoli@ubuntu:~$ cd backup/
- maoli@ubuntu:~/backup$ ls
- index.html
- maoli@ubuntu:~/backup$ mv index.html csdn.html
- maoli@ubuntu:~/backup$ ls
- csdn.html
文件重命名
文件重命名 - 「rename」。
查找文件和查找內(nèi)容
查找文件和查找內(nèi)容 - 「find」 / 「grep」。
- maoli@ubuntu:~/backup$ find ./ -name "*.html"
- ./csdn.html
- maoli@ubuntu:~/backup$ find / -name "*.html"
- /usr/local/java/jdk-11.0.6/README.html
- /usr/local/python3/lib/python3.6/idlelib/help.html
- /usr/local/python3/lib/python3.6/test/sgml_input.html
- /usr/local/python3/lib/python3.6/test/test_difflib_expect.html
- ...
- maoli@ubuntu:~/backup$ find . -type f -size +2k
- ./csdn.html
- maoli@ubuntu:~/backup$ find . -type f -name "*.swp" -delete
- maoli@ubuntu:~/backup$ grep "<script>" csdn.html -n
- 3192: <script>
- maoli@ubuntu:~/backup$ grep -E \<\/?script.*\> csdn.html -n
- 12: <script src='//g.csdnimg.cn/tingyun/1.8.3/www.js' type='text/javascript'></script>
- 18: <script src="//csdnimg.cn/public/common/libs/jquery/jquery-1.9.1.min.js" type="text/javascript"></script>
- 19: <script src="//g.csdnimg.cn/??login-box/1.0.7/login-box.js,login-box/1.0.7/login-auto.js" type="text/javascript"></script>
- ...
說明:grep在搜索字符串時可以使用正則表達(dá)式,如果需要使用正則表達(dá)式可以用grep -E或者直接使用egrep。
創(chuàng)建鏈接和查看鏈接
創(chuàng)建鏈接和查看鏈接 - 「ln」 / 「readlink」。
- maoli@ubuntu:~/backup$ ls -l csdn.html
- -rw-rw-r-- 1 maoli maoli 430482 5月 28 08:30 csdn.html
- maoli@ubuntu:~/backup$ ln /home/maoli/backup/csdn.html /home/maoli/csdn
- maoli@ubuntu:~/backup$ ls -l csdn.html
- -rw-rw-r-- 2 maoli maoli 430482 5月 28 08:30 csdn.html
說明:鏈接可以分為硬鏈接和軟鏈接(符號鏈接)。硬鏈接可以認(rèn)為是一個指向文件數(shù)據(jù)的指針,就像Python中對象的引用計(jì)數(shù),每添加一個硬鏈接,文件的對應(yīng)鏈接數(shù)就增加1,只有當(dāng)文件的鏈接數(shù)為0時,文件所對應(yīng)的存儲空間才有可能被其他文件覆蓋。我們平常刪除文件時其實(shí)并沒有刪除硬盤上的數(shù)據(jù),我們刪除的只是一個指針,或者說是數(shù)據(jù)的一條使用記錄,所以類似于“文件粉碎機(jī)”之類的軟件在“粉碎”文件時除了刪除文件指針,還會在文件對應(yīng)的存儲區(qū)域填入數(shù)據(jù)來保證文件無法再恢復(fù)。軟鏈接類似于Windows系統(tǒng)下的快捷方式,當(dāng)軟鏈接鏈接的文件被刪除時,軟鏈接也就失效了。
壓縮/解壓縮和歸檔/解歸檔
.壓縮/解壓縮和歸檔/解歸檔 - 「gzip」 / 「gunzip」 / 「xz」。
- maoli@ubuntu:~$ ls
- redis-4.0.10.tar.tar.gz
- maoli@ubuntu:~$ gunzip redis-4.0.10.tar.tar.gz
- maoli@ubuntu:~$ ls
- redis-4.0.10.tar.tar
歸檔和解歸檔
歸檔和解歸檔 - 「tar」。
- maoli@ubuntu:~$ tar -xvf redis-4.0.10.tar
- redis-4.0.10/
- redis-4.0.10/.gitignore
- redis-4.0.10/00-RELEASENOTES
- redis-4.0.10/BUGS
- redis-4.0.10/CONTRIBUTING
- redis-4.0.10/COPYING
- redis-4.0.10/INSTALL
- redis-4.0.10/MANIFESTO
- redis-4.0.10/Makefile
- redis-4.0.10/README.md
- redis-4.0.10/deps/
- redis-4.0.10/deps/Makefile
- redis-4.0.10/deps/README.md
- ...
說明:歸檔(也稱為創(chuàng)建歸檔)和解歸檔都使用tar命令,通常創(chuàng)建歸檔需要-cvf三個參數(shù),其中c表示創(chuàng)建(create),v表示顯示創(chuàng)建歸檔詳情(verbose),f表示指定歸檔的文件(file);解歸檔需要加上-xvf參數(shù),其中x表示抽取(extract),其他兩個參數(shù)跟創(chuàng)建歸檔相同。
將標(biāo)準(zhǔn)輸入轉(zhuǎn)成命令行參數(shù)
將標(biāo)準(zhǔn)輸入轉(zhuǎn)成命令行參數(shù) - 「xargs」。
下面的命令會將查找當(dāng)前路徑下的html文件,然后通過xargs將這些文件作為參數(shù)傳給rm命令,實(shí)現(xiàn)查找并刪除文件的操作。
- maoli@ubuntu:~$ find . -type f -name "*.html" | xargs rm -f
下面的命令將a.txt文件中的多行內(nèi)容變成一行輸出到b.txt文件中,其中<表示從a.txt中讀取輸入,>表示將命令的執(zhí)行結(jié)果輸出到b.txt中。
- maoli@ubuntu:~$ xargs < a.txt > b.txt
顯示文件或目錄
顯示文件或目錄 - 「basename」 / 「dirname」。
其他相關(guān)工具
其他相關(guān)工具。
- 「sort」 - 對內(nèi)容排序
- 「uniq」 - 去掉相鄰重復(fù)內(nèi)容
- 「tr」 - 替換指定內(nèi)容為新內(nèi)容
- 「cut」 / 「paste」 - 剪切/黏貼內(nèi)容
- 「split」 - 拆分文件
- 「file」 - 判斷文件類型
- 「wc」 - 統(tǒng)計(jì)文件行數(shù)、單詞數(shù)、字節(jié)數(shù)
- 「iconv」 - 編碼轉(zhuǎn)換
- maoli@ubuntu:~$ cat foo.txt
- grape
- apple
- pitaya
- maoli@ubuntu:~$ cat bar.txt
- 100
- 200
- 300
- 400
- maoli@ubuntu:~$ paste foo.txt bar.txt
- grape 100
- apple 200
- pitaya 300
- 400
- maoli@ubuntu:~$ paste foo.txt bar.txt > hello.txt
- maoli@ubuntu:~$ cut -b 4-8 hello.txt
- pe 10
- le 20
- aya 3
- 0
- maoli@ubuntu:~$ cat hello.txt | tr '\t' ','
- grape,100
- apple,200
- pitaya,300
- ,400
- maoli@ubuntu:~$ split -l 100 sohu.html hello
- maoli@ubuntu:~$ wget https://www.baidu.com/img/bd_logo1.png
- maoli@ubuntu:~$ file bd_logo1.png
- bd_logo1.png: PNG image data, 540 x 258, 8-bit colormap, non-interlaced
- maoli@ubuntu:~$ wc index.html
- 3820 18696 430482 index.html
- maoli@ubuntu wget http://www.qq.com -O qq.html
- maoli@ubuntu iconv -f gb2312 -t utf-8 qq.html
管道和重定向管道的使用
管道的使用 - 「|」。
例子:查找當(dāng)前目錄下文件個數(shù)。
- maoli@ubuntu:~$ find ./ | wc -l
- 80801
例子:列出當(dāng)前路徑下的文件和文件夾,給每一項(xiàng)加一個編號。
- maoli@ubuntu:~$ ls | cat -n 1 abc 2 backup
例子:查找record.log中包含AAA,但不包含BBB的記錄的總數(shù)
- maoli@ubuntu:~$ cat record.log | grep AAA | grep -v BBB | wc -l
輸出重定向
輸出重定向和錯誤重定向 - 「>」 / 「>>」 / 「2>」。
- maoli@ubuntu:~$ cat readme.txt
- banana
- apple
- grape
- apple
- grape
- watermelon
- pear
- pitaya
- maoli@ubuntu:~$ cat readme.txt | sort | uniq > result.txt
- maoli@ubuntu:~$ cat result.txt
- apple
- banana
- grape
- pear
- pitaya
- watermelon
輸入重定向
輸入重定向 - 「<」。
- maoli@ubuntu:~$ echo 'hello, world!' > hello.txt
- maoli@ubuntu:~$ echo 'I will show you some code.' >> hello.txt
- maoli@ubuntu:~$ cat hello.txt
- hello, world!
- I will show you some code.
多重定向
多重定向 - 「tee」。
下面的命令除了在終端顯示命令ls的結(jié)果之外,還會追加輸出到ls.txt文件中。
- maoli@ubuntu:~$ ls | tee -a ls.txt
- maoli@ubuntu:~$ cat ls.txt
- abc
- backup
別名
「alias」創(chuàng)建別名
- maoli@ubuntu:~$ alias ll='ls -l'
- maoli@ubuntu:~$ alias frm='rm -rf'
- maoli@ubuntu:~$ ll
- 量 144920
- drwxrwxr-x 3 maoli maoli 4096 5月 28 08:20 abc
- maoli@ubuntu:~$frm abc
「unalias」刪除別名
- maoli@ubuntu:~$ funalias frm
- maoli@ubuntu:~$ frm index.html
- -bash: frm: command not found
文本處理字符流編輯器
字符流編輯器 - 「sed」。
sed是操作、過濾和轉(zhuǎn)換文本內(nèi)容的工具。假設(shè)有一個名為fruit.txt的文件,內(nèi)容如下所示。
- maoli@ubuntu:~$ cat -n fruit.txt
- 1 banana
- 2 grape
- 3 apple
- 4 watermelon
- 5 orange
接下來,我們在第2行后面添加一個pitaya。
- maoli@ubuntu:~$ sed '2a pitaya' fruit.txt
- banana
- grape
- pitaya
- apple
- watermelon
- orange
在第2行前面插入一個waxberry。
- maoli@ubuntu:~$ sed '2i waxberry' fruit.txt
- banana
- waxberry
- grape
- apple
- watermelon
- orange
刪除第3行。
- maoli@ubuntu:~$ sed '3d' fruit.txt
- banana
- grape
- watermelon
- orange
刪除第2行到第4行。
- maoli@ubuntu:~$ sed '2,4d' fruit.txt
- banana
- orange
將文本中的字符a替換為@。
- maoli@ubuntu:~$ sed 's#a#@#' fruit.txt
- b@nana
- gr@pe
- @pple
- w@termelon
- or@nge
將文本中的字符a替換為@,使用全局模式。
- maoli@ubuntu:~$ sed 's#a#@#g' fruit.txt
- b@n@n@
- gr@pe
- @pple
- w@termelon
- or@nge
模式匹配和處理語言
模式匹配和處理語言 - 「awk」。
awk是一種編程語言,也是Linux系統(tǒng)中處理文本最為強(qiáng)大的工具,它的作者之一和現(xiàn)在的維護(hù)者就是之前提到過的Brian Kernighan(ken和dmr最親密的伙伴)。通過該命令可以從文本中提取出指定的列、用正則表達(dá)式從文本中取出我們想要的內(nèi)容、顯示指定的行以及進(jìn)行統(tǒng)計(jì)和運(yùn)算,總之它非常強(qiáng)大。
假設(shè)有一個名為fruit2.txt的文件,內(nèi)容如下所示。
- \maoli@ubuntu:~$ cat fruit2.txt
- 1 banana 120
- 2 grape 500
- 3 apple 1230
- 4 watermelon 80
- 5 orange 400
顯示文件的第3行。
- maoli@ubuntu:~$ awk 'NR==3' fruit2.txt
- 3 apple 1230
顯示文件的第2列。
- maoli@ubuntu:~$awk '{print $2}' fruit2.txt
- banana
- grape
- apple
- watermelon
- orange
顯示文件的最后一列。
- maoli@ubuntu:~$ awk '{print $NF}' fruit2.txt
- 120
- 500
- 1230
- 80
- 400
輸出末尾數(shù)字大于等于300的行。
- maoli@ubuntu:~$ awk '{if($3 >= 300) {print $0}}' fruit2.txt
- 2 grape 500
- 3 apple 1230
- 5 orange 400
用戶管理
創(chuàng)建和刪除用戶
創(chuàng)建和刪除用戶 - 「useradd」 / 「userdel」。需要用root賬號創(chuàng)建
- maoli@ubuntu:~$ su root
- 密碼:
- root@ubuntu:/home/maoli# useradd Runsen
- root@ubuntu:/home/maoli# userdel Runsen
- -d - 創(chuàng)建用戶時為用戶指定用戶主目錄
- -g - 創(chuàng)建用戶時指定用戶所屬的用戶組
創(chuàng)建和刪除用戶組
創(chuàng)建和刪除用戶組 - 「groupadd」 / 「groupdel」。
用戶組主要是為了方便對一個組里面所有用戶的管理。
修改密碼
修改密碼 - 「passwd」。
- root@ubuntu:/home/maoli# passwd maoli
- New password:
- Retype new password:
- passwd: all authentication tokens updated successfully.
如果使用passwd命令時沒有指定命令作用的對象,則表示要修改當(dāng)前用戶的密碼。如果想批量修改用戶密碼,可以使用chpasswd命令。
- -l / -u - 鎖定/解鎖用戶。
- -d - 清除用戶密碼。
- -e - 設(shè)置密碼立即過期,用戶登錄時會強(qiáng)制要求修改密碼。
- -i - 設(shè)置密碼過期多少天以后禁用該用戶。
查看和修改密碼有效期查看和修改密碼有效期 - 「chage」。
設(shè)置maoli用戶100天后必須修改密碼,過期前15天通知該用戶,過期后15天禁用該用戶。
- root@ubuntu:/home/maoli# chage -M 100 -W 15 -I 15 maoli
切換用戶 - 「su」。
- root@ubuntu:/home/maoli# su maoli
- maoli@ubuntu:~$
以管理員身份執(zhí)行命令
以管理員身份執(zhí)行命令 - 「sudo」。
- maoli@ubuntu:~$ ls /root
- ls: cannot open directory /root: Permission denied
- maoli@ubuntu:~$
- sudo ls /root
- [sudo] password for maoli:
「說明」:如果希望用戶能夠以管理員身份執(zhí)行命令,用戶必須要出現(xiàn)在sudoers名單中,sudoers文件在 /etc目錄下,如果希望直接編輯該文件也可以使用下面的命令。
編輯sudoers文件
編輯sudoers文件 - 「visudo」。
這里使用的編輯器是vim,關(guān)于vim的知識在前面有講解。
五十二、ViM的使用該文件的部分內(nèi)容如下所示:
- ## Allow root to run any commands anywhere
- root ALL=(ALL) ALL
- ## Allows members of the 'sys' group to run networking, software,
- ## service management apps and more.
- # %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS
- ## Allows people in group wheel to run all commands
- %wheel ALL=(ALL) ALL
- ## Same thing without a password
- # %wheel ALL=(ALL) NOPASSWD: ALL
- ## Allows members of the users group to mount and unmount the
- ## cdrom as root
- # %users ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom
- ## Allows members of the users group to shutdown this system
- # %users localhost=/sbin/shutdown -h now
顯示用戶與用戶組的信息
顯示用戶與用戶組的信息 - 「id」。
- root@ubuntu:/home/maoli# id
- uid=0(root) gid=0(root) 組=0(root)
給其他用戶發(fā)消息
給其他用戶發(fā)消息 -「write」 / 「wall」。
發(fā)送方:
- root@ubuntu# write maoli
- Hello Maoli
- EOF
鍵入EOF表示信息結(jié)束,用Crtl+D組合鍵發(fā)送信息。輸入內(nèi)容會出現(xiàn)在用戶的屏幕上,同時通信中止。接收方:
- maoli@ubuntu:~$
- Message from root on pts/0 at 9:41 ...
- Hello Maoli
- EOF
查看/設(shè)置是否接收其他用戶發(fā)送的消息 - 「mesg」。
- maoli@ubuntu:~$ mesg
- is y
- maoli@ubuntu:~$ mesg n
- maoli@ubuntu:~$ mesg
- is n
如果想要發(fā)送一條信息給系統(tǒng)中所有用戶,可以使用wall命令,wall表示:write all。輸入wall,然后編輯信息,如果shell支持可以使用中文。然后使用Crtl+D組合鍵發(fā)送信息。這樣系統(tǒng)所有登錄用戶的桌面會收到信息。如 果在網(wǎng)絡(luò)上,可以使用rwall命令把信息發(fā)送到局域網(wǎng)上所有的用戶。
文件系統(tǒng)
- 文件和路徑命名規(guī)則:文件名的最大長度與文件系統(tǒng)類型有關(guān),一般情況下,文件名不應(yīng)該超過255個字符,雖然絕大多數(shù)的字符都可以用于文件名,但是最好使用英文大小寫字母、數(shù)字、下劃線、點(diǎn)這樣的符號。文件名中雖然可以使用空格,但應(yīng)該盡可能避免使用空格,否則在輸入文件名時需要用將文件名放在雙引號中或者通過\對空格進(jìn)行轉(zhuǎn)義。
- 擴(kuò)展名:在Linux系統(tǒng)下文件的擴(kuò)展名是可選的,但是使用擴(kuò)展名有助于對文件內(nèi)容的理解。有些應(yīng)用程序要通過擴(kuò)展名來識別文件,但是更多的應(yīng)用程序并不依賴文件的擴(kuò)展名,就像file命令在識別文件時并不是依據(jù)擴(kuò)展名來判定文件的類型。
- 隱藏文件:以點(diǎn)開頭的文件在Linux系統(tǒng)中是隱藏文件(不可見文件)。
目錄結(jié)構(gòu)
- /bin - 基本命令的二進(jìn)制文件。
- /boot - 引導(dǎo)加載程序的靜態(tài)文件。
- /dev - 設(shè)備文件。
- 「/etc」 - 配置文件。
- /home - 普通用戶主目錄的父目錄。
- /lib - 共享庫文件。
- /lib64 - 共享64位庫文件。
- /lost+found - 存放未鏈接文件。
- /media - 自動識別設(shè)備的掛載目錄。
- /mnt - 臨時掛載文件系統(tǒng)的掛載點(diǎn)。
- /opt - 可選插件軟件包安裝位置。
- /proc - 內(nèi)核和進(jìn)程信息。
- 「/root」 - 超級管理員用戶主目錄。
- /run - 存放系統(tǒng)運(yùn)行時需要的東西。
- /sbin - 超級用戶的二進(jìn)制文件。
- /sys - 設(shè)備的偽文件系統(tǒng)。
- /tmp - 臨時文件夾。
- 「/usr」 - 用戶應(yīng)用目錄。
- /var - 變量數(shù)據(jù)目錄。
訪問權(quán)限
改變文件模式
1.「chmod」 - 改變文件模式比特。
- maoli@ubuntu:~/backup$ ls -l
- 總用量 424
- -rw-rw-r-- 2 maoli maoli 430482 5月 28 08:30 csdn.html
- maoli@ubuntu:~/backup$ chmod g+w,o+w csdn.html
- maoli@ubuntu:~/backup$ ls -l
- 總用量 424
- -rw-rw-rw- 2 maoli maoli 430482 5月 28 08:30 csdn.html
- maoli@ubuntu:~/backup$ chmod 644 csdn.html
- maoli@ubuntu:~/backup$ ls -l
- 總用量 424
- -rw-r--r-- 2 maoli maoli 430482 5月 28 08:30 csdn.html
說明:通過上面的例子可以看出,用chmod改變文件模式比特有兩種方式:一種是字符設(shè)定法,另一種是數(shù)字設(shè)定法。除了chmod之外,可以通過umask來設(shè)定哪些權(quán)限將在新文件的默認(rèn)權(quán)限中被刪除。

改變文件所有者
2.「chown」 - 改變文件所有者。
- maoli@ubuntu:~/backup$ ls -l
- 總用量 424
- -rw-r--r-- 2 maoli maoli 430482 5月 28 08:30 csdn.html
- maoli@ubuntu:~/backup$ sudo chown root csdn.html
- [sudo] maoli 的密碼:
- maoli@ubuntu:~/backup$ ls -l
- 總用量 424
- -rw-r--r-- 2 root maoli 430482 5月 28 08:30 csdn.html
3.「chgrp」 - 改變用戶組。
本文已收錄 GitHub,傳送門~[1] ,里面更有大廠面試完整考點(diǎn),歡迎 Star。
Reference
[1]傳送門~https://github.com/MaoliRUNsen/runsenlearnpy100