1. 程式人生 > >linux-用戶及組管理

linux-用戶及組管理

sys mes 登陸 默認 inux spl 用戶及組管理 創建 -a

用戶及組管理

環境:ubuntu-16.04.4-server-amd64

/etc/passwd
root:x:0:0:root:/root:/bin/bash
用戶名稱 用戶密碼 用戶ID 主組ID 註釋 用戶目錄 shell
x表示加密(點位符)

/etc/group
root:x:0:
組名稱 組密碼 組ID 組內用戶(多個以逗號分隔)
通常用戶組不需要密碼

/etc/shadow
root:!:17738:0:99999:7:::
用戶名稱 用戶密碼

useradd [option] newuser
-c, --comment COMMENT GECOS field of the new account
-g, --gid GROUP name or ID of the primary group of the new account
-G, --groups GROUPS list of supplementary groups of the new account
-h, --help display this help message and exit
-m, --create-home create the user‘s home directory 創建目錄/home/new-user
-r, --system create a system account
-s, --shell SHELL login shell of the new account
-u, --uid UID user ID of the new account 否則系統默認分配

passwd newuser

usermod [option] newuser
-c, --comment COMMENT new value of the GECOS field
-d, --home HOME_DIR new home directory for the user account
-g, --gid GROUP force use GROUP as new primary group
-G, --groups GROUPS new list of supplementary GROUPS
-h, --help display this help message and exit
-l, --login NEW_LOGIN new value of the login name
-m, --move-home move contents of the home directory to the new location (use only with -d)
-s, --shell SHELL new login shell for the user account
-u, --uid UID new UID for the user account

userdel [option] newuser
-h, --help display this help message and exit
-r, --remove remove home directory and mail spool
對於已經登陸用戶,需要取得其相關進程id,kill之,再刪除
# ps -ef|grep newuser
root 2112 1128 0 11:06 ? 00:00:00 sshd: newuser [priv]
newuser 2114 1 0 11:06 ? 00:00:00 /lib/systemd/systemd --user
newuser 2115 2114 0 11:06 ? 00:00:00 (sd-pam)
newuser 2150 2112 0 11:06 ? 00:00:00 sshd: newuser@pts/1
newuser 2151 2150 0 11:06 pts/1 00:00:00 -sh
root 2157 1618 0 11:06 pts/0 00:00:00 grep --color=auto newuser
# kill 2112
# userdel -r newuser
# ll /home
#cat /etc/passwd

groupadd [option] newgroup
-g, --gid GID use GID for the new group 類似useradd -u即自定義id
-h, --help display this help message and exit
-r, --system create a system account

groupmod [option] newgroup
-g, --gid GID change the group ID to GID
-h, --help display this help message and exit
-n, --new-name NEW_GROUP change the name to NEW_GROUP

groupdel newgroup
如果刪除的組是某些用戶的主組,需要解除關系才可刪除,比如修改這些用戶的-g
# usermod -g 0 newuser
# groupdel newgroup

linux-用戶及組管理