1、yum源的配置與使用(5分)
1). 創建一個本地yum源
base源指向https://mirrors.aliyun.com/centos/7/os/x86_64/
epel源指向https://mirrors.aliyun.com/epel/7Server/x86_64/
2). 安裝開發包組
①
[base]
name=base
baseurl=https://mirrors.aliyun.com/centos/7/os/x86_64/
enbale=1
gpgcheck=0
[epel]
name=epel
baseurl=https://mirrors.aliyun.com/epel/7Server/x86_64/
enable=1
gpgcheck=0
②.yum groupinstall "Development Tools"
2、復制/etc/ssh/sshd_config 到/tmp/中并更名為sshd_config.bak。將/tmp/sshd_config.bak文件中所有以非#號開頭與包含空白字符的行保存至/tmp/sshd_config中。(5分)
cp /etc/sshd_config /tmp/sshd_config.bak
cat /tmp/sshd_config.bak |grep -Ev '^#|^[[:space:]]*$'>/tmp/sshd_config
3、編寫腳本/root/bin/sysinfo.sh顯示當前主機系統信息,包括主機名,操作系統版本,內核版本,CPU型號,內存大小,硬盤分區(5分)
Vim /root/bin/sysinfo.sh
#!/bin/bash
hostname
cat /etc/redhat-release
uname -r
free -m
fdisk -l
Chmod +x /root/bin/sysinfo.sh
4、給root用戶定義別名命令vimnet,相當于vim /etc/sysconfig/network-scripts/ifcfg-ens33,并使root執行history命令時,顯示每個命令執行的具體時間。(5分)
echo "alias vimnet="vim /etc/sysconfig//etc/sysconfig/network-scripts/ifcfg-ens33"" >> ~/.bashrc
echo 'HISTTIMEFORMAT="%F %T"' >> ~/.bash_profile
. ~/.bash_profile
. ~/.bashrc
5、指出軟鏈接與硬鏈接的異同之處(至少四處)(5分)
硬鏈接
1、硬鏈接必須在同一個分區中創建,不能跨分區,跨分區只能用軟鏈接
2、硬鏈接不支持對目錄創建,硬鏈接不支持跨設備跨分區
3、不復制原數據,僅分配一個inode號和文件名
4、硬鏈接文件使用同一個inode號
5、硬鏈接工作的相對路徑相對于當前目錄
6、鏈接文件沒有依賴性關系,鏈接數會加1
軟鏈接
1、可以對目錄進行
2、可以跨分區
3、指向的是另一個文件的路徑;其大小為指向的路徑字符串的長度;不增加或減少目標文件inode的引用計數;
4、刪除鏈接原文件,則鏈接指向失效
5、軟鏈接用相對路徑時相對的不是當前工作目錄,而是相對軟鏈接工作路徑
6、下載編譯安裝httpd 2.4最新版本,寫出安裝過程(5分)
1. httpd源碼下載
httpd2.4.25下載地址http://hc.apache.org/downloads.cgi
2.將下載的httpd源碼包復制到系統路徑下,再此存放到了路徑/app下,然后tar解壓源碼包到當前目錄下
cd /app
tar xvf httpd-2.4.25.tar.bz2
3. 安裝開發包組
yum groupinstall "Development Tools"
4. 查看幫助文件,了解如何安裝
cat README
cat INSTALL
5. cd /root/httpd-2.4.25 切換到軟件包目錄下
./configure --prefix=/app/apache --sysconfdir=/tmp/etc/ --bindir=/tmp/etc --enable-rewrite
指定所有文件默認安裝在/app/apache目錄下,sysconfdir=/etc/apache/為/etc/配置文件存在目錄,bin二進制文件放在/tmp/bin目錄下,--enable-rewrite為啟用重定向特性
在安裝過程觀察有無軟件包安裝錯誤,若有,則對應安裝對應的開發軟件包
yum install 包名-devel
6. make 根據makefile 文件,構建應用程序
7. make install 復制文件到相應路徑
8. 安成功檢測
/etc/ /bin目錄存放在/tmp下,其他配置文件在/app/apache目錄下
9. 防火墻
centos6
service iptables stop 馬上禁用
chkconfig iptables off 下次開機禁用
centos7
systemctl stop firewalld.service 馬上禁用
systemctl disable firewalld.service 下次開機禁用
10. 訪問測試
netstat -ntl 查看服務對應端口是否打開
iptables -vnL 查看防火墻
/app/apache/bin/apachectl start 開啟httpd服務,apachectl的路徑為/app/apache/bin/apachectl
11. 將apachectl命令導入PATH環境變量
PATH=PATH:/app/apache/bin/apachectl
10. apachectl start 開啟服務
7、過濾ifconfig命令結果中所有大于0且小于255的三位數(5分)
ifconfig | egrep -o "\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>"
ifconfig | egrep -o "\<25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]"
8、將用戶mage被誤刪除的的家目錄恢復,復制/etc/shadow到mage家目錄中。并設置只有用戶wang可以讀取/home/mage/shadow(5分)
cp -a /etc/skel/ /home/mage
cd /home/mage
chown mage:mage mage/
chmod 700 mage/
cp /etc/shadow /home/mage
setfacl -m u:mage:r-- /home/mage/shadow
9、統計/var/log/httpd/access.log日志訪問頻繁前十的地址,并從大到小排序(5分)
cut -d" " -f1 /var/log/httpd/access.log |sort|uniq -c| sort -nr|head -n10|tr -s " "|cut -d" " -f3
10、開啟兩個終端,將終端1 中輸入命令的執行結果輸出,并同時輸出到終端2 (5分)
echo "wangnan sb" > /dev/pts/1
11、誤刪除/lib64/libc.so.6系統庫文件,如何恢復之,實驗說明(5分)
先進入系統修復模式下
Cp /lib64/libc.so.6 /mnt/sysimage/lib64 /mnt/sysimage為真實系統根目錄
12、誤刪除rpm包命令,如何恢復之,實驗說明 (5分)
先進入修復模式下
mkdir /mnt/cdrom
mount /dev/cdrom /mnt/cdrom
rpm -ivh /mnt/cdrom/Packages/rpm-4.11.3-21.e17.x86_64.rpm --root=/mnt/sysimage 以root的身份將rpm包安裝在系統真實目錄/mnt/sysimage下
chmod /mnt/sysimage 切換進入真實系統下
mount /dev/cdrom /mnt
rpm -ivh /mnt/Packages/httpd* 測試rpm是否修復成功
13、計算2+4+6+…+96+98+100之和(5分)
echo {2..100..2} | tr -s ' ' '+' | bc
14、取/etc/sysconfig/network-scripts/ifcfg-ens33基名,用兩種方法實現(5分)
echo /etc/sysconfig/network-scripts/ifcfg-ens33 |egrep -o '[^/]+?$'
basename /etc/sysconfig/network-scripts/ifcfg-ens33
15、對/etc/目錄,分別執行命令,實現以下功能(5分)
(1)按從大到小順序顯示文件列表
ll /etc/ | tr -s " " | cut -d' ' -f 5,9 | sort -n -r | cut -d ' ' -f 2
(2)只顯示隱藏文件
ls -aI "[^.]*" /etc
(3)只顯示目錄
ll -a /etc/ | egrep ^d
(4)按mtime時間顯示文件列表
ls -a -t /tmp/
(5)按atime時間顯示文件列表
ls -a -ut /tmp/
16、編寫/root/bin/excute.sh,實現與用戶交互,判斷用戶給予的參數是否可讀,可寫,可執行(5分)
#!/bin/bash
read -p "please input fimename " name || exit
[ -r $name ] && echo "file $name have read"
[ -w $name ] && echo "file $name have write"
[ -x $name ] && echo "file $name have excute"
17、編寫/root/bin/create.sh可以生成新的腳本包括作者、聯系方式、版本、時間和描述等,并且可以直接對其進行編輯,編輯完后自動加上執行權限(5分)
#!/bin/bash
[ $# -gt 1 ] &&{ echo "the args is error";exit;}
[ $# == 0 ] && read -s -p "please input script name: " name || name="$1"
echo "#!/bin/bash
# filename "$name"
# author:danran
# telephone:1709369XXXX
# versions:1.5
# time is `date +%F`
# describe:This is a script to create a template"
>"$name"
chmod +x "$name"
vim "$name"
18、寫一個腳本,讓它可以傳遞兩個參數后,實現對該參數的加、減、乘、除運算并輸出運算后的值(5分)
#!/bin/bash
[ $# != 2 ] && { echo "usage:file.sh num1 num2" ; exit ; }
echo "$1+$2=$[ $1+$2 ]"
echo "$1*$2=$[ $1*$2 ]"
echo "$1-$2=$[ $1-$2 ]"
[ "$2" -eq 0 ] && echo "The divisor is 0 " ||echo "$1/$2=$[ $1/$2 ]"
19、編寫/root/bin/wcfile.sh統計/etc目錄中的目錄的個數,文件的個數,并求出/etc/目錄中的目錄和文件個數的總和(5分)
#!/bin/bash
dir=`ls -al /etc/ | egrep \(^d\|^.d\) | wc -l`
file=`ls -al /etc/ | egrep \(^-\|^.-\) | wc -l`
let sum=$dir+$file
echo "the dir is $dir;the file is $file;the file and dir sum is $sum"
20、/編寫/root/bin/baketc.sh 查找/etc/目錄中超過1天未修改的文件,將其壓縮備份至/bakup目錄。若之前沒有備份過則備份之,若存在的備份文件超過了2分鐘則備份之,否則退出。備份的格式為YYYY-MM-DD-hh-mm-ss.xz(Y表示年,M表示月,D表示日,h表示時,m表示分,s表示秒)(5分)
#!/bin/bash
ls *.xz > /dev/null || { find /etc -mtime +0 > /app/tar.txt && tar Jcf /backup/
`date "+%F-%H-%M-%S"`.xz -T /app/tar.txt ;exit; }
[ -z `find /backup/*.xz -mmin -2` ] && find /etc -mtime +0 > /app/tar.txt &&
tar Jcf /app/backup/`date "+%F-%H-%M-%S"`.xz -T /app/tar.txt || exit
參考答案
#!/bin/bash
DATE=`date +"%Y-%m-%d-%H-%M-%S"`
WCFILE=`ls /backup | wc -l`
[ -d /backup ] || mkdir /backup
[ $WCFILE -eq 0 ] && find /etc/ -mtime +1 | xargs tar -Jcvf /backup/$DATE\.tar.xz
FILE=$[`ls -lt /backup/* | cut -d" " -f10 | head -1 |tr -dc [:digit:]`+200]
echo "new file old time add two minute was: $FILE"
NOW=$[`date +"%Y%m%d%H%M%S"`]
echo "NOW system time is: $NOW"
[ $FILE -lt $NOW ] && find /etc/ -mtime +1 | xargs tar -Jcvf /backup/$DATE\.tar.xz
有趣的郵件
[test]
name=test
baseurl=https://mirrors.aliyun.com/centos/7/os/x86_64/
gpgcheck=0
enabled=1
[test2]
name=test2
baseurl=https://mirrors.aliyun.com/epel/7Server/x86_64/
gpgcheck=0
enabled=1
yum -y install msmtp mutt
cat .muttrc
set sendmail="/usr/bin/msmtp"
set use_from=yes