LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 01-15-2016, 07:13 AM   #1
iFunction
Member
 
Registered: Nov 2015
Posts: 185

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:
Code:
#!/bin/bash
# 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: 192.168.1.152

# Clear screen and keeps focus on window
NAME="SD_Card_Image_Rebuild"; echo -en "\033]0;$NAME\a"
clear
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:]]')
iptemplate=192.168.1.1
ipaddress=$iptemplate$rpi
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
iFunc
 
Old 01-15-2016, 04:44 PM   #2
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,602

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
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:

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

To do it try:
Code:
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:

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

    d
    o
    n
    p
    1

    +100M
    t
    b
    p
    2


    w
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 04:54 PM.
 
1 members found this post helpful.
  


Reply


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 01:42 PM
Setting up my second box as a backup DNS and Database Replication netdynamix Linux - Networking 5 03-27-2007 08:19 PM
how to use the hostname instead of ip address? tuxtamer Linux - Newbie 1 04-15-2005 08:29 AM
hostname to IP address bruse Linux - Networking 5 03-30-2005 12:21 AM
From IP address to hostname swmok Linux - Networking 1 08-22-2003 08:26 AM


All times are GMT -5. The time now is 10:26 PM.

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