linux基礎:12、基礎命令(5) - 使用者管理

來源:互聯網
上載者:User

標籤:linux   使用者管理   

設定檔)


/etc/passwd

文檔結構:

    由":"分隔成7個欄位"username:x:uid:gid:remark:homedir:shell"

預設許可權:-rw-r--r--

各欄位含義:

    (1) username,使用者名稱稱 

        命名規則:大小寫字母、數字、減號(不能出現在首位)、點以及底線,其他字元不合法。 

    (2) x,密碼空位鍵 

        密碼安全起見放到 /etc/shadow。 

    (3) uid,使用者ID  

        可用ID,0-4294967294=2^32-2; 

        root的uid=0;1-499系統保留;普通賬戶是從500開始。 

        普通使用者的uid可以改成0,此時系統就將其識別成root使用者。 

    (4) gid,主組id 

        組設定檔/etc/group。 

    (5) remark,注釋說明 

        該欄位沒有實際意義,通常記錄使用者的一些屬性,例如姓名、電話、地址等等  用 chfn 更改; 

    (6) home directory,使用者家目錄 

    (7) shell environment,shell環境

        /bin/bash; /sbin/nologin(禁止該使用者登入)。 

======================================================================[[email protected] ~]# head /etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltmail:x:8:12:mail:/var/spool/mail:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologin======================================================================

        

/etc/shadow 

文檔結構:

    使用":"分隔成9個欄位

    username

    password(密文)

    此密碼設定日期

    密碼不可更改的天數 

    密碼需要重設的天數

    密碼到期提前提醒天數

    帳號失效後寬限天數

    帳號失效日期

    保留位

預設許可權:----------(除了root使用者,其他所有普通使用者包括root群組成員都沒有任何許可權呦) 

    (1) 使用者名稱 

        是與/etc/passwd檔案中的登入名稱相一致的使用者帳號。 

    (2) 密碼 

        已加密顯示密文,無法復原,但可被破解; 

        !!表示密碼為空白 

    (3)當前密碼的設定日期 

        此日期用設定當前密碼的日期和1970年1月1日的差值來表示 

==================================================================[[email protected] ~]$ echo $(($(date --date="2015/03/16" +%s)/86400+1))16510==================================================================

    (4)密碼不可被更改的天數(與第三段日期比較) 

        值為0時,意為不限制,隨時可更改。 

        如果設定為20,則20天內使用者不可更改密碼。 

    (5)密碼需要重設的天數(與第三段日期比較) 

        預設值是99999,遠遠超出電腦壽命,基本可以認為永不到期。 

    (6)密碼到期前提前提醒天數(與第五段日期比較) 

        設定為7既為密碼到期日提前7天提醒使用者修改密碼,“您的密碼還有n天就要到期,請及時修改密碼”。  

    (7)帳號失效後寬限天數 

        到期後過多少天鎖定帳號  

    (8)帳號失效日期 

        計算方式同第三段; 

        應該可以用到收費服務上。 

    (9)保留位 

=====================================================================[[email protected] ~]# head /etc/shadowroot:$6$oEAOlU/b$PPPeEPWk359I0AP3Fz0IAVyCtHs1m0xLBPJefLszn0xAKk3WfDEv7HJakf33skp8Pz/A1dVRmG/vkIQyjQOtH/:16505:0:99999:7:::bin:*:16231:0:99999:7:::daemon:*:16231:0:99999:7:::adm:*:16231:0:99999:7:::lp:*:16231:0:99999:7:::sync:*:16231:0:99999:7:::shutdown:*:16231:0:99999:7:::halt:*:16231:0:99999:7:::mail:*:16231:0:99999:7:::operator:*:16231:0:99999:7:::=====================================================================


使用者及使用者組相關命令)


id 

作用:查看使用者的uid、gid和所屬組資訊 

man: print real and effective user and group IDs

文法:id username

========================================================[[email protected] ~]# id essenceuid=1000(essence) gid=1000(essence) groups=1000(essence)========================================================


groupadd 

作用:增加使用者組 

man:groupadd - create a new group

設定檔:/etc/group & /etc/gshadow 

文法:groupadd [選項] groupname  

選項:

    -g 指定gid 

=========================================================[[email protected] ~]# groupadd -g 1999 admin[[email protected] ~]# cat /etc/group |grep 1999admin:x:1999:=========================================================


groupdel 

作用:刪除使用者組 

文法:groupdel groupname 

PS:單純作為附屬組的時候是可以直接刪除的,但如果作為使用者的主組,就不能隨意刪除了,需要提前把使用者清空 

=================================================================[[email protected] ~]# usermod -aG admin essence  #!把admin設定為essence的附屬組[[email protected] ~]# id essenceuid=1000(essence) gid=1000(essence) groups=1000(essence),1999(admin)[[email protected] ~]# groupdel admin[[email protected] ~]# id essenceuid=1000(essence) gid=1000(essence) groups=1000(essence)#!附屬組可被直接刪除[[email protected] ~]# groupdel essencegroupdel: cannot remove the primary group of user ‘essence‘#!主組必須要先把user刪掉才可以刪除=================================================================


useradd 

作用:增加使用者 

文法:useradd [選項] username 

選項: 

    -u 指定uid (The default value for UID_MIN (resp.  UID_MAX) is 1000 (resp. 60000).)

    -g 指定gid或組名(但指定的組名必須存在) 

    -d 指定家目錄 

    -M 不建立家目錄,但依然會建立使用者的mail目錄 

    -s 自訂shell環境,可控制普通使用者不能登入,通過設定“/sbin/nologin” 

PS:多個使用者可以共用一個主組 

PS:uid和gid分配的時候,以該id的最大數字+1 

PS:uid最小是1000(centos7),和centos6的最小值不同(uid_min=500)

=================================================================[[email protected] ~]# useradd test1 -g 1000 -M -s /sbin/nologin#!建立test1使用者,和essence共用一個組,無家目錄,不允許登陸系統[[email protected] ~]# cat /etc/passwd|grep test1test1:x:1001:1000::/home/test1:/sbin/nologin#!第六段依然有家目錄路徑,為何?[[email protected] ~]# ls /home/test1ls: cannot access /home/test1: No such file or directory#!原來此目錄並不存在,看來/etc/passwd不足以作為判斷家目錄是否存在的標準。[[email protected] ~]# id test1uid=1001(test1) gid=1000(essence) groups=1000(essence)#!和essence共用了一個組=================================================================


userdel 

作用:刪除使用者,無參數時預設不刪除家目錄 

文法:userdel [選項] username 

選項:-r 刪除使用者的家目錄和郵件目錄(/var/spool/mail/username) 

=================================================================[[email protected] ~]# ls /var/spool/mail/test1test1[[email protected] ~]# userdel -r test1userdel: test1 home directory (/home/test1) not found#!之所以會報錯,是因為我們根本沒建立test1的家目錄。[[email protected] ~]# ls /var/spool/mail/test1ls: cannot access /var/spool/mail/test1: No such file or directory=================================================================


usermod 

作用:修改使用者uid、gid、家目錄、附屬組等資訊 

文法:usermod [選項 選項內容] username 

選項 :

    -a append追加模式,僅可與-G一起使用,用來避免-G指定附屬組時造成使用者脫離其他附屬組。 

    -u 修改使用者uid 

    -s 修改使用者shell環境 

    -d 修改目錄家目錄 

    -L 鎖定使用者密碼,使之不能用密碼登入 

    -U 解鎖使用者密碼 

    -g 指定使用者主組gid 

    -G 指定使用者附屬組(1個使用者可以有多個附屬組) 

PS:usermod -G "" user 把附屬組清空 

==================================================================[[email protected] ~]# usermod -G root essence[[email protected] ~]# id essenceuid=1000(essence) gid=1000(essence) groups=1000(essence),0(root)#!現在essence加入了自己的主組essence和root[[email protected] ~]# usermod -G test1 essence[[email protected] ~]# id essenceuid=1000(essence) gid=1000(essence) groups=1000(essence),1001(test1)#!如果沒有-a參數,附屬組root被抹掉,換成了test1==================================================================


passwd   

作用:修改使用者密碼 

文法:passwd 使用者名稱 

建立密碼的規則: 

長度大於10;含大小寫字母數字以及特殊字元 ;不規則性;不要帶有自己名字、公司名字、自己電話、自己生日等。 

擴充內容:mkpasswd  產生密碼的工具,安裝 expect包 

==================================================================[[email protected] ~]# yum install expect[[email protected] ~]# mkpasswdsiSXzp~96==================================================================


su 

作用:切換effective使用者 

man:run a command with substitute user and group ID

文法:su 使用者名稱(改變$HOME,不改變$PATH) 

文法: su - 使用者名稱 (改變$HOME和$PATH) 

擴充資料:http://www.aminglinux.com/bbs/thread-6912-1-1.html 

 

sudo 

作用:臨時獲得root許可權(su到root需要輸入root的密碼不安全,所以sudo是一個好選擇) 

修改設定檔命令:visudo 

安裝方法: yum install -y sudo 

文法:sudo 正常命令輸入 

設定檔:/etc/sudoers 

*****************************************************

......

root    ALL=(ALL)       ALL

essence ALL=(root)      /bin/ls

......

**********************************************************

意義是,讓essence這個普通使用者,擁有root的許可權,針對ls這個命令。(要加絕對路徑哦) 


安全擴充

/etc/ssh/sshd_config

禁止root遠程登入,用普通使用者登入後,在用”sudo su -“登入root 

本文出自 “三零妖人” 部落格,請務必保留此出處http://301ren.blog.51cto.com/8887653/1621596

linux基礎:12、基礎命令(5) - 使用者管理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.