# Linux 常用指令 :::success [toc] ::: ## User ### 生成對鑰匙 ``` ssh-keygen -t rsa -b 4096 -C "your_email@example.com" ``` ### 查看所有使用者帳號 執行指令 ```bash #印出passwd內容 cat /etc/passwd ``` 得到所有使用者 ```bash root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin bin:x:2:2:bin:/bin:/usr/sbin/nologin sys:x:3:3:sys:/dev:/usr/sbin/nologin sync:x:4:65534:sync:/bin:/bin/sync games:x:5:60:games:/usr/games:/usr/sbin/nologin man:x:6:12:man:/var/cache/man:/usr/sbin/nologin #... ``` ### 更改密碼 1. 假設你要更改`phpini`這個使用者的密碼 ```bash passwd phpini ``` 2. 要求你輸入新密碼 ```bash New password: Retype new password: passwd: password updated successfully ``` 3. 這樣就修改完成了 ## 修改資料夾權限 當前資料夾權限全開 ``` sudo chmod 777 . ``` https://weikaiwei.com/linux/chmod-commands/ ## 使用檔案管理員(GUI)開啟路徑 > gnome-open 要開啟的路徑 > 第一次使用時要先執行下面命令 > sudo apt install libgnome2-bin > 新增使用者 `sudo adduser userName` 刪除使用者 `sudo userdel userName` [參考](https://www.cyberciti.biz/faq/create-a-user-account-on-ubuntu-linux/) webmin https://www.tecmint.com/install-webmin-on-ubuntu/ ## 使用root登入 ``` sudo -s ``` ## 檢查port有無開啟 ``` nc -v 192.168.0.175 5000 ``` ## GCP ### 沒有root 但又一定要root身分操作? 1. 給root一個密碼 `sudo passwd` 2. 登入root `su root` 3. Done [參考](https://stackoverflow.com/questions/35016795/get-root-password-for-google-cloud-engine-vm) ## PHP ### PHP的版本切換 (apache) > 舉例從7.2 到 7.4 關閉7.2 ``` sudo a2enmod php7.2 ``` 啟動7.4 ``` sudo a2enmod php7.4 ``` 重新啟動apache ``` sudo systemctl restart apache2 ``` ## Linux ### 背景執行程式 #### 在背景執行程式並將結果log起來 ```bash nohup java -jar /web/server.jar > log.log 2>&1 & ``` [參考](https://stackoverflow.com/questions/12102270/run-java-jar-file-on-a-server-as-background-process) #### 確認背景程式進程 ```bash jobs ``` ![](https://hackmd.io/_uploads/HySdSfTLY.png) #### 刪除進程 若想要刪除第二個job ```bash kill %2 ``` 簡單ㄅ~ ### 檢查ubuntu版本 ```shell lsb_release -a ``` ### 重啟 ```shell reboot ``` ### 查看當前連線 ```shell sudo apt install iftop sudo iftop ``` ![](https://i.imgur.com/3VfTSFm.png) ### 查看當前使用中程序 ```output watch -n 0.5 sudo ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head ``` ![](https://i.imgur.com/vkER3Yh.png) >參數說明 >* `-n x` 每x秒更新一次 >* `-d` 高亮與上次不同的地方 >![](https://i.imgur.com/kZdy1Rj.png) ### 查看當前主機數據 ```output htop ``` ```output nmon ``` ![](https://i.imgur.com/aX1JLuq.png) 常用鍵入指令 * ==c== CPU * ==m== MEMORY * ==d== Disks * ==n== NetWork ![](https://i.imgur.com/u58eMcZ.png) [詳細介紹](https://www.techrepublic.com/article/how-to-monitor-your-linux-servers-with-nmon/) ### 確認linux磁碟空間 #### ncdu 1. 安裝 ncdu ``` sudo apt install ncdu ``` 2. 使用ncdu很簡單。在命令行中,輸入: ``` ncdu ``` [參考](https://www.muo.com/tag/how-to-analyze-your-disk-usage-pattern-in-linux/) ## 壓縮 ### 壓縮資料夾 ``` tar -czvf 輸出的壓縮檔名稱.tar.gz 要被壓縮的資料夾名稱/ ``` 參數代表意義 - -c:存檔(解壓縮就用x,下面有範例) - -z:使用gzip壓縮 - -v:在創建存檔時在終端中顯示進度,不會加快速度。效果如圖 - -f:允許命名 -v效果(列出目前壓縮的檔案) ![](https://i.imgur.com/juvC5vc.png) ### 解壓縮資料夾 #### zip **解壓縮檔案及目錄例子:** 用 unzip 將 file.zip 壓縮檔內所有檔案及目錄解壓到當前目錄: ``` unzip file.zip ``` 假如只想解壓壓縮檔內其中一個檔案, 可以加上檔案名稱, 例如解壓 file.zip 內的 test.pdf 檔案: ``` unzip file.zip test.pdf ``` 將壓縮檔所有檔案解壓到指定目錄, 可以用 -d 參數設定, 以下假設解壓到 /home/phpini ``` unzip file.zip -d /home/phpini ``` 列出壓縮檔所有內容: ``` unzip -l file.zip ``` https://www.opencli.com/linux/linux-zip-unzip #### tar 解壓縮到當前位置 ``` tar -xzvf 要被壓縮的壓縮檔名稱.tar.gz  ``` 解壓縮到指定位置 ``` tar -xzvf 要被壓縮的壓縮檔名稱.tar.gz  -C ~/path/hereisgood/ ``` [參考](https://support.hostway.com/hc/en-us/articles/360000263544-How-to-compress-and-extract-files-using-tar-command-in-Linux?mobile_site=true) ## MYSQL ### 各種操作 You should really use the Sys-V init scripts located in `/etc/init.d`. **Start:** ``` sudo /etc/init.d/mysql start ``` **Stop:** ``` sudo /etc/init.d/mysql stop ``` **Restart / reload configs:** ``` sudo /etc/init.d/mysql restart ``` **Check run status:** ``` sudo /etc/init.d/mysql status ``` ### 登入 ```output mysql -uroot -p123456 ``` 以帳密(root 123456)為例 密碼為明碼 ### 顯示處理中的執行序 ```output SHOW PROCESSLIST ``` ### 刪除執行序 [參考](https://stackoverflow.com/questions/44192418/how-to-find-mysql-process-list-and-to-kill-those-processes/44192419) ```output kill <id> ``` ## Apache Server 依個人使用率排序 ### 查看 ```output sudo systemctl status apache2 ``` ### 啟動 ```output sudo systemctl start apache2 ``` ### 停止 ```output sudo systemctl stop apache2 ``` ### 重啟 ```shell sudo systemctl restart apache2 ``` ###### tags: `網站知識` `Linux`