如何在Linux上使用chfn和usermod更改用户数据

A Linux terminal window on a Ubuntu-style laptop.
Fatmawati Achmad Zaenuri/Shutterstock Fatmawati Achmad Zaenuri / Shutterstock

There’s more to a user account than a user name. Learn how to set and change all the metadata associated with a Linux user account from the command line.

用户帐户不只是用户名。 了解如何从命令行设置和更改与Linux用户帐户关联的所有元数据。

名字叫什么? (What’s in a Name?)

If you use a Linux or Unix-like computer, you’ll have a user account. The name given to the account is your user account name. This is the name you use to log in with. It’s also (by default) the name of your login group and the name of your home directory. They all use the same identifier.

如果您使用Linux或类似Unix的计算机,那么您将拥有一个用户帐户。 给该帐户的名称是您的用户帐户名称。 这是您用来登录的名称。 默认情况下,它也是登录组的名称和主目录的名称。 它们都使用相同的标识符。

There is another set of information that can be stored for each user account. Real-world data such as the person’s full name, their office number, and their work telephone number, for example, can be defined and attached to them. Indeed, completely arbitrary snippets of information can be tagged to user accounts.

可以为每个用户帐户存储另一组信息。 例如,可以定义真实世界的数据,例如该人的全名,其办公室号码和其工作电话号码。 实际上,可以将完全任意的信息片段标记到用户帐户。

In practical terms, this will be more useful for system administrators looking after computers that have many user accounts configured on them. But even for the single-user Linux computer, it’s always interesting to know what’s going on beneath the hood.

实际上,这对于系统管理员照顾在其上配置了许多用户帐户的计算机而言更为有用。 但是,即使对于单用户Linux计算机,了解幕后的情况也总是很有趣。

GECOS领域 (The GECOS Field)

At one time, the pioneers of Unix had no printer of their own. They had to spool their print jobs to a General Electric mainframe that was running the General Electric Comprehensive Operating System (GECOS). To accomplish that, the users on the Unix systems needed to store and use the credentials of an account on the GECOS system.

一次,Unix的先驱者没有自己的打印机。 他们不得不将打印作业放到运行通用电气综合操作系统 (GECOS)的通用电气大型机上。 为此,Unix系统上的用户需要在GECOS系统上存储和使用帐户的凭据。

The GECOS field was created to store those credentials. That very niche requirement has long since passed into history, and the GECOS field has been re-purposed to store other data related to the owner of the user account. It may have a new job, but it retains its old name. It is still called the GECOS field.

创建了GECOS字段来存储这些凭据。 这种非常特殊的需求早已成为历史,并且GECOS字段已重新用于存储与用户帐户所有者相关的其他数据。 它可能有新工作,但保留了旧名称。 它仍然被称为GECOS领域。

The field is stored in the /etc/passwd file, along with other information regarding the user account:

该字段与有关用户帐户的其他信息一起存储在/etc/passwd文件中:

  • The account user name.

    帐户用户名。
  • The user ID.

    用户ID。
  • The group ID.

    组ID。
  • The path to the home directory for the user account.

    用户帐户的主目录路径。
  • The shell that is started when the user logs in.

    用户登录时启动的外壳。

This is where the finger command and the pinky command retrieve the information that they display.

这是finger命令和pinky命令检索它们显示的信息的地方。

chfn命令 (The chfn Command)

The chfn (change finger information) allows you to set and alter the information stored within the GECOS field. The information is stored as a comma-separated list within the field.

chfn (更改手指信息)使您可以设置和更改GECOS字段中存储的信息 。 信息以逗号分隔的列表形式存储在该字段中。

The chfn command was already present on Ubuntu 18.04.1 and Manjaro 18.1.0. It had to be installed on Fedora 31. Use this command to install it:

chfn命令已经存在于Ubuntu 18.04.1和Manjaro 18.1.0中。 它必须安装在Fedora 31上。使用以下命令进行安装:

sudo dnf install util-linux-user
sudo dnf install util-linux-user in a terminal window

Let’s see what finger can find out about the owner of the user account “dave.” To get the maximum information, we’ll use the -l (long format) option:

让我们看看finger可以找到有关用户帐户“ dave”的所有者的信息。 为了获得最大的信息,我们将使用-l (长格式)选项:

finger dave -l
finger dave -l in a terminal window

This is what it could find:

这是它可以找到的内容:

Output of finger dave -l in a terminal window

It retrieves the account user name, the home directory, and the default shell. These are all related to the user account. They don’t refer to the real person who uses that account. We can use the chfn command to store information about the actual person.

它检索帐户用户名,主目录和默认外壳程序。 这些都与用户帐户有关 。 他们不是指使用该帐户的真实人。 我们可以使用chfn命令存储有关实际人员的信息。

chfn dave
chfn dave in a terminal window

Using chfn in this way starts a short, interactive process. You are prompted to type in a data value for each piece of information that the GECOS field can hold. The current value for each data value is displayed in brackets []. If you want to keep the current data, you can press the “Enter” key, and the value within the brackets will be retained.

以这种方式使用chfn启动一个简短的交互式过程。 提示您输入GECOS字段可以保存的每条信息的数据值。 每个数据值的当前值显示在方括号[]中。 如果要保留当前数据,可以按“ Enter”键,括号内的值将保留。

The chfn interactive process in a terminal window

The information we entered was:

我们输入的信息是:

  • Room number: 512

    房间号 :512

  • Work phone: 555-4567

    工作电话 :555-4567

  • Home phone: 555-5432

    家庭电话 :555-5432

Completed chfn session in a terminal window

Let’s check that finger picks up that new information:

让我们检查一下finger拾取了新信息:

finger dave -l
finger dave -l in a terminal window

Yes, it does. But you may have noticed that it didn’t prompt to change the full name. It’ll only do that if you use sudo. But we don’t have to go through the whole chfn session again, just because we forgot to use sudo. We can change any of the data values individually.

是的,它确实。 但是您可能已经注意到,它没有提示您更改全名。 仅当您使用sudo时才这样做。 但是我们不必再次经历整个chfn会话,只是因为我们忘记了使用sudo 。 我们可以单独更改任何数据值。

更改单个数据值 (Changing the Individual Data Values)

We can set the full name for the owner of the user account by using the -f (full name) option, together with sudo.

我们可以使用-f (全名)选项以及sudo来设置用户帐户所有者的全名。

sudo chfn -f "Dave McKay" dave
sudo chfn -f "Dave McKay" dave ina terminal window

And if we check once more with finger:

如果我们再次用finger检查:

Output from finger in a terminal window

We can see that the full name has been added.

我们可以看到已经添加了全名。

There are other options to change the room number, home phone number, and work phone number. To change the room (office) number, use the -r (room number) option:

还有其他选项可以更改房间号码,家庭电话号码和工作电话号码。 要更改房间(办公室)号,请使用-r (房间号)选项:

sudo chfn -r 633 dave
sudo chfn -r 633 dave in a terminal window

Note that on some distributions, you might use the -o (office room number) option instead of the -r option. More on this shortly. For now, to see the options for your version of chfn, use:

请注意,在某些发行版中,您可以使用-o (办公房号)选项而不是-r选项。 不久之后会更多。 现在,要查看您的chfn版本的选项,请使用:

man chfn

To change the office telephone number, use the -w (work telephone) option:

要更改办公室电话号码,请使用-w (工作电话)选项:

sudo chfn -w 555-1122 dave
sudo chfn -w 555-1122 dave in a terminal window

And finally, to change the home telephone number use the -h (home telephone number) option:

最后,要更改家庭电话号码,请使用-h (家庭电话号码)选项:

sudo chfn -h 555-6576 dave
sudo chfn -h 555-6576 dave in a terminal window

Let’s use finger again to see if all of those changes have been accepted:

让我们再次使用finger来查看所有这些更改是否已被接受:

finger dave -l
finger dave -l in a terminal window

Success. All of those commands have worked, and the data values have been updated.

成功。 所有这些命令均已起作用,并且数据值已更新。

在其他用户帐户上使用chfn (Using chfn On Other User Accounts)

So much for changing our own information, what about changing the GECOS information for other users? That’s just as easy. You must use sudo when you do, but that’s the only difference. Let’s check what data is set for user account mary:

对于更改我们自己的信息而言,如此之多,如何为其他用户更改GECOS信息呢? 就这么简单。 这样做时必须使用sudo ,但这是唯一的区别。 让我们检查一下为用户帐户mary设置了哪些数据:

finger mary -l
finger mary -l in a terminal window

The usual default information is set for this user account. Let’s add to the information:

为此用户帐户设置了通常的默认信息。 让我们添加到信息中:

sudo chfn mary
sudo chfn mary  in a terminal window

We’ll run through the same session as we did before, being prompted for a data value for each piece of information that can be stored. And because we used sudo , we are asked for the full name data value.

我们将像以前一样进行相同的会话,系统会提示您为每个可以存储的信息提供一个数据值。 并且因为我们使用了sudo ,所以要求我们提供全名数据值。

Completed session for sudo chfn mary in a terminal window

Wait a moment, what’s that last item, called “Other?”

请稍等,最后一个叫做“其他”的项目是什么?

it isn’t supported by all versions of chfn, and you only see it if you use sudo, which is why we didn’t see it earlier when we (deliberately) didn’t use sudo with:

并非所有版本的chfn都支持它,并且只有在使用sudo您才能看到它,这就是为什么当我们(故意)不使用sudo时我们没有看到它的原因:

chfn dave

The “Other” data value can hold anything you like. It doesn’t have any prescribed or expected use.

“其他”数据值可以容纳您喜欢的任何内容。 没有任何规定或预期的用途。

Versions of chfn that support the “Other” field also provide the -o (other) option to change this value directly. It’s because of the conflict between -o for “office number” and -o for “other” that some systems use -r for “room number”.

支持“其他”字段的chfn版本也提供了-o (其他)选项来直接更改此值。 由于-o代表“办公室编号”和-o代表“其他”,因此某些系统将-r用作“房间编号”。

We can see the data stored for user account mary in the /etc/paswd file, by using less:

通过使用less ,我们可以在/etc/paswd文件中看到为用户帐户mary存储的数据:

less /etc/passwd
lass /etc/passwd in a terminal window

On systems that support it (check the man page for your version of chfn ) you can set the “other” field directly using the -o (other) option:

在支持它的系统上(检查您的chfn版本的man页),您可以直接使用-o (其他)选项设置“其他”字段:

sudo chfn -o "HTG Freelancer" dave
sudo chfn -o "HTG Freelancer" dave in a terminal window

And we can check the data for user account dave using the less command:

我们可以使用less命令检查用户帐户dave的数据:

less /etc/passwd
less /etc/p<span data-mce-type=

[asswd in a terminal window” width=”646″ height=”382″ />

[在终端窗口中输入“宽度=“ 646”高度=“ 382” />

That raises an obvious question. If your version of chfn doesn’t support the “Other” field, how do you change it? We can do that with the usermod command.

这就提出了一个明显的问题。 如果您的chfn版本不支持“其他”字段,则如何更改? 我们可以使用usermod命令来做到这一点。

usermod命令 (The usermod Command)

The usermod command allows you to modify aspects of a user account through such actions as adding or remove the user account from groups, and changing their default shell. It can also be used to directly manipulate the GECOS field.

usermod命令可以修改用户帐户的各个方面 ,通过各种行动,如添加或删除用户帐户 ,并改变其默认的shell。 它也可以用来直接操纵GECOS领域。

Let’s take a closer look at the line in the /etc/passwd file for user account mary. We’ll use grep to isolate that line for us. This will work because user names have to be unique. There can only be one user account called mary.

让我们仔细看看用户帐户mary的/etc/passwd文件中的行。 我们将使用grep为我们隔离该行 。 这将起作用,因为用户名必须唯一。 只能有一个名为mary的用户帐户。

grep mary /etc/passwd
grep mary /etc/passwd in a terminal window

The fields in the etc/passwd file are displayed. A colon “:” is used as the field separator. from left to right, the fields are:

显示etc/passwd文件中的字段。 冒号“ : ”用作字段分隔符。 从左到右,这些字段是:

  • The account user name.

    帐户用户名。
  • An “x” which indicates the password for this user account is held encrypted in the /etc/shadow file.

    表示该用户帐户密码的“ x”以加密方式保存在/etc/shadow文件中。

  • The user ID for the mary user account.

    mary用户帐户的用户ID。
  • The group ID for the mary user account.

    mary用户帐户的组ID。
  • The GECOS field.

    GECOS字段。
  • The path to the home directory for the mary user account.

    mary用户帐户的主目录路径。
  • The shell that is started when the owner of the mary account logs in.

    mary帐户所有者登录时启动的外壳。

A comma “,” is used as the separator for the data values within the GECOS field. The values within the GECOS field are, from left to right:

逗号“,”用作GECOS字段中数据值的分隔符。 GECOS字段中的值从左到右为:

  • Full Name.

    全名。
  • Room (or office) number.

    房间(或办公室)号。
  • Work telephone number.

    工作电话号码。
  • Home telephone number.

    家庭电话号码。
  • Other information.

    其他信息。

Note that the usermod command sets the entire GECOS field to the new value you provide. If all you provide is the full name of the person, then the only thing in the GECOS field will be the full name. This means you have to provide any existing data values that you wish to keep.

请注意, usermod命令将整个GECOS字段设置为您提供的新值。 如果您提供的只是该人的全名,那么GECOS字段中唯一的就是全名。 这意味着您必须提供希望保留的所有现有数据值。

Here’s an example. The user Mary has been promoted, and she is going to move to the fourth floor. She’s getting a new office number, a new work telephone number, and we’re going to add in her middle name. Her “Other” field value is also going to change. Although her home telephone number is not going to change, we must provide it in the string to usermod.

这是一个例子。 用户Mary已晋升,她将移至四楼。 她正在获得一个新的办公室号码,一个新的工作电话号码,我们将添加她的中间名。 她的“其他”字段值也将发生变化。 尽管她的家庭电话号码不会更改,但我们必须在字符串中将其提供给usermod

We need to use the -c (comment) option and we need to run the command with sudo.

我们需要使用-c (注释)选项,并且需要使用sudo运行命令。

sudo usermod -c "Mary Carol Quinn,405,5559654,555-7704,Linux Advocate" mary
sudo usermod -c "Mary Carol Quinn,405,5559654,555-7704,Linux Advocate" mary in a terminal window

Using grep to report on the contents of the /etc/passwd file for mary, shows us the new values have been added.

使用grep报告mary的/etc/passwd文件的内容,向我们展示了已添加的新值。

grep mary /etc/passwd
grep mary /etc/passwd in a terminal window

万事俱备 (All is Flux, Nothing Abides)

Inaccurate data is worthless. When the information about people changes—office moves, name changes, role titles—you can easily update their meta-data to reflect the current position.

不准确的数据是毫无价值的。 当有关人员的信息发生更改(办公室移动,姓名更改,职务等)时,您可以轻松地更新其元数据以反映当前职位。

翻译自: https://www.howtogeek.com/449160/how-to-change-user-data-with-chfn-and-usermod-on-linux/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值