创建用户
生成密钥对
1.以dsa的数字算法进行密钥生成
ssh-keygen -t dsa
2.默认路径以及不给密码(接下来的是选项直接回车)
分发公钥
1.默认端口
ssh-copy-id -i id_dsa.pub vagrant@172.16.17.11
2.非默认端口
ssh-copy-id -i id_dsa.pub "-p 22 vagrant@172.16.17.11"
3.确认结果:终端最终会出现如下提示文字
Now try logging into the machine, with "ssh 'vagrant@172.16.17.11'", and check in:
.ssh/authorized_keys
验证
vagrant@172.16.17.11(直接登录到11的服务器)
ssh-copy-id原理
1.复制分发机器上的.ssh/id_dsa.pub到目标机器.ssh目录
2.修改文件名为authorized_keys,修改文件权限为600
如果ssh-copy-id不存在
分发公钥文件到登陆机器上
scp /Users/zhouzhi/.ssh/id_dsa.pub zhi.zhou@172.16.22.63:/home/zhi.zhou/id_dsa.pub
登陆到目标服务器并创建.ssh文件夹及authorized_keys文件(此处为63服务器)
mkdir .ssh && touch .ssh/authorized_keys
将公钥拷贝到目标登陆服务器
cat id_dsa.pub >> .ssh/authorized_keys
修改公钥文件权限为600
chmod 600 .ssh/authorized_keys
加一个超级简单的登陆脚本
#!/bin/bash
env=$1
if [ $env == 'dev' ];then
ip=172.16.22.63
fi
if [ ${env} == 'test' ];then
ip=172.16.22.117
fi
ssh zhi.zhou@$ip