有關/etc/passwd,/etc/shadow,/etc/group,以及用戶管理與查看用戶信息的命令知識總結


有關/etc/passwd,/etc/shadow,/etc/group,以及用戶管理與查看用戶信息的命令知識總結


/etc/passwd文件

passwd文件存放的是用戶相關的信息,包含有用戶名,密碼,屬主uid,屬組gid,註釋相關,家目錄,用戶所用的shell

root:x:0:0:root:/root:/bin/bash

有7個字段,每個字段代表的是:
1.用戶名
2.密碼,用一個X表示,其以加密的形式存放在/etc/shadow下
3.屬主id
4.屬組id
5.對用戶進行的註釋信息,以便日後查看
6.用戶家目錄
7.用戶所用的shell,有一類用戶是不登錄的,也就是說,根本用不上shell
    daemon:x:2:2:daemon:/sbin:/sbin/nologin

系統用戶:    
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash

自定義用戶:
    fedora:x:2003:2003:Fedora Community:/home/fedora:/bin/tcsh

**

系統的uid在500以下(1-499),用戶自定義的賬號的gid在500以上(500-6000)

**

添加用戶:

useradd

    -c:註釋說明,相當於第五個字段
    -d:指定家目錄,若不指定,則會在/home目錄下建立一個以當前用戶名爲名的家目錄
    
    -g:組名,必須事先存在
    
    -G:附加組
    
    -M:不建立家目錄
    
    -r:建立系統用戶
    
    -s shell:指定用戶登錄的shell

    -u uid :指定uid

/etc/login.defs:這個有建立用戶時,默認的設定
 
實例:添加一個用戶名爲test,uid爲5500,附加組爲testone

#添加一個組

#groupadd testone

#添加一個用戶

#useradd test -u 5500 -G testone

刪除用戶:userdel
    -r:連家目錄一起刪除
    
修改用戶信息:usermod,這裏的選項參數和useradd產不多


組:
用戶組類別:
1.私有組:創建用戶時,如果沒有爲其指定組,系統自動爲其創建一個與用戶同名的組

2.附加組

/etc/group

添加組:
groupadd

    -r:建立系統組
    -g gid:指定組id

root:x:0:
test11:x:12366:test122

1.組名
2.組的管理密碼
3.組id
4.附加組,附加組可以有多個

刪除組:
groupdel
只能刪除使用groupadd添加的組,而不能刪除當使用useradd創建用戶時創建的組
    -n group_name:更改組名
    -g gid :更改組id
    
密碼文件:/etc/shadow

root:$6$H7hZfcMAaOhsYXTo$i6iSH8ddgj.FmmCQxRQe2eKJS7kyEe8cIwmOXMbygB9uChYWiWs0uf0y3knDYyfKBVw2sufH2nbGd/QWb9g8g.:16449:0:99999:7:::
ubuntu:!!:16682:0:99999:7:::

各個字段代表的意義:
1.登錄名
2.加密密碼
3.自1970.1.1起最後一次修改密碼的時間(天數)
4.密碼持續的最小天數
5.密碼持續的最大天數
6.密碼將要過期的時間提醒(在要快到期時的警告)
7.密碼到期後的寬限天數(在密碼到期後的提醒,如再不更改密碼,則用戶被鎖定)
8.密碼的到期時間

爲用戶設定密碼:passwd

passwd username
    如不指定用戶名,則爲當前用戶更改密碼

    也可以使用如下方法改密
    echo "redhat" | passwd --stdin username

    -l:鎖定
    -u:解鎖
    -d:刪除用戶密碼
    
與用戶賬號有關的其他命令:
    
pwck檢查用戶賬號的完整性,傳入的是passwd文件    

# pwck /etc/passwd
user 'adm': directory '/var/adm' does not exist
user 'uucp': directory '/var/spool/uucp' does not exist
user 'gopher': directory '/var/gopher' does not exist
user 'oprofile': directory '/home/oprofile' does not exist
user 'avahi-autoipd': directory '/var/lib/avahi-autoipd' does not exist
user 'saslauth': directory '/var/empty/saslauth' does not exist
user 'pulse': directory '/var/run/pulse' does not exist

pwck:無改變id:查看用戶賬號的使用信息
    -u:顯示uid
    -g:顯示gid
    -G:顯示Gid

# id ubuntu
uid=12362(ubuntu) gid=12367(ubuntu) 組=12367(ubuntu)

chsh:更改用戶的登錄shell
    -s:指定一個shell
    

# tail /etc/passwd    
ubuntu:x:12362:12367::/home/ubuntu:/bin/bash
# chsh -s /bin/tcsh ubuntu
Changing shell for ubuntu.
Shell changed.
# tail /etc/passwd    
ubuntu:x:12362:12367::/home/ubuntu:/bin/tcsh

   

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章