gethostname is a package to mock the gethostname() Sys call with a custom host name The gethostname() Sys call is intercepted via LD_PRELOAD before other libs are loaded.
The main intention of this change is to allow Host Names with "_" characters.
Execute the yum or dnf command and verify /usr/lib64/libgethostname.so.1
has been added to the container path:
dnf install -y gethostname
Define NEWHOSTNAME and LD_PRELOAD to spoof the container Host Name inside a container.
$ export NEWHOSTNAME=my_host_name
$ export LD_PRELOAD=/usr/lib64/libgethostname.so.1
$ hostname
my_host_name
Makefile can be used to verify the result for modifying the hostname.
$ make build
gcc -fPIC -rdynamic -g -Wall -shared -Wl,-soname,libgethostname.so.1 -lc -ldl -o libgethostname.so.1 gethostname.c
$ make test
LD_PRELOAD=~/home/opc/GitLab/cicd/gethostname/libgethostname.so.1 NEWHOSTNAME=my_host_name hostname
my_host_name