Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

網站運維 SSH 常用命令速查手冊:壓縮、文件操作、遠程下載、數據庫備份

編輯:Linux 教學

SSH (Secure Shell) 是 Linux 服務器遠程管理的標準協議,通過加密傳輸、壓縮數據、防止 DNS/IP 欺騙保障安全。本文整理網站開發運維中高頻使用的 SSH 端命令,涵蓋壓縮解壓、文件操作、目錄瀏覽、遠程下載、MySQL 備份恢復,附帶參數詳解與避坑指南。

一、 壓縮與解壓縮

1. tar.gz(最推薦,壓縮率高、普遍支持)

# 壓縮目錄/文件
tar -czvf archive.tar.gz /path/to/source

# 解壓到當前目錄
tar -zxvf archive.tar.gz

# 解壓到指定目錄
tar -zxvf archive.tar.gz -C /target/path

# 僅查看不解壓
tar -ztvf archive.tar.gz

參數:c創建 x解壓 zgzip v顯示過程 f文件名 C切換目錄。

2. .gz 單文件

gzip file.txt        # 壓縮(源文件消失)
gunzip file.txt.gz   # 解壓
zcat file.txt.gz     # 查看內容不解壓

3. .zip(跨平台兼容)

# 壓縮目錄遞歸
zip -r archive.zip /path/to/dir

# 解壓
unzip archive.zip

# 解壓到指定目錄
unzip archive.zip -d /target/path

4. .rar(需額外安裝)

# CentOS/RHEL 安裝
wget https://dag.wieers.com/rpm/packages/unrar/unrar-3.6.8-1.el5.rf.i386.rpm
rpm -Uvh unrar-3.6.8-1.el5.rf.i386.rpm

# 解壓
unrar x archive.rar /target/path/

# 分卷解壓(首卷為 .rar 或 .r00)
unrar x archive.r00 /target/path/

二、 文件與目錄操作

操作命令關鍵參數
複製cp src dst-a保留屬性 -f強制覆蓋 -r遞歸
遠程複製scp -r src user@host:/dst-P port指定端口 -i key密鑰
移動/重命名mv src dst-f強制 -i交互確認
刪除rm file-r目錄 -f無提示 -i確認
修改權限chmod 755 file目錄 755,文件 644;-R遞歸
修改所有者chown user:group file-R遞歸

權限數字含義

  • r=4 w=2 x=1 → 7=4+2+1(全權限)
  • 三位數:擁有者、組用戶、其他人。例 700 僅擁有者可讀寫執行。

三、 目錄瀏覽與跳轉

ls -alh          # 詳細列表,人類可讀大小,含隱藏文件
ls -l file       # 文件屬性
ls -ld dir       # 目錄自身屬性
ls -alh dir      # 目錄下所有文件屬性

cd /absolute/path
cd relative/path
cd ~             # 家目錄
cd -             # 上一個目錄

四、 遠程下載

wget https://example.com/file.tar.gz
wget -c https://example.com/large.iso        # 斷點續傳
wget -O custom_name https://example.com/f    # 重命名保存
wget --limit-rate=1M https://example.com/f   # 限速
wget -r -np -k https://example.com/dir/      # 鏡像整站

優勢:服務器直連公網下載,速度極快,適用 CMS 更新、鏡像獲取。

五、 MySQL 數據庫備份與恢復

1. 備份

# 單庫備份
mysqldump -h localhost -u root -p dbname > backup_$(date +%F).sql

# 多庫備份
mysqldump -h localhost -u root -p --databases db1 db2 > multi.sql

# 全庫備份(含系統庫)
mysqldump -h localhost -u root -p --all-databases > full.sql

# 僅結構不含數據
mysqldump -h localhost -u root -p -d dbname > schema.sql

# 壓縮備份
mysqldump -h localhost -u root -p dbname | gzip > backup.sql.gz

2. 恢復

# 登錄 MySQL 再執行
mysql -h localhost -u root -p dbname < backup.sql

# 或直接管道
gunzip < backup.sql.gz | mysql -h localhost -u root -p dbname

# 遠程備份/恢復(-h 指定遠程主機)
mysqldump -h remote_ip -u user -p dbname | mysql -h localhost -u user -p dbname

3. 大數據優化

  • --single-transaction:InnoDB 熱備份不鎖表。
  • --quick:流式讀取不緩存全表。
  • --extended-insert:多行合並 INSERT(默認開啟)。
  • 恢復時關閉索引:ALTER TABLE t DISABLE KEYS → 導入 → ENABLE KEYS。

六、 實用技巧與避坑

  1. 命令忘記參數:cmd --help 或 man cmd。
  2. SSH 客戶端推薦:PuTTY (Windows)、Tabby/Termius (跨平台)、VS Code Remote-SSH。
  3. 免密登錄:ssh-copy-id user@host 分發公鑰,配合 ssh-agent 管理密鑰。
  4. 會話保持:screen、tmux 防止網絡斷開任務中斷。
  5. 禁用密碼登錄:/etc/ssh/sshd_config 設置 PasswordAuthentication no、PermitRootLogin prohibit-password。
  6. 端口轉發:ssh -L 8080:localhost:80 user@server 將遠程 80 映射到本地 8080。
Copyright © Windows教程網 All Rights Reserved