zoukankan      html  css  js  c++  java
  • Linux字符界面下用户账户的设置

      在Linux系统字符界面下创建、修改以及删除用户账户主要使用useradd,usermod和userdel这3个命令。

    一.创建用户账户

         创建用户账户就是在系统中创建一个新账户,然后为新账户分配用户UID、用户组群、主目录和登录shell等资源,新创建的用户账户默认是被锁定的,无法使用,需要使用passwd命令设置密码后才能使用。创建用户账户就是在/etc/passwd文件中为新用户增加一条记录,同时更新其他系统文件如/etc/shadow,/etc/group等。

        使用 useradd命令可以在Linux系统下创建用户账户。

       命令语法:

    [root@redhat2 ~]# useradd

    Usage: useradd [options] LOGIN

    Options:

      -b, --base-dir BASE_DIR       base directory for the home directory of the

                                    new account

      -c, --comment COMMENT         GECOS field of the new account

      -d, --home-dir HOME_DIR       home directory of the new account

      -D, --defaults                print or change default useradd configuration

      -e, --expiredate EXPIRE_DATE  expiration date of the new account

      -f, --inactive INACTIVE       password inactivity period of the new account

      -g, --gid GROUP               name or ID of the primary group of the new   account

      -G, --groups GROUPS           list of supplementary groups of the new account

      -h, --help                    display this help message and exit

      -k, --skel SKEL_DIR           use this alternative skeleton directory

      -K, --key KEY=VALUE           override /etc/login.defs defaults

      -l, --no-log-init             do not add the user to the lastlog and   faillog databases

      -m, --create-home             create the user's home directory

      -M, --no-create-home          do not create the user's home directory

      -N, --no-user-group           do not create a group with the same name as the user

      -o, --non-unique              allow to create users with duplicate  (non-unique) UID

      -p, --password PASSWORD       encrypted password of the new account

      -r, --system                  create a system account

      -s, --shell SHELL             login shell of the new account

      -u, --uid UID                 user ID of the new account

      -U, --user-group              create a group with the same name as the user

      -Z, --selinux-user SEUSER     use a specific SEUSER for the SELinux user mapping

    例1:创建用户账户zhangsan并设置口令。

    [root@redhat2 ~]# useradd lisi

    [root@redhat2 ~]# useradd wangwu

    [root@redhat2 ~]# passwd wangwu

    Changing password for user wangwu.

    New password:

    BAD PASSWORD: it is WAY too short

    BAD PASSWORD: is too simple

    Retype new password:

    passwd: all authentication tokens updated successfully.

    [root@redhat2 ~]# cat /etc/passwd |grep lisi

    lisi:x:502:502::/home/lisi:/bin/bash

    [root@redhat2 ~]# cat /etc/shadow |grep lisi

    lisi:!!:16442:0:99999:7:::

    //查看/etc/shadow文件,可以看到在用户lisi的口令字段上显示的是“!!”,表示该用户还没有设置口令,不能登录到Linux系统。

    [root@redhat2 ~]# cat /etc/passwd |grep wangwu

    wangwu:x:503:503::/home/wangwu:/bin/bash

    [root@redhat2 ~]# cat /etc/shadow |grep wangwu

    wangwu:$1$MC.MaDLW$KFpEpYbJb6mwiAH72ixbb/:16442:0:99999:7:::

    [root@redhat2 ~]#

    //查看/etc/shadow文件,可以看到在用户wangwu的口令字段上显示的是加密的口令,表示该用户已经设置口令,能登陆到Linux系统。

    例2:创建用户moon,并设置该用户UID为510。

    [root@redhat2 ~]# useradd -u 510 moon

    [root@redhat2 ~]# cat /etc/passwd | grep moon

    moon:x:510:510::/home/moon:/bin/bash

    [root@redhat2 ~]#

    //查看/etc/passwd文件,可以看到用户moon的UID是510

    例3:创建用户newuser,并设置该用户主目录为/home/www。

    [root@redhat2 ~]# useradd -d /home/www newuser

    [root@redhat2 ~]# cat /etc/passwd|grep newuser

    newuser:x:511:511::/home/www:/bin/bash

    //查看/etc/passwd文件,可以看到用户newuser的主目录是/home/www。

    [root@redhat2 ~]# ls -l /home

    total 24

    drwx------  4 lisi     lisi     4096 Jan  7 05:29 lisi

    drwx------  4 moon     moon     4096 Jan  7 05:45 moon

    drwx------  4 wangwu   wangwu   4096 Jan  7 05:29 wangwu

    drwx------  4 newuser  newuser  4096 Jan  7 05:53 www

    drwx------. 4 yang     yang     4096 Jun  2  2014 yang

    drwx------  4 zhangsan zhangsan 4096 Jan  7 05:26 zhangsan

    [root@redhat2 ~]#

    //用户newuser的主目录/home/www在创建用户时已经创建了。

    例4:创建用户pp,并指定该用户是属于组群root的成员。

    [root@redhat2 ~]# useradd -g root pp

    [root@redhat2 ~]# cat /etc/passwd |grep pp

      pp:x:512:0::/home/pp:/bin/bash

    //查看/etc/passwd文件,可以看到pp用户GID字段为0,0为root组群的GID。

    [root@redhat2 ~]# id pp

      uid=512(pp) gid=0(root) groups=0(root)

    [root@redhat2 ~]#

    //使用id命令,可以看到用户pp是属于组群root的成员。

    例5:创建用户abc,并设置该用户的shell类型是/bin/ksh。

    [root@redhat2 ~]# useradd -s /bin/ksh abc

    [root@redhat2 ~]# cat /etc/passwd | grep abc

    abc:x:513:513::/home/abc:/bin/ksh

    [root@redhat2 ~]#

    例:[root@redhat2 ~]# su - abc

    $ vi yang

       #!/bin/ksh

       echo "nihao!"

    $ ksh yang

        nihao!

    $

    //查看/etc/passwd文件,可以看到用户abc的shell类型是/bin/ksh。

    二.修改用户账户

      使用usermod命令能更改用户的shell类型、所属的用户组群、用户口令的有效期,还能更改用户的登录名。

    [root@redhat2 ~]# usermod

    Usage: usermod [options] LOGIN

    Options:

      -c, --comment COMMENT         new value of the GECOS field

      -d, --home HOME_DIR           new home directory for the user account

      -e, --expiredate EXPIRE_DATE  set account expiration date to EXPIRE_DATE

      -f, --inactive INACTIVE       set password inactive after expiration  to INACTIVE

      -g, --gid GROUP               force use GROUP as new primary group

      -G, --groups GROUPS           new list of supplementary GROUPS

      -a, --append                  append the user to the supplemental GROUPS

                                    mentioned by the -G option without removing

                                    him/her from other groups

      -h, --help                    display this help message and exit

      -l, --login NEW_LOGIN         new value of the login name

      -L, --lock                    lock the user account

      -m, --move-home               move contents of the home directory to the

                                    new location (use only with -d)

      -o, --non-unique              allow using duplicate (non-unique) UID

      -p, --password PASSWORD       use encrypted password for the new password

      -s, --shell SHELL             new login shell for the user account

      -u, --uid UID                 new UID for the user account

      -U, --unlock                  unlock the user account

      -Z, --selinux-user            new SELinux user mapping for the user account

    例1:修改用户zhangsan的主目录为/home/kkk,并手动创建/home/kkk目录。

    [root@redhat2 ~]# usermod -d /home/kkk zhangsan

    [root@redhat2 ~]# cat /etc/passwd|grep zhangsan

       zhangsan:x:501:501::/home/kkk:/bin/bash

    [root@redhat2 ~]#

    //查看/etc/passwd文件,可以看到用户zhangsan的主目录已经更改为/home/kkk

    [root@redhat2 ~]# mkdir /home/kkk

    //必须使用mkdir命令创建/home/kkk目录,这样用户zhangsan才能使用该主目录。

    例2:修改用户wangwu的主目录/home/opop,并自动创建/home/opop目录。

    [root@redhat2 ~]# usermod -d /home/opop wangwu

    [root@redhat2 ~]# cat /etc/passwd|grep wangwu

          wangwu:x:503:503::/home/opop:/bin/bash

    [root@redhat2 ~]#

    例3:修改用户wangwu的登录名为zhaoliu

    [root@redhat2 ~]# usermod -l jiacheng zhangsan

    [root@redhat2 ~]# cat /etc/passwd|grep jiacheng

      jiacheng:x:501:501::/home/kkk:/bin/bash

    例4:修改用户zhangsan在口令过期后20天就禁用该账号。

    [root@redhat2 home]# useradd zhang

    [root@redhat2 home]# cat /etc/shadow|grep zhang

    zhang:!!:16443:0:99999:7:::

    //用户zhang在口令过期后几天禁用该账号默认是没有设置的

    [root@redhat2 home]# usermod -f 20 zhang

    [root@redhat2 home]# cat /etc/shadow |grep zhang

    zhang:!!:16443:0:99999:7:20::

    //查看/etc/passwd文件,可以看到用户zhang将口令过期后20天就禁用该账号。

    例5:修改用户yang所属的组群为root,该组群必须事先存在。

    [root@redhat2 home]# usermod -g root yang

    [root@redhat2 home]# cat /etc/passwd |grep yang

      yang:x:500:0:yang:/home/yang:/bin/bash

    [root@redhat2 home]#

    例6:锁住用户yang口令,使口令无效。

    [root@redhat2 home]# usermod -L yang

    [root@redhat2 home]# passwd -S yang

    yang LK 2014-06-01 0 99999 7 -1 (Password locked.)

    //查看用户yang口令状态,可以看到该用户口令已经锁住,该用户不能在系统上登录,但是却可以从其他用户账户切换到该账户。

    例7:修改用户yang账号的过期日期是2008年12月12号。

    [root@redhat2 ~]# cat /etc/shadow |grep yang

    yang:!$1$gUMyUM/y$derjmWWPUVY1/qisPvaQ6/:16223:0:99999:7:::

    [root@redhat2 ~]# usermod -e 12/12/2008 yang

    [root@redhat2 ~]# cat /etc/shadow |grep yang

    yang:!$1$gUMyUM/y$derjmWWPUVY1/qisPvaQ6/:16223:0:99999:7::14225:

    [root@redhat2 ~]#

    例8:修改用户yang的shell类型为/bin/ksh

    [root@redhat2 ~]# cat /etc/passwd|grep yang

    yang:x:500:0:yang:/home/yang:/bin/bash

    [root@redhat2 ~]# usermod -s /bin/ksh yang

    [root@redhat2 ~]# cat /etc/passwd |grep yang

    yang:x:500:0:yang:/home/yang:/bin/ksh

    [root@redhat2 ~]#

    //查看/etc/shadow文件,可以看到用户yang的SHELL类型已经更改为/bin/ksh

    三.删除用户账户

      使用userdel命令可以在LINUX系统下删除用户账户。

    [root@redhat2 ~]# userdel

    Usage: userdel [options] LOGIN

    Options:

      -f, --force                   force removal of files,      even if not owned by user

      -h, --help                    display this help message and exit

      -r, --remove                  remove home directory and mail spool

      -Z, --selinux-user            remove SELinux user from SELinux user mapping

    [root@redhat2 ~]#

    例1:删除用户lisi

    [root@redhat2 ~]# cd /home

    [root@redhat2 home]# ls

    lisi  moon  pp  wangwu  www  yang  zhang

    [root@redhat2 home]# userdel lisi

    [root@redhat2 home]# cat /etc/passwd |grep lisi

    [root@redhat2 home]# ls /home

    lisi  moon  pp  wangwu  www  yang  zhang

    [root@redhat2 home]#

    例2:删除用户moon,并且在删除该用户的同时一起删除主目录。

    [root@redhat2 home]# userdel -r moon

    [root@redhat2 home]# ls /home

    lisi  pp  wangwu  www  yang  zhang

    [root@redhat2 home]#

    //查看/home目录的内容,可以看到用户moon的主目录随该用户一起删除啦。

     

  • 相关阅读:
    人生转折点:弃文从理
    人生第一站:大三暑假实习僧
    监听器启动顺序和java常见注解
    java常识和好玩的注释
    182. Duplicate Emails (Easy)
    181. Employees Earning More Than Their Managers (Easy)
    180. Consecutive Numbers (Medium)
    178. Rank Scores (Medium)
    177. Nth Highest Salary (Medium)
    176. Second Highest Salary(Easy)
  • 原文地址:https://www.cnblogs.com/numberone/p/4216740.html
Copyright © 2011-2022 走看看