生信人的Linux1-13

Linux Day1: cd/pwd/mkdir/rmdir

1.目錄的相關(guān)操作:

比較特殊的目錄:

  • . 代表此層目錄
  • .. 代表上一層目錄
  • -代表前一個(gè)工作目錄
  • ~ 代表“目前用戶身法”所在的文件夾
  • ~account 代表 account這個(gè)用戶的主文件夾(account是個(gè)賬戶的名稱(chēng))

幾個(gè)常見(jiàn)的處理目錄的命令:

  • cd:切換目錄(Change DIrectory)
    例:cd dirname:切換至dirname路徑下
    cd 沒(méi)有加上任何命令,也還是代表回到自己主文件夾的意思(同 cd ~)

  • pwd:顯示當(dāng)前目錄(Print Working Directory),以絕對(duì)路徑的方式顯示當(dāng)前工作目錄。
  • 絕對(duì)路徑:由根目錄(/)開(kāi)始寫(xiě)起的文件名或目錄名稱(chēng),例如/home/dmtsai/.bashrc。
  • 相對(duì)路徑: 相對(duì)于當(dāng)前路徑的文件名寫(xiě)法。例如./home/dmtsai 或 ../../home/dmtsai/。
  • 反正開(kāi)頭不是/就屬于相對(duì)路徑的寫(xiě)法。

  • mkdir :新建一個(gè)新的目錄
  • mkdir有兩個(gè)重要的參數(shù):
  1. -m:配置文件案的權(quán)限。
  2. -p:幫助你直接將所需的目錄(包含上層目錄)遞歸創(chuàng)建起來(lái)

對(duì)比touch、nano

例:利用cd切換到tmp文件夾cd /tmp,在tmp下創(chuàng)建文件夾testmkdir test,也可以用到-p在tmp下面創(chuàng)建分層文件夾mkdir -p test1/test2/test3。


  • rmdir:刪除一個(gè)空的目錄
  • 相對(duì)于mkdir,rmdir也有參數(shù):-p:連同上層“空的”目錄一起刪除。

例:現(xiàn)在我們想要?jiǎng)h除上面tmp下的test文件夾,原理同mkdir。


  • tar:壓縮/解壓縮(后續(xù)章節(jié)會(huì)講到)

Linux Day2: ls/cp/rm/mv

2.1 查看文件與目錄: ls

  • 需要結(jié)合不同參數(shù)反復(fù)練習(xí)。

2.2 復(fù)制、刪除與移動(dòng):cp,rm,mv

cp:復(fù)制+創(chuàng)建連接文件(快捷方式)
mv:移動(dòng)+ 重命名(rename)
rm:刪除


  • cp (復(fù)制文件或目錄)
    例1cp ~/.bashrc /tmp/bashrc表示:將 .bashrc 復(fù)制到 /tmp 文件下,并更名為bashrc。
cp -i  ~/.bashrc /tmp/bashrc
cp:是否覆蓋'/tmp/bashrc'? y  

例2:切換目錄到 /tmp,并將var/log/wtmp 復(fù)制到 /tmp 且查看屬性。
cp /var/log/wtmp .最后的“空格 ."不要忘。
查看屬性:ls -l /var/log/wtmp wtmp
(ls -l :列出長(zhǎng)數(shù)據(jù)串,包含文件的屬性與權(quán)限等數(shù)據(jù))

rw-rw-r-- 1 root utmp 6528 10月  3 10:45 /var/log/wtmp
-rw-rw-r-- 1 lc   lc   6528 10月  3 10:53 wtmp

cp 在不加任何參數(shù)的情況下,文件的某些屬性/權(quán)限會(huì)改變(如上 wtmp的時(shí)間發(fā)生量改變); 那么考慮以下命令:
cp -a /var/log/wtmp wtmp_2這里用到了 cp 的參數(shù) -a :相當(dāng)于 -pdr 的意思。


例子3:復(fù)制 /etc/ 這個(gè)目錄下的所有內(nèi)容到 /tmp 下面。
cp /etc/ /tmp提示cp: 略過(guò)目錄'/etc/',由于目錄不能直接復(fù)制的原因。
考慮:cp -r /etc/ /tmp,但是我的電腦etc里面的文件打不開(kāi),提示權(quán)限不夠。
(-r :遞歸持續(xù)復(fù)制,用于目錄的復(fù)制行為,但是文件與目錄的權(quán)限可能會(huì)改變)也可以用cp -a /etc/ /tmp來(lái)執(zhí)行命令,尤其是在備份的情況下。
例4:建立例1 bashrc的連接檔(symbolic link)
cp的更多用法參考鳥(niǎo)哥的例子(5/6)
總之-再?gòu)?fù)制時(shí),需要考慮到以下:
1.是否需要完整的保留來(lái)源檔案的信息?
2.來(lái)源檔案是否為連接檔(symbolic link file)?(需要后面完善連接檔的學(xué)習(xí))
3.來(lái)源檔是是否為特殊的文檔,例如FIFO,socket(后面理解)?
4.來(lái)源文件是否為目錄?


  • rm(移除檔案或目錄)
    選項(xiàng)和參數(shù):
  1. -f:就是force的意思,忽略不存在的 檔案,不會(huì)警告訊息;
  2. -i:互動(dòng)模式,在刪除前會(huì)詢問(wèn)使用者是否動(dòng)作(常用這個(gè)吧)。
  3. -r :遞歸刪除,最常用在目錄的刪除
    4.考慮以下兩個(gè)命令:rm -rf;rm -rf /刪除根目錄。
    例1:通過(guò)通配符的幫忙,將/tmp底下以bashrc開(kāi)頭檔名通通刪除。
    代表的是0到無(wú)窮多個(gè)任意字符)
    rm -i bashrc.
    例2:刪除一個(gè)帶有-開(kāi)頭的檔案。
    首先用touch命令建立一個(gè)空檔案,touch ./-aaa-,查看剛創(chuàng)立的文件ls -l,提示為:-rw-rw-r-- 1 ywu ywu 0 Oct 3 18:31 -aaa-(檔案大小為零)。
    試著用rm命令刪除文件,rm -aaa-,
    提示為:
    Try 'rm ./-aaa-' to remove the file '-aaa-'.為什么會(huì)提示這個(gè)命令不太明白,因?yàn)閞m有移動(dòng)的意思?
    Try 'rm --help' for more information.因?yàn)椤?“是選項(xiàng),所以系統(tǒng)會(huì)誤判,所以盡量避免首字母是”-“是的方法,在目錄上加上[ ./ ]。(”-“需要進(jìn)一步理解其意義)

  • mv (移動(dòng)檔案與目錄,或更名)
    選項(xiàng)和參數(shù):
  1. -f:就是force的意思,如果檔案已經(jīng)存在,不會(huì)詢問(wèn)而直接覆蓋;
  2. -i:互動(dòng)模式,若目標(biāo)檔案(destination)已經(jīng)存在,不會(huì)詢問(wèn)而直接覆蓋。
  3. -u :若目標(biāo)檔案(destination)已經(jīng)存在,且source比較新,才會(huì)更新(update)。
    例1:建立目錄wy,并在wy目錄下分別建立文件mvtest,bashrc,通過(guò)mv命令將bashrc移動(dòng)到mvtest。mkdir wy,利用pwd確認(rèn)是否創(chuàng)建成功,切換到cd wy,分別建立文件mvtest,bashrc。再通過(guò)mv bashrc mvtest.
    mv命令

Linux Day3: cat/nl/head/tail

2.3 檔案內(nèi)容查閱

  • cat 由第一行開(kāi)始顯示檔案內(nèi)容。
  • tac 從最后一行開(kāi)始顯示(可以看出tac是cat的倒著寫(xiě)。)
  • nl 顯示的時(shí)候,順著輸出行號(hào)!
  • head 只看頭幾行
  • tail 只看尾巴就行
  • od 以二進(jìn)制的方式讀取檔案內(nèi)容?。ǘM(jìn)制需要了解)
  • 直接檢視檔案內(nèi)容可以使用 cat/tac/nl 這幾個(gè)命令!

  • cat (concatenate連續(xù))
    -n: 打印出行號(hào),連同空白行也會(huì)有行號(hào),與 -b 的選項(xiàng)不同;
    -b: 列出行號(hào),僅針對(duì)非空白行做行號(hào)顯示,空白行不標(biāo)行號(hào)!
    例1:檢閱/etc/issue 這個(gè)檔案的內(nèi)容
cat /etc/issue 
Ubuntu 16.04.3 LTS \n \l

給上面的例子加印行號(hào):
用 -n查看:

 cat -n /etc/issue
     1  Ubuntu 16.04.3 LTS \n \l
     2  

用-b查看:

cat -b /etc/issue
     1  Ubuntu 16.04.3 LTS \n \l
  • cat配合more/less 效果更好。

  • tac(反向列示)
    tac 與 cat 是相反的兩個(gè)例子。tac是由最后一行到第一行,cat由第一行到最后一行顯示。

  • nl(添加行號(hào)打印)
$ nl /etc/issue
     1  Ubuntu 16.04.3 LTS \n \l
       
$ nl -b a /etc/issue
     1  Ubuntu 16.04.3 LTS \n \l
     2  
$ nl -b a -n rz /etc/issue
000001  Ubuntu 16.04.3 LTS \n \l
000002  
$ nl -b a -n rz -w3 /etc/issue
001 Ubuntu 16.04.3 LTS \n \l
002 
$ nl -b a -n rz -w2 /etc/issue
01  Ubuntu 16.04.3 LTS \n \l
02  

nl 可以將輸出的檔案內(nèi)容自動(dòng)的補(bǔ)上行號(hào)!其預(yù)設(shè)的結(jié)果與cat -n 有點(diǎn)不太一樣 , nl 可以將行號(hào)做比較多的顯示設(shè)計(jì),包括位數(shù)與是否補(bǔ)齊 0 等等功能。


  • head(取出前面幾行)
    選項(xiàng)與參數(shù):
    1.-n :后面接數(shù)字,代表顯示幾行的意思。默認(rèn)的情況下,顯示前面十行:(空格也算是一行)
$ head /etc/inputrc 
# /etc/inputrc - global inputrc for libreadline
# See readline(3readline) and `info rluserman' for more information.

# Be 8 bit clean.
set input-meta on
set output-meta on

# To allow the use of 8bit-characters like the german umlauts, uncomment
# the line below. However this makes the meta key not work as a meta key,
# which is annoying to those which don't need to type in 8-bit characters.

若要顯示前面20行,head -n 20 /etc/inputrc;

$ head -n 20 /etc/inputrc
# /etc/inputrc - global inputrc for libreadline
# See readline(3readline) and `info rluserman' for more information.

# Be 8 bit clean.
set input-meta on
set output-meta on

# To allow the use of 8bit-characters like the german umlauts, uncomment
# the line below. However this makes the meta key not work as a meta key,
# which is annoying to those which don't need to type in 8-bit characters.

# set convert-meta off

# try to enable the application keypad when it is called.  Some systems
# need this to enable the arrow keys.
# set enable-keypad on

# see /usr/share/doc/bash/inputrc.arrows for other codes of arrow keys

# do not bell on tab-completion

問(wèn)題: 好像也沒(méi)有20行?

2.后面100行如果不打?。?code>head -n -100/etc/man.config(注意-100)。


tail(取出后面幾行)
選項(xiàng)與參數(shù):

  1. -n:后面數(shù)字,代表顯示幾行的意思。
  2. -f: 表示持續(xù)偵測(cè)后面所接的 檔名,要等到按下【ctrl】-c 才會(huì)結(jié)束tail的偵測(cè)。
  3. 與head相比較記憶:tail -n +100 /etc/man.config代表從改檔案100行以后都會(huì)被列出來(lái)。
  4. 我的tail與head有同樣的問(wèn)題存在樣。
  • 需要顯示 /etc/man.config 的11行到20行?
  • head -n 20 /etc/man.config l tail -n 10需要用到管道命令,后面學(xué)習(xí)。

od:非純文本文件
選項(xiàng)或參數(shù):

  1. -t: 后面可以接各種【類(lèi)型(TYPE0】的輸出,例如:
    a :利用默認(rèn)的字符來(lái)輸出;(有點(diǎn)多,顯示不完)
    c :使用ASCⅡ字符來(lái)輸出。

Linux Day4: more/less/touch

1. more 一頁(yè)一頁(yè)的顯示檔案內(nèi)容。

  • 空格鍵 (space):代表向下翻一頁(yè);
  • Enter :代表向下翻『一行』;
  • /字符串 :代表在這個(gè)顯示癿內(nèi)容弼中,向下搜尋『字符串』這個(gè)關(guān)鍵詞;
  • :f :立刻顯示出文件名以及目前顯示癿行數(shù);
  • q :代表立刻離開(kāi) more ,不再顯示該檔案內(nèi)容。
  • b 或者 [ctrl]-b :代表往回翻頁(yè),不過(guò)這勱作叧對(duì)檔案有用,對(duì)管線無(wú)用。
$ more /etc/inputrc
# /etc/inputrc - global inputrc for libreadline
# See readline(3readline) and `info rluserman' for more information.

# Be 8 bit clean.
set input-meta on
set output-meta on

# To allow the use of 8bit-characters like the german umlauts, uncomment
# the line below. However this makes the meta key not work as a meta key,
# which is annoying to those which don't need to type in 8-bit characters.

# set convert-meta off

# try to enable the application keypad when it is called.  Some systems
# need this to enable the arrow keys.
# set enable-keypad on

# see /usr/share/doc/bash/inputrc.arrows for other codes of arrow keys

# do not bell on tab-completion
# set bell-style none
# set bell-style visible

# some defaults / modifications for the emacs mode
$if mode=emacs

# allow the use of the Home/End keys
"\e[1~": beginning-of-line
"\e[4~": end-of-line

# allow the use of the Delete/Insert keys
"\e[3~": delete-char

最后一行
more命令

對(duì)于52%的說(shuō)明:more 后面接的檔案內(nèi)容行數(shù)大亍屏幕輸出的行數(shù)時(shí), 就會(huì)出現(xiàn)類(lèi)似上面的圖示。最后一行會(huì)顯示出目前顯示癿百分比, 而且還可以在最后一行輸入一些有用的指令:
按下enter:

enter

2. less 與 more 類(lèi)似,但是比 more 更好的是,他可以往前翻頁(yè),也可以向前搜索!

其命令有:

  • 空格鍵 :向下翻勱一頁(yè);
  • [pagedown]:向下翻勱一頁(yè);
  • [pageup] :向上翻勱一頁(yè);
  • /字符串 :向下搜尋『字符串』癿功能;
  • ?字符串 :向上搜尋『字符串』癿功能;
  • n :重復(fù)前一個(gè)搜尋 (不 / 戒 ? 有關(guān)!)
  • N :反向癿重復(fù)前一個(gè)搜尋 (不 / 戒 ? 有關(guān)!)
  • q :離開(kāi) less 這個(gè)程序;

這個(gè)為使用less搜索文件后的:less /etc/inputrc

less

3.修改檔案時(shí)間或建置新檔: touch

選項(xiàng)與參數(shù):
-a :僅修訂 access time;
-c :僅修改檔案的時(shí)間,若該檔案不存在則不建立新檔案;
-d :后面可以接欲修訂的日期而不用目前的日期,也可以使用 --date="日期或時(shí)間"
-m :僅修改 mtime ;
-t :后面可以接欲修訂的間而不用目前的時(shí)間,格式為[YYMMDDhhmm]

linux 底下都會(huì)記錄許多的時(shí)間參數(shù), 其實(shí)是有三個(gè)主要的變動(dòng)時(shí)間,那么三個(gè)時(shí)間的意義是什么呢?

  • modification time (mtime):
    當(dāng)該檔案的『內(nèi)容數(shù)據(jù)』變更時(shí),就會(huì)更新這個(gè)時(shí)間!內(nèi)容數(shù)據(jù)指的是檔案的內(nèi)容,而不是檔案的屬性或權(quán)限喔!
  • status time (ctime):
    當(dāng)該檔案的『狀忞 (status)』改變時(shí),就會(huì)更新這個(gè)時(shí)間,舉例來(lái)說(shuō),像是權(quán)限不屬性被更改了,都會(huì)更新這個(gè)時(shí)間啊。
  • access time (atime):
    當(dāng)『該檔案癿內(nèi)容被取用』時(shí),就會(huì)更新這個(gè)讀取時(shí)間 (access)。舉例來(lái)說(shuō),我們使用 cat 去讀取/etc/inputrc, 就會(huì)更新該檔案的 atime 了。
    范例一:新建一個(gè)空的檔案并觀察時(shí)間
$ cd /tmp
$ pwd
/tmp
$ touch testtouch
$ ls -l testtouch
-rw-rw-r-- 1 ywu ywu 0 Oct  5 18:49 testtouch

這個(gè)檔案的大小是 0 呢!在預(yù)設(shè)的狀忞下,如果 touch 后面有接檔
案, 則該檔案的三個(gè)時(shí)間 (atime/ctime/mtime) 都會(huì)更新為目前的時(shí)間。若該檔案不存在,則會(huì)建立一個(gè)新的空的檔案喔!
范例二:修改案例二的testtouch檔案,將日期調(diào)整為兩天前

$ touch -d "2 days ago" testtouch
$ ls -l testtouch
-rw-rw-r-- 1 ywu ywu 0 Oct  3 18:56 testtouch

范例三:將上個(gè)范例的testtouch 日期改為 2018/10/05 2:02

$ touch -t 1810050202 testtouch
$ ls -l testtouch
-rw-rw-r-- 1 ywu ywu 0 Oct  5 02:02 testtouch

Linux Day5:操作系統(tǒng)與基礎(chǔ)

Linux操作系統(tǒng)與基礎(chǔ)。

  • Linux的基本原則:
  1. 由目的單一的小程序組成:組合小程序完成負(fù)責(zé)任務(wù)。
  2. 一切皆文件;
  3. 盡量避免捕獲用戶接口;
  4. 配置文件保存為純文本格式;

  • GUI接口:
  • ClI接口:
  • 命令提示符:# root;$ 普通用戶。

  • 命令格式:
    命令 選項(xiàng) 參數(shù)
  • 選項(xiàng):
    1.短選項(xiàng):-(多個(gè)選項(xiàng)可以組合:-a -b = -ab);
    2.長(zhǎng)選項(xiàng): --
    3.某些選項(xiàng)是可以帶參數(shù)的,
  • 參數(shù):命令的作用對(duì)象。

  • su : switch user
    (# su [-1] 用戶名)全切換的意思。

  • passwd:
$ passwd
Changing password for ywu.
(current) UNIX password: 
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully

Linux Day6: 路徑、權(quán)限

一些概念:

  • 目錄:working directoy、current + directory;目錄也是一種文件,映像路徑(FHS:文件系統(tǒng)層級(jí)標(biāo)準(zhǔn)。)

  • 路徑:從指定起始點(diǎn)到目的地所經(jīng)過(guò)的位置。

  • 文件系統(tǒng):file system。
    馬哥:文件名是不是文件的數(shù)據(jù)?

  • 命令類(lèi)型:
    內(nèi)置命令(shell內(nèi)置),內(nèi)部,內(nèi)建
    外部命令:在文件系統(tǒng)的某個(gè)路徑下有一個(gè)與命令對(duì)應(yīng)的可執(zhí)行文件。

  • 環(huán)境變量:PATH:使用冒號(hào)分隔的路徑。
    printenv顯示出工作環(huán)境:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/

相對(duì)路徑與絕對(duì)路徑:(樹(shù)狀圖說(shuō)明)

路徑.PNG
  • 絕對(duì)路徑:想要查找??這個(gè)文件,如果從?(根目錄)開(kāi)始查找,通過(guò)?,找到??;這個(gè)就叫絕對(duì)路徑。
  • 相對(duì)路徑:如果你目前所處?這個(gè)文件,那么想要查找??這個(gè)文件,無(wú)需要回到而根目錄,可以直接開(kāi)始從?向下查找。(相對(duì)路徑:相對(duì)于目前所處位置的路徑)
  • 那么目前多處位置為??,想要查找??這個(gè)文件,就不能用相對(duì)路徑,需要先回到?到??的某個(gè)節(jié)點(diǎn)上開(kāi)始查找。

  • list:ls(列出,列表)
$ ls -l
total 12
-rw-rw-r-- 1 ywu  ywu     0 Oct  3 18:31 -aaa-
-rw-r--r-- 1 root root  207 Oct  1 09:45 readme.txt
-rw-rw-r-- 1 ywu  ywu     0 Oct  5 02:02 testtouch
drwxrwxr-x 2 ywu  ywu  4096 Oct  3 17:22 tmp
drwxrwxr-x 3 ywu  ywu  4096 Oct  5 18:56 wy

鳥(niǎo)哥:-rw-rw-r--第一個(gè)字符代表這個(gè)檔案是『目彔、檔案戒鏈接文件等等』:

  • 當(dāng)為[ d ]則是目彔,例如上表檔名為『.gconf』的那一行;
  • 當(dāng)為[ - ]則是檔案,例如上表檔名為『install.log』那一行;
  • 若是[ l ]則表示為連結(jié)檔(link file);
  • 若是[ b ]則表示為裝置文件里面的可供儲(chǔ)存的接口謳備(可隨機(jī)存取裝置);
  • 若是[ c ]則表示為裝置文件里面的串行端口謳備,例如鍵盤(pán)、鼠標(biāo)(一次性讀取裝置)

文件類(lèi)型:(馬哥簡(jiǎn)潔版)
-:普通文件(f)
d:目錄文件
b:塊設(shè)備文件(block)
c: 字符設(shè)備文件(character)
l:符號(hào)連接文件(symbolic link file)
p:命令管道文件(pipe)
s:套接字文件(socket)

  • 鳥(niǎo)哥:接下來(lái)的字符中,以三個(gè)為一組,且均為『rwx』 的三個(gè)參數(shù)的組合其中,[ r ]代表可讀(read)、 [ w ]代表可寫(xiě)(write)、 [ x ]代表可執(zhí)行(execute)。 要注意的是,這三個(gè)權(quán)限的位置不會(huì)改變,如果沒(méi)有權(quán)限,就會(huì)出現(xiàn)減號(hào)[ - ]而已。
    先將整個(gè)類(lèi)型不權(quán)限數(shù)據(jù)分開(kāi)查閱,并將十個(gè)字符整理成為如下所示:
    [-][rwx][r-x][r--]
    1 234 567 890
    1 為:代表這個(gè)文件名為目彔戒檔案,本例中為檔案(-);
    234 為:擁有者的權(quán)限,本例中為可讀、可寫(xiě)、可執(zhí)行(rwx);
    567 為:同群組用戶權(quán)力,本例中為可讀可執(zhí)行(rx);
    890 為:其他用戶權(quán)力,本例中為可讀(r)
    見(jiàn)下圖:
    文件屬性.鳥(niǎo)哥
-rw-rw-r-- 1 ywu  ywu     0 Oct  5 02:02 testtouch
ls -l.鳥(niǎo)哥

ls的選項(xiàng)與參數(shù):(鳥(niǎo)哥)
-a :全部的檔案,連同隱藏檔( 開(kāi)頭為 . 的檔案) 一起列出來(lái)(常用)
-A :全部的檔案,連同隱藏檔,但不包括 . 不 .. 這兩個(gè)目錄
-d :僅列出目錄本身,而不是列出目弽內(nèi)癿檔案數(shù)據(jù)(常用)
-f :直接列出結(jié)果,而丌迚行排序 (ls 預(yù)謳會(huì)以檔名排序!)
-F :根據(jù)檔案、目弽等信息,給予附加數(shù)據(jù)結(jié)構(gòu),例如:
*:代表可執(zhí)行文件; /:代表目弽; =:代表 socket 檔案; |:代表 FIFO 檔案;
-h :將檔案容量以人類(lèi)較易讀癿方式(例如 GB, KB 等等)列出杢;
-i :列出 inode 號(hào)碼,inode 癿意義下一章將會(huì)介紹;
-l :長(zhǎng)數(shù)據(jù)串行出,包噸檔案癿屬性不權(quán)限等等數(shù)據(jù);(常用)
-n :列出 UID 不 GID 而非使用者不群組癿名稱(chēng) (UID 不 GID 會(huì)在賬號(hào)管理提
到!)
-r :將排序結(jié)果反向輸出,例如:原本檔名由小到大,反向則為由大到?。?br> -R :連同子目弽內(nèi)容一起列出杢,等亍該目弽下癿所有檔案都會(huì)顯示出杢;
-S :以檔案容量大小排序,而丌是用檔名排序;
-t :依時(shí)間排序,而丌是用檔名。
--color=never :丌要依據(jù)檔案特性給予顏色顯示;
--color=always :顯示顏色
--color=auto :譏系統(tǒng)自行依據(jù)謳定杢判斷是否給予顏色
--full-time :以完整時(shí)間模式 (包噸年、月、日、時(shí)、分) 輸出
--time={atime,ctime} :輸出 access 時(shí)間戒改變權(quán)限屬性時(shí)間 (ctime)
而非內(nèi)容變更時(shí)間 (modification time)


Linux Day7: Date/echo/print

學(xué)習(xí)man,結(jié)合date一起學(xué)習(xí):

date:時(shí)間管理
linux:rtc

硬件時(shí)間(clock),系統(tǒng)時(shí)鐘(date)
hwclock
-w:
-s:
我的用戶好像顯示不出clock時(shí)間。

獲得命令的幫助:

  • 內(nèi)部命令:help COMMAND
  • 外部命令:COMMAND --help
  • 命令手冊(cè):manual(man COMMAND)

man 分章節(jié):
1.用戶命令
2.系統(tǒng)調(diào)用
3.庫(kù)用戶
4.特殊軟件(設(shè)備文件)
5.文件格式(配置文件的語(yǔ)法)
6.游戲
7.雜項(xiàng)
8.管理命令(/sbin,/usr/sbin,/usr/local/sbin)

基本上,man page 大致分成底下這幾個(gè)部分:


man命令.鳥(niǎo)哥

舉例:man time

DATE(1)                             User Commands                             DATE(1)

NAME
       date - print or set the system date and time

SYNOPSIS
       date [OPTION]... [+FORMAT]
       date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

DESCRIPTION
       Display the current time in the given FORMAT, or set the system date.

       Mandatory arguments to long options are mandatory for short options too.

       -d, --date=STRING
              display time described by STRING, not 'now'

       -f, --file=DATEFILE
              like --date; once for each line of DATEFILE

       -I[FMT], --iso-8601[=FMT]
等等...

同more/less一樣,man最后也有個(gè)光標(biāo)供你翻頁(yè):


man 翻頁(yè).PNG

man page常用的按鍵有:


man 按鍵,鳥(niǎo)哥.PNG

那么上面synopsis:說(shuō)明可以設(shè)置的時(shí)間格式
date一下目前的時(shí)間:

date
Sun Oct  7 17:41:41 CST 2018

如果想要修改時(shí)間,那么可以按照[MMDDhhmm[[CC]YY][.ss]]的格式修改時(shí)間。按照DESCRIPTION中介紹:

$ date +%y
18
$ date +%Y
2018
$ date +%Y/%m/%d
2018/10/07
$ date +%H:%M
18:18
$ date +%T
18:21:58
$ date +%F
2018-10-07
  • 顯示日歷的指令
  • 簡(jiǎn)單好用的計(jì)算機(jī):bc

練習(xí)echo、printf命令:
1.echo命令是內(nèi)部命令還是外部命令?

$ type echo
echo is a shell builtin
  1. 作用?
$ man echo
 echo - display a line of text

3.如何換行顯示

$ echo -e "this year is 2018.\n today is 08"
this year is 2018.
 today is 08
$ echo -e "this year is 2018.\n today is 08"
this year is 2018.
 today is 08
$ echo -e "this year is 2018./b today is 08"
this year is 2018./b today is 08
$ echo -e "this year is 2018.\b today is 08"
this year is 2018 today is 08
$ echo -e "this year is 2018.\t today is 08"
this year is 2018.   today is 08
$ echo -e "this year is 2018.\v today is 08"
this year is 2018.
                   today is 08

printf:

man printf
NAME
       printf - format and print data
$ printf "The year is 2018"
The year is 2018

$ printf "The year is 2018.\n Today is 08"
The year is 2018.
 Today is 08$ 

Linux Day8: 目錄管理

學(xué)習(xí)寄言:時(shí)間相關(guān)函數(shù)在生信用的少,環(huán)境變量,還有目錄路徑需要加強(qiáng)理解。

$ ls /
bin   dev   initrd.img  lost+found  opt     root  snap  tmp   umac  vmlinuz
boot  etc   lib     media       proc    run   srv   trainee   usr   wyx
data  home  lib64   mnt     public  sbin  sys   trainee2  var
  • /boot:系統(tǒng)啟動(dòng)相關(guān)的文件,如內(nèi)核,initrd,以及grub(bootloader)
  • /dev:設(shè)備文件
    1.塊設(shè)備:隨機(jī)訪問(wèn),數(shù)據(jù)塊(如有ABCD四個(gè)文件,想要訪問(wèn)C,可直接訪問(wèn)C,無(wú)需經(jīng)過(guò)AB)
    2.字符設(shè)備:線性訪問(wèn),按字符為單位
    3.設(shè)備號(hào):主設(shè)備號(hào),次設(shè)備號(hào)
  • /etc:配置文件
  • /home:用戶的家目錄;每個(gè)用戶的家目錄默認(rèn)為/home/USERNAME。
  • /root:管理員的家目錄。
  • lib:庫(kù)文件
    1.靜態(tài)庫(kù):.a
    2.動(dòng)態(tài)庫(kù):.dll,
    3./lib/modules:內(nèi)核模塊文件
  • media:掛載點(diǎn)目錄,移動(dòng)設(shè)備
  • /mnt:掛載點(diǎn)目錄,額外的臨時(shí)文件系統(tǒng)
  • /opt:可選目錄,第三方程序的安裝目錄。
  • /proc:偽文件系統(tǒng),內(nèi)核映射文件。
  • /sys:偽文件系統(tǒng),跟硬件設(shè)備相關(guān)的屬性映射文件。
  • /tmp:臨時(shí)文件
  • /var:可變化的文件。
  • /bin:可執(zhí)行文件,用戶命令
  • /sbin:管理命令
  • /usr:shared,read-only

文件命令規(guī)則:

1.長(zhǎng)度不超過(guò)255個(gè)字符
2.不能使用/當(dāng)文件名
3.嚴(yán)格區(qū)分大小寫(xiě)

目錄管理:

  • ls
  • cd
  • pwd
  • mkdir:創(chuàng)建空目錄,/root/x/y/z創(chuàng)建z目錄,只有x/y路徑存在時(shí)才能創(chuàng)建z文件。
mkdir -pv test/m/n/1
$ tree test
test
└── m
    └── n
        └── 1

3 directories, 0 files

$ #創(chuàng)建a_b,a_c,d_b,d_c
$ #思想為:(a+b)(b+c)=ab+ac+db+dc
$ mkdir -pv test/test2/{a,d}_{b,c}
mkdir: created directory 'test/test2'
mkdir: created directory 'test/test2/a_b'
mkdir: created directory 'test/test2/a_c'
mkdir: created directory 'test/test2/d_b'
mkdir: created directory 'test/test2/d_c'

$ mkdir test/test1/{x/m,y} -pv
mkdir: created directory 'test/test1'
mkdir: created directory 'test/test1/x'
mkdir: created directory 'test/test1/x/m'
mkdir: created directory 'test/test1/y'
# { }展開(kāi)作用。
  • rmdir:刪除空文件
$ rmdir test/test1
rmdir: failed to remove 'test/test1': Directory not empty

文件創(chuàng)造

  • touch:可用來(lái)創(chuàng)作空文件
$ touch a
$ file a
a: empty
NAME
       touch - change file timestamps

SYNOPSIS
       touch [OPTION]... FILE...

DESCRIPTION
       Update the access and modification times of each FILE to the current time.

       A  FILE argument that does not exist is created empty, unless -c or -h is sup‐
       plied.

       A FILE argument string of - is handled specially and causes  touch  to  change
       the times of the file associated with standard output.

       Mandatory arguments to long options are mandatory for short options too.

       -a     change only the access time

       -c, --no-create
              do not create any files
$ ls
-aaa-  readme.txt  testtouch  tmp  wy
$ touch a
$ ls 
a  -aaa-  readme.txt  testtouch  tmp  wy
# 出現(xiàn)了a文件
a  -aaa-  readme.txt  testtouch  tmp  wy
$ touch -c c
$ ls
a  -aaa-  readme.txt  testtouch  tmp  wy
# 未出現(xiàn)c文件

touch是用來(lái)改時(shí)間窗的:

$ stat a
  File: 'a'
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: fd71h/64881d    Inode: 27266337    Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1091/     ywu)   Gid: ( 1092/     ywu)
Access: 2018-10-09 20:08:28.189589153 +0800
Modify: 2018-10-09 20:08:28.189589153 +0800
Change: 2018-10-09 20:08:28.189589153 +0800
 Birth: -
$ touch a
$ stat a
  File: 'a'
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: fd71h/64881d    Inode: 27266337    Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1091/     ywu)   Gid: ( 1092/     ywu)
Access: 2018-10-09 20:11:24.229993592 +0800
Modify: 2018-10-09 20:11:24.229993592 +0800
Change: 2018-10-09 20:11:24.229993592 +0800
 Birth: -

創(chuàng)建文件,可以使用文件編輯器:

ASCII:美國(guó)國(guó)家信息交換代碼

  • cp:復(fù)制:
$ cp /etc/passwd /tmp/
# 將passwd復(fù)制到tmp目錄下
$ cp /etc/passwd /tmp/test
# 將passwd復(fù)制到tmp目錄下:如果test不存在,那么passwd將更名為test;
如果test為目錄,那么psswd將放在test目錄下;
如果為文件,則覆蓋test。
$ cp /etc/init.d/ /tmp/
cp: omitting directory '/etc/init.d/'
# cp默認(rèn)情況下是不會(huì)復(fù)制目錄的
# -R, -r, --recursive
              copy directories recursively
  • mv:移動(dòng)文件(原則基本類(lèi)似cp);并更改文件名字
  • install:copy files and set attributes。
    -d:創(chuàng)建目錄/文件;

Linux Day9: cut/sort/uniq/wc/tr/history

  • 目錄管理:
    ls、cd、pwd、mkdir、tree
  • 文件管理:
    touch、stat、file、rm、cp、mv、nano
  • 日期時(shí)間:
    date、clock、hwclock、cal
  • 查看文本:
    cat、more、tac、less、head、tail
  • 文本處理:
    cut、join、sed、awk
    三駕馬車(chē)awk、sed、grep

cut

-d: 指定字段分隔符,默認(rèn)空格
-f: 指定要顯示的字段
> + -f 1,3:顯示1,3字段
> + -f 1-3:顯示1-3字段
  • 做個(gè)小練習(xí)
$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
...

# 如果剪切第一行:
$ cut -d : -f1 /etc/passwd
root
daemon
bin
sys
sync
...

文本排序:sort

  • 參數(shù)及其選項(xiàng)
 不影響源文件排序,只影響顯示排序
 -n:數(shù)值排序
 -r :降序
 -t: 字段分隔符
 -k以那個(gè)字段為關(guān)鍵字進(jìn)行排序
 排序后相同的行只顯示一次
$ sort -t: -k3 /etc/passwd
root:x:0:0:root:/root:/bin/bash
jmzeng:x:1000:1000:,,,:/home/jmzeng:/bin/bash
systemd-timesync:x:100:102:systemd Time Synchronization,,,:/run/systemd:/bin/false
spguo:x:1001:1003:,,,:/home/spguo:/bin/bash
fzhao:x:1002:1004:,,,:/home/fzhao:/bin/bash
zgxu:x:1003:1005:,,,:/home/zgxu:/bin/bash
myshen:x:1004:1006:,,,:/home/myshen:/bin/bash

uniq:report or omit repeated lines

-d:只顯示重復(fù)的行
-c: 顯示文件中行重復(fù)的次數(shù)。

文本統(tǒng)計(jì):wc (word count)

$ wc /etc/fstab
  9  54 541 /etc/fstab
$ wc -l /etc/fstab
9 /etc/fstab
$ wc -w /etc/fstab
54 /etc/fstab

字符處理命令:

tr:轉(zhuǎn)換/刪除字符

tr [OPTION]... SET1 [SET2]

$ tr 'ab' 'AB'
abc
ABc
able
ABle
acd
Acd

`tr 'a-z' 'A-Z' < /etc/passwd```將/etc/passwd文件中的a-z換成A-Z
-d:刪除出現(xiàn)在字符集中的所有字符

$ tr -d 'ab'
helloab
hello
helloba
hello
are
re

LInux Day10: bash特性

bash及其特性:

  • 光標(biāo)跳轉(zhuǎn):
Ctrl+a:跳到命令行首
+e:行尾
+u:刪除光標(biāo)至命令行首的內(nèi)容
+k:刪除光標(biāo)至命令行尾的內(nèi)容
+l:清屏 clear
  • 命令歷史:
查看命令歷史:history
-c : 清空命令歷史
-d OFFSET [n] :刪除指定位置的命令
-w:保存歷史命令至歷史文件中
命令歷史的使用技巧:
!n:執(zhí)行命令歷史中第n條命令
!-n:執(zhí)行命令歷史中倒數(shù)第n條命令
?。。簣?zhí)行上一個(gè)命令
! string:執(zhí)行命令歷史中最近一個(gè)以string開(kāi)頭的命令
! $應(yīng)用上個(gè)命令的最后 一個(gè)參數(shù);ESC, .  及 Alt+.(本地)

Tab

  • 命令補(bǔ)全:Tab(命令搜索路徑下)
  • 路徑補(bǔ)全全:Tab

環(huán)境變量:

PATH:命令搜索路徑
HISTSIZE:命令歷史緩沖區(qū)大小

命令別名:

alias:在shell中定義的別名僅在當(dāng)前shell的生命周期中有效,有效范圍為當(dāng)前shell進(jìn)程;
alias的基本使用方法為:alias COMMANDS=‘COMMANDS [option] [ arguments]’

$ cls
-sh: cls: command not found
$ alias cls=clear
  • 撤銷(xiāo)命令別名,可以使用unalias命令
$ cls
-sh: cls: command not found
$ alias cls=clear
$ unalias cls
$ cls
-sh: cls: command not found

命令替換:

把命令中的某個(gè)子命令替換為執(zhí)行結(jié)果的過(guò)程
格式為:
$(COMMAND)或COMMAND(反引號(hào))
例子1:

$ echo "The current directory is $(pwd)"
The current directory is /umac/ht1T/home//ywu
$ pwd
/umac/ht1T/home//ywu

例2:date +%F命令可以查看今天的日期,我們?nèi)粝虢ㄒ粋€(gè)新文件,以今天的日期命名,則可以使用以下命令:

$ date +%F-%H-%M
2018-10-11-13-12
$ touch ./file-$(date +%F-%H-%M).txt
$ ls
a  -aaa-  file-2018-10-11-13-13.txt  readme.txt  sort.test.save  testtouch  tmp  wy
  • bash支持的引號(hào):
    :命令替換
    " ":弱引用,可以實(shí)現(xiàn)變量替換
    ' ':強(qiáng)引號(hào),不完全變量替換

文件名通配:

用法:

*:匹配任意長(zhǎng)度的任意字符;(可以為零)
?:匹配任意單個(gè)字符;
[]:匹配指定范圍內(nèi)的任意單個(gè)字符;
[^]:匹配指定范圍外的任意單個(gè)字符;
[::]:中括號(hào)和冒號(hào)中間加某些字母,可以表示某個(gè)范圍的字符,外面再加一個(gè)中括號(hào)可表示匹配;
[[:space:]]:空白字符;
[[:punct:]]:標(biāo)點(diǎn)符號(hào);
[[:lower:]]:小寫(xiě)字母;
[[:upper:]]:大寫(xiě)字母;
[[:alpha:]]:大小寫(xiě)字母;
[[:digit:]]:數(shù)字;
[[:alnum:]]:數(shù)字和大小寫(xiě)字母;
[^[:space:]]:取非空白字符;
  • 小練習(xí):
$ touch a123 abc xyz x12 xyz123
$ ls
a123  abc  x12  xyz  xyz123
$ ls a*
a123  abc
$ ls a*3
a123
$ ls *y*
xyz  xyz123
$ touch y123
$ ls *y*
xyz  xyz123  y123
$ ls ?y*
xyz  xyz123
$ ls [a-z]*[0-9]
a123  x12  xyz123  y123
$ ls [^0-9]*
a123  abc  x12  xyz  xyz123  y123

Linux Day11/12:文件權(quán)限

學(xué)習(xí)寄言:
文件名通配,跟正則表達(dá)式的區(qū)別(在后面學(xué)習(xí)了正則表達(dá)式了,一起解決吧)

文件權(quán)限

  • 文件的權(quán)限是文件的一項(xiàng)重要屬性,它可以限定不同用戶對(duì)該文件的操作,
  • 對(duì)于一個(gè)文件來(lái)說(shuō):
r: 可讀;
w:可寫(xiě),可編輯或刪除;
x: 可以執(zhí)行;
  • 而對(duì)于一個(gè)目錄:
r: 可用ls列出目錄內(nèi)所有文件;
w: 可在此目錄中創(chuàng)建文件;
x: 可以使用cd切換進(jìn)此目錄,也可以使用ls -l查看內(nèi)部文件詳細(xì)信息;
文件的權(quán)限也可用八進(jìn)制或二進(jìn)制數(shù)字表示:
  • 八進(jìn)制表示方法:
0 000 ---
1 001 --x
2 010 -w-
3 011 -wx
4 100 r--
5 101 r-x
6 110 rw-
7 111 rwx

#rwx-xr-x-rw-r-----:640
drwxrwxr-x 4 ywu  ywu  4096 Oct  9 20:03 wy
八進(jìn)制表示為:775
7 rwx:ywu
7 rwx:ywu
5 r-x:其他用戶

用戶:UID,/etc/shadow
組:/etc/gshadow
用戶組
用戶類(lèi)別:

  • 管理員:0 (內(nèi)在設(shè)定的)
  • 普通用戶:普通用戶的uid范圍是1-65535,它又可以分為系統(tǒng)用戶和一般用戶。
    1.系統(tǒng)用戶:uid范圍1-499,保障系統(tǒng)的運(yùn)行,一般沒(méi)有登錄密碼。
    2.一般用戶:也就是我們使用賬號(hào)和密碼登入的用戶啦。uid范圍500-60000

文件組的概念

用戶組:
管理員組:
普通組:系統(tǒng)組+一般組

passwd.馬哥
加密方法.馬哥

用戶相關(guān)的兩個(gè)重要文件:

/etc/passwd和/etc/shadow。

這兩個(gè)文件對(duì)整個(gè)系統(tǒng)中的所有用戶和密碼進(jìn)行管理,是linux系統(tǒng)中最重要的文件之一。如果它們出了問(wèn)題,輕則某些用戶無(wú)法登陸,重則整個(gè)系統(tǒng)無(wú)法啟動(dòng)。

/etc/passwd
該文件記錄了系統(tǒng)中所有的用戶信息,由“:”分割成7個(gè)字段。

$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin

每字段的具體含義如下:

  1. account: 用戶名。代表用戶賬號(hào)的字符串,如root, sys等;用來(lái)對(duì)應(yīng) UID 的。例如 root 的 UID 對(duì)應(yīng)就是 0 (第三字段);
  2. password: 密碼。用戶的登錄密碼。Linux中密碼都存放在/etc/shadow中,此處以x代替;
$ cat /etc/shadow
cat: /etc/shadow: Permission denied
# 用root賬戶cat一下
  1. UID: 用戶名ID。通常 Linux 對(duì)于 UID 有幾個(gè)限制需要說(shuō)給您了解一下:


    UID.PNG

    UID 為 0 的時(shí)候,就是 root 呦!所以請(qǐng)?zhí)貏e留意一下你的/etc/passwd 檔案

  2. GID:基本組ID。用戶的基本組,具體的組信息存放在/etc/group文件中; /etc/group的觀念與 /etc/passwd 差多,只是他是用來(lái)規(guī)范組名與 GID的對(duì)應(yīng)而已!
  3. comment: 用戶注釋信息,可以注釋用戶的一些信息如地址電話等,通常為空;
  4. HOME DIR:家目錄。這里可以看到,root的家目錄是/root,普通用戶的家目錄是/home/USER;如果你有個(gè)賬號(hào)的使用空間特刪的大,你想要將該賬號(hào)的家目錄移動(dòng)到其他盤(pán)?可以在這個(gè)字段進(jìn)行修改呦!默認(rèn)的用戶家目錄在
    /home/yourIDname
  5. shell: 用戶的默認(rèn)shell。CentOS發(fā)行版的默認(rèn)shell是bash,此處root和sxy等用戶的默認(rèn)shell是/bin/bash,而系統(tǒng)用戶的是/sbin/nologin,這代表該用戶無(wú)法登錄。

/etc/shadow
普通用戶是沒(méi)有權(quán)限查看該文件的,我們切換到管理員查看該文件的內(nèi)容。

shadow 鳥(niǎo)哥

基本上, shadow 同樣以『:』作為分隔符,如果數(shù)一數(shù),會(huì)發(fā)現(xiàn)共有九個(gè)字段啊,這九個(gè)字段的用途是這樣的:
每個(gè)字段的具體含義如下:

  1. account:用戶名;
  2. encrypted password:加密密碼;
    使用md5加密,分為以$隔開(kāi)的3個(gè)字段,第2個(gè)字段為雜質(zhì),在加密前將雜質(zhì)加入密碼中,之后再進(jìn)行加密,第3個(gè)字段為加密后的md5碼;
    md5是常用的單向加密方式,可以由明文取得密文,但反之不行,可以使用md5sum獲取md5碼。
  3. 上次更改密碼的時(shí)間:在linux系統(tǒng)中經(jīng)常會(huì)用到linux時(shí)間,即距離1970年1月1日0時(shí)0分0秒的時(shí)間。這個(gè)字段的數(shù)字就是上次修改密碼的時(shí)間距離1970年1月1日的總天數(shù)。
  4. 修改密碼后再次修改密碼需要經(jīng)過(guò)多少天:默認(rèn)是0,即不需等待,可連續(xù)修改密碼;
  5. 密碼的有效期:即密碼修改后在多少天內(nèi)有效,超過(guò)一定時(shí)間后必須重新設(shè)置密碼。默認(rèn)99999,可以認(rèn)為永不到期,嗯,除非你有意把密碼世代相傳;
  6. 密碼到期前警告:此處為7,即密碼到期7天前,系統(tǒng)會(huì)給予警告;
  7. 賬號(hào)失效期限:假如此處為7,則密碼到期7天后用戶還未修改密碼則會(huì)被鎖定。我們的系統(tǒng)中此處為空;
  8. 賬號(hào)的有效期限:同樣按照距離1970年1月1日的天數(shù)計(jì)算。在此天數(shù)內(nèi)賬號(hào)有效,超過(guò)日期則失效;
  9. 保留用,無(wú)意義。

/etc/group

dbjiang:x:1086:
ztsong:x:1087:
sstian:x:1088:
hhwang:x:1089:
yxliu:x:1090:
zwbao:x:1091:
ywu:x:1092:

這個(gè)檔案每一行代表一個(gè)群組,也是以冒號(hào)『:』作為字段的分隔符,共分為四欄,每一字段的意義是:(參考鳥(niǎo)哥)

  1. 組名:
    就是組名啦!
  2. 群組密碼:
    通常不需要訓(xùn)定,這個(gè)訓(xùn)定通常是給『群組管理員』使用, 同樣的,密碼已經(jīng)秱勱刡 /etc/gshadow 去,因此這個(gè)字段叧會(huì)存在一個(gè)『x』而已;
  3. GID:
    就是群組的 ID 啊。我們 /etc/passwd 第四個(gè)字段使用的 GID 對(duì)應(yīng)的群組名,就是由這里對(duì)應(yīng)出來(lái)的!
  4. 此群組支持癿賬號(hào)名稱(chēng):
    我們知道一個(gè)賬號(hào)可以加入多個(gè)群組,那某個(gè)賬號(hào)想要加入此群組時(shí),將該賬號(hào)填入這個(gè)字段即可。 如果我想要讓 dmtsai 也加入 root 這個(gè)群組,那么在第一行的最后面加上
    『,dmtsai』,注意不要有空格, 使成為『root:x:0:root,dmtsai 』就可以啰~

Linux Day13:useradd及相關(guān)

  • 新增與移除使用者: useradd, 相關(guān)配置文件, passwd, usermod, userdel

useraddd

  • useradd [-u UID] [-g 初始群組] [-G 次要群組] [-mM] [-c 說(shuō)明欄] [-d 家目錄絕對(duì)路徑] [-s shell] 使用者賬號(hào)名


    useradd選項(xiàng)
  • 使用『useradd 賬號(hào) 』來(lái)建立使用者即可。 CentOS 這些默認(rèn)值主要會(huì)幫我們處理幾個(gè)項(xiàng)目:
    1.在 /etc/passwd 里面建立一行與賬號(hào)相關(guān)的數(shù)據(jù),包括建立 UID/GID/家目錄等;
    2.在 /etc/shadow 里面將此賬號(hào)的密碼相關(guān)參數(shù)填入,但是尚未有密碼;
    3.在 /etc/group 里面加入一個(gè)不賬號(hào)名稱(chēng)一模一樣的組名;
    4.在 /home 底下建立一個(gè)不賬號(hào)同名的目錄作為用戶家目錄,且權(quán)限為 700
# 創(chuàng)建user1用戶
root@VM-0-3-ubuntu:~# useradd user1
root@VM-0-3-ubuntu:~# tail -1 /etc/passwd
user1:x:1000:1000::/home/user1:
root@VM-0-3-ubuntu:~# #在創(chuàng)建用戶的時(shí)候,沒(méi)有為用戶指定組,將創(chuàng)造同用戶相同UID的組。
# 如果兩者不一樣,則會(huì)出現(xiàn)特殊用戶。
#創(chuàng)建新用戶并加入到mygroup中
root@VM-0-3-ubuntu:~# useradd -g mygroup user2
useradd: group 'mygroup' does not exist
# 說(shuō)名這個(gè)組要事先存在才行
# 將user2加入到剛剛創(chuàng)建的user1中去
root@VM-0-3-ubuntu:~# useradd -g user1 user2
root@VM-0-3-ubuntu:~# tail -1 /etc/passwd
user2:x:1001:1000::/home/user2:
root@VM-0-3-ubuntu:~# 其實(shí),這個(gè)user1的UID為1000,在創(chuàng)建新用戶的時(shí)候,系統(tǒng)默認(rèn)根據(jù)/etc/passwd的最后一位UID自動(dòng)創(chuàng)建下一位,就是這里的1001.
# 創(chuàng)建user3,并制定其附加組為user1
root@VM-0-3-ubuntu:~# tail /etc/group
lxd:x:110:
messagebus:x:111:
uuidd:x:112:
mlocate:x:113:
ssh:x:114:
ubuntu:x:500:
lpadmin:x:115:ubuntu
sambashare:x:116:ubuntu
ntp:x:117:
user1:x:1000:
root@VM-0-3-ubuntu:~# useradd -G user1 user3
root@VM-0-3-ubuntu:~# tail /etc/group
messagebus:x:111:
uuidd:x:112:
mlocate:x:113:
ssh:x:114:
ubuntu:x:500:
lpadmin:x:115:ubuntu
sambashare:x:116:ubuntu
ntp:x:117:
user1:x:1000:user3
user3:x:1002:
# 指定名稱(chēng)和家目錄
root@VM-0-3-ubuntu:~# useradd -c "wuyi" -d /home/wuyi user5
root@VM-0-3-ubuntu:~# tail -1 /etc/passwd
user5:x:1004:1004:wuyi:/home/wuyi:
# 默認(rèn)情況下創(chuàng)建的用戶:
root@VM-0-3-ubuntu:~# echo $SHELL
/bin/bash

userdel:刪除用戶

  • 用法:userdel [option] USERNAME目的在刪除用戶的相關(guān)數(shù)據(jù),而用戶的數(shù)據(jù)有:
    ?1. 用戶賬號(hào)/密碼相關(guān)參數(shù):/etc/passwd, /etc/shadow
    ? 2.使用者群組相關(guān)參數(shù):/etc/group, /etc/gshadow
    ? 3.用戶個(gè)人檔案數(shù)據(jù): /home/username, /var/spool/mail/username
#刪除用戶user4,默認(rèn)情況下不會(huì)刪除其家目錄,要想刪除-r
root@VM-0-3-ubuntu:~# userdel user4
# 刪除user5及其家目錄
root@VM-0-3-ubuntu:~# userdel -r user5
userdel: user5 mail spool (/var/mail/user5) not found
userdel: user5 home directory (/home/wuyi) not found
# user5創(chuàng)建了家目錄的,為什么提示找不到?

id: 查看用戶賬號(hào)的屬性

SYNOPSIS
       id [OPTION]... [USER]

DESCRIPTION
       Print  user  and group information for the specified USER, or (when USER omit‐
       ted) for the current user.

       -a     ignore, for compatibility with other versions

       -Z, --context
              print only the security context of the process

       -g, --group
              print only the effective group ID

       -G, --groups
              print all group IDs

       -n, --name
  • 上面幾個(gè)option都可以與-n一起用
$ #查看當(dāng)前用戶的id信息
$ id
uid=1091(ywu) gid=1092(ywu) groups=1092(ywu),1019(student)
$ #id user1查看user1的賬戶信息
$ id user1
id: ‘user1’: no such user
$ id -n
id: cannot print only names or real IDs in default format
$ id -u -n ywu
ywu
$ id -G -n ywu
ywu student

finger 查看用戶賬戶信息

  • finger 的中文字面意義是:『手指』戒者是『挃紋』的意思。這個(gè) finger 可以查閱很多用戶相關(guān)的信息喔! 大部分都是在 /etc/passwd 這個(gè)檔案里面的信息啦
root@VM-0-3-ubuntu:~# finger user1
Login: user1                    Name: 
Directory: /home/user1                  Shell: /bin/sh
Never logged in.
No mail.
No Plan.

修改賬戶信息:

usermod:

  • 選項(xiàng):


    uermod.PNG

chsh:修改用戶的默認(rèn)的shell

root@VM-0-3-ubuntu:~# chsh user2
Changing the login shell for user2
Enter the new value, or press ENTER for the default
    Login Shell []: /bin/tcsh
chsh: Warning: /bin/tcsh does not exist
root@VM-0-3-ubuntu:~# chsh user2
Changing the login shell for user2
Enter the new value, or press ENTER for the default
    Login Shell [/bin/tcsh]: /bin/bash
root@VM-0-3-ubuntu:~# finger user2
Login: user2                    Name: 
Directory: /home/user2                  Shell: /bin/bash
Never logged in.
No mail.
No Plan.

chfn:修改注釋信息

root@VM-0-3-ubuntu:~# chfn user2
Changing the user information for user2
Enter the new value, or press ENTER for the default
    Full Name []: wuyi^H^H^H^H
    Room Number []: chongqing
    Work Phone []: 5^H023
    Home Phone []: 15870000000
    Other []: nothing
chfn: name with non-ASCII characters: ''uyi
chfn: invalid work phone: '023'

密碼管理:

passwd:

root@VM-0-3-ubuntu:~# tail /etc/shadow
user2:!:17825:0:99999:7:::

root@VM-0-3-ubuntu:~# passwd user2
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
root@VM-0-3-ubuntu:~# tail /etc/shadow
user2:$6$TyWhVhuR$wDzvvJx8GlArSe9OigIgdNejH3M.dYsly79ebgtuLqsK9a2KSTBVb4rTvMydrnF.eWX0np7xP2zsLbdT45C4Z.:17825:0:99999:7:::

pwck:檢查用戶賬號(hào)的完整性

root@VM-0-3-ubuntu:~# pwck
user 'lp': directory '/var/spool/lpd' does not exist
user 'news': directory '/var/spool/news' does not exist
user 'uucp': directory '/var/spool/uucp' does not exist
user 'www-data': directory '/var/www' does not exist
user 'list': directory '/var/list' does not exist
user 'irc': directory '/var/run/ircd' does not exist
user 'gnats': directory '/var/lib/gnats' does not exist
user 'nobody': directory '/nonexistent' does not exist
user 'systemd-resolve': directory '/run/systemd/resolve' does not exist
user 'syslog': directory '/home/syslog' does not exist
user '_apt': directory '/nonexistent' does not exist
user 'ntp': directory '/home/ntp' does not exist
user 'user1': directory '/home/user1' does not exist
user 'user2': directory '/home/user2' does not exist
user 'user3': directory '/home/user3' does not exist
pwck: no changes

chage:

-d :最近一次的修改時(shí)間
-E:過(guò)期時(shí)間
-I:非活動(dòng)時(shí)間
-m:最短使用期限
-M: 最長(zhǎng)使用權(quán)限
-W: 警告時(shí)間

組管理:

創(chuàng)建組:groupadd:

root@VM-0-3-ubuntu:~# groupadd -r wechat
root@VM-0-3-ubuntu:~# tail -1 /etc/group
wechat:x:999:
# -r, --system
           Create a system group.
# 按照馬哥來(lái)說(shuō)的話是<500,為什么這里為999?
  • groupmod:修改

  • groupdel:刪除

  • gpasswd:給組加密碼

newgrp:切換到新的組

root@VM-0-3-ubuntu:~# useradd hadoop
root@VM-0-3-ubuntu:~# su - hadoop
No directory, logging in with HOME=/
$ cd /tmp
$ touch a.hadoop
$ ls -l
total 12
-rw-rw-r-- 1 hadoop hadoop   0 Oct 21 23:12 a.hadoop
-rw-r--r-- 1 root   root   140 Oct 21 12:30 net_affinity.log
-rw-r--r-- 1 root   root    26 Oct 21 12:30 nv_gpu_conf.log
-rw-r--r-- 1 root   root   191 Oct 21 12:30 setRps.log
$ #一個(gè)用戶創(chuàng)建的文件屬于用戶的基本組

$ id
uid=1003(hadoop) gid=1003(hadoop) groups=1003(hadoop)
$ #將一個(gè)文件的基本組切換到其他組去
$ newgrp user1
Password: 
# 如果想要回到原來(lái)的組,則使用exit

馬哥作業(yè).PNG

Linux Day14:chgrp/chown/chmod

權(quán)限管理:

復(fù)習(xí):

 [ r ]代表可讀(read)
 [ w ]代表可寫(xiě)(write)
 [ x ]代表可執(zhí)行(execute)
  • 三類(lèi)用戶
  1. u:屬主
  2. g:屬組
  3. o:其它用戶
  • 第一組為『檔案擁有者的權(quán)限』,以『install.log』那個(gè)檔案為例, 該檔案的擁有者可以讀寫(xiě),但不可執(zhí)行;
  • 第二組為『同群組的權(quán)限』;
  • 第三組為『其他非本群組的權(quán)限』。

  • 我們現(xiàn)在知道檔案權(quán)限對(duì)于一個(gè)系統(tǒng)的安全重要悵了,也知道檔案的權(quán)限對(duì)于使用者不群組的相關(guān)性,那么如何修改一個(gè)檔案的屬悵與權(quán)限呢?又!有多少檔案的權(quán)限我們可以修改呢? 其實(shí)一個(gè)檔案的屬悵與權(quán)限有很多!我們先介紹幾個(gè)常用于群組、擁有者、各種身份的權(quán)限的修改的挃令,如下所示:
    ? chgrp :改變檔案所屬群組
    ? chown :改變檔案擁有者
    ? chmod :改變檔案的權(quán)限, SUID, SGID, SBIT 等等的特性

chown:改變文件屬主(只有管理員可以使用此命令)

SYNOPSIS
       chown [OPTION]... [OWNER][:[GROUP]] FILE...
       chown [OPTION]... --reference=RFILE FILE...

root@VM-0-3-ubuntu:~# ls /tmp -l
total 12
-rw-rw-r-- 1 hadoop hadoop   0 Oct 21 23:12 a.hadoop
-rw-r--r-- 1 root   root   140 Oct 21 12:30 net_affinity.log
-rw-r--r-- 1 root   root    26 Oct 21 12:30 nv_gpu_conf.log
-rw-r--r-- 1 root   root   191 Oct 21 12:30 setRps.log
# 修改文件a.hadoop的屬主為root
root@VM-0-3-ubuntu:~# chown root /tmp/a.hadoop
root@VM-0-3-ubuntu:~# ls -l /tmp/a.hadoop
-rw-rw-r-- 1 root hadoop 0 Oct 21 23:12 /tmp/a.hadoop
# -R:修改目錄及其內(nèi)部文件的屬性
# 通常改變目錄的屬主,但是其中的文件屬主并沒(méi)有改變,可以-R進(jìn)行修改

# --reference=/path/to/somefile file ,...(屬主,屬組一起改的)
ubuntu@VM-0-3-ubuntu:~$ ls -l /tmp
total 12
-rw-rw-r-- 1 root hadoop   0 Oct 21 23:12 a.hadoop
-rw-r--r-- 1 root root   140 Oct 21 12:30 net_affinity.log
-rw-r--r-- 1 root root    26 Oct 21 12:30 nv_gpu_conf.log
-rw-r--r-- 1 root root   191 Oct 21 12:30 setRps.log
root@VM-0-3-ubuntu:~# chown --reference=/tmp/a.hadoop /tmp/setRps.log
root@VM-0-3-ubuntu:~# ls -l /tmp
total 12
-rw-rw-r-- 1 root hadoop   0 Oct 21 23:12 a.hadoop
-rw-r--r-- 1 root root   140 Oct 21 12:30 net_affinity.log
-rw-r--r-- 1 root root    26 Oct 21 12:30 nv_gpu_conf.log
-rw-r--r-- 1 root hadoop 191 Oct 21 12:30 setRps.log
# chown USRNAME(屬主):GRPNAME(屬組) file,...
root@VM-0-3-ubuntu:~# chown hadoop.hadoop /tmp/a.hadoop
root@VM-0-3-ubuntu:~# chown hadoop:hadoop /tmp/a.hadoop
root@VM-0-3-ubuntu:~#ls -l /tmp/a.hadoop
-rw-rw-r-- 1 hadoop hadoop   0 Oct 21 23:12 a.hadoop
`chown :GRPNAME file,...`(只改變屬組)
`chown USRNAME.GRPNAME file,...`(也是同時(shí)改變屬主、屬組,但是在不同的文件中有不同的用法)
chown --reference=/tmp/abc  /tmp/test
# 表示將/tmp/test的屬主和屬組改成和/tmp/abc/一樣

chgrp改變文件所屬組

-R:遞歸修改文件內(nèi)的屬組
-- reference=path to /somefile

# 表示修改a.hadoop的屬組為root
root@VM-0-3-ubuntu:~# ls -l /tmp/a.hadoop
-rw-rw-r-- 1 root hadoop   0 Oct 21 23:12 a.hadoop
root@VM-0-3-ubuntu:~# chgrp root /tmp/a.hadoop
root@VM-0-3-ubuntu:~#ls -l /tmp/a.hadoop

chomd;修改文件權(quán)限

  • 修改三類(lèi)用戶的權(quán)限
    chmod MODE file,...
    -R:
    --reference=
  • Linux 檔案的基本權(quán)限就有九個(gè),分別是 owner/group/others 三種身份各有自己的read/write/execute 權(quán)限, 先復(fù)習(xí)一下剛剛上面提到的數(shù)據(jù):檔案的權(quán)限字符為:『-rwxrwxrwx』, 這九個(gè)權(quán)限是三個(gè)三個(gè)一組的!其中,我們可以使用數(shù)字來(lái)代表各個(gè)權(quán)限,各
    權(quán)限的分?jǐn)?shù)對(duì)照表如下:
r:4
w:2
x:1
  • 每種身份(owner/group/others)各自的三個(gè)權(quán)限(r/w/x)分?jǐn)?shù)是需要累加的,例如當(dāng)權(quán)限為: [-rwxrwx---] 分?jǐn)?shù)則是:
owner = rwx = 4+2+1 = 7
group = rwx = 4+2+1 = 7
others= --- = 0+0+0 = 0
root@VM-0-3-ubuntu:~# ls -l /tmp/a.hadoop
-rw-rw-r-- 1 hadoop hadoop   0 Oct 21 23:12 a.hadoop

# 修改文件a.hadoop的權(quán)限為750
root@VM-0-3-ubuntu:~# chmod 750 /tmp/a.hadoop
root@VM-0-3-ubuntu:~# ls -l /tmp/a.hadoop
-rwxr-x--- 1 hadoop hadoop   0 Oct 21 23:12 a.hadoop

# 考慮chmod 75 /tmp/a.hadoop
root@VM-0-3-ubuntu:~# chmod 75 /tmp/a.hadoop
root@VM-0-3-ubuntu:~#ls -l /tmp/a.hadoop
----rwxr-x 1 hadoop hadoop   0 Oct 21 23:12 a.hadoop
  • 還有一個(gè)改變權(quán)限的方法呦!仍以前的介紹中我們可以發(fā)現(xiàn),基本上就九個(gè)權(quán)限分別是(1)user(2)group (3)others 三種身份啦!那舉我們就可以藉由 u, g, o 來(lái)代表三種身份的權(quán)限!此外,a 則代表 all 亦即全部的身份!那舉讀寫(xiě)的權(quán)限就可以寫(xiě)成 r, w, x 啰!也就是可以使用底下的方式來(lái)看:


    鳥(niǎo)哥
  • 修改某類(lèi)用戶的權(quán)限:
root@VM-0-3-ubuntu:~#ls -l /tmp/a.hadoop
----rwxr-x 1 hadoop hadoop   0 Oct 21 23:12 a.hadoop
root@VM-0-3-ubuntu:~# chmod u=rwx /tmp/a.hadoop
root@VM-0-3-ubuntu:~#ls -l /tmp/a.hadoop
-rwxrwxr-x 1 hadoop hadoop   0 Oct 21 23:12 a.hadoop

root@VM-0-3-ubuntu:~# chmod go=rx /tmp/a.hadoop
root@VM-0-3-ubuntu:~# ls -l /tmp/a.hadoop
-rwxr-xr-x 1 hadoop hadoop 0 Oct 21 23:12 /tmp/a.hadoop
root@VM-0-3-ubuntu:~# chmod g=r,o=rx /tmp/a.hadoop
root@VM-0-3-ubuntu:~# ls -l /tmp/a.hadoop
-rwxr--r-x 1 hadoop hadoop 0 Oct 21 23:12 /tmp/a.hadoop
  • 修改某類(lèi)用戶的某位或某些位權(quán)限
root@VM-0-3-ubuntu:~# ls -l /tmp/a.hadoop
-rwxr--r-x 1 hadoop hadoop 0 Oct 21 23:12 /tmp/a.hadoop
root@VM-0-3-ubuntu:~# chmod u-x,g+x /tmp/a.hadoop
root@VM-0-3-ubuntu:~# ls -l /tmp/a.hadoop
-rw-r-xr-x 1 hadoop hadoop 0 Oct 21 23:12 /tmp/a.hadoop

練習(xí):

$ ls -l xyz
-rw-rw-r-- 1 ywu ywu 0 Oct 11 13:23 xyz
$ chmod u=rwx xyz
$ ls -l xyz
-rwxrw-r-- 1 ywu ywu 0 Oct 11 13:23 xyz
$ chmod u=rx xyz
$ ls -l xyz
-r-xrw-r-- 1 ywu ywu 0 Oct 11 13:23 xyz
$ chmod 664 xyz
$ ls -l xyz
-rw-rw-r-- 1 ywu ywu 0 Oct 11 13:23 xyz
$ chmod g=rwx xyz
$ ls -l xyz
-rw-rwxr-- 1 ywu ywu 0 Oct 11 13:23 xyz
$ chmod g=rwx,o=w xyz
$ ls -l xyz
-rw-rwx-w- 1 ywu ywu 0 Oct 11 13:23 xyz
$ chmod -w xyz
chmod: xyz: new permissions are r--r-x-w-, not r--r-x---
# 這個(gè)沒(méi)有明白?not r--r-x---
捕獲.PNG
root@VM-0-3-ubuntu:~# useradd -M openstack
root@VM-0-3-ubuntu:~# finger openstack
Login: openstack                Name: 
Directory: /home/openstack              Shell: /bin/sh
Never logged in.
No mail.
No Plan.
root@VM-0-3-ubuntu:~# ls /home
jmz  ubuntu
root@VM-0-3-ubuntu:~# id openstack
uid=1006(openstack) gid=1006(openstack) groups=1006(openstack)
root@VM-0-3-ubuntu:~# cp -r /etc/skel /home/openstack
root@VM-0-3-ubuntu:~# ls -l /home
total 12
drwxr-xr-x 3 jmz    jmz    4096 Oct 22 23:14 jmz
drwxr-xr-x 2 root   root   4096 Oct 23 21:08 openstack
drwxr-xr-x 5 ubuntu ubuntu 4096 Oct 21 12:23 ubuntu
root@VM-0-3-ubuntu:~# ls -l /home/openstack -a
total 20
drwxr-xr-x 2 root root 4096 Oct 23 21:08 .
drwxr-xr-x 5 root root 4096 Oct 23 21:08 ..
-rw-r--r-- 1 root root  220 Oct 23 21:08 .bash_logout
-rw-r--r-- 1 root root 3771 Oct 23 21:08 .bashrc
-rw-r--r-- 1 root root  655 Oct 23 21:08 .profile
root@VM-0-3-ubuntu:~# chown -R openstack.openstack /home/openstack
root@VM-0-3-ubuntu:~# ls -ld /home/openstack
drwxr-xr-x 2 openstack openstack 4096 Oct 23 21:08 /home/openstack
root@VM-0-3-ubuntu:~# ls -la /home/openstack
total 20
drwxr-xr-x 2 openstack openstack 4096 Oct 23 21:08 .
drwxr-xr-x 5 root      root      4096 Oct 23 21:08 ..
-rw-r--r-- 1 openstack openstack  220 Oct 23 21:08 .bash_logout
-rw-r--r-- 1 openstack openstack 3771 Oct 23 21:08 .bashrc
-rw-r--r-- 1 openstack openstack  655 Oct 23 21:08 .profile

# 修改為組即其它用戶不能訪問(wèn)/home/openstack/
root@VM-0-3-ubuntu:~# chmod -R go= /home/openstack/
root@VM-0-3-ubuntu:~# ls -la /home/openstack/
total 20
drwx------ 2 openstack openstack 4096 Oct 23 21:08 .
drwxr-xr-x 5 root      root      4096 Oct 23 21:08 ..
-rw------- 1 openstack openstack  220 Oct 23 21:08 .bash_logout
-rw------- 1 openstack openstack 3771 Oct 23 21:08 .bashrc
-rw------- 1 openstack openstack  655 Oct 23 21:08 .profile
root@VM-0-3-ubuntu:~# su - openstack
openstack@VM-0-3-ubuntu:~$ 
  • 手動(dòng)添加用戶hive,基本組為hive(5000),附加組為mygroup

生信技能樹(shù)公益視頻合輯:學(xué)習(xí)順序是linux,r,軟件安裝,geo,小技巧,ngs組學(xué)!
請(qǐng)猛戳下面鏈接
B站鏈接:https://m.bilibili.com/space/338686099

YouTube鏈接:https://m.youtube.com/channel/UC67sImqK7V8tSWHMG8azIVA/playlists

生信工程師入門(mén)最佳指南:https://mp.weixin.qq.com/s/vaX4ttaLIa19MefD86WfUA

學(xué)徒培養(yǎng):https://mp.weixin.qq.com/s/3jw3_PgZXYd7FomxEMxFmw

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容