單純提供一個相對的解答,並不是標準答案!
單純就是個解答的參考,寫完之後再來這邊查查看答案跟你想的一樣不一樣!?
# 先設定好網路參數,所有的參數通通設定好再啟用! [root@localhost ~]# nmcli connection modify ens3 ipv4.method manual \ > ipv4.addresses 172.16.60.111/16 ipv4.gateway 172.16.200.254 ipv4.dns 168.95.1.1 # 比較重要的,就是上面反白的部份!每個同學都要不一樣! [root@localhost ~]# nmcli connection up ens3 [root@localhost ~]# hostnamectl hostname station111.rockylinux
# a. 先經過 nmcli 來檢查剛剛設定的是否正確的生效了! [root@station111 ~]# nmcli connection show ens3 connection.id: ens3 connection.interface-name: ens3 connection.autoconnect: 是 ipv4.method: manual ipv4.dns: 168.95.1.1 ipv4.addresses: 172.16.60.111/16 ipv4.gateway: 172.16.200.254 IP4.ADDRESS[1]: 172.16.60.111/16 IP4.GATEWAY: 172.16.200.254 IP4.DNS[1]: 168.95.1.1 # 比較需要注意的,大概就是這幾個設定項目與實際運作的項目了! # b.1. 檢查各種訊息,先找到上述 ipv4.gateway 後面接的那個 IP,直接 ping 她! [root@station111 ~]# ping 172.16.200.254 PING 172.16.200.254 (172.16.200.254) 56(84) bytes of data. 64 bytes from 172.16.200.254: icmp_seq=1 ttl=64 time=0.302 ms 64 bytes from 172.16.200.254: icmp_seq=2 ttl=64 time=0.301 ms 64 bytes from 172.16.200.254: icmp_seq=3 ttl=64 time=0.315 ms ^C <==這裡使用 [crtl]+c 中斷指令 --- 172.16.200.254 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 70ms rtt min/avg/max/mdev = 0.301/0.306/0.315/0.006 ms # b.2. 如上所示,要出現 time=xxx ms 的字樣,才是真的連到該通訊閘喔!很重要! # b.3. 那個 ms 指的是 10-3 秒,就是 0.001 秒,很小的單位。 # 意義是,將訊息在 Server/Client 兩端傳送所經過的時間,越小越好! # c. 使用 dig 可以觀察 DNS 由主機名稱找到 IP 的資訊。 [root@station111 ~]# dig www.google.com ; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el8 <<>> www.google.com ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 60556 ;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ; COOKIE: 15d71555225eb80855fcf4d25ec0df934fd9517a795492fb (good) ;; QUESTION SECTION: ;www.google.com. IN A <==指的是 Address 的縮寫 ;; ANSWER SECTION: www.google.com. 104 IN A 172.217.27.132 <==找到正確的 IP 囉! ;; Query time: 4 msec ;; SERVER: 168.95.1.1#53(168.95.1.1) <==我們是向這部主機詢問的結果! ;; WHEN: 日 5月 17 14:54:08 CST 2020 ;; MSG SIZE rcvd: 87 # 透過 QUESTION 分析問題,透過 ANSWER 回答問題,透過 SERVER 確認哪個伺服器的回應, # 所以,當然由 ANSWER 回答了我們的問題,得到了上述 172.217.27.132 這個 IP 位址。 # d. 要找到本機的主機名稱,通常有兩個常用的指令如下: [root@station111 ~]# hostname station111.rockylinux [root@station111 ~]# hostnamectl Static hostname: station200.rockylinux Icon name: computer-vm Chassis: vm Machine ID: d587a2c04fbb458e8015aef30df28fec Boot ID: d47cd3298e1c4e919003299b9cf413be Virtualization: kvm Operating System: Rocky Linux 9.1 (Blue Onyx) CPE OS Name: cpe:/o:rocky:rocky:9::baseos Kernel: Linux 5.14.0-162.12.1.el9_1.0.2.x86_64 Architecture: x86-64 Hardware Vendor: Red Hat Hardware Model: KVM [root@station111 ~]# cat /etc/hostname station111.rockylinux # 事實上,主機的名稱記載在 /etc/hostname 嚕!
[root@station111 ~]# timedatectl --help timedatectl [OPTIONS...] COMMAND ... Query or change system time and date settings. ....... Commands: status Show current time settings show Show properties of systemd-timedated set-time TIME Set system time set-timezone ZONE Set system time zone list-timezones Show known time zones set-local-rtc BOOL Control whether RTC is in local time set-ntp BOOL Enable or disable network time synchronization ....... [root@station111 ~]# timedatectl list-timezones Africa/Abidjan Africa/Accra Africa/Addis_Ababa ...... Pacific/Tongatapu Pacific/Wake Pacific/Wallis UTC lines 383-426/426 (END)可以找到標準時區的 UTC,但是找不到日本時區!要用東京 (Tokyo) 就可以找到!
[root@station111 ~]# find /usr/share/zoneinfo -name '*Japan*' /usr/share/zoneinfo/posix/Japan /usr/share/zoneinfo/right/Japan /usr/share/zoneinfo/Japan [root@station111 ~]# find /usr/share/zoneinfo -name '*Taipei*' /usr/share/zoneinfo/Asia/Taipei /usr/share/zoneinfo/posix/Asia/Taipei /usr/share/zoneinfo/right/Asia/Taipei其實是有日本時區喔!等等使用 /usr/share/zoneinfo/Japan 來檢查看看!
[root@station111 ~]# date Mon May 1 09:40:53 PM CST 2023 [root@station111 ~]# timedatectl set-timezone Asia/Tokyo [root@station111 ~]# date Mon May 1 10:41:27 PM JST 2023原來時間差異 1 個小時喔!台灣與日本東京!
[root@station111 ~]# date Mon May 1 10:42:01 PM JST 2023 [root@station111 ~]# timedatectl set-timezone UTC [root@station111 ~]# date Mon May 1 01:42:12 PM UTC 2023格林威治時間比日本時間慢了 9 個小時啊~比台灣慢了 8 個小時啊!
[root@station111 ~]# timedatectl set-timezone Asia/Taipei [root@station111 ~]# date Mon May 1 09:42:50 PM CST 2023 [root@station111 ~]# man timedatectl .... set-time [TIME] Set the system clock to the specified time. This will also update the RTC time accordingly. The time may be specified in the format "2012-10-30 18:17:16". [root@station111 ~]# timedatectl set-ntp false [root@station111 ~]# timedatectl set-time "2023-05-01 21:50:00" [root@station111 ~]# date Mon May 1 09:50:02 PM CST 2023
[root@station111 ~]# firewall-cmd --get-services RH-Satellite-6 amanda-client amanda-k5-client amqp amqps apcupsd audit bacula bacula-client bb bgp bitcoin bitcoin-rpc bitcoin-testnet bitcoin-testnet-rpc bittorrent-lsd ceph ceph-mon cfengine cockpit ...... [root@station111 ~]# firewall-cmd --permanent --list-services cockpit dhcpv6-client http https ssh # 底下先刪除所有的服務 [root@station111 ~]# firewall-cmd --permanent --remove-service=cockpit --remove-service=http \ > --remove-service=dhcpv6-client --remove-service=ssh --remove-service=https success [root@station111 ~]# firewall-cmd --permanent --list-services # 再加入需要的服務囉! [root@station111 ~]# firewall-cmd --permanent --add-service=http --add-service=https \ > --add-service=ssh --add-service=ftp success [root@station111 ~]# firewall-cmd --permanent --list-services ftp http https ssh [root@station111 ~]# firewall-cmd --reload
[root@station111 ~]# man firewalld.richlanguage EXAMPLES These are examples of how to specify rich language rules. This format (i.e. one string that specifies whole rule) uses for example firewall-cmd --add-rich-rule (see firewall-cmd(1)) as well as D-Bus interface. ....... Example 3 Allow new IPv4 connections from address 192.168.0.0/24 for service tftp and log 1 per minutes using syslog rule family="ipv4" source address="192.168.0.0/24" service name="tftp" log prefix="tftp" level="info" limit value="1/m" accept ....... Example 6 White-list source address to allow all connections from 192.168.2.2 rule family="ipv4" source address="192.168.2.2" accept ....... [root@station111 ~]# firewall-cmd --permanent \ > --add-rich-rule="rule family='ipv4' source address='172.16.100.254' accept" [root@station111 ~]# firewall-cmd --permanent --add-rich-rule="rule family='ipv4' \ > source address='172.16.0.0/16' service name='ssh' accept" [root@station111 ~]# firewall-cmd --permanent --list-all public target: default icmp-block-inversion: no interfaces: sources: services: ftp http https ssh ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: rule family="ipv4" source address="172.16.100.254" accept rule family="ipv4" source address="172.16.0.0/16" service name="ssh" accept
[root@station111 ~]# firewall-cmd --reload [root@station111 ~]# firewall-cmd --list-services ftp http https ssh [root@station111 ~]# firewall-cmd --list-rich-rules rule family="ipv4" source address="172.16.100.254" accept rule family="ipv4" source address="172.16.0.0/16" service name="ssh" accept
[root@station111 ~]# cd /dev/shm [root@station111 shm]# mkdir zip [root@station111 shm]# cd zip [root@station111 zip]# ll -Sr /etc ..... -rw-r--r--. 1 root root 39107 Feb 26 14:46 ld.so.cache -rw-r--r--. 1 root root 67454 Apr 22 2020 mime.types -rw-r--r--. 1 root root 692252 Jun 23 2020 services [root@station111 zip]# cp /etc/services filename.1 [root@station111 zip]# cp /etc/services filename.2 [root@station111 zip]# cp /etc/services filename.3 [root@station111 zip]# cp /etc/services filename.4 [root@station111 zip]# ll -rw-r--r--. 1 root root 692252 May 7 15:30 filename.1 -rw-r--r--. 1 root root 692252 May 7 15:30 filename.2 -rw-r--r--. 1 root root 692252 May 7 15:30 filename.3 -rw-r--r--. 1 root root 692252 May 7 15:30 filename.4
[root@station111 zip]# time gzip filename.1 real 0m0.029s user 0m0.027s sys 0m0.002s [root@station111 zip]# time bzip2 filename.2 real 0m0.069s user 0m0.063s sys 0m0.002s [root@station111 zip]# time xz filename.3 real 0m0.381s user 0m0.337s sys 0m0.009s [root@station111 zip]# man xz -T threads, --threads=threads [root@station111 zip]# time xz -T 2 --block-size=128K filename.4 real 0m0.212s user 0m0.358s sys 0m0.024s [root@station111 zip]# ll -rw-r--r--. 1 root root 142530 May 7 15:30 filename.1.gz -rw-r--r--. 1 root root 129328 May 7 15:30 filename.2.bz2 -rw-r--r--. 1 root root 105872 May 7 15:30 filename.3.xz -rw-r--r--. 1 root root 113868 May 7 15:30 filename.4.xz基本上,xz 透過多執行緒進行壓縮時,最好給予一個固定的 block size 作為每個執行緒分到的量,然後個別進行壓縮, 這樣的效率會好上很多。因為我們的檔案容量不大,因此採用比較小的 128K 作為切割,你可以看到 user 的時間花費大約是 0.358s, 跟預設的 xz 單執行緒沒有差很多!不過,因為用到兩顆 CPU,那個 0.358s 其實是兩顆 CPU 使用時間的總和!所以, 事實上人類時間花費只有 0.212s,是比單執行緒好很多的!另外,因為我們上面測試的是小型的檔案,所以, xz 的多執行緒效果不明顯。如果是大型的檔案,使用 xz 的多執行緒,其速度效能要比其他兩個好喔!
[root@station111 zip]# time gzip -d filename.1.gz real 0m0.007s user 0m0.006s sys 0m0.001s [root@station111 zip]# time bzip2 -d filename.2.bz2 real 0m0.027s user 0m0.023s sys 0m0.004s [root@station111 zip]# time xz -d filename.3.xz real 0m0.016s user 0m0.014s sys 0m0.001s [root@station111 zip]# ll -rw-r--r--. 1 root root 692252 May 7 15:30 filename.1 -rw-r--r--. 1 root root 692252 May 7 15:30 filename.2 -rw-r--r--. 1 root root 692252 May 7 15:30 filename.3 -rw-r--r--. 1 root root 113868 May 7 15:30 filename.4.xz
[root@station111 zip]# gzip -c filename.1 > filename.1.gz [root@station111 zip]# ll filename.1* -rw-r--r--. 1 root root 692252 May 7 15:30 filename.1 -rw-r--r--. 1 root root 142530 May 7 15:34 filename.1.gz
# 解壓縮的方式,當然就是 -x 這個支援項目 # 先在本目錄底下解開: [root@station111 zip]# ll -rw-r--r--. 1 root root 3267448 May 7 15:35 etc.tar.xz [root@station111 zip]# tar -Jx -f etc.tar.xz [root@station111 zip]# ll drwxr-xr-x. 130 root root 4820 May 7 15:04 etc -rw-r--r--. 1 root root 3267448 May 7 15:35 etc.tar.xz # 在非本目錄解開,可以透過 -C 來處理 [root@station111 zip]# ll -d /tmp/etc* ls: 無法存取 '/tmp/etc*': 沒有此一檔案或目錄 [root@station111 zip]# tar -Jx -f etc.tar.xz -C /tmp [root@station111 zip]# ll -d /tmp/etc* drwxr-xr-x. 130 root root 8192 May 7 15:04 /tmp/etc
# 先測試單執行緒的時間 [root@station111 zip]# time tar -Jc -f etc2.tar.xz /etc tar: Removing leading `/' from member names real 0m12.891s user 0m12.752s sys 0m0.141s # 使用管線的方式,來處理一下資料的壓縮: [root@station111 zip]# tar -Jc -f - /etc | cat > etc3.tar.xz [root@station111 zip]# ll etc*tar* -rw-r--r--. 1 root root 3267448 May 7 15:38 etc2.tar.xz -rw-r--r--. 1 root root 3267448 May 7 15:40 etc3.tar.xz -rw-r--r--. 1 root root 3267448 May 7 15:35 etc.tar.xz # 使用多執行緒來處理看看: [root@station111 zip]# cat /proc/cpuinfo | grep processor processor : 0 processor : 1 # 出現幾行就是有幾顆 CPU 的意思。不過,這裡應該指的是執行緒喔! [root@station111 zip]# time tar -c -f - /etc | xz -c > etc4.tar.xz tar: Removing leading `/' from member names real 0m13.181s user 0m13.029s sys 0m0.145s [root@station111 zip]# time tar -c -f - /etc | xz -T 2 --block-size=1M -c > etc4.tar.xz tar: Removing leading `/' from member names real 0m6.006s user 0m11.654s sys 0m0.124s # 你只要看 real 項目就好,真的速度有差喔!雖然壓縮後的容量會稍微放大! [root@station111 zip]# ll -d etc* drwxr-xr-x. 130 root root 4820 May 7 15:04 etc -rw-r--r--. 1 root root 3267448 May 7 15:38 etc2.tar.xz -rw-r--r--. 1 root root 3267448 May 7 15:40 etc3.tar.xz -rw-r--r--. 1 root root 3582312 May 7 15:41 etc4.tar.xz -rw-r--r--. 1 root root 3267448 May 7 15:35 etc.tar.xz
[root@station111 ~]# mkdir /backups [root@station111 ~]# cd /backups/ [root@station111 backups]# vim backup_system.sh #!/bin/bash mysource="/etc /home /root /var/spool/mail /var/spool/cron /var/spool/at" mytarget="/backups/backup_system_$( date +%Y_%m_%d ).tar.gz" tar -zc -f ${mytarget} ${mysource} [root@station111 backups]# sh backup_system.sh tar: Removing leading `/' from member names tar: Removing leading `/' from hard link targets [root@station111 backups]# ll -rw-r--r--. 1 root root 39847266 May 7 15:43 backup_system_2023_05_07.tar.gz -rw-r--r--. 1 root root 184 May 7 15:43 backup_system.sh
# 某些小型系統確實不會啟動 atd 的,所以,如果發現沒啟動,就啟動它即可! [root@station111 backups]# systemctl restart atd [root@station111 backups]# systemctl enable atd
[root@station111 backups]# at 17:00 2023-12-31 warning: commands will be executed using /bin/sh at> wall 'This system will shutdown at 17:30. Please backup your work and logout system' at> sleep 30m at> poweroff at> <EOT> job 2 at Sun Dec 31 17:00:00 2023
[root@station111 ~]# at now + 4 hours warning: commands will be executed using /bin/sh at> poweroff at> <EOT> job 3 at Sun May 7 19:50:00 2023
[student@station111 22:50 2 ~]$ crontab -e 0 11 * * * /usr/sbin/ip addr show > /home/student/myipshow.txt */10 * * * * ip addr show > /dev/shm/myip [student@station111 22:52 3 ~]$ crontab -l 0 11 * * * /usr/sbin/ip addr show > /home/student/myipshow.txt */10 * * * * ip addr show > /dev/shm/myip
[root@station111 ~]# chmod 744 /backups/backup_system.sh [root@station111 ~]# ll /backups/backup_system.sh -rwxr--r--. 1 root root 184 May 7 15:43 /backups/backup_system.sh [root@station111 ~]# cd /etc/cron.monthly/ [root@station111 cron.monthly]# ln /backups/backup_system.sh . [root@station111 cron.monthly]# ll -rwxr--r--. 2 root root 184 May 7 15:43 backup_system.sh
# a. 探查一下有多少 at job 存在! [root@station111 ~]# atq 1 Mon May 8 11:00:00 2023 a student 2 Sun Dec 31 17:00:00 2023 a root 3 Sun May 7 19:50:00 2023 a root [root@station111 ~]# ll /var/spool/at total 12 -rwx------. 1 student student 2844 May 7 15:47 a0000101ac23d4 -rwx------. 1 root root 3517 May 7 15:49 a0000201b15a5c -rwx------. 1 root root 3422 May 7 15:50 a0000301ac2046 drwx------. 2 root root 6 Oct 26 2022 spool # 可以看到三個檔案!這三個檔案就是 at job 的實際設定資訊! # b. 檢視有沒有 poweroff 時,可以這樣做: [root@station111 ~]# at -c 1 | tail PATH=/home/student/.local/bin:/home/student/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:\~/cmd; export PATH MAIL=/var/spool/mail/student; export MAIL cd /home/student || { echo 'Execution directory inaccessible' >&2 exit 1 } ${SHELL:-/bin/sh} << 'marcinDELIMITER5e00cb31' ip addr show &> /home/student/myipshow.txt marcinDELIMITER5e00cb31 [root@station111 ~]# at -c 2 | tail cd /backups || { echo 'Execution directory inaccessible' >&2 exit 1 } ${SHELL:-/bin/sh} << 'marcinDELIMITER41e6be6a' wall 'This system will shutdown at 17:30. Please backup your work and logout system' sleep 30m poweroff marcinDELIMITER41e6be6a [root@station111 ~]# at -c 3 | tail SSH_TTY=/dev/pts/0; export SSH_TTY OLDPWD=/root; export OLDPWD cd /backups || { echo 'Execution directory inaccessible' >&2 exit 1 } ${SHELL:-/bin/sh} << 'marcinDELIMITER6ff1bc3e' poweroff marcinDELIMITER6ff1bc3e # 所以看起來只有 2, 3 號有關機任務,所以,取消這兩個即可! [root@station111 ~]# atrm 2 [root@station111 ~]# atrm 3 [root@station111 ~]# atq 1 Mon May 8 11:00:00 2023 a student # c. 檢查一下 crond 這個服務的紀錄檔! [root@station111 ~]# tail /var/log/cron May 7 15:53:01 station200 anacron[2089]: Job `cron.weekly' started May 7 15:53:01 station200 anacron[2089]: Job `cron.weekly' terminated May 7 15:53:01 station200 anacron[2089]: Normal exit (2 jobs run) May 7 15:53:13 station200 crontab[3265]: (student) REPLACE (student) May 7 15:53:13 station200 crontab[3265]: (student) END EDIT (student) May 7 15:53:15 station200 crontab[3270]: (student) LIST (student) May 7 15:54:01 station200 crond[1115]: (*system*) RELOAD (/etc/crontab) May 7 16:00:01 station200 CROND[3307]: (student) CMD (ip addr show > /dev/shm/myip) May 7 16:00:01 station200 CROND[3304]: (student) CMDOUT (/bin/sh: line 1: ip: command not found) May 7 16:00:01 station200 CROND[3304]: (student) CMDEND (ip addr show > /dev/shm/myip) # 確實有執行喔!不過指令有點問題!應該要用絕對路徑比較好! # d. 其實就是查看一般用戶的 crontab 設定 [root@station111 ~]# ll /var/spool/cron -rw-------. 1 student student 107 May 7 15:53 student [root@station111 ~]# cat /var/spool/cron/student 0 11 * * * /usr/sbin/ip addr show > /home/student/myipshow.txt */10 * * * * ip addr show > /dev/shm/myip # e. 幫一般帳號處理 crontab 的方法: [root@station111 ~]# crontab -e -u student 0 11 * * * /usr/sbin/ip addr show > /home/student/myipshow.txt #*/10 * * * * ip addr show > /dev/shm/myip [root@station111 ~]# crontab -l -u student