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用起 ...
随机推荐
- php变量函数,回调函数
一,变量可以直接传递函数 <?php function demo($num , $n )//$n是个函数 { for($i=0;$i<$num;++$i) { if($n($i)) { e ...
- 人脸识别SDK小结
Face++人脸识别 进入官网 Face++ 致力于研发世界最好的人脸技术,提供免费的API和SDK供企业和开发者调用,更有灵活的定制化服务满足不同需求.已有多家公司使用Face++技术服务,完成包括 ...
- 北大poj-1062
昂贵的聘礼 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 43523 Accepted: 12760 Descripti ...
- windows核心编程---第四章 进程
上一章介绍了内核对象,这一节开始就要不断接触各种内核对象了.首先要给大家介绍的是进程内核对象.进程大家都不陌生,它是资源和分配的基本单位,而进程内核对象就是与进程相关联的一个数据结构.操作系统内核通过 ...
- webApp--cordova 系列1
1.在node,npm 可用的前提下 npm install -g cordova 大约10分钟后装完,cmd中输入cordova -v 验证是否安装成功 2.cordova项目打包是用ant完成, ...
- MAC上python+Eclipse+pydev环境搭建
转自:http://www.cnblogs.com/Bonker/p/3584707.html 本文重点介绍使用Eclipse+pydev插件来写Python代码, 以及在Mac上配置Eclipse ...
- 【Avalon源码】iterator
function iterator(vars, body, ret) { var fun = 'for(var ' + vars + 'i=0,n = this.length; i < n; i ...
- iOS9适配中的各种问题
1.http在ios9上不能在使用.需要进行配置. the resource could not be loaded because the app transport security policy ...
- 黑马程序员——JAVA基础之反射
------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------- Java 反射是Java语言的一个很重要的特征,它使得Java具体了"动态 ...
- Python中optionParser模块的使用方法[转]
本文以实例形式较为详尽的讲述了Python中optionParser模块的使用方法,对于深入学习Python有很好的借鉴价值.分享给大家供大家参考之用.具体分析如下: 一般来说,Python中有两个内 ...