How to rename LXD / LXC container

How do I rename an LXD managed LXC container under a Linux operating system?

You can move or rename containers within or in between lxd instances. In this quick tutorial, I will show how to rename an LXD managed LXC container using lxc command line.

How to rename a local container

Let us say you want to rename a local container named file-server to debian-wheezy. The syntax is:
lxc move {old-lxc-name} {new-lxc-name}
Please note that renaming of running container not allowed. So first stop the container, enter:
$ lxc stop file-server
Now rename it:
$ lxc move file-server debian-wheezy
Start it:
$ lxc start debian-wheezy
Verify it:
$ lxc info debian-wheezy
Sample outputs:

Fig.01: Rename a local container

Login to debian-wheezy LXD container:
$ lxc exec debian-wheezy bash
Change hostname and updated /etc/hosts if you want:
# echo 'debian-wheezy' > /etc/hostname
# sed -i 's/file-server/debian-wheezy/g' /etc/hosts
# exit
$ lxc restart debian-wheezy

Verify new changes:

$ lxc exec debian-wheezy bash
[email protected]:~# cat /etc/os-release

Sample outputs:

PRETTY_NAME="Debian GNU/Linux 7 (wheezy)"
NAME="Debian GNU/Linux"
VERSION="7 (wheezy)"

Also you can move a container between two hosts, renaming it if destination name differs. The syntax is:
$ lxc move [<remote>:][<remote>:][<destination container>]
$ lxc stop server1:foo
$ lxc move server1:foo server2:bar

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.