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.

ADVERTISEMENTS

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_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

This entry is 7 of 19 in the LXD Tutorial series. Keep reading the rest of the series:
  1. Install LXD container hypervisor on Ubuntu 16.04 LTS
  2. How to install and setup LXC (Linux Container) on Fedora Linux 26
  3. Set up LXD container under KVM or Xen virtual machine
  4. List VM images in LXD (Linux Containers)
  5. Upgrade LXD containers powered by Ubuntu/Debian or CentOS Linux
  6. Auto start LXD containers at boot time in Linux
  7. Command to rename LXD / LXC container
  8. Run commands on Linux Container (LXD) instance at provision launch time
  9. Use LXD (Linux containers) in a shell script to create VM when the cloud instance launches
  10. Move/migrate LXD VM to another host on Linux
  11. Fedora install and set up LXD
  12. CentOS 7.x install and set up LXD server
  13. Install LXD pure-container hypervisor on Ubuntu 18.04 LTS
  14. Create snapshots with lxc command for LXD
  15. Set up and install LXD on CentOS/RHEL 8
  16. Ubuntu 20.04 LTS install and set up LXD
  17. Full backup and restore LXD containers
  18. Disable firewall and NAT rules on the LXD bridge
  19. Delete or remove LXD container using the lxc

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
1 comment… add one
  • Casper Jul 21, 2017 @ 12:09

    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 Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.