溫馨提示×

Ubuntu SSH命令大全及使用方法

小樊
51
2025-06-21 03:47:57
欄目: 智能運維

在Ubuntu系統中,SSH(Secure Shell)是一種用于加密客戶端與服務器之間鏈接的加密協議,提供了一種安全的遠程訪問和數據傳輸解決方案。以下是在Ubuntu系統中使用SSH命令的基本方法和一些使用技巧:

安裝SSH

  • 安裝SSH服務器

    sudo apt update
    sudo apt install openssh-server
    
  • 安裝SSH客戶端(通常默認已安裝):

    sudo apt install openssh-client
    

啟動和停止SSH服務

  • 啟動SSH服務

    sudo systemctl start ssh
    
  • 停止SSH服務

    sudo systemctl stop ssh
    
  • 重啟SSH服務

    sudo systemctl restart ssh
    

連接到SSH服務器

  • 基本連接命令

    ssh username@ip_address
    
  • 指定端口號連接

    ssh -p port_number username@ip_address
    
  • 免密登錄

    • 生成密鑰對:
      ssh-keygen -t rsa
      
    • 將公鑰復制到遠程服務器:
      ssh-copy-id username@ip_address
      

常用SSH命令使用技巧

  • 查看SSH服務狀態

    sudo systemctl status ssh
    
  • 配置防火墻允許SSH連接

    sudo ufw allow ssh
    
  • 從遠程服務器復制文件到本地

    scp username@ip_address:/path/to/remote/file /path/to/local/destination
    
  • 從本地復制文件到遠程服務器

    scp /path/to/local/file username@ip_address:/path/to/remote/destination
    
  • 斷開SSH連接

    exit
    

其他有用的命令

  • 列出遠程目錄內容

    ssh username@hostname 'ls -l /path/to/directory'
    
  • 創建遠程目錄

    ssh username@hostname 'mkdir -p /path/to/new_directory'
    
  • 刪除遠程文件或目錄

    ssh username@hostname 'rm -rf /path/to/file_or_directory'
    

安全性增強

  • 更改SSH默認端口
    • /etc/ssh/sshd_config 中修改 Port 參數。
  • 禁用root登錄
    • /etc/ssh/sshd_config 中設置 PermitRootLogin no。
  • 使用防火墻限制訪問
    • 使用 ufw 或其他防火墻工具配置規則。

以上信息提供了在Ubuntu系統中使用SSH命令的基本指南和一些常見技巧。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女