1. 程式人生 > >Linux學習之基礎篇-2

Linux學習之基礎篇-2

1、Linux上檔案管理類命令及其常用方法和相關例項

  • ls命令:- list directory contents

    ls命令用於顯示目錄內容,類似DOS下的dir命令,它的使用許可權是所有使用者。

    1、格式:ls [OPTION]… [FILE]…

    2、常用option和示例

    ls -a :顯示所有檔案,包括隱藏檔案
    ls -a /path:顯示指定路徑下的所有檔案
    例:

    [[email protected] /]# ls -a /bin
    .    dbus-send    grep    mkdir    rm         tracepath6
    ..   dbus-uuidgen gtar    mknod    rmdir      traceroute
    

    ls -A:顯示除了 · 和 ·· 之外的所有檔案
    例:

    [[email protected] /]# ls -A /bin
    alsaunmute     dnsdomainname  kbd_mode    ping      sync
    arch           domainname     keyctl      ping6     tar
    

    ls -l:列出檔案的詳細資訊
    例:

    [[email protected] /]# ls -l /bin
    total 7888
    -rwxr-xr-x. 1 root root    123 Jan 26  2015 alsaunmute
    -rwxr-xr-x. 1 root root  27776 Oct 15  2014 arch
    lrwxrwxrwx. 1 root root      4 Oct  8 04:03 awk -> gawk
    

    ls -h:將檔案大小轉換為易識別單位(K,M,G),轉換後為非準確數
    例:

    [[email protected] recover]# ls -hl
    total 8.0K
    drwxr-xr-x. 2 root root 4.0K Nov 24 06:00 temp
    drwxr-xr-x. 2 root root 4.0K Nov 24 06:00 test
    

    ls -ld:檢視目錄自身屬性,並非目錄下檔案屬性
    例:

    [[email protected] /]# ls -dl /bin
    lrwxrwxrwx. 1 root root 7 Oct 11 08:36 /bin -> usr/bin
    

    ls -d

    :將目錄像檔案一樣顯示,而不是顯示其下的檔案
    例:

    [[email protected] /]# ls -d /bin
    /bin
    

    ls -r:對目錄反向排序
    例:

    [[email protected] /]# ls /bin
    gst-typefind-0.10                    ps2pdfwr                     znew
    gst-typefind-1.0                     ps2ps                        zsoelim
    [[email protected] /]# ls -r /bin
    zsoelim                         ps2ps                        gst-typefind-1.0
    znew                            ps2pdfwr                     gst-typefind-0.10
    

    ls -R:列出所有子目錄下的檔案
    例:

    [[email protected] /]# ls -R /bin
    /bin:
    [                                    gst-xmlinspect               ps2ps2
    a2p                                  gst-xmlinspect-0.10          psed
    
  • cat 命令:- concatenate files and print on the standard output

    cat命令用於將[檔案]或標準輸入組合輸出到標準輸出。

    1、格式:cat [OPTION]… [FILE]…

    2、常用option和示例
    cat -b(–number):對非空輸出行編號
    例:

    [[email protected] test]# cat -b cattest.txt
      1	ferst line:cat - concatenate files and print on the standard ouput
      2	second line:cat option rile 
      3	-A equivalent to 
      4	-b number nonempty output lines ,overrides 
    

    cat -E(- -show-ends):在每行結束處顯示$
    例:

    [[email protected] test]# cat -E cattest.txt
    ferst line:cat - concatenate files and print on the standard ouput$
    second line:cat option rile $
    -A equivalent to $
    -b number nonempty output lines ,overrides $
    $
    

    cat -n(- -number-nonblank):對輸出的所有行進行編號
    例:

    [[email protected] test]# cat -n cattest.txt
     1	ferst line:cat - concatenate files and print on the standard ouput
     2	second line:cat option rile  
     3	-A equivalent to 
     4	-b number nonempty output lines ,overrides  
     5	
    
  • rm命令:- remove files or directories

    rm命令用於刪除指定檔案

    1、格式:rm [OPTION]… FILE…

    2、常用option和示例
    rm -r(-R,- -recursive):遞迴刪除目錄以及內容。
    例:

    [[email protected] tmp]# rm -r ssh-rUyqoLrCjwNf
    rm: descend into directory ‘ssh-rUyqoLrCjwNf’? y
    rm: remove socket ‘ssh-rUyqoLrCjwNf/agent.2898’? y
    rm: remove directory ‘ssh-rUyqoLrCjwNf’? y
    

    rm -v(- -verbose):詳細顯示進行的步驟。
    例:

    [[email protected] tmp]# rm -rv tracker-extract-files.0
    rm: remove directory ‘tracker-extract-files.0’? y
    removed directory: ‘tracker-extract-files.0’
    

    rm -f(- -force):略過不存在的檔案,不提示。
    例:

    [[email protected] tmp]# rm -rfv systemd-private-79decba513f549008b76c93fa5a90bbf-colord.service-3FbKkY
    removed directory: ‘systemd-private-79decba513f549008b76c93fa5a90bbf-colord.service-3FbKkY/tmp’
    removed directory: ‘systemd-private-79decba513f549008b76c93fa5a90bbf-colord.service-3FbKkY’
    

    rm -i(- -interactive):進行任何刪除操作前必須先確認。
    例:

    [[email protected] tmp]# rm -i /recover/temp/tmp/test.txt
    rm: remove regular empty file ‘/recover/temp/tmp/test.txt’? 
    

    rm -d(- -directory):刪除<檔案>,即使該檔案可能是非空目錄(只有超級使用者,僅當系統支援“unlink”非空目錄時才有效)
    例:

    [[email protected] temp]# rm -d test
    rm: remove directory ‘test’? y
    
  • less命令:

    less命令的功能和more命令一樣,也是用來按頁顯示檔案,不同之處在於less命令顯示檔案時,使用者既可以向前又可以向後翻閱檔案(其實man命令就是呼叫的less命令進行檢視手冊)

    1、格式:less [OPTION] FILE

    2、常用option和示例
    less -c:從頂部(由上到下)重新整理螢幕,並顯示檔案內容,而不是通過底部滾動完成重新整理。
    less -f:強制開啟檔案,二進位制檔案顯示時,不提示警告。
    例:

       [[email protected] test]# less -f /bin/ls
      ^?	ELF^B^A^A^@^@^@^@^@^@^@^@^@^B^@>	^@^A^@^@^@[email protected]^@^@^@^@^@@^@^@^	@^@^@^@^@<F0>	<C3>^A^@^@^@^@^@^@^@^@^@@
        ^@8^@      	^@@^@^^^@^]^@^F^@^@^@^E^@^@^@@^	@^@^@^@^@^@^@@^@@^@^@^@^@^@@^	@@^@^@^@^@^@<F8>^A^@^@^@^@^@^@
    

    less -i:搜尋時忽略大小寫,除非搜尋串中包含大寫字母。
    less -I:搜尋時忽略大小寫,除非搜尋串中包含小寫字母。

    less -m:顯示讀取檔案的百分比。
    less -M:顯示讀取檔案百分比、行號及總行數。
    例:

    [[email protected] test]# less -m cattest.txt
    -ppattern or --pattern=patterncattest.txt 14%
    
       [[email protected] test]# less -M cattest.txt
    -ppattern or --pattern=patterncattest.txt lines 1-41/246 14%
    

    less -N:在每行前輸出行號。
    例:

    [[email protected] test]# less -N cattest.txt
      1 ferst line:cat - concatenate files and print on the standard ouput
      2 second line:cat option rile 
      3 -A equivalent to 
      4 -b number nonempty output lines ,overrides 
    

    less -p pattern:搜尋pattern,比如在/etc/profile下搜尋MAIT,就用less -p MAIT /etc/profile。
    例:

    [[email protected] test]# less -p ferst ./cattest.txt
    ferst line:cat - concatenate files and print on the standard ouput
    second line:cat option rile 
    

    less -s:把連續多個空白行作為一個空白行顯示

  • cp 命令:- copy files and directories

    cp命令用於複製檔案或目錄。

    1、格式:
    cp [OPTION]… [-T] SOURCE DEST
    (1)、若目標檔案-DEST-不存在;則事先建立此原始檔,並複製原始檔資料流至此目標檔案中
    (2)、若目標檔案存在

    • 若目標檔案是非目錄檔案:則複製原始檔資料流,覆蓋目標檔案
    • 若目標檔案時目錄:則先在目標檔案下建立一個與原始檔名相同的檔案,並將原始檔資料複製到目標檔案中。

    cp [OPTION]… SOURCE… DIRECTORY
    cp [OPTION]… -t DIRECTORY SOURCE…
    (1)、若目標檔案不存在:則報錯。
    (2)、 複製時需要加上-r選項。
    (3)、若目標檔案存在

    • 若目標檔案時非目錄檔案:則報錯。
    • 若目標檔案是目錄:分別複製每個檔案至目標檔案中,並保持原名。
      將<源>檔案複製到<目錄>,或將多個<源>複製到<目錄>。

    2、常用option和示例
    cp -i:人機互動模式,顯示覆蓋已存在的檔案或目錄的警告資訊。
    例:

    [[email protected] recover]# cp testcp ./cptest
    cp: overwrite ‘./cptest/testcp’? y
    

    cp -f:刪除已經存在的目標檔案(強制覆蓋目標檔案)
    例:

    [[email protected] recover]# cp testcp ./cptest
    cp: overwrite ‘./cptest/testcp’? y
    

    cp -r:遞迴複製,複製目錄時包括此目錄下的子目錄和檔案

    [[email protected] cptest]# cp -r /etc ./
    [[email protected] cptest]# ls
    etc  testcp
    

    cp -d:複製符號連結檔案本身,而非其指向的原始檔,保持原始檔和目標檔案之間的硬連結關係。

    [[email protected] recover]# cp -d /etc/system-release ./temp
    lrwxrwxrwx.   1 root root    14 Nov 24 23:11 system-release -> centos-release
    

    cp -a:等同於-dpR引數同時使用,在複製過程中儘量可能保留檔案或目錄的屬性。

    [[email protected] recover]# cp -a ./test/test.txt ./temp
    [[email protected] recover]# ll ./test
    total 4
    -rw-r--r--. 1 root root 76 Nov 24 22:58 test.txt
    [[email protected] recover]# ll ./temp/test.txt
    -rw-r--r--. 1 root root 76 Nov 24 23:14 ./temp/test.txt
    

    cp -b:給被覆蓋的檔案做備份。如將file1複製到file2時,若file2存在就將其覆蓋掉同時生成一個名為file2~的檔案。

    [[email protected] recover]# cp -b ./test/test.txt ./temp
    cp: overwrite ‘./temp/test.txt’? y
    [[email protected] recover]# ls ./temp
    test.txt  test.txt~  
    

    cp -v:在複製前,輸出每個檔案的名字

    [[email protected] recover]# cp -rv /home ./cptest
    ‘/home’ -> ‘./cptest/home’
    ‘/home/hu’ -> ‘./cptest/home/hu’
    ‘/home/hu/Music’ -> ‘./cptest/home/hu/Music’
    ‘/home/hu/.bash_profile’ -> ‘./cptest/home/hu/.bash_profile’
    ‘/home/hu/.bash_logout’ -> ‘./cptest/home/hu/.bash_logout’
    

    cp -p
    –preserve:

    • mode:許可權
    • ownership:從屬關係
    • timestamps:時間截
    • context:安全標籤
    • links:符號連結
    • all:上訴所有屬性
  • mv命令: - move (rename) files
    mv命令用於移動一個檔案(或目錄)到另一個檔案(或目錄),若檔案(或目錄)不存在,則建立它。也可以用來修改檔案(或目錄)名字。

    1、格式:

    mv [OPTION]… [-T] SOURCE DEST
    mv [OPTION]… SOURCE… DIRECTORY
    mv [OPTION]… -t DIRECTORY SOURCE…

    注意:單位件移動和cp命令是一樣的;但是多檔案移動和cp命令不同,mv命令不需要加-r選項

    2、常用option和示例
    mv -i:覆蓋前先詢問使用者。
    mv -f:若目標檔案或目錄與現有檔案或目錄重複,則直接覆蓋現有檔案或目錄。
    例:

    [[email protected] test4]# cat log2.txt 
    odfdfs
    [[email protected] test4]# cat log3.txt 
    dfosdfsdfdss
    [[email protected] test4]# mv -f log3.txt log2.txt 
    [[email protected] test4]# cat log2.txt 
    dfosdfsdfdss
    

    mv -v:執行時顯示詳細的資訊。
    mv -b:若需要覆蓋檔案,則覆蓋前先進行備份。
    例:

    [[email protected] test4]# mv -bv *.txt /home/pungki/office
    
  • grep命令:- print lines matching a pattern

    grep命令用於查詢檔案裡符合條件的字串。

    1、格式:
    grep [OPTIONS] PATTERN [FILE…]
    grep [OPTIONS] [-e PATTERN | -f FILE] [FILE…]
    注意:pattern表示正則表示式
    正則表示式(也稱“regex”或“regexp”)是一個可以描述一類字串和該正則表示式匹配(Match),正則表示式通常被用來查詢文字的模式,以及對文字執行搜尋/替換操作和其它功能。

    2、常用option和示例
    grep -l:忽略字元大小寫
    例:

    [[email protected] recover]# grep -l "text" file1 file2 file3...
    

    grep -o:僅顯示匹配到的字串本身
    例:

    [[email protected] recover]# echo gun is not unix | grep -b -o "not"
    7:not
    

    grep -v:顯示不能被模式匹配到的行
    例:

    [[email protected] recover]# grep-v test* 
    testfile1:helLinux!  
    testfile1:Linis a free Unix-type 	operating system.  
    testfile1:Lin  
    testfile_1:HELLO LINUX!  
    

    grep -E:支援使用擴充套件的正則表示式元字元
    grep -F:搜尋固定字串
    grep -G:支援基本的正則表示式元字元
    grep -q:靜默模式,不輸出任何資訊
    例:

    [[email protected] recover]# grep -q "test" filename
    #不會輸出任何資訊,如果命令執行成功返回0,失敗則返回非0值。一般用於條件測試。
    

    grep -A#:顯示匹配的行和此行的後#行
    例:

    [[email protected] recover]# seq 10 | grep "5" -A 3
    5
    6
    7
    8
    

    grep -B:顯示匹配的行和此行的前#行
    例:

    [[email protected] recover]# seq 10 | grep "5" -B 3
    2
    3
    4
    5
    

    grep -C:顯示匹配的行和此行的前#行和後#行
    例:

    [[email protected] recover]# seq 10 | grep "5" -C 3
    2
    3
    4
    5
    6
    7
    8
    

    grep - -color:對匹配到的本著色高亮顯示
    例:

    [[email protected] recover]# grep "first" test --color=auto
    first
    
  • head 命令:- output the first part of files

    head命令用於顯示一個檔案的內容的前多少行。

    1、格式: head [OPTION]… [FILE]…

    2、常用option和示例
    head -c:處理檔案前面指定的位元組數加b(512位元組)、k(KB數)、m(MB)。
    例:

    [[email protected] test]# head -c 20 log2014.log
    2014-01
    2014-02
    2014
    

    head -n:顯示檔案頭n行內容。
    例:

    [[email protected] test]# head -n -6 log2014.log
    2014-01
    2014-02
    2014-03
    2014-04
    2014-05
    2014-06
    

    head -p:處理多個檔案時不顯示檔案頭資訊。
    head -v:處理多個檔案時顯示檔案頭資訊

  • tail命令:- output the last part of files

    tail命令顯示一個檔案內容的最後多少行。

    1、格式:tail [OPTION]… [FILE]…

    2、常用option和示例
    tail -n:顯示檔案末尾n行內容。
    例:

    [[email protected] test]# tail -n 5 log2014.log 
    2014-09
    2014-10
    2014-11
    2014-12
    

    tail -f:若檔案大小在增長的話,tail將隨檔案增長而一直顯示。
    例:

    [[email protected] ~]# tail -f test.log 
    PING 192.168.120.204 (192.168.120.204) 56(84) bytes of data.
    64 bytes from 192.168.120.204: icmp_seq=1 ttl=64 time=0.038 ms
    64 bytes from 192.168.120.204: icmp_seq=2 ttl=64 time=0.036 ms
    64 bytes from 192.168.120.204: icmp_seq=3 ttl=64 time=0.033 ms
    64 bytes from 192.168.120.204: icmp_seq=4 ttl=64 time=0.027 ms
    
  • tac命令:- concatenate and print files in reverse

    tac命令用於將檔案已行為單位的反序輸出,即第一行最後顯示,最後一行先顯示。

    1、格式: tac [OPTION]… [FILE]…
    2、常用option和示例
    tac -b:在行前而非行尾新增分隔標誌
    tac -r:將分隔標誌視作正則表示式來解析
    tac -s:使用指定字串代替換行作為分隔標誌
    例:

    [[email protected] www]# cat test
    1234
    abcd
    [[email protected] www]# tac test
    abcd
    1234
    
  • file命令:— determine file type

    file命令用於通過探測檔案內容來判斷檔案型別。

    1、格式:
    file [-bchiklLNnprsvz0] [–apple] [–mime-encoding] [–mime-type] [-e testname] [-F separator] [-f namefile] [-m magicfiles] file …

    2、常用option和示例
    file :顯示檔案型別
    例:

    [[email protected] ~]# file install.log
    install.log: UTF-8 Unicode text
    

    file -i:顯示MIME類別。
    例:

    [[email protected] ~]# file -i install.log      
    install.log: text/plain; charset=utf-8
    

    file -L:顯示符號連結的檔案型別
    例:

    [[email protected] ~]# file -L /var/mail
    /var/mail: directory
    

    file -b:不顯示檔名稱
    例:

    [[email protected] ~]# file -b install.log   
    UTF-8 Unicode text
    
  • echo命令:- display a line of text

    echo命令用於顯示文字內容。

    1、格式:
    echo [SHORT-OPTION]… [STRING]…
    echo LONG-OPTION

    2、常用option和示例
    echo string:輸入一行文字顯示在標準輸出上
    例:

    [[email protected] ~]# echo Tecmint is a community of Linux Nerds 
    Tecmint is a community of Linux Nerds 
    

    echo :輸出變數的值
    例:

    [[email protected] ~]#  x=10
    [[email protected] ~]#  echo The value of variable x = $x 
    The value of variable x = 10 
    
  • date命令: - print or set the system date and time

    date命令用於顯示或設定系統時間與日期。

    1、格式:
    date [OPTION]… [+FORMAT]
    date [-u|–utc|–universal] [MMDDhhmm[[CC]YY][.ss]]

  • mkdir命令:- make directories

    1、格式:

    mkdir [OPTION]… DIRECTORY…

    2、常用option和示例
    mkdir -p:自動按需建立父目錄
    例:

    [[email protected] test]# mkdir -p test2/test22
    [[email protected] test]# ll
    drwxr-xr-x 2 root root 4096 10-25 17:42 test1
    drwxr-xr-x 3 root root 4096 10-25 17:44 test2
    

    mkdir -v:顯示詳細過程
    mkdir -m:直接給定許可權,否則為預設許可權

  • rmdir命令:- remove empty directories

    rmdir命令用於刪除一個或多個目錄。

    1、格式:

    rmdir [OPTION]… DIRECTORY…

    2、常用option和示例
    rmdir -p:刪除某目錄後,若父目錄為空則一併刪除
    rmdir -v:顯示詳細過程
    例:

    [[email protected] ~]# rmdir -p logs
    rmdir: logs: 目錄非空
    

2、使用命令列展開功能,創 建/tmp/a1,/tmp/a2,/tmp/a1/a,/tmp/a1/b,在/tmp目錄下建立目錄:x_y,x_z,q_y,q_z 。

  • 命令列展開功能簡介

    (1)、~:自動展開為使用者家目錄,或指定使用者的家目錄。
    (2)、{}:可承載一個以逗號分隔的路徑列表,並能將其展開為多個路徑。

  • 建立/tmp/a1,/tmp/a2,/tmp/a1/a,/tmp/a1/b

      [[email protected] temp]# mkdir -pv ./tmp/{a1/{a,b},a2}
    	mkdir: created directory ‘./tmp/a1’
    	mkdir: created directory ‘./tmp/a1/a’
    	mkdir: created directory ‘./tmp/a1/b’
    	mkdir: created directory ‘./tmp/a2’
    
  • 建立目錄:x_y,x_z,q_y,q_z

    [[email protected] temp]# mkdir -v {x,q}_{y,z}
    mkdir: created directory ‘x_y’
    mkdir: created directory ‘x_z’
    mkdir: created directory ‘q_y’
    mkdir: created directory ‘q_z’
    

3、檔案的元資料資訊有哪些,分別表示什麼含義,如何檢視?如何修改檔案的時間截資訊。

  • 元資料資訊
    例:[[email protected] temp]# stat tmp
    檔名-File: ‘tmp’
    檔案大小-Size: 4096
    檔案佔用多少資料塊-Blocks: 8
    檔案佔據資料塊的塊大小-IO Block: 4096 directory
    硬體-Device: fd00h/64768d(說明該檔案在硬碟的哪個柱面)
    節點號-Inode: 261644
    連結-Links: 4
    許可權-Access: (0755/drwxr-xr-x)
    該檔案屬主-Uid: ( 0/ root)
    該檔案的屬組-Gid: ( 0/ root)
    安全上下文-Context: unconfined_u:object_r:default_t:s0
    檔案上一次的訪問時間-Access: 2018-11-24 11:20:01.061419943 -0500
    檔案上一次修改的時間-Modify: 2018-11-24 11:17:24.060605259 -0500
    檔案上一次屬性更改的時間-Change: 2018-11-24 11:17:24.060605259 -0500
    建立時間-Birth: -

  • 如何檢視元資料

    使用stat命令檢視元資料-如上例題所示

  • 如何修改檔案的時間截資訊

    (1)、touch(-change file timestamps)命令修改時間截資訊,如下面例題所示
    (2)、格式:touch [OPTION]… FILE…
    注意:touch filename:當filename存在時改變時間截;當filename不存在時則以此filename建立一個空檔案

    例:
    初始時間

    [[email protected] temp]# stat tmp
    Access: 2018-11-24 11:20:01.061419943 -0500
    Modify: 2018-11-24 11:17:24.060605259 -0500
    Change: 2018-11-24 11:17:24.060605259 -0500
     Birth: -
    

    改變後的時間

    [[email protected] temp]# touch tmp
    [[email protected] temp]# stat tmp
    Access: 2018-11-24 11:52:14.634641099 -0500
    Modify: 2018-11-24 11:52:14.634641099 -0500
    Change: 2018-11-24 11:52:14.634641099 -0500
     Birth: -
    

    touch命令option講解
    touch -c:當filename不存在時也不建立新的空檔案、路徑
    touch -a:僅改變檔案上一次的訪問時間-Access
    touch -m:僅改變檔案上一次修改的時間-Modify
    touch -t:自己指定更改時間(時間格式為:[MMDDhhmm[[CC]YY][.ss]])

4、在/tmp目錄下建立以tfile開頭,後面跟當前日期和時間的檔案,檔名形如:tfile-2016-05-27-09-32-22。

	[[email protected] temp]# mkdir tfile-$(date "+%Y-%m-%d-%R:%S")
	[[email protected] temp]# ls
	etc  q_y  q_z  tfile-2018-11-24-12:06:54  tmp  x_y  x_z

5、複製/etc目錄下所有以p開頭,以非數字結尾的檔案或目錄到/tmp/mytest1目錄中。

	[[email protected] temp]# ll ./tmp/mytest1ot 4096 Nov 24 12:12 popt.d
	drwxr-xr-x. 2 root root 4096 Nov 24 12:12 postfix
	drwxr-xr-x. 3 root root 4096 Nov 24 12:12 ppp
	drwxr-xr-x. 2 root root 4096 Nov 24 12:12 prelink.conf.d
	-rw-r--r--. 1 root root  233 Nov 24 12:12 printcap
	-rw-r--r--. 1 root root 1750 Nov 24 12:12 profile
	drwxr-xr-x. 2 root root 4096 Nov 24 12:12 profile.d
	-rw-r--r--. 1 root root 6545 Nov 24 12:12 protocols
	drwxr-xr-x. 2 root root 4096 Nov 24 12:12 pulse
	drwxr-xr-x. 2 root root 4096 Nov 24 12:12 purple
	drwxr-xr-x. 2 root root 4096 Nov 24 12:12 python

6、建立使用者tom,指定UID為5001,指定家目錄為/tmp/tom,指定shell為、bin/zsh,指定基本組為tom,附加組為jack

[[email protected] /]# useradd -u 5001 -d /recover/temp/tom -s /bin/zsh -G jack tom
[[email protected] /]# cat /etc/passwd
tom:x:5001:5001::/recover/temp/tom:/bin/zsh
[[email protected] /]# cat /etc/group
jack:x:5002:tom

7、常用的使用者管理命令有哪些,並演示命令以及用法。

  • useradd:- create a new user or update default new user information

    useradd命令用於建立一個新使用者或更新預設新使用者資訊

    1、格式:
    useradd [options] LOGIN
    useradd -D
    useradd -D [options]

    2、options
    useradd -u:指定使用者UID
    useradd -g:指定使用者基本組,此組事先得存在
    useradd -G:指定使用者所屬的附加組,多個組之間用逗號分隔
    useradd -c:指明註釋資訊
    useradd -d:以指定的路徑為使用者的家目錄
    useradd -s:指定使用者預設shell(可用的所有shell列表在/etc/shells檔案中)
    useradd -r:建立系統使用者
    useradd -m:若不存在則建立使用者主目錄,骨架目錄中的檔案會複製到主目錄
    useradd -M:不建立使用者主目錄即使系統設定了也不建立
    useradd -f:密碼過期後,賬戶徹底禁用之前的天數
    useradd -D:表示建立使用者時的預設資訊
    useradd -D option:用於修改或更改預設資訊

  • usermod:- modify a user account

    useradd命令用於修改使用者屬性

    1、格式:
    usermod [options] LOGIN

    2、options
    usermod -u:修改使用者的UID為此處指定的新UID
    usermod -g:修改使用者所屬基本組
    usermod -G:修改使用者所屬的附加組,若原有附加組則用新組覆蓋原附加組
    usermod -a:與-G一同使用,用於使用者追加新的附加組
    usermod -c:修改註釋資訊
    usermod -d:修改使用者家目錄,單檔案不會轉移到修改路徑下
    usermod -m:只能與-d使用,用於將原來家目錄移到新路徑下
    usermod -l:修改該使用者名稱
    usermod -s:修改使用者預設shell
    usermod -L:鎖定使用者密碼;禁止登陸
    usermod -U:解鎖使用者的密碼

  • userdel:- delete a user account and related files

    useradd命令用於刪除使用者

    1、格式:
    userdel [options] LOGIN

    2、options
    userdel -r:刪除使用者的同事刪除使用者的家目錄

  • passwd:- update user’s authentication tokens

    useradd命令用於為使用者設定密碼

    1、格式:
    passwd [-k] [-l] [-u [-f]] [-d] [-e] [-n mindays] [-x maxdays] [-w warn‐
    days] [-i inactivedays] [-S] [–stdin] [username]

    2、options
    passwd -l,-u:表示用來鎖定和解鎖使用者
    passwd -d:清除使用者密碼
    passwd -e:密碼過期期限
    passwd -i:非活動期限
    passwd -n:密碼最短使用期限
    passwd -x:密碼最常使用期限
    passwd -w:警告期限
    passwd - -stdin