Ubuntu Desktop Server - 添加用户 / 删除用户和添加 sudo 权限

1. 添加新用户 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 user-name

新用户添加成功后,使用 ls /home 检查新用户文件夹。

amax@amax-server:~$ 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
amax@amax-server:~$
strong@strong-sys:~$ sudo adduser yongqiang
[sudo] strong 的密码: 
正在添加用户"yongqiang"...
正在添加新组"yongqiang" (1001)...
正在添加新用户"yongqiang" (1001) 到组"yongqiang"...
创建主目录"/home/yongqiang"...
正在从"/etc/skel"复制文件...
输入新的 UNIX 密码: 
重新输入新的 UNIX 密码: 
passwd:已成功更新密码
正在改变 yongqiang 的用户信息
请输入新值,或直接敲回车键以使用默认值
	全名 []: yongqiang
	房间号码 []: 
	工作电话 []: 
	家庭电话 []: 
	其它 []: 
这些信息是否正确? [Y/n] Y
strong@strong-sys:~$ 
strong@strong-sys:~$ cd /home/
strong@strong-sys:/home$ ls
strong  yongqiang
strong@strong-sys:/home$ 
strong@strong-sys:/home$ cd ~
strong@strong-sys:~$

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.

2. 用户 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

strong@strong-sys:~$ sudo vim /etc/sudoers
[sudo] strong 的密码: 
strong@strong-sys:~$ 
strong@strong-sys:~$ cat /etc/sudoers
cat: /etc/sudoers: 权限不够
strong@strong-sys:~$ 
strong@strong-sys:~$ 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
strong@strong-sys:~$

原始文件信息:

deepnorth@deepnorth-amax:~$ cat /etc/sudoers
cat: /etc/sudoers: Permission denied
deepnorth@deepnorth-amax:~$
deepnorth@deepnorth-amax:~$ 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

# 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
deepnorth@deepnorth-amax:~$

3. 删除用户

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 来实现。

4. useradd - adduser

useradd / adduser 命令用来建立用户帐号,使用权限是超级用户。

利用 adduser 创建新用户 (adduser + username),在 /home 目录下会自动创建同名文件夹。

useradd 仅仅创建了一个用户名 (useradd + username),并没有在 /home 目录下创建同名文件夹,也没有创建密码。因此利用这个用户登录系统,是登录不了的。

可以用 (useradd -m + username) 的方式创建,它会在 /home 目录下创建同名文件夹,然后利用 (passwd + username)为指定的用户名设置密码。

5. 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

6. 查看 Ubuntu 系统中的用户

  • grep bash /etc/passwd
(base) yongqiang@yongqiang:~$ grep bash /etc/passwd
root:x:0:0:root:/root:/bin/bash
yongqiang:x:1000:1000:,,,:/home/yongqiang:/bin/bash
(base) yongqiang@yongqiang:~$
  • getent passwd

  • cat /etc/passwd

References

[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/

Ubuntu 20.04 Desktop版本中,可以使用Samba来搭建文件服务器。以下是安装和配置Samba的步骤: 1. 安装Samba 在终端输入以下命令来安装Samba: ``` sudo apt install samba ``` 2. 创建共享目录 在Samba中,需要将要共享的目录配置为共享目录。可以在文件管理器中创建一个目录,用于共享。 例如,在文件管理器中创建一个名为“share”的目录,在终端中进入该目录,然后输入以下命令将其设置为共享目录: ``` sudo chmod 777 share/ sudo chown nobody:nogroup share/ ``` 其中,chmod命令用于设置文件或目录的权限,777表示所有用户都有读、写、执行权限;chown命令用于设置文件或目录的所有者和所属组,nobody:nogroup表示该目录的所有者和所属组都为nobody。 3. 配置Samba 在终端输入以下命令编辑Samba的配置文件: ``` sudo vim /etc/samba/smb.conf ``` 在文件的最后添加以下内容: ``` [share] comment = Ubuntu File Server Share path = /home/username/share browsable = yes read only = no guest ok = yes ``` 其中,[share]表示共享的名称,comment是注释,path表示共享的目录路径,browsable表示是否可以浏览该共享,read only表示是否只读,guest ok表示是否允许匿名访问。 将“username”替换为你的用户名。 在Samba配置文件中添加上述内容后,保存并退出。 4. 重启Samba 在终端输入以下命令重启Samba服务: ``` sudo systemctl restart smbd.service ``` 5. 访问共享 在Windows或其他计算机中,可以通过网络访问该共享。打开文件管理器,输入以下地址: ``` \\ubuntu_ip_address\share ``` 其中,ubuntu_ip_address是Ubuntu计算机的IP地址,share是共享的名称。 如果需要输入用户名和密码才能访问共享,可以在Samba配置文件中进行配置。 至此,在Ubuntu 20.04 Desktop版本上搭建文件服务器的操作流程就结束了。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yongqiang Cheng

梦想不是浮躁,而是沉淀和积累。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值