0
点赞
收藏
分享

微信扫一扫

《Linux系统管理初学者指南——基于CentOS7.6》 第三章思考与练习

青乌 1天前 阅读 1

1.Linux 中的用户管理主要涉及用户账号文件__、用户密码文件__、用户组文件___

用户账号文件/etc/passwd,用户密码文件/etc/shadow,用户组文件/etc/group和/etc/group和/ect/gshadow

2.通过id命令查看student 用户的身份信息,并将命令执行后的信息全部重定向到黑洞文件中。

[root@localhost ~]# id student 2> /dev/null uid=1000(student) gid=1000(student) 组=1000(student)

3.显示系统中用户账号的个数。

[root@localhost ~]# cat /etc/passwd | wc -l 57 [root@localhost ~]# wc -l /etc/passwd 57 /etc/passwd [root@localhost ~]# grep -v "^$" /etc/passwd | wc -l 57

4.将student用户的家目录复制到/tmp目录中,并保持源文件的属性不变。

[root@localhost ~]# grep "student" /etc/passwd
student:x:1000:1000:student:/home/student:/bin/bash
[root@localhost ~]# ls -ld /home/student
drwx------. 15 student student 4096 8月  30 05:36 /home/student
[root@localhost ~]# cp -rp /home/student /tmp
[root@localhost ~]# ls -ld /tmp/student
drwx------. 15 student student 4096 8月  30 05:36 /tmp/student

5.创建一个名为financial的组。

[root@localhost ~]# groupadd financial
[root@localhost ~]# tail -1 /etc/group
financial:x:1016:

6.创建一个名为test1的用户,指定其 UID 为1600,将financial组设置为test1用户的基本组。

[root@localhost ~]# useradd -u 1600 -g financial test1 [root@localhost ~]# id test1 uid=1600(test1) gid=1016(financial) 组=1016(financial)

7,通过非交互方式为用户 test1 设置密码123。

[root@localhost ~]# echo "123" | passwd --stdin test1 更改用户 test1 的密码 。 passwd:所有的身份验证令牌已经成功更新。 [root@localhost ~]# tail -1 /etc/shadow test1:$6$QFne1FY/inrzPuST$NUzdZTuQeHe3QHij7paVpAyoXhsBYbUMF6uwXIGSgut/wn0FSe0lqktpaCZSwx7m7XT8bn85KpDilKr1.Rc1E.:20337:0:99999:7:::

8.另外打开一个终端,以test1用户的身份登录系统。在test1的家目录中创建一个名为test1.txt的文件,并查看test1.txt文件的权限设置。

[root@localhost ~]# su - test1
[test1@localhost ~]$ tail -1 /etc/passwd
test1:x:1600:1016::/home/test1:/bin/bash
[test1@localhost ~]$ pwd
/home/test1
[test1@localhost ~]$ touch test1.txt
[test1@localhost ~]$ ls -l test1.txt
-rw-r--r--. 1 test1 financial 0 9月   6 23:05 test1.txt

9.首先对文件test1.txt进行权限设置,要求文件所有者具有读写权限,所属组和其他用户具有只读权限。然后,使test1 用户退出登录。

[test1@localhost ~]$ ls -l test1.txt -rw-r--r--. 1 test1 financial 0 9月 6 23:05 test1.txt [test1@localhost ~]$ chmod 644 test1.txt [test1@localhost ~]$ ls -l test1.txt -rw-r--r--. 1 test1 financial 0 9月 6 23:05 test1.txt [test1@localhost ~]$ exit 注销

10.将test1 用户锁定,禁止其登录。再次打开一个终端,尝试能否以test1用户的身份登录。

[root@localhost ~]# passwd -l test1 锁定用户 test1 的密码 。 passwd: 操作成功

注意:root用户可以用su切换到test1,其他用户不能切换到test1

11.将testl用户解锁,在终端中尝试能否以test1用户的身份登录。

[root@localhost ~]# passwd -u test1 解锁用户 test1 的密码。 passwd: 操作成功

12.将用户testl家目录中的test1.txt文件的所有者改为root,所属组改为users。

[root@localhost ~]# ls -l /home/test1/test1.txt -rw-r--r--. 1 test1 financial 0 9月 6 23:05 /home/test1/test1.txt [root@localhost ~]# chown root:users /home/test1/test1.txt [root@localhost ~]# ls -l /home/test1/test1.txt -rw-r--r--. 1 root users 0 9月 6 23:05 /home/test1/test1.txt

13.将test1 用户连同家目录一并删除。

[root@localhost ~]# userdel -r test1 [root@localhost ~]# ls /home admin natasha student test user4 instructor project temp01 user1 user5

14.创建用户test2,要求没有家目录,并且禁止其登录。

[root@localhost ~]# useradd -M -s /sbin/nologin test2 [root@localhost ~]# ls /home admin natasha student test user4 instructor project temp01 user1 user5 [root@localhost ~]# su - test2 su: 警告:无法更改到 /home/test2 目录: 没有那个文件或目录 This account is currently not available.

15.创建用户test3,并指定其家目录为/test3。

[root@localhost ~]# useradd -d /test3 test3 [root@localhost ~]# ls / bin etc lib64 opt root srv test3 var boot home media proc run sys tmp dev lib mnt public sbin test usr [root@localhost ~]# ls /home admin natasha student test user4 instructor project temp01 user1 user5

16.将test3用户加人root组,使roo组成为其附加组。

[root@localhost ~]# usermod -a -G root test3 [root@localhost ~]# id test3 uid=1017(test3) gid=1018(test3) 组=1018(test3),0(root)

17.将用户test3的基本组修改为users。

[root@localhost ~]# id test3 uid=1017(test3) gid=1018(test3) 组=1018(test3),0(root) [root@localhost ~]# usermod -g users test3 [root@localhost ~]# id test3 uid=1017(test3) gid=100(users) 组=100(users),0(root)

18.将用户test3的家目录/test3移动到/home目录中,然后将其家目录改为/home/test3。

[root@localhost ~]# grep "test3" /etc/passwd
test3:x:1017:100::/test3:/bin/bash
[root@localhost ~]# usermod -m -d /home/test3 test3
[root@localhost ~]# grep "test3" /etc/passwd
test3:x:1017:100::/home/test3:/bin/bash

19.当用户对目录有写权限,但对目录下的文件没有写权限时,能否修改此文件内容?能否删除此文件?

没有写权限时,不可以修改文件内容,但可以删除文件。

20.Linux文件的权限位x对目录和文件有何不同?

  1. 拥有目录的x权限表示用户可以进入该目录成为工作目录,能不能进入一个目录,只与该目录的x权限有关,如果用户对于某个目录不具有x权限,则无法切换到该目录下,也就无法执行该目录下的任何命令,即使具有该目录的r权限。且如果用户对于某目录不具有x权限,则该用户不能查询该目录下的文件的内容,如果有r 权限是可以查看该目录下的文件名列表或子目录列表的。所以要开放目录给任何人浏览时,应该至少要给与r及x权限。
  2. x权限表示该文件具有可以被系统执行的权限。文件是否能被执行就是由该权限来决定的,跟文件名没有绝对的关系。
  3. 对文件来说,具有执行文件的权限;对目录来说,该用户具有进入目录的权限。

21.新建目录/tmp/mike,并设置如下权限。

  • 将此目录的所有者设置为mike,并设置读、写和执行权限。
  • 将此目录的所属组设置为 sales,并设置读和执行权限。
  • 其他用户没有任何权限。

[root@localhost ~]# mkdir /tmp/mike [root@localhost ~]# useradd mike [root@localhost ~]# groupadd sales [root@localhost ~]# chmod 750 /tmp/mike [root@localhost ~]# chown mike:sales /tmp/mike [root@localhost ~]# ls -ld /tmp/mike drwxr-x---. 2 mike sales 6 9月 9 22:32 /tmp/mike

22,创建/var/test目录,要求在此目录中任何用户都可以创建文件或目录,但只有用户自身和 root 用户可以删除用户所创建的文件或目录。

[root@localhost ~]# mkdir /var/test [root@localhost ~]# ls -dl /var/test drwxr-xr-x. 2 root root 6 9月 9 22:44 /var/test [root@localhost ~]# chmod 1777 /var/test [root@localhost ~]# ls -dl /var/test drwxrwxrwt. 2 root root 6 9月 9 22:44 /var/test

23.新建一个名为manager 的用户组,创建两个用户账号:natasha 和 harry,并将 manager组设为这两个用户的附加组。复制文件/etc/fstab到/var/tmp目录中,对文件/var/tmp/fstab进行权限设置。

[root@centos8 ~]# groupadd manager [root@centos8 ~]# useradd natasha [root@centos8 ~]# useradd harry [root@centos8 ~]# gpasswd -a natasha manager Adding user natasha to group manager [root@centos8 ~]# gpasswd -a harry manager Adding user harry to group manager [root@centos8 ~]# id natasha uid=1003(natasha) gid=1004(natasha) groups=1004(natasha),1003(manager) [root@centos8 ~]# id harry uid=1004(harry) gid=1005(harry) groups=1005(harry),1003(manager) [root@centos8 ~]# cp /etc/fstab /var/tmp

  • 添加 ACL条目,使manager 组具有读取权限。
  • 添加 ACL条目,使用户harry具有读写权限。
  • 添加 ACL条目,使用户 natasha 没有任何权限。
  • 删除manager组的ACL条目。
  • 删除所有附加的ACL条目。

[root@centos8 ~]# setfacl -m g:manager:r-- /var/tmp/fstab
[root@centos8 ~]# setfacl -m u:harry:rw- /var/tmp/fstab
[root@centos8 ~]# setfacl -m u:natasha:--- /var/tmp/fstab
[root@centos8 ~]# getfacl /var/tmp/fstab 
getfacl: Removing leading '/' from absolute path names
# file: var/tmp/fstab
# owner: root
# group: root
user::rw-
user:natasha:---
user:harry:rw-
group::r--
group:manager:r--
mask::rw-
other::r--

[root@centos8 ~]# setfacl -x g:manager /var/tmp/fstab 
[root@centos8 ~]# getfacl /var/tmp/fstab 
getfacl: Removing leading '/' from absolute path names
# file: var/tmp/fstab
# owner: root
# group: root
user::rw-
user:natasha:---
user:harry:rw-
group::r--
mask::rw-
other::r--

[root@centos8 ~]# setfacl -b  /var/tmp/fstab 
[root@centos8 ~]# getfacl /var/tmp/fstab 
getfacl: Removing leading '/' from absolute path names
# file: var/tmp/fstab
# owner: root
# group: root
user::rw-
group::r--
other::r--

24、假设/var/test目录的所属组是users,要求对/var/test目录进行权限设置,使得任何用户在该目录中所创建的文件或子目录的所属组都自动使用 users组。

[root@centos8 ~]# mkdir /var/test [root@centos8 ~]# ls -ld /var/test drwxr-xr-x. 2 root root 6 Sep 12 17:02 /var/test [root@centos8 ~]# chown -R :users /var/test [root@centos8 ~]# ls -ld /var/test drwxr-xr-x. 2 root users 6 Sep 12 17:02 /var/test [root@centos8 ~]# chmod g+s /var/test [root@centos8 ~]# ls -ld /var/test drwxr-sr-x. 2 root users 6 Sep 12 17:02 /var/test [root@centos8 ~]# touch /var/test/1.txt [root@centos8 ~]# ll /var/test/1.txt -rw-r--r--. 1 root users 0 Sep 12 17:09 /var/test/1.txt

25.假设普通用户student已经被授权,要求以该用户身份将系统的IP地址修改为192.168.80.10/24。

[root@centos8 ~]# vim /etc/sudoers

增加:

student ALL=(ALL) ALL

[root@centos8 ~]# sudo vi /etc/sysconfig/network-scripts/ifcfg-ens160

修改:BOOTPROTO=static

增加:IPADDR=192.168.80.10 gateway=192.168.230.1 netmask=255.255.255.0 dns1=8.8.8.8

26.为/etc/passwd文件添加只读属性。

[root@localhost ~]# chattr +i /etc/passwd [root@localhost ~]# lsattr /etc/passwd ----i--------------- /etc/passwd

27.将/var/log/messages文件设置为只能向其中追加写入数据,但不能删除原有数据。

[root@localhost ~]# chattr +a /var/log/messages [root@localhost ~]# lsattr /var/log/messages -----a-------------- /var/log/messages

28.在系统中查找所有人都有写权限的目录。

[root@localhost ~]# find / -perm -222 -type d -ls 2> /dev/null

举报

相关推荐

0 条评论