Linux 小筆記 === ### 監控 磁碟 ``` df -h //可知道目前硬碟剩餘空間與使用空間 du -h //可知目前此資料夾下所有檔案與資料夾所佔硬碟大小總數 du -s //可知目前此資料夾總共佔用硬碟大小總數,以G為單位 du -sh //可知目前此資料夾總共佔用硬碟大小總數,以G為單位 ``` 記憶體 ``` free -m //顯示實體記憶體和 SWAP 的大小及使用狀況 ``` htop 監控台 ``` # 安裝 yum install htop -y # 使用 htop ``` --- 複製目錄 ``` cp -Rv /path/from /path/to ``` -R 遞迴, 包含 /path/from 底下的目錄及檔案 -v 覆蓋 --- repo清單 ``` ll /etc/yum.repos.d/ ``` --- #### 搜尋 查看過往指令, | grep 為 option ``` history | grep <關鍵字> ``` 用名稱來刪除執行緒 ``` #列出執行緒 pid ps aux | grep <process name> | grep -v grep | awk '{print $2}' | xargs #刪除 ps aux | grep <process name> | grep -v grep | awk '{print $2}' | xargs kill -9 #刪除2 ps -ef | grep <process name> | grep -v grep | awk -F" " 'system("kill "$2"")' ``` 搜尋關鍵字 > -r or -R is recursive > -n 顯示行號 > -w 匹配整個單詞 > -i 忽略大小寫 > -l 僅顯示文件名稱 ``` grep -rnw "something2search" /path/to/dir/ ``` --- #### 網路測試 顯示port使用清單 ``` # 棄用 netstat -ant netstat -tulpn ``` ``` #TCP listen ss -lt #UDP listen ss -ul #TCP 連線 ss -t ``` netcat ``` yum install nc -y #TCP nc -vz <ip> <port> #UDP nc -vzu <ip> <port> ``` --- #### 服務 服務腳本路徑 > /usr/lib/systemd/system 服務清單 ``` systemctl list-unit-files ``` 腳本指令 ``` systemctl stop <service name> // 停止服務 systemctl start <service name> // 啟動服務 systemctl restart <service name> // 重啟服務 systemctl status <service name> // 查看服務狀態 systemctl enable <service name> // 使服務為開機啟動 systemctl daemon-reload // 腳本修改後需重新載入 // 串聯使用 systemctl daemon-reload && systemctl enable <service name> && systemctl start <service name> ``` 及時看服務log ``` journalctl -fu <service name> ``` --- #### nginx 設定檔路徑 > /etc/nginx/conf.d ``` nginx -s reload // 重讀設定檔+重啟 ``` --- #### vim from取代成replaced ``` :%s/from/replaced/g ``` 複製貼上不跑版 ``` #貼上前, 關閉自動縮排 :set paste #貼完後, 打開自動縮排 :set nopaste ``` 行號 > vim 全域設定路徑 /etc/vimrc ``` #顯示行號 :set nu #取消行號 :set nonu ``` 搜尋 ``` / = 搜尋 n = 移往下一個搜尋結果 N = 移往上一個搜尋結果 ``` --- #### 時區相關 時間同步 ``` # 安裝時間同步套件 yum install ntp -y # 同步到 國家時間與頻率標準實驗室 ntpdate time.stdtime.gov.tw # 自啟動服務 systemctl start ntpd && systemctl enable ntpd ``` 設定時區 ``` # 顯示系統目前時區 ls -l /etc/localtime # 搜尋系統時區 timedatectl list-timezones | grep Taipei # 設定時區 timedatectl set-timezone Asia/Taipei ``` --- #### linux ssh, scp > <cmd>: ssh或scp > <port>: port號 > <filename>: 檔案名稱, 可用*代替所有(scp only) > <remote>: 遠端伺服器 > <remote path>: 遠端目錄(scp only) ``` <cmd> -P <port> <filename> root@<remote>:<remote path> #example ssh -p 2201 root@172.16.30.99 scp -P 2201 VMwareTools.tar.gz root@172.16.30.99:/tmp ``` --- #### 7zip 安裝 ``` yum install epel-release yum install p7zip p7zip-plugins ``` 使用 ``` #壓縮 7z a <file> #解壓 7z x <file.zip> ``` --- #### AWS-EC2 改用 root 登入 1. 更改為root並設置密碼 ``` sudo su passwd root ``` 2. 調整 sshd_config 設定 ``` vim /etc/ssh/sshd_config PermitRootLogin yes PasswordAuthentication yes UsePAM no // 重啟服務 service sshd restart ``` 3. 調整 authorized_keys 設定 ``` vim /root/.ssh/authorized_keys // 刪除登入提示 command="echo 'Please login as the ec2-user user rather than root user.';echo;sleep 10" ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up