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用起 ...
随机推荐
- 使用函数库(JAVA API)
/*使用函数库(JAVA API) * 在JAVA的API里类被封装在一个个的package,要使用package的类之前必须 * 要知道这个类属于哪个package * 引用类方式: * 1.通过i ...
- MVC的过滤器
过滤器分类: Action过滤器 View结果渲染过滤器 全局错误异常过滤器 身份验证过滤器 1.Action过滤器:在Action执行之前和执行之后分别干一些事 接口:(IA ...
- Zyxel Switch-How to block a fake DHCP server without enabling DHCP snooping?
How to block a fake DHCP server without enabling DHCP snooping? Scenario How to block a fake DHCP se ...
- CSS透明opacity和IE各版本透明度滤镜filter的最准确用法
转载,原文见:http://blog.csdn.net/freshlover/article/details/17143341 CSS3的透明度属性opacity想必大家都已经用的无处不在了.而对于不 ...
- Sprint第二个冲刺(第十三天)
一.Sprint 计划会议: sprint2计划的功能还没有全部完成,还在实现剩余的两个功能模块,目前工作进展还比较顺利,争取下一个sprint完成剩余的所有目标. 以下是真机测试下的拍照和系统相册取 ...
- .jre下的lib和jdk下的lib的区别
jre是JDK的一个子集.提供一个运行环境.JDK的lib目录是给JDK用的,例如JDK下有一些工具,可能要用该目录中的文件.例如,编译器等.JRE的lib目录是为JVM,运行时候用的.包括所有的标准 ...
- (转)神经网络和深度学习简史(第一部分):从感知机到BP算法
深度|神经网络和深度学习简史(第一部分):从感知机到BP算法 2016-01-23 机器之心 来自Andrey Kurenkov 作者:Andrey Kurenkov 机器之心编译出品 参与:chen ...
- 解决Ubuntu输入正确密码后无法进入桌面,一直停留在登陆界面的问题
在登陆界面按下Ctrl + Shift + F1 进入命令行模式,输入你的用户名和密码之后,敲入下面几行命令就可以了! $ cd - $ sudo chown 你的用户名:你的用户名 .Xauthor ...
- document.write 方法
如果在文档加载结束后再调用document.write(),那么输出的内容将会 重写 整个页面. 某次被问及此问题,志之!
- MapReduce简介
MapReduce简介 参考自[http://www.cnblogs.com/swanspouse/p/5130136.html] MapReduce定义: MapReduce是一种可用于数据处理的编 ...