How to rename LXD / LXC container

Posted on 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
[email protected]:~# cat /etc/os-release

Sample outputs:

PRETTY_NAME="Debian GNU/Linux 7 (wheezy)"
NAME="Debian GNU/Linux"
VERSION_ID="7"
VERSION="7 (wheezy)"
ID=debian
ANSI_COLOR="1;31"
HOME_URL="http://www.debian.org/"
SUPPORT_URL="http://www.debian.org/support/"
BUG_REPORT_URL="http://bugs.debian.org/"

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 and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

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.

Leave a Comment