{"id":8553,"date":"2025-02-01T14:57:08","date_gmt":"2025-02-01T09:27:08","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=8553"},"modified":"2025-02-01T14:57:19","modified_gmt":"2025-02-01T09:27:19","slug":"how-to-create-an-openvz-container-via-bash-script","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/how-to-create-an-openvz-container-via-bash-script\/","title":{"rendered":"How to Create an OpenVZ Container via Bash Script?"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Introduction:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Prerequisites:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>OpenVZ is installed on the server.<\/li>\n\n\n\n<li>Sufficient disk space and resources for the container<\/li>\n\n\n\n<li>A pre-downloaded OpenVZ OS template<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Steps to Create an OpenVZ Container via Bash Script:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 1:<\/strong> Create a Bash Script<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Create a file named <code>create_container.sh<\/code> and make it executable.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>#!\/bin\/bash<br>read -p &#8220;Enter Container ID:&#8221; CT_ID<br>read -p &#8220;Enter OS Template (e.g., ubuntu-20.04-x86_64):&#8221; OS_TEMPLATE<br>read -p &#8220;Enter RAM size (e.g., 1G, 2G):&#8221; RAM<br>read -p &#8220;Enter CPU cores: &#8221; CPU_CORES<br>read -p &#8220;Enter Disk size (e.g., 10G):&#8221; DISK_SIZE<br>read -p &#8220;Enter IP Address:&#8221; IP_ADDR<br>read -p &#8220;Enter the Hostname:&#8221; HOSTNAME<br><br>#Create the container<br>echo &#8220;Started Creating Container&#8221;<br>vzctl create $CT_ID &#8211;ostemplate $OS_TEMPLATE<br><br>#Save the Onboot<br>vzctl set $CT_ID &#8211;save &#8211;onboot yes<br><br>#Asign the IP address<br>vzctl set $CT_ID &#8211;save &#8211;ipadd $IP_ADDR<br><br>#Set CPUs for the container<br>vzctl set $CT_ID &#8211;save &#8211;cpus $CPU_CORES<br><br>#Set RAM for the container<br>vzctl set $CT_ID &#8211;save &#8211;ram $RAM<br><br>#Set DISK SPACE for the container<br>vzctl set $CT_ID &#8211;save &#8211;diskspace $DISK_SIZE<br><br>#Set Name server<br>vzctl set $CT_ID &#8211;save &#8211;nameserver 8.8.8.8 &#8211;nameserver 8.8.4.4<br><br>#Set up the hostname<br>vzctl set $CT_ID &#8211;save &#8211;hostname $HOSTNAME<br><br>#Start the Container<br>vzctl start $CT_ID<br><br>echo &#8220;Container $CT_ID created and started successfully.&#8221;<br><br>echo &#8220;Username: root&#8221;<br>echo &#8220;IP address: $IP_ADDR&#8221;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 2:<\/strong> Make the Script Executable<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Run the following command to give execution permissions to the script:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>chmod +x create_container.sh<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 3:<\/strong> Execute the Script<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Run the script to create and start the container:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>.\/create_container.sh<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Enter configurations<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/image-1.png\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"525\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/image-1-1024x525.png\" alt=\"\" class=\"wp-image-8554\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/image-1-1024x525.png 1024w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/image-1-300x154.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/image-1-768x394.png 768w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/image-1-1536x787.png 1536w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/image-1-585x300.png 585w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/01\/image-1.png 1887w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Conclusion:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This Bash script automates the process of creating an OpenVZ container with the specified configurations. Adjust the parameters according to the requirements<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction: OpenVZ is a container-based virtualization solution that enables multiple isolated Linux containers to run on a single physical server. [&hellip;]<\/p>\n","protected":false},"author":496,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1020],"tags":[],"class_list":["post-8553","post","type-post","status-publish","format-standard","hentry","category-containers-kubernetes","psol-cat-containers-kubernetes"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-2dX","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/8553","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/496"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=8553"}],"version-history":[{"count":0,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/8553\/revisions"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=8553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=8553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=8553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}