By default, the hostname of the ESP32 is espressif. In this guide, you’ll learn how to set a custom hostname for your board.
To set a custom hostname for your board, call WiFi.setHostname(YOUR_NEW_HOSTNAME); before WiFi.begin();

Setting an ESP32 Hostname
The default ESP32 hostname is espressif.

There is a method provided by the WiFi.h library that allows you to set a custom hostname.
First, start by defining your new hostname. For example:
String hostname = "ESP32 Node Temperature";
Then, call the WiFi.setHostname() function before calling WiFi.begin(). You also need to call WiFi.config() as shown below:
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE);
WiFi.setHostname(hostname.c_str()); //define hostname
You can copy the complete example below:
/*
Rui Santos
Complete project details at https://RandomNerdTutorials.com/esp32-set-custom-hostname-arduino/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
#include <WiFi.h>
// Replace with your network credentials (STATION)
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";
String hostname = "ESP32 Node Temperature";
void initWiFi() {
WiFi.mode(WIFI_STA);
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE);
WiFi.setHostname(hostname.c_str()); //define hostname
//wifi_station_set_hostname( hostname.c_str() );
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi ..");
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(1000);
}
Serial.println(WiFi.localIP());
}
void setup() {
Serial.begin(115200);
initWiFi();
Serial.print("RRSI: ");
Serial.println(WiFi.RSSI());
}
void loop() {
// put your main code here, to run repeatedly:
}
You can use this previous snippet of code in your projects to set a custom hostname for the ESP32.
Important: you may need to restart your router for the changes to take effect.
After this, if you go to your router settings, you’ll see the ESP32 with the custom hostname.

Wrapping Up
In this tutorial, you’ve learned how to set up a custom hostname for your ESP32. This can be useful to identify the devices connected to your network easily. For example, if you have multiple ESP32 boards connected simultaneously, it will be easier to identify them if they have a custom hostname.
For more Wi-Fi related functions, we recommend reading the following tutorial:
We hope you’ve found this tutorial useful.
Learn more about the ESP32 with our resources:
Good idea to set a nice host name. But spaces are not a normally valid character and smaller names are better, so “esp32temp” or “officetemp” are better names. Also, skip the overly complex c++ and just use plain C like in the SSID:
const char* hostname = “esp32temp”;
WiFi.setHostname(hostname);
The valid letters for hostname are a-z 0-9 and -.
I was hoping to access the esp32 via browser using the hostname
In this case I set my hostname to “esp32test”.
I’m able to see that the device connects to the router, and the router shows the LAN IP as well as the hostname above.
I was hoping to access the server via browser at http://esp32test or http://esp32test.local or possibly http://esp32test.home.
None of these work. http://<Esp32'sLanIp> works fine.
Is possible access the device using hostname?
Look at mDNS if you want to use http://esp32test.local
Not having a local DNS server complicates things but the device is apparently getting an IP from a DHCP server, probably in your router. Look and see what HostName is shown in the DHCP address leases for that IP. That should work.
Use mDNS to set a local address.
Update: For a brief moment I got positive results from using
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE);
WiFi.setHostname(“CustomHostname”);
But not in the way described.
After giving up on WiFi.setHostname() entirely, connecting without it to a static IP, a day later I noticed the ESP was accessible by both the static LAN IP and the customized hostname I’d given it.
Haven’t been able to repeat it since, but it suggests ESP32 remembers a prior WiFi.setHostname attempt such that a subsequent WiFi.config with definitive values (as opposed to INADDR_NONE) gives you the ability to completely control your IP addresses and your hostname, both.
at least in the latest libs (2.0.3), WiFi.setHostname() needs to be called before WiFi.mode(), it is not sufficient to call it before just before WiFi.begin().
I use the following code to get a unique Host Name in the familiar form of “ESP_” plus the last 6 hex digits of the MAC address.
Global Variable:
char Host_Name[11];
Local:
byte mac[6];
WiFi.macAddress(mac);
sprintf(Host_Name, “ESP_%02X%02X%02X”, mac[3], mac[4], mac[5]);
WiFi.setHostname(Host_Name);
With an ESP32-S the above mentioned solution did not work.
However when changing the order of of code to this:
const char* newHostname = “ESP32-Test”;
WiFi.setHostname(newHostname); //define hostname
WiFi.mode(WIFI_STA); // station mode
WiFi.begin(ssid, password);
the newHostname was accepted and reacable via ping ESP32-Test