Last active
March 19, 2019 04:51
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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