马哥Linux——第三周作业

时间:2021-12-27 03:31:45

1、编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到/root/etcYYYY-mm-dd中

[root@centos7 ~]#cat /root/bin/backup.sh 
#!/bin/bash
cp -a /etc/ /root/etc`date +%F`
[root@centos7 ~]#chmod +x bin/backup.sh

 

2、编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利用率最大的值

[root@centos7 ~]#cat /root/bin/disk.sh 
#!/bin/bash
df | grep "/dev/sd" | tr -s " " % | cut -d % -f 5 | sort -nr | sed -n '1p'
[root@centos7 ~]#chmod +x /root/bin/disk.sh
[root@centos7 ~]#disk.sh 
16

 

3、编写脚本/root/bin/nologin.sh和login.sh,实现禁止和允许普通用户登录系统

创建/etc/nologin文件就可以实现禁止普通用户。

[root@centos7 ~]#cat /root/bin/nologin.sh 
#!/bin/bash

FILE="/etc/nologin"
[ ! -e "$FILE" ] && touch $FILE && { echo "已禁止普通用户登录" ; exit ; } || { echo "默认已禁止普通用户登录" ; exit ; }
[root@centos7 ~]#chmod +x /root/bin/nologin.sh 
[root@centos7 ~]#nologin.sh 
已禁止普通用户登录

马哥Linux——第三周作业

[root@centos7 ~]#nologin.sh 
默认已禁止普通用户登录
[root@centos7 ~]#cat /root/bin/login.sh 
#!/bin/bash

FILE="/etc/nologin"
[ -e "$FILE" ] && rm -f $FILE && { echo "已允许普通用户登录" ; exit ; } || { echo "默认已允许普通用户登录" ; exit ; }
[root@centos7 ~]#chmod +x /root/bin/login.sh
[root@centos7 ~]#login.sh 
已允许普通用户登录

马哥Linux——第三周作业

[root@centos7 ~]#login.sh 
默认已允许普通用户登录

 

4、查找/var目录下不属于root、lp、gdm的所有文件

[root@centos7 ~]#find /var/ ! \( -user root -o -user lp -o -user gdm \)
/var/tmp/abrt
/var/lib/rpcbind
/var/lib/colord
/var/lib/colord/icc
/var/lib/colord/mapping.db
/var/lib/colord/storage.db
/var/lib/colord/.cache
/var/lib/chrony
/var/lib/tpm
/var/lib/libvirt/qemu
/var/lib/libvirt/qemu/save
/var/lib/libvirt/qemu/snapshot
/var/lib/libvirt/qemu/dump
/var/lib/libvirt/qemu/channel
/var/lib/libvirt/qemu/channel/target
/var/lib/libvirt/qemu/nvram
/var/lib/libvirt/qemu/ram
/var/lib/geoclue
/var/lib/nfs/statd
/var/lib/nfs/statd/sm
/var/lib/nfs/statd/sm.bak
/var/lib/nfs/state
/var/lib/setroubleshoot
/var/lib/setroubleshoot/setroubleshoot_database.xml
/var/lib/setroubleshoot/email_alert_recipients
/var/lib/pulse
/var/lib/postfix
/var/lib/postfix/master.lock
/var/log/chrony
/var/cache/abrt-di
/var/cache/libvirt/qemu
/var/spool/mail/rpc
/var/spool/mail/kkk
/var/spool/mail/test
/var/spool/abrt-upload
/var/spool/postfix/active
/var/spool/postfix/bounce
/var/spool/postfix/corrupt
/var/spool/postfix/defer
/var/spool/postfix/deferred
/var/spool/postfix/flush
/var/spool/postfix/hold
/var/spool/postfix/incoming
/var/spool/postfix/maildrop
/var/spool/postfix/private
/var/spool/postfix/private/tlsmgr
/var/spool/postfix/private/rewrite
/var/spool/postfix/private/bounce
/var/spool/postfix/private/defer
/var/spool/postfix/private/trace
/var/spool/postfix/private/verify
/var/spool/postfix/private/proxymap
/var/spool/postfix/private/proxywrite
/var/spool/postfix/private/smtp
/var/spool/postfix/private/relay
/var/spool/postfix/private/error
/var/spool/postfix/private/retry
/var/spool/postfix/private/discard
/var/spool/postfix/private/local
/var/spool/postfix/private/virtual
/var/spool/postfix/private/lmtp
/var/spool/postfix/private/anvil
/var/spool/postfix/private/scache
/var/spool/postfix/public
/var/spool/postfix/public/pickup
/var/spool/postfix/public/cleanup
/var/spool/postfix/public/qmgr
/var/spool/postfix/public/flush
/var/spool/postfix/public/showq
/var/spool/postfix/saved
/var/spool/postfix/trace
/var/spool/at
/var/spool/at/spool
/var/spool/at/.SEQ

 

5、统计/etc/init.d/functions文件中每个单词的出现次数,并排序(用grep和sed两种方法分别实现)

[root@centos7 ~]#grep -Eo "[[:alpha:]]+" /etc/init.d/functions | sort | uniq -c | sort -nr
[root@centos7 ~]#sed -nre 's/[^[:alpha:]]+/\n/gp' /etc/init.d/functions | sort -nr | tr -s "\n" | uniq -c | sort -nr
[root@centos7 ~]#cat /etc/init.d/functions | tr -c "[:alpha:]" "\n" | tr -s "\n" | sort -nr | tr -s "[:space:]" | uniq -c | sort -nr

 

6、利用sed取出ifconfig命令中本机的IPv4地址

[root@centos7 ~]#ifconfig | sed -nr '/inet[[:space:]]+/p' | sed -r 's/.*inet[[:space:]]+(.*)[[:space:]]+netmask.*/\1/'
192.168.30.104 
192.168.1.109 
127.0.0.1 
192.168.122.1

 

7、总结yum的配置和使用,包括yum仓库的创建

检查httpd是否安装

[root@centos7 ~]#rpm -q httpd
package httpd is not installed

安装httpd服务

[root@centos7 ~]#yum install httpd -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-67.el7.centos will be installed
--> Processing Dependency: httpd-tools = 2.4.6-67.el7.centos for package: httpd-2.4.6-67.el7.centos.x86_64
--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-67.el7.centos.x86_64
--> Running transaction check
---> Package httpd-tools.x86_64 0:2.4.6-67.el7.centos will be installed
---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

============================================================================
 Package           Arch         Version                    Repository  Size
============================================================================
Installing:
 httpd             x86_64       2.4.6-67.el7.centos        base       2.7 M
Installing for dependencies:
 httpd-tools       x86_64       2.4.6-67.el7.centos        base        87 k
 mailcap           noarch       2.1.41-2.el7               base        31 k

Transaction Summary
============================================================================
Install  1 Package (+2 Dependent packages)

Total download size: 2.8 M
Installed size: 9.6 M
Downloading packages:
----------------------------------------------------------------------------
Total                                          5.3 MB/s | 2.8 MB  00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : httpd-tools-2.4.6-67.el7.centos.x86_64                   1/3 
  Installing : mailcap-2.1.41-2.el7.noarch                              2/3 
  Installing : httpd-2.4.6-67.el7.centos.x86_64                         3/3 
  Verifying  : httpd-2.4.6-67.el7.centos.x86_64                         1/3 
  Verifying  : mailcap-2.1.41-2.el7.noarch                              2/3 
  Verifying  : httpd-tools-2.4.6-67.el7.centos.x86_64                   3/3 

Installed:
  httpd.x86_64 0:2.4.6-67.el7.centos                                        

Dependency Installed:
  httpd-tools.x86_64 0:2.4.6-67.el7.centos   mailcap.noarch 0:2.1.41-2.el7  

Complete!

启动httpd,并设置开机自启动

[root@centos7 ~]#systemctl start httpd
[root@centos7 ~]#systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

查看httpd状态

[root@centos7 ~]#systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2019-07-23 02:28:22 CST; 58s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 9052 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─9052 /usr/sbin/httpd -DFOREGROUND
           ├─9053 /usr/sbin/httpd -DFOREGROUND
           ├─9054 /usr/sbin/httpd -DFOREGROUND
           ├─9055 /usr/sbin/httpd -DFOREGROUND
           ├─9056 /usr/sbin/httpd -DFOREGROUND
           └─9057 /usr/sbin/httpd -DFOREGROUND

Jul 23 02:28:21 centos7.localdomain systemd[1]: Starting The Apache HTTP ...
Jul 23 02:28:21 centos7.localdomain httpd[9052]: AH00558: httpd: Could no...
Jul 23 02:28:22 centos7.localdomain systemd[1]: Started The Apache HTTP S...
Hint: Some lines were ellipsized, use -l to show in full.

在httpd路径下创建yum仓库目录

[root@centos7 ~]#mkdir -p /var/www/html/centos/{6,7}/os/x86_64/
[root@centos7 ~]#tree /var/www/html/
/var/www/html/
└── centos
    ├── 6
    │   └── os
    │       └── x86_64
    └── 7
        └── os
            └── x86_64

7 directories, 0 files

把镜像挂载到仓库目录,并检查挂载情况

[root@centos7 ~]#mount /dev/sr0 /var/www/html/centos/7/os/x86_64/
mount: /dev/sr0 is write-protected, mounting read-only
[root@centos7 ~]#df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda2       52403200 3995032  48408168   8% /
devtmpfs          535296       0    535296   0% /dev
tmpfs             550036       0    550036   0% /dev/shm
tmpfs             550036    7960    542076   2% /run
tmpfs             550036       0    550036   0% /sys/fs/cgroup
/dev/sda3       31441920   35756  31406164   1% /data
/dev/sda1        1038336  161620    876716  16% /boot
tmpfs             110008      20    109988   1% /run/user/0
/dev/sr0         8490330 8490330         0 100% /misc/cd
tmpfs             110008       0    110008   0% /run/user/1000

打开浏览器访问http://X.X.X.X/centos/7/os/x86_64

马哥Linux——第三周作业

编写repo文件

[root@centos7 ~]#cat /etc/yum.repos.d/base.repo
[base]
name=base
baseurl=http://192.168.30.104/centos/7/os/x86_64/
gpgcheck=0

清除旧缓存并创建新缓存

[root@centos7 ~]#yum clean all
Loaded plugins: fastestmirror, langpacks
Cleaning repos: base
Cleaning up everything
Maybe you want: rm -rf /var/cache/yum, to also free up space taken by orphaned data from disabled or removed repos
Cleaning up list of fastest mirrors
[root@centos7 ~]#yum makecache 
Loaded plugins: fastestmirror, langpacks
base                                                 | 3.6 kB     00:00     
(1/4): base/group_gz                                   | 156 kB   00:00     
(2/4): base/primary_db                                 | 5.7 MB   00:00     
(3/4): base/filelists_db                               | 6.7 MB   00:00     
(4/4): base/other_db                                   | 2.5 MB   00:00     
Determining fastest mirrors
Metadata Cache Created

查看所有仓库

[root@centos7 ~]#yum repolist 
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
repo id                             repo name                         status
base                                base                              9,591
repolist: 9,591

安装软件测试

[root@centos7 ~]#yum install tree -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package tree.x86_64 0:1.6.0-10.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

============================================================================
 Package       Arch            Version                  Repository     Size
============================================================================
Installing:
 tree          x86_64          1.6.0-10.el7             base           46 k

Transaction Summary
============================================================================
Install  1 Package

Total download size: 46 k
Installed size: 87 k
Downloading packages:
tree-1.6.0-10.el7.x86_64.rpm                           |  46 kB   00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : tree-1.6.0-10.el7.x86_64                                 1/1 
  Verifying  : tree-1.6.0-10.el7.x86_64                                 1/1 

Installed:
  tree.x86_64 0:1.6.0-10.el7                                                

Complete!

创建yum仓库

createrepo

[root@centos7 ~]#cp /misc/cd/Packages/httpd-2.4.6-67.el7.centos.x86_64.rpm /data/
[root@centos7 ~]#createrepo /data/
Spawning worker 0 with 1 pkgs
Spawning worker 1 with 0 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete

 

8、编写系统初始化脚本reset.sh,包括别名,提示符颜色,yum仓库配置问紧啊,安装tree、ftp、lftp、telnet等包

[root@centos7 ~]#cat /root/bin/reset.sh 
#!/bin/bash

echo "Init system"
sleep 1
VERSION=`egrep -o "[0-9]" /etc/redhat-release | head -n 1`
echo "Current system version is Centos $VERSION"

echo "Start setting alias"
sleep 1
cat >> /root/.bashrc << END
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias cdnet='cd /etc/sysconfig/network-scripts/'
alias ipconfig='ifconfig | egrep -o "\<([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}\>" | head -n 1'
END
echo "Alias be ready"

echo "Start setting SP1"
sleep 1
[ "$VERSION" -eq 7 ] && echo 'PS1="\[\e[1;36m\][\u@\h \W]\\$\[\e[0m\]"' >> /etc/profile.d/env.sh && source /etc/profile.d/env.sh && echo "SP1 be ready"
# [ "$VERSION" -eq 6 ] && echo 'PS1="\[\e[1;32m\][\u@\h \W]\\$\[\e[0m\]"' >> /etc/profile.d/env.sh && source /etc/profile.d/env.sh && echo "SP1 be ready"

echo "Start setting yum repo"
sleep 1
ls /meida/cdrom/ &> /dev/null || { mkdir /media/cdrom &> /dev/null ; mount /dev/sr0 /media/cdrom &> /dev/null }
mkdir /etc/yum.repos.d/bak
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak/
cat >> /etc/yum.repos.d/base.repo << END
[base]
name=CentOS "$VERSION"
baseurl=file:///media/cdrom
gpgcheck=0

[epel]
name=epel
baseurl=https://mirrors.aliyun.com/epel/7/x86_64/
gpgcheck=0
END
echo "Yum repo be ready"

echo "Start intalling rpms"
sleep 1
yum clean all && yum makecache &> /dev/null
yum install tree ftp lftp telnet -y &> /dev/null
echo "Tree, ftp, lftp, telnet already install"

echo "Start stopping firewall"
sleep 1
[ "$VERSION" -eq 7 ] && `systemctl stop firewalld` && `systemctl disable firewalld` &> dev/null && echo "Firewall already stop"
# [ "$VERSION" -eq 6 ] && `service iptables stop` && `chkconfig iptables off` && echo "Firewall already stop"

echo "Init system over"

 

9、在CentOS7上编译安装apache2.4源码包,并启动此服务

安装Development Tools

[root@centos7 ~]#yum groupinstall "Development Tools" -y

安装apr-devel

[root@centos7 ~]#yum install apr-devel -y

安装apr-util-devel

[root@centos7 ~]#yum install apr-util-devel -y

安装pcre-devel

[root@centos7 ~]#yum install pcre-devel -y

创建/app目录

[root@centos7 ~]#mkdir /app

解压

[root@centos7 ~]#tar xvf httpd-2.4.39.tar.bz2

进入httpd目录并执行configure编译

[root@centos7 ~]#cd httpd-2.4.39/
[root@centos7 httpd-2.4.39]#./configure --prefix=/app

安装

[root@centos7 httpd-2.4.39]#make -j 8 && make install

环境变量加上httpd路径

[root@centos7 ~]#echo "PATH=/app/bin:\$PATH" >> /etc/profile.d/env.sh 
[root@centos7 ~]#. /etc/profile.d/env.sh

启动httpd

[root@centos7 ~]#apachectl start

马哥Linux——第三周作业