How to change hostname of a Cisco router

Here, we will discuss how to change the hostname of a Cisco router or Cisco switch. Changing the hostname on a router or switch is a simple task, and it doesn’t take that amount of time to change. At the end of the post, we will use the netmiko Python script to change the hostname of a Cisco router which is hosted in Sanbox.

Before we start changing the hostname, consider the below points.

A hostname is used to identify the device on the network. Don’t try to configure two devices with the same hostname.
The hostname must be up to 63 characters and should start and end with a letter or digit.
It can have “hyphen” anywhere in the characters, but should not be at the start or end.

This task can be explained using the GNS3 lab. I have a router with a hostname of “R1” that can be changed to “Core_Router”.

R1#

Go into configuration mode and use the command hostname to change the device name.

R1#configure terminal
Enter configuration commands, one per line.  End with CNTL/Z.
R1(config)#

Now, use the command “hostname Core_Router” to change the hostname.

R1(config)#hostname Core_Router
Core_Router(config)#

Press the enter key and you will see that the hostname has changed. The most important step after making changes is to save the configuration. To do that, go to enable mode and use the command “copy run start.”

Core_Router#copy run start
Destination filename [startup-config]?
Building configuration...
[OK]
Core_Router#

You can use the same command to change the hostname on other Cisco devices.

Here, we will use a netmiko Python script to change the hostname of the device. We will display the current hostname of the device and then change it using the send_config_set() function. I am using a Cisco router hosted in a sandbox. The following are the details.

IP Address : 131.226.217.143
Username : developer
Password :C1sco12345

from netmiko import ConnectHandler
net_connect = ConnectHandler(device_type = 'cisco_ios', host='131.226.217.143', username='developer',password ='C1sco12345')
output = net_connect.send_command("show running | include hostname")
hostname = list(output.split(' '))
print('Current hostname of a device:',hostname[1])
print('Changing the hostname.....')
command = ['hostname New_Hostname']
net_connect.send_config_set(command)
output = net_connect.send_command("show running | include hostname")
hostname = list(output.split(' '))
print('New hostname of a device:',hostname[1])

Output

Current hostname of a device: csr1000v-1
Changing the hostname.....
New hostname of a device: New_Hostname

Let’s talk about the above program. The first line imports ConnectHandler from the netmiko library. In the second line, we are using the IP address, username, and password to connect to the device. Then, using the function send_command() to send the command “show running | include hostname” to find the current hostname of a device. We have used the function send_config_set() to change the hostname of a device and print a new hostname 

Advertisement

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s