set hostname on dhcp


13 posts
by damaru » Thu Jun 07, 2012 8:53 pm
I am reposting this here since it might be useful for other. (and that my older post got deleted)

I use my rpi headless so I needed an easy way to track which ip it got when connecting to the network. I uses dhcp but when connecting to a bigger network it becomes hard to find where it is.

to do that I changed the

Code: Select all
/etc/dhcp/dhcpclient.conf

uncommented the send host-name and added a more suitable host-name:

Code: Select all
send host-name "raspberry.pi";

So now I can simply :

Code: Select all
ssh pi@raspberry


to connect to my freshly connected computer. Also since I use lighttp I can simply browse to

http://raspberry/

and my main page shows up!
User avatar
Posts: 39
Joined: Fri May 11, 2012 2:11 am
Location: Canada
by grumpyoldgit » Thu Jun 07, 2012 9:15 pm
I have seen this elsewhere on here in relation to getting a hostname to appear on the list of addresses on a router.


You need to edit the instructions as the file is called dhclient.conf.
Although this procedure enables the Pi to show up on a router I cannot ssh or ping it by the hostname.
User avatar
Posts: 1454
Joined: Thu Jan 05, 2012 12:20 pm
by aXon » Thu Jun 07, 2012 9:37 pm
Hi

I use avahi to detect which IP address my raspberry pi has. It is also called Zeroconf/Bonjour/Rendezvous, initiated by Apple and so far mostly used in Mac applications. Observation post shows all announced services in the local network. Development is discontinued, but still available as download http://www.macupdate.com/app/mac/18505/observation-post . On linux machines you can also use
Code: Select all
avahi-browse -arp

to show all available services and resolved IP addresses.

My services definition in /etc/avahi/services/multiple.service looks like this:
Code: Select all
<?xml version="1.0" standalone='no'?>
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
<service-group>
        <name replace-wildcards="yes">%h</name>
        <service>
                <type>_device-info._tcp</type>
                <port>0</port>
                <txt-record>model=RackMac</txt-record>
        </service>
        <service>
                <type>_ssh._tcp</type>
                <port>22</port>
        </service>
</service-group>
Posts: 2
Joined: Wed Jun 06, 2012 10:07 pm
by damaru » Thu Jun 07, 2012 10:26 pm
Is it just my brain not working or I can't find how to edit my old post ?? Seems like I can edit post I just wrote (like what I am doing right now ) but can't edit older post :(

And grumpyoldgit, yes I was surprise I could ssh to that host. I am not sure if it's due to me being on mac or because my router uses openwrt ? But it's working just as described throughout the network from http to ssh...
User avatar
Posts: 39
Joined: Fri May 11, 2012 2:11 am
Location: Canada
by Joe Schmoe » Thu Jun 07, 2012 11:01 pm
Re: Editing posts

I think the rules are that you can only edit a post if it is the last one in the thread. I.e., once someone else has (possibly) commented upon your post, then you aren't allowed to change it.
Never answer the question you are asked. Rather, answer the question you wish you had been asked.

- Robert S. McNamara - quoted in "Fog of War" -
Posts: 2776
Joined: Sun Jan 15, 2012 1:11 pm
by grumpyoldgit » Fri Jun 08, 2012 12:19 am
damaru wrote:Is it just my brain not working or I can't find how to edit my old post ?? Seems like I can edit post I just wrote (like what I am doing right now ) but can't edit older post :(

And grumpyoldgit, yes I was surprise I could ssh to that host. I am not sure if it's due to me being on mac or because my router uses openwrt ? But it's working just as described throughout the network from http to ssh...


I am at a bit of a loss then. I am trying to SSH and ping from Lubuntu 12.04 and the hostname does not get recognised though it is there now on the router.
User avatar
Posts: 1454
Joined: Thu Jan 05, 2012 12:20 pm
by AndyD » Fri Jun 08, 2012 1:43 am
grumpyoldgit wrote:Although this procedure enables the Pi to show up on a router I cannot ssh or ping it by the hostname.


Probably because when you ssh hostname the hostname is turned into an IP address by either looking in the hosts file http://en.wikipedia.org/wiki/Hosts_%28file%29 or by querying the DNS server http://en.wikipedia.org/wiki/Domain_Name_System. Most people don't run their own local DNS server and so use the one provided by their ISP.

damaru wrote:yes I was surprise I could ssh to that host. I am not sure if it's due to me being on mac or because my router uses openwrt ? But it's working just as described throughout the network from http to ssh...


Possibly because OpenWrt has a dnsmasq http://wiki.openwrt.org/doc/uci/dhcp#using.plain.dnsmasq.conf http://www.thekelleys.org.uk/dnsmasq/doc.html
Posts: 973
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
by damaru » Fri Jun 08, 2012 2:36 am
yes OpenWrt seems to be the culprit here - I am trying from home on a linux only network with few machines and openwrt router and I can easily ssh pi@raspberry or view my lighttp server with http://raspberry/

Sad that it's not a more generalized behavior for the router as it would be a nice simple hack to simplify the ssh login to a headless pi...

What router are you using grumpyoldgit ? (might be time to switch to openwrt! another really kool project based on linux!!)
User avatar
Posts: 39
Joined: Fri May 11, 2012 2:11 am
Location: Canada
by Joe Schmoe » Fri Jun 08, 2012 10:21 am
damaru wrote:I use my rpi headless so I needed an easy way to track which ip it got when connecting to the network. I uses dhcp but when connecting to a bigger network it becomes hard to find where it is.

to do that I changed the

/etc/dhcp/dhcpclient.conf

uncommented the send host-name and added a more suitable host-name:

send host-name "raspberry.pi";


Yes. I can confirm that this method works. You have to do /etc/init.d/networking restart
of course, to get it to "take". Also, the file is, as GoG notes, dhclient.conf (no "cp")

Anyway, going off-topic for a moment, does anyone know how to do the same thing on a Mac? My Mac does not show up in my DHCP table. The net tells me that this is a bug in the Mac side of things - that if the hostname is greater than 15 characters, it won't work - and that the fix is to make sure your hostname is less than 16. And, indeed, my hostname is longer than 15.

But interestingly enough, I noticed that the router display seems to truncate the name to 15 as well. So, maybe both parties are partly responsible for the dilemma.
Never answer the question you are asked. Rather, answer the question you wish you had been asked.

- Robert S. McNamara - quoted in "Fog of War" -
Posts: 2776
Joined: Sun Jan 15, 2012 1:11 pm
by AndrewS » Mon Jun 11, 2012 10:37 am
When I was with BeBroadband, the ISP-provided router did local DNS resolution of local DHCP hostnames. But now I'm with VirginMedia (after moving house) the ISP-provided router doesn't do local DNS resolution :(
So I've configured DHCP on my VM router to always allocate a fixed IP address to my Pi's MAC address, and then set up an entry in /etc/hosts on each of my machines (or %systemroot%\system32\drivers\etc\hosts on Windows) to map the IP address to raspberrypi.
User avatar
Posts: 3626
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
by meltwater » Mon Jul 02, 2012 2:20 pm
Perfect...This was just what I was looking for to make my ssh and vnc sessions a lot easier when switching networks!
______________
http://www.themagpi.com/
A Magazine for Raspberry Pi Users
Read Online or Download for Free.

My new book: goo.gl/dmVtsc

Meltwater's Pi Hardware - pihardware.com

Like the MagPi? @TheMagP1 @TheMagPiTeam
User avatar
Posts: 993
Joined: Tue Oct 18, 2011 11:38 am
by mrmandrill » Fri Jul 12, 2013 7:13 am
Anyone know if it possible to get avahi to communicate it's ip/hostname in a more limited way.

I have a class of 30 computers (each with 2nd NIC), each has a pi connected for vnc sessions. The first NIC (in windows machine) in connected to the school network (10.xxx,xxx,xxx) and the second uses windows connection sharing the give the pi an ip (192.168.xxx.xxx)

Currently the pis use avahi to broadcast their hostname using aXon's config. This broadcasts from the pi through the windows machine and out onto the network (10.xxx) this means that when anyone uses VNC or ping they may connect to any random pi rather than the one next to them.

Currently I'm settling for changing each pis hostname but wondered if their a better way, either avahi just informing the host machine or firewall settings on the host machine.

I could fix the IP of the pi and set each on however I want them to be used by pupils at home and therefore wish to use dynamic ips
User avatar
Raspberry Pi Certified Educator
Raspberry Pi Certified Educator
Posts: 25
Joined: Fri Jul 20, 2012 9:12 am
Location: Cambridge
by itimpi » Fri Jul 12, 2013 11:06 am
It sounds as if you should be setting a hostname that is Pi specific before bringing up the network? Should be easy enough to do I would have thought by using start-up script that derives a name based on the Pi's MAC address (or serial number which is effectively the same). I think it would also be possible to pass a name in via the config.txt file if that would be more convenient.
Posts: 1035
Joined: Sun Sep 25, 2011 11:44 am
Location: Potters Bar, United Kingdom