vad555
Posts: 8
Joined: Wed Jun 19, 2013 7:45 am

Setting hostname dymanically

Wed Jul 03, 2013 4:08 pm

Hi all,

I'm using rpi-tc based on debian and looking to use pi's as thin clients. The hostname defaults to rpitc on this build. I am looking for a way to set this dynamically on boot to the MAC address of the machine so I can drop the same image down on all and create a unique hostname without extra effort.

Any ideas on how to do this? Is there a variable I can put in the hostname file that it can read?

Regards

User avatar
jackokring
Posts: 816
Joined: Tue Jul 31, 2012 8:27 am
Location: London, UK
Contact: ICQ

Re: Setting hostname dymanically

Wed Jul 03, 2013 4:15 pm

I'm not sure, but maybe there is a /proc file you can link to, and an edit of an up script.
Pi[NFA]=B256R0USB CL4SD8GB Raspbian Stock.
Pi[Work]=A+256 CL4SD8GB Raspbian Stock.
My favourite constant 1.65056745028

vad555
Posts: 8
Joined: Wed Jun 19, 2013 7:45 am

Re: Setting hostname dymanically

Wed Jul 03, 2013 7:31 pm

Can anyone offer any help on how to do this as bit of noob so not certain?

User avatar
DeeJay
Posts: 2027
Joined: Tue Jan 01, 2013 9:33 pm
Location: East Midlands, UK

Re: Setting hostname dymanically

Wed Jul 03, 2013 7:42 pm

Perhaps read the file /usr/bin/raspi-config, to see how that utility changes the hostname? Maybe there is a snippet of code there that you could re-use, that passes in the MAC address as a parameter instead of the input from the user.
How To Ask Questions The Smart Way: http://www.catb.org/~esr/faqs/smart-questions.html
How to Report Bugs Effectively: http://www.chiark.greenend.org.uk/~sgtatham/bugs.html

User avatar
Jim Manley
Posts: 1600
Joined: Thu Feb 23, 2012 8:41 pm
Location: SillyCon Valley, California, and Powell, Wyoming, USA, plus The Universe
Contact: Website

Re: Setting hostname dymanically

Wed Jul 03, 2013 9:04 pm

You can set the host name using the shell command:

hostname NEW_NAME

However, the host name will be reset after a reboot. If you want to permanently change the host name, edit the file /etc/hostname and it will be configured when /etc/init.d/hostname.sh is executed during boot-up.
The best things in life aren't things ... but, a Pi comes pretty darned close! :D
"Education is not the filling of a pail, but the lighting of a fire." -- W.B. Yeats
In theory, theory & practice are the same - in practice, they aren't!!!

vad555
Posts: 8
Joined: Wed Jun 19, 2013 7:45 am

Re: Setting hostname dymanically

Wed Jul 03, 2013 9:10 pm

Jim Manley wrote:You can set the host name using the shell command:

hostname NEW_NAME

However, the host name will be reset after a reboot. If you want to permanently change the host name, edit the file /etc/hostname and it will be configured when /etc/init.d/hostname.sh is executed during boot-up.
So I could use a start up screen to set the following at each boot?:-

HOSTNAME ifconfig | grep "HWaddr" | cut -d" " -f11 | tr -s ":" "."

Would this work?

User avatar
Jim Manley
Posts: 1600
Joined: Thu Feb 23, 2012 8:41 pm
Location: SillyCon Valley, California, and Powell, Wyoming, USA, plus The Universe
Contact: Website

Re: Setting hostname dymanically

Thu Jul 04, 2013 2:30 am

Unless you're doing MAC address spoofing, the MAC address should be the same all of the time, as it's burned into the port by the manufacturer. Once you have it, you should be good to go. I can see where you might have lots of Pii and you don't want to manually set the MAC on each of them, though.

Your proposed command string looks OK at first blush, but I'm a bit lightheaded recovering from tens of miles of cycling ... uphill, into the wind, with the Sun in my eyes, both ways, all in a couple of hours. Meet Jim Manley, a common man with an uncommon problem ... there's a signpost up ahead and it reads ... "The Twilight Zone" ... :o Bah-bahhhh, bah-bah-bah! I should be OK in a week or so, emphasis on the "so" :lol:
The best things in life aren't things ... but, a Pi comes pretty darned close! :D
"Education is not the filling of a pail, but the lighting of a fire." -- W.B. Yeats
In theory, theory & practice are the same - in practice, they aren't!!!

User avatar
jojopi
Posts: 3085
Joined: Tue Oct 11, 2011 8:38 pm

Re: Setting hostname dymanically

Thu Jul 04, 2013 3:32 am

vad555 wrote:HOSTNAME ifconfig | grep "HWaddr" | cut -d" " -f11 | tr -s ":" "."
I assume you mean: hostname $(ifconfig | grep "HWaddr" | cut -d" " -f11 | tr -s ":" ".")

"ifconfig" with no arguments will only print the MAC address if the network is up, which is almost by definition a bit late to be setting the hostname. Make that "ifconfig eth0" if you want to do this early.

I am not sure why you would want the hostname in that form. Most programs abbreviate it to the first node name, so all your Pies will give a shell prompt of "pi@b8", for instance. (The default MAC address passed to the kernel by the boot firmware consists of the Foundation's vendor prefix b8:27:eb, plus the last three octets of the SoC serial number.) Perhaps it would be better with hyphens.

Ideally you should not edit the system init scripts such as /etc/init.d/hostname.sh, in case your changes are lost in a future package update. Either create a script that clobbers /etc/hostname just before hostname.sh, or one that changes hostname again just after hostname.sh.

If you do not care about daemons starting with the original hostname, before it is dynamically set, then it is easiest to put your command in /etc/rc.local

User avatar
rpdom
Posts: 15379
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Setting hostname dymanically

Thu Jul 04, 2013 6:41 am

The MAC address is available in /proc/cmdline before the network is brought up, so I'd create an init script that parses that "file" and extracts the MAC from it and writes it (after suitable processing) to /etc/hostname.

Code: Select all

MAC="$( sed "s/^.*macaddr=\([0-9A-F:]*\) .*$/\1/" /proc/cmdline )"
(unless you've got a model A, where the MAC is defined, but not used due to lack of built-in NIC.)

Also, the MAC is (as jojopi said) generated from "B8:27:EB" plus the last 6 hex digits of your serial number, which you can get from /proc/cpuinfo

Bakul Shah
Posts: 321
Joined: Sun Sep 25, 2011 1:25 am

Re: Setting hostname dymanically

Thu Jul 04, 2013 8:56 am

Not sure what you're trying to do but you shouldn't have to do anything special on individual hosts. Assuming there is a DHCP server on your network, you can associate mac address and host name for each RPi on the server.

vad555
Posts: 8
Joined: Wed Jun 19, 2013 7:45 am

Re: Setting hostname dymanically

Thu Jul 04, 2013 11:21 am

Thanks for your comments everyone.

I'm not sure that I have made myself clear on what I am trying to achieve.

1. Basically I plan to deploy between 10 & 50 pi's in the organisation
2. This will be running the thin client project build - rpi-tc
3. This image defaults to a hostname of 'rpitc'
4. I want one image to deploy to all pi's
5. This will mean that all my hostnames are set to 'rpitc' if I don't manually go in and change them all which I don't want to do

I am looking for a suggestion on how to set the hostname automatically to a unique value (i.e. MAC address or something else unique) so that all my Pi's don't have 'rpitc' as their hostname?

Does this make my query clearer? Sorry if I am not getting your points mentioned.

Any ideas?

Regards

Bakul Shah
Posts: 321
Joined: Sun Sep 25, 2011 1:25 am

Re: Setting hostname dymanically

Thu Jul 04, 2013 6:00 pm

vad555 wrote:4. I want one image to deploy to all pi's
...
I am looking for a suggestion on how to set the hostname automatically to a unique value (i.e. MAC address or something else unique) so that all my Pi's don't have 'rpitc' as their hostname?
Typically a DHCP server in your org's network gives a machine its IP address when first booted up. One or more DNS servers maintain the org'sIP address <-> hostname mapping (and somehow the DHCP and DNS servers are kept in sync). So IMHO it is best to pick up a client's name from the DHCP server. This way everyone can refer to these client machines the same way and you can deploy the same image to all of them. If this is what you want, you can set a dhclient "exit hook". See http://askubuntu.com/questions/104918/h ... hcp-server

User avatar
Jim Manley
Posts: 1600
Joined: Thu Feb 23, 2012 8:41 pm
Location: SillyCon Valley, California, and Powell, Wyoming, USA, plus The Universe
Contact: Website

Re: Setting hostname dymanically

Fri Jul 05, 2013 1:52 am

rpdom wrote:

Code: Select all

MAC="$( sed "s/^.*macaddr=\([0-9A-F:]*\) .*$/\1/" /proc/cmdline )"
Good point. This, modified to only pick up the last six hex characters and replacing colons with hyphens for the reasons jojopi noted, is good enough for my purposes (see below). A script in /etc/rc.local that writes $MAC to /etc/hostname should then be sufficient. The DHCP method seems much more complicated to do essentially the same thing. If boot sequence timing proves to be an issue per the comment about the network interface possibly not yet being established, perhaps a loop that checks for the existence of the MAC needs to be placed before the attempt at extraction, with a reasonable wait() to ensure that bootup isn't further delayed by a script nagging about whether there's a MAC yet.
rpdom wrote:(unless you've got a model A, where the MAC is defined, but not used due to lack of built-in NIC.)
If there's no NIC, there won't be a MAC, but I don't have a Model A yet, so I don't know what's in /proc/cmdline in that case. The MAC in /proc/cmdline (and elsewhere) is obtained by querying the NIC. However, even a Model A can have a WiFi dongle, so wlan0 can exist and it's going to have a NIC/MAC.

I'm going to have the OP's challenge in spades as the schools where I'm helping get Pii introduced in the coming school year aren't going to have IT support capable of keeping up with a Linux system for every one of about 1,000 students per school. Teachers aren't going to have the time, and some won't yet have the experience, either. An early part of the curriculum will be teaching the kids how to perform their own IT support, at least for the kids who can pick it up readily and there are enough of them (I have 6th graders who develop iOS and Android apps in Objective C and Java). So, something like what we're discussing here will be useful until the kids can come up with their own unique host names, if desired.

In our situation, we won't have to worry about system files getting clobbered by updates, as the schools aren't going to have the Internet bandwidth for upwards of 1,000 Pii each downloading a lot of redundant update/upgrade data (some schools have less than 10 Mbps of Internet access for the entire campus, including administration needs). We may not even connect most of the Pi to the real Internet and instead will have local servers providing only the content they need via classroom-only LANs. Even with the novelty of Linux, it wouldn't be long before one of the geniuses figures out how to circumvent the weak Internet filtering measures ineffective IT staffs currently have in place.

If students want to try doing updates/upgrades and similar sorts of things at home, that's fine, and if something gets screwed up, it will become a "learning moment". The limited bandwidth in many schools is where the Foundation's brilliance in providing the simple SD card swap-out capability will really prove its worth. The kids will be able to back up their Pi's data to each other's Pii, USB flash drives, local servers on the LAN, or devices/Internet services at home, and then swap out or reburn their SD cards from images that have been updated/upgraded and configured the way they need to be for our educational purposes.

I need to find out if Clive, et al, have been collecting these sorts of things in an educator's guide as an adjunct to the educational curricula content package they're assembling. If not, I think I know what I need to add to the to-do list and start contributing. The OP can get hooked up as a recipient of that, as I'm sure there are other issues he will face, and we might as well put all of the answers in one place. Thanks for the great points, everyone.
The best things in life aren't things ... but, a Pi comes pretty darned close! :D
"Education is not the filling of a pail, but the lighting of a fire." -- W.B. Yeats
In theory, theory & practice are the same - in practice, they aren't!!!

User avatar
AndyD
Posts: 2331
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: Setting hostname dymanically

Fri Jul 05, 2013 3:38 am

Jim Manley wrote:This, modified to only pick up the last six hex characters and replacing colons with hyphens for the reasons jojopi noted, is good enough for my purposes ...
If there's no NIC, there won't be a MAC, but I don't have a Model A yet, so I don't know what's in /proc/cmdline in that case ...
The last six hex characters of the mac address are the last six characters of the serial number (from /proc/cpuinfo), so you might as well get the serial number and then you won't need to worry about what happens if you are using a Model A.

User avatar
rpdom
Posts: 15379
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Setting hostname dymanically

Fri Jul 05, 2013 4:58 am

Jim Manley wrote:
rpdom wrote:

Code: Select all

MAC="$( sed "s/^.*macaddr=\([0-9A-F:]*\) .*$/\1/" /proc/cmdline )"
Good point. This, modified to only pick up the last six hex characters and replacing colons with hyphens for the reasons jojopi noted, is good enough for my purposes (see below). A script in /etc/rc.local that writes $MAC to /etc/hostname should then be sufficient.
I would have the script in init.d to run before the network is started. That way the Pi will send its generated hostname to the DHCP server along with the DHCP request.
rpdom wrote:(unless you've got a model A, where the MAC is defined, but not used due to lack of built-in NIC.)
If there's no NIC, there won't be a MAC, but I don't have a Model A yet, so I don't know what's in /proc/cmdline in that case. The MAC in /proc/cmdline (and elsewhere) is obtained by querying the NIC. However, even a Model A can have a WiFi dongle, so wlan0 can exist and it's going to have a NIC/MAC.
No. The MAC is generated by the firmware from the serial number whether there is a NIC or not. It is then put into the cmdline fed to the OS via the ATAGS. If there is an on-board NIC then it will be given that MAC - it doesn't get queried for one. Any other NIC on USB will have its own MAC. The model A does get the same MAC in /proc/cmdline as if it were a model B. I know, because I tested the example code I gave on one of my model As. :)

vad555
Posts: 8
Joined: Wed Jun 19, 2013 7:45 am

Re: Setting hostname dymanically

Fri Jul 05, 2013 3:33 pm

Jim Manley wrote:
rpdom wrote:

Code: Select all

MAC="$( sed "s/^.*macaddr=\([0-9A-F:]*\) .*$/\1/" /proc/cmdline )"
Good point. This, modified to only pick up the last six hex characters and replacing colons with hyphens for the reasons jojopi noted, is good enough for my purposes (see below). A script in /etc/rc.local that writes $MAC to /etc/hostname should then be sufficient.

Excellent, thanks for this it works fine but does make my hostnames long especially when I have a departmental idenitfier added to this.

Could you provide any guidance with the script to take the last 6 digits and remove the : from the MAC Address obtained in your example above. I've been struggling with this as when using DASH the cut command is invalid in a script when used after a |

Any help would be appreciated.

Thank you

User avatar
rpdom
Posts: 15379
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Setting hostname dymanically

Fri Jul 05, 2013 4:20 pm

No problem

Code: Select all

MAC="$( sed "s/^.*macaddr=[0-9A-F:]\{9\}\([0-9A-F:]*\) .*$/\1/;s/://g" /proc/cmdline )"
That's with the ":"s removed. If you want them replaced by hyphens instead you can use

Code: Select all

MAC="$( sed "s/^.*macaddr=[0-9A-F:]\{9\}\([0-9A-F:]*\) .*$/\1/;s/:/-/g" /proc/cmdline )"

Gene Long
Posts: 1
Joined: Fri Aug 21, 2015 1:44 pm

Re: Setting hostname dymanically

Fri Aug 21, 2015 1:51 pm

I am in the early stages of using raspberry pi as a possible alternative to our old WYSE systems.
I am planning on using one setup and cloning it to all the other systems.
Of course they will all need their own unique names and this code seems to work nicely.

I placed the code:
MAC="pi""$( sed "s/^.*macaddr=\([0-9A-F:]*\) .*$/\1/;s/://g" /proc/cmdline )"
echo "$MAC" > "/etc/hostname"

In the beginning of the /etc/rc.local file.
It seems to change the /etc/hostname file but it doesn't update the /etc/hosts file and its registration in DHCP doesn't change.
What else do I need to do or should I be placing this in some other file?

ghellquist
Posts: 68
Joined: Thu Aug 02, 2012 8:47 am
Location: Stockholm Sweden

Re: Setting hostname dymanically

Sat Aug 22, 2015 9:58 am

Please share some light to me. Why would each box need its own host name? What and where is it used?
The router will give each machine its own IP adress, could not care less about host name.
Just asking.
// Gunnar

User avatar
DougieLawson
Posts: 36310
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Setting hostname dymanically

Sat Aug 22, 2015 11:08 am

Gene Long wrote: I placed the code:

Code: Select all

MAC="pi""$( sed "s/^.*macaddr=\([0-9A-F:]*\) .*$/\1/;s/://g" /proc/cmdline )"
echo "$MAC" > "/etc/hostname"
You'll also want to add an update to the hosts file and the hostname command

Code: Select all

MAC="pi""$( sed "s/^.*macaddr=\([0-9A-F:]*\) .*$/\1/;s/://g" /proc/cmdline )"
echo "$MAC" > "/etc/hostname"
CURRENT_HOSTNAME=$(cat /proc/sys/kernel/hostname)
sed -i "s/127.0.1.1.*$CURRENT_HOSTNAME/127.0.1.1\t$MAC/g" /etc/hosts
hostname $MAC
ghellquist wrote:Please share some light to me. Why would each box need its own host name? What and where is it used?
The router will give each machine its own IP adress, could not care less about host name.
Just asking.
// Gunnar
As soon as you have more than one server running on your network you need to be able to easily tell which machine you're connected to. With client machines it means you can easily log what stuff is being done by that machine on any server machines.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

aerthqwtjh
Posts: 27
Joined: Mon Sep 10, 2018 5:22 pm

Re: Setting hostname dymanically

Thu Feb 14, 2019 12:54 pm

does not work on stretch:

Code: Select all

root@localhost:~# MAC="pi""$( sed "s/^.*macaddr=\([0-9A-F:]*\) .*$/\1/;s/://g" /proc/cmdline )"
root@localhost:~# echo $MAC
pi8250.nr_uarts=1 bcm2708_fb.fbwidth=1824 bcm2708_fb.fbheight=984 bcm2708_fb.fbswap=1 vc_mem.mem_base=0x3ec00000 vc_mem.mem_size=0x40000000 dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait rootdelay=5
root@localhost:~#

...somebody has a solution for stretch?
thx for feedbacks!!

aerthqwtjh
Posts: 27
Joined: Mon Sep 10, 2018 5:22 pm

Re: Setting hostname dymanically

Thu Feb 14, 2019 1:45 pm

Correct solution:
MAC="xxx-""$(echo `ifconfig -a | grep -o ".\{0,12\}enx.\{0,12\}" | cut -c 10-15`)"
CURRENT_HOSTNAME=$(cat /proc/sys/kernel/hostname)
chattr -i /etc/hostname
echo "$MAC" > "/etc/hostname"
chattr -i /etc/hosts
sed -i "s/127.0.1.1.*$CURRENT_HOSTNAME/127.0.1.1\t$MAC/g" /etc/hosts
hostname $MAC
chattr +i /etc/hostname
chattr +i /etc/hosts

Other "solution" is not usable for stretch, and much more complicated. Just remove the (unnecessary) entries from /etc/hosts then make it read-only.

Return to “General discussion”