ubuntu小技巧22--ssh 常见操作大全

1 ssh 基础介绍

SSH 为 Secure Shell 的缩写,由 IETF 的网络小组(Network Working Group)所制定;SSH 为建立在应用层基础上的安全协议。SSH 是较可靠,专为远程登录会话和其他网络服务提供安全性的协议。利用 SSH 协议可以有效防止远程管理过程中的信息泄露问题。SSH最初是UNIX系统上的一个程序,后来又迅速扩展到其他操作平台。SSH在正确使用时可弥补网络中的漏洞。SSH客户端适用于多种平台。几乎所有UNIX平台—包括HP-UX、Linux、AIX、Solaris、Digital UNIX、Irix,以及其他平台,都可运行SSH。

基于 ssh 可以实现安全登陆,安全数据拷贝等功能。本文主要记录ssh相关的登陆、拷贝技巧,后续会进一步在此基础上更新ssh相关的其它重要技能。

2 ssh 常见使用方法

2.1 基础口令登陆

口令登录非常简单,只需要一条命令,命令格式为:
ssh 客户端用户名@服务器ip地址
例如: ssh xg@182.168.2.21
该方式登陆需要用户输入密码,按照提示输入即可。

2.2 通过公私钥认证登陆

  1. ssh-keygen 产生 id_rsa 和 id_rsa.pub
  2. 将本地机器LA 上的 id_rsa.pub 追加到 远程服务器 RB 的.ssh/authorized_keys 中
  3. 在LA 机器上直接通过 ssh -p 22(默认22,有些机构也会对其进行更改) username@RB_ip 来登陆RB机器

2.3 ssh 相关的配置

  1. sshd 的基础配置信息在 /etc/ssh/sshd_config
    通过修改 sshd 的基础配置信息 /etc/ssh/sshd_config, 来限制登陆认证和 sshd 的端口;
    例如 Port 12200 ,表明 ssh 对应端口为12200
  2. ssh 登陆基本配置信息在 ~/.ssh/config
    该文件可以配置客户端ssh的基础信息,使其对不同的ip使用不通的用户名和端口;
    以下为笔者的配置:
    github.com 直接使用443端口;
    192.168开头的直接使用 22 端口 和 xg用户;
    tc2 直接使用22端口和root用户(tc2上笔者没有创建个人用户);
    其它的IP直接使用 12200端口和user_xxx用户(user_xxx 是12200端口机器上笔者的个人用户,此处暂用user_xxx代替)
    Host github.com
        StrictHostKeyChecking no
        Port 443
        Hostname ssh.github.com
    Host tc gitlab.xxx.com 192.168.2.* 192.168.1.* es* 
        Port 22
        User xg
    Host tc2 
        Port 22
        User root
    Host *
        StrictHostKeyChecking no
        Port 12200
        SendEnv LANG LC_*
        User user_xxx
    
    注意 config文件权限可以设置755, 否则会报错 Bad owner or permissions on /home/your-user/.ssh/config

2.4 scp 拷贝

scp 是基于ssh协议原创文件拷贝程序,多用于多个主机之间的文件拷贝,其相关用法如下:

scp [-12346BCpqrTv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P port] [-S program] [[user@]host1:]file1 ... [[user@]host2:]file2
文件拷贝:
scp localFile user@RB_host:~ 
scp user@RB_host:~/localFile . 
文件夹拷贝:
scp localDir user@RB_host:~ 
scp user@RB_host:~/localDir . 

注意:scp -P port 中的P为大写的,而ssh 中的p为小写的。

3 ssh agent forwarding

假设场景:有3台机器,本地机器localA(LA),远程服务器remoteB(RB),远程服务器remoteC(RC); LA 和 RB 可以正常通信,RB和RC可以正常通信,但是LA和RC直接网络不能正常通信;此时可以把RB服务器做为跳板机器,通过RB跳到RC上,从而可以登陆RC。
LA:

~ % ssh-add .ssh/id_rsa
Identity added: .ssh/id_rsa (.ssh/id_rsa)
xg@xgmac ~ % ssh -A 10.xx.xx.27(RB ip)
Last login: Wed Nov 18 10:13:26 2020 from 10.xx.xx.27

此时登陆上RB了;
RB:

~$ ssh 7.xx.xx.127
Welcome to Ubuntu 16.04.7 LTS (GNU/Linux 4.4.0-194-generic x86_64)

此时登陆上RC了;
至此,已经通过RB跳到RC机器上了。
相关理论知识见 SSH Agent Forwarding原理

4 常见问题

  1. WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED 错误

    xg@xgmac single_files % ssh -p 22 fhl@106.52.xxx.xxx
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    @    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:t4ZBf2hGPk/SM30MECoMTWaPXKYWODt2mXKBo2wHAqQ.
    Please contact your system administrator.
    Add correct host key in /Users/xg/.ssh/known_hosts to get rid of this message.
    Offending ECDSA key in /Users/xg/.ssh/known_hosts:1228
    Password authentication is disabled to avoid man-in-the-middle attacks.
    Keyboard-interactive authentication is disabled to avoid man-in-the-middle attacks.
    fhl@106.52.xxx.xxx: Permission denied (publickey,password).
    

    问题原因:
    .ssh/known_hosts 中已经通过另外一个用户xg登陆来该服务,已经存在了该身份信息,从而导致冲突了;
    解决方法:

    mv ~/.ssh/known_hosts ~/.ssh/known_hosts_bak
    或者
    ssh-keygen -f "/home/xg/.ssh/known_hosts" -R "fhl"
    # Host fhl found: line 1228
    /home/xg/.ssh/known_hosts updated.
    Original contents retained as /home/xg/.ssh/known_hosts.old
    

    然后再通过 ssh -p 22 fhl@106.52.xxx.xxx 登陆即可

  2. .ssh 权限不正确导致无法正常ssh登陆

    chmod 700 .ssh
    chmod 600 .ssh/authorized_keys
    
  3. ssh 的时候报错 no matching host key type found. Their offer: ssh-rsa

    报错现象:
    $ ssh-jumpserver 
    Unable to negotiate with 46.*.*.* port 2222: no matching host key type found. Their offer: ssh-rsa
    解决方法:
    在 ~/.ssh/config 中加上如下内容
    Host your-jumpserver-host
        Port 22
        User your-name
        HostkeyAlgorithms +ssh-rsa
        PubkeyAcceptedAlgorithms +ssh-rsa
    

    参考 Unable to negotiate with 40.74.28.9 port 22: no matching host key type found. Their offer: ssh-rsa

  4. 从其他机器拷贝 id_rsa和 id_rsa.pub 注意事项
    从其它机器拷贝.ssh 文件后(拷贝到linux系统),需要调整对应权限, 否则导致ssh免密登录无效(需要输入密码)

    chmod 600 id_rsa
    chmod 644 id_rsa.pub 
    

5 说明

  1. 参考文档
    SSH Agent Forwarding原理
    ssh (安全外壳协议
  2. 补充说明
    scp 虽然具有拷贝功能,但是其中不支持exclude参数,每次都会把文件夹下的全部文件拷贝过去,而且不能实现增量拷贝;
    若要解决上述问题,则可以通过 rsync 实现 excude 文件夹和增量拷贝。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

昕光xg

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值