View and modify hostnames

The hostname identifies your virtual machine (VM) in the user interface and Azure operations. You first assign the hostname of a VM in the Virtual machine name field during the creation process in the Azure portal. After you create a VM, you can view and modify the hostname either through a remote connection or in the Azure portal.

View hostnames

You can view the hostname of your VM in a cloud service by using any of the following tools.

Azure portal

In the Azure portal, go to your VM, and on the service menu, select Properties. On the Properties page, you can view the hostname under Computer Name.

Screenshot that shows the Properties page of a virtual machine and highlights Computer Name.

Remote desktop

You can connect to your VM by using a remote desktop tool like Remote Desktop (Windows), Windows PowerShell remoting (Windows), SSH (Linux and Windows) or Bastion (Azure portal). You can then view the hostname in a few ways:

  • Enter hostname in PowerShell, the command prompt, or an SSH terminal.
  • Enter ipconfig /all in the command prompt (Windows only).
  • View the computer name in the system settings (Windows only).

Prerequisites

None

Azure API

From a REST client, follow these instructions:

  1. Ensure that you have an authenticated connection to the Azure portal. Follow the steps presented in Create a Microsoft Entra application and service principal that can access resources.

  2. Send a request in the following format:

    GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachines/{vmName}?api-version=2022-11-01`. 
    

    For more information on GET requests for VMs, see Virtual machines - GET.

  3. Look for the osProfile and then the computerName element to find the hostname.

    Warning

    You can also view the internal domain suffix for your cloud service by running ipconfig /all from a command prompt in a remote desktop session (Windows) or by running cat /etc/resolv.conf from an SSH terminal (Linux).

Modify a hostname

You can modify the hostname for any VM by renaming the computer from a remote desktop session or by using Run command in the Azure portal.

  1. Change the hostname.

    • From a remote session:

      • For Windows, you can change the hostname from PowerShell by using the Rename-Computer command.
      • For Linux, you can change the hostname by using hostnamectl.
    • From the Azure portal: You can also run these commands to find the hostname for your VM from the Azure portal by using Run command. In the Azure portal, go to your VM, and on the service menu, select Run command. On the Run command page in the Azure portal:

      • For Windows, select RunPowerShellScript and use Rename-Computer in the Run Command Script pane.
      • For Linux, select RunShellScript and use hostnamectl in the Run Command Script pane.

      The following image shows the Run command page in the Azure portal for a Windows VM.

      Screenshot that shows the Run command page for a Windows virtual machine and highlights the RunPowerShellScript feature.

  2. After you run either Rename-Computer or hostnamectl on your VM, you need to restart your VM for the hostname to change.

    Azure classic deployment model

    The Azure classic deployment model uses a configuration file that you can download and upload to change the hostname. To allow your hostname to reference your role instances, you must set the value for the host name in the service configuration file for each role. You do that by adding the hostname that you want to the vmName attribute of the Role element. The value of the vmName attribute is used as a base for the hostname of each role instance.

    For example, if vmName is webrole and there are three instances of that role, the hostnames of the instances are webrole0, webrole1, and webrole2. You don't need to specify a hostname for VMs in the configuration file because the hostname for a VM is populated based on the VM name. For more information about configuring an Azure service, see Azure service configuration schema (.cscfg File).

    Service configuration file

    In the Azure classic deployment model, you can download the service configuration file for a deployed service from the Configure pane of the service in the Azure portal. You can then look for the vmName attribute for the Role name element to see the hostname. This hostname is used as a base for the hostname of each role instance. For example, if vmName is webrole and there are three instances of that role, the hostnames of the instances are webrole0, webrole1, and webrole2. For more information, see Azure Virtual Network configuration schema.