Linux - 用户管理 - 用户

bugsycrack 2014-07-18

【基本介绍】

Linux系统的一大块就是用户管理,管理用户的信息,这里介绍用户。

常用的命令有useradd,userdel,usermod,users

【基本操作】

添加用户

[root@bogon ~]# useradd --help
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

简单实例

useradd -c "This user cannot login to a shell" -s /sbin/nologin username
#添加用户,但是不能登入shell
useradd -c "This user login by bash" -s /bin/bash username
#添加用户登入用后环境是bash
useradd -d /usr/username  username
#添加用户并指定加目录,默认是/home下面
useradd -s /bin/sh -g group -G adm,root gem
#添加用户指定shell,主组是group,从组是adm,root,gem

删除用户

[root@bogon ~]# userdel --help
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

简单实例

userdel username
#删除用户(passwd ,shadow),但是不会删除该用户的家目录
userdel -r username
#删除用户,包括家目录

修改用户

[root@bogon ~]# usermod --help
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

简单实例

usermod -d /usr/tttt -m tttt
#移动家目录到/usr/tttt
usermod -d /usr/tttt tttt
#修改家目录到/user/tttt,原来的/home/tttt还保留
usermod -s /bin/ksh -d /home/z -g developer sam
#用户sam的登录Shell修改为ksh,主目录改为/home/z,用户组改为developer

查看用户信息:finger,id

NAME
     finger - user information lookup program
SYNOPSIS
     finger [-lmsp] [user ...] [user@host ...]
DESCRIPTION
     The finger displays information about the system users.

finger用户名----可以查看用户的相关信息,包括用户的主目录,启动shell,用户名等

finger-l用户名----以长格式显示用户信息

finger-s用户名----以短格式显示用户信息

[root@bogon srv]# finger spark
Login: spark                            Name: Spark
Directory: /home/spark                  Shell: /bin/bash
On since Tue Jul 15 16:56 (CST) on tty1 from :0
   23 days 18 hours idle
No mail.
No Plan.
[root@bogon srv]# id --help
Usage: id [OPTION]... [USERNAME]
Print user and group information for the specified USERNAME,
or (when USERNAME omitted) for the current user.

  -a              ignore, for compatibility with other versions
  -Z, --context   print only the security context of the current user
  -g, --group     print only the effective group ID
  -G, --groups    print all group IDs
  -n, --name      print a name instead of a number, for -ugG
  -r, --real      print the real ID instead of the effective ID, with -ugG
  -u, --user      print only the effective user ID
      --help     display this help and exit
      --version  output version information and exit

【参考】

http://www.g-loaded.eu/2005/11/06/manage-users-from-the-command-line/

http://www.cnblogs.com/end/archive/2011/05/25/2057129.html

相关推荐