---
title: "User data for cloud servers"
sidebar_label: "User data"
sidebar_position: 10
description: "How to specify custom configuration parameters when creating a cloud server, examples of user data scripts"
toc_max_heading_level: 3
---

import Tabs from '@theme/Tabs'
import TabItem from '@theme/TabItem'
import {TabItemLabel} from '@selectel/docux/components'
import Formbricks from '@theme/MDXComponents/Formbricks'

# User data for cloud servers

User data is custom configuration parameters for the server operating system. You can use user data to automate server setup. Custom parameters are described as cloud-config scripts (text files with YAML syntax) or as bash scripts. The scripts are automatically encoded in Base64, sent to the server, and executed using the [cloud-init](https://cloud-init.io/) agent during the first operating system boot.

Learn more about cloud-config and bash script formats in the [User data formats](https://cloudinit.readthedocs.io/en/latest/explanation/format.html) section of the cloud-init documentation.

[You can specify user data](#specify-user-data) when creating a cloud server. You can pass operating system configuration parameters and scripts. For example, you can:

* [create a directory and upload files to it](#create-directory);
* [update repositories and install software packages](#update-repositories-and-packages);
* [place SSH keys on the server](#add-ssh-keys);
* [configure the resolv.conf DNS resolver configuration file](#configure-file) ​.

See other examples in the [Cloud config examples](https://cloudinit.readthedocs.io/en/latest/reference/examples.html#cloud-config-examples) section of the cloud-init documentation.

## Specify user data \{#specify-user-data}

You can specify user data only when [creating a cloud server](/cloud-servers/create/create-server.mdx):

* in the Control Panel — at step 4 [additional settings](/cloud-servers/create/create-server.mdx#additional-settings) you can insert a script into the **User data** field, upload a file in `txt`, `gz`, `sh` format, or as a MIME archive. The maximum size of the script with data that is not Base64 encoded is 16 KB;
* via OpenStack CLI and Terraform — only scripts with Base64 encoded data.

You cannot change the script after the server is created.

## User data examples \{#user-data-script-examples}

### Create a directory and upload files to it⁠ \{#create-directory}

<Tabs queryString="create-directory">
  <TabItem value="cloud-config" default>
    <TabItemLabel>
      Cloud-config
    </TabItemLabel>

    Example script to create a directory and download a file to it over a network:

    ```yaml
    #cloud-config
    runcmd:
    - mkdir /run/newdir
    - [ wget, "http://example.com", -O, /run/newdir/index.html ]
    ```
  </TabItem>

  <TabItem value="bash">
    <TabItemLabel>
      Bash script⁠
    </TabItemLabel>

    Example script to create a directory and download a file to it over a network:

    ```bash
    #!/bin/sh
    mkdir /run/newdir
    wget http://example.com -O /run/newdir/index.html
    ```
  </TabItem>
</Tabs>

### Update repositories and install packages \{#update-repositories-and-packages}

<Tabs queryString="update-repositories-and-packages">
  <TabItem value="cloud-config" default>
    <TabItemLabel>
      Cloud-config
    </TabItemLabel>

    Example script to install pwgen and pastebinit:

    ```yaml
    #cloud-config
    package_update: true
    packages:
     - pwgen
     - pastebinit
    ```
  </TabItem>

  <TabItem value="bash">
    <TabItemLabel>
      Bash script⁠
    </TabItemLabel>

    Example script to install docker:

    ```bash
    #!/bin/sh
    apt update
    apt install docker
    ```
  </TabItem>
</Tabs>

### Place SSH keys on the server \{#add-ssh-keys}

<Tabs queryString="add-ssh-keys">
  <TabItem value="cloud-config" default>
    <TabItemLabel>
      Cloud-config
    </TabItemLabel>

    Example script:

    ```yaml
    #cloud-config
    ssh_authorized_keys:
      - ssh-rsa AAAAB3N…V7NZ user1@host
      - ssh-rsa AAAAB3N…NtHw== user2@server
    ```
  </TabItem>

  <TabItem value="bash">
    <TabItemLabel>
      Bash script⁠
    </TabItemLabel>

    Example script:

    ```bash
    #!/bin/sh
    echo "ssh-rsa AAAAB3N…V7NZ user1@host" >> /root/.ssh/authorized_keys
    echo "sssh-rsa AAAAB3N…NtHw== user2@server" >> /root/.ssh/authorized_keys
    ```
  </TabItem>
</Tabs>

### Configure a configuration file \{#configure-file}

<Tabs queryString="configure-file">
  <TabItem value="cloud-config" default>
    <TabItemLabel>
      Cloud-config
    </TabItemLabel>

    example script for the resolv.conf DNS resolver:

    ```yaml
    #cloud-config
    manage_resolv_conf: true
    resolv_conf:
      nameservers: ['4.4.4.4', '8.8.8.8']
      searchdomains:
        - foo.example.com
        - bar.example.com
      domain: example.com
      options:
        rotate: true
        timeout: 1
    ```
  </TabItem>

  <TabItem value="bash">
    <TabItemLabel>
      Bash script⁠
    </TabItemLabel>

    example script for the resolv.conf DNS resolver:

    ```bash
    #!/bin/sh
    cat <<EOF > /etc/resolv.conf
    domain example.com
    nameserver 4.4.4.4
    nameserver 8.8.8.8
    search foo.example.com bar.example.com
    options rotate
    options timeout:1
    EOF
    ```
  </TabItem>
</Tabs>

<Formbricks />
