Skip to content

Instantly share code, notes, and snippets.

@ereli
Last active March 19, 2019 04:51
Show Gist options
  • Save ereli/49a2b92b5ff2d60afa25d18369050157 to your computer and use it in GitHub Desktop.
Save ereli/49a2b92b5ff2d60afa25d18369050157 to your computer and use it in GitHub Desktop.
set hostname on linux. Useful if the machine doesn't have hostnamectl and you don't want to reboot.
package main
import (
"flag"
"fmt"
"os"
"syscall"
)
func main() {
hostnamePtr := flag.String("hostname", "foo", "a string")
flag.Parse()
fmt.Println("hostame:", *hostnamePtr)
err := syscall.Sethostname([]byte(*hostnamePtr))
if err != nil {
fmt.Println(err)
} else {
hostname, err := os.Hostname()
if err != nil {
fmt.Println(hostname)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment