SSH - chaolunner/CloudNotes GitHub Wiki

  • 下载并解压到C:\Program Files\OpenSSH

  • 在搜索框中输入Windows PowerShell并以管理员身份运行

  • 按如下方式键入命令:

    cd C:\Program Files\OpenSSH
    powershell.exe -ExecutionPolicy Bypass -File install-sshd.ps1
    netsh advfirewall firewall add rule name=sshd dir=in action=allow protocol=TCP localport=22
    
  • 启动服务:

    • 命令行启动:net start sshd,停止:net stop sshd
    • bat启动:
      # 自动以管理员身份运行
      @echo off&(cd/d "%~dp0")&(cacls "%SystemDrive%\System Volume Information" >nul 2>&1)||(start "" mshta vbscript:CreateObject^("Shell.Application"^).ShellExecute^("%~snx0"," %*","","runas",1^)^(window.close^)&exit /b)
      net start sshd
      
    • 开机自启动: 将上面的bat文件创建快捷方式并添加到C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp
  • 免密登陆:

    • 生成SSH密钥:

      使用CMD或者Git Bash运行如下命令:

      # -t 指定要创建的密钥类型,-C 注释(一般填写用户名或邮箱),-f 指定用来保存密钥的文件名(不指定的话:~/.ssh/id_ras;~/.ssh/id_ras.pub;)
      ssh-keygen -t rsa -C "<用户名>"
      
    • 配置sshd_config

      用以下内容覆盖C:\ProgramData\ssh\sshd_config文件中的内容

      PubkeyAuthentication yes
      AuthorizedKeysFile	.ssh/authorized_keys
      PasswordAuthentication yes
      Subsystem	sftp	sftp-server.exe
      

      如果你已经启动了服务,需要先运行net stop sshd再运行net start sshd来重启服务

    • 在服务器添加公钥:

      • 客户端使用命令行添加

        ssh-copy-id -f -i ~/.ssh/id_rsa.pub <用户名>@<IP地址>
        
      • 直接在服务端添加

        复制客户端~/.ssh/id_rsa.pub文件中的内容,粘贴到服务端~/.ssh/authorized_keys文件中

    • 使用ssh -i ~/.ssh/id_rsa <用户名>@<IP地址>登陆

      如果还是需要输入密码,可以添加-vvv参数来输出详细信息

  • 登陆配置:

    还可以通过在~/.ssh/config文件中添加如下内容来简化登陆流程:

    Host <任意命名>
        HostName <IP地址>
        Port 22
        User <用户名>
        IdentityFile ~/.ssh/id_rsa
    

    之后你就可以通过ssh <任意命名>来登陆了

  • 安装步骤:Tmux Plugin Manager
    • 执行 git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
    • 复制以下内容到 ~/.tmux.conf (没有则创建)
      # 开启鼠标模式
      set-option -g mouse on
      
      # List of plugins
      set -g @plugin 'tmux-plugins/tpm'
      set -g @plugin 'tmux-plugins/tmux-sensible'
      set -g @plugin 'tmux-plugins/tmux-resurrect'
      set -g @plugin 'tmux-plugins/tmux-continuum'
      
      # 15分钟保存一次
      set -g @continuum-save-interval '15'
      set -g @continuum-restore 'on'
      set -g @resurrect-capture-pane-contents 'on'
      
      # Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
      run '~/.tmux/plugins/tpm/tpm'
      
    • 执行 ~/.tmux/plugins/tpm/bin/install_plugins
  • 在正常终端模式下:
    • tmux 直接新建一会话,并进入
    • tmux ls 列出当前有哪些会话
    • tmux a -t <编号> 连接回之前的会话
  • Tmux模式下:
    • prefix 默认 Ctrl + b
    • prefix + S 保存
    • prefix + R 恢复
    • 启用鼠标模式后,需要按住 Shift 键来使用原始右键菜单

常用命令

  • 复制命令:
    • 从客户端复制内容到服务端:
      #单个文件的复制去掉参数-r
      #<客户端地址><服务端地址>:可以填当前路径下的相对路径也可以填绝对路径
      #路径存在 空格 的时候可以在地址两端添加单引号+双引号,如:'"<服务端地址>"'
      scp -r <客户端地址> <用户名>@<IP地址>:<服务端地址>
      
⚠️ **GitHub.com Fallback** ⚠️