密钥登陆
linux下可以使用ssh远程登陆集群, 为了登陆方便, 我们可以使用密钥, 生成密钥的命令为: 1
ssh-keygen
使用ssh-copy-id
命令可以将filename.pub
公钥分发给远程主机: 1
ssh-copy-id hapo@webserver1
1
ssh-copy-id -i ~/.ssh/filename.pub hapo@webserver1
此时就能不需要密码登陆远程服务器, 若密钥名字不是默认的名字(~/.ssh/id_rsa
等), 则可以用以下命令指定: 1
ssh -i ~/.ssh/filename hapo@websever1
1
2ssh-keygen -l -f ~/.ssh/filename
ssh-keygen -l -f ~/.ssh/filename.pub~/.ssh/config
中写如下命令简化ssh
登陆时所需参数: 1
2
3
4
5
6
7Host hapo
HostName ip
Port 22
User hapo
IdentityFile ~/.ssh/id_rsa
ServerAliveInterval 60 # 每60秒发送一次空请求
ServerAliveCountMax 10 # 断开时重新连接的次数
使用ssh-agent
和ssh-add
ssh-agent
可以记录密钥的指纹, 并且自动查找和发送到服务器端, 因此不需要在指定所使用的密钥。 启动ssh-agent
的命令为 1
eval `ssh-agent`
ssh-add
添加密钥指纹 1
2ssh-add #添加默认的密钥指纹
ssh-add ~/.ssh/id_rsa_1 #指定密钥的指纹1
ssh-add -L
1
2ssh-add -D # 删除ssh-agent中的所有密钥指纹
ssh-add -d key_file # 删除指定密钥指纹ssh-agent
的命令为 1
ssh-agent -k
oathtool
oathtool
可以用于生成二次验证, 其使用命令行是 1
oathtool -b --totp <identity>
<identity>
为用于生成二次验证的身份码。
sshpass
sshpass
可以用于在命令行输入密码, 命令行如下: 1
sshpass -p <password> <user>@<hostname>
oathtool
, 可以实现免二次验证: 1
2
3#!/bin/bash
totp=`oathtool -b --totp <identity>`
sshpass -p "<password> $totp" <user>@<hostname>
expect
expect
命令可以用于与终端进行交互, expect
使用的是tcl语言, 这里不准备说明语法,只说明对应的一些用法