我想从我的Java应用程序创建一个带有密码的Linux系统用户。我目前使用以下代码:
ProcessBuilder pb =
new ProcessBuilder("/bin/bash", "-c", "/usr/sbin/useradd -p $(openssl passwd -1 " + password + ") " + username);
unix命令useradd
要求用户sudoer /输入密码,因此我将用户添加到/etc/sudoers
:
myUser ALL=NOPASSWD: /usr/sbin/useradd
直接从命令行创建用户(使用sudo
,但不输入密码)可以正常工作。但是,如果我从Java应用程序创建用户,我会得到:
[info] application - useradd: Permission denied.
[info] application - useradd: cannot lock /etc/passwd; try again later.
将sudo
添加到流程字符串("sudo /usr/sbin/useradd -p $(openssl passwd -1 " + password + ") " + username)
)时,我没有得到任何(错误)输出,但也没有创建用户。
编辑:
/etc/sudoers
- 文件
#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults env_reset
Defaults mail_badpass
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"
# Host alias specification
# User alias specification
# Cmnd alias specification
# User privilege specification
root ALL=(ALL:ALL) ALL
myUser ALL=NOPASSWD: /usr/sbin/useradd
# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL
# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) ALL
# See sudoers(5) for more information on "#include" directives:
#includedir /etc/sudoers.d
Java应用程序由myUser