1. 程式人生 > >ubuntu 16.04 desktop + server LTS - 新增 / 刪除使用者

ubuntu 16.04 desktop + server LTS - 新增 / 刪除使用者

ubuntu 16.04 desktop + server LTS - 新增 / 刪除使用者

How to add & delete users on Ubuntu 16.04?

新增新使用者 yongqiang

Ubuntu users can add a new user using “adduser” command. When you run the “adduser” command to add a user account, you will have to give the new user account a password and name.

sudo adduser username

新使用者新增成功後,使用 ls /home 檢查新使用者資料夾。

[email protected]:~$ sudo adduser yongqiang
[sudo] password for amax: 
Adding user `yongqiang' ...
Adding new group `yongqiang' (1007) ...
Adding new user `yongqiang' (1007) with group `yongqiang' ...
Creating home directory `/home/yongqiang' ...
Copying files from `/etc/skel' ...
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
Changing the user information for yongqiang
Enter the new value, or press ENTER for the default
	Full Name []: yongqiang
	Room Number []: 
	Work Phone []: 
	Home Phone []: 
	Other []: 
Is the information correct? [Y/n] Y
[email protected]
:~$
[email protected]:~$ sudo adduser yongqiang
[sudo] strong 的密碼: 
正在新增使用者"yongqiang"...
正在新增新組"yongqiang" (1001)...
正在新增新使用者"yongqiang" (1001) 到組"yongqiang"...
建立主目錄"/home/yongqiang"...
正在從"/etc/skel"複製檔案...
輸入新的 UNIX 密碼: 
重新輸入新的 UNIX 密碼: 
passwd:已成功更新密碼
正在改變 yongqiang 的使用者資訊
請輸入新值,或直接敲回車鍵以使用預設值
	全名 []: yongqiang
	房間號碼 []: 
	工作電話 []: 
	家庭電話 []: 
	其它 []: 
這些資訊是否正確? [Y/n] Y
[email protected]
:~$ [email protected]:~$ cd /home/ [email protected]:/home$ ls strong yongqiang [email protected]trong-sys:/home$ [email protected]:/home$ cd ~ [email protected]:~$

You can also create a new user using the System Settings. Open System Settings and click on User Accounts. From here you can easily manage new Ubuntu user. Please note that you must have root privilege to add or delete a new user.

在這裡插入圖片描述

Once the user is created, you can easily use the newly created user account. Please note that when a new user is created, the adduser utility creates a new home directory named /home/username.

使用者 yongqiang 新增 sudo 許可權

# User privilege specification
root	ALL=(ALL:ALL) ALL
yongqiang	ALL=(ALL:ALL) ALL

複製 root ALL=(ALL:ALL) ALL 至下一行,修改為 yongqiang ALL=(ALL:ALL) ALL 。

[email protected]:~$ sudo vim /etc/sudoers
[sudo] strong 的密碼: 
[email protected]:~$ 
[email protected]:~$ cat /etc/sudoers
cat: /etc/sudoers: 許可權不夠
[email protected]:~$ 
[email protected]:~$ sudo cat /etc/sudoers
#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults	env_reset
Defaults	mail_badpass
Defaults	secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root	ALL=(ALL:ALL) ALL
yongqiang    ALL=(ALL:ALL) ALL

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL

# Allow members of group sudo to execute any command
%sudo	ALL=(ALL:ALL) ALL

# See sudoers(5) for more information on "#include" directives:

#includedir /etc/sudoers.d
[email protected]:~$

刪除使用者

If you wish to delete the newly created user, use the command “deluser” to remove and delete the specific Ubuntu user.
Please note that when you delete a specific user account, it does not remove their respective home folder. You can delete the folder manually.
Ubuntu 刪除使用者需要注意的是,如果要刪除的使用者當前已登陸,是刪除不掉的。必須登出掉當前使用者,切換到其他使用者下,才能刪除。

sudo userdel username
sudo userdel -r username

刪除成功後,系統無任何提示。
最好將使用者留在系統上的檔案也刪除掉,可以使用 userdel -r username 來實現。

useradd / adduser

useradd / adduser 命令用來建立使用者帳號,使用許可權是超級使用者。
利用 adduser 建立新使用者 (adduser + username),在 /home 目錄下會自動建立同名資料夾。
useradd 僅僅建立了一個使用者名稱 (useradd + username),並沒有在 /home 目錄下建立同名資料夾,也沒有建立密碼。因此利用這個使用者登入系統,是登入不了的。
可以用 (useradd -m + username) 的方式建立,它會在 /home 目錄下建立同名資料夾,然後利用 (passwd + username)為指定的使用者名稱設定密碼。

change user permissions

You can easily change and grant permissions for a specific user - change UID/GID values. Run the following command as required:

sudo chown -R root:root /home/username/
sudo mkdir /home/archived_users/
sudo mv /home/username /home/archived_users/

Not only this, you can also temporarily lock or unlock a specific user account using the following command:

sudo passwd -l username
sudo passwd -u username