带有示例Linux usermod命令教程

Linux provides usermod command by default for the most of the distributions. This command is used to user modification. After user creation the user related information, values and policies can be changed with usermod command. In this tutorial we will look various features of usermod command with examples.

默认情况下,Linux为大多数发行版提供usermod命令。 此命令用于用户修改。 创建用户后,可以使用usermod命令更改与用户相关的信息,值和策略。 在本教程中,我们将通过示例介绍usermod命令的各种功能。

句法 (Syntax)

Syntax of the usermod command is like below.

usermod命令的语法如下。

usermod [options] LOGIN

帮帮我 (Help)

$ usermod -h
Usermod Help
Usermod Help
Usermod帮助

添加评论或信息(Add Comment or Information)

User information is stored in a file named passwd . This file is located as /etc/passwd . In the ancient days of Linux and Linux this file is also used to store users password. But storing user information and user credential created security problems and passwords are stored in a separate file named shadown in the same directory etc .

用户信息存储在名为passwd的文件中。 该文件位于/etc/passwd 。 在Linux和Linux的远古时代,此文件还用于存储用户密码。 但是,存储用户信息和用户认证创建的安全问题和密码存储在名为单独的文件shadown在同一目录etc

There is a columns used to store information about the user. This information can be anything like First name, surname, the birthday etc.

有一个列用于存储有关用户的信息。 这些信息可以是名字,姓氏,生日等。

$ usermod -c "İsmail Baydan " ismail
Add Comment or Information
Add Comment or Information
添加评论或信息

We have set the comment for the user ismail as İsmail Baydan . We can check the modification with grep command by filtering line contains the user name ismail.

我们为用户ismail设置的注释为İsmail Baydan 。 我们可以通过过滤包含用户名ismail的行来使用grep命令检查修改。

$ grep ismail /etc/passwd

更改主目录 (Change Home Directory)

Human users generally have home directory for storing data, file, media etc for their personal or corporate usage. This home directory is generally reside in /home in Linux root file system.

人类用户通常具有主目录,用于存储数据,文件,媒体等以供个人或公司使用。 此主目录通常位于Linux根文件系统中的/home中。

For example user ismail will have the /home/ismail directory as his own home directory for the most the Linux systems. This value can be changed after creation for some reasons like we need to set and application folder for an application user like apache .

例如,对于大多数Linux系统,用户ismail会将/home/ismail目录作为自己的主目录。 由于某些原因(例如我们需要为apache类的应用程序用户设置和应用程序文件夹),可以在创建后更改此值。

LEARN MORE  Linux pwd Command Tutorial With Examples
了解更多Linux pwd命令教程和示例

In this example we will change the home directory of user nick . We will set /mnt as nick’s home directory like below.

在此示例中,我们将更改用户nick的主目录。 我们将/mnt设置为nick的主目录,如下所示。

$ sudo usermod -d "/mnt " nick
Change Home Directory
Change Home Directory
更改主目录

As we can see from grep command output column number 6 is /mnt which is nick’s new home directory.

从grep命令输出中可以看到,第6列是/mnt ,这是nick的新主目录。

设置用户帐户有效期 (Set User Account Expiry Date)

Some users are generally created in a hurry for temporary usage and than they forgotten and resides in the system as an active account. This is an security vulnerability and should be avoided with practical solution. One of the action to take to prevent this security issue is setting account expiry date for required accounts. Expire date will be specified in YYYY-MM-DD format as 2017-02-28 in this example for the user nick

通常会急于创建一些用户以供临时使用,然后他们忘记了这些用户,并将其作为活动帐户驻留在系统中。 这是一个安全漏洞,应通过实际解决方案避免。 防止此安全问题采取的措施之一是设置所需帐户的帐户到期日期。 在本示例中,用户昵称的到期日期将以YYYY-MM-DD格式指定为2017-02-28

$ sudo usermod -e 2017-02-28 nick
Set User Account Expiry Date
Set User Account Expiry Date
设置用户帐户有效期

And we will check the account expire date with the following command for the user nick.

我们将使用以下命令为用户昵称检查帐户过期日期。

$ sudo chage -l nick

显示用户组 (Show User’s Groups)

As like in the Windows operating systems in Linux operating systems user accounts are associated with groups. There are two type of user group. First type is primary group and each user account have single primary user group. Other type of group is secondary groups where each user may have more than one group. Primary groups of existing users can be changed with usermod command easily.

就像Linux操作系统中的Windows操作系统一样,用户帐户与组关联。 用户组有两种类型。 第一种是主要组,每个用户帐户都有一个主要用户组。 其他类型的组是辅助组,其中每个用户可以有多个组。 可以使用usermod命令轻松更改现有用户的主要组。

$ id nick
Show User's Groups
Show User’s Groups
显示用户组

更改用户主要组(Change User Primary Group)

Previously we have printed the user group. Now we will change the user primary group more detailed information about primary group can be found previous example.

以前我们已经打印了用户组。 现在,我们将更改用户主组,可以在前面的示例中找到有关主组的更多详细信息。

In this example we will add user nick to the root group.

在此示例中,我们将用户昵称添加到根组。

$ sudo usermod -g root nick
Change User Primary Group
Change User Primary Group
更改用户主要组

将组添加到现有用户(Adding Group To Existing User)

There is two type of supplementary or secondary group addition. First we will specify the supplementary groups for the user and previous supplementary group memberships of the user will be removed if old groups do not presented. Which simply means the user will have only specified groups and all old groups will be reset.

有两种类型的补充或辅助组添加。 首先,我们将为用户指定补充组,如果未显示旧组,则将删除用户以前的补充组成员身份。 这只是意味着用户将只有指定的组,所有旧组将被重置。

$ usermod -G avahi,test nick
Adding Group To Existing User
Adding Group To Existing User
将组添加到现有用户

And the second way we will add new supplementary groups to the user but old groups will be preserved. For his operations we will use extra option -a like below.

第二种方式,我们将向用户添加新的补充组,但保留旧的组。 对于他的操作,我们将使用额外的选项-a如下所示。

$ usermod -a -G games nick
Adding Group To Existing User
Adding Group To Existing User
将组添加到现有用户

更改用户登录名(Change User Login Name)

User accounts have login names to authentication themselves. In Linux operations systems users are identified by their user ID’s. User names are used an easy way to remember and use. So A user account user ID can not be changed after creation but the user name can be changed easily. In this example we will change user name nick to the nick1 with option -l .

用户帐户具有用于身份验证的登录名。 在Linux操作系统中,用户通过其用户ID进行标识。 使用用户名可以方便地记住和使用。 因此,创建后无法更改用户帐户用户ID,但可以轻松更改用户名。 在此示例中,我们将使用选项-l将用户名nick更改为nick1

$ sudo usermod -l nick1 nick
Change User Login Name
Change User Login Name
更改用户登录名

锁定用户帐号(Lock User Account)

Some times we may need to lock some users for various reasons like security, holiday, test etc. User locking means the account will be inactive up to unlock operations and will be no user login for this period. In this example we will lock user nick1 with the -L option.

有时,出于各种原因(例如安全性,假期,测试等),我们可能需要锁定某些用户。用户锁定意味着该帐户将无法激活以解锁操作,并且在此期间将无用户登录。 在此示例中,我们将使用-L选项锁定用户nick1

$ sudo usermod -L nick1
Lock User Account
Lock User Account
锁定用户帐号

As we can see from screenshot in the shadow file a ! is added which simply symbolize the account is locked.

正如我们从影子文件中的屏幕截图中看到的! 添加,仅表示帐户已被锁定。

LEARN MORE  How To Set or Change User Password In Linux?
了解更多如何在Linux中设置或更改用户密码?

解锁用户帐号 (Unlock User Account)

Now in the previous example we have locked the account and we want to unlock the account named nick1. Simply provide -U options to the usermod command like below.

现在,在前面的示例中,我们已经锁定了帐户,并且想要解锁名为nick1的帐户。 只需为usermod命令提供-U选项,如下所示。

$ sudo usermod -U nick1

更改用户外壳 (Change User Shell)

In Linux user accounts are associated with shells. Shell is the application which interface with user and operating system. There are different type of shells used in Linux community. But the most popular shell is bash and generally used for most Linux distributions. This default shell for the user account can be changed with -s options.

在Linux中,用户帐户与shell关联。 Shell是与用户和操作系统交互的应用程序。 Linux社区中使用了不同类型的外壳。 但是最受欢迎的外壳是bash ,通常用于大多数Linux发行版。 可以使用-s选项更改用户帐户的默认外壳程序。

In this example we will change the shell of user account nick1 with sh shell.

在此示例中,我们将使用sh shell更改用户帐户nick1的外壳。

$ sudo usermod -s "/bin/sh" nick1
Change User Shell
Change User Shell
更改用户外壳

变更使用者编号(Change User ID)

User id of and account can be changed too. This change operation is done with -u option like below. In this example we will change the user nick1 user ID to the 1234 .

和的用户ID也可以更改。 此更改操作通过如下的-u选项完成。 在此示例中,我们将用户nick1用户ID更改为1234

$ sudo usermod -u 1234  nick1
Change User ID
Change User ID
变更使用者编号

翻译自: https://www.poftut.com/linux-usermod-command-tutorial-examples/

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值