1、显示/etc目录下,以非字母开头,后面跟了一个字母以及其它任意长度任意字符的文件或目录

1 [root@centos8 etc]#touch 5a.txt 9a.txt
2 [root@centos8 etc]#mkdir 3eeee
3 [root@centos8 etc]#ls -d /etc/[^[:alpha:]][[:alpha:]]*
4 /etc/3eeee /etc/5a.txt /etc/9a.txt

2、复制/etc目录下所有以p开头,以非数字结尾的文件或目录到/tmp/mytest1目录中。

1 [root@centos8 ~]#mkdir -p /tmp/mytest1
2 [root@centos8 ~]#cd /tmp/mytest1
3 [root@centos8 mytest1]#cp -rf /etc/p[[:alpha:]]*[^[:digit:]] /tmp/mytest1
4 [root@centos8 mytest1]#ls /tmp/mytest1
5 pam.d passwd pbm2ppa.conf pipewire plymouth popt.d prelink.conf.d profile protocols
6 papersize passwd- pinforc pki pnm2ppa.conf postfix printcap profile.d pulse

3、将/etc/issue文件中的内容转换为大写后保存至/tmp/issue.out文件中

 1 [root@centos8 ~]#ls /etc/issue
2 /etc/issue
3 [root@centos8 ~]#cat /etc/issue
4 \S
5 Kernel \r on an \m
6 [root@centos8 ~]#cat /etc/issue |tr a-z A-Z
7 \S
8 KERNEL \R ON AN \M
9 [root@centos8 ~]#cat /etc/issue |tr a-z A-Z >> /tmp/issue.out
10 [root@centos8 ~]#cat /tmp/issue.out
11 \S
12 KERNEL \R ON AN \M

4、请总结描述用户和组管理类命令的使用方法并完成以下练习:

用户管理命令
useradd
usermod
userdel

组帐号维护命令
groupadd
groupmod
groupdel


4.1 useradd 用户创建

useradd 命令可以创建新的Linux用户
格式:

useradd [options] LOGIN

-u UID: 新UID
-g GID: 新主组
-G GROUP1[,GROUP2,...[,GROUPN]]]:新附加组,原来的附加组将会被覆盖;若保留原有,则要同时使
用-a选项
-s SHELL:新的默认SHELL
-c 'COMMENT':新的注释信息
-d HOME: 新家目录不会自动创建;若要创建新家目录并移动原家数据,同时使用-m选项
-l login_name: 新的名字
-L: lock指定用户,在/etc/shadow 密码栏的增加 !
-U: unlock指定用户,将 /etc/shadow 密码栏的 ! 拿掉
-e YYYY-MM-DD: 指明用户账号过期日期
-f INACTIVE: 设定非活动期限,即宽限期


4.2 usermod 用户属性修改

usermod 命令可以修改用户属性
格式:

usermod [OPTION] login

常见选项:

-u UID: 新UID
-g GID: 新主组
-G GROUP1[,GROUP2,...[,GROUPN]]]:新附加组,原来的附加组将会被覆盖;若保留原有,则要同时使
用-a选项
-s SHELL:新的默认SHELL
-c 'COMMENT':新的注释信息
-d HOME: 新家目录不会自动创建;若要创建新家目录并移动原家数据,同时使用-m选项
-l login_name: 新的名字
-L: lock指定用户,在/etc/shadow 密码栏的增加 !
-U: unlock指定用户,将 /etc/shadow 密码栏的 ! 拿掉
-e YYYY-MM-DD: 指明用户账号过期日期
-f INACTIVE: 设定非活动期限,即宽限期

4.3 userdel 删除用户

userdel 可删除Linux 用户
格式:

userdel [OPTION]... Login

常见选项:

-f, --force 强制
-r, --remove 删除用户家目录和邮箱

4.4 groupadd 创建组

格式:

groupadd [OPTION]... group_name

常见选项:

-g GID 指明GID号;[GID_MIN, GID_MAX]
-r 创建系统组,CentOS 6之前: ID<500,CentOS 7以后: ID<1000

4.5 groupmod 组属性修改

格式:

groupmod [OPTION]... group

常见选项:

-n group_name: 新名字
-g GID: 新的GID

4.6 groupdel 删除组

格式:

groupdel [options] GROUP

常见选项:

-f, --force 强制删除,即使是用户的主组也强制删除组,但会导致无主组的用户不可用无法登录

4.7 查看用户相关的ID信息

id 命令可以查看用户的UID,GID等信息

id [OPTION]... [USER]

常见选项

-u: 显示UID
-g: 显示GID
-G: 显示用户所属的组的ID
-n: 显示名称,需配合ugG使用

4.8 切换用户或以其他用户身份执行命令

su: 即 switch user,命令可以切换用户身份,并且以指定用户的身份执行命令

格式:

su [options...] [-] [user [args...]]

常见选项:

-l --login su -l UserName 相当于 su - UserName
-c, --command <command> pass a single command to the shell with -c

4.9 设置密码

passwd 可以修改用户密码
格式:

passwd [OPTIONS] UserName

常用选项:

-d:删除指定用户密码
-l:锁定指定用户
-u:解锁指定用户
-e:强制用户下次登录修改密码
-f:强制操作
-n mindays:指定最短使用期限
-x maxdays:最大使用期限
-w warndays:提前多少天开始警告
-i inactivedays:非活动期限
--stdin:从标准输入接收用户密码,Ubuntu无此选项

4.10 修改用户密码策略

chage 可以修改用户密码策略
格式:

chage [OPTION]... LOGIN

常用选项:

-d LAST_DAY #更改密码的时间
-m --mindays MIN_DAYS
-M --maxdays MAX_DAYS
-W --warndays WARN_DAYS
-I --inactive INACTIVE #密码过期后的宽限期
-E --expiredate EXPIRE_DATE #用户的有效期
-l 显示密码策略

4.11 临时切换主组
newgrp 命令可以临时切换主组, 如果用户本不属于此组,则需要组密码
格式:

newgrp [-] [group]

4.12 更改和查看组成员
groupmems 可以管理附加组的成员关系
格式

groupmems [options] [action]

常见选项

-g, --group groupname #更改为指定组 (只有root)
-a, --add username #指定用户加入组
-d, --delete username #从组中删除用户
-p, --purge #从组中清除所有成员
-l, --list #显示组成员列表

4.13 groups 可查看用户组关系

格式

#查看用户所属组列表
groups [OPTION].[USERNAME]...

 

(1)、创建组distro,其GID为2019;

1 [root@centos8 ~]#groupadd -g 2019  distro

(2)、创建用户mandriva, 其ID号为1005;基本组为distro;

1 [root@centos8 ~]#useradd -u 1005 -g distro mandriva
2 useradd: user 'mandriva' already exists
3 [root@centos8 ~]#getent passwd mandriva
4 mandriva:x:1001:1001::/home/mandriva:/bin/bash

(3)、创建用户mageia,其ID号为1100,家目录为/home/linux;

1 [root@centos8 ~]#useradd -u 1100 -d /home/linux mageia
2 [root@centos8 ~]#getent passwd mageia
3 mageia:x:1100:1100::/home/linux:/bin/bash

(4)、给用户mageia添加密码,密码为mageedu,并设置用户密码7天后过期

1 [root@centos8 ~]#passwd mageia
2 Changing password for user mageia.
3 New password:
4 BAD PASSWORD: The password is shorter than 8 characters
5 Retype new password:
6 passwd: all authentication tokens updated successfully.
7 [root@centos8 ~]#chage -E 7 mageia

(5)、删除mandriva,但保留其家目录;

1 [root@centos8 ~]#userdel mandriva
2 [root@centos8 ~]#id mandriva
3 id: ‘mandriva’: no such user
4 [root@centos8 ~]#getent passwd mandriva
5 [root@centos8 ~]#ll /home
6 total 4
7 drwx------. 16 brucelebron brucelebron 4096 Jan 10 09:53 brucelebron
8 drwx------ 3 mageia mageia 78 Jan 13 21:06 linux
9 drwx------ 3 1001 1001 78 Jan 13 20:40 mandriva

(6)、创建用户slackware,其ID号为2002,基本组为distro,附加组peguin;

1 [root@centos8 ~]#groupadd peguin
2 [root@centos8 ~]#useradd -u 2002 -g distro -G peguin slackware
3 [root@centos8 ~]#id slackware
4 uid=2002(slackware) gid=2019(distro) groups=2019(distro),2020(peguin)

(7)、修改slackware的默认shell为/bin/tcsh;

1 [root@centos8 ~]#usermod -s /bin/tcsh slackware
2 [root@centos8 ~]#getent passwd slackware
3 slackware:x:2002:2019::/home/slackware:/bin/tcsh

(8)、为用户slackware新增附加组admins,并设置不可登陆。

1 [root@centos8 ~]#usermod  -G admins slackware
2 [root@centos8 ~]#usermod -s /sbin/nologin slackware
3 [root@centos8 ~]#su slackware
4 This account is currently not available.

5、创建用户user1、user2、user3。在/data/下创建目录test

1 [root@centos8 ~]#mkdir -p /data/test
2 [root@centos8 ~]#useradd user1;useradd user2;useradd user3;
3 useradd: user 'user1' already exists
4 useradd: user 'user2' already exists
5 useradd: user 'user3' already exists

(1)、目录/data/test属主、属组为user1

1 [root@centos8 data]#chown user1:user1 /data/test
2 [root@centos8 data]#ll
3 total 0
4 drwxr-xr-x 2 user1 user1 6 Jan 13 21:49 test

(2)、在目录属主、属组不变的情况下,user2对文件有读写权限

1 [user2@centos8 data]$echo hello >> a.txt
2 bash: a.txt: Permission denied
3 [user2@centos8 data]$exit
4 exit
5 [root@centos8 data]#setfacl -m u:user2:rw a.txt
6 [root@centos8 data]#su - user2 -c "echo hello >> a.txt
7 > hello

(3)、user1在/data/test目录下创建文件a1.sh, a2.sh, a3.sh, a4.sh,设置所有用户都不可删除1.sh,2.sh文件、除了user1及root之外,所有用户都不可删除a3.sh, a4.sh

 1 [user1@centos8 root]$cd /data/test
2 [user1@centos8 test]$touch a{1..4}.sh
3 [user1@centos8 test]$ll
4 total 0
5 -rw-rw-r-- 1 user1 user1 0 Jan 13 22:47 a1.sh
6 -rw-rw-r-- 1 user1 user1 0 Jan 13 22:47 a2.sh
7 -rw-rw-r-- 1 user1 user1 0 Jan 13 22:47 a3.sh
8 -rw-rw-r-- 1 user1 user1 0 Jan 13 22:47 a4.sh
9 [user1@centos8 test]$chattr +i a1.sh a2.sh
10 chattr: Operation not permitted while setting flags on a1.sh
11 chattr: Operation not permitted while setting flags on a2.sh
12 [user1@centos8 test]$chmod o+x a3.sh a4.sh
13 [user1@centos8 test]$chmod o+t a3.sh a4.sh

(4)、user3增加附加组user1,同时要求user1不能访问/data/test目录及其下所有文件

 1 [root@centos8 ~]#usermod user3 -G user1
2 [root@centos8 ~]#id user3
3 uid=2005(user3) gid=2005(user3) groups=2005(user3),2003(user1)
4 [root@centos8 ~]#setfacl -m u:user:- /data/test
5 setfacl: Option -m: Invalid argument near character 3
6 [root@centos8 ~]#setfacl -m u:user1:- /data/test
7 [root@centos8 ~]#getfacl /data/test/
8 getfacl: Removing leading '/' from absolute path names
9 # file: data/test/
10 # owner: user1
11 # group: user1
12 user::rwx
13 user:user1:---
14 group::r-x
15 mask::r-x
16 other::r-x

(5)、清理/data/test目录及其下所有文件的acl权限

1 [root@centos8 ~]#setfacl -b /data/test/
2 [root@centos8 ~]#getfacl /data/test/
3 getfacl: Removing leading '/' from absolute path names
4 # file: data/test/
5 # owner: user1
6 # group: user1
7 user::rwx
8 group::r-x
9 other::r-x

最新文章

  1. 如何在没有域的环境中搭建AlwaysOn(一)
  2. gradle中使用嵌入式(embedded) tomcat, debug 启动
  3. PV 与 并发数 之间的故事
  4. .net概念之程序集说明
  5. Papa Parse – 超强大的多线程 CSV 文本解析库
  6. UVA1629Cake slicing(记忆化搜索)
  7. ubuntu14.04安装OpenVirteX
  8. codeforces B. Sereja and Stairs 解题报告
  9. Oracle一列的多行数据拼成一行显示字符
  10. 线段树—Lazy_Tag
  11. user is not mapped
  12. Java jdbc数据库连接池总结!(转)
  13. 疯狂学习java web5(SSI框架)
  14. 项目Splash页面的开发与设计
  15. Bitmap的读写
  16. Windows下PythonQt3.2使用pandas.pivot_table
  17. 架构探险笔记3-搭建轻量级Java web框架
  18. python-MongoDB 非关系型数据库
  19. Git 打标签(分布式版本控制系统)
  20. Winform判断EventHandler是否已经添加

热门文章

  1. [论文总结] kmeans聚类和WGCNA
  2. S2-008
  3. C++ 编译依赖管理系统分析以及 srcdep 介绍
  4. A排列方案
  5. Java学习笔记:2022年1月9日(其二)
  6. Java安全之JDBC Attacks学习记录
  7. ArcGIS实现打点、线路图、色块、自定义弹窗
  8. Hugging Face - 推理(Inference)解决方案
  9. 跟着廖雪峰学python 003
  10. 你知道CDN是干嘛的吗?