当前位置: 首页>>技术教程>>正文


列出所有用户的命令?以及如何添加,删除,修改用户?

, ,

问题描述

我需要一个命令来列出终端中的所有用户。以及如何从终端添加,删除和修改用户。

这可能有助于终端轻松管理您的帐户。

最佳解决方案

列出您可以使用的所有本地用户:

cut -d: -f1 /etc/passwd

要列出所有能够进行身份验证的用户(包括non-local),请参阅此回复:https://askubuntu.com/a/414561/571941

一些更有用的user-management命令(也限于本地用户):

要添加新用户,您可以使用:

sudo adduser new_username

要么:

sudo useradd new_username

另见:What is the difference between adduser and useradd?

要删除/删除用户,首先您可以使用:

sudo userdel username

然后,您可能需要删除已删除的用户帐户的主目录:

sudo rm -r /home/username

(请谨慎使用上述命令!)

要修改用户的用户名:

usermod -l new_username old_username

要更改用户的密码:

sudo passwd username

要更改用户的shell:

sudo chsh username

要更改用户的详细信息(例如真实姓名):

sudo chfn username

当然,另请参阅:man adduserman useraddman userdel …等等。

次佳解决方案

只需按下键盘上的Ctrl + Alt + T即可打开终端。打开时,运行下面的命令:

cat /etc/passwd

要么

less /etc/passwd
more /etc/passwd

您也可以使用awk:awk

awk -F':' '{ print $1}' /etc/passwd

第三种解决方案

获取此类信息的最简单方法是getent – 请参阅getent命令的手册页。虽然该命令提供与cat /etc/passwd相同的输出,但记住它很有用,因为它会给出您在OS中的几个元素的列表。

command-line,user-management,ubuntu

要获取您键入的所有用户的列表(因为用户列在/etc/passwd中)

getent passwd

要将用户新用户添加到系统中,您需要输入

sudo adduser newuser

创建应用了所有默认设置的用户。

奖励:将任何用户(例如anyuser)添加到组(例如cdrom)类型

sudo adduser anyuser cdrom

您可以删除一个用户(例如过时)

sudo deluser obsolete

如果你想删除他的主目录/邮件以及你输入

sudo deluser --remove-home obsolete

sudo deluser --remove-all-files obsolete

将在整个系统中删除用户和该用户拥有的所有文件。

第四种方案

在大多数正常情况下,这应该会得到所有正常的(non-system,不怪异等)用户:

awk -F'[/:]' '{if ($3 >= 1000 && $3 != 65534) print $1}' /etc/passwd

这工作:

  • /etc/passwd读入

  • 使用:作为分隔符

  • 如果第三个字段(用户ID号)大于1000而不是65534,则打印第一个字段(用户的用户名)。

这是因为在许多Linux系统上,超过1000的用户名被保留给非特权用户(你可以说是普通用户)。关于这个here的一些信息:

A user ID (UID) is a unique positive integer assigned by a Unix-like operating system to each user. Each user is identified to the system by its UID, and user names are generally used only as an interface for humans.

UIDs are stored, along with their corresponding user names and other user-specific information, in the /etc/passwd file…

The third field contains the UID, and the fourth field contains the group ID (GID), which by default is equal to the UID for all ordinary users.

In the Linux kernels 2.4 and above, UIDs are unsigned 32-bit integers that can represent values from zero to 4,294,967,296. However, it is advisable to use values only up to 65,534 in order to maintain compatibility with systems using older kernels or filesystems that can only accommodate 16-bit UIDs.

The UID of 0 has a special role: it is always the root account (i.e., the omnipotent administrative user). Although the user name can be changed on this account and additional accounts can be created with the same UID, neither action is wise from a security point of view.

The UID 65534 is commonly reserved for nobody, a user with no system privileges, as opposed to an ordinary (i.e., non-privileged) user. This UID is often used for individuals accessing the system remotely via FTP (file transfer protocol) or HTTP (hypertext transfer protocol).

UIDs 1 through 99 are traditionally reserved for special system users (sometimes called pseudo-users), such as wheel, daemon, lp, operator, news, mail, etc. These users are administrators who do not need total root powers, but who perform some administrative tasks and thus need more privileges than those given to ordinary users.

Some Linux distributions (i.e., versions) begin UIDs for non-privileged users at 100. Others, such as Red Hat, begin them at 500, and still others, such Debian, start them at 1000. Because of the differences among distributions, manual intervention can be necessary if multiple distributions are used in a network in an organization.

Also, it can be convenient to reserve a block of UIDs for local users, such as 1000 through 9999, and another block for remote users (i.e., users elsewhere on the network), such as 10000 to 65534. The important thing is to decide on a scheme and adhere to it.

Among the advantages of this practice of reserving blocks of numbers for particular types of users is that it makes it more convenient to search through system logs for suspicious user activity.

Contrary to popular belief, it is not necessary that each entry in the UID field be unique. However, non-unique UIDs can cause security problems, and thus UIDs should be kept unique across the entire organization. Likewise, recycling of UIDs from former users should be avoided for as long as possible.

第五种方案

所有可以登录的用户列表(没有像bin,deamon,mail,sys等系统用户)

awk -F':' '$2 ~ "\$" {print $1}' /etc/shadow

添加新用户

sudo adduser new_username

要么

sudo useradd new_username

删除/删除用户名

sudo userdel username

如果你想删除主目录(默认目录/home /username)

sudo deluser --remove-home username

要么

sudo rm -r /path/to/user_home_dir

如果你想从这个用户的系统中删除所有文件(不仅是家庭指令)

sudo deluser --remove-all-files

第六种方案

您也可以使用compgen 内置:

compgen -u

将列出所有用户。

第七种方案

好的,这里有一个技巧可以帮助你分类。如果您键入用户并按Tab键两次,终端会自动完成,它将列出用户存在的所有命令作为前4个字符。

user (tab tab)

给我尽可能的选择useradd userdel usermod用户users-admin如果你想知道更多关于一个命令谷歌它或键入man man useradd给useradd – 创建一个新的用户或更新默认的新用户信息……

列出用户应该与Mitch所说的一致。

希望能帮助我在bash中完成标签填充,从而避免我记住事物。

第八种方案

要找出机器上/home-folder中具有home-directories的用户,请运行以下命令

cd /home
ls 

然后您可以看到有权登录到服务器的用户。如果我们想查看任何用户的文件,您必须是root用户。

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/17.html,未经允许,请勿转载。