美文网首页程序猿阵线联盟-汇总各类技术干货
Mac 上玩 Linux(三)使用 SSH 连接 Ubuntu

Mac 上玩 Linux(三)使用 SSH 连接 Ubuntu

作者: JamFF | 来源:发表于2019-05-26 00:35 被阅读0次

    Mac 上玩 Linux(一)安装Ubuntu
    Mac 上玩 Linux(二)Ubuntu 支持 root 登录
    Mac 上玩 Linux(三)使用 SSH 连接 Ubuntu
    Mac 上玩 Linux(四)安装 VMware Tools

    首先 Mac 不像 Windows 需要类似 XShell 这样的客户端进行 SSH 连接,直接使用终端即可。

    一、首先查看 IP 地址

    我这里是使用的 Ubuntu 18.04 的模拟器。

    1. 输入命令:ifconfig

      未安装提示
    2. 需要安装 net-tools:apt install net-tools

      安装 net-tools
    3. 安装成功后,再次 ifconfig

      IP 地址

    二、ssh 登录

    1. ssh 登录 root 用户
      使用 ssh root@IP地址 或者 ssh -p 22 root@IP地址 命令
      出现错误:
      ➜  ~ ssh root@192.168.139.129
      ssh: connect to host 192.168.139.129 port 22: Connection refused
      ➜  ~ ssh -p 22 root@192.168.139.129
      ssh: connect to host 192.168.139.129 port 22: Connection refused
      
    2. 检查服务端 ssh 服务是否开启
      ps -e | grep sshd
      
      没有找到 sshd ,说明 ssh 服务没有启动,需要开启 ssh 服务。

    三、启动服务器的 ssh

    1. 开启 ssh 服务
    service ssh start
    
    没有安装 ssh
    1. 更新源列表
    apt-get update
    
    1. 安装 openssh-server
    apt-get install openssh-server
    
    1. 再次查看 ssh 服务是否启动
    ssh 服务已启动

    四、ssh 登录

    1. 连接 root 用户
    ➜  ~ ssh root@192.168.139.129      
    The authenticity of host '192.168.139.129 (192.168.139.129)' can't be established.
    ECDSA key fingerprint is SHA256:rVB8kHFOI0hCUyibvY6w5TfDF6I+5z0qG2jcVIxi94c.
    Are you sure you want to continue connecting (yes/no)? yes
    
    1. 输入 root 用户密码
    Warning: Permanently added '192.168.139.129' (ECDSA) to the list of known hosts.
    root@192.168.139.129's password: 
    
    1. 权限被拒绝
    Permission denied, please try again.
    

    这种情况,很大的可能是由于服务器默认禁止 root 用户进行 ssh 远程登录。

    1. 尝试普通用户登录,排除配置问题
    ➜  ~ ssh ff@192.168.139.129
    ff@192.168.139.129's password: 
    
    1. 输入用户密码,普通用户登录成功。
    Welcome to Ubuntu 18.04.2 LTS (GNU/Linux 4.18.0-17-generic x86_64)
    
     * Documentation:  https://help.ubuntu.com
     * Management:     https://landscape.canonical.com
     * Support:        https://ubuntu.com/advantage
    
    
     * Canonical Livepatch is available for installation.
       - Reduce system reboots and improve kernel security. Activate at:
         https://ubuntu.com/livepatch
    
    167 个可升级软件包。
    75 个安全更新。
    
    Your Hardware Enablement Stack (HWE) is supported until April 2023.
    
    The programs included with the Ubuntu system are free software;
    the exact distribution terms for each program are described in the
    individual files in /usr/share/doc/*/copyright.
    
    Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
    applicable law.
    
    ff@FF-VM:~$ 
    

    到这里就基本确定了就是,服务器禁止 root 用户进行 ssh 远程登录了。

    五、允许 root 用户 ssh 登录

    1. 修改 sshd_config
      注意,安装了 openssh 才会有这个文件,如果文件不存在请检查是否安装了 openssh。
    vim /etc/ssh/sshd_config
    
    1. 配置 PermitRootLogin
    • 检查是否有 PermitRootLogin
    • 存在 PermitRootLogin ... 的话(...代表任何语句),全部修改为 PermitRootLogin yes
    • 不存在 PermitRootLogin 的话,增加 PermitRootLogin yes
    配置 PermitRootLogin yes

    不过这里有问题,正常情况下 PermitRootLogin 应该也是黄色高亮的。
    我尝试了多次,还是不能高亮,不管了先保存。

    1. 重启 ssh 服务
    /etc/init.d/ssh restart
    
    1. 连接 root 用户
    ➜  ~ ssh root@192.168.139.129      
    root@192.168.139.129's password: 
    Permission denied, please try again.
    

    依然权限被拒绝,说明配置 PermitRootLogin yes 没有生效。

    六、重新安装 openssh-server

    大多数情况下,完成上一步就可以完成登录了,是不需要该步骤的。

    1. 卸载 openssh-server
    apt-get purge openssh-server
    
    1. 删除 sshd_config
    rm /etc/ssh/sshd_config
    
    1. 更新源列表
    apt-get update
    
    1. 安装 openssh-server
    apt-get install openssh-server
    
    1. 打开 sshd_config
    vim /etc/ssh/sshd_config
    
    1. 配置 PermitRootLogin
    配置 PermitRootLogin yes
    1. 启动 ssh 服务
    /etc/init.d/ssh start
    

    七、 ssh 登录

    连接 root 用户

    ➜  ~ ssh root@192.168.139.129
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    @    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
    Someone could be eavesdropping on you right now (man-in-the-middle attack)!
    It is also possible that a host key has just been changed.
    The fingerprint for the ECDSA key sent by the remote host is
    SHA256:5devwrE/cIsO8P0l9nc7zQ+xxKRO5BIUDZ1tjd8BB+0.
    Please contact your system administrator.
    Add correct host key in /Users/ff/.ssh/known_hosts to get rid of this message.
    Offending ECDSA key in /Users/ff/.ssh/known_hosts:5
    ECDSA host key for 192.168.139.129 has changed and you have requested strict checking.
    Host key verification failed.
    

    出现错误的原因是,重装了 openssh-server ,服务器的公钥发生了变化导致的错误。

    SSH 服务是通过公钥和私钥来进行连接的,它会把每个曾经访问过计算机或服务器的公钥,记录在 known_hosts 中,当下次访问曾经访问过的计算机或服务器时,SSH 就会核对公钥,如果和上次记录的不同,OpenSSH 会发出警告。

    八、修改客户端 known_hosts 文件

    根据提示进行修改

    Add correct host key in /Users/ff/.ssh/known_hosts to get rid of this message.
    
    1. 进入 known_hosts
    ➜  ~ vim /Users/ff/.ssh/known_hosts
    
    1. 找到以 192.168.139.129 起始的行,将正行删除
    192.168.139.129 ecdsa-sha2-nistp256 ...省略
    

    九、ssh 登录

    1. 连接 root 用户
    ➜  ~ ssh root@192.168.139.129      
    The authenticity of host '192.168.139.129 (192.168.139.129)' can't be established.
    ECDSA key fingerprint is SHA256:5devwrE/cIsO8P0l9nc7zQ+xxKRO5BIUDZ1tjd8BB+0.
    Are you sure you want to continue connecting (yes/no)? yes
    
    1. 输入 root 用户密码
    Warning: Permanently added '192.168.139.129' (ECDSA) to the list of known hosts.
    root@192.168.139.129's password: 
    
    1. root 用户登录成功
    Welcome to Ubuntu 18.04.2 LTS (GNU/Linux 4.18.0-20-generic x86_64)
    
     * Documentation:  https://help.ubuntu.com
     * Management:     https://landscape.canonical.com
     * Support:        https://ubuntu.com/advantage
    
    
     * Canonical Livepatch is available for installation.
       - Reduce system reboots and improve kernel security. Activate at:
         https://ubuntu.com/livepatch
    
    0 个可升级软件包。
    0 个安全更新。
    
    Your Hardware Enablement Stack (HWE) is supported until April 2023.
    
    The programs included with the Ubuntu system are free software;
    the exact distribution terms for each program are described in the
    individual files in /usr/share/doc/*/copyright.
    
    Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
    applicable law.
    
    root@FF-VM:~# 
    

    相关文章

      网友评论

        本文标题:Mac 上玩 Linux(三)使用 SSH 连接 Ubuntu

        本文链接:https://www.haomeiwen.com/subject/psfzzqtx.html