ubuntu 20.04 添加新用户,并赋予 sudo 权限

发布时间: 2020-11-14 15:28:27 作者: 大象笔记

避免所有操作和部署都使用 root 用户,以免被利用漏洞被入侵系统。 所以在新购的 Ubuntu 20.04 服务器上,部署服务之前,我需要先新建一个用户, 并赋予 sudo 权限。

然后禁用 root 用户远程登录服务器。

使用 root 登录服务器

ssh root@YOUR_SERVER_IP

adduser

adduser YOUR_USER_NAME 

例如新建一个名为 elephant 的账号,按照提示依次填写信息即可。

# adduser elephant
Adding user `elephant' ...
Adding new group `elephant' (1000) ...
Adding new user `elephant' (1000) with group `elephant' ...
Creating home directory `/home/elephant' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for elephant
Enter the new value, or press ENTER for the default
        Full Name []: Elephant Leg 
        Room Number []: 1024 
        Work Phone []: 133xxxxxxxx
        Home Phone []: 133xxxxxxxx
        Other []:
Is the information correct? [Y/n] Y

可以看到 adduser 不单新建了用户,还新建了

添加新用户到 sudo 用户组

usermod -aG sudo YOUR_USER_NAME 

执行完之后,可以确认用户是否在 sudo 用户组中,例如:

# groups elephant
elephant : elephant sudo

可以看到 elephant 这个用户属于两个用户组

测试新用户 ssh 远程登录

ssh YOUR_USER_NAME@YOUR_SERVER_IP

果然可以了。

参考

https://www.digitalocean.com/community/tutorials/how-to-create-a-new-sudo-enabled-user-on-ubuntu-20-04-quickstart

我是一名山东烟台的开发者,联系作者