zoukankan      html  css  js  c++  java
  • (译)"usermod"命令使用完全指导---15个练习例程截图

    "usermod"命令使用完全指导---15个练习例程截图

     

    在Unix/Linux各个发行版,‘usermod’通过命令行常常用来修订和改变一个现存用户的任何属性.‘usermod’命令类似于‘useradd’与‘adduser’,但是‘usermod’只能用于现存用户。

    usermod Command Examples

    15 usermod Command Examples

    ‘useradd’与‘adduser’被用于在linux操作系统中创建用户.若要了解更多关于如何创建系统用户,请阅读如下指南:

    1. “useradd”命令完全指南:

    在创建用户账号后,有的情况下我们需要改变现存用户账号的属性.比如,改变用户的主目录,登录名,登录shell,密码过期时间,等待,这种情况下‘usermod’就派上用场了。

    当我们在终端使用‘usermod’命令时,如下相关文件将会被该命令执行后影响。

    1. /etc/passwd –用户账号信息文件.
    2. /etc/shadow – 用户账号安全文件.
    3. /etc/group – 群组信息文件.
    4. /etc/gshadow – 群组安全文件.
    5. /etc/login.defs – 设置用户账号限制文件..

    命令的基本语法:

    usermod [options] username
    

    要求

    1. 此命令只能针对现存的用户账号
    2. 只有超级用户才能执行此命令
    3. 此命令适用于任何Linux发行版本
    4. 你必须了解该命令各个选项的作用

    ‘Usermod’ 命令的选项

    The ‘usermod‘ command is simple to use with lots of options to make changes to an existing user. Let us see how to use usermod command by modifying some existing users in Linux box with the help of following options.

    ‘usermod’命令的丰富选项很容易改变现存用户的属性.在如下命令选项的帮助下我们将见证如何使用usermod命令修改现存的用户属性。

    1. -c = 修改用户账号的备注信息.
    2. -d = 修改用户登录的目录.
    3. -e = 修改账号的有效期限.
    4. -g = 修改用户所属的群组.
    5. -G = 修改用户所属的附加群组.
    6. -l = 修改用户账号名.
    7. -L = 锁定用户密码使密码无效.
    8. -m = 将现存主目录的内容移动到新的目录中
    9. -p = 修改用户账号密码(不安全).
    10. -s = 指定用户shell.
    11. -u = 修改用户ID.
    12. -U = 解锁.

    In this article we will see ‘15 usermod commands‘ with their practical examples and usage in Linux, which will help you to learn and enhance your command-line skills using these options.

    在这篇文章中将看到关于15个usermod命令的实际例子以及在linux中的使用,这会帮助你学习和提高使用该命令选项的技巧

    1. 增加用户账号的备注

    The ‘-c‘ option is used to set a brief comment (information) about the user account. For example, let’s add information on ‘tecmint‘ user, using the following command.

    ‘-c’选项通常用来对用户账户作一个简洁的备注.例如,例如我们用如下的命令为‘tecmint’用户添加备注。

    # usermod -c "This is Tecmint" tecmint
    

    在为用户账号添加备注后,相应的在/ect/passwd会添加相应的信息.

    # grep -E --color 'tecmint' /etc/passwd
    
    tecmint:x:500:500:This is Tecmint:/home/tecmint:/bin/sh
    
    Add User Information in Linux

    Add Information to User

    2. 改变用户的主目录

    在如上的步骤中我们可以看出我们的主目录在/home/tecmint/之下,如果我们需要将该目录改变为其他目录我们需要用到命令行选项'-d'

    例如,我们想将用户主目录改变为/var/www/,在那之前,我们需要使用如下命令检查用户的当前主目录

    # grep -E --color '/home/tecmint' /etc/passwd
    
    tecmint:x:500:500:This is Tecmint:/home/tecmint:/bin/sh

    现在,如下命令将用户主目录从/home/tecmint改变为/var/www/.改变之后我们仍然需要确认用户主目录是否已经改变

    # usermod -d /var/www/ tecmint
    # grep -E --color '/var/www/' /etc/passwd
    
    tecmint:x:500:500:This is Tecmint:/var/www:/bin/sh
    
    Change User Home Directory

    Change User Home Directory

    3. 设置用户账号过期日期

    '-e'选项被用来设置用户账号的过期日期(日期格式为YYYY-MM-DD).在此之前,让我们先使用'chage'命令找出账户当前的账号过期时间。

    # chage -l tecmint
    
    Last password change					: Nov 02, 2014
    Password expires					: never
    Password inactive					: never
    Account expires						: Dec 01, 2014
    Minimum number of days between password change		: 0
    Maximum number of days between password change		: 99999
    Number of days of warning before password expires	: 7
    

    The expiry status of a ‘tecmint‘ user is Dec 1 2014, let’s change it to Nov 1 2014 using ‘usermod -e‘ option and confirm the expiry date with ‘chage‘ command.

    用户'tecmint'账号过期时间为Dec 1 2014,现在使用'usermod -e'将其改为Nov 1 2014并且使用'chage'命令确认是否得以改变。

    # usermod -e 2014-11-01 tecmint
    # chage -l tecmint
    
    Last password change					: Nov 02, 2014
    Password expires					: never
    Password inactive					: never
    Account expires						: Nov 01, 2014
    Minimum number of days between password change		: 0
    Maximum number of days between password change		: 99999
    Number of days of warning before password expires	: 7
    
    Add User Expiry Date in Linux

    Set User Account Expiry Date

    4. 改变用户主群组

    为了设置或改变用户的主群组,我们使用usermod命令的'-g'选项,在此之前,我们应该先确认用户的当前群组为tecmint_test

    # id tecmint_test
    
    uid=501(tecmint_test) gid=502(tecmint_test) groups=502(tecmint_test)

    如家,将主群组改变为babin并确认依据改变了

    # usermod -g babin tecmint_test
    # id tecmint_test
    
    uid=501(tecmint_test) gid=502(babin) groups=502(tecmint_test)
    
    Change User Group in Linux

    Change User Primary Group

    5. 对现存用户附加群组

    如果你想为tecmint用户添加一个新的名为tecmint_test()群组,你可以使用usermod命令的'-G'选项。

    # usermod -G tecmint_test0 tecmint
    # id tecmint
    
    Add Group to User in Linux

    Add Group to User

    注意:当心,当仅仅使用'-G'选项添加新群组到现存的用户中将会移除用户所属的现存的群组。所以,通过将'-a'与'-G'选项一起使用来为用户增加新的群组。

    6. Adding Supplementary and Primary Group to User

    如果你需要增加一个附加群组,你可以使用'-a'与'-G'选项.比如,现在要为tecmint_test()添加一个附加群组wheeluser

    # usermod -a -G wheel tecmint_test0
    # id tecmint_test0

    因此,用户tecmint_test0保持其主要组以及在中学)。这将使我的普通用户帐户执行在Linux系统中任何root权限的命令。

    eg : sudo service httpd restart
    
    Add Multiple Groups to User

    Add Multiple Groups to User

    7. 改变用户账号登陆名

    为了改变任何现存的用户登陆名,我们可以使用'-l'选项.在如下的例子中,我们将登陆名tecmint改为tecmint_admin。因此用户名tecmint已经被tecming_admin重新命名了。

    # usermod -l tecmint_admin tecmint

    现在我们如何检查tecmint用户,该用户将不会存在因为我们已经将用户名改为tecmint_admin

    # id tecmint

    检查用户名tecmint_admin的账户,你将发现该用户名的UID与群组与之前tecmint是一样的

    # id tecmint_admin
    
    Change User Login Name in Linux

    Change User Login Name

    8. 锁定用户账号

    To Lock any system user account, we can use ‘-L‘ (lock) option, After the account is locked we can’t login by using the password and you will see a ! added before the encrypted password in /etc/shadow file, means password disabled.

    为了锁定任何系统用户账号,我们可以使用'-L'选项,当账号被锁定后我们不能使用密码登陆,并且你在/etc/shadow文件该用户的行中的加密密码项之前会发现一个感叹号“!”,其意思是密码不可用

    # usermod -L babin

    现在检查这个锁定的账号

    # grep -E --color 'babin' cat /etc/shadow
    
    Lock User Account in Linux

    Lock User Account

    9. 解除账号密码锁定

    ‘-U’选项被用于解锁任何被锁定的用户账号,该操作也会移除之前出现的感叹号‘!’

    # grep -E --color 'babin' /etc/shadow
    # usermod -U babin
    

    让我们证实下之前的感叹号是否已经被移除了。

    # grep -E --color 'babin' /etc/shadow
    
    Unlock User Account in Linux

    Unlock User Account

    10. 将用户目录移到新的目录

    你的账号已经有了‘/home/pinky’的主目录,你想要将主目录改为‘/var/pinky’.你可以使用'-d'与'-m’选项将现存主目录的的用户文件移动到新的目录中.

    检查用户账号及其主目录

    # grep -E --color 'pinky' /etc/passwd

    然后列出主目录的用户文件

    # ls -l /home/pinky/

    现在我们将把主目录从/home/pinky移动到/var/pinky

    # usermod -d /var/pinky/ -m pinky
    

    接下来,确认其已经做出改变

    # grep -E --color 'pinky' /etc/passwd

    检查目录/home/pinky下的文件。这里因为我们使用了'-m'选项移动文件因此该目录不再存在文件.用户的文件将会存在于/var/pinky目录下

    # ls -l /home/pinky/
    # ls -l /var/pinky/
    
    Move User Home Directory in Linux

    Move User Home Directory

    11. 为用户创建未加密的密码

    为了创建未加密的密码,我们使用'-p'选项.为了证实我们的目的,我将为Pinky用户设置一个名为'redhat'的新密码

    # usermod -p redhat pinky

    在设置密码之后,现在来检测/etc/shadow文件以确认其密码是加密还是没加密

    # grep -E --color 'pinky' /etc/shadow
    
    Create Unencrypted User Password in Linux

    Create Unencrypted User Password

    Note:你看到上图了吗,密码清晰的对每个人可见,因此,该选项不推荐大家使用.

    12. 改变用户的shell

    用户的登录shell可以使用命令'useradd'在用户创建的时候被改变和定义,也可以使用'usermod'的'-s'选项改变.例如,’babin‘用户的默认shell为

    /bin/bash,现在我想要将其改为/bin/sh

    # grep -E --color 'babin' /etc/passwd
    # usermod -s /bin/sh babin

    在改变了用户的登录shell之后,使用如下命令确认用户的当前shell

    # grep -E --color 'babin' /etc/passwd
    
    Change User Login Shell in Linux

    Change User Login Shell

    13. 改变用户ID

    在下面的例子中,你可以看出'babin'用户的uid为502,现在我想将其改为888,UID可用的范围为(0-999)

    # grep -E --color 'babin' /etc/passwd
    OR
    # id babin

    现在,让我们使用'-u'选项改变babin用户的UID并证实这一改变

    # usermod -u 888 babin
    # id babin
    
    Change User UID in Linux

    Change User UID

    14. 使用多个选项修改用户账号属性

    我想将现存的用户jack的主目录,shell,账号有效日期,标签,UID以及群组马上做出改变,但是我将使用一条命令做出所有改变

    用户jack的默认主目录为/home/jack,现在我想将其改为/var/www/html 并且将其shell定位bash shell,设置账号有效日期至Deceber 10th 2014,增加新的备注This is jack,UID该位555,群组改为apple

    现在让我们看看到底如何一次性修改

    # usermod -d /var/www/html/ -s /bin/bash -e 2014-12-10 -c "This is Jack" -u 555 -aG apple jack
    

    确认其主目录,登录shell

    # grep -E --color 'jack' /etc/passwd
    

    确认其账号有效日期

    # chage -l jack
    

    确认其所属群组

    # grep -E --color 'jack' /etc/group
    
    Using Multiple Options with usermod

    Using Multiple Options with usermod

    15. 改变账号的UID与GID

    我们可以改变当前用户的UID与GID。因为改变新的GID我们需要一个已经存在的群组,现在已经有一个GID为777名为orange的群组了

    现在用户jack想要将UID改为666 GID改为777

    确认当前的UID与GID已经被修改

    # id jack

    修改UID于GID

    # usermod -u 666 -g 777 jack
    

    确认其已经改变

    # id jack
    
    Change User GID

    Change User UID and GID

    结论

    这里我们已经详细的呈现了如何使用usermod命令及其选项,在学习usermod命令之前,读者应该首先了解useradd命令及其选项。如果在本文中有任何疏漏请通过评论让我知晓,请各位同学千万不要吝啬你们的意见。



    转载请注明:小刘

    译文不容易,不作伸手党

  • 相关阅读:
    更多的bash shell命令
    Docker(一)Docker概述
    SpringCloud(一)版本选择
    Scala 技术笔记之 可变长参数
    嵌入式 ThriftServer in Spark
    Spark 代码走读之 Cache
    Scala 技术笔记之 Option Some None
    Spark作业执行
    Shuffle
    Jetty初探
  • 原文地址:https://www.cnblogs.com/vpoet/p/4659681.html
Copyright © 2011-2022 走看看