Ubuntu 添加删除用户 How to Add and Delete Users on Ubuntu 16.04

Introduction

One of the most basic tasks that you should know how to do on a fresh Linux server is add and remove users. When you create a new system, you are often (such as on DigitalOcean Droplets) only given the root account by default.

While running as the root user gives you a lot of power and flexibility, it is also dangerous and can be destructive. It is almost always a better idea to add an additional, unprivileged user to do common tasks. You also should create additional accounts for any other users you may have on your system. Each user should have a different account.

You can still acquire administrator privileges when you need them through a mechanism called sudo. In this guide we will cover how to create user accounts, assign sudo privileges, and delete users.

How To Add a User

If you are signed in as the root user, you can create a new user at any time by typing:

adduser newuser

If you are signed in as a non-root user who has been given sudo privileges, as demonstrated in the initial server setup guide, you can add a new user by typing:

sudo adduser newuser

Either way, you will be asked a series of questions. The procedure will be:

  • Assign and confirm a password for the new user
  • Enter any additional information about the new user. This is entirely optional and can be skipped by hitting Enter if you don‘t wish to utilize these fields.
  • Finally, you‘ll be asked to confirm that the information you provided was correct. Enter Y to continue.

Your new user is now ready for use! You can now log in using the password you set up.

Note: Continue if you need your new user to have access to administrative functionality.

How To Grant a User Sudo Privileges

If your new user should have the ability to execute commands with root (administrative) privileges, you will need to give the new user access to sudo. Let‘s examine two approaches to this problem: Adding the user to a pre-defined sudo user group, and specifying privileges on a per-user basis in sudo‘s configuration.

Add the New User to the Sudo Group

By default, sudo on Ubuntu 16.04 systems is configured to extend full privileges to any user in the sudo group.

You can see what groups your new user is in with the groups command:

groups newuser
Output
    newuser : newuser

By default, a new user is only in their own group, which is created at the time of account creation, and shares a name with the user. In order to add the user to a new group, we can use the usermod command:

usermod -aG sudo newuser

The -aG option here tells usermod to add the user to the listed groups.

Test Your User‘s Sudo Privileges

Now, your new user is able to execute commands with administrative privileges.

When signed in as the new user, you can execute commands as your regular user by typing commands as normal:

  • some_command

You can execute the same command with administrative privileges by typing sudo ahead of the command:

  • sudo some_command

You will be prompted to enter the password of the regular user account you are signed in as.

Specifying Explicit User Privileges in /etc/sudoers

As an alternative to putting your user in the sudo group, you can use the visudo command, which opens a configuration file called /etc/sudoers in the system‘s default editor, and explicitly specify privileges on a per-user basis.

Using visudo is the only recommended way to make changes to /etc/sudoers, because it locks the file against multiple simultaneous edits and performs a sanity check on its contents before overwriting the file. This helps to prevent a situation where you misconfigure sudo and are prevented from fixing the problem because you have lost sudo privileges.

If you are currently signed in as root, type:

visudo

If you are signed in using a non-root user with sudo privileges, type:

sudo visudo

Traditionally, visudo opened /etc/sudoers in the vi editor, which can be confusing for inexperienced users. By default on new Ubuntu installations, it should instead use nano, which provides a more familiar text editing experience. Use the arrow keys to move the cursor, and search for the line that looks like this:

/etc/sudoers

root    ALL=(ALL:ALL) ALL

Below this line, copy the format you see here, changing only the word "root" to reference the new user that you would like to give sudo privileges to:

/etc/sudoers

root    ALL=(ALL:ALL) ALL
newuser ALL=(ALL:ALL) ALL

You should add a new line like this for each user that should be given full sudo privileges. When you are finished, you can save and close the file by hitting Ctrl-X, followed by Y, and then Enter to confirm.

How To Delete a User

In the event that you no longer need a user, it is best to delete the old account.

You can delete the user itself, without deleting any of their files, by typing this as root:

deluser newuser

If you are signed in as another non-root user with sudo privileges, you could instead type:

sudo deluser newuser

If, instead, you want to delete the user‘s home directory when the user is deleted, you can issue the following command as root:

deluser --remove-home newuser

If you‘re running this as a non-root user with sudo privileges, you would instead type:

sudo deluser --remove-home newuser

If you had previously configured sudo privileges for the user you deleted, you may want to remove the relevant line again by typing:

visudo

Or use this if you are a non-root user with sudo privileges:

sudo visudo
root    ALL=(ALL:ALL) ALL
newuser ALL=(ALL:ALL) ALL   # DELETE THIS LINE

This will prevent a new user created with the same name from being accidentally given sudo privileges.

Conclusion

You should now have a fairly good handle on how to add and remove users from your Ubuntu 16.04 system. Effective user management will allow you to separate users and give them only the access that they are required to do their job.

For more information about how to configure sudo, check out our guide on how to edit the sudoers filehere.

来源: https://www.digitalocean.com/community/tutorials/how-to-add-and-delete-users-on-ubuntu-16-04

原文地址:https://www.cnblogs.com/jins-note/p/10004025.html

时间: 2024-08-03 17:18:24

Ubuntu 添加删除用户 How to Add and Delete Users on Ubuntu 16.04的相关文章

<linux小脚本>批量添加/删除用户

批量添加/删除用户,当输入add时,判断用户是否存在,存在则显示存在,不存在则添加:当输入del时,判断用户是否存在,存在则删除用户,不存在则显示不存在. #!/bin/bash if [ $1 == "add" ];then for i in {1..10}; do if id user$i &> /dev/null;then echo "the user$i exists!" else useradd user$i &> /dev/n

【收藏】Linux添加/删除用户和用户组

1.建用户:adduser phpq                             //新建phpq用户passwd phpq                               //给phpq用户设置密码 2.建工作组groupadd test                          //新建test工作组 3.新建用户同时增加工作组useradd -g test phpq                      //新建phpq用户并增加到test工作组 注::

添加删除用户、组,分配目录权限

一.用户.组部分 1.添加删除用户(并分配到组) useradd -g preferred -G supplement username -g 登陆首选组 -G 其他补充组 username 用户名 2.添加删除组 groupadd groupname groupmod -n newgroupname(切换到新的组名) groupdel groupname 3.查看用户的组情况 id user id root 4.改变用户的组 usermod -g privategroup -G supplem

Powershell 添加删除用户到组

简单备忘一下: 今天需要将某个OU下面的所有用户加入一个安全组,稍微翻阅了一下Powershell的命令,方法如下 首先通过Get-ADUser 搜索某个OU下的所有用户,然后通过管道将所有用户的账号(SAMAccount)加入指定的组. 类似的方法,可以把某个OU下所有用户从指定的组中移掉.因为remove的命令一般都会有提示,所以我添加了个-confirm:$false来取消确认提示 很简单的命令,不过很有效. Powershell 添加删除用户到组

<linux小脚本>case实现批量添加/删除用户

#!/bin/bash #输入add添加用户,输入del删除用户,-v选择是否显示结果信息 DEBUG=0 ADD=0 DEL=0 for i in `seq 0 $#`; do if [ $# -gt 0 ];then case $1 in -v) DEBUG=1 shift ;; -h) echo "please open the help page....." exit 0 ;; --add) ADD=1 ADDUSER=$2 shift 2 ;; --del) DEL=1 DE

ubuntu添加新用户并添加管理员权限

Ubuntu创建新用户并增加管理员权限  Family 2014-06-24 22:21:22 $是普通管员,#是系统管理员,在Ubuntu下,root用户默认是没有密码的,因此也就无法使用(据说是为了安全).想用root的话,得给root用户设置一个密码: $ sudo passwd root然后登录时用户名输入root,再输入密码就行了.ubuntu建用户最好用adduser,虽然adduser和useradd是一样的在别的linux糸统下,但是我在ubuntu下用useradd时,并没有创

Linux添加/删除用户和用户组

显示用户信息id usercat /etc/passwd 1.建用户:useradd username                             //新建用户passwd username                               //给用户设置密码 2.建工作组groupadd groupname                          //新建工作组 3.新建用户同时增加工作组useradd -g groupname username        

【CentOS 7.1】添加删除用户

添加用户 [[email protected] home]# adduser eric [[email protected] home]# 修改密码 [[email protected] home]# passwd eric 更改用户 eric 的密码 . 新的 密码: 无效的密码: 密码少于 8 个字符 重新输入新的 密码: passwd:所有的身份验证令牌已经成功更新. [[email protected] home]# 删除用户和主目录 [[email protected] home]#

Ubuntu 添加管理员用户

Ubuntu 默认只有安装时候的那个账号是系统管理员账号,也不会给出root用户权限. 在管理员账号下,用passwd命令给root一个密码,就可以使用root了. 但是,使用useradd时候,如果不加参数,添加的为普通账号.如何添加一个新的系统管理员账号 #useradd -mr  ***(你要添加的账号名称)然后 # paddwd ***(新添加的账号名称) 其中,useradd的参数说明可以使用useradd --help查看. -m 为创建账号主目录,默认不创建. -r 为创建系统管理