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

Centos Linux下useradd 命令uid计算问题

2014-05-27 20:15 405 查看
生产环境应用服务器推送一个账号的时候发现报错,账号UID已存在无法创建,查看/etc/passwd中无相关UID信息,想到服务器开启了ldap服务,getent passwd 查看发现有ldap用户占用相关uid。
默认情况下,ldap新增的用户和系统本地的用户uid是混在一起,本来设想着给ldap中添加一个uid比较大的用户,每次ldap添加用户的时候能和本地账号的uid明显区分开来。于是,在ldap数据库每个组中增加了一个uid为30000的用户。再往ldap数据库中增加用户的时候,发现新增的用户的uid已经为30001了。

本以为就此就ok了,感觉还是验证下本地创建账号是否正常,然后使用useradd在本地创建了一个账号,发现创建的账号的uid为30002,没有像如期的那样uid为2004。

修改 /etc/nsswitch.conf 将ldap数据库禁用:
# To use db, put the "db" in front of "files" for entries you want to be
# looked up first in the databases
#
# Example:
#passwd:    db files nisplus nis
#shadow:    db files nisplus nis
#group:     db files nisplus nis
#passwd:     files ldap
passwd:     files
shadow:     files ldap
group:      files ldap
再次创建新用户测试,发现现在创建的用户的uid为2004。

useradd命令的声明,CAVEATS
You may not add a user to a NIS or LDAP group. This must be performed
on the corresponding server.

Similarly, if the username already exists in an external user database
such as NIS or LDAP, useradd will deny the user account creation
request.

查看了下useradd命令的帮助信息:
有关uid的解释,
-u, --uid UID
The numerical value of the user′s ID. This value must be unique, unless the -o option is used. The value must be non-negative. The default is to use the smallest ID value
greater than 999 and greater than every other user. Values between 0 and 999 are typically reserved for system accounts.
系统在使用useradd命令创建用户的时候,如果没有指定uid的情况,默认是使用超过999,且大于现有所有账号的最小uid。

当然也可以通过 -o参数指定非唯一uid,这样创建的用户会和其他用户公用一个uid,相当于这两个账号是一个账号。
useradd -o -u 503 test.dianping
当然使用-o参数是有一定的风险的,建议每个账号还是有自己唯一的uid。

在找资料解决,在添加ldap用户和 linux local users时分别使用不同uid段,比如本地账号使用10000以下的uid,ldap用30000以上的uid,10000到30000之间的uid作为保留。

在linux中存在一个文件允许限制我们通过useradd命令创建uid时的最大和最小值范围:

/etc/login.defs
#
# Min/max values for automatic uid selection in useradd
#
UID_MIN 500
UID_MAX 60000

文件修改后:
/etc/login.defs 是设置用户帐号限制的文件。该文件里的配置对root用户无效。
如果/etc/shadow文件里有相同的选项,则以/etc/shadow里的设置为准,也就是说/etc/shadow的配置优先级高于/etc/login.defs
# *REQUIRED* required
#  Directory where mailboxes reside, _or_ name of file, relative to the
#   home directory.  If you _do_ define both, MAIL_DIR takes precedence.
#   QMAIL_DIR is for Qmail
#
#QMAIL_DIR      Maildir
MAIL_DIR        /var/spool/mail
#创建用户时,要在目录/var/spool/mail中创建一个用户mail文件
#MAIL_FILE      .mail
# Password aging controls:
#
#       PASS_MAX_DAYS   Maximum number of days a password may be used.
#       PASS_MIN_DAYS   Minimum number of days allowed between password changes.
#       PASS_MIN_LEN    Minimum acceptable password length.
#       PASS_WARN_AGE   Number of days warning given before a password expires.
#
PASS_MAX_DAYS   99999
#密码最大有效期
PASS_MIN_DAYS   0
#两次修改密码的最小间隔时间
PASS_MIN_LEN    5
#密码最小长度,对于root无效
PASS_WARN_AGE   7
#密码过期前多少天开始提示
#
# Min/max values for automatic uid selection in useradd
#创建用户时不指定UID的话自动UID的范围
UID_MIN                   500
#用户ID的最小值
UID_MAX                 10000
#用户ID的最大值
#
# Min/max values for automatic gid selection in groupadd
#自动组ID的范围
GID_MIN                   500
#组ID的最小值
GID_MAX                 10000
#组ID的最大值
#
# If defined, this command is run when removing a user.
# It should remove any at/cron/print jobs etc. owned by
# the user to be removed (passed as the first argument).
#
#USERDEL_CMD    /usr/sbin/userdel_local
#当删除用户的时候执行的脚本
#
# If useradd should create home directories for users by default
# On RH systems, we do. This option is overridden with the -m flag on
# useradd command line.
#
CREATE_HOME     yes
#使用useradd的时候是够创建用户目录
# The permission mask is initialized to this value. If not specified,
# the permission mask will be initialized to 022.
UMASK           077
# This enables userdel to remove user groups if no members exist.
#
USERGROUPS_ENAB yes
#用MD5加密密码


这样设置以后,本地系统创建账号的uid默认为500到10000之间,而ldap账号可以在添加一个uid为30000的账号后将ldap数据库中每个组最大的uid取出来,然后最大uid的基础上每次增加1.

本文出自 “运维者说:从菜鸟到老鸟” 博客,请务必保留此出处http://liuqunying.blog.51cto.com/3984207/1417882
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: