« Back to Settings for systemd units

ProtectHostname setting

The property ProtectHostname is a systemd unit setting used for sandboxing. It is available since systemd 242.

Purpose: defines if hostname or NIS domain name can be changed

Why and when to use ProtectHostname

Systemd provides the unit setting ProtectHostname to restrict changing the hostname of the system. This makes use of the UTS namespace, which defines the hostname and NIS domain name. The hostname can normally be changed using the syscall sethostname(2), while the NIS domain name has the related setdomainname(2) syscall. If the the ProtectHostname setting is active (boolean yes or true), a new UTS namespace is created that prevents making any changes.

The creation of a new UTS namespace is done using clone(2) or unshare(2) syscall with the CLONE_NEWUTS flag set. This functionality requires a Linux kernel that is compiled with the CONFIG_UTS_NS option.

Syscalls of interest are:

  • sethostname
  • setdomainname

If a program contains these system calls, then additional research is needed if ProtectHostname can be used safely.

Generic advice

Setting the hostname is rarely needed, so most services can be configured with ProtectHostname=yes.

Values

Systemd unit setting ProtectHostname expects a boolean (yes/no or true/false) or string value.

Values for systemd unit setting ProtectHostname
ValueIntended actionAvailable since
systemd version
nonormal behavior of hostname changes - default242
yesno changes to hostname or NIS domain name allowed242

Example to show the current value of ProtectHostname for the ssh service:

systemctl show --property=ProtectHostname ssh.service

Related hardening profiles

The systemd unit setting ProtectHostname is used in the following systemd hardening profiles. These hardening profiles help improving security of common Linux services and usually require minimal tuning.

Frequently Asked Questions

How to use systemctl edit?

Run systemctl with the 'edit' subcommand and service.

systemctl edit UNIT.service

See full answer at How to use systemctl edit to change a service?

Feedback

Small picture of Michael Boelen

This article has been written by our Linux security expert Michael Boelen. With focus on creating high-quality articles and relevant examples, he wants to improve the field of Linux security. No more web full of copy-pasted blog posts.

Discovered outdated information or have a question? Share your thoughts. Thanks for your contribution!

Mastodon icon

Related articles

Like to learn more? Here is a list of articles within the same category or having similar tags.