|
| 1 | +# Creating a server |
| 2 | + |
| 3 | +This tutorial covers the process of creating a server using the Hetzner Cloud CLI. It includes creating an SSH Key, |
| 4 | +uploading it, creating a Server and then connecting to it via SSH. |
| 5 | + |
| 6 | +## Prerequisites |
| 7 | + |
| 8 | +- A functioning installation of the [hcloud CLI](setup-hcloud-cli.md), with a valid active context. |
| 9 | + |
| 10 | +## 1. Create an SSH Key |
| 11 | + |
| 12 | +### 1.1 Generate an SSH Key |
| 13 | + |
| 14 | +While an SSH key is not strictly required to create a server, it is highly recommended for secure access. |
| 15 | +If you don't have an SSH key yet, you can generate one using the following command: |
| 16 | + |
| 17 | +```bash |
| 18 | +ssh-keygen -t ed25519 -f ~/.ssh/hcloud |
| 19 | +``` |
| 20 | + |
| 21 | +Your private key will now be located at `~/.ssh/hcloud`. **Do not share your private key with anyone!** |
| 22 | + |
| 23 | +### 1.2 Upload the SSH Key |
| 24 | + |
| 25 | +You can upload your SSH key to Hetzner Cloud using the following command: |
| 26 | + |
| 27 | +```bash |
| 28 | +hcloud ssh-key create --name my-ssh-key --public-key-from-file ~/.ssh/hcloud.pub |
| 29 | +``` |
| 30 | + |
| 31 | +**Pro Tip**: You can set this SSH key as the default SSH key for your context using the following command: |
| 32 | + |
| 33 | +```bash |
| 34 | +hcloud config set default-ssh-keys my-ssh-key |
| 35 | +``` |
| 36 | + |
| 37 | +## 2. Create a Server |
| 38 | + |
| 39 | +### 2.1 Pick a Server Type |
| 40 | + |
| 41 | +Before creating a server, you need to choose a server type. You can list all available server types using the following command: |
| 42 | + |
| 43 | +```bash |
| 44 | +hcloud server-type list |
| 45 | +``` |
| 46 | + |
| 47 | +For this example we will use the `cpx11` server type. |
| 48 | +You can view the details of this server type using the following command: |
| 49 | + |
| 50 | +```bash |
| 51 | +hcloud server-type describe cpx11 |
| 52 | +``` |
| 53 | + |
| 54 | +### 2.2 Pick an Image |
| 55 | + |
| 56 | +You need to choose an image for your server. You can list all available images using the following command: |
| 57 | + |
| 58 | +```bash |
| 59 | +hcloud image list |
| 60 | +``` |
| 61 | + |
| 62 | +There are many images available, including various Linux distributions and pre-configured app images. |
| 63 | +For this example we will use the `ubuntu-24.04` image. |
| 64 | + |
| 65 | +### 2.3 Pick a Location (Optional) |
| 66 | + |
| 67 | +You can choose a location for your server. You can list all available locations using the following command: |
| 68 | + |
| 69 | +```bash |
| 70 | +hcloud location list |
| 71 | +``` |
| 72 | + |
| 73 | +If you don't specify a location, one will be chosen for you. This is what we will do in this example. |
| 74 | + |
| 75 | +### 2.4 Create the Server |
| 76 | + |
| 77 | +Now you can create the server using the following command: |
| 78 | + |
| 79 | +```bash |
| 80 | +hcloud server create --name my-server --type cpx11 --image ubuntu-24.04 --ssh-key my-ssh-key |
| 81 | +``` |
| 82 | + |
| 83 | +If you set the SSH key as the default SSH key for your context, you can omit the `--ssh-key` flag. |
| 84 | +After the server was created, you will see information about the server, including its IP address. |
| 85 | + |
| 86 | +## 3. Connect to the Server |
| 87 | + |
| 88 | +You can connect to the server using SSH. The CLI contains a built-in utility to do this: |
| 89 | + |
| 90 | +```bash |
| 91 | +hcloud server ssh my-server -i ~/.ssh/hcloud |
| 92 | +``` |
| 93 | + |
| 94 | +This command will open an SSH connection to the server using the private key you generated earlier. |
| 95 | + |
| 96 | +## 4. Clean Up |
| 97 | + |
| 98 | +After you are done with the server, you can delete it using the following command: |
| 99 | + |
| 100 | +```bash |
| 101 | +hcloud server delete my-server |
| 102 | +``` |
| 103 | + |
| 104 | +You can also delete the SSH key using the following command: |
| 105 | + |
| 106 | +```bash |
| 107 | +hcloud ssh-key delete my-ssh-key |
| 108 | +``` |
0 commit comments