How to Create an OpenVZ Container via Bash Script?
Introduction:
OpenVZ is a container-based virtualization solution that enables multiple isolated Linux containers to run on a single physical server. This guide provides a Bash script to automate the creation of an OpenVZ container.
Prerequisites:
- OpenVZ is installed on the server.
- Sufficient disk space and resources for the container
- A pre-downloaded OpenVZ OS template
Steps to Create an OpenVZ Container via Bash Script:
Step 1: Create a Bash Script
Create a file named create_container.sh
and make it executable.
#!/bin/bash read -p “Enter Container ID:” CT_ID read -p “Enter OS Template (e.g., ubuntu-20.04-x86_64):” OS_TEMPLATE read -p “Enter RAM size (e.g., 1G, 2G):” RAM read -p “Enter CPU cores: ” CPU_CORES read -p “Enter Disk size (e.g., 10G):” DISK_SIZE read -p “Enter IP Address:” IP_ADDR read -p “Enter the Hostname:” HOSTNAME #Create the container echo “Started Creating Container” vzctl create $CT_ID –ostemplate $OS_TEMPLATE #Save the Onboot vzctl set $CT_ID –save –onboot yes #Asign the IP address vzctl set $CT_ID –save –ipadd $IP_ADDR #Set CPUs for the container vzctl set $CT_ID –save –cpus $CPU_CORES #Set RAM for the container vzctl set $CT_ID –save –ram $RAM #Set DISK SPACE for the container vzctl set $CT_ID –save –diskspace $DISK_SIZE #Set Name server vzctl set $CT_ID –save –nameserver 8.8.8.8 –nameserver 8.8.4.4 #Set up the hostname vzctl set $CT_ID –save –hostname $HOSTNAME #Start the Container vzctl start $CT_ID echo “Container $CT_ID created and started successfully.” echo “Username: root” echo “IP address: $IP_ADDR” |
Step 2: Make the Script Executable
Run the following command to give execution permissions to the script:
chmod +x create_container.sh |
Step 3: Execute the Script
Run the script to create and start the container:
./create_container.sh |
Enter configurations

Conclusion:
This Bash script automates the process of creating an OpenVZ container with the specified configurations. Adjust the parameters according to the requirements