--- title: Linux 上課筆記 tags: Note_Linux, Linux description: Linux . --- # Linux 上課筆記([RockyLinux Download ISO](https://rockylinux.org/download)) > https://rockylinux.org/download > curl -o RockyLinux_9.0_VMM.7z https://dldatac.linuxvmimages.com/VMware/R/rockylinux/9/RockyLinux_9.0_VMM.7z # [WSL 安裝](https://hackmd.io/msZd8xT-QFeS0jWgw3kvKQ) # [Linux 上課筆記 Vmware Network 設定](https://hackmd.io/@JSYang1967/Vmware) # 瞭解Linux - Linux簡史:1991年,林納斯·托瓦茲在赫爾辛基大學上學時,對作業系統很好奇。他對MINIX只允許在教育上使用很不滿(在當時MINIX不允許被用作任何商業使用),於是他便開始寫他自己的作業系統,這就是後來的Linux核心。林納斯·托瓦茲在1991年10月5日首次發布。 - 1991年:芬蘭大學生Linus Torvalds的一則簡訊 Torvalds 在 1991 年於 BBS 上面公告他透過 GNU 的 bash, gcc 等,透過學習 Minix 系統,在 x86 (386) 上面成功的開發一個小型的作業系統, 並且放在 Internet 上面提供提供大家自由下載。同時,還鼓勵大家告知 Torvalds 自己,這個系統還有哪些部份可以值得繼續修改等的訊息。 這就是 Linux 的起源! - Linux的發行版(distribution):Linux被打包成供個人電腦和伺服器使用的Linux套件,一些流行的主流Linux發布版,包括Debian(及其衍生版本Ubuntu、Linux Mint)、Fedora(及其相關版本Red Hat Enterprise Linux、CentOS)和OpenSUSE等。較為知名的有Debian、Ubuntu、Fedora、CentOS、Arch Linux和OpenSUSE等 - Linux也是自由軟體和開放原始碼軟體發展中最著名的例子。只要遵循GNU 通用公共許可證(GPL),任何個人和機構都可以自由地使用Linux的所有底層原始碼,也可以自由地修改和再發布。 - GNU :GNU(GNU’s Not UNIX)是由Richard Stallman所推廣,後來成立自由軟體基金會(Free Software Foundation, FSF),全力為GNU的推展。GNU主要是要發展一套UNIX-Like的作業系統,而且是Free Software。目前使用Linux為核心的作業系統,嚴格說來應該稱為GNU/Linux systems。 GNU的發音是『guh-NEW』。 - 1984年之一:x86架構的Minix作業系統開始撰寫並於兩年後誕生 因為 SystemV 之後,大學老師不可以教授 Unix 核心原始碼,因此 Andrew Tanenbaum 自己動手寫了 Minix 這個 Unix Like 的核心程式! 同時搭配 BBS 新聞群組與相關書籍來販售 Unix Like 的程式碼。因為強調的是學習程式碼,因此改版的速度相當緩慢。 - 1984年之二:GNU計畫與FSF基金會的成立 Richard Mathew Stallman(史托曼)在1984年發起的 GNU 計畫,目的是想要恢復以前『知識分享的駭客文化』,因此強調程式碼需要公開以利學習的自由軟體概念, 並開發出 bash, gcc, glibc, emacs 等膾炙人口的軟體。Stallman 將所有的軟體都上網,但是沒有網路的朋友也能夠透過郵件請 Stallman 寄送軟體磁帶,Stallman 經由這樣販售 emacs 的『服務費用』(Stallman 認為協助人們燒錄軟體,花費他很多的時間成本) 賺了點錢,然後成立了自由軟體基金會 (FSF, Free Software Foundation),同時與律師共同簽署了 GNU 的通用公共許可證(General Public License, GPL), 該授權讓使用者可以自由的使用軟體,且軟體的授權可以永續的存在。 - GPL:許多的Linux工具都在GPL(GNU General Public License)保護之下發展。 在GPL保護之下,程式設計者仍然擁有合法的版權,但是允許其它人以任何方式取得、更改和販賣改好的程式,唯一的限制是,不能剝奪他人同樣的權利。而且,由於程式碼開放,某些工具可能經過許多人共同開發、修改,因此它的所有權是由許多人擁有的。 - GNU 的 GPL 授權有底下的權力與義務: - 取得軟體與原始碼:你可以根據自己的需求來執行這個自由軟體; - 複製:你可以自由的複製該軟體; - 修改:你可以將取得的原始碼進行程式修改工作,使之適合你的工作; - 再發行:你可以將你修改過的程式,再度的自由發行,而不會與原先的撰寫者衝突; - 回饋:你應該將你修改過的程式碼回饋於社群! - 不可修改授權:你不能將一個GPL授權的自由軟體,在你修改後而將他取消GPL授權~ - 不可單純販賣:你不能單純的販賣自由軟體。 - 資料來源: https://linux.vbird.org/linux_basic_train/centos8/unit01.php ![](https://i.imgur.com/aokW9dW.png) > https://www.tibame.com/offline/k8s?classUid=821 ![](https://i.imgur.com/id9nJov.png) ![](https://i.imgur.com/50kQYvr.png) https://www.tibame.com/offline/PenetrationTest?classUid=909 * 64位元電腦(window、mac、linux都可) * 建議記憶體8G以上、最少 20GB 磁碟空間 * 建請先安裝VM虛擬化軟體 (windows系統: VMware Workstation Player),並下載 Linux VMware images。 * 使用mac系統者,請安裝UTM、VMware Fusion或parallels desktop,並安裝 Linux。 > [Day25 CentOS Stream 圖形模式基本安裝](https://ithelp.ithome.com.tw/articles/10222392) > [【 VM 】如何在 Oracle VirtualBox 中安裝 Ubuntu](https://learningsky.io/install-ubuntu-on-oracle-virtualbox/) > [rocky linux vmware image](https://www.linuxvmimages.com/images/rockylinux-9/#import-vm-image) > Username: rockylinux > Password : rockylinux > (to become root, use sudo su -) ![](https://i.imgur.com/oXITyu1.png) ## HD Tune 硬碟檢測軟體 ![](https://i.imgur.com/AZTbkv7.png) ![](https://i.imgur.com/3nyUfvu.png) # [VMware](https://hackmd.io/R7tGeQdCRImvFwkBE5FgOQ) ## 事前準備 - DOMAIN及IP 安裝系統之前,請先確認IP Address設定需要的訊息,包括主機的網域名稱以及電腦可以使用的IP位址。 - 如果並未申請網域名稱的話,你自己也可以訂個網域名稱 ,那我就自行決定設定我自己的網域名稱為『ntcbim??.ntub.tw』。 - 如果本身並沒有固定IP位址可以使用,那麼你可以使用192.168.1.1這個IP位址。 - VmWare:ens160 inet 192.168.18.129 ### 登入系統 * 請輸入『root』以系統管理員身份登入,輸入後按『Enter』鍵繼續。 * 輸入密碼『centos』或者是你之前輸入的密碼,再按『Enter』鍵登入。 * Rocky Linux: student ### 虛擬的終端機 * 在一台Linux系統中,可同時在本機端使用七個終端機,我們可以透過『Ctrl + Alt + F1』~『Ctrl + Alt + F7』的組合鍵來切換七個不同的終端機。(『Alt + F1』~『Alt + F7』) * 其中第1,2(7)個虛擬終端機比較特別,是專門給X-Window的視窗環境使用,也就是說,我們一啟動Linux系統,進入X-Window之後,就一直使用第1,2(7)個終端機。 <!-- ## 分割硬碟的觀念 * 硬碟編號 在Linux系統中的硬碟是以sda、sdb…的方式來指定的,它的關係圖如下: * 分割硬碟 硬碟的分割必須視狀況決定該如何分割,最少一定要有二個:『/』、『swap』: – / 除了給swap的記憶體之外,其餘都分給『/』使用 * 實際上Linux Server在運作時,我們會考量很多的因素而將硬碟分割成數個分割區,分別給予適當大小,例如: – / 8GB;目前Linux完全安裝需4.7GB左右,夠用即可 – /home 和『/var』一樣大;使用者的個人目錄、網頁空間 – /var 和『/home』一樣大;ftp、資料庫、Email使用的空間 – swap 記憶體的1.5倍至2倍空間;作為虛擬記憶體 --> ## 登入登出 ### 登入 我們要使用系統,就一定要做『登入(login)』的動作,由系統驗證我們的帳號、密碼是否為系統所允許的使用者。 * 我們可以在遠端透過網路連線到Linux系統,使我們不必親自到Linux的電腦前面,就可以直接透過網路在遠端針對Linux系統做管理、設定、維護的動作。遠端登入的方式有二種: –ssh 140.131.114.?? (ssh –p 3939 centos@140.131.114.??) –telnet 140.131.114.?? ```shell= $ ssh student@192.168.x.52 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY! Someone could be eavesdropping on you right now (man-in-the-middle attack)! It is also possible that a host key has just been changed. The fingerprint for the ED25519 key sent by the remote host is SHA256:WGHEdbUeRGrvM1PgFPc/bhjO/kuY831yiTBZEky7OtI. Please contact your system administrator. Add correct host key in /c/Users/user/.ssh/known_hosts to get rid of this message. Offending ECDSA key in /c/Users/user/.ssh/known_hosts:3 Host key for 192.168.x.52 has changed and you have requested strict checking. Host key verification failed. ``` - 系統會顯示目前的登入名稱 ```shell= whoami ``` - 系統會顯示誰在線上 ```shell= who ``` ### 登出 『登出(logout)』是告知系統結束我們的服務,使用『logout』、『exit』都可以離開目前操作的使用者身份。 - 登出是使用者不再繼續使用系統,但是系統仍然還在運作(開機)、提供服務給其它的使用者。 ## AppStream [Failed to download metadata for repo ‘AppStream’ [CentOS]](https://techglimpse.com/failed-metadata-repo-appstream-centos-8/) ``` [centos@autocontroller ~]$ su - [root@autocontroller ~]# cd /etc/yum.repos.d/ [root@localhost yum.repos.d]# cp CentOS-*.* /home/centos ``` > 強救 AppStream,新機器不用做 ``` cd /etc/yum.repos.d/ rm -f CentOS-*.* cd /home/centos cp CentOS-*.* /etc/yum.repos.d/ cd /etc/yum.repos.d/ ``` > 20220517 強救 AppStream,新機器不用做 ``` cd /etc/yum.repos.d/ rm -f CentOS-*.* cd /home/centos wget http://ms.ntub.edu.tw/~shyong/Linux/repos.tar.gz tar zxvf repos.tar.gz cd repos cp CentOS-*.* /etc/yum.repos.d/ cd /etc/yum.repos.d/ ``` ``` [root@localhost yum.repos.d]# ls -l total 52 -rw-r--r--. 1 root root 734 Mar 16 01:01 CentOS-AppStream.repo -rw-r--r--. 1 root root 715 Mar 16 01:01 CentOS-Base.repo -rw-r--r--. 1 root root 801 Mar 16 01:01 CentOS-centosplus.repo -rw-r--r--. 1 root root 1045 Mar 16 01:01 CentOS-CR.repo -rw-r--r--. 1 root root 668 Mar 16 01:01 CentOS-Debuginfo.repo -rw-r--r--. 1 root root 746 Mar 16 01:01 CentOS-Devel.repo -rw-r--r--. 1 root root 759 Mar 16 01:01 CentOS-Extras.repo -rw-r--r--. 1 root root 340 Mar 16 01:01 CentOS-fasttrack.repo -rw-r--r--. 1 root root 741 Mar 16 01:01 CentOS-HA.repo -rw-r--r--. 1 root root 928 Mar 16 01:01 CentOS-Media.repo -rw-r--r--. 1 root root 739 Mar 16 01:01 CentOS-PowerTools.repo -rw-r--r--. 1 root root 1383 Mar 16 01:01 CentOS-Sources.repo -rw-r--r--. 1 root root 74 Mar 16 01:01 CentOS-Vault.repo ``` 只能做一次,不能重複做,很重要 ``` [root@autocontroller ~]# sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-* ``` ``` CentOS-AppStream.repo [AppStream] name=CentOS-$releasever - AppStream #mirrorlist=http://#mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=AppStream&infra=$infra #baseurl=http://mirror.centos.org/$contentdir/$releasever/AppStream/$basearch/os/ gpgcheck=1 enabled=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial ``` 只能做一次,不能重複做,很重要 ``` [root@autocontroller ~]# sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-* ``` ``` [AppStream] name=CentOS-$releasever - AppStream #mirrorlist=http://#mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=AppStream&infra=$infra baseurl=http://vault.centos.org/$contentdir/$releasever/AppStream/$basearch/os/ gpgcheck=1 enabled=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial ``` 檢查AppStream ``` sudo dnf install telnet ``` 千萬先不要做這 ``` [root@autocontroller ~]# yum update -y ``` ```shell= [root@localhost ~]# dnf install telnet [root@localhost ~]# telnet ms.ntub.edu.tw 110 [root@localhost ~]# telnet ms.ntub.edu.tw 22 [root@localhost ~]# telnet ms.ntub.edu.tw 80 [root@localhost ~]# telnet www.ntub.edu.tw 110 #curl ifconfig.me [root@localhost ~]# telnet www.ntub.edu.tw 53 Trying 140.131.110.3... ^C [root@localhost ~]# telnet www.ntub.edu.tw 80 Trying 140.131.110.3... Connected to www.ntub.edu.tw. Escape character is '^]'. ^] ``` 20220301上課錄影 [https://youtu.be/refmHNtBOl8](https://youtu.be/refmHNtBOl8) ## Linux 關機 - 在文字模式下關機,指令動作: |關機 | 重新啟動系統 | | -------- | -------- | | shutdown –h now | shutdown –r now | - 當系統管理員下達shutdown指令後,其它使用telnet、ssh登入到本機的使用者都會收到訊息告知系統馬上要關機。 ## 測試網路的接通 - 安裝完Linux之後,我們可以立即來測試網路的連結是否正常,最常用的指令有: ```shell= ip a #(ifconfig) # 這個指令是用來檢查網路卡的狀態。 netstat -ntp #(ss -ntp) ping fb.me # ping會透過網路將ICMP回應請求,送到目的主機。這也是檢查網路最常用的方式。 ``` ### 網路管理 ``` $ ip addr $ ip route ``` ```shell= # nmtui ``` ## 使用輔助說明 - 學習Linux最好的方式就是善用它的輔助說明: - man 指令名稱 ## 幫助輸入『上、下』『tab』 - 在Linux系統中常常會需要輸入指令,以下二個動作可以幫助我們: - 『上、下』的方向鍵可以隨時找到之前所輸入的指令,可減少重新key-in指令的動作。 - 善加使用『tab』鍵可以更省力,『tab』鍵可以找出檔案中最符合的檔案或目錄。 例如我們輸入『X』再按二次『tab』鍵就可以列出檔案中是『X』開頭的檔案,要按二次的因素是在目前目錄中找不到相符合的檔案或目錄、或者是有二個以上符合的檔案或目錄。如果只按一次『tab』就出現的話,表示這是唯一符合條件的檔案或目錄。 - 在 Linux系統下的英文大小寫是不一樣, 因此我們在輸入指令和參數時,要注意英文大小的問題, 注意看英文的大小寫輸入是否正確! # 檔案與目錄系統 ## 檔案系統相關指令 - pwd:顯示目前的目錄路徑(Print Working Directory)。 - df:顯示檔案系統(硬碟空間)使用的狀況。 選項 說明 - -k --kilobytes 以KB的容量顯示 - -m --megabytes 以MB的容量顯示 - df -h - du:顯示某個目錄中的檔案所使用的容量。 - du -h - du -sh /var/* - du -sh /home/* ```shell= [centos@localhost ~]$ du -sh /home/* 0 /home/201410H.txt 0 /home/201410S.txt 117M /home/centos du: cannot read directory '/home/ntcb1501': Permission denied 0 /home/ntcb1501 0 /home/ntcb1504 du: cannot read directory '/home/ntcb1505': Permission denied 0 /home/ntcb1505 du: cannot read directory '/home/ntubimd': Permission denied 0 /home/ntubimd du: cannot read directory '/home/ntubimd001': Permission denied 0 /home/ntubimd001 ``` <!-- - dmesg:顯示開機過程中的系統記錄。 --> ## mount - 在Linux系統中所使用到的檔案系統都必須經由『掛載』的動作,將該項裝置掛載成為檔案系統中的某一個目錄。 - 我們在安裝Linux系統時,有做過硬碟分割的動作,其中除了指令分割的容量大小之外,還特別有指定『mount point』的動作,這個就是將硬碟分割區掛載到檔案系統的某一個目錄。 ``` mount [-fnrsvw] [-t vfstype] [-o options] device dir mount /dev/hdd /media/cdrom #將光碟機mount到/media/cdrom ``` ## 目錄結構 - 在Linux檔案系統中,和Windows不一樣的地方就是Linux沒有『C:、D:』,而是將所有的『目錄』『設備裝置』都為檔案系統中的其中一個目錄。 - 底下簡單列出一些較常使用的目錄結構: ``` / ‘系統的根目錄 ├─bin ├─boot ‘開機時執行的啟動程式所存放的目錄 ├─etc ‘一般常用到的伺服器、設定檔案所存放的目錄 ├─home ‘一般使用者的目錄都放在這底下 │ └─centos ‘一般使用者centos的個人目錄 ├─root ‘系統管理員root的個人目錄 ├─sbin ├─var │ ├─named ‘名稱伺服器設定檔放存的目錄 │ ├─www │ │ └─html ‘存放網頁的目錄 │ ├─ftp │ ├─lib─mysql ‘mysql資料匣 │ └─spool │   ├─mail ‘收到的郵件都放在這個目錄 │   └─mqueue ‘待寄郵件都會放在這個目錄 └─tmp ‘暫存檔案會放在這個目錄 ``` - 所有的目錄都可以使用『絕對路徑』的方式表示,參考如下: |目錄 | 完整路徑 | | -------- | -------- | |etc | /etc | | home | /home | | centos| /home/centos| |mail |/var/spool/mail| |html |/var/www/html| - 在Linux系統中最上層的目錄稱為root、根目錄,以『/』表示,它是所有目錄的最上層,另外有一個目錄是『/root』,是系統管理員root的個人目錄,不要和系統的根目錄『/』混淆。 - 每一個使用者都有自己的個人目錄,有時候會說成『家目錄』(或者是『home目錄』),指的就是自己個人專屬的目錄。和『/home』 目錄是不一樣的。 - 每個目錄中一定會有二個檔案,『.』為目前所在的目錄,『\.\.』是上一層目錄。 ## 檔案目錄相關指令 - ls:顯示目前工作目錄的內容。 |選項 | 說明 | | -------- | -------- | | -a | 列出所有檔案 | | -n | 列出檔案,以UID和 GID顯示 | | -l (小寫的L) | 以較長的格式顯示,包含存取權限、連結數目、擁有者、大小、最後更動時間等 | | -i | 顯示各檔案的inode | - cd:切換目前工作的目錄,可以使用相對路徑或絕對路徑。 |選項 | 說明 | | -------- | -------- | | cd /etc | 移到/etc的目錄 | | cd /var/www/html | 移到/var/www/html的目錄 | | cd 或 cd ~ | 回到使用者家目錄/home/ | | cd - | 回到目前所在目錄的前一個目錄 (二個資料匣的切換) | | cd \.\. | 切換目錄到上一層目錄 | |目錄名稱 | 目錄意義 | | -------- | -------- | |/ | 根目錄,Linux系統中最上層的目錄稱 | |~ | 使用者的家目錄,不同用戶的家目錄均不相同 | |. | 一個小數點,代表的是『本目錄』『目前所在的目錄』,亦即目前的工作目錄之意 | |\.\. | 兩個小數點,代表的是『上一層目錄』 | |-| 一個減號,代表『上一次的工作目錄』之意 | # How To Install the Apache Web Server on CentOS 8 ``` cd /etc/yum.repos.d/ [student@localhost yum.repos.d]$ sudo vi Rocky-AppStream.repo ``` ``` #baseurl=http://free.nchc.org.tw/rocky/8/AppStream/x86_64/os/ baseurl=http://dl.rockylinux.org/pub/rocky/8/AppStream/x86_64/os/ ``` ``` [student@localhost yum.repos.d]$ sudo vi Rocky-BaseOS.repo ``` ``` #baseurl=http://free.nchc.org.tw/rocky/8/BaseOS/x86_64/os/ baseurl=http://dl.rockylinux.org/pub/rocky/8/BaseOS/x86_64/os/ ``` ```shell= # Step 1 — Installing Apache sudo dnf install httpd sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=https sudo firewall-cmd --reload #Step 2 — Checking your Web Server sudo systemctl start httpd sudo systemctl status httpd sudo systemctl enable httpd #Step 3 in Firefox(windows chrome) #http://localhost #檢查點:確認 http服務 和 firewall設定 http://192.168.x.52 #Step 4 PHP7.3 對於 EPEL8 以及 CentOS 8 預設的 repository 來說是更新的套件,所以需要再安裝包含新版 PHP7.3 的 repository ,下列步驟是以 Remi-php7.3 作示範。 #sudo dnf install http://rpms.remirepo.net/enterprise/remi-release-8.rpm #下列指令中的 php73-php 就是 PHP 7.3 主程式,後面則是順手安裝一些常用套件。 #sudo dnf install php73-php php73-php-gd php73-php-mbstring php73-php-mysqlnd sudo dnf install php php-gd php-mbstring php-mysqlnd #比較特別的是 PHP-FPM 這個程式可以控制諸多與 PHP 相關的參數設定,這會影響到伺服器的記憶體使用量與效能等重要因素,PHP-FPM 是個很重要的東西。如果改了 php.ini 的設定,強烈建議重啟 php-fpm 服務比較保險。 #sudo systemctl restart php73-php-fpm sudo systemctl restart php-fpm sudo systemctl restart httpd ``` 防火牆 開放 http 服務 永久性生效 firewall-cmd --add-service=http --permanent 防火牆不中斷服務 重新載入 firewall-cmd --reload ```shell= [centos@localhost ~]# sudo dnf install vim ``` ```shell= [centos@localhost ~]$ cd /var/www/html/ [centos@localhost html]$ sudo vim test.php ``` ``` <?php phpinfo(); ?> ``` ```shell= [centos@localhost html]$ cat test.php ``` 進入 vim 進入編輯模式 ```shell= i ``` 在 vim 存檔 ```shell= esc --> :wq! ``` ``` i vim 進入編輯模式 esc 進入命令模式 :w 存檔 :q 離開 ! 強制離開 ## esc --> :wq! esc --> :q! ## ``` ``` [centos@localhost html]$ chcon -t httpd_sys_content_t test.php ``` {%youtube PvWNXNDPs20 %} + 檢查點:驗証 PHP 套件是否正常,目前進度只到可以看到系統網頁的內容(個人使用者的網頁內容尚未完成) ```shell= #Step in Centos Firefox http://192.168.x.52/test.php ``` Listen 80-->8080,因為之後安裝的wordpress要用到 80 port。 ``` sudo vim /etc/httpd/conf/httpd.conf 45 Listen 8080 sudo systemctl restart httpd ``` ★★★ 加入自行指定的連接埠 ★★★ ``` sudo firewall-cmd --add-port=8080/tcp --permanent #success sudo firewall-cmd --reload #success sudo firewall-cmd --list-all ``` + 檢查點:測試8080是否正常運作 ``` #Step in Windows chrome http://192.168.x.52:8080/test.php ``` 設定個人使用者的網頁內容放置資料匣為"/home/*/www" 在vim中顯示行號 :set nu ```shell= cd /etc/httpd/conf.d sudo vim userdir.conf 17 #UserDir disabled (加#) 24 UserDir www (去# public_html > www ) 31 <Directory "/home/*/www"> (public_html > www ) ## esc --> :wq esc --> :q! ## sudo systemctl restart httpd #sudo setenforce 0 ``` >在vim中顯示行號 :set nu ![](https://hackmd.io/_uploads/S1cLg6L-T.png) 切換 SELinux 模式,關閉安全性 ``` # 0: permissive 宽容模式 sudo setenforce 0 # 1: enforcing 强制模式 sudo setenforce 1 ``` ```shell= [centos@localhost www]$ sudo getenforce Enforcing [centos@localhost www]$ ``` 設定個人使用者的網頁資料匣"/home/*/www"目錄權限開放和http selinux設定 ```shell= cd /home sudo vim userwww.sh sudo chmod 700 userwww.sh sudo sh userwww.sh ``` userwww.sh內容 ```shell= #!/bin/bash for i in `ls /home` do test -d /home/$i || continue chmod 755 /home/$i mkdir -p /home/$i/www mygroup=$(groups $i | awk '{print $3}') chown -R $i:$mygroup /home/$i chcon -R -t httpd_sys_content_t /home/$i/www chcon -t user_home_dir_t /home/$i done ``` * chcon: The command used to change the security context. * -t httpd_sys_content_t: Set the type component of the security context to httpd_sys_content_t, which is a type commonly used for web content accessible by the Apache web server. * -t user_home_dir_t: Set the type component of the security context to user_home_dir_t, which is a type commonly used for user home directories. * semanage $ mkdir -p {middle_dir}/{bottom_dir} # 中間資料夾不存在時自動建立 ```shell= [centos@localhost home]$ cd /home/student/www/ [centos@localhost www]$ pwd /home/student/www [centos@localhost www]$ vim test.php [centos@localhost www]$ cat test.php <?php phpinfo(); ?> ``` {%youtube N9V87c5JoZM %} ```shell= [centos@localhost home]$ cd /home/student/www/ [centos@localhost www]$ pwd /home/student/www [centos@localhost www]$ vim 11146000.html [centos@localhost www]$ cat 11146000.html 11146000 ``` + 檢查點:個人使用者的網頁內容是否正常運作 0418驗收, 2023/04/18 驗收 http://192.168.XXX.52/~student/test.php http://192.168.XXX.52/~student/11146000.html #https://www.digitalocean.com/community/tutorials/how-to-install-the-apache-web-server-on-centos-8 #https://www.brilliantcode.net/1955/centos-8-apache24-php73-installation-settings/ - mkdir:建立目錄 |選項 | 說明 | | -------- | -------- | | mkdir abc | 在此目錄下建立abc的子目錄 | | mkdir -p aaa/bbb | 在aaa底下建立一個bbb的目錄,若aaa原本不存在,則建立一個aaa目錄 | ### 上機操作 ```shell= sudo su - mkdir -p /var/www/html/0303 ls -la /home ls -la /home > /var/www/html/0303/index.html cd /var/www/html/0303 ls -la vim index.html # 要注意Windows Linux MacOS的換行都不一樣, 網頁也不一樣, 在index.html <br> # http://localhost/0303/index.html mkdir -p /var/www/html/0303p/10846000 # http://localhost/0303/iondex.html ``` {%youtube 4Tm_7vTPOWA %} mkdir -p and cp https://youtu.be/4Tm_7vTPOWA - cp:將一個或數個檔案複製到另一個目錄。 |選項 | 說明 | | -------- | -------- | | -R 或 -r | 將目錄中的檔案及子目錄複製到目的地 | | -f | 若目的地有相同名稱的檔案存在,會直接覆蓋而不會再出現訊息警告我們 | |範例 | 說明 | | -------- | -------- | | cp -rf /var/www/html public_html | 將/var/www/html目錄底下的所有檔案全部複製到目前目錄下的public_html目錄中,若有同檔名存在則直接刪除後複製 | ### 0425上機操作 cp -rf /var/www/html/0303 /home/centos/www/ 練習 ```shell= sudo su - useradd centos passwd centos cd /home sudo vim userwww.sh sudo chmod 700 userwww.sh sudo sh userwww.sh cp -rf /var/www/html/0303 /home/centos/www/ #sudo sh userwww.sh cd /home/centos/www/0303 vim index.html # 1,$s/$/<br>/g # http://192.168.X.52/~centos/0303/ ``` - rm:刪除檔案或目錄。(rmdir只能刪除空的目錄) |選項 | 說明 | | -------- | -------- | | -i | 刪除前逐一詢問確認 | | -f | 直接刪除,不需確認 | | -r | 將目錄及子目錄中的檔案一併刪除 | - mv:將檔案移至另一目錄中,或是移至另一檔案(也就是重新命名)。 |選項 | 說明 | | -------- | -------- | | -i | 若有同名檔案,先詢問是否覆蓋 | | -f | 若目的地有相同名稱的檔案存在,會直接覆蓋而不會再出現訊息警告我們 | |範例 | 說明 | | -------- | -------- | | mv index.htm index.html | 將index.htm更名為index.html | ## 顯示檔案內容相關指令 - cat:把由FILE(s)或標準輸入讀取的資料連結起來,輸出到標準輸出。最簡單的用法就是在『cat』後加上檔案名稱直接顯示檔案內容。 - more:顯示文字檔案內容,以一頁一頁的方式逐頁顯示。 - less:和more的作用類似,但less的功能更強,除了可以往回捲動,還可以對內容搜尋關鍵字。對於大型檔案的開啟,速度很快。 - head - tail -n 或 -f - touch:產生空檔和時間標記 ```shell= [root@localhost /]# cat /etc/passwd [root@localhost /]# cat /etc/passwd | more [root@localhost /]# cat /etc/passwd | less [root@localhost /]# head -n 5 /etc/passwd [root@localhost /]# tail -n 5 /etc/passwd cd /home ls -l sudo touch userwww.sh ls -l sudo touch userwww.sh2 ls -l sudo cp userwww.sh userwww.sh.tmp ls -l sudo cp userwww.sh2 userwww.sh.tmp ``` {%youtube bc6rKKZ6lK4 %} ## 記錄系統資訊的目錄 - 實際上『/proc』並不是一個真正的目錄,而是當系統啟動之後,存放在記憶體的資料,透過目錄及檔案的型式呈現出來,因此,這個目錄中的資料都會在重新啟動系統後,又更新為新的資料。 - /proc/cpuinfo - /proc/meminfo - /proc/version (二)系統使用量(查詢時間:112 年 2 月 22 日) 1. 硬碟:36GB / 46GB : df -h 2. RAM: free -h 3. ## inode - 每一個檔案都會擁有一個數字,稱為inode,在inode裡存放檔案的所有資訊,包括檔案型態及所處的位址等。而所有的inode都存放在inode table中,至於inode table是在磁碟格式化時就已經配置好的,也就是說,每個分割區會有各自獨立的inode,每一個inode在每一個分割區之中都是唯一的。 - 我們可以使用『df –i』或『df –ih』查閱這些資訊。 ## 連結 - 使用ln指令可以將檔案產生連結,連結分成二種:符號連結、硬式連結。 - 不論是符號連結或硬式連結,它的主要作用都是將一個檔案分給很多人使用,而且檔案內容都是同一份的檔案內容。 - 建立連結的方式如下: ln [-s] 來源 目的 ### 符號連結 - 建立符號連結的方式為『ln -s 來源 目的』 - 符號連結可以針對檔案、目錄,也可以跨越裝置的界限。 - 符號連結的意義就類似windows系統中的捷徑,透過符號連結所建立的檔案是指向真正的檔案位址。 - 若直接刪除原來檔案,則符號連結的檔案會以閃礫來提示已經發生錯誤。 ### 硬式連結 - 建立硬式連結的方式為『ln 來源 目的』 - 硬式連結的意義有些像『分身』。不論增加幾個硬式連結,其inode都指向同一個,所以在硬碟中都只佔一個檔案的大小,當所有的連結都刪除時,檔案才真正的被刪除。 - 硬式連結::不能::使用在目錄,而且,由於硬式連結是指向同一個inode,所以也**不能跨越不同的磁碟分割區**。 ## 上機操作: ```shell= cd /home touch 201410.txt ls -li ln -s 201410.txt 201410S.txt ls -li ln 201410.txt 201410H.txt #硬式連結是指向同一個inode ls -li ``` ## 尋找檔案相關指令 - find:搜尋檔案 |選項 | 說明 | | -------- | -------- | | -name file | 搜尋檔案名稱;可以是檔名或含有萬用字元的檔名 | | -iname file | 同上,但會忽略英文大小寫 | | -cmin n | 在過去 n 分鐘內被修改過的檔案 | | -ctime n | 在過去 n 天內被修改過的檔案 | | -cnewer | | |範例 | 說明 | | -------- | -------- | | find / -name index.html | 自根目錄起,搜尋檔案index.html | |find . -cmin -20 | 自目前所在目錄,找出最近20分鐘內有更新過的檔案 | | find . -size +5M | 自目前所在目錄,找出檔案大小超過5M的檔案 | – find /home/ -size +5M – find /root -ctime -12 – find /root -cnewer /root/timebase1 <!-- - locate:搜尋檔案;由於是先建立檔案的資料庫,因此,在第二次之後使用時的速度會比較快。 由於『locate』指令是由資料庫中取出符合的資料內容,因此,有可能找出的路徑、檔名是過時或不正確的,這時候就必須更新資料庫中的內容。 --> # 檔案的存取權限 存取權限 - 使用長格式來顯示目前的檔案內容,可以看到第三、四個欄位都是顯示『root』,第三個欄位代表的是『檔案的擁有者』,第四個欄位代表的是『檔案的群組擁有者』。 – -rw-r--r-- 1 root root 2898 10月 2 11:20 index.html - 最左邊一欄的十個字元,除了第一個字元代表的是檔案的型態之外,後面的九個字元分為三組,每一組都是三個字元,分別代表是否允許讀取、寫入、執行該檔案的權限。 | - | rwx | rwx | rwx | | -------- | -------- | -------- | -------- | | 型態 | 擁有者權限 |群組權限 | 其它使用者權限 | ## 檔案型態 - 檔案型態有以下幾種,較常見的是前三項: |型態 | 說明 | | -------- | -------- | | d | 目錄 | | l | 符號連結的檔案 | | b | 區塊設備;Block-special file | | c |週邊設備;Character-special file | ## 檔案的存取權限 - 易記方式 檔案存取權限的『rwx rwx rwx』可以視為『421 421 421』三組數字的組合,我們只需將對應的數字加總,例如: | 存取權限 | 擁有者權限 | 群組權限 | 擁有者權限 | 存取權限 | | -------- | -------- | -------- | -------- | -------- | | rwxr-xr-x | 4+2+1 |4+0+1 | 4+0+1 |755 | - 檔案可不可以執行,是由檔案是否開啟『執行』的權限所決定的,只要這個檔案的執行權限有設定,那這個檔案就可以執行。 - 二進位 其實檔案的存取權限是對照二進位的0或1來決定的,0則表示不提供該權限,1則表示具有該權限。 - 無論是『擁有者』、『群組』、『其它使用者』的讀取、寫入、執行權限設定,其對照表格如下: | 讀取 寫入 執行 | 表示為 | 二進位值 | | -------- | -------- | -------- | | 0 0 0 |- - - - | 0 | | 0 0 1 |- - - x | 1 | | 0 1 0 |- - w - | 2 | | 0 1 1 |- - w x | 3 | | 1 0 0 |- r - - | 4 | | 1 0 1 |- r - x | 5 | | 1 1 0 |- r w - | 6 | | 1 1 1 |- r w x | 7 | ### 三組權限 | 縮寫 | 全名 | 說明 | | :---: | :---: | :---: | | u | user | 所有者 | | g | group | 群組 | | o | others | 其他人 | ### 數字加總 | 縮寫 | 全名 | 數字 | 說明 | | :---: | :---: | :---: | :---: | | r | read | 4 | 讀取權限 | | w | write | 2 | 寫入權限 | | x | execute | 1 | 執行權限 | ```shell $ chmod u=rwx, g=rx, o=r test.txt $ chmod 754 test.txt # user: 7 = 4(讀取) + 2(寫入) + 1 (執行) # group: 5 = 4(讀取) + 1(執行) # others: 4 = 4(寫入) ``` ## 改變檔案權限及擁有人相關指令 - chmod:設定使用者對檔案使用的權限控制。 |範例 | 說明 | | -------- | -------- | | chmod 755 index.htm | 設定index.htm的存取權限為755 | - chown:更改檔案的擁有者及群組。 |範例 | 說明 | | -------- | -------- | | chown -R centos:root /home/centos | 將目錄中的所有檔案擁有者改為centos,而群組改為root群組 | - 一般來說,這個指令只有是由系統管理者(root)所使用,一般使用者沒有權限可以改變別人的檔案擁有者,也沒有權限可以將自己的檔案改設為別人。只有系統管理者(root)才有這樣的權限。 ### 上機操作: ``` cd /home/centos/ ls -l touch 0310f ls -l chown centos:root 0310f #-rwxr-xr-x. 1 root root 0 Mar 10 18:48 0310f #-rwxr-xr-x. 1 centos root 0 Mar 10 18:48 0310f ls -l chmod 755 0310f #-rwxr-xr-x. 1 centos root 0 Mar 10 18:48 0310f [root@localhost centos]# chmod 754 0310f [root@localhost centos]# ls -l #-rwxr-xr--. ``` ## 目錄和帳號權限 上機操作: + 新增使用者ntub2021, + sudo useradd ntub2021 + 再由 centos身分切換到ntub2021身分 + [centos@localhost ~]$ su - ntub2021 + ntub2021的身分看不到 centos的檔案 + [ntub2021@localhost ~]$ du -sh /home/* + du: cannot read directory '/home/centos/.mozilla/firefox': Permission denied ```shell= [centos@localhost home]$ sudo useradd ntub2021 [sudo] password for centos: [centos@localhost home]$ sudo passwd ntub2021 Changing password for user ntub2021. [centos@localhost ~]$ su - ntub2021 Password: [ntub2021@localhost ~]$ du -sh /home/* 0 /home/201410H3.txt 0 /home/201410S.txt du: cannot read directory '/home/centos/.mozilla/firefox': Permission denied du: cannot read directory '/home/centos/.mozilla/systemextensionsdev': Permission denied du: cannot read directory '/home/centos/.config': Permission denied 60K /home/centos 0 /home/ntcb1501 0 /home/ntcb1502 ``` + ntub2021的身分看不到 centos的檔案,用chmod開放centos目錄權限 ```shell= [centos@localhost home]$ sudo chmod 755 /home/* -R ``` ```shell= [centos@localhost ntcb1501]$ cd /home/centos/ [centos@localhost ~]$ ls -l drwxr-xr-x. 5 root root 43 Mar 10 18:20 public_html [centos@localhost ~]$ sudo chown centos:centos public_html/ -R [centos@localhost ntcb1501]$ cd /home/centos/ [centos@localhost ~]$ ls -l drwxr-xr-x. 5 centos centos 43 Mar 10 18:20 public_html ``` + /var/的目錄有些系統資訊,只有管理者才能讀取 ```shell= [centos@localhost html]$ du -sh /home/* 0 /home/03102 0 /home/0310s 0 /home/201410H.txt 0 /home/201410S.txt 123M /home/centos 4.0K /home/ntcb1504 4.0K /home/ntcb1505 12K /home/ntubimd 52K /home/ntubimd001 [centos@localhost home]$ sudo du -sh /var/* ``` ``` [centos@localhost home]$ pwd /home [centos@localhost home]$ sudo chmod 750 ntub2021 -R drwx------. 4 ntub03 ntub03 92 Mar 24 16:02 ntub03 drwxr-x---. 4 ntub2021 ntub2021 92 Mar 24 14:10 ntub2021 ``` ## 其它相關指令 - cmp:比對二個檔案之間的差異。 - diff:比對二個檔案之間的差異,diff與cmp之間的差異在於cmp是逐字比對,而diff是逐列比較,因此,cmp比較適合用於二進位檔案的比較。 - file:顯示檔案類型。 ## 其它常用指令 ### 其它指令 - date:顯示目前日期時間,亦可更新日期時間。 - 另外,clock指令也可以顯示目前的日期時間,clock指令只有root身份才能使用。 - 修改日期的格式是『月 日 時 分 年 . 秒』,和我們平常的系統不太相同,要特別注意。 - 若要修改系統的日期時間,使用date及clock二個指令互相配合。 ``` [root@linux /root]# date ‘顯示目前日期時間 Sat Jul 7 13:09:35 CST 2010 [root@linux /root]# date 031013062021.22 ‘修改日期時間 Wed Mar 10 13:06:22 CST 2021 [root@linux /root]# clock –w ‘將修改的日期時間寫入至BIOS中 [root@linux /root]# ntpdate time.stdtime.gov.tw [root@localhost centos]# ntpdate time.stdtime.gov.tw bash: ntpdate: command not found... [root@localhost centos]# date Wed Mar 10 19:01:33 CST 2021 [root@localhost centos]# systemctl start chronyd.service [root@localhost centos]# systemctl enable chronyd [root@localhost centos]# date Wed Mar 10 11:10:09 CST 2021 ``` ## CentOS8 網路校時 - chrony -1. 安裝 chrony ```shell= $ sudo dnf install chrony ``` -2. 修改 /etc/chrony.conf ```shell= $ sudo vi /etc/chrony.conf ``` ```shell= :set nu 到最後一行再按 o ``` set nu顯示行號 然後增加下列幾行 Stdtime NTP servers ```shell= server tock.stdtime.gov.tw server watch.stdtime.gov.tw server time.stdtime.gov.tw server clock.stdtime.gov.tw server tick.stdtime.gov.tw ``` -3. 啟動 chronyd - 請注意這邊是 chronyd 唷! 因為是 daemon ```shell= $ sudo systemctl start chronyd $ sudo systemctl enable chronyd ``` Set timezone. ``` sudo timedatectl | grep Taipei #Time zone: Asia/Taipei (CST, +0800) sudo timedatectl set-timezone Asia/Taipei ``` #In Linux systems, the Network Time Protocol (NTP) protocol is implemented by a daemon running in user space. #https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html-single/configuring_basic_system_settings/index#differences-between-ntp-and-chrony_using-chrony-to-configure-ntp ## CentOS 8 特色功能 目前,最新版本為 CentOS 8.1 (1911),並且與舊版 CentOS 7.x 有很大的不同,舉例來說,套件庫主要有二個「BaseOS 和 AppStream」、預設檔案系統採用「XFS」、套件管理工具由過去的 YUM 升級為「dnf」、時間校時工具僅採用「Chrony」、網路組態僅採用「NetworkManager」、防火牆 Firewalld 底層由 iptables 改為「nftables」……等,詳細資訊請參考 [Considerations in adopting RHEL 8 Red Hat Enterprise Linux 8 | Red Hat Customer Portal](https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html-single/considerations_in_adopting_rhel_8/index) ### 使用者 - whoami:告知目前正在操作系統的使用者身份。 ``` [root@Linux root]# whoami ``` - who:顯示目前登入到系統的使用者、登入的日期。 - 底下的例子是我直接在Linux的電腦中登入的,所以第二個欄位處會顯示『tty1』、『tty2』…來代表是使用那一個虛擬終端機(也就是用Ctrl + Alt + F1或Ctrl + Alt + F2…切換的畫面)登入的: ```shell= [root@Linux root]# who root tty1 Apr 27 17:05 ntubim tty2 Apr 27 17:06 ntubim pts/0 Apr 27 16:58 ``` ## 上機操作: ``` [root@localhost Desktop]# ps aux | grep vim [root@localhost Desktop]# ps aux | grep firefox [root@localhost Desktop]# kill -9 6019 [root@localhost Desktop]# top ``` Other User: ```shell= vi test.txt ``` Root: ```shell= ps aux | grep centos sudo kill -9 PID ``` Linux顯示系統執行中的程序,最常用的有ps -ef 和ps aux。Unix系統中的兩種風格,System V風格和BSD 風格,而ps -ef被用在System V Style中,ps aux最初用到Unix Style(BSD)中。 原文網址:https://kknews.cc/code/qnnpkmg.html ### How to configure a static IP address on RHEL 8 / CentOS 8 Linux https://linuxconfig.org/rhel-8-configure-static-ip-address ### RHEL / CentOS 8 設定網路固定 IP https://www.opencli.com/linux/rhel-centos8-setup-static-ip ### How to Configure Network Settings on CentOS 7 https://phoenixnap.com/kb/configure-centos-network-settings https://blog.gtwang.org/linux/centos-linux-static-network-configuration-tutorial/ ### 取得登入資訊 /var/log/wtmp ```shell= $ last ``` ### 系統版本 ```shell= /etc/system-release ``` ### 輸出重導 + 正常輸出 ls -l 1> ok.txt 通常省略1 ls -l > ok.txt + 錯誤輸出 ls -l 2> err.txt 儲存輸入跟輸出 兩個檔案 ls -l xxx.txt 1> ok.txt 2> err.txt 同個檔案 ls -l xxx.txt > output.txt 2>&1 > 建立 or 清空檔案 ### [Server Wiki](https://learnku.com/server/wikis) ### [dywang (呆王)](https://dywang.csie.cyut.edu.tw/) ### [mirrorlist](https://mirrors.rockylinux.org/mirrorlist?arch=x86_64&repo=AppStream-8) > Rocky Linux yum/dnf Repo 镜像 ### [[研究]CentOS, Rock Linux 的 yum, dnf 連去 Internet 的哪裡](http://shaurong.blogspot.com/2022/11/centos-rock-linux-yum-dnf-internet.html) ### [如何使用 CCNS 鏡像站服務?][(https://](https://mirror.ccns.ncku.edu.tw/howto/))