配置靜態網絡地址
1.1 問題
本例要求為虛擬機 server 配置以下靜態地址參數:
- 主機名:server0.example.com
- IP地址:172.25.0.11
- 子網掩碼:255.255.255.0
- 默認網關:172.25.0.254
- DNS服務器:172.25.254.254
1.2 方案
使用nmcli配置網絡連接時的基本操作,
查看網絡連接、連接詳情:
- nmcli con show
- nmcli con show "連接名"
修改網絡連接參數:
- nmcli con modify "連接名" ipv4.method auto|manual
- nmcli con modify "連接名" ipv4.addresses "IP地址/掩碼長度 [默認網關]" ipv4.dns DNS服務器地址
- nmcli con modify "連接名" connection.autoconnect yes|no
1.3 步驟
實現此案例需要按照如下步驟進行。
步驟一:配置固定主機名
1)配置前, 檢查是否設置靜態主機名
- [root@server0 ~]# hostnamectl
- Static hostname: n/a //未設置靜態主機名
- Transient hostname: server0.example.com
2)設置為指定的主機名
- [root@server0 ~]# vim /etc/hostname //建立主機名配置文件
- server0.example.com
3)配置后,檢查結果
- [root@server0 ~]# hostnamectl
- Static hostname: server0.example.com //已設置靜態主機名
- Icon name: computer
- .. ..
步驟二:配置靜態IP地址參數
1)查看當前主機的網卡設備、網絡連接
- [root@server0 ~]# nmcli connection show
- 名稱 UUID 類型 設備
- System eth0 5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03 802-3-ethernet eth0
2)修改連接“System eth0”的配置
將配置方式指定為manual,指定IP地址、默認網關、DNS地址,並配置自動連接:
- [root@server0 ~]# nmcli connection modify "System eth0" ipv4.method manual ipv4.addresses "172.25.0.11/24 172.25.0.254" ipv4.dns 172.25.254.254 connection.autoconnect yes
3)重新激活連接“System eth0”
通過up指令激活連接配置,必要時也可以先down再up:
- [root@server0 ~]# nmcli connection up "System eth0" //激活連接
- Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/1)
確保系統服務NetworkManager開機自啟:
- [root@server0 ~]# systemctl restart NetworkManager
- [root@server0 ~]# systemctl enable NetworkManager
4)檢查修改結果,確認無誤
檢查IP地址:
- [root@server0 ~]# ifconfig eth0
- eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
- inet 172.25.0.11 netmask 255.255.255.0 broadcast 172.25.0.255
- inet6 fe80::5054:ff:fe00:b prefixlen 64 scopeid 0x20<link>
- ether 52:54:00:00:00:0b txqueuelen 1000 (Ethernet)
- RX packets 1394 bytes 138855 (135.6 KiB)
- RX errors 0 dropped 0 overruns 0 frame 0
- TX packets 944 bytes 98495 (96.1 KiB)
- TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
檢查默認網關地址:
- [root@server0 ~]# route -n
- Kernel IP routing table
- Destination Gateway Genmask Flags Metric Ref Use Iface
- 0.0.0.0 172.25.0.254 0.0.0.0 UG 1024 0 0 eth0
- 172.25.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
檢查DNS服務器地址:
- [root@server0 ~]# cat /etc/resolv.conf
- # Generated by NetworkManager
- search example.com
- nameserver 172.25.254.254
如果在使用nmcli修改網絡連接時並未指定ipv4.dns,也可以直接修改DNS客戶端配置文件/etc/resolv.conf,確保添加有上述記錄即可。
步驟三:驗證網絡配置結果
通過ssh遠程訪問server0:
- [root@room9pc13 ~]# ssh -X root@server0.example.com
- Warning: Permanently added 'server0.example.com' (ECDSA) to the list of known hosts.
- Last login: Fri Dec 23 19:00:12 2016 from 172.25.0.250
- [root@server0 ~]# hostname //確認自己的主機名
- server0.example.com
在虛擬機server0上,可以查詢server0、desktop0、content等站點:
- [root@server0 ~]# host server0.example.com
- server0.example.com has address 172.25.0.11
- [root@server0 ~]# host desktop0.example.com
- desktop0.example.com has address 172.25.0.10
- desktop0.example.com mail is handled by 10 smtp0.example.com.
- [root@server0 ~]# host content.example.com
- content.example.com has address 172.25.254.254
2 案例2:使用yum軟件源
2.1 問題
本例要求為虛擬機 server0指定可用的yum軟件源,相關要求如下:
- YUM軟件庫源為 http://content.example.com/rhel7.0/x86_64/dvd
- 將此配置為虛擬機 server0 的默認軟件倉庫
2.2 步驟
實現此案例需要按照如下步驟進行。
步驟一:檢查現有yum倉庫,去除不可用的設置
1)列出yum庫
- [root@server0 ~]# yum repolist
- 已加載插件:langpacks
- rhel_dvd | 4.1 kB 00:00:00
- (1/2): rhel_dvd/group_gz | 134 kB 00:00:00
- (2/2): rhel_dvd/primary_db | 3.4 MB 00:00:00
- 源標識 源名稱 狀態
- rhel_dvd Remote classroom copy of dvd 4,305
- repolist: 4,305
2)移除不可用的yum庫配置文件
當執行yum repolist操作報錯時,才執行此步驟(否則此步可跳過)。
- [root@server0 ~]# mkdir /etc/yum.repos.d/repobak
- [root@server0 ~]# mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/repobak/
步驟二:添加指定的yum倉庫配置
1)使用yum-config-manager工具建立新配置文件
- [root@server0 ~]# yum-config-manager --add-repo http://content.example.com/rhel7.0/x86_64/dvd
- 已加載插件:langpacks
- adding repo from: http://content.example.com/rhel7.0/x86_64/dvd
- [content.example.com_rhel7.0_x86_64_dvd]
- name=added from: http://content.example.com/rhel7.0/x86_64/dvd
- baseurl=http://content.example.com/rhel7.0/x86_64/dvd
- enabled=1
2)修改新建的倉庫配置,添加gpgcheck=0以禁用GPG簽名檢查
- [root@server0 ~]# vim /etc/yum.repos.d/content.example.com_rhel7.0_x86_64_dvd
- [content.example.com_rhel7.0_x86_64_dvd]
- name=added from: http://content.example.com/rhel7.0/x86_64/dvd
- baseurl=http://content.example.com/rhel7.0/x86_64/dvd
- enabled=1
- gpgcheck=0
步驟三:確認新配置的yum源可用
- [root@server0 ~]# yum clean all //清理緩存
- 已加載插件:langpacks
- 正在清理軟件源: content.example.com_rhel7.0_x86_64_dvd
- Cleaning up everything
- [root@server0 ~]# yum repolist //重新列出可用的源
- 已加載插件:langpacks
- content.example.com_rhel7.0_x86_64_dvd | 4.1 kB 00:00:00
- (1/2): content.example.com_rhel7.0_x86_64_dvd/group_gz | 134 kB 00:00:00
- (2/2): content.example.com_rhel7.0_x86_64_dvd/primary_db | 3.4 MB 00:00:00
- 源標識 源名稱 狀態
- content.example.com_rhel7.0_x86_64_dvd added from: http://content.example.com 4,305
- repolist: 4,305
3 案例3:升級Linux內核
3.1 問題
本例要求為虛擬機 server0安裝升級版的新內核:
- 新版本的內核安裝文件可以從以下地址獲取:
- http://classroom/content/rhel7.0/x86_64/errata/Packages/
- 升級內核,並滿足下列要求:當系統重新啟動后,升級的新內核應該作為默認內核;原來的內核要被保留,並且仍然可以正常啟動
3.2 步驟
實現此案例需要按照如下步驟進行。
步驟一:下載新版內核的安裝文件
1)確認新版內核的下載地址
如果給定的下載地址中未包含kernel-...rpm文件路徑,則打開firefox瀏覽器,訪問指定的網址(如圖-5所示)。

圖-5
在打開的網頁上找到需要的內核文件,右擊對應的鏈接,選擇“Copy Link Location”復制下載地址(如圖-6所示)。

圖-6
2)下載新版內核安裝文件
根據前一步獲取到的內核下載地址,使用wget命令下載:
- [root@server0 ~]# wget http://classroom/content/rhel7.0/x86_64/errata/Packages/kernel-3.10.0-123.1.2.el7.x86_64.rpm
- --2016-12-23 22:13:47-- http://classroom/content/rhel7.0/x86_64/errata/Packages/kernel-3.10.0-123.1.2.el7.x86_64.rpm
- 正在解析主機 classroom (classroom)... 172.25.254.254
- 正在連接 classroom (classroom)|172.25.254.254|:80... 已連接。
- 已發出 HTTP 請求,正在等待回應... 200 OK
- 長度:30266784 (29M) [application/x-rpm]
- 正在保存至: “kernel-3.10.0-123.1.2.el7.x86_64.rpm”
- 100%[=========================================>] 30,266,784 40.4MB/s 用時 0.7s
- 2016-12-23 22:13:47 (40.4 MB/s) - 已保存 “kernel-3.10.0-123.1.2.el7.x86_64.rpm” [30266784/30266784])
- [root@server0 ~]# ls -lh kernel-*.rpm //確認下載結果
- -rw-r--r--. 1 root root 29M 6月 11 2014 kernel-3.10.0-123.1.2.el7.x86_64.rpm
步驟二:安裝新版內核
Linux系統支持安裝多個不同版本的內核,開機引導時可以選擇使用哪個版本。因此只需要正常安裝新版內核即可。
1)查看現有內核版本
- [root@server0 ~]# uname -r
- 3.10.0-123.el7.x86_64
2)安裝新版本內核
- [root@server0 ~]# rpm -ivh kernel-3.10.0-123.1.2.el7.x86_64.rpm
- 警告:kernel-3.10.0-123.1.2.el7.x86_64.rpm: 頭V3 RSA/SHA256 Signature, 密鑰 ID fd431d51: NOKEY
- 准備中... ################################# [100%]
- 正在升級/安裝...
- 1:kernel-3.10.0-123.1.2.el7 ################################# [100%]
- //此處需耐心等待,千萬別強行終止
- [root@server0 ~]#
步驟三:確認新內核版本
1)重啟系統
- [root@server0 ~]# reboot
- .. ..
2)登入系統,確認使用的內核已是新版本
- [root@server0 ~]# uname -r
- 3.10.0-123.1.2.el7.x86_64
4 案例4:查找並處理文件
4.1 問題
本例要求采用不少於兩種方法完成以下任務:
- 找出所有用戶 student 擁有的文件
- 把它們拷貝到 /root/findfiles/ 文件夾中
4.2 步驟
實現此案例需要按照如下步驟進行。
步驟一:確認能找到指定的文件
1)確認新版內核的下載地址
- [root@server0 ~]# find / -user student -type f
- find: ‘/proc/1853/task/1853/fdinfo/6’: 沒有那個文件或目錄
- find: ‘/proc/1853/fdinfo/6’: 沒有那個文件或目錄
- /var/spool/mail/student
- /home/student/.bash_logout
- /home/student/.bash_profile
- /home/student/.bashrc
- /home/student/.ssh/authorized_keys
- /home/student/.config/gnome-initial-setup-done
- /home/student/.config/monitors.xml
對於上述操作中出現的/proc信息忽略即可。
步驟二:處理找到的文件
1)創建目標文件夾
- [root@server0 ~]# mkdir /root/findfiles
2)拷貝找到的文件到目標文件夾
以下兩種方法任選一種:
- [root@server0 ~]# find / -user student -type f -exec cp -p {} /root/findfiles/ \;
- .. ..
- 或者
- [root@server0 ~]# \cp -p $(find / -user student -type f) /root/findfiles/
- .. ..
3)確認拷貝結果
- [root@server0 ~]# ls -lhA /root/findfiles/
- 總用量 24K
- -rw-------. 1 student student 1.7K 7月 11 2014 authorized_keys
- -rw-r--r--. 1 student student 18 1月 29 2014 .bash_logout
- -rw-r--r--. 1 student student 193 1月 29 2014 .bash_profile
- -rw-r--r--. 1 student student 231 1月 29 2014 .bashrc
- -rw-r--r--. 1 student student 4 7月 11 2014 gnome-initial-setup-done
- -rw-r--r--. 1 student student 1.5K 7月 11 2014 monitors.xml
- -rw-rw----. 1 student mail 0 7月 11 2014 student
5 案例5:查找並提取文件內容
5.1 問題
本例要求在文件/usr/share/dict/words中查找到所有包含字符串seismic的行,並滿足下列要求:
- 將找到的行按原文順序拷貝到 /root/wordlist 文件中
- 文件 /root/wordlist 不要包含空行,並且其中所有行的內容必須是 /usr/share/dict/words 文件中原始行的准確副本
5.2 步驟
實現此案例需要按照如下步驟進行。
1)使用grep命令查找指定的關鍵詞,並通過重定向輸出保存到指定的文件:
- [root@serverX ~]# grep 'seismic' /usr/share/dict/words > /root/wordlist
2)確認提取結果
- [root@server0 ~]# cat /root/wordlist
- anaseismic
- antiseismic
- aseismic
- aseismicity
- bradyseismic
- .. ..