如何设置sshconfiguration文件(〜/ .ssh / config)来识别/ etc / hosts主机名?

我已将以下行(使用不同的IP)添加到/etc/hosts文件中:

 192.168.0.4 my_pc 

同样, ~/.ssh/config文件中有以下条目:

 Host 192.168.0.* IdentityFile ~/id_rsa 

发送一个pingmy_pc ,发送一个ping到192.168.0.4 。 我也可以用ssh [email protected] ssh进入机器。 如果我尝试ssh user@my_pc ,身份validation失败,因为没有使用密钥。

如何设置SSH以便/etc/hosts被识别? 我知道一个主机名可以在~/.ssh/config完成,但是有没有办法让这个主机名不在这个文件里面,并且把IP地址匹配到/etc/hosts

您不需要使用/etc/hosts文件来连接user@my-pc~/.ssh/config添加到~/.ssh/config

 Host my-pc Hostname 192.168.0.4 Port 22 User john IdentityFile ~/id_rsa 

所以现在当你inputssh my-pc它会连接到[email protected]

我已经给自己做了一个脚本,尽快添加新的主机,你也可以使用它;)

 #!/bin/bash #Made by Gen Lee ^.^ ####### #Adds new host to ~/.ssh/config ver="Ezy SSH v0.1" #Colors: red='\033[01;31m' norm='\033[00m' echo "Started $ver" # Questions echo -en "Enter ${red}host${norm}: " read config_host echo -en "Enter ${red}domain${norm}/${red}ip${norm}: " read config_hostname echo -en "Enter ${red}port${norm} (${red}22${norm}): " read config_port echo -en "Enter ${red}username${norm} (${red}root${norm}): " read config_user echo -en "Enter ${red}private key location${norm}: " read config_key # Checking information check_host=$(awk "/^Host $config_host$/" ~/.ssh/config) if [ ! $check_host ]; then sleep 0 else echo -e "You already have following host (${red}$config_host${norm})!" exit 0 fi if [ ! $config_port ]; then config_port="22" fi if [ ! $config_user ]; then config_user="root" fi test -f $config_key if [ $? -eq 1 ]; then echo "Private key location is invalid!" exit 0 fi # Adding parameters to ~/.ssh/config echo "" >> .ssh/config echo "Host $config_host" >> ~/.ssh/config echo -e "\t Hostname $config_hostname" >> ~/.ssh/config echo -e "\t Port $config_port" >> ~/.ssh/config echo -e "\t User $config_user" >> ~/.ssh/config if [ ! $config_key ]; then sleep 0 else echo -e "\t IdentityFile $config_key" >> .ssh/config fi echo "All done! ^.^" exit 0