Linux字符界面下用户账户的设置
在Linux系统字符界面下创建、修改以及删除用户账户主要使用useradd,usermod和userdel这3个命令。
一.创建用户账户
创建用户账户就是在系统中创建一个新账户,然后为新账户分配用户UID、用户组群、主目录和登录shell等资源,新创建的用户账户默认是被锁定的,无法使用,需要使用passwd命令设置密码后才能使用。创建用户账户就是在/etc/passwd文件中为新用户增加一条记录,同时更新其他系统文件如/etc/shadow,/etc/group等。
使用 useradd命令可以在Linux系统下创建用户账户。
命令语法:
[root@redhat2 ~]# useradd
Usage: useradd [options] LOGIN
Options:
-b, --base-dir BASE_DIR base directory for the home directory of the
new account
-c, --comment COMMENT GECOS field of the new account
-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
-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
-l, --no-log-init do not add the user to the lastlog and faillog databases
-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
-p, --password PASSWORD encrypted password of the new account
-r, --system create a system account
-s, --shell SHELL login shell of the new account
-u, --uid UID user ID of the new account
-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
例1:创建用户账户zhangsan并设置口令。
[root@redhat2 ~]# useradd lisi
[root@redhat2 ~]# useradd wangwu
[root@redhat2 ~]# passwd wangwu
Changing password for user wangwu.
New password:
BAD PASSWORD: it is WAY too short
BAD PASSWORD: is too simple
Retype new password:
passwd: all authentication tokens updated successfully.
[root@redhat2 ~]# cat /etc/passwd |grep lisi
lisi:x:502:502::/home/lisi:/bin/bash
[root@redhat2 ~]# cat /etc/shadow |grep lisi
lisi:!!:16442:0:99999:7:::
//查看/etc/shadow文件,可以看到在用户lisi的口令字段上显示的是“!!”,表示该用户还没有设置口令,不能登录到Linux系统。
[root@redhat2 ~]# cat /etc/passwd |grep wangwu
wangwu:x:503:503::/home/wangwu:/bin/bash
[root@redhat2 ~]# cat /etc/shadow |grep wangwu
wangwu:$1$MC.MaDLW$KFpEpYbJb6mwiAH72ixbb/:16442:0:99999:7:::
[root@redhat2 ~]#
//查看/etc/shadow文件,可以看到在用户wangwu的口令字段上显示的是加密的口令,表示该用户已经设置口令,能登陆到Linux系统。
例2:创建用户moon,并设置该用户UID为510。
[root@redhat2 ~]# useradd -u 510 moon
[root@redhat2 ~]# cat /etc/passwd | grep moon
moon:x:510:510::/home/moon:/bin/bash
[root@redhat2 ~]#
//查看/etc/passwd文件,可以看到用户moon的UID是510
例3:创建用户newuser,并设置该用户主目录为/home/www。
[root@redhat2 ~]# useradd -d /home/www newuser
[root@redhat2 ~]# cat /etc/passwd|grep newuser
newuser:x:511:511::/home/www:/bin/bash
//查看/etc/passwd文件,可以看到用户newuser的主目录是/home/www。
[root@redhat2 ~]# ls -l /home
total 24
drwx------ 4 lisi lisi 4096 Jan 7 05:29 lisi
drwx------ 4 moon moon 4096 Jan 7 05:45 moon
drwx------ 4 wangwu wangwu 4096 Jan 7 05:29 wangwu
drwx------ 4 newuser newuser 4096 Jan 7 05:53 www
drwx------. 4 yang yang 4096 Jun 2 2014 yang
drwx------ 4 zhangsan zhangsan 4096 Jan 7 05:26 zhangsan
[root@redhat2 ~]#
//用户newuser的主目录/home/www在创建用户时已经创建了。
例4:创建用户pp,并指定该用户是属于组群root的成员。
[root@redhat2 ~]# useradd -g root pp
[root@redhat2 ~]# cat /etc/passwd |grep pp
pp:x:512:0::/home/pp:/bin/bash
//查看/etc/passwd文件,可以看到pp用户GID字段为0,0为root组群的GID。
[root@redhat2 ~]# id pp
uid=512(pp) gid=0(root) groups=0(root)
[root@redhat2 ~]#
//使用id命令,可以看到用户pp是属于组群root的成员。
例5:创建用户abc,并设置该用户的shell类型是/bin/ksh。
[root@redhat2 ~]# useradd -s /bin/ksh abc
[root@redhat2 ~]# cat /etc/passwd | grep abc
abc:x:513:513::/home/abc:/bin/ksh
[root@redhat2 ~]#
例:[root@redhat2 ~]# su - abc
$ vi yang
#!/bin/ksh
echo "nihao!"
$ ksh yang
nihao!
$
//查看/etc/passwd文件,可以看到用户abc的shell类型是/bin/ksh。
二.修改用户账户
使用usermod命令能更改用户的shell类型、所属的用户组群、用户口令的有效期,还能更改用户的登录名。
[root@redhat2 ~]# usermod
Usage: usermod [options] LOGIN
Options:
-c, --comment COMMENT new value of the GECOS field
-d, --home HOME_DIR new home directory for the user account
-e, --expiredate EXPIRE_DATE set account expiration date to EXPIRE_DATE
-f, --inactive INACTIVE set password inactive after expiration to INACTIVE
-g, --gid GROUP force use GROUP as new primary group
-G, --groups GROUPS new list of supplementary GROUPS
-a, --append append the user to the supplemental GROUPS
mentioned by the -G option without removing
him/her from other groups
-h, --help display this help message and exit
-l, --login NEW_LOGIN new value of the login name
-L, --lock lock the user account
-m, --move-home move contents of the home directory to the
new location (use only with -d)
-o, --non-unique allow using duplicate (non-unique) UID
-p, --password PASSWORD use encrypted password for the new password
-s, --shell SHELL new login shell for the user account
-u, --uid UID new UID for the user account
-U, --unlock unlock the user account
-Z, --selinux-user new SELinux user mapping for the user account
例1:修改用户zhangsan的主目录为/home/kkk,并手动创建/home/kkk目录。
[root@redhat2 ~]# usermod -d /home/kkk zhangsan
[root@redhat2 ~]# cat /etc/passwd|grep zhangsan
zhangsan:x:501:501::/home/kkk:/bin/bash
[root@redhat2 ~]#
//查看/etc/passwd文件,可以看到用户zhangsan的主目录已经更改为/home/kkk
[root@redhat2 ~]# mkdir /home/kkk
//必须使用mkdir命令创建/home/kkk目录,这样用户zhangsan才能使用该主目录。
例2:修改用户wangwu的主目录/home/opop,并自动创建/home/opop目录。
[root@redhat2 ~]# usermod -d /home/opop wangwu
[root@redhat2 ~]# cat /etc/passwd|grep wangwu
wangwu:x:503:503::/home/opop:/bin/bash
[root@redhat2 ~]#
例3:修改用户wangwu的登录名为zhaoliu
[root@redhat2 ~]# usermod -l jiacheng zhangsan
[root@redhat2 ~]# cat /etc/passwd|grep jiacheng
jiacheng:x:501:501::/home/kkk:/bin/bash
例4:修改用户zhangsan在口令过期后20天就禁用该账号。
[root@redhat2 home]# useradd zhang
[root@redhat2 home]# cat /etc/shadow|grep zhang
zhang:!!:16443:0:99999:7:::
//用户zhang在口令过期后几天禁用该账号默认是没有设置的
[root@redhat2 home]# usermod -f 20 zhang
[root@redhat2 home]# cat /etc/shadow |grep zhang
zhang:!!:16443:0:99999:7:20::
//查看/etc/passwd文件,可以看到用户zhang将口令过期后20天就禁用该账号。
例5:修改用户yang所属的组群为root,该组群必须事先存在。
[root@redhat2 home]# usermod -g root yang
[root@redhat2 home]# cat /etc/passwd |grep yang
yang:x:500:0:yang:/home/yang:/bin/bash
[root@redhat2 home]#
例6:锁住用户yang口令,使口令无效。
[root@redhat2 home]# usermod -L yang
[root@redhat2 home]# passwd -S yang
yang LK 2014-06-01 0 99999 7 -1 (Password locked.)
//查看用户yang口令状态,可以看到该用户口令已经锁住,该用户不能在系统上登录,但是却可以从其他用户账户切换到该账户。
例7:修改用户yang账号的过期日期是2008年12月12号。
[root@redhat2 ~]# cat /etc/shadow |grep yang
yang:!$1$gUMyUM/y$derjmWWPUVY1/qisPvaQ6/:16223:0:99999:7:::
[root@redhat2 ~]# usermod -e 12/12/2008 yang
[root@redhat2 ~]# cat /etc/shadow |grep yang
yang:!$1$gUMyUM/y$derjmWWPUVY1/qisPvaQ6/:16223:0:99999:7::14225:
[root@redhat2 ~]#
例8:修改用户yang的shell类型为/bin/ksh
[root@redhat2 ~]# cat /etc/passwd|grep yang
yang:x:500:0:yang:/home/yang:/bin/bash
[root@redhat2 ~]# usermod -s /bin/ksh yang
[root@redhat2 ~]# cat /etc/passwd |grep yang
yang:x:500:0:yang:/home/yang:/bin/ksh
[root@redhat2 ~]#
//查看/etc/shadow文件,可以看到用户yang的SHELL类型已经更改为/bin/ksh
三.删除用户账户
使用userdel命令可以在LINUX系统下删除用户账户。
[root@redhat2 ~]# userdel
Usage: userdel [options] LOGIN
Options:
-f, --force force removal of files, even if not owned by user
-h, --help display this help message and exit
-r, --remove remove home directory and mail spool
-Z, --selinux-user remove SELinux user from SELinux user mapping
[root@redhat2 ~]#
例1:删除用户lisi
[root@redhat2 ~]# cd /home
[root@redhat2 home]# ls
lisi moon pp wangwu www yang zhang
[root@redhat2 home]# userdel lisi
[root@redhat2 home]# cat /etc/passwd |grep lisi
[root@redhat2 home]# ls /home
lisi moon pp wangwu www yang zhang
[root@redhat2 home]#
例2:删除用户moon,并且在删除该用户的同时一起删除主目录。
[root@redhat2 home]# userdel -r moon
[root@redhat2 home]# ls /home
lisi pp wangwu www yang zhang
[root@redhat2 home]#
//查看/home目录的内容,可以看到用户moon的主目录随该用户一起删除啦。
Linux字符界面下用户账户的设置的更多相关文章
- 两款【linux字符界面下】显示【菜单】,【选项】的powershell脚本模块介绍
两款[linux字符界面下]显示[菜单],[选项]的powershell脚本模块介绍 powershell linux ps1 menu choice Multiselect 传教士 菜单 powe ...
- linux字符界面下root用户无法登录成功
os: rhel5.6_x86_64 ———————————————————————————————————————————— 故障:图形界面登录正常,其他一切正常,但是切换到字符界面时,输入用户ro ...
- Linux字符模式下如何设置/删除环境变量
Linux字符模式下设置/删除环境变量方法: bash下 设置:export 变量名=变量值 删除:unset 变量名 csh下 设置:setenv 变量名 变量值 删除:unsetenv 变量名 h ...
- linux复习3:linux字符界面的操作
一.前言 1.对linux服务器进行管理的时候,经常要进入字符界面进行操作,使用命令需要记住该命令的相关选项和参数.vi编辑器可以用于编辑任何ASCII文本,功能非常的强大,可以对文本进行创建.查找. ...
- 在linux中添加ftp用户,并设置相应的权限
在linux中添加ftp用户,并设置相应的权限,操作步骤如下: 1.环境:ftp为vsftp.被限制用户名为test.被限制路径为/home/test 2.建用户:在root用户下: useradd ...
- linux中添加ftp用户,并设置相应的权限
在linux中添加ftp用户,并设置相应的权限,操作步骤如下: 1.环境:ftp为vsftp.被限制用户名为test.被限制路径为/home/test 2.建用户:在root用户下: useradd ...
- ubuntu字符界面下显示中文和调整分辨率
1.sudo apt-get install zhcon 2.vi /etc/zhcon.conf 修改下面两行 x_resolution 1024 y_resolution 768 完成这两步后在 ...
- Linux字符界面和图形界面
Ubuntu图形界面和字符界面的切换 Ubuntu和其他的Linux系统一样,有图形界面和字符界面,同时能够设置默认的启动界面. linux的显示界面分为命令行的字符界面和图形界面,我们可以设置lin ...
- linux 字符界面浏览器 w3m(转)
最近找到了几个linux终端下使用的浏览器,主要用来测试本机web服务器是否搭建成功.因为我们一般是用ssh客户端连接linux的,所以很需要一个字符界面的浏览器.找了几个显示都不理想,只有w3m用起 ...
随机推荐
- c#基础知识-2
1.在控制台接受数据时可以这样输入: using System; using System.Collections.Generic; using System.Linq; using System.T ...
- Ubuntu user switch
To list all users you can use: cut -d: -f1 /etc/passwd To add a new user you can use: sudo adduser n ...
- 10-20日 && 抽签问题
Ants # include <cstdio> #include <algorithm> ; int L, n, x[MAX_N]; void solve() { ; ; i ...
- C语言之const和define
const修饰的是只读变量,不是常量,其值在编译时不能被使用,因为编译器在编译时不知道其存储的内容.编译器通常不为普通const只读变量分配存储空间,而使将他们保存在符号表中,这使得他成为一个编译期间 ...
- HDU 4777 Rabbit Kingdom (2013杭州赛区1008题,预处理,树状数组)
Rabbit Kingdom Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- LNMP安装一键安装包
系统需求: CentOS/Debian/Ubuntu Linux系统 需要2GB以上硬盘剩余空间 128M以上内存,OpenVZ的建议192MB以上(小内存请勿使用64位系统) VPS或服务器必须已经 ...
- Ubuntn系统忘记密码的解决方法(虚拟机下同样处理)
不知道你有没有遇到过忘记了ubuntn系统密码的情况,反正我都了,一段时间没用就很容易忘记密码的,此时无奈只能修改密码了!下面分享一个简单实用的方法: 版本号为: (我用的是V-BOX虚拟机安装的ub ...
- flume+kafka+smart数据接入实施手册
1. 概述 本手册主要介绍了,一个将传统数据接入到Hadoop集群的数据接入方案和实施方法.供数据接入和集群运维人员参考. 1.1. 整体方案 Flume作为日志收集工具,监控一个文件目录或者一 ...
- ubuntu15.10跑裸机程序跑.bin文件
1:安装tftp:#apt-get update#apt-get install tftp-hpa tftpd-hpa xinetd2:#cd /srv#mkdir tftp#chmod 777 tf ...
- HDU 2274 Magic WisKey
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2274 Magic WisKey Time Limit: 2000/1000 MS (Java/Othe ...