In a shell script, it ssh to a server cluster, which includes dozens of servers. But some new servers do not run commands properly. I believe it is ssh yes or no problem. So I would like to have ssh use 'yes' automatically for ssh a new server. like yum -y install.

sshpass -p "$pass" ssh root@host "command"
sshpass -p "$pass" scp /local/file root@host:$directory/

This may be because StrictHostKeyChecking option is set to ask. It may not be set explicitly, ask is the default value.

From man 5 ssh_config:

If this flag is set to yes, ssh(1) will never automatically add host keys to the ~/.ssh/known_hosts file, and refuses to connect to hosts whose host key has changed. This provides maximum protection against trojan horse attacks, though it can be annoying when the /etc/ssh/ssh_known_hosts file is poorly maintained or when connections to new hosts are frequently made. This option forces the user to manually add all new hosts. If this flag is set to no, ssh will automatically add new host keys to the user known hosts files. If this flag is set to ask, new host keys will be added to the user known host files only after the user has confirmed that is what they really want to do, and ssh will refuse to connect to hosts whose host key has changed. The host keys of known hosts will be verified automatically in all cases. The argument must be yes, no, or ask. The default is ask.

Looks like you would like to set the option to no. In /etc/ssh/ssh_config the line would be:

StrictHostKeyChecking no

Or you can override the option during ssh or scp invocation by passing -o StrictHostKeyChecking=no to the tool, e.g:

ssh -o StrictHostKeyChecking=no root@host "command"
scp -o StrictHostKeyChecking=no /local/file root@host:"$directory"/

Note there is also VerifyHostKeyDNS option with the default value ask. Refer to the manual. If you need to change it, the solution is similar.


You may try this one :

ssh-keyscan -t ecdsa serverName

it will add the key to ~/.ssh/known_hosts.

  • Thank you for your answer. You could improve it by adding a bit more detail such as: where the command should be run, as what user it should run, and what the effect is of adding the key to ~/.ssh/known_hosts. – StarCat Mar 18 '20 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.