通过配置ssh-config,简化ssh连接

  • 很多时候我们需要通过ssh连接开发,测试,线上服务器。在windows下我们有xshell等工具,配置一下密钥认证就可以快速连接。但是在mac或者在linux工作环境下下可能就没有比较通用的客户端了,这里我们介绍一种原生的通过配置ssh_config的方式简化连接命令。
  • 这是一段ssh_config文件的注释,解释这样做的原理:
# This is the ssh client system-wide configuration file.  See
# ssh_config(5) for more information.  This file provides defaults for
# users, and the values can be changed in per-user configuration files
# or on the command line.
  • 步骤如下:
  • 配置你的ssh_config文件(一般在/etc/ssh目录下)如下:
Host 98  //这里是设置你的连接的一个别名
Hostname 111.111.111.111   //设置ip
Port 56789 //设置端口号
User wangsan   //设置登录名
PasswordAuthentication no  //是否需要密码认证,如果是sshkey认证,则no
ForwardAgent yes   //是否需要转发key到代理服务器,如果需要通过跳转机连接同学,这里需要填写yes
AddKeysToAgent yes //将key添加到sshkey转发列表
IdentityFile ~/.ssh/98/id_rsa //这里是你生成的私钥地址,对应的公钥也需要放入id_rsa.pub
  • 将你的公私钥文件放入上面填写的地址,这里需要注意,需要将/.ssh/98目录设置成700权限,将id_rsa文件设置成600权限,拥有者改为自己,原因如下:
~/.ssh/identity
     ~/.ssh/id_dsa
     ~/.ssh/id_ecdsa
     ~/.ssh/id_ed25519
     ~/.ssh/id_rsa
             Contains the private key for authentication.  These files contain sensitive data and should be readable by the user but not accessible by others (read/write/execute).  ssh will simply ignore a private key file if it is accessible by others.  It is possible to specify a passphrase when generating the key which will be used to encrypt the sensitive part of this file using 3DES.
也就是私钥文件因为安全需要,不能被其他用户访问,如果访问安全不够,则会默认忽略该私钥。
具体怎么改?
修改权限:
    1.把从.ssh到获取密钥之间的目录权限修改成700,上面的例子我们需要修改98目录的权限:chmod 700 目录
    2.把私钥文件权限修改成600:chmod 600 私钥文件
    3.查看当前登录用户 whoami
    4.修改私钥所有者为自己 chown 当前用户
  • 配置好后,我们只需要键入:ssh 98,即可访问配置好的远程服务器
  • 备注
    1. 网上也有通过iterm2配置profile的方式简化登录方式,也不失为一种好办法,但是比较依赖客户端程序
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值