|
| 1 | +--- |
| 2 | +title: Prepare machine key for contained availability groups on Linux and containers |
| 3 | +description: Learn how to prepare a machine key in a contained availability group, for SQL Server on Linux and containers. |
| 4 | +author: rwestMSFT |
| 5 | +ms.author: randolphwest |
| 6 | +ms.date: 09/05/2024 |
| 7 | +ms.service: sql |
| 8 | +ms.subservice: linux |
| 9 | +ms.topic: how-to |
| 10 | +ms.custom: |
| 11 | + - linux-related-content |
| 12 | +--- |
| 13 | + |
| 14 | +# Prepare machine key for contained availability groups on Linux and containers |
| 15 | + |
| 16 | +[!INCLUDE [SQL Server - Linux](../includes/applies-to-version/sql-linux.md)] |
| 17 | + |
| 18 | +This article provides an example of how to prepare a machine key for [!INCLUDE [ssnoversion-md](../includes/ssnoversion-md.md)] running on Linux in a contained availability group (AG). |
| 19 | + |
| 20 | +A [contained AG](../database-engine/availability-groups/windows/contained-availability-groups-overview.md) is an availability group that supports: |
| 21 | + |
| 22 | +- managing metadata objects (users, logins, permissions, [!INCLUDE [ssnoversion-md](../includes/ssnoversion-md.md)] Agent jobs, and so on) at the AG level in addition to the instance level. |
| 23 | + |
| 24 | +- specialized contained system databases within the AG. |
| 25 | + |
| 26 | +The examples in this article target [!INCLUDE [ssnoversion-md](../includes/ssnoversion-md.md)] in Linux containers, but you can use the same steps for [!INCLUDE [ssnoversion-md](../includes/ssnoversion-md.md)] on Linux, running on physical machines, virtual machines, and in a Kubernetes-based deployment. |
| 27 | + |
| 28 | +> [!CAUTION] |
| 29 | +> These instructions should only be used for *contained availability groups*. When you configure a contained AG with a common machine key across all replicas, first ensure there's no existing encryption hierarchy (for example, transparent data encryption, column-level encryption, or any other security-related feature that requires key management). Changing the machine key could break the encryption and cause data loss. After configuration, avoid creating or modifying the encryption hierarchy for security reasons. |
| 30 | +
|
| 31 | +## Overview of the machine key |
| 32 | + |
| 33 | +In [!INCLUDE [ssnoversion-md](../includes/ssnoversion-md.md)] on Linux, the machine key plays a vital role in securing communication and data. |
| 34 | +The following table describes its primary functions. |
| 35 | + |
| 36 | +| Function | Description | |
| 37 | +| --- | --- | |
| 38 | +| **Encryption and decryption** | The machine key is used to encrypt and decrypt data exchanged between nodes in an AG | |
| 39 | +| **Authentication** | It helps in authenticating communication between the primary and secondary replicas in an AG | |
| 40 | +| **Security** | The machine key and associated certificates must be protected to prevent unauthorized access | |
| 41 | + |
| 42 | +When you work with [!INCLUDE [sssql22-md](../includes/sssql22-md.md)] on Linux and contained AGs, you must synchronize the machine keys between [!INCLUDE [ssnoversion-md](../includes/ssnoversion-md.md)] replicas. This process allows the service master key (SMK) in the contained `master` database to be used for decrypt and encrypt operations. |
| 43 | + |
| 44 | +## Prepare SQL Server container on standalone host to run custom machine key |
| 45 | + |
| 46 | +The following instructions show an example of generating a new machine key in `base64` format. |
| 47 | + |
| 48 | +1. Generate a machine key file using OpenSSL (into a file called `machine-key.bin`) using the following script. |
| 49 | + |
| 50 | + ```bash |
| 51 | + openssl rand -out machine-key.bin 44 |
| 52 | + printf '\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' | dd of=machine-key.bin bs=1 seek=0 count=12 conv=notrunc |
| 53 | + cat machine-key.bin | base64 |
| 54 | + ``` |
| 55 | + |
| 56 | + The previous script performs these steps: |
| 57 | + |
| 58 | + - Create a 44-byte binary file called `machine-key.bin`, containing random data. |
| 59 | + - Overwrite the first 12 bytes with a fixed header. This might change in the future. |
| 60 | + - Encode `machine-key.bin` in `base64` format and print it to the console. |
| 61 | + |
| 62 | +1. Run the container by mounting the file to the machine key of the [!INCLUDE [ssnoversion-md](../includes/ssnoversion-md.md)] container. |
| 63 | + |
| 64 | + ```bash |
| 65 | + docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=yourpassword" -p 14331:1433 --name sqlcontainer --hostname sqlcontainer -v ~/machine-key.bin:/var/opt/mssql/secrets/machine-key -d mcr.microsoft.com/mssql/server:2022-latest |
| 66 | + ``` |
| 67 | + |
| 68 | +1. Verify that the `machine-key.bin` on the host machine is the same as the `machine-key.bin` used by the [!INCLUDE [ssnoversion-md](../includes/ssnoversion-md.md)] container deployed in the previous step. |
| 69 | + |
| 70 | + ```bash |
| 71 | + cat machine-key.bin | base64 |
| 72 | + ``` |
| 73 | + |
| 74 | + Here's the sample output: |
| 75 | + |
| 76 | + ```output |
| 77 | + AQAAAAAAAAAAAAAA//////////////////////////////////////////8= |
| 78 | + ``` |
| 79 | + |
| 80 | +1. Inside the container, verify the key. First, connect to an interactive terminal in the container. |
| 81 | + |
| 82 | + ```bash |
| 83 | + docker exec -it sqlcontainer "bash" |
| 84 | + ``` |
| 85 | + |
| 86 | + Then, verify the key. |
| 87 | + |
| 88 | + ```bash |
| 89 | + cat machine-key | base64 |
| 90 | + ``` |
| 91 | + |
| 92 | + Here's the sample output: |
| 93 | + |
| 94 | + ```output |
| 95 | + AQAAAAAAAAAAAAAA//////////////////////////////////////////8= |
| 96 | + ``` |
| 97 | + |
| 98 | +You can use the same steps across all the [!INCLUDE [ssnoversion-md](../includes/ssnoversion-md.md)] container deployments that you intend to use as replicas in your contained AG. You must use the same machine key, and don't generate different machine keys for each of the [!INCLUDE [ssnoversion-md](../includes/ssnoversion-md.md)] container deployments. |
| 99 | + |
| 100 | +## Related content |
| 101 | + |
| 102 | +- [Deploy and connect to SQL Server Linux containers](sql-server-linux-docker-container-deployment.md) |
| 103 | +- [Quickstart: Deploy a SQL Server Linux container to Kubernetes using Helm charts](sql-server-linux-containers-deploy-helm-charts-kubernetes.md) |
0 commit comments