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

更新日期: 2020-11-14 阅读次数: 12075 字数: 333 分类: Linux

避免所有操作和部署都使用 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 不单新建了用户,还新建了

  • 一个对应的同名用户组
  • 同名的 home 目录

添加新用户到 sudo 用户组

usermod -aG sudo YOUR_USER_NAME 

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

# groups elephant
elephant : elephant sudo

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

  • elephant
  • sudo

测试新用户 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

tags: ubuntu 20.04

关于作者 🌱

我是来自山东烟台的一名开发者,有敢兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊, 查看更多联系方式