Homebrew (macOS)
安裝 Homebrew:
# 安裝 Homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# 設定環境變數 (Apple Silicon Mac)
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zshrc
source ~/.zshrc
# 設定環境變數 (Intel Mac)
echo 'eval "$(/usr/local/bin/brew shellenv)"' >> ~/.zshrc
source ~/.zshrc
常用指令:
# 安裝套件
brew install git
brew install node
brew install docker
brew install hugo
# 安裝 GUI 應用程式
brew install --cask visual-studio-code
brew install --cask docker
brew install --cask google-chrome
# 更新套件
brew update # 更新 Homebrew 本身
brew upgrade # 更新所有套件
brew upgrade git # 更新特定套件
# 搜尋套件
brew search nginx
brew search --cask chrome
# 查看已安裝套件
brew list
brew list --cask
# 移除套件
brew uninstall git
brew uninstall --cask docker
# 清理舊版本
brew cleanup
brew cleanup git
# 查看套件資訊
brew info git
brew deps git # 查看相依性
APT (Ubuntu/Debian)
更新套件清單:
# 更新套件清單
sudo apt update
# 升級所有套件
sudo apt upgrade
# 升級系統 (包含核心)
sudo apt full-upgrade
# 更新並升級 (一次完成)
sudo apt update && sudo apt upgrade -y
安裝套件:
# 安裝單一套件
sudo apt install git
sudo apt install nginx
sudo apt install docker.io
# 安裝多個套件
sudo apt install git curl wget vim
# 安裝特定版本
sudo apt install nginx=1.18.0-0ubuntu1
# 從 .deb 檔案安裝
sudo dpkg -i package.deb
sudo apt install -f # 修復相依性問題
移除套件:
# 移除套件 (保留設定檔)
sudo apt remove nginx
# 完全移除套件 (包含設定檔)
sudo apt purge nginx
# 移除不需要的相依套件
sudo apt autoremove
# 移除並清理
sudo apt remove nginx && sudo apt autoremove
搜尋與查詢:
# 搜尋套件
apt search nginx
apt search "web server"
# 查看套件資訊
apt show nginx
apt info docker.io
# 查看已安裝套件
apt list --installed
apt list --installed | grep nginx
# 查看可升級套件
apt list --upgradable
# 查看套件相依性
apt depends nginx
apt rdepends nginx # 反向相依性
清理系統:
# 清理下載的套件檔案
sudo apt clean
# 清理部分快取
sudo apt autoclean
# 修復損壞的套件
sudo apt install -f
# 重新設定套件
sudo dpkg-reconfigure package-name
YUM/DNF (CentOS/RHEL/Fedora)
DNF (Fedora):
# 更新套件清單
sudo dnf check-update
# 升級所有套件
sudo dnf upgrade
# 安裝套件
sudo dnf install git
sudo dnf install nginx
# 移除套件
sudo dnf remove nginx
# 搜尋套件
dnf search nginx
# 查看套件資訊
dnf info nginx
# 查看已安裝套件
dnf list installed
# 清理快取
sudo dnf clean all
YUM (CentOS 7):
# 更新套件
sudo yum update
# 安裝套件
sudo yum install git
sudo yum install epel-release # EPEL 倉庫
# 移除套件
sudo yum remove nginx
# 搜尋套件
yum search nginx
# 查看套件資訊
yum info nginx
# 查看已安裝套件
yum list installed
# 清理快取
sudo yum clean all
Snap (Universal Packages)
# 安裝 snapd (如果尚未安裝)
sudo apt install snapd
# 安裝 snap 套件
sudo snap install code --classic
sudo snap install docker
sudo snap install hugo
# 查看已安裝的 snap
snap list
# 更新 snap 套件
sudo snap refresh
sudo snap refresh code
# 移除 snap 套件
sudo snap remove code
# 搜尋 snap 套件
snap find "text editor"
# 查看 snap 資訊
snap info code
# 查看 snap 版本
snap version
常用開發工具快速安裝
Node.js 與 npm:
# Ubuntu/Debian
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs
# macOS
brew install node
# 驗證安裝
node --version
npm --version
Docker:
# Ubuntu
sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt update
sudo apt install docker-ce
# macOS
brew install --cask docker
# 啟動 Docker 服務
sudo systemctl start docker
sudo systemctl enable docker
# 將使用者加入 docker 群組
sudo usermod -aG docker $USER
Git:
# Ubuntu/Debian
sudo apt install git
# macOS
brew install git
# 設定 Git
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Hugo:
# Ubuntu (從 GitHub 下載)
wget https://github.com/gohugoio/hugo/releases/download/v0.148.1/hugo_extended_0.148.1_linux-amd64.deb
sudo dpkg -i hugo_extended_0.148.1_linux-amd64.deb
# macOS
brew install hugo
# 驗證安裝
hugo version
系統資訊查詢
# 查看系統資訊
uname -a # 完整系統資訊
uname -r # 核心版本
lsb_release -a # 發行版資訊 (Ubuntu/Debian)
cat /etc/os-release # 系統版本資訊
# 查看硬體資訊
lscpu # CPU 資訊
free -h # 記憶體使用情況
df -h # 磁碟使用情況
lsblk # 區塊設備資訊
lsusb # USB 設備
lspci # PCI 設備
# 查看網路資訊
ip addr show # 網路介面資訊
ip route show # 路由表
netstat -tuln # 網路連線狀態
ss -tuln # 現代版 netstat
# 查看程序資訊
ps aux # 所有程序
top # 即時程序監控
htop # 更好的 top (需安裝)
pgrep nginx # 尋找特定程序
檔案與目錄操作
# 檔案操作
ls -la # 詳細列出檔案
find /path -name "*.log" # 尋找檔案
locate filename # 快速尋找檔案 (需 updatedb)
which command # 尋找指令位置
whereis command # 尋找指令、手冊等
# 檔案權限
chmod 755 file # 設定檔案權限
chmod +x script.sh # 加入執行權限
chown user:group file # 變更擁有者
chgrp group file # 變更群組
# 檔案內容
cat file.txt # 顯示檔案內容
less file.txt # 分頁顯示
head -n 10 file.txt # 顯示前 10 行
tail -f /var/log/syslog # 即時顯示檔案末尾
grep "error" file.txt # 搜尋文字
# 壓縮與解壓縮
tar -czf archive.tar.gz folder/ # 壓縮
tar -xzf archive.tar.gz # 解壓縮
zip -r archive.zip folder/ # ZIP 壓縮
unzip archive.zip # ZIP 解壓縮
服務管理 (systemd)
# 服務狀態
sudo systemctl status nginx # 查看服務狀態
sudo systemctl is-active nginx # 檢查是否運行
sudo systemctl is-enabled nginx # 檢查是否開機啟動
# 服務控制
sudo systemctl start nginx # 啟動服務
sudo systemctl stop nginx # 停止服務
sudo systemctl restart nginx # 重啟服務
sudo systemctl reload nginx # 重新載入設定
# 開機啟動
sudo systemctl enable nginx # 設定開機啟動
sudo systemctl disable nginx # 取消開機啟動
# 查看日誌
sudo journalctl -u nginx # 查看服務日誌
sudo journalctl -u nginx -f # 即時查看日誌
sudo journalctl -u nginx --since "1 hour ago" # 查看最近一小時日誌
# 列出服務
systemctl list-units --type=service # 所有服務
systemctl list-units --type=service --state=running # 運行中服務
systemctl list-unit-files --type=service # 所有服務檔案
網路診斷工具
# 連線測試
ping google.com # 測試連線
ping -c 4 8.8.8.8 # 發送 4 個封包
# 路由追蹤
traceroute google.com # 追蹤路由
mtr google.com # 持續追蹤 (需安裝)
# 埠掃描
nmap localhost # 掃描本機埠
nmap -p 80,443 example.com # 掃描特定埠
# 網路連線
netstat -tuln # 查看監聽埠
ss -tuln # 現代版 netstat
lsof -i :80 # 查看使用特定埠的程序
# 下載工具
wget https://example.com/file.zip # 下載檔案
curl -O https://example.com/file.zip # 使用 curl 下載
curl -I https://example.com # 只取得 HTTP 標頭
效能監控
# CPU 監控
top # 基本監控
htop # 進階監控 (需安裝)
iotop # I/O 監控 (需安裝)
vmstat 1 # 虛擬記憶體統計
# 記憶體監控
free -h # 記憶體使用情況
cat /proc/meminfo # 詳細記憶體資訊
# 磁碟監控
df -h # 磁碟使用情況
du -sh /path/* # 目錄大小
iostat 1 # I/O 統計 (需安裝 sysstat)
# 網路監控
iftop # 網路流量監控 (需安裝)
nethogs # 按程序顯示網路使用 (需安裝)
定時任務 (Cron)
# 編輯 crontab
crontab -e # 編輯當前使用者的 crontab
sudo crontab -e # 編輯 root 的 crontab
# 查看 crontab
crontab -l # 列出當前使用者的 crontab
sudo crontab -l # 列出 root 的 crontab
# Cron 時間格式
# 分 時 日 月 週 指令
# 0 2 * * * /path/to/script.sh
# 常用範例
0 2 * * * # 每天凌晨 2 點
0 */6 * * * # 每 6 小時
0 0 1 * * # 每月 1 號
0 0 * * 0 # 每週日
*/5 * * * * # 每 5 分鐘
# 查看 cron 日誌
sudo tail -f /var/log/cron # CentOS/RHEL
sudo tail -f /var/log/syslog # Ubuntu/Debian