No.2 Linux文件管理和用户权限配置

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

#查看是否存在非字母开头的文件
[root@localhost home]# ls /etc/[^[:alpha:]]*
ls: cannot access /etc/[^[:alpha:]]*: No such file or directory
#手动创建文件用于实验
[root@localhost etc]# mkdir 123{a..c}
[root@localhost etc]# touch _456{a..c}
#查找符合要求的文件
[root@localhost etc]# ll -d /etc/[^[:alpha:]]*
drwxr-xr-x. 2 root root 4096 Sep  6 11:04 /etc/123a
drwxr-xr-x. 2 root root 4096 Sep  6 11:04 /etc/123b
drwxr-xr-x. 2 root root 4096 Sep  6 11:04 /etc/123c
-rw-r--r--. 1 root root    0 Sep  6 11:05 /etc/_456a
-rw-r--r--. 1 root root    0 Sep  6 11:05 /etc/_456b
-rw-r--r--. 1 root root    0 Sep  6 11:05 /etc/_456c

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

#查看p开头的文件或目录
[root@localhost etc]# ll -d p*
drwxr-xr-x.  2 root root 4096 Aug 24 21:56 pam.d
-rw-r--r--.  1 root root 1192 Aug 24 21:56 passwd
-rw-r--r--.  1 root root 1192 Aug 24 21:56 passwd-
-rw-r--r--.  1 root root 2872 Jun 10  2014 pinforc
drwxr-xr-x.  3 root root 4096 Aug 24 21:50 pkcs11
drwxr-xr-x. 10 root root 4096 Aug 24 21:51 pki
drwxr-xr-x.  2 root root 4096 Aug 24 21:51 plymouth
drwxr-xr-x.  5 root root 4096 Aug 24 21:50 pm
drwxr-xr-x.  5 root root 4096 Aug 24 21:51 polkit-1
drwxr-xr-x.  2 root root 4096 Jun 10  2014 popt.d
drwxr-xr-x.  2 root root 4096 Aug 24 21:51 postfix
drwxr-xr-x.  3 root root 4096 Aug 24 21:51 ppp
drwxr-xr-x.  2 root root 4096 Aug 24 21:51 prelink.conf.d
-rw-r--r--.  1 root root  233 Jun  7  2013 printcap
-rw-r--r--.  1 root root 1875 Aug 25 10:29 profile
drwxr-xr-x.  2 root root 4096 Aug 24 21:51 profile.d
-rw-r--r--.  1 root root 6545 Apr  1  2020 protocols
drwxr-xr-x.  2 root root 4096 Aug 24 21:50 python

#查看p开通数字结尾的文件或目录
[root@localhost etc]# ll -d p*[0-9]
drwxr-xr-x. 3 root root 4096 Aug 24 21:50 pkcs11
drwxr-xr-x. 5 root root 4096 Aug 24 21:51 polkit-1

#查看p开通非数字结尾的文件或目录
[root@localhost etc]# ll -d p*[^0-9]
drwxr-xr-x.  2 root root 4096 Aug 24 21:56 pam.d
-rw-r--r--.  1 root root 1192 Aug 24 21:56 passwd
-rw-r--r--.  1 root root 1192 Aug 24 21:56 passwd-
-rw-r--r--.  1 root root 2872 Jun 10  2014 pinforc
drwxr-xr-x. 10 root root 4096 Aug 24 21:51 pki
drwxr-xr-x.  2 root root 4096 Aug 24 21:51 plymouth
drwxr-xr-x.  5 root root 4096 Aug 24 21:50 pm
drwxr-xr-x.  2 root root 4096 Jun 10  2014 popt.d
drwxr-xr-x.  2 root root 4096 Aug 24 21:51 postfix
drwxr-xr-x.  3 root root 4096 Aug 24 21:51 ppp
drwxr-xr-x.  2 root root 4096 Aug 24 21:51 prelink.conf.d
-rw-r--r--.  1 root root  233 Jun  7  2013 printcap
-rw-r--r--.  1 root root 1875 Aug 25 10:29 profile
drwxr-xr-x.  2 root root 4096 Aug 24 21:51 profile.d
-rw-r--r--.  1 root root 6545 Apr  1  2020 protocols
drwxr-xr-x.  2 root root 4096 Aug 24 21:50 python

#复制符合要求的文件或目录到/tmp/mytest1下
[root@localhost etc]# cp -ra  p*[^0-9] /tmp/mytest1/
#查看复制后的目标目录列表
[root@localhost etc]# ll /tmp/mytest1/
total 68
drwxr-xr-x.  2 root root 4096 Aug 24 21:56 pam.d
-rw-r--r--.  1 root root 1192 Aug 24 21:56 passwd
-rw-r--r--.  1 root root 1192 Aug 24 21:56 passwd-
-rw-r--r--.  1 root root 2872 Jun 10  2014 pinforc
drwxr-xr-x. 10 root root 4096 Aug 24 21:51 pki
drwxr-xr-x.  2 root root 4096 Aug 24 21:51 plymouth
drwxr-xr-x.  5 root root 4096 Aug 24 21:50 pm
drwxr-xr-x.  2 root root 4096 Jun 10  2014 popt.d
drwxr-xr-x.  2 root root 4096 Aug 24 21:51 postfix
drwxr-xr-x.  3 root root 4096 Aug 24 21:51 ppp
drwxr-xr-x.  2 root root 4096 Aug 24 21:51 prelink.conf.d
-rw-r--r--.  1 root root  233 Jun  7  2013 printcap
-rw-r--r--.  1 root root 1875 Aug 25 10:29 profile
drwxr-xr-x.  2 root root 4096 Aug 24 21:51 profile.d
-rw-r--r--.  1 root root 6545 Apr  1  2020 protocols
drwxr-xr-x.  2 root root 4096 Aug 24 21:50 python

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

#查看文件原有内容
[root@localhost etc]# cat /etc/issue
\S
Kernel \r on an \m
#将文件内容使用tr命令转换成大写后,输出到屏幕,方便查看效果。
[root@localhost etc]# cat /etc/issue | tr 'a-z' 'A-Z'
\S
KERNEL \R ON AN \M
#将转换后的内存重定向到目标文件中
[root@localhost etc]# cat /etc/issue | tr 'a-z' 'A-Z' >>/tmp/issue.out
[root@localhost etc]# 
#查看目标文件内容
[root@localhost etc]# cat /tmp/issue.out
\S
KERNEL \R ON AN \M
[root@localhost etc]# 

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

用户管理命令:
useradd 创建用户
常见选项:
-u UID
-g GID 指明用户所属基本组,可为组名,也可以GID
-d HOME_DIR 以指定的路径(不存在)为家目录
-s SHELL 指明用户的默认shell程序,可用列表在/etc/shells文件中
-G GROUP1[,GROUP2,...] 为用户指明附加组,组须事先存在
-p 指定加密的密码
---------------------
usermod 修改用户属性:
-u UID:UID
-g GID: 新主组
-G GROUP1[,GROUP2,...[,GROUPN]]]:新附加组,原来的附加组将会被覆盖;若保留原有,则要同时使用-a选项
-s SHELL:新的默认SHELL
-d HOME: 新家目录不会自动创建;若要创建新家目录并移动原家数据,同时使用-m选项
-e YYYY-MM-DD: 指明用户账号过期日期
-f INACTIVE: 设定非活动期限,即宽限期
---------------------
userdel 删除用户
-f, --force   强制
-r, --remove 删除用户家目录和邮箱
id 查看用户信息
-u: 显示UID
-g: 显示GID
-G: 显示用户所属的组的ID
-n: 显示名称,需配合ugG使用
组账号维护命令:
groupadd 创建组
-g GID 指明GID号;[GID_MIN, GID_MAX]
-r 创建系统组,CentOS 6之前: ID<500,CentOS 7以后: ID<1000
------------------
groupmod 修改组
-n group_name: 新名字
-g GID: 新的GID
------------------
gpasswd 更改组密码,也可以修改附加组的成员关系
-a user 将user添加至指定组中
-d user 从指定附加组中移除用户user
-A user1,user2,... 设置有管理权限的用户列表
------------------
groupmems 修改组成员关系
-g, --group groupname   #更改为指定组(只有root)
-a, --add username     #指定用户加入组
-d, --delete username #从组中删除用户
-p, --purge               #从组中清除所有成员
-l,  --list                 #显示组成员列表
------------------
groupdel 删除组
-f, --force 强制删除,即使是用户的主组也强制删除组,但会导致无主组的用户不可用无法登录
passwd 设置和修改用户密码
gpasswd 设置和修改组密码
getent 用来察看系统的数据库中的相关记录
finger查看用户个人信息
su 切换用户身份 sudo 查看和赋予用户权限

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

[root@localhost etc]# groupadd -g  2019  distro
[root@localhost ~]# getent group distro
distro:x:2019:

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

[root@localhost etc]# useradd mandriva -u 1005 -g distro 
[root@localhost etc]# id mandriva
uid=1005(mandriva) gid=2019(distro) groups=2019(distro)

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

[root@localhost etc]# useradd mageia -u 1100 -d /home/linux
[root@localhost ~]# su - mageia
[mageia@localhost ~]$ pwd
/home/linux
[mageia@localhost ~]$

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

[root@localhost ~]# echo "mageedu" | passwd --stdin mageia
Changing password for user mageia.
passwd: all authentication tokens updated successfully.
[root@localhost ~]# passwd -x 7 mageia 
Adjusting aging data for user mageia.
passwd: Success
[root@localhost ~]# cat /etc/shadow | grep mageia
mageia:$6$x3BTlfZj$5lfMAhPfU9ls1VvfJGhE0UYoW5D9xOLZl2Dm4YW1Ro8lZ7hmrf03cricxSeuVvyBcIhPMcpqTSaDrBBh.VxAT.:18876:0:7:7:::

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

[root@localhost home]# id mandriva
uid=1005(mandriva) gid=2019(distro) groups=2019(distro)
[root@localhost home]# ls
distro  guo  linux  mage  mandriva
[root@localhost home]# userdel mandriva 
[root@localhost home]# ls
distro  guo  linux  mage  mandriva
[root@localhost home]# id mandriva
id: mandriva: no such user

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

[root@localhost home]# getent group peguin
[root@localhost home]# 

[root@localhost home]# groupadd peguin
[root@localhost home]# getent group peguin
peguin:x:2020:
[root@localhost home]# useradd slackware -u 2002 -g distro
[root@localhost home]# id slackware
uid=2002(slackware) gid=2019(distro) groups=2019(distro)
[root@localhost home]#
[root@localhost home]# usermod -G peguin slackware 
[root@localhost home]# id slackware
uid=2002(slackware) gid=2019(distro) groups=2019(distro),2020(peguin)
······················
方法2
[root@localhost home]#useradd -u 2002 slackware -g distro -G peguin

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

[root@localhost home]# usermod -s /bin/tcsh slackware 
[root@localhost home]# cat /etc/passwd | grep slackware
slackware:x:2002:2019::/home/slackware:/bin/tcsh

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

[root@localhost home]# usermod -G admins -s /sbin/nologin slackware -a
[root@localhost home]# id slackware
uid=2002(slackware) gid=2019(distro) groups=2019(distro),2020(peguin),2021(admins)

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

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

[root@localhost data]# chown -R user1:user1 test
[root@localhost data]# 
[root@localhost data]# ll -d test/
drwxr-xr-x. 2 user1 user1 4096 Sep  6 21:40 test/
[root@localhost data]# 

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

[root@localhost data]# chmod o+w test
[root@localhost data]# ll -d test
drwxr-xrwx. 2 user1 user1 4096 Sep  6 21:40 test
---------------------
[root@localhost data]# setfacl -m u:user2:rw test
[root@localhost data]# getfacl test
# file: test
# owner: user1
# group: user1
user::rwx
user:user2:rw-
group::r-x
mask::rwx
other::r-x
[root@localhost data]# 

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

[root@localhost test]# chattr +i a1.sh a2.sh 
[root@localhost test]# lsattr
----i--------e-- ./a2.sh
-------------e-- ./a3.sh
-------------e-- ./a4.sh
----i--------e-- ./a1.sh
[root@localhost test]# rm -f a1.sh 
rm: cannot remove ‘a1.sh’: Operation not permitted
[root@localhost test]# rm -f a2.sh 
rm: cannot remove ‘a2.sh’: Operation not permitted
-------------------------------
[root@localhost test]# chmod o+t a3.sh 
[root@localhost test]# chmod o+t a4.sh 
[root@localhost test]# 
[root@localhost test]# ll
total 0
-rw-rw-r--. 1 user1 user1 0 Sep  6 21:47 a1.sh
-rw-rw-r--. 1 user1 user1 0 Sep  6 21:47 a2.sh
-rw-rw-r-T. 1 user1 user1 0 Sep  6 21:47 a3.sh
-rw-rw-r-T. 1 user1 user1 0 Sep  6 21:47 a4.sh
[root@localhost test]# su user2
[user2@localhost test]$ rm -f a3.sh
rm: cannot remove ‘a3.sh’: Permission denied
[root@localhost test]# su - user1
Last login: Mon Sep  6 21:46:40 CST 2021 on pts/0
[user1@localhost ~]$ cd /data/test/
[user1@localhost test]$ ls
a1.sh  a2.sh  a3.sh  a4.sh
[user1@localhost test]$ rm -f a3.sh 

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

[root@localhost test]# id user3
uid=2005(user3) gid=2005(user3) groups=2005(user3)
[root@localhost test]# 
[root@localhost test]# 
[root@localhost test]# usermod -G user1 user3
[root@localhost test]# id user3
uid=2005(user3) gid=2005(user3) groups=2005(user3),2003(user1)
---------------------
[root@localhost test]# setfacl -m u:user1:- /data/test/
[root@localhost test]# getfacl /data/test
getfacl: Removing leading '/' from absolute path names
# file: data/test
# owner: user1
# group: user1
user::rwx
user:user1:---
user:user2:rw-
group::r-x
mask::rwx
other::r-x

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

[root@localhost test]# setfacl -Rb /data/test
[root@localhost test]# 
[root@localhost test]# chattr -i a1.sh 
[root@localhost test]# chattr -i a2.sh 
[root@localhost test]# 
[root@localhost test]# getfacl /data/test
getfacl: Removing leading '/' from absolute path names
# file: data/test
# owner: user1
# group: user1
user::rwx
group::r-x
other::r-x
[root@localhost test]# lsattr
-------------e-- ./a2.sh
-------------e-- ./a3.sh
-------------e-- ./a4.sh
-------------e-- ./a1.sh
[root@localhost test]# ll
total 0
-rw-rw-r--. 1 user1 user1 0 Sep  6 21:47 a1.sh
-rw-rw-r--. 1 user1 user1 0 Sep  6 21:47 a2.sh
-rw-rw-r-t. 1 user1 user1 0 Sep  6 21:59 a3.sh
-rw-rw-r-t. 1 user1 user1 0 Sep  6 21:47 a4.sh
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值