useradd或adduser命令用来建立用户帐号和创建用户的起始目录,使用权限是超级用户。
useradd [-d home] [-s shell] [-c comment] [-m [-k template]] [-f inactive] [-e expire ] [-p passwd] [-r] name
Usage: useradd [options] LOGIN useradd -D useradd -D [options]
Options: -b, --base-dir BASE_DIR base directory for the home directory of the new account 翻译:新帐户的主目录的BASE_DIR基目录 -c, --comment COMMENT GECOS field of the new account 翻译:注释注释新帐户的GECOS字段 -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 翻译:gid组名称或新帐户的主要组的ID -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 翻译:覆盖/etc/login.defs违约 -l, --no-log-init do not add the user to the lastlog and faillog databases 翻译:不将用户添加到lastlog和faillog数据库 -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 翻译:允许创建具有重复(非唯一)UID的用户 -p, --password PASSWORD encrypted password of the new account 翻译:新帐户的加密密码 -r, --system create a system account 翻译:创建系统帐户 -R, --root CHROOT_DIR directory to chroot into 翻译:要chroot到的目录 -P, --prefix PREFIX_DIR prefix directory where are located the /etc/* files *翻译:位于/etc/文件的前缀目录 -s, --shell SHELL login shell of the new account 翻译:新帐户的登录外壳 -u, --uid UID user ID of the new account 翻译:新帐户的用户ID -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 翻译:对SELinux用户映射使用特定的SEUSER
useradd可用来建立用户账号,它和adduser命令是相同的。账号建好之后,再用passwd设定账号的密码。使用useradd命令所建立的账号,实际上是保存在/etc/passwd文本文件中。
建立一个新用户账户testuser1,并设置UID为544,主目录为/usr/testuser1,属于users组:
#useradd -u 544 -d /usr/testuser1 -g users -m testuser1加-m 如果主目录不存在则自动创建
使用管理员账号登陆系统,建立用户tmp_3452 密码3sdt5:Eawhg
使用管理员账号登陆系统,建立用户tmp_3452 密码3sdt5:Eawhg
添加用户命令:
[root@ptr228 ~]# adduser tmp_3452 修改密码命令: [root@ptr228 ~]# passwd tmp_3452在系统出现提示输入密码是输入密码:3sdt5:Eawhg 系统提示输入确认密码后再输入一次。OK添加成功
使用useradd时,如果后面不添加任何参数选项,例如:#sudo useradd test创建出来的用户将是默认“三无”用户:一无Home Directory,二无密码,三无系统Shell。
步骤如下:
(1)建立用户名列表文件username.txt (同上)
(2)创建用户密码对应文件serc.txt,格式为username:password (注意文件的格式)
stu1:tt1 stu2:tt2 stu3:tt3 stu4:tt4 stu5:tt5 stu6:tt6(3)批量添加的脚本文件aa.sh
##添加用户,并且在/home/ 下为用户生成用户目录。cat < username.txt | xargs -n 1 useradd -m##批处理模式下更新密码 chpasswd < serc.txt##将上述的密码转换到密码文件和组文件 pwconv##结束验证信息 echo "OK 新建完成"(4)执行该脚本文件,查看执行过程 root@liu:/home/liu/Desktop/Dos# sh aa.sh新建完成 useradd命令,在执行没有出错的情况下,不会输出任何的信息,不会与用户交互。但是用户必须要记住那些设置项目,否则添加的用户可能出现一些预想不到的结果。
#useradd oracle -g oinstall -G dba
新创建一个oracle用户,这初始属于oinstall组,且同时让他也属于dba组。
#useradd tomcat -d /var/servlet/service -s /sbin/nologin
无法使用shell,且其用户目录至/var/servlet/service
删除刚创建的账号 tmp_3452
删除用户命令:
[root@ptr228 ~]# userdel tmp_3452或者连同用户目录一并删除:
[root@ptr228 ~]# userdel -f tmp_3452注意:这里如果用户还在登陆的话,会提示,用户正在登陆无法删除。此时可能需要先强制用户退出。
查看当前登陆用户的命令:
[root@ptr228 ~]# w会输入如下结果: 12:10:27 up 21:13, 1 user, load average: 0.00, 0.01, 0.08 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 *..*. 11:33 0.00s 0.08s 0.00s w tmp_3254 ps1 *..*. 11:33 0.00s 0.08s 0.00s ls
这里知道了登陆用户的tty是ps1执行强制退出命令pkill:
命令原型: pkill -kill -t [TTY]
[root@ptr228 ~]# pkill -kill -t ps1执行之后再执行名w 可以看到用户已经退出。
重复执行第二步的删除用户命令,删除成功。
