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

docs: fix init-hook script for hostname matching #1182

Merged
merged 1 commit into from
Feb 15, 2024

Conversation

michaelhaaf
Copy link
Contributor

@michaelhaaf michaelhaaf commented Feb 1, 2024

The init-hook provided in the useful tips documentations for creating a distrobox with the the same hostname as its host has two typos:

  • Command substitution wrapping uname -n suppressing stdout redirect
  • trailing "`"

Using this init-hook as suggested causes build errors (see #1164). This fix corrects the suggested script by removing the command substitution and the trailing "`".

To elaborate: I think command substitution here is misplaced since uname -n already prints to stdout and can be redirected directly. Or you can use echo on the provided command substitution. Or you can use the hostname command. I.e.

uname -n > /etc/hostname
echo "$(uname -n)" > /etc/hostname
hostname "$(uname -n)"

Tested on Fedora 39 below:

# Also tested the 'echo "$(uname -n)" > /etc/hostname' with the same result

user@hostname $ distrobox create --image ghcr.io/username/sample-image:latest -n box-name \ 
    --init-hooks 'uname -n > /etc/hostname' && distrobox enter box-name

...

Setting up groups...                    	 [ OK ]
Setting up users...                     	 [ OK ]
Setting up skel...                      	 [ OK ]
Executing init hooks...                 	 [ OK ]

Container Setup Complete!

[email protected] $ cat /etc/hostname 
box-name.hostname

[email protected] $ hostname
box-name.hostname  

@89luca89
Copy link
Owner

89luca89 commented Feb 1, 2024

Hi @michaelhaaf
You're right about the wrong command

But I think the correct one would be

user@hostname $ distrobox create --image ghcr.io/username/sample-image:latest -n box-name \ 
    --init-hooks "echo $(uname -n) > /etc/hostname; hostname $(uname -n)" && distrobox enter box-name

Tested and it works

@89luca89 89luca89 linked an issue Feb 1, 2024 that may be closed by this pull request
@michaelhaaf
Copy link
Contributor Author

The following are equivalent and all work:

uname -n > /etc/hostname
echo "$(uname -n)" > /etc/hostname
uname -n > /etc/hostname; hostname "$(uname -n)"
echo "$(uname -n)" > /etc/hostname; hostname "$(uname -n)"

Maybe there's room for preference; personally the first choice makes the most sense to me, the rest have superfluous commands.

(to elaborate: The call to hostname seems redundant, beyond that it is not system-independent and does not make the change permanent, unlike overwriting /etc/hostname. I think the first choice is the most concise: why evaluate uname -n then echo that evaluation if you can just redirect uname -n stdout to begin with.)

@89luca89
Copy link
Owner

89luca89 commented Feb 3, 2024

The uname -n > /etc/hostname will result in the container injecting its own hostname in the file

The echo $(uname -n) > /etc/hostname works because the $(uname -n) is evaulated at distrobox-create time, so in the end the result is just one echo foo > /etc/hostname in the init_hook

The init-hook provided for creating a distrobox with the
the same hostname as its host has two typos:

- Command substitution wrapping `uname -n` suppressing stdout redirect
- trailing "`"

Using this init-hook as suggested causes build errors.
This fix corrects the suggested script by echoing the command
substitution and removing the trailing "`".

See issue 89luca89#1164 for details.
@michaelhaaf
Copy link
Contributor Author

The echo $(uname -n) > /etc/hostname works because the $(uname -n) is evaulated at distrobox-create time, so in the end the result is just one echo foo > /etc/hostname in the init_hook

It's unclear to me how that works (in my tests I got the same result using both).

But, in the interest of getting a working version in the documentation/removing the typo/moving on, I've updated my PR to use echo "$(uname -n)" > /etc/hostname instead.

@89luca89
Copy link
Owner

Thanks a lot @michaelhaaf

@89luca89 89luca89 merged commit 0a9f8aa into 89luca89:main Feb 15, 2024
8 checks passed
@robinhellmers
Copy link

Adding a little bit to this conversation as there was a typo in this change. Having the single quotation marks outside of the double quotation marks will prevent $(uname -n) from being evaluated during the distrobox-create command. That does, for me, result in <container-name>.<hostname>, meaning no change at all.

Changing
--init-hooks 'echo "$(uname -n)" > /etc/hostname'
to
--init-hooks "echo '$(uname -n)' > /etc/hostname"
solves that issue.

Other than that, the command
echo '$(uname -n)' > /etc/hostname
is not equivalent to
echo '$(uname -n)' > /etc/hostname; hostname '$(uname -n)'

as the first example requires a reboot for the hostname command to actually use the changed /etc/hostname. Meanwhile if you also execute hostname "$(uname -n)", a reboot isn't required.

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

Successfully merging this pull request may close these issues.

[Error] Not able to change hostname in init_hooks
3 participants