chapter02 - 03 做業linux
一、 分別用cat \tac\nl三個命令查看文件/etc/ssh/sshd_config文件中的內容,並用本身的話總計出這三個文檔操做命令的不一樣之處?c++
[root@localhost ~]# cat -n /etc/ssh/sshd_configweb
1 # $OpenBSD: sshd_config,v 1.93 2014/01/10 05:59:19 djm Exp $apache
2 vim
3 # This is the sshd server system-wide configuration file. See緩存
。bash
。less
。ssh
151 # AllowTcpForwarding noide
152 # PermitTTY no
153 # ForceCommand cvs server
[root@localhost ~]# tac /etc/ssh/sshd_config
# ForceCommand cvs server
# PermitTTY no
# AllowTcpForwarding no
。
。
。
# This is the sshd server system-wide configuration file. See
# $OpenBSD: sshd_config,v 1.93 2014/01/10 05:59:19 djm Exp $
[root@localhost ~]# nl /etc/ssh/sshd_config
1 # $OpenBSD: sshd_config,v 1.93 2014/01/10 05:59:19 djm Exp $
2 # This is the sshd server system-wide configuration file. See
3 # sshd_config(5) for more information.
。
。
。
125 # AllowTcpForwarding no
126 # PermitTTY no
127 # ForceCommand cvs server
不一樣之處:
cat和tac標識空白行,nl不標示空白行
cat以正序的方式顯示文件內容,tac以倒敘的方式顯示文件的內容
二、 分別用more和less查看/etc/ssh/sshd_config裏面的內容,請用總結more和less兩個命令的相同和不一樣之處?
[root@localhost ~]# more /etc/ssh/sshd_config
# $OpenBSD: sshd_config,v 1.93 2014/01/10 05:59:19 djm Exp $ ......
[root@localhost ~]# less /etc/ssh/sshd_config
# $OpenBSD: sshd_config,v 1.93 2014/01/10 05:59:19 djm Exp $ ......
不一樣之處:
「more」按Enter鍵和空格鍵上下滾動,按q鍵退出
「less」 功能與「more」大體相同,less按pgUp、pgDn鍵上下翻頁,按「/」查找內容
三、將/etc/passwd文件中的前20行重定向保存到/root下更名爲20_pass.txt,將/etc/passwd文件中的後15行重定向保存到/root下更名爲:pass_15.txt
[root@localhost ~]# head -20 /etc/passwd >/root/20_pass.txt
[root@localhost ~]# ls
20_pass.txt test.txt 公共 視頻 文檔 音樂
anaconda-ks.cfg test.txt~ 模板 圖片 下載 桌面
[root@localhost ~]# tail -15 /etc/passwd >/root/pass_15.txt
[root@localhost ~]# ls
20_pass.txt pass_15.txt test.txt~ 模板 圖片 下載 桌面
anaconda-ks.cfg test.txt 公共 視頻 文檔 音樂
三、 請用一個命令統計/etc/hosts文件包含有多少行?多少字節?多少單詞數?
[root@localhost ~]# wc /etc/hosts
2 10 158 /etc/hosts
五、練習使用grep和egrep
5.1.經過grep管道工具過濾出ifconfig命令顯示信息中的IP字段?
[root@localhost ~]# ifconfig | grep "inet"
inet 192.168.100.147 netmask 255.255.255.0 broadcast 192.168.100.255
inet6 fe80::20c:29ff:fe69:4a49 prefixlen 64 scopeid 0x20<link>
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
(先利用ifconfig命令將IP調出來,再經過gerp命令濾除帶有「inet」的字段。)
[root@localhost ~]# ifconfig | grep "inet " | grep "t .* n"
inet 192.168.100.147 netmask 255.255.255.0 broadcast 192.168.100.255
inet 127.0.0.1 netmask 255.0.0.0
(繼續過濾,濾掉帶有「inet6」的段落)
[root@localhost ~]# ifconfig | grep "inet " | grep -o "t .* n"
t 192.168.100.147 n
t 127.0.0.1 n
(經過「-o」參數扣出帶有IP的字段)
[root@localhost ~]# ifconfig | grep "inet " | grep -o "t .* n" | grep -o " .* "
192.168.100.147
127.0.0.1
(再次過濾把數字摳出來)
[root@localhost ~]# ifconfig | grep "inet " | grep -o "t.*n"| " | head -1
192.168.100.147
(利用「head」命令摘出第一段,IP字段查看完畢)
5.2.將/etc/passwd文件中的前20行重定向保存到/root下名稱爲pass?
[root@localhost ~]# head -20 /etc/passwd >/root/pass
[root@localhost ~]# ls
20_pass.txt pass test.txt 公共 視頻 文檔 音樂
anaconda-ks.cfg pass_15.txt test.txt~ 模板 圖片 下載 桌面
5.3.過濾/etc/passwd文件中含有/sbin/nologin 的行並統計行數?
[root@localhost ~]# grep "/sbin/nologin" /etc/passwd | wc -l
35
5.4 過濾/etc/passwd文件中以sh結尾的行,及以 root開頭的行,不顯示包含login的行?
[root@localhost ~]# grep "sh$" /etc/passwd
root:x:0:0:root:/root:/bin/bash
tom:x:1000:1000:tom:/home/tom:/bin/bash
[root@localhost ~]# grep "^root" /etc/passwd | grep -v "login"
root:x:0:0:root:/root:/bin/bash
5.5 分別用grep和egrep過濾出/etc/ssh/sshd_config文件中不包含「#」開頭和空白的行?
[root@localhost ~]# grep -v "^#" /etc/ssh/sshd_config | grep -v "^$"
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
SyslogFacility AUTHPRIV
AuthorizedKeysFile .ssh/authorized_keys
PasswordAuthentication yes
ChallengeResponseAuthentication no
GSSAPIAuthentication yes
GSSAPICleanupCredentials no
UsePAM yes
X11Forwarding yes
UsePrivilegeSeparation sandbox # Default for new installations.
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS
Subsystem sftp /usr/libexec/openssh/sftp-server
[root@localhost ~]# egrep -v "^# | ^$" /etc/ssh/sshd_config
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
SyslogFacility AUTHPRIV
AuthorizedKeysFile .ssh/authorized_keys
PasswordAuthentication yes
ChallengeResponseAuthentication no
GSSAPIAuthentication yes
GSSAPICleanupCredentials no
UsePAM yes
X11Forwarding yes
UsePrivilegeSeparation sandbox # Default for new installations.
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS
Subsystem sftp /usr/libexec/openssh/sftp-server
6.1 經過tar命令將/etc/passwd文件打包壓縮成/root/file.tar.gz
[root@localhost ~]# cd /etc
(進入etc文件夾)
[root@localhost etc]# tar czf /root/file.tar.gz passwd
(壓縮文件並命名)
[root@localhost etc]# ls /root
20_pass.txt file.tar.gz pass_15.txt test.txt~ 模板 圖片 下載 桌面
anaconda-ks.cfg pass test.txt 公共 視頻 文檔 音樂
(查看)
6.2經過tar命令將/etc/passwd文件打包壓縮成/root/file.tar.bz2
[root@localhost ~]# cd /etc
[root@localhost etc]# tar cjf /root/file.tar.bz2 passwd
[root@localhost etc]# ls /root
20_pass.txt file.tar.bz2 pass test.txt 公共 視頻 文檔 音樂
anaconda-ks.cfg file.tar.gz pass_15.txt test.txt~ 模板 圖片 下載 桌面
(作法與上題同樣)
6.3建立空文件夾/web/test1,並將file.tar.bz2 解包並釋放到/web/test1目錄下?
[root@localhost ~]# mkdir -p /web/test1/
(建立空文件夾/web/test1)
[root@localhost ~]# tar -xf file.tar.bz2 -C /web/test1
(將file.tar.bz2 解包並釋放到/web/test1目錄下)
[root@localhost ~]# ls /web/test1
passwd
(查看)
7.1 經過vi編輯/web/test1/passwd文件將文件裏爲root單詞所有替換成benet。
[root@localhost ~]# vi /web/test1/passwd
(經過vi編輯/web/test1/passwd)
:% s/root/benet/g
(將文件裏爲root單詞所有替換成benet)
7.2 經過vi編輯 刪除pass文件第一、五、10行。
[root@localhost ~]# vi /web/test1/passwd
:set nu
:1G D
:5G D
:10G D
7.3 在vi中顯示pass文件行號複製文件2 3 4行粘貼到以lp開頭的行下。
[root@localhost ~]# vi pass
: set nu
將光標移動至第二行,3yy
將光標移動到ip行,p
7.4 經過vi編輯 查找文件內包含mail var等字符串,並記錄所在行號。
mail(12)
var(4,5,8,12,15,21,23)
7.5 經過vi編輯 快速跳轉到文件的第二行,經過r 讀取 /etc/hosts 文件的內容到第二行下。
:r /etc/hosts
7.6將更改後的文件使用vim另存爲/root/new_pass。
:w /root/new_pass
[root@localhost ~]# ls /root/
20_pass.txt file.tar.bz2 new_pass pass_15.txt test.txt~ 模板 圖片 下載 桌面
anaconda-ks.cfg file.tar.gz pass test.txt 公共 視頻 文檔 音樂
7.7將new_pass文件壓縮成gz格式並更名爲npass.gz文件。
[root@localhost ~]# tar -czf npass.gz new_pass
[root@localhost ~]# ls /root 0
20_pass.txt file.tar.bz2 new_pass pass test.txt 公共 視頻 文檔 音樂
anaconda-ks.cfg file.tar.gz npass.gz pass_15.txt test.txt~ 模板 圖片 下載 桌面
8統計/dev 目錄下的文件數量。
包含文件夾
[root@localhost dev]# ls -l | grep -v "總用量" | wc -l
155
[root@localhost dev]# ls | wc -l
155
不包含文件夾
[root@localhost dev]# ls -l /dev | grep -v "drwxr" | grep -v "總用量" | wc -l
138
9.1在/boot下查找文件名以vmlinuz開頭的文件?
[root@localhost dev]# ls /boot
config-3.10.0-229.el7.x86_64 initrd-plymouth.img
grub symvers-3.10.0-229.el7.x86_64.gz
grub2 System.map-3.10.0-229.el7.x86_64
initramfs-0-rescue-7f8ba12e352e4670a26f091cb7a2014d.img vmlinuz-0-rescue-7f8ba12e352e4670a26f091cb7a2014d
initramfs-3.10.0-229.el7.x86_64.img vmlinuz-3.10.0-229.el7.x86_64
(查詢boot文件夾下的文件)
[root@localhost dev]# ls /boot | grep "vmlinuz"
vmlinuz-0-rescue-7f8ba12e352e4670a26f091cb7a2014d
vmlinuz-3.10.0-229.el7.x86_64
(過濾出含有vmlinuz的字段)
[root@localhost dev]# ls /boot | grep "vmlinuz" | wc -l
2
(統計行數)
9.2在/boot下查找文件大小大於3M 小於 20M 的文件
[root@localhost dev]# find /boot -size +3M -a -size -20M
/boot/vmlinuz-3.10.0-229.el7.x86_64
/boot/vmlinuz-0-rescue-7f8ba12e352e4670a26f091cb7a2014d
/boot/initramfs-3.10.0-229.el7.x86_64.img
10 請詳細寫出構建本地yum倉庫的步驟?並在每行命令後面用本身的話作上中文註釋?
[root@localhost ~]# umount /dev/sr0(卸載光盤)
[root@localhost ~]# mount /dev/sr0 /media/(掛載光盤)
mount: /dev/sr0 寫保護,將以只讀方式掛載
[root@localhost ~]# ls /media/(查看)
CentOS_BuildTag EULA images LiveOS repodata RPM-GPG-KEY-CentOS-Testing-7
EFI GPL isolinux Packages RPM-GPG-KEY-CentOS-7 TRANS.TBL
[root@localhost ~]# cd /etc/yum.r*(構建本地YUM倉庫文檔)
[root@localhost yum.repos.d]# mkdir a/(構建本地YUM倉庫文檔)
[root@localhost yum.repos.d]# mv C* a/(構建本地YUM倉庫文檔)
[root@localhost yum.repos.d]# vi ./local.repo(建立本地yum倉庫文檔)
[cdrom] //倉庫名稱
name=cdrom
baseurl=file:///media //指定rpm包的位置
enabled=1 //啓用本地yum倉庫
gpgcheck=0 //禁用gpg校驗
[root@localhost yum.repos.d]# yum -y clean all(清除yum緩存)
已加載插件:fastestmirror, langpacks
正在清理軟件源: cdrom
Cleaning up everything
Cleaning up list of fastest mirrors
[root@localhost yum.repos.d]# yum makecache(重建yum緩存)
已加載插件:fastestmirror, langpacks
cdrom | 3.6 kB 00:00:00
(1/4): cdrom/group_gz | 154 kB 00:00:00
(2/4): cdrom/filelists_db | 2.7 MB 00:00:00
(3/4): cdrom/other_db | 1.1 MB 00:00:00
(4/4): cdrom/primary_db | 2.7 MB 00:00:00
Determining fastest mirrors
元數據緩存已創建
十一、用yum命令安裝vsftpd,查詢安裝狀況,最後卸載vsftpd,並再次查詢卸載狀況?
[root@localhost ~]# rpm -q vsftpd
未安裝軟件包 vsftpd
(查詢是否安裝vsftpd)
[root@localhost ~]# yum -y install vsftpd
已加載插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
正在解決依賴關係
。
。
。
完畢!
(用yum安裝vsftpd)
[root@localhost ~]# rpm -q vsftpd
vsftpd-3.0.2-9.el7.x86_64
(用yum安裝vsftpd)
[root@localhost ~]# yum -y remove vsftpd
已加載插件:fastestmirror, langpacks
正在解決依賴關係
--> 正在檢查事務
。
。
。
完畢!
(用yum卸載vsftpd)
[root@localhost ~]# rpm -q vsftpd
未安裝軟件包 vsftpd
(查詢是否卸載vsftpd)
十二、用rpm命令安裝vsftpd,查詢安裝狀況,最後卸載vsftpd,並再次查詢卸載狀況?
[root@localhost ~]# rpm -q vsftpd"vsftpd"
未安裝軟件包 vsftpdvsftpd
(超序是否安裝vsftpdvsftpd包)
[root@localhost ~]# find / -name "vsftpd*.rpm"
/media/Packages/vsftpd-3.0.2-9.el7.x86_64.rpm
(查找安裝vsftpdvsftpd包)
[root@localhost ~]# cd /media/Packages/
(進入/media/Packages/)
[root@localhost Packages]# rpm -ihv vsftpd-3.0.2-9.el7.x86_64.rpm
警告:vsftpd-3.0.2-9.el7.x86_64.rpm: 頭V3 RSA/SHA256 Signature, 密鑰 ID f4a80eb5: NOKEY
準備中... ################################# [100%]
正在升級/安裝...
1:vsftpd-3.0.2-9.el7 ################################# [100%]
(安裝rpm包)
[root@localhost Packages]# rpm -q vsftpd
vsftpd-3.0.2-9.el7.x86_64
(查詢是否安裝rpm包)
[root@localhost Packages]# rpm -e vsftpd
(卸載rpm包)
[root@localhost Packages]# rpm -q vsftpd
未安裝軟件包 vsftpd
(查詢是否安裝rpm包)
1三、經過源碼方式經過解包、配置、編譯、安裝四個步驟安裝源碼軟件httpd-2.2.17.tar.gz?並進行測試?
[root@localhost Packages]# yum -y install gcc gcc-c++ make
已加載插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
。
。
。
完畢!
(準備操做,先安裝c++)
[root@localhost ~]# tar -xf httpd-2.2.17.tar.gz -C /usr/src
(解包到/usr/src)
[root@localhost ~]# cd /usr/src/httpd-2.2.17/
(進入包)
[root@localhost httpd-2.2.17]# ./configure –prefix=/usr/local/apache
。
。
。
config.status: creating build/config_vars.sh
config.status: creating include/ap_config_auto.h
config.status: executing default commands
(預配置)
[root@localhost httpd-2.2.17]# make
。
。
。
prutil-1.la /usr/src/httpd-2.2.17/srclib/apr-util/xml/expat/libexpat.la /usr/src/httpd-2.2.17/srclib/apr/libapr-1.la -lrt -lcrypt -lpthread -ldl
make[1]: 離開目錄「/usr/src/httpd-2.2.17」
(編譯)
[root@localhost httpd-2.2.17]# make install
Making install in srclib
make[1]: 進入目錄「/usr/src/httpd-2.2.17/srclib」
。
。
。
mkdir /usr/local/apache/man/man8
mkdir /usr/local/apache/manual
make[1]: 離開目錄「/usr/src/httpd-2.2.17」
(安裝)
[root@localhost httpd-2.2.17]# cd /usr/local/apache/conf/
(進入/usr/local/apache/conf/)
[root@localhost conf]# cp httpd.conf httpd.conf.bak
(拷貝httpd.conf httpd.conf.bak)
[root@localhost conf]# vi /usr/local/apache/conf/httpd.conf
(修改配置文件)
[root@localhost conf]# /usr/local/apache/bin/apachectl start
(啓動)
[root@localhost conf]# yum -y install lynx
已加載插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
。
。
。
完畢
(安裝lynx)
[root@localhost conf]# lynx 127.0.0.1
(運行)