diff --git a/config/main.py b/config/main.py index 0dc0a5d41f11..725693600b3b 100755 --- a/config/main.py +++ b/config/main.py @@ -556,6 +556,25 @@ def load_minigraph(): click.echo("Please note setting loaded from minigraph will be lost after system reboot. To preserve setting, run `config save`.") +# +# 'hostname' command +# +@config.command('hostname') +@click.argument('new_hostname', metavar='', required=True) +def hostname(new_hostname): + """Change device hostname without impacting the traffic.""" + + config_db = ConfigDBConnector() + config_db.connect() + config_db.mod_entry('DEVICE_METADATA' , 'localhost', {"hostname" : new_hostname}) + try: + command = "service hostname-config restart" + run_command(command, display_cmd=True) + except SystemExit as e: + click.echo("Restarting hostname-config service failed with error {}".format(e)) + raise + click.echo("Please note loaded setting will be lost after system reboot. To preserve setting, run `config save`.") + # # 'portchannel' group ('config portchannel ...') # diff --git a/doc/Command-Reference.md b/doc/Command-Reference.md index 314717abbbe3..417905fcb360 100644 --- a/doc/Command-Reference.md +++ b/doc/Command-Reference.md @@ -221,6 +221,7 @@ This command lists all the possible configuration commands at the top level. acl ACL-related configuration tasks bgp BGP-related configuration tasks ecn ECN-related configuration tasks + hostname Change device hostname without impacting traffic interface Interface-related configuration tasks interface_naming_mode Modify interface naming mode for interacting... load Import a previous saved config DB dump file. @@ -1636,6 +1637,26 @@ The list of the WRED profile fields that are configurable is listed in the below Go Back To [Beginning of the document](#SONiC-COMMAND-LINE-INTERFACE-GUIDE) or [Beginning of this section](#ECN-Configuration-And-Show-Commands) +# Update Device Hostname Configuration Commands + +This sub-section of commands is used to change device hostname without traffic being impacted. + +**config hostname ** +This command is used to change device hostname without traffic being impacted. + +- Usage: config hostname [OPTIONS] + + Change device hostname without impacting the traffic. + Options: + -?, -h, --help Show this message and exit. + +- Examples: + ``` + admin@lnos-x1-a-csw06:~$ sudo config hostname CSW06 + Running command: service hostname-config restart + Please note loaded setting will be lost after system reboot. To preserve setting, run `config save`. + ``` + # Interface Configuration And Show-Commands ## Interface Show Commands