linux的用户名规则,CentOS 6和RHEL 6上Linux用户名的真正规则是什么?

我正在编写一些可用于创建Linux用户帐户的Web UI页面。该Web UI将在CentOS 6(从RHEL

6派生)上使用。我发现关于什么构成有效的Linux用户名的信息不一致且不完整。我去了源代码,检查了Linux shadow-

utils源代码软件包,但我不能确保所查看的版本实际上与CentOS 6的版本相同。

以下是我当前使用的代码片段,其中包括从shadow-

utils软件包版本4.1.4.3复制/粘贴注释,加上我自己的一些注释以及Java正则表达式搜索,以使我了解阴影- utils来源。

chkname.c中引用的“

is_valid_name()”检查显然不是Linux上的useradd命令所使用的,因为注释(和C代码源)不允许名称以数字开头。但是,useradd确实允许创建一个帐户,例如“

1234”。

我将不胜感激,从现在的内容到更正确的调整,以及有关如何通过一些稍有不同的is_valid_name函数实现useradd.c的信息,我们将不胜感激。

谢谢!艾伦

/**

* Define constants for use in isNameLinuxCompatible(...) method.

*

* The source for the Linux compatible user name rule is is_valid_name(...) a function in the "shadow" package

* for Linux. The source file for that function has a comment as follows:

* User/group names must match [a-z_][a-z0-9_-]*[$]

* That expression is a little loose/sloppy since

* (1) the trailing $ sign is optional, and

* (2) uppercase A-Z is also ok (and case is significant, 'A' != 'a').

*

* We deal with (1) by using the [$]? form where the ? means zero or more characters (aka "greedy").

* We deal with (2) by using the CASE_INSENSITIVE option.

*

* Another way to express this is:

* 1st character: a-z_ required at least one char

* chars other than first and last: a-z0-9_- optional

* last character: $ optional

* Max length is 31. Min length is 1.

*

* NOTE: The initial ^ and final $ below are important since we need the entire string to satisfy the rule,

* from beginning to end.

*

* See http://download.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html for reference info on pattern matching.

*/

private static final String LINUX_USERNAME_REGEX = "^[a-z_][a-z0-9_-]*[$]?$";

private static final Pattern LINUX_USERNAME_PATTERN = Pattern.compile(LINUX_USERNAME_REGEX, Pattern.CASE_INSENSITIVE);

private static final int LINUX_USERNAME_MINLENGTH = 1;

private static final int LINUX_USERNAME_MAXLENGTH = 31;

/**

* See if username is compatible with standard Linux rules for usernames, in terms of length and

* in terms of content.

*

* @param username the name to be checked for validity

* @return true if Linux compatible, else false

*/

public boolean isNameLinuxCompatible (final String username) {

boolean nameOK = false;

if (username != null) {

int len = username.length();

if ((len >= LINUX_USERNAME_MINLENGTH) && (len <= LINUX_USERNAME_MAXLENGTH)) {

Matcher m = LINUX_USERNAME_PATTERN.matcher(username);

nameOK = m.find();

}

}

return (nameOK);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值