您的位置:首页 > 运维架构 > Linux

Linux添加用户

2009-07-02 00:44 465 查看
本文介绍linux添加用户的命令useradd,同时介绍一个添加用户的脚本。
1、useradd命令
man手册页中对useradd描述如下:create a new user or update default new user information。从描述中可以看出useradd有两项功能:创建用户和更新创建新用户时的默认信息。它的语法格式为:
useradd [options] LOGIN
useradd -D
useradd -D [options]
-D选项是用来标示useradd的功能是创建用户还是更新创建新用户时的默认信息,换句话说,这个选项是用来显示和修改用useradd命令添加用户时的一些默认参数的,当用上面的第二种格式时(useradd -D),只显示默认信息,当用上面的第三种格式时(useradd -D [options]),修改默认信息,例如:
wangjiankun:/home/wangjk# useradd -D
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/sh
SKEL=/etc/skel
CREATE_MAIL_SPOOL=no
wangjiankun:/home/wangjk# useradd -D -e 2009-12-01
wangjiankun:/home/wangjk# useradd -D
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=2009-12-01
SHELL=/bin/sh
SKEL=/etc/skel
CREATE_MAIL_SPOOL=no
wangjiankun:/home/wangjk#
注意三个红色部分的变化。要想将EXPIRE的值改回原值运行命令:
useradd –D –e “”
即可。
总之,第一种格式是用来创建新用户的;第二种格式是用来显示创建新用户时的默认信息的;第三种格式是用来修改创建新用户时的默认信息的。
在第一种和第三种格式中的[options]可以是如下选项(-D选项只支持其中的前五个选项,用绿色表示):
(1)-b选项
我不知道把这个选项翻译成什么最合适,或许可以把它叫做“用户基址目录”,不过我通过几个实际的操作一定能说明白这个参数。
我们先查看一下默认参数的值,如下:
wangjiankun:/# useradd -D
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=2009-12-01
SHELL=/bin/sh
SKEL=/etc/skel
CREATE_MAIL_SPOOL=no
现在我们添加一个用户test_b,如下:
wangjiankun:/# useradd -c "test -b" test_b
wangjiankun:/#
此时在/etc/passwd文件中增加了如下一行:
test_b:x:1003:1003:test -b:/home/test_b:/bin/sh
注意红色部分:说明用户test_b的家目录为:/home/test_b
然后我们运行如下命令:
wangjiankun:/# useradd -D -b /opt
默认参数变为:
wangjiankun:/# useradd -D
GROUP=100
HOME=/opt
INACTIVE=-1
EXPIRE=2009-12-01
SHELL=/bin/sh
SKEL=/etc/skel
CREATE_MAIL_SPOOL=no
注意红色部分的变化:由/home变为/opt,然后我们再添加一个用户test_b_opt,如下:
wangjiankun:/# useradd -c "test -b @ /opt" test_b_opt
结果在/etc/passwd文件中增加了一行:
test_b_opt:x:1004:1004:test -b @ /opt:/opt/test_b_opt:/bin/sh
用户test_b_opt的家目录到了/opt目录下面。
通过上面的例子说明:-b选项决定着用户家目录所在的位置,而用户的实际家目录是由-b指定的或默认的HOME值加上用户名构成的。
下面把man手册中的解释粘贴在下面:
-b, --base-dir BASE_DIR
         The default base directory for the system if -d dir is not
         specified.  BASE_DIR is concatenated with the account name to define
         the home directory. If the -m option is not used, BASE_DIR must
         exist.
(2)-e选项
指定用户的账号的到期时间,详细信息可参考文章开头介绍-D选项时的用例。
(3)-f选项
这个参数用到的不多,也不好试验,所以先将man手册中的解释粘贴在下面:
-f, --inactive INACTIVE
   The number of days after a password expires until the account is
   permanently disabled. A value of 0 disables the account as soon as
   the password has expired, and a value of -1 disables the feature.
   The default value is -1.
(4)-g选项
-g, --gid GROUP
   The group name or number of the user's initial login group. The
   group name must exist. A group number must refer to an already
   existing group. The default group number is 1 or whatever is
   specified in /etc/default/useradd.
这段话中提到了一个文件:/etc/default/useradd,值得注意。这个文件决定了创建新用户时的默认信息,也就是文章开头介绍的用命令useradd -D显示的信息,其实也可直接修改这个文件来修改默认值。
(5)-s选项
这个选项决定了用户的默认登陆shell,有关登陆shell的概念,请参考文章《

  bash学习之一:登陆、非登陆shell,交互、非交互shell,以及它们的startup文件》一文,地址:http://blog.csdn.net/jiankun_wang/archive/2009/07/02/4317407.aspx
-s, --shell SHELL
   The name of the user's login shell. The default is to leave this
   field blank, which causes the system to select the default login
   shell.
(6)-c选项
这个选项后面可以跟任意的一个字符串,用来说明用户的信息,这个字符串将显示在/etc/passwd文件的第5个域,例如:我们用命令:
useradd -c "test the option -c" mytest
来添加一个用户:mytest
/etc/passwd文件中增加了一行:
mytest:x:1002:1002:test the option -c:/home/mytest:/bin/sh
(7)-d选项
指定用户的家目录,如果指定的用户的家目录不存在,不会创建这个目录。
-d, --home HOME_DIR
   The new user will be created using HOME_DIR as the value for the
   user's login directory. The default is to append the LOGIN name to
   BASE_DIR and use that as the login directory name. The directory
   HOME_DIR does not have to exist but will not be created if it is
   missing.
(7)-m参数
这个参数是一个非常重要的参数,几乎所有的添加用户操作都要用到这个参数。在此,我将man手册中的解释翻译一下:
-m, --create-home
         The user's home directory will be created if it does not exist. The
         files contained in SKEL_DIR will be copied to the home directory if
         the -k option is used, otherwise the files contained in /etc/skel
         will be used instead. Any directories contained in SKEL_DIR or
         /etc/skel will be created in the user's home directory as well. The
         -k option is only valid in conjunction with the -m option. The
         default is to not create the directory and to not copy any files.
         This option may not function correctly if the username has a / in
         it.
如果用户的家目录不存在的话,useradd会创建用户的家目录。如果-k选项存在的话,存在于目录SKEL_DIR(自注:SKEL_DIR目录是-k选项指定的目录)中的文件将被拷贝到用户家目录下,否则,存在于目录/etc/skel中的文件将被拷贝到用户的家目录下,同时,SKEL_DIR或/etc/skel中的所有目录也将在用户家目录下创建。-k选项只有与-m选项配合使用时才有效。默认情况下不创建任何目录,也不拷贝任何文件。如果用户名中含有/字符,这个选项可能不能正常的工作。
正是由于上面红色的两句话导致了-m选项的重要性。
2、添加用户的脚本
wangjk@wangjiankun:~/scripts$ cat add_user.sh
     1  #!/bin/bash
     2  #
     3  # add_user.sh
     4  #
     5  # Wang Jiankun
     6  #
     7  # July 23, 2009
     8
     9  if [ $UID != 0 ]; then
    10          echo "Error! This script needs privilege right to be executed."
    11          exit 1
    12  fi
    13
    14  if [ $# == 0 ]; then
    15          echo "Syntax:"
    16          echo "  $0 USERNAME"
    17          echo "Please give a username."
    18          exit 1
    19  fi
    20
    21  USERNAME="$1"
    22  # PASSWORD="$1"
    23
    24  useradd -c "$USERNAME"                  /
    25                  -d "/home/$USERNAME"    /
    26                  -m                                              /
    27                  -s /bin/bash                    /
    28                  $USERNAME
    29
    30  if [ $? == 0 ]; then
    31          echo "Successfully added the user:$USERNAME."
    32          #echo -n "$USERNAME" | passwd --stdin "$USERNAME"
    33          echo "Please set password by command: passwd."
    34  else
    35          echo "Error adding user:$USERNAME."
    36          exit 1
    37  fi
wangjk@wangjiankun:~/scripts$
本想在脚本中将新用户的密码设置为用户名,可是debian的passwd命令不支持--stdin选项,所以将相关的两行注释掉了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: