OpenBSD Change Hostname

Posted on in Categories , last updated January 6, 2018

I am a new OpenBSD Unix user. How do I change my hostname under OpenBSD operating system using the cli?

The hostname command displays the name of the current OpenBSD server or host. The superuser (root) can set the host name by editing a /etc/myname file. This is used at system boot time by netstart to initialize the hostname.

How to display hostname on OpenBSD

Type the any one of the following command
# hostname
# cat /etc/myname

Sample outputs:

openbsd.nixcraft.in

OpenBSD Change Hostname Commands

OpenBSD Chane Host Name

Simply login as root:
$ su -
OR
$ doas -s
Type the following command to change/update your hostname:
# echo "new-hostname.domain.com" > /etc/myname
# echo "server.nixcraft.in" > /etc/myname
# echo 'unixbox' > /etc/myname

Or edit using a text editor such as vi:
# vi /etc/myname
From the man page:

/etc/myname contains the symbolic name of the host machine. The file should contain a single line specifying the fully qualified domain name (FQDN) of the system (e.g. host.example.com). The name must be resolvable, either by matching a hostname specified in /etc/hosts (see hosts(5)) or through DNS (see resolv.conf(5)). The hostname is set via the hostname(1) utility at boot time. See hostname(7) for a description of hostname resolution.

During the system boot, netstart is executed. netstart performs the various operations including setting up the machine’s name. You can reboot box to test new settings:
# reboot
OR just set hostname at the cli i.e. no need to reboot the server:
# hostname server.nixcraft.in
You must edit /etc/hosts file too:
# vi /etc/hosts
Rename all old hostname to new hostname if present in that file. For more info see the following man pages:
$ man hostname
$ man 5 myname
$ man 5 hosts
$ man 8 netstart
$ man 1 su
$ man 1 doas

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Share this on (or read 1 comments/add one below):

1 comment

  1. To make hostname changes effective, one could issue this command:
    hostname `cat /etc/myname`

    Have a question? Post it on our forum!