SSH 公钥检查是一个重要的安全机制,能够防范中间人劫持等黑客攻击。可是在特定状况下,严格的 SSH 公钥检查会破坏一些依赖 SSH 协议的自动化任务,就须要一种手段可以绕过 SSH 的公钥检查。mysql
什么是SSH公钥检查
SSH 链接远程主机时,会检查主机的公钥。若是是第一次该主机,会显示该主机的公钥摘要,提示用户是否信任该主机:sql
The authenticity of host '10.0.0.1 (10.0.0.1)' can't be established.
ECDSA key fingerprint is 91:63:21:08:4a:96:23:5b:f6:98:c9:a8:cd:cb:8b:91.
Are you sure you want to continue connecting (yes/no)?
当选择接受,就会将该主机的公钥追加到文件 ~/.ssh/known_hosts 中。当再次链接该主机时,就不会再提示该问题了。shell
如何去掉公钥确认?
在首次链接服务器时,会弹出公钥确认的提示。这会致使某些自动化任务因为初次链接服务器而任务中断。或者因为~/.ssh/known_hosts 文件内容清空,致使自动化任务中断。 SSH 客户端的 StrictHostKeyChecking 配置指令,能够实现当第一次链接服务器时,自动接受新的公钥。只须要修改 /etc/ssh/ssh_config 文件,包含下列语句:安全
Host *
StrictHostKeyChecking no
或者在 ssh 命令行中用 -o 参数bash
$ ssh -o StrictHostKeyChecking=no 10.0.0.1
--------------------- 服务器
1: 当经过ssh链接远程服务器的时候,可能会出现如下繁琐场景,须要手工输入yes:ssh
ssh username@ip分布式
这对于某些分布式集群来讲是不行的,甚至致使集群都不能启动成功,对于像pssh,pscp这样的自动化工具来讲,也不但愿这一步的验证,如何在链接的时候不提示这个信息呢:ide
1
|
方法一、
ssh
-o
"StrictHostKeyChecking no"
username@
hostname
<br>方法2:修改
/etc/ssh/ssh_config
,在文件最后添加 StrictHostKeyChecking no,接着重启
ssh
服务
|
2:远程执行服务器上面的命令能够经过sshpass(当集群机器之间没有免密的时候),若是没有安装,,执行 yum install sshpass -y工具
用法:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@
test
~]
# sshpass -h
Usage: sshpass [-f|-d|-p|-e] [-hV]
command
parameters
-f filename Take password to use from
file
-----指定一个文件,从文件中读取密码
-d number Use number as
file
descriptor
for
getting password
-p password Provide password as argument (security unwise) ----命令行直接输入密码,不安全
-e Password is passed as
env
-var
"SSHPASS"
-----经过设置环境变量SSHPASS来保存密码
With no parameters - password will be taken from stdin
-P prompt Which string should sshpass search
for
to detect a password prompt
-
v
Be verbose about what you're doing
-h Show help (this
screen
)
-V Print version information
At most one of -f, -d, -p or -e should be used
|
Eg:
(1)使用 -f
1
2
3
4
|
[root@
test
~]
# sshpass -f passwd.txt ssh root@192.168.0.235 "free -m"
total used
free
shared buff
/cache
available
Mem: 96405 27169 12563 4066 56672 63775
Swap: 126 19 107
|
(2)使用 -p
1
2
3
4
|
[root@
test
~]
# sshpass -p "mypasswd" ssh root@192.168.0.235 "free -m"
total used
free
shared buff
/cache
available
Mem: 96405 27168 12584 4066 56652 63777
Swap: 126 19 107
|
注:生产环境不要使用这种方式,不安全
(3)使用 -e
1
2
3
4
5
6
7
|
[root@
test
~]
# export SSHPASS="mypasswd"
[root@
test
~]
# echo $SSHPASS
mypasswd
[root@
test
~]
# sshpass -e ssh hduser@192.168.0.235 "free -m"
total used
free
shared buff
/cache
available
Mem: 96405 27209 12561 4066 56634 63735
Swap: 126 19 107
|
固然,这种方式只针对当前shell有用,若是要配置永久生效,请修改/etc/profile文件
(4)sshpass、ssh都支持多命令调用,只要在命令之间使用&&号就行。
1
2
3
4
5
6
7
8
|
[root@
test
~]
# sshpass -e ssh -l root -o 'StrictHostKeyChecking no' 192.168.4.50 ls /home && free -m
hadoop
mysql
yjt
zbc
total used
free
shared buff
/cache
available
Mem: 977 364 95 49 518 366
Swap: 4095 35 4060
|
三、若是想要远程机器调用本地脚本,那么能够以下实现
(1)ssh方式
1
2
|
[root@
test
~]
# ssh -l root -o 'StrictHostKeyChecking no' 192.168.4.50 bash -s < test46.sh
runstone.com
|
(2)sshpass方式
1
2
|
[root@
test
~]
# sshpass -e ssh -l root -o 'StrictHostKeyChecking no' 192.168.4.50 bash -s < test46.sh
runstone.com
|
四、支持sudo
有些命令须要权限才行,当不想重复输入密码的时候,能够经过这种方式。
(1)格式:cmd ---> 'echo password | sudo -S cmd'
eg:
1
|
[root@
test
~]
# sshpass -p 123456 ssh -o 'StrictHostKeyChecking no' yjt@192.168.4.50 'echo 123456 | sudo -S mkdir /backup'
|
注:-S的意思是从标准输入读取密码
对于echo,dd等命令,可能会出现权限不够问题,如:
1
2
|
[root@
test
~]
# sshpass -p 123456 ssh -o 'StrictHostKeyChecking no' yjt@192.168.4.50 'echo 123456 | sudo -S echo hello > /backup/file'
bash
:
/backup/file
: Permission denied
|
对于上述方式,解决办法以下:
(2)格式:cmd ---> 'echo password | sudo -S sh/bash -c "cmd"'
eg:
1
|
root@
test
~]
# sshpass -p 123456 ssh -o 'StrictHostKeyChecking no' yjt@192.168.4.50 'echo 123456 | sudo -S bash -c "echo hello > /backup/file"'
|