Score:0

The ssh agent cannot 'resolve hostname' when called from within a bash script

ru flag

I have a bash script

#!/bin/bash
scp -r some_directory remote_host:~

which I made executable with chmod u+x. The remote_host has been defined in my .ssh/config as follows:

Host remote_host
HostName ec2-x-xxx-xx-xxx.eu-central-1.compute.amazonaws.com
User ubuntu
IdentityFile ~/path_to_pem_file/pem_file.pem
IdentitiesOnly yes

when I call scp -r some_directory remote_host:~ or ssh remote_host it works without problems to access the remote machine via ssh. However inside the bash script it doesn't work. I get the following error:

ssh: Could not resolve hostname mochacle: Name or service not known
lost connection
ssh: Could not resolve hostname mochacle: Name or service not known
ssh: Could not resolve hostname mochacle: Name or service not known

irrespective of whether I sudo the execution of the script or not.

Zac67 avatar
ru flag
Have you tried the FQDN?
ph flag
When you run the script with `sudo`, it's running as root and hence will look in root's ~/.ssh/config file, not yours; if that doesn't have an entry for remote_host, you'll get this result. When you use the script without `sudo` and get this error, are you also running it as root (or some other user) via some other means?
ru flag
@Zac67: What is FQDN?
ru flag
@Gordon Davisson: I will try this tomorrow. I think this is the issue.
ph flag
@Marlo FQDN = Fully Qualified Domain Name, i.e. ec2-x-xxx-xx-xxx.eu-central-1.compute.amazonaws.com. That is, have you tried `sudo scp -r -i ~/path_to_pem_file/pem_file.pem some_directory -o IdentitiesOnly=yes [email protected]:~`
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.