Set hostname from within a snap

is there a interface I could use to set the hostname from with in the snap ? I am trying to use hostnamectl set-hostname <new-host-name> command from with in my snap but the operation is denied.

Time: Feb 25 22:46:23
Log: apparmor="DENIED" operation="dbus_method_call"  bus="system" 
path="/org/freedesktop/hostname1" interface="org.freedesktop.hostname1" member="SetPrettyHostname" 
mask="send" name="org.freedesktop.hostname1" pid=26082 label="snap.testpal.core" peer_pid=26083 
DBus access

Today, there is no interface for this. The network-manager slot policy almost allows it, by allowing SetHostname. This interface isn’t for plugging snaps though.

If we added this we would probably want to allow the dbus call, using the sethostname() syscall, writing /etc/hostname and making sure the ‘hostname’ command works as expected with --file/-F <filename>.

If this interface were separate, I imagine it should be called hostname-control, and it would be manually connected by default. There is an argument to have it in the existing network-control interface.

I will add this to backlog. If someone wants to implement this for the short term, I suggest creating a PR with changes to interfaces/builtin/network_control.go (and network_control_test.go) and then we can handle implementation details in the PR.

I’m looking at this now and opting for a separate hostname-control.

@dhoomakethu do note that if you use network-manager, you should be able to set the hostname via network-manager for the time being (ie, your snap plugs network-manager then your snap uses whatever DBus APIs network-manager exposes for setting the hostname).

1 Like

Do I have to ship hostnamectl along with my snap as is the case with nmcli and mmcli to set the hostname ?

You will not have to ship it yourself.

For any one coming here with the same problem. nmcli general hostname <hostname> should do the trick. provided network-manager is plugged.

1 Like