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.

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

  1. vue简单案例_动态添加删除用户数据

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. [CentOS]添加删除用户

    摘要 在安装CentOS的时候,我们只设置了root,类似windows的超级管理员.当然我们在工作的时候,为了安全考虑,不可能对外开发root,一方面是从安全的角度,另一方面也是方便管理. 添加删除 ...

  3. centos的用户、组权限、添加删除用户等操作的详细操作命令

    1.Linux操作系统是多用户多任务操作系统,包括用户账户和组账户两种 细分用户账户(普通用户账户,超级用户账户)除了用户账户以为还 有组账户所谓组账户就是用户账户的集合,centos组中有两种类型, ...

  4. Linux——CentOS7添加/删除用户和用户组1

    Linux--CentOS7添加/删除用户和用户组 2017.05.02 19:58 23012浏览   前言 今天又重新装了centos7突然有关用户和用户组有关的命令记不清了,所以记一下,也方便你 ...

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

    Ubuntu创建新用户并增加管理员权限  Family 2014-06-24 22:21:22 $是普通管员,#是系统管理员,在Ubuntu下,root用户默认是没有密码的,因此也就无法使用(据说是为 ...

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

    声明:现大部分文章为寻找问题时在网上相互转载,在此博客中做个记录,方便自己也方便有类似问题的朋友,故原出处已不好查到,如有侵权,请发邮件表明文章和原出处地址,我一定在文章中注明.谢谢. 本文总结了Li ...

  7. 详解centos用户&组权限&添加删除用户

    1.Linux用户操作系统 Linux操作系统是多用户多任务操作系统,包括用户账户和组账户两种: 细分用户账户(普通用户账户,超级用户账户)除了用户账户以为还有组账户所谓组账户就是用户账户的集合,ce ...

  8. Ubuntu 添加sudo用户

    第一种方法: 添加sudo用户 当你安装Ubuntu的时候,它会自动添加第一个用户到sudo组,允许这个用户通过键入其自身帐户密 码来获得超级用户(root)身份.然而,系统不会再自动添加其他的用户到 ...

  9. mysql基础:登录退出,修改用户密码,添加删除用户

    今天刚开始学习mysql,最先接触用户管理,给大家分享下 注:mysql中命令的大小写都可以的 ==========登录退出相关=================== root@jack-deskto ...

随机推荐

  1. POJ 2301

    #include<iostream> #include<stdio.h> using namespace std; int main() { //freopen("a ...

  2. apache2.4脚本一键安装(linux环境)

    1.下载apache安装包和相关组件 下载地址:https://pan.baidu.com/s/1o85i6Jw 其中包括 apache安装包:httpd-2.4.29.tar.gz apache安装 ...

  3. Neko and Aki's Prank CodeForces - 1152D (括号序列,dp)

    大意: 将所有长度为2*n的合法括号序列建成一颗trie树, 求trie树上选出一个最大不相交的边集, 输出边集大小. 最大边集数一定不超过奇数层结点数. 这个上界可以通过从底层贪心达到, 所以就转化 ...

  4. java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification

    ListView UI重绘时触发layoutChildren, 此时会校验listView的mItemCount与其Adapter.getCount是否相同,不同报错. ListView.layout ...

  5. How React Works (一)首次渲染

    How React Works (一)首次渲染 一.前言 本文将会通过一个简单的例子,结合React源码(v 16.4.2)来说明 React 是如何工作的,并且帮助读者理解 ReactElement ...

  6. Tomcat性能调优-让小猫飞奔

    一.总结前一天的学习 从“第三天”的性能测试一节中,我们得知了决定性能测试的几个重要指标,它们是: ü   吞吐量 ü   Responsetime ü   Cpuload ü   MemoryUsa ...

  7. 如何VMare虚拟机里安装Mac操作系统(图文详解)

    不多说,直接上干货! 大部分用户玩的是windows,现在,跟随我来玩玩Mac. 1. VMware Workstation 11 2. unlocker 206(for OS X 插件补丁),这是V ...

  8. jieba分词加入特殊字符和空格

    将原始jieba字典中的空格更改为 @@ 1.原来:雅不可攀 3 nr 2.现在:雅不可攀@@3@@nr 将自定义用户词典加入的词与词性中间的空格更改为@@ 1.原来:牵连关系 50 n 2.现在:牵 ...

  9. 编程开发(C/C++&Java&Python&JavaScript&Go&PHP&Ruby&Perl&R&Erlang)

    使用Docker快速部署主流编程语言的开发.编译环境及其常用框架,包括C.C++.Java.Python.JavaScript.Go.PHP.Ruby.Perl.R.Erlang等. 在今后采用编程语 ...

  10. 《深入理解Java虚拟机》目录

    第一部分 走进Java 第1章 走进Java   第二部分 自动内存管理机制 第2章 Java内存区域与内存溢出异常 2.2 运行时数据区域 2.3 HotSpot虚拟机对象探秘 第3章 垃圾收集器与 ...