Skip to content

Commit

Permalink
libpod: fix /etc/hostname with --uts=host
Browse files Browse the repository at this point in the history
when --uts=host is provided, the expectation is to use the hostname
from the host not the container name.

Closes: #20448

Signed-off-by: Giuseppe Scrivano <[email protected]>
  • Loading branch information
giuseppe committed Oct 31, 2023
1 parent 735e243 commit b332ca7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
17 changes: 17 additions & 0 deletions libpod/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
11 changes: 11 additions & 0 deletions test/system/500-networking.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit b332ca7

Please sign in to comment.