温馨提示×

Ubuntu SSH命令大全及使用方法

小樊
65
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