Announcement

Collapse
No announcement yet.

SSH into a local PC without a static IP

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    SSH into a local PC without a static IP

    I came up with a simple solution to a complex problem.

    I have 6 Linux machines on my home network. I don't want to hunt them all down when updates or service is needed, so I use SSH to log into them remotely. I use a private key to avoid password entry and I use non-standard ssh ports to a tad extra security. For my static IP machines (if it has a wired connection, I configure it statically) I simply put the hostname and IP into my /etc/hosts file and the username, hostname, and port into ~.ssh/config. Then in a terminal, I just have to enter "ssh <hostname>" and I'm logged in. To further shortcut this, I put an alias into my ~/.bash_aliases file that does the ssh part, so I just type the hostname.

    Example:
    My server has a hostname of "server", a static IP of 192.168.1.20, and uses port 4459 for ssh. My username on the server is "overlord".

    The .ssh/confg entry looks like:
    Host server
    Port 4599
    User overlord
    HostName server

    the /etc/hosts entry looks like this:
    192.168.1.20 server.mylan server

    and the ~/.bash_aliases entry looks like this:
    alias server="ssh server"

    When I want to log into the server and run an update, simply typing "server" into konsole and I'm logged in and ready to work. Works like a charm.

    HOWEVER, some of the machines use wifi and DHCP to connect to the network and so I don't know their IP addresses without checking. The above only works if you know the IP. I could configure them to have fixed IP's but then when they wonder out of the house - to school or Starbucks - they can't connect or need to be re-configured again. I looked at adding zeroconf or other things, but then I'd have to do it to ALL the laptops. There had to be a way to do it with existing tools.

    My solution: The arp -a command will print all the hostnames on the network - assume they're configured properly (mine are) with their IPs. So I combined arp with grep, awk, and tr and put it all in my .bash_aliases file.

    Example - one of the wireless computers has "Asus" in it's hostname:

    arp -a lists all the hostnames and IP's in this format: Asus-CN60 (192.168.1.99) at c3:45:42:69:30:20 [ether] on enp0s31f6
    grep Asus lists only the line with the above info
    awk '{print $2}' prints the second field from the above line: (192.168.1.99)
    tr -d '()' strips the parenthesis and leaves just the IP address

    Put them all together with an alias and here's the command in .bash_aliases:

    alias asus="ssh -p 4499 `arp -a |grep Asus | awk '{print $2 }' | tr -d '()'`"

    Now with one word I can log into that machine using ssh by just typing "asus".

    Instead of spending half a day installing and configuring otherwise unneeded tools on all the wifi machines, this command took me less than 2 minutes to perfect and put into place.

    I'm sure there are "neater" solutions or maybe even the above could be done better - but it works!

    Please Read Me

    #2
    A very clever solution!
    Click image for larger version

Name:	Bow_Down_smiley.gif
Views:	7
Size:	32.7 KB
ID:	643805
    "A nation that is afraid to let its people judge the truth and falsehood in an open market is a nation that is afraid of its people.”
    – John F. Kennedy, February 26, 1962.

    Comment


      #3
      if your local stuff is set up correctly you should be able to access local machines by their name or name.local
      Ive toyed with stuff like automounting for things like that. but in the end it was ultimately easier to just use the remote address to do whatever..

      not a bad solution
      Mark Your Solved Issues [SOLVED]
      (top of thread: thread tools)

      Comment


        #4
        Thanks for that sithlord! I first tried using the hostname and it didn't work in konsole - just hangs there and eventually times out. I read that you had to have mDNS installed to use the .local extension - obviously wrong info - so I didn't try that before, but it does work. Adding the entry with the .local extension to .ssh/config also works.

        The .local does not work from within an alias, which seems strange. The exact hostname is "Asus-CN60". If I type "ssh -p 4355 Asus-CN60.local" it logs in immediately. If I put the that in an alias, it returns:

        "ssh: Could not resolve hostname asus-ch60.local: Name or service not known"

        Is bash forcing lowercase and causing the issue? Regardless, the .ssh/config entry with .local works so I can use an alias to that command. So now I have two solutions.

        Next up - ssh into my wife's Windows 8 Pro machine!

        Please Read Me

        Comment


          #5
          Originally posted by oshunluvr View Post
          Is bash forcing lowercase and causing the issue?
          Hostnames are case insensitive, so that shouldn't be an issue.

          Could be a typo in your post, but your error message says "asus-ch60.local" while your successful command says "asus-cn60.local", is that the problem?

          Comment


            #6
            Originally posted by kubicle View Post
            Hostnames are case insensitive, so that shouldn't be an issue.

            Could be a typo in your post, but your error message says "asus-ch60.local" while your successful command says "asus-cn60.local", is that the problem?
            Yup, the typo was in the aliases. Good catch. I didn't know the hostnames weren't case sensitive. Must be the only place in linux where case doesn't matter!

            Please Read Me

            Comment


              #7
              Originally posted by oshunluvr View Post
              Yup, the typo was in the aliases. Good catch.
              What can I say, I have a keen eye for other people's mistakes...luckily I don't make any myslef.

              Originally posted by oshunluvr View Post
              I didn't know the hostnames weren't case sensitive. Must be the only place in linux where case doesn't matter!
              Probably due to the fact that that hostnames (and the DNS spec) aren't specific to linux.

              Comment

              Working...
              X