1

I have two Ubuntu 18.04 and 20.x installs at home and trying to ssh between them, I can connect by Internal IP but not by hostname Setup:

Machine A: 192.x.x.115

Machine B: 192.x.x.125

both installs have ssh running and I can connect fine using internal IP.

From Machine B I would like to connect to Machine A using hostname or hostname.ddns.net

Here are things I have setup:

Machine A:

machineA> /etc/hosts

127.0.0.1    machineA.ddns.net machineA

192.x.x.115  machineA

machineA> /etc/hostname

machineA.ddns.net

machineA> /etc/resolv.conf

nameserver 127.0.0.53
options edns0 trust-ad
search hsd1.xx.isp.net

Machine B:

machineB> ssh [email protected] # i.e machineA is successful

machineB> ssh -vvv [email protected] gives

Connecting to machineA.ddns.net [ISP assigned address] port 22.

ssh: connect to host machineA.ddns.net port 22: Connection timed out

machineB> ssh -vvv user@machineA

ssh: Could not resolve hostname machineA: Name or service not known

SSH_from_external_to_machineA is successful

ssh [email protected] # is successful

commands tried on machineA:

machineA> sudo service ssh status # gives active(running)

machineA> sudo netstat -alnp | grep :22 # shows :22 with LISTEN

machineA> sudo iptables -A INPUT -p tcp --dport ssh -j ACCEPT

  1. I feel ISP is not blocking ssh from ssh since I can connect from external using machineA.ddns.net

  2. Router is allowing port 22 since I can connect using IP.

I feel hostname is not resolved correctly, please review and let me know if I am missing any additional steps.

Thanks,

Additional edits:

machineB> ping machineA_Hostname is successful

machineB> nslookup machineA.ddns.net is showing external IP as expected

machineB> ssh machineA # Could not resolve hostname: Name or service not known

machineB> ssh machineA.ddns.net attempts to connect to exact external IP that was reported by ping and 'Connection timed out'

4
  • 1
    How is machineB to know what IP address machineA is located at? The /etc/hosts file on both machines will need entries that point to each other if you are not specifically setting A record DNS entries anywhere.
    – user1091774
    Jan 30, 2021 at 4:49
  • @user68186: thanks for link, I checked some commands. My issue is ssh from machineB does not recognize the hostname of machineA. However ping works with both internal IP and hostname which makes it feel like it is hostname related.
    – Borealis
    Jan 30, 2021 at 15:10
  • @Matigo: I use 'ipconfig' on both machines and know their internal IPs. I have edited machineA /etc/hosts and put in internal IP like 192.x.x.115 machineA.ddns.net machineA machineB can ssh to machineA using IP address, it just does not know hostname. machineB can also ping using hostname, Hope I clarified. can you please elaborate on the required DNS entries. Thanks
    – Borealis
    Jan 30, 2021 at 15:29
  • ssh: Could not resolve hostname machineA.local: Name or service not known
    – Borealis
    Jan 30, 2021 at 20:10

2 Answers 2

0

To avoid hardcoding the internal IP address, setup avahi-daemon on Ubuntu for so you can reach hostname ubuntu.local from host OS.

sudo apt-get install avahi-daemon avahi-discover avahi-utils libnss-mdns mdns-scan

1
  • 1
    Thank you, this solution worked and I could ssh ubuntu.local successfully. While searching on forums I found askubuntu.com/questions/23741/… which may help future visitors.
    – Borealis
    Jan 30, 2021 at 22:28
0

You wanna ssh machine_B from machine_A via hostname, right?

so just put the internal IP of machine_B with a desired name (for calling machine_B) in machine_A

in order to do that, just edit /etc/hosts of machine_A and add something like below:

192.168.222.1 my_lovely_machine_B

now if you type:

ssh user@my_lovely_machine_B

that should work!

BUT if you don't want to bother yourself for hardcoding the internal IP address and make changes everytime (Because IP addresses may change) you can do two things.

First approach: The hard approach but the practical one: you need to run a DNS service such as BIND to get what you want

Second approach: The simple one but not good for every scenario (For you is OK): write a bash script for automation of setting static IP for machine_B and let it run on every startup. in this way you don't need to worry about IP changes and always your machine_B has the same IP.

2
  • This worked, Is it possible to avoid hardcoding the internal IP address, since the IP could change in future. If possible I would like to machineB to be recognized by all internal machines without hardcoding IP in every client.
    – Borealis
    Jan 30, 2021 at 20:22
  • Of course is possible ! . I edit the answer for that as well Jan 30, 2021 at 21:14

Not the answer you're looking for? Browse other questions tagged .