用户管理命令 | 学习笔记(二)

简介: 快速学习用户管理命令。

开发者学堂课程【Linux用户和组管理用户管理命令学习笔记,与课程紧密联系,让用户快速学习知识.

课程地址:https://developer.aliyun.com/learning/course/580/detail/7993


用户管理命令


内容简介:

一、密码的复杂性策略 续

二、文件操作

三、用户和组管理命令

四、用户创建


三、用户和组管理命令

(一)用户管理命令

 useradd

 usermod

 Userdel

(二)组账号维护命令

 groupadd

 groupmod

 groupdel

1、useradd 基本用法:

[root@centos7 ~]#getent passwd

[root@centos7 ~]#useradd alice

[root@centos7 ~]#id alice

uid=1002(alice) gid=1002(alice) groups=1002(alice)

[root@centos7 ~]#ls /home -」

total 0

口令信息:

[root@centos7 ~]#getent shadow

改为空口令:

[root@centos7 ~]#nano /etc/shadow

将!!解禁:

[root@centos7 ~]#useradd tom

[root@centos7 ~]#getent shadow tom

[root@centos7 ~]#usermod -u tom

usermod:unlocking the user’s password would result in a passwordless accout.

You should set a password with user mod -p to unlock this user’s password

操作不被允许

可以将账号口令加锁再解禁:

[root@centos7 ~]#usermod -L mage

[root@centos7 ~]#getent shadow

解锁:

[root@centos7 ~]#usermod -U mage

[root@centos7 ~]#getent shadow

对于空口令 tom 用户则无法解锁

[root@centos7 ~]#useadd mage

[root@centos7 ~]#cat /etc/shadow

在老版本中允许用命令解锁(安全性不高):

[root@centos7 ~]#usermod -U mage

[root@centos7 ~]#cat /etc/shadow

  1. useradd 常见选项:

更改密码默认有效期最大值:

[root@centos7 ~]#cat /etc/login.defs

uid 自主制定、指定新的 uid:

[root@centos7 ~]#useradd -u 1234 li

用户账号可以共用 ID

指定家目录:

[root@centos7 ~]#useradd -d /data/judir hu

[root@centos7 ~]#getent passwd

设置为系统账号:

[root@centos7 ~]#useradd -r app

指定 nologin 类型:

[root@centos7 ~]#useradd -r app2 -s -s /sbin/nologin

app2 不可登陆

useradd 描述操作:

[root@centos7 ~]#useradd -c ‘web user’ -r -s /sbin/nologin webuser [root@centos7 ~]#getent passwd

令已有组为主组:

[root@centos7 ~]#useradd -g app app3

[root@centos7 ~]#getent passwd

创建新用户时,可以直接加入作为主组:

[root@centos7 ~]#usereadd -N zhang

[root@centos7 ~]#id zhang

uid=1238(zhang) gid=100(users) groups=100(users)

[root@centos7 ~]#cat /etc/default/useradd

# useradd defaults file

GROUP=100

HOME=/home

INACTIVE=-1

EXPIRE=

SHELL=/bin/bash

SKEL=/etc/skel

CREATE_MAIL_SPOOL=yes

新建用户时自动建立家目录:

[root@centos7 ~]#cd /home/alice/

[root@centos7 ~]#ls -a

.bash_logout  .bash_profile  .bashrc  .cache  .config  .mozilla

[root@centos7 ~]#ls /home/tom -a

.bash_logout   .bash_profile   .bashrc  .mozilla  skel.txt

[root@centos7 ~]#useradd test

[root@centos7 ~]#s ~test -a

.bash_logout   .bash_profile   .bashrc  .mozilla  skel.txt

还原不小心删除掉的家目录:

[root@centos7 skel]#rm -rf /home/test

[root@centos7 skel]#mkdir /home/test

[root@centos7 skel]#ls /home/test -a

[root@centos7 skel]#rm -rf skel.txt

[root@centos7 skel]#cp /etc/skel/* /home/test/

cp: cannot stat ‘/etc/skel/*’ : No such file or directory

[root@centos7 skel]#cp -r /etc/skel/.[^.]* /home/test/

[root@centos7 skel]#ls -a /home/test/

.bash_logout   .bash_profile   .bashrc  .mozilla

不足之处:

只能复制隐藏文件,不可复制非隐藏文件

[root@centos7 skel]#touch a.txt

[root@centos7 skel]#Pmd

/etc/skel

[root@centos7 skel]#ls

a .txt

[root@centos7 skel]#cp -r /etc/skel/.[^.]* /home/test/

[root@centos7 skel]#ls -a /home/test/

.bash_logout   .bash_profile   .bashrc  .mozilla  

解决方法:直接复制整个目录

[root@centos7 skel]#rm -rf /home/test/

[root@centos7 skel]#cp -r /etc/skel/ /home/test

[root@centos7 skel]#ls -a /home/test

将隐藏文件与非隐藏文件全部复制到目录下:

[root@centos7 skel]#cp -r /etc/skel/.[^.]* /etc/skel/* /home/test/

[root@centos7 skel]#ls -a /home/test/

简便方法:

[root@centos7 skel]#rm -rf /home/test

[root@centos7 skel]#mkdir /home/test

[root@centos7 skel]#cp -r /etc/skel/.  /home/test/

[root@centos7 skel]#ls /home/test -a

  1. 补充—附加组

[root@centos7 skel]#useradd -g bin -G wang,mage,alice test2

[root@centos7 skel]#id test2

或用 group 看用户属于哪一些组

[root@centos7 skel]#groups test2

test2 : bin wang mag alice


四、用户创建:

useradd  [options] LOGIN

-u UID

-0配合-u选项,不检查UID的唯一性

-g GID:指明用户所属基本组,可为组名,也可以GID

-c "COMMENT":用户的注释信息

-d HOME_DIR:以指定的路径(不存在)为家目录

-s SHELL:指明用户的默认shell程序,可用列表在/etc/shells文件中

-G GROUP1[,GROUP2,…]:为用户指明附加组,组须事先存在

-N不创建私用组做主组,使用users组做主组

-r 创建系统用户CentOS 6:ID<500,CentOS 7:ID<1000

-m创建家自录,用于系统用户

-M不创建家目录,用于非系统用户

1、使用 m 强制创建家目录:

[root@centos7 skel]#ls /home/

没有 app4 家目录

[root@centos7 skel]#useradd -r -m app5

[root@centos7 skel]#ll /home

2、使用 M 不创建家目录

[root@centos7 skel]#useradd -M test4

相关文章
|
8月前
|
安全 Linux Shell
Linux用户管理指南:创建、删除、权限、最佳实践,全面掌握用户管理技巧
Linux用户管理指南:创建、删除、权限、最佳实践,全面掌握用户管理技巧
654 0
|
3月前
|
自然语言处理 数据安全/隐私保护 开发者
wikijs的基本操作-网站配置和用户管理
wikijs的基本操作-网站配置和用户管理
|
9月前
|
Oracle 关系型数据库 数据库
用户管理
ORACLE用户删除
83 0
|
开发者 Python
添加用户功能的实现 | 学习笔记
快速学习 添加用户功能的实现
95 0
|
Linux 数据安全/隐私保护 开发者
用户管理命令 | 学习笔记(一)
快速学习用户管理命令。
70 0
|
开发者 Python
删除用户功能实现 | 学习笔记
快速学习 删除用户功能实现
78 0
删除用户功能实现 | 学习笔记
|
数据安全/隐私保护 开发者 Python
登录功能的实现| 学习笔记
快速学习登录功能的实现
66 0
|
Linux 数据安全/隐私保护
【Linux实操篇二】用户管理是什么?该如何进行用户管理(用户、组、密码等介绍)?(二)
【Linux实操篇二】用户管理是什么?该如何进行用户管理(用户、组、密码等介绍)?(二)
【Linux实操篇二】用户管理是什么?该如何进行用户管理(用户、组、密码等介绍)?(二)
|
Linux 开发工具 数据安全/隐私保护
【Linux实操篇二】用户管理是什么?该如何进行用户管理(用户、组、密码等介绍)?(一)
【Linux实操篇二】用户管理是什么?该如何进行用户管理(用户、组、密码等介绍)?(一)
【Linux实操篇二】用户管理是什么?该如何进行用户管理(用户、组、密码等介绍)?(一)
|
SQL 存储 关系型数据库
十七、用户管理
十七、用户管理
116 0
十七、用户管理