Skip to content

Conversation

@nadzyah
Copy link
Member

@nadzyah nadzyah commented Oct 11, 2025

This PR adds a partial Rust implementation of the hostname command from net-tools as the first utility. The following options have been implemented:

  • Basic get/set hostname functionality
  • --short - display short hostname (just the part before the first dot)
  • --domain - display DNS domain name (everything after the first dot)
  • --ip-address - resolve and display IP addresses for the hostname
  • --all-ip-addresses - display all IP addresses from all network interfaces
  • --yp, --nis - get/set NIS/YP domain name
  • --file - read hostname from a file
  • --verbose - verbose output (additional option)

The options of the original hostname command like --fqdn, --alias, --all-fqdns, and --boot have not been implemented yet because currently I haven't found a way to implement them without using a lot of unsafe blocks

There are two unsafe blocks in the code, both for NIS domain operations, which are getdomainname and setdomainname. The nix crate does not wrap these system calls. They are necessary for NIS/YP functionality and there's no safe Rust alternative. I tried to find safe wrappers, but nix doesn't provide them (unlike gethostname which does have a safe wrapper)
Each unsafe block has a SAFETY comment, and I've added undocumented_unsafe_blocks = "warn" to the Cargo.toml file

Also, since this command uses the original net-tools implementation as a reference, which is licensed under the copyleft licenseGPL-2+, I've changed the license of the project correspondingly and added the copyright notice for the original net-tools authors

@nadzyah nadzyah requested review from bdrung and lvoytek October 11, 2025 10:25
@nadzyah nadzyah changed the title hostname: add command implemenation Add hostname command implemenation Oct 11, 2025
Copy link
Member

@lvoytek lvoytek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This addition looks good to me! I tested it on 25.10 and the behavior matches perfectly for all options except -F, which is a bit less strict than the original net-tools when reading files for some reason. The only missing args are alias, all-fqdns, boot, and fqdn which is fine for now. Nice job!

"Lena Voytek <[email protected]>",
]
license = "MIT"
license = "GPL-2.0-or-later"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The swap to GPL2 seems reasonable to me as a nod to the original net-tools. License + metadata lgtm

eprintln!("Setting hostname to `{name}'");
}

unistd::sethostname(name).map_err(|e| match e {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nix unistd is doing the heavy lifting for hostname set/get, which leads to the use of C bindings from the Linux kernel. I think that is fine though as it matches the original net-tools behavior and I don't think the goal of net-tools-rs is to rewrite kernel modules. LGTM!

@nadzyah nadzyah merged commit 16cf03e into main Oct 13, 2025
5 checks passed
@nadzyah nadzyah deleted the hostname-command branch October 13, 2025 16:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants