Courseiva
SystemGlobal Config

hostname

Identify the device in the network by setting a meaningful hostname that appears in the prompt, syslog messages, and CDP advertisements.

Definition: hostname is a Cisco IOS global config command. Sets the hostname of a Cisco router or switch. The hostname appears in the CLI prompt, is used in SSH key generation (hostname.domain-name), and is advertised via CDP to neighbour devices.

Overview

The `hostname` command is a fundamental configuration command in Cisco IOS that sets the device's hostname. The hostname appears in the CLI prompt (e.g., Router(config)#) and is used by network management systems to identify the device. This command is typically one of the first executed when configuring a new router or switch, as it personalizes the device and helps avoid confusion in multi-device environments.

The hostname is stored in the running configuration and can be saved to the startup configuration. Changing the hostname does not affect device operation but is critical for operational clarity, especially when managing multiple devices via SSH or console. There is no alternative command for setting the hostname; it is the sole method.

The command requires global configuration mode (privilege level 15) and takes immediate effect. The hostname can be up to 63 characters, but it is recommended to keep it short and meaningful. Common mistakes include using spaces or special characters (only letters, digits, and hyphens are allowed).

The hostname is also used in syslog messages and SNMP traps, making it important for network monitoring and troubleshooting. In a broader workflow, setting the hostname is often followed by configuring domain name, enable secret, and interface IP addresses. The command is available in all IOS versions and platforms, including IOS-XE, NX-OS, and IOS-XR, though syntax may vary slightly.

The hostname does not affect routing protocols or switching behavior but is essential for device identification in network diagrams and documentation. When troubleshooting, verifying the hostname helps confirm you are on the correct device. The command is simple but foundational for network configuration management.

Syntax·Global Config
hostname <name>

When to Use This Command

  • First command in any device configuration — identify the device
  • Required before crypto key generate rsa (hostname becomes part of SSH key name)
  • Appears in CDP neighbour tables to help identify devices in topology discovery
  • Used in syslog messages to identify the source device

Parameters

ParameterSyntaxDescription
nameWORD (1-63 characters)Specifies the new hostname for the device. The name can contain letters, digits, and hyphens, but must not start or end with a hyphen. Spaces and special characters are not allowed. Common mistakes include using underscores or spaces, which will cause the command to fail.

Command Examples

Set hostname on a core switch

Switch(config)# hostname CORE-SW1
CORE-SW1(config)# end
CORE-SW1# show running-config | include hostname
hostname CORE-SW1

Understanding the Output

The prompt changes immediately to reflect the new hostname. Hostname is saved to startup-config with write memory. The hostname is included in CDP advertisements (show cdp neighbors lists it as Device ID).

Hostname length is limited to 63 characters and cannot include spaces — use hyphens instead.

Configuration Scenarios

Set hostname on a new branch router

A network engineer is deploying a new Cisco router at a branch office. The router must be identified as 'Branch-Router' in the CLI and network management systems.

Topology

Single router: Branch-Router

Steps

  1. 1.Step 1: Access the router via console or SSH and enter privileged EXEC mode: Router> enable
  2. 2.Step 2: Enter global configuration mode: Router# configure terminal
  3. 3.Step 3: Set the hostname: Router(config)# hostname Branch-Router
  4. 4.Step 4: Exit configuration mode and verify: Branch-Router(config)# end
Configuration
! Enter global config
Router# configure terminal
! Set hostname
Router(config)# hostname Branch-Router
Branch-Router(config)# end

Verify: Use the command 'show running-config | include hostname' to verify the hostname is set. Expected output: hostname Branch-Router. Also check the prompt changes to Branch-Router#.

Watch out: Forgetting to save the configuration with 'write memory' or 'copy running-config startup-config' will cause the hostname to be lost after a reload.

Rename a core switch in a production network

A core switch currently named 'Switch-1' needs to be renamed to 'Core-SW-01' to align with a new naming convention. The change must be done without disrupting network operations.

Topology

Single switch: Core-SW-01

Steps

  1. 1.Step 1: Connect to the switch and enter privileged EXEC mode: Switch-1> enable
  2. 2.Step 2: Enter global configuration mode: Switch-1# configure terminal
  3. 3.Step 3: Change the hostname: Switch-1(config)# hostname Core-SW-01
  4. 4.Step 4: Exit and verify: Core-SW-01(config)# end
Configuration
! Enter global config
Switch-1# configure terminal
! Change hostname
Switch-1(config)# hostname Core-SW-01
Core-SW-01(config)# end

Verify: The prompt changes immediately to Core-SW-01#. Use 'show running-config | include hostname' to confirm.

Watch out: If the switch is part of a stack, the hostname change applies to the entire stack. Ensure the new name is unique within the network to avoid confusion.

Troubleshooting with This Command

The `hostname` command is rarely used for troubleshooting network issues, but verifying the hostname is a common first step when connecting to a device to ensure you are on the correct device. A healthy output shows the expected hostname in the prompt and in the running configuration. Problem indicators include an unexpected hostname (e.g., 'Router' default) indicating the device may have been reset or the configuration lost.

To troubleshoot, first check the prompt: if it shows 'Router>' or 'Switch>', the hostname has not been set or the configuration was lost. Use 'show running-config | include hostname' to see the configured hostname. If the hostname is missing, reconfigure it.

If the hostname appears but the prompt shows a different name, the device may have been reloaded without saving. Correlate with 'show version' to check uptime and configuration register. In multi-device environments, incorrect hostnames can lead to misconfiguration if commands are applied to the wrong device.

Always verify the hostname before making changes. The command does not produce debug output, but syslog messages include the hostname, so incorrect hostnames can cause confusion in log analysis. To correlate with other commands, use 'show cdp neighbors' to see hostnames of neighboring devices.

If a device has a duplicate hostname, CDP will show duplicate entries. The hostname is also used in SNMP sysName, so verify with 'show snmp mib ifmib ifName' or similar. In summary, while not a troubleshooting command per se, verifying the hostname is a critical sanity check in any network diagnostic workflow.

CCNA Exam Tips

1.

CCNA exam: hostname is one of the first commands taught

2.

Know that it affects: (1) CLI prompt, (2) SSH key name (hostname.domain-name), (3) CDP device ID

3.

Changing hostname after generating SSH keys requires regenerating the RSA keys because the key name includes the old hostname

Common Mistakes

Using spaces in hostname — IOS truncates at first space

Not realising hostname change invalidates existing RSA keys for SSH

Forgetting to save with write memory — hostname reverts on reload

Using 'Router' or 'Switch' as hostname (the defaults) — makes devices indistinguishable in logs

hostname vs no ip domain-lookup

Both `hostname` and `no ip domain-lookup` are global configuration commands that affect the CLI experience, but they serve different purposes. `hostname` sets the device's identity and appears in the prompt, while `no ip domain-lookup` prevents long delays when mistyping commands by disabling DNS resolution. They are often configured together to improve usability.

Aspecthostnameno ip domain-lookup
ScopeSets device hostname globallyDisables DNS lookups globally
Configuration ModeGlobal ConfigGlobal Config
PurposeIdentify device and affect prompt, SSH keys, CDPEliminate 30-second delays on typo commands
Effect on CLIChanges router prompt to <hostname>No visual change; unknown words produce immediate error
PersistenceSaved in running-config, startup-configSaved in running-config, startup-config
Related Commandsip domain-name, ip hostip domain-lookup (re-enables), ip name-server

Use hostname when you need to set the router or switch name for management, SSH key generation, or CDP advertisement.

Use no ip domain-lookup when you want to avoid 30-second delays caused by accidental DNS lookups for mistyped commands.

Platform Notes

In Cisco IOS-XE, the `hostname` command syntax is identical to classic IOS. The hostname is stored in the running configuration and can be saved to startup. In NX-OS, the command is also `hostname` and works similarly, but the prompt format may differ slightly (e.g., 'switch#').

In IOS-XR, the command is `hostname` as well, but it is executed in global configuration mode. There is no difference in behavior between IOS versions 12.x, 15.x, and 16.x for this command. On ASA firewalls, the command is `hostname` and works the same way.

The command is available on all Cisco platforms that support CLI configuration. Note that in some older IOS versions, the hostname could not contain certain characters, but modern versions allow letters, digits, and hyphens. The maximum length is 63 characters across all platforms.

There is no NX-OS equivalent; the command is identical. In IOS-XE, the hostname is also used in the 'show license' output and in call-home messages. Always ensure the hostname is unique within the network to avoid confusion in management systems.

Related Commands

Practice for the CCNA 200-301

Test your knowledge with practice questions covering all CCNA 200-301 exam domains.

Practice CCNA 200-301 Questions