作业练习

一、

1.每天将/etc/目录下所有文件,备份到/data独立的子目录下,并要求子目录格式为backupYYYY-mm-dd,备份过程可见。

[root@centos7 data]# pwd
/data
[root@centos7 data]# ll
total 0
[root@centos7 data]# cp -av /etc/ /data/backup`date +%F`
[root@centos7 data]# ll
total 12
drwxr-xr-x. 76 root root 8192 Mar 24 11:51 backup2021-03-24

二、

(1)如何创建/testdir/dir1/x,/testdir/dir1/y, /testdir/dir1/x/a, /testdir/dir1/x/b, /testdir/dir1/y/a, /testdir/dir1/y/b

[root@centos7 data]# mkdir -p testdir/dir1/{x,y}/{a,b}
[root@centos7 data]# tree testdir/
testdir/
└── dir1
    ├── x
    │   ├── a
    │   └── b
    └── y
        ├── a
        └── b

7 directories, 0 files

(2)如何创建/testdir/dir2/x, /testdir/dir2/y, /testdir/dir2/x/a, /testdir/dir2/x/b

[root@centos7 data]# mkdir -p testdir/dir2/{x,y} testdir/dir2/x/{a,b}
[root@centos7 data]# mkdir -p testdir/dir2/{x/{a,b},y} [root@centos7 data]# tree testdir
/ testdir/ ├── dir1 │   ├── x │   │   ├── a │   │   └── b │   └── y │   ├── a │   └── b └── dir2 ├── x │   ├── a │   └── b └── y 12 directories, 0 files

(3)如何创建/testdir/dir3, /testdir/dir4, /testdir/dir5, /testdir/dir5/dir6, /testdir/dir5/dir7

[root@centos7 data]# mkdir -p testdir/dir{3..5} testdir/dir5/dir{6,7}
[root@centos7 data]# mkdir -p testdir/dir{3,4,5/dir{6,7}} [root@centos7 data]# tree testdir
/ testdir/ ├── dir1 │   ├── x │   │   ├── a │   │   └── b │   └── y │   ├── a │   └── b ├── dir2 │   ├── x │   │   ├── a │   │   └── b │   └── y ├── dir3 ├── dir4 └── dir5 ├── dir6 └── dir7 17 directories, 0 files

 三、

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

[root@centos7 data]# cat /etc/issue
S
Kernel 
 on an m

[root@centos7 data]# cat /etc/issue |tr [a-z] [A-Z] > /tmp/issue.out
[root@centos7 data]# cat /tmp/issue.out 
S
KERNEL R ON AN M

2、将当前系统登录用户的信息转换为大写后保存至/tmp/who.out文件中

[root@centos7 data]# who | tr [a-z] [A-Z] > /tmp/who.out
[root@centos7 data]# cat /tmp/who.out 
ROOT     TTY1         2021-03-24 11:26
ROOT     PTS/2        2021-03-24 14:09 (10.0.0.1)
[root@centos7 data]# 

3、一个linux用户给root发邮件,要求邮件标题为"help",邮件正文如下:
Hello, l am 用户名,The system version is here,please help me to check it ,thanks!
操作系统版本信息

[wang@centos7 ~]$ id
uid=1000(wang) gid=1000(wang) groups=1000(wang) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[wang@centos7 ~]$ echo Hello, I am `whoami`,The system version is here,please help me to check it ,thanks! 
 `cat /etc/redhat-release` | mail -s 'help' root
[wang@centos7 ~]$ 


#root
[root@centos7 data]# mail
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/root": 4 messages 1 new 2 unread
 U  1 Mail Delivery Subsys  Mon Mar 22 14:31  71/2726  "Returned mail: see transcript for details"
    2 Mail Delivery Subsys  Mon Mar 22 14:32  71/2727  "Returned mail: see transcript for details"
    3 wang@localhost.local  Wed Mar 24 14:52  21/774   "help"
>N  4 wang@localhost.local  Wed Mar 24 14:53  20/875   "help"
& 4
Message  4:
From wang@localhost.localdomain  Wed Mar 24 14:53:07 2021
Return-Path: <wang@localhost.localdomain>
From: wang@localhost.localdomain
Date: Wed, 24 Mar 2021 14:53:07 +0800
To: root@localhost.localdomain
Subject: help
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
Status: R

Hello, I am wang,The system version is here,please help me to check it ,thanks! 
 CentOS Linux release 7.9.2009 (Core)

& 

4、将/root/下文件列表,显示成一行,并文件名之间用空格隔开

[root@centos7 data]# ls /root
anaconda-ks.cfg  a.txt  dead.letter  mail.rc
[root@centos7 data]# ls /root/ | tr "
" ' '
anaconda-ks.cfg a.txt dead.letter mail.rc 

5、计算1+2+3+..+99+100的总和

[root@centos7 data]# seq -s + 100|bc
5050
[root@centos7 data]# 

6、删除Windows文本文件中的回车字符,即“ ”

[root@centos7 data]# file win.txt 
win.txt: ASCII text, with CRLF line terminators
[root@centos7 data]# file linux.txt 
linux.txt: ASCII text
[root@centos7 data]# hexdump -c win.txt 
0000000   a   s   d   a   s   d  
  
   a   s   d   n   k   j  
  

0000010   a   s   d   a   s   d  
  
   q   j   i   w   o   e   1   2
0000020   3  
  
   3   4   j   9   j   a   2                        
000002a
[root@centos7 data]# hexdump -c linux.txt 
0000000   a   s   d   a   s  
   m   s   k   d   l   m   f   l   s  

0000010   a   m   s   l   k   d  
   q   m   w   e   l  
            
000001d
[root@centos7 data]# dos2unix win.txt 
dos2unix: converting file win.txt to Unix format ...
[root@centos7 data]# hexdump -c win.txt 
0000000   a   s   d   a   s   d  
   a   s   d   n   k   j  
   a   s
0000010   d   a   s   d  
   q   j   i   w   o   e   1   2   3  
   3
0000020   4   j   9   j   a   2                                        
0000026

#方法2
[root@centos7 data]# tr -d "
" < win.txt

7、处理字符串“xt,I 1 jr#!$mn2 c*/fe 3 uz4" ,只保留其中的数字和空格

[root@centos7 data]# echo xt,I 1 jr#uz4mn2 c*/fe 3 uz4 | tr -d [[:punct:]][[:alpha:]]
 1 42  3 4

8、将PATH变量每个目录显示在独立的一行

[root@centos7 data]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@centos7 data]# echo $PATH | tr : \n
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/root/bin

 四、

1.显示/etc目录下所有以l开头,以一个小写字母结尾,且中间出现至少一位数字的文件或目录列表

[root@centos7 data]# ll /etc/l*[[:digit:]]*[[:lower:]]

 [root@centos7 data]# find /etc/ -name "l*[[:digit:]]*[[:lower:]]"
 /etc/selinux/targeted/active/modules/100/l2tp

2.显示/etc目录下以任意-一位数字开头,且以非数字结尾的文件或目录列表

[root@centos7 data]# ll /etc/[[:digit:]]*[^[:digit:]]
[root@centos7 data]# find /etc/ -name "[[:digit:]]*[^[:digit:]]"
/etc/grub.d/00_header
/etc/grub.d/01_users
/etc/grub.d/10_linux
/etc/grub.d/20_linux_xen
/etc/grub.d/20_ppc_terminfo
/etc/grub.d/30_os-prober
/etc/grub.d/40_custom
/etc/grub.d/41_custom
/etc/grub.d/00_tuned
/etc/sysctl.d/99-sysctl.conf
/etc/profile.d/256term.csh
/etc/profile.d/256term.sh
/etc/NetworkManager/dispatcher.d/00-netreport
/etc/NetworkManager/dispatcher.d/11-dhclient
/etc/NetworkManager/dispatcher.d/10-sendmail
/etc/X11/xorg.conf.d/00-keyboard.conf
/etc/polkit-1/rules.d/50-default.rules
/etc/polkit-1/rules.d/49-polkit-pkla-compat.rules
/etc/polkit-1/localauthority/10-vendor.d
/etc/polkit-1/localauthority/20-org.d
/etc/polkit-1/localauthority/30-site.d
/etc/polkit-1/localauthority/50-local.d
/etc/polkit-1/localauthority/90-mandatory.d
/etc/cron.hourly/0anacron
/etc/cron.d/0hourly
/etc/security/limits.d/20-nproc.conf
/etc/kernel/postinst.d/51-dracut-rescue-postinst.sh

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

[root@centos7 data]# find /etc/ -name "[^[:alpha:]][[:alpha:]]*"
/etc/skel/.bash_logout
/etc/skel/.bash_profile
/etc/skel/.bashrc
/etc/cron.hourly/0anacron
/etc/cron.d/0hourly
/etc/selinux/targeted/.policy.sha512
/etc/.pwd.lock
/etc/.updated
/etc/1abc2.txt2

4.显示/etc/目录下所有以rc开头,并后面是0-6之间的数字,其它为任意字符的文件或目录列表

[root@centos7 data]# find /etc/ -name "rc[0-6]*"
/etc/rc.d/rc0.d
/etc/rc.d/rc1.d
/etc/rc.d/rc2.d
/etc/rc.d/rc3.d
/etc/rc.d/rc4.d
/etc/rc.d/rc5.d
/etc/rc.d/rc6.d
/etc/rc0.d
/etc/rc1.d
/etc/rc2.d
/etc/rc3.d
/etc/rc4.d
/etc/rc5.d
/etc/rc6.d

5.显示/etc目录下, 所有.conf结尾,且以m,n,rp开头的文件或目录列表

[root@centos7 data]# find /etc/ -name "[mnrp]*.conf"
/etc/resolv.conf
/etc/dbus-1/system.d/nm-dispatcher.conf
/etc/dbus-1/system.d/nm-ifcfg-rh.conf
/etc/prelink.conf.d/nss-softokn-prelink.conf
/etc/ld.so.conf.d/mariadb-x86_64.conf
/etc/nsswitch.conf
/etc/security/pwquality.conf
/etc/security/namespace.conf
/etc/security/pam_env.conf
/etc/plymouth/plymouthd.conf
/etc/rsyslog.conf
/etc/man_db.conf
/etc/mke2fs.conf

6.只显示/root下的隐藏文件和目录列表

#方法1
[root@centos7 ~]# l.
.  ..  .bash_history  .bash_logout  .bash_profile  .bashrc  .cshrc  .lesshst  .tcshrc

#方法2
[root@centos7 ~]# ls -d .*
.  ..  .bash_history  .bash_logout  .bash_profile  .bashrc  .cshrc  .lesshst  .tcshrc


#方法3
[root@centos7 ~]# find /root/ -name ".*"
/root/.bash_logout
/root/.bash_profile
/root/.bashrc
/root/.cshrc
/root/.tcshrc
/root/.bash_history
/root/.lesshst

7.只显示/etc下的非隐藏目录列表

[root@centos7 ~]# ls /etc/[^.]*/ -d
/etc/alternatives/       /etc/dhcp/           /etc/logrotate.d/     /etc/popt.d/          /etc/rpm/         /etc/systemd/
/etc/audisp/             /etc/dracut.conf.d/  /etc/mail/            /etc/postfix/         /etc/rsyslog.d/   /etc/terminfo/
/etc/audit/              /etc/firewalld/      /etc/modprobe.d/      /etc/ppp/             /etc/rwtab.d/     /etc/tmpfiles.d/
/etc/bash_completion.d/  /etc/gcrypt/         /etc/modules-load.d/  /etc/prelink.conf.d/  /etc/sasl2/       /etc/tuned/
/etc/binfmt.d/           /etc/gnupg/          /etc/my.cnf.d/        /etc/profile.d/       /etc/security/    /etc/udev/
/etc/chkconfig.d/        /etc/groff/          /etc/NetworkManager/  /etc/python/          /etc/selinux/     /etc/vmware-tools/
/etc/cron.d/             /etc/grub.d/         /etc/openldap/        /etc/rc0.d/           /etc/skel/        /etc/wpa_supplicant/
/etc/cron.daily/         /etc/gss/            /etc/opt/             /etc/rc1.d/           /etc/smrsh/       /etc/X11/
/etc/cron.hourly/        /etc/init.d/         /etc/pam.d/           /etc/rc2.d/           /etc/ssh/         /etc/xdg/
/etc/cron.monthly/       /etc/iproute2/       /etc/pkcs11/          /etc/rc3.d/           /etc/ssl/         /etc/xinetd.d/
/etc/cron.weekly/        /etc/kernel/         /etc/pki/             /etc/rc4.d/           /etc/statetab.d/  /etc/yum/
/etc/dbus-1/             /etc/krb5.conf.d/    /etc/plymouth/        /etc/rc5.d/           /etc/sudoers.d/   /etc/yum.repos.d/
/etc/default/            /etc/ld.so.conf.d/   /etc/pm/              /etc/rc6.d/           /etc/sysconfig/
/etc/depmod.d/           /etc/libnl/          /etc/polkit-1/        /etc/rc.d/            /etc/sysctl.d/
[root@centos7 ~]# ls -d /etc/*/
/etc/alternatives/       /etc/dhcp/           /etc/logrotate.d/     /etc/popt.d/          /etc/rpm/         /etc/systemd/
/etc/audisp/             /etc/dracut.conf.d/  /etc/mail/            /etc/postfix/         /etc/rsyslog.d/   /etc/terminfo/
/etc/audit/              /etc/firewalld/      /etc/modprobe.d/      /etc/ppp/             /etc/rwtab.d/     /etc/tmpfiles.d/
/etc/bash_completion.d/  /etc/gcrypt/         /etc/modules-load.d/  /etc/prelink.conf.d/  /etc/sasl2/       /etc/tuned/
/etc/binfmt.d/           /etc/gnupg/          /etc/my.cnf.d/        /etc/profile.d/       /etc/security/    /etc/udev/
/etc/chkconfig.d/        /etc/groff/          /etc/NetworkManager/  /etc/python/          /etc/selinux/     /etc/vmware-tools/
/etc/cron.d/             /etc/grub.d/         /etc/openldap/        /etc/rc0.d/           /etc/skel/        /etc/wpa_supplicant/
/etc/cron.daily/         /etc/gss/            /etc/opt/             /etc/rc1.d/           /etc/smrsh/       /etc/X11/
/etc/cron.hourly/        /etc/init.d/         /etc/pam.d/           /etc/rc2.d/           /etc/ssh/         /etc/xdg/
/etc/cron.monthly/       /etc/iproute2/       /etc/pkcs11/          /etc/rc3.d/           /etc/ssl/         /etc/xinetd.d/
/etc/cron.weekly/        /etc/kernel/         /etc/pki/             /etc/rc4.d/           /etc/statetab.d/  /etc/yum/
/etc/dbus-1/             /etc/krb5.conf.d/    /etc/plymouth/        /etc/rc5.d/           /etc/sudoers.d/   /etc/yum.repos.d/
/etc/default/            /etc/ld.so.conf.d/   /etc/pm/              /etc/rc6.d/           /etc/sysconfig/
/etc/depmod.d/           /etc/libnl/          /etc/polkit-1/        /etc/rc.d/            /etc/sysctl.d/

 五、

1.创建用户gentoo,附加组为bin和root, 默认shell为/bin/csh, 注释信息为"Gentoo Distribution"

[root@centos7 ~]# useradd -G bin,root -s /bin/csh -c "Gentoo Distribution" gentoo
[root@centos7 ~]# id -a gentoo 
uid=1001(gentoo) gid=1001(gentoo) groups=1001(gentoo),0(root),1(bin)
[root@centos7 ~]# cat /etc/passwd | grep gentoo
gentoo:x:1001:1001:Gentoo Distribution:/home/gentoo:/bin/csh

2.创建下面的用户、组和组成员关系
  名字为webs的组
  用户nginx,使用webs作为附加组
  用户varnish,使用webs作为附加组
  用户mysql,不可交互登录系统,且不是webs的成员,nginx, varnish, mysq密码都是123456

[root@centos7 ~]# groupadd webs
[root@centos7 ~]# useradd -G webs nginx 
[root@centos7 ~]# useradd -G webs varnish
[root@centos7 ~]# useradd -s /bin/false mysql
[root@centos7 ~]# echo -e "123456
123456" |passwd nginx
Changing password for user nginx.
New password: BAD PASSWORD: The password is shorter than 8 characters
Retype new password: passwd: all authentication tokens updated successfully.
[root@centos7 ~]# echo -e "123456
123456" |passwd varnish
Changing password for user varnish.
New password: BAD PASSWORD: The password is shorter than 8 characters
Retype new password: passwd: all authentication tokens updated successfully.
[root@centos7 ~]# echo -e "123456
123456" |passwd mysql
Changing password for user mysql.
New password: BAD PASSWORD: The password is shorter than 8 characters
Retype new password: passwd: all authentication tokens updated successfully.
[root@centos7 ~]# tail -n 4 /etc/passwd
gentoo:x:1001:1001:Gentoo Distribution:/home/gentoo:/bin/csh
nginx:x:1002:1003::/home/nginx:/bin/bash
varnish:x:1003:1004::/home/varnish:/bin/bash
mysql:x:1004:1005::/home/mysql:/bin/false

 六、

1.当用户docker对/testdir目录无执行权限时,意味着无法做哪些操作?

无法进入目录

无法删除目录内的文件

无法在目录内创建新文件
2.当用户mongodb对/testdir目录无读权限时,意味着无法做哪些操作?

无法查看目录内的文件列表
3.当用户redis对testdir目录无写权限时,该目录下的只读文件file1是否可修改和删除?

不可以删除文件,但是可以修改文件
4.当用户zabbix对/testdir目录有写和执行权限时,该目录下的只读文件file1是否可修改和删除?

可以修改和删除
5.复制/et/fstab文件到/var/tmp下,设置文件所有者为tomcat读写权限,所属组为apps组有读写
权限,其他人无权限

[root@centos7 ~]# useradd tomcat
[root@centos7 ~]# groupadd apps
[root@centos7 ~]# cp /etc/passwd /var/tmp/
[root@centos7 ~]# cd /var/tmp/
[root@centos7 tmp]# ll
total 4
-rw-r--r--. 1 root root 1271 Mar 25 14:12 passwd
[root@centos7 tmp]# chown tomcat.apps passwd 
[root@centos7 tmp]# ll
total 4
-rw-r--r--. 1 tomcat apps 1271 Mar 25 14:12 passwd
[root@centos7 tmp]# chmod 660 passwd 
[root@centos7 tmp]# ll
total 4
-rw-rw----. 1 tomcat apps 1271 Mar 25 14:12 passwd

6.误删除了用户git的家目录,请重建并恢复该用户家目录及相应的权限属性

[root@centos7 data]# useradd git
[root@centos7 data]# ll /home/
total 0
drwx------. 3 docker  docker   77 Mar 25 14:07 docker
drwx------. 2 gentoo  gentoo   62 Mar 24 17:23 gentoo
drwx------. 2 git     git      62 Mar 25 14:15 git
drwx------. 2 mysql   mysql    62 Mar 24 17:31 mysql
drwx------. 2 nginx   nginx    83 Mar 24 17:33 nginx
drwx------. 2 tomcat  tomcat   62 Mar 25 14:12 tomcat
drwx------. 2 varnish varnish  83 Mar 24 17:33 varnish
drwx------. 2 wang    wang    102 Mar 22 14:02 wang
[root@centos7 data]# man useradd
[root@centos7 data]# rm -rf /home/git/

[root@centos7 home]# mkdir git
[root@centos7 home]# cp -a /etc/skel/. /home/git/
[root@centos7 home]# chmod 700 git/
[root@centos7 home]# chown -R git:git git/

 七、

1.在/testdir/dir里创建的新文件自动属于webs组,组apps的成员如: tomcat能对这些新文件有读写
权限,组dbs的成员如: mysq|只能对新文件有读权限, 其它用户(不属于webs,apps,dbs) 不能
访问这个文件夹

[root@centos7 data]# mkdir testdir/dir -p
[root@centos7 data]# groupadd webs
[root@centos7 testdir]# groupadd dbs
[root@centos7 testdir]# chmod g+s dir/
[root@centos7 testdir]# ll
total 0
drwxr-sr-x. 2 root root 6 Mar 25 14:44 dir
[root@centos7 testdir]# chgrp webs dir/
[root@centos7 testdir]# ll
total 0
drwxr-sr-x. 2 root webs 6 Mar 25 14:44 dir
[root@centos7 testdir]# touch dir/1.txt
[root@centos7 testdir]# ll dir/
total 0
-rw-r--r--. 1 root webs 0 Mar 25 14:45 1.txt
[root@centos7 testdir]# ll
total 0
drwxr-sr-x. 2 root webs 19 Mar 25 14:45 dir
[root@centos7 testdir]# chmod o=- dir/
[root@centos7 testdir]# ll
total 0
drwxr-s---. 2 root webs 19 Mar 25 14:45 dir
[root@centos7 testdir]# usermod tomcat -G apps
[root@centos7 testdir]# id tomcat
uid=1006(tomcat) gid=1007(tomcat) groups=1007(tomcat),1008(apps)
[root@centos7 testdir]# setfacl -m  g:apps:rw dir/
[root@centos7 testdir]# setfacl -m g:dbs:r dir/
[root@centos7 testdir]# getfacl dir/
# file: dir/
# owner: root
# group: webs
# flags: -s-
user::rwx
group::r-x
group:apps:rw-
group:dbs:r--
mask::rwx
other::---

2.误将/bin/chmod文件的执行权限删除,如何恢复?

1.从别的机器拷贝了一个
#2.使用acl

[root@centos7 data]# ll /bin/chmod 
-rwxr-xr-x. 1 root root 58592 Aug 20  2019 /bin/chmod
[root@centos7 data]# chmod a-x /bin/chmod 
[root@centos7 data]# ll /bin/chmod 
-rw-r--r--. 1 root root 58592 Aug 20  2019 /bin/chmod
[root@centos7 data]# touch 1.txt
[root@centos7 data]# chmod 777 1.txt 
-bash: /usr/bin/chmod: Permission denied
[root@centos7 data]# setfacl -m u:root:rwx /bin/chmod
[root@centos7 data]# getfacl /bin/chmod
getfacl: Removing leading '/' from absolute path names
# file: bin/chmod
# owner: root
# group: root
user::rw-
user:root:rwx
group::r--
mask::rwx
other::r--

[root@centos7 data]# chmod 777 1.txt 
[root@centos7 data]# ll
total 0
-rwxrwxrwx. 1 root root 0 Mar 25 15:11 1.txt
[root@centos7 data]# chmod a+x /bin/chmod 
[root@centos7 data]# ll /bin/chmod
-rwxrwxr-x+ 1 root root 58592 Aug 20  2019 /bin/chmod
[root@centos7 data]# 
原文地址:https://www.cnblogs.com/tianakong/p/14573225.html