Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 01-15-2016, 06:13 AM   #1
Registered: Nov 2015
Posts: 248

Rep: Reputation: Disabled
Raspberry pi self replication script, setting ip address and hostname

Hi there,

I have written a script to relicate the sd card image on a raspberry pi, but have got a little stuck on how to approach the last part, setting the hostname and ipaddress. My experience is the the hosts file needs to have the hostname modified also. Could someone point me in the right direction please. this is the file so far:
# The ip addresses are allocated according to their place in the garage the
# last octet of the ip address starts and 100 the last to numbers are the
# garage pod number and the unit in the pod. So the second raspberry pi in
# pod 5 would be:
# hostname: pi5slave2
# ipaddress:

# Clear screen and keeps focus on window
NAME="SD_Card_Image_Rebuild"; echo -en "\033]0;$NAME\a"
sleep 0.75s
wmctrl -a "SD_Card_Image_Rebuild"

echo "Enter name on side of the Raspberry Pi that needs to be rebuilt>"
read -r INPUT

# condition input to produce hostname and ip address
no_space=${INPUT// /}
rpi=$(echo $no_space | tr -cd '[[:digit:]]')
echo $ipaddress
echo $INPUT

sudo umount /dev/sda1
sudo umount /dev/sda2

# Formats sd card correcttly for bootable Raspberry Pi image
(echo d; echo ; echo d; echo o; echo n; echo p; echo 1; echo ; echo +100M; echo t; echo b; echo n; echo p; echo 2; echo ; echo ; echo w) | sudo fdisk /dev/sda

sudo mkfs.vfat /dev/sda1
echo "y" | sudo mkfs.ext4 -j /dev/sda2

# Mount sd card for writing image
sudo mkdir /tmp/newpi
sudo mount /dev/sda2 /tmp/newpi/
sudo mkdir /tmp/newpi/boot
sudo mount /dev/sda1 /tmp/newpi/boot/

sudo rsync -av --one-file-system / /boot /tmp/newpi/

# Here is where I am stuck, I tried this line for the hostname, 
# though although it works in the command line, I haven't
# successfully managed to get it to work in a script
# It obviously will take half an hour to test, so I commented out
# the formatting and writing of the disk and remounted it to the 
# same filenames but get permission denied

sudo echo "$INPUT" > /tmp/newpi/etc/hostname

# need to change ip adress too

sudo umount /dev/sda1
sudo umount /dev/sda2

read -p "Format complete, please hit [escape] key then [enter] key to clear window and remove sd_card."
Some pointers would be much appreciated.

Kind regards
Old 01-15-2016, 03:44 PM   #2
Senior Member
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,842

Rep: Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472Reputation: 1472
At least on my Pi the hostname is set by the file /etc/hostname.

There is no direct tie in with the /etc/hosts file, other than for convenience.

If you are trying to use "sudo" to create a /etc/hostname, then you have to do it differently than:

sudo echo "$INPUT" > /tmp/newpi/etc/hostname
As this leaves you as the owner of the file, rather than root.

To do it try:
sudo bash -c "echo '$INPUT' >/tmp/newpi/etc/hostname"
Here the sudo executes bash, which is run as root. That bash process will then interpret the string to set the hostname (and since the redirection is done by the root bash, it gets owned by root).

Side comment... You might prefer the partitioning done this way - mostly due to it being easier to read:

# Formats sd card correcttly for bootable Raspberry Pi image
sudo fdisk /dev/sda <<-DONE



This would also make it simpler to read if the +100M were replaced by an environment variable.

Either way should work.

Last edited by jpollard; 01-15-2016 at 03:54 PM.
1 members found this post helpful.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to get hostname from ip address doughyi8u Linux - Networking 4 11-05-2014 12:42 PM
Setting up my second box as a backup DNS and Database Replication netdynamix Linux - Networking 5 03-27-2007 07:19 PM
how to use the hostname instead of ip address? tuxtamer Linux - Newbie 1 04-15-2005 07:29 AM
hostname to IP address bruse Linux - Networking 5 03-29-2005 11:21 PM
From IP address to hostname swmok Linux - Networking 1 08-22-2003 07:26 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:25 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration