Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Enable setting hostname #5

Open
cinderblock opened this issue Jul 18, 2021 · 0 comments
Open

[Feature Request] Enable setting hostname #5

cinderblock opened this issue Jul 18, 2021 · 0 comments

Comments

@cinderblock
Copy link

cinderblock commented Jul 18, 2021

I've been adding the following service to my Pi images so that I can use a file /boot/hostname to set the hostname the Pi will boot with before putting the SD card into the Pi.

I'm curious if you would consider adding this script to this package, or alternatively, if there was a different package that this would be good for.

Here is the service file:

[Unit]
Description=Change hostname by /boot file
ConditionFileNotEmpty=/boot/hostname
Before=network-pre.target
Wants=network-pre.target

[Service]
Type=oneshot
# Print current hostname for logs
ExecStart=/usr/bin/hostname
# Copy hostname file
ExecStart=/bin/cp /boot/hostname /etc/hostname
# Ensure file ends with a newline
ExecStart=/usr/bin/sed -i -e $$a\\ /etc/hostname
# Set hostname from file
ExecStart=/usr/bin/hostname -F /etc/hostname
# Remove default hostname from /etc/hosts
# Should this be changed to work for any hostname that is currently in `/etc/hosts`, instead of hard coded "raspberrypi"?
ExecStart=/usr/bin/sed -i.orig -E /^127.0.1.1\\s+raspberrypi\\s*$$/d /etc/hosts
# Add new hostname to /etc/hosts
ExecStart=/bin/sh -c 'echo 127.0.1.1\\t$(hostname) >> /etc/hosts'
# Reset hostname file in /boot
ExecStart=/usr/bin/truncate -s 0 /boot/hostname

[Install]
WantedBy=multi-user.target

I opted to keep the file around and just empty it to indicate that it's been used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant