From b332ca7a021d3f57a062a348e2aa27613ae5ee7a Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano Date: Tue, 31 Oct 2023 12:41:11 +0100 Subject: [PATCH] libpod: fix /etc/hostname with --uts=host when --uts=host is provided, the expectation is to use the hostname from the host not the container name. Closes: https://github.com/containers/podman/issues/20448 Signed-off-by: Giuseppe Scrivano --- libpod/container.go | 17 +++++++++++++++++ test/system/500-networking.bats | 11 +++++++++++ 2 files changed, 28 insertions(+) diff --git a/libpod/container.go b/libpod/container.go index ddb2c1b105..bc904f0ba4 100644 --- a/libpod/container.go +++ b/libpod/container.go @@ -686,6 +686,23 @@ func (c *Container) Hostname() string { return c.config.Spec.Hostname } + // if the container is not running in a private UTS namespace, + // return the host's hostname. + privateUTS := false + if c.config.Spec.Linux != nil { + for _, ns := range c.config.Spec.Linux.Namespaces { + if ns.Type == spec.UTSNamespace { + privateUTS = true + break + } + } + } + if !privateUTS { + hostname, err := os.Hostname() + if err == nil { + return hostname + } + } if len(c.ID()) < 11 { return c.ID() } diff --git a/test/system/500-networking.bats b/test/system/500-networking.bats index 5a53169f0f..38a10ffa92 100644 --- a/test/system/500-networking.bats +++ b/test/system/500-networking.bats @@ -911,4 +911,15 @@ EOF run_podman network rm $net1 } +# Issue #20448 - /etc/hostname with --uts=host must show "uname -n" +@test "podman --uts=host must use 'uname -n' for /etc/hostname" { + run_podman info --format '{{.Host.Hostname}}' + hostname="$output" + run_podman run --rm --uts=host $IMAGE cat /etc/hostname + assert "$output" = $hostname "/etc/hostname with --uts=host must be equal to 'uname -n'" + + run_podman run --rm --net=host --uts=host $IMAGE cat /etc/hostname + assert "$output" = $hostname "/etc/hostname with --uts=host --net=host must be equal to 'uname -n'" +} + # vim: filetype=sh