How to rename LXD / LXC container

in Categories , , , , , , last updated February 15, 2017

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
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
root@debian-wheezy:~# 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: Vivek Gite

The author is the creator of nixCraft 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.

Share this on (or read 1 comments/add one below):

1 comment

  1. To my knowledge ‘lxc’ is a Debian/Ubunto only tool. You should stop using it if you want your scripts to be easily usable on CentOS/RHEL. Or provide a link to lxc so that someone can package it for CentOS/RHEL.

    Have a question? Post it on our forum!