|
| 1 | +--- |
| 2 | +title: Harden the Linux image to remove sudo users |
| 3 | +description: Learn how to use the Azure CLI to harden the linux image to remove sudo users. |
| 4 | +author: vvenug |
| 5 | +ms.service: virtual-machines |
| 6 | +mms.subservice: confidential-computing |
| 7 | +ms.topic: how-to |
| 8 | +ms.workload: infrastructure |
| 9 | +ms.date: 7/21/2023 |
| 10 | +ms.author: vvenugopal |
| 11 | +ms.custom: devx-track-azurecli |
| 12 | +--- |
| 13 | + |
| 14 | +# Harden the Linux image to remove sudo users |
| 15 | + |
| 16 | +**Applies to:** :heavy_check_mark: Linux Images |
| 17 | + |
| 18 | +This "how to" shows you steps to remove sudo users from the Linux image and deploy a confidential virtual machine (confidential VM) in Azure. |
| 19 | + |
| 20 | +The objective of this article is to create an admin-less Linux image for confidential VM deployments. Removing the guest admin has immense security value, it reduces admin privileges across OS. |
| 21 | + |
| 22 | +Understanding different types of users in Unix/Linux systems: |
| 23 | +- Admin user (sudoer): Regular users with extra permissions. These users can perform certain tasks that modify system configurations. |
| 24 | + |
| 25 | +- Regular user: Regular users are nonadministrative users. They don't have permission to modify system configurations or install system-wide software. |
| 26 | + |
| 27 | +In the context of admin-less Linux images, the aim is to deploy systems without sudo users. |
| 28 | + |
| 29 | +> [!NOTE] |
| 30 | +> The configuration alone does not ensure prevention of users from being added to the sudo group. Any service with root or sudo privileges has the potential to escalate privileges. |
| 31 | +
|
| 32 | +## Prerequisites |
| 33 | + |
| 34 | +- If you don't have an Azure subscription, [create a free Azure account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F) before you begin. |
| 35 | +- An Ubuntu image - you can choose one from the [Azure Marketplace](/azure/virtual-machines/linux/cli-ps-findimage). |
| 36 | + |
| 37 | +### Remove sudo users and prepare a generalized Linux image |
| 38 | + |
| 39 | +The proposed solution results in a Linux image without sudo users. |
| 40 | + |
| 41 | +Steps to create a generalized image that removes the sudo users are as follows: |
| 42 | + |
| 43 | +1. Download an Ubuntu image. |
| 44 | +[Create a custom image for Azure confidential VM](/azure/confidential-computing/how-to-create-custom-image-confidential-vm) |
| 45 | + |
| 46 | +2. Mount the image. |
| 47 | + |
| 48 | + There are several ways to do this [Attach the disk](/azure/virtual-machines/linux/attach-disk-portal?tabs=ubuntu#mount-the-disk:~:text=new%20filesystem%20immediately.-,Mount%20the%20disk,-Create%20a%20directory), the example uses the loop device to mount the image. It can either be a disk attached or a loop device [Mount the image](https://manpages.ubuntu.com/manpages/jammy/man8/mount.8.html). |
| 49 | + |
| 50 | + $imagedevice is the root filesystem's partition on the device that contains the image. |
| 51 | + ``` |
| 52 | + mount /dev/$imagedevice /mnt/dev/$imagedevice |
| 53 | + ``` |
| 54 | +
|
| 55 | + This process is commonly used to access and work with disk images. Here, it is used to remove the sudo users on the Ubuntu image. |
| 56 | +
|
| 57 | +3. Chroot into the vhd filesystem to run the following command, which lists users under the sudo group. |
| 58 | + ``` |
| 59 | + sudo chroot /mnt/dev/$imagedevice/ getent group sudo |
| 60 | + ``` |
| 61 | +
|
| 62 | +4. Validate step 3 by listing out the users in the sudoers.d home directory and in /etc/passwd, /etc/shadow files. |
| 63 | +If there are any users with sudo privileges, they are listed here, |
| 64 | +
|
| 65 | + ``` |
| 66 | + sudo ls /mnt/dev/$imagedevice/etc/sudoers.d |
| 67 | +
|
| 68 | + sudo cat /mnt/dev/$imagedevice/etc/passwd |
| 69 | +
|
| 70 | + sudo cat /mnt/dev/$imagedevice/etc/shadow |
| 71 | + ``` |
| 72 | +
|
| 73 | +5. Remove sudo privileges: Use the deluser command to remove sudo privilege for the user, |
| 74 | + ``` |
| 75 | + sudo chroot /mnt/dev/$imagedevice/ deluser -r [sudo_username] |
| 76 | + ``` |
| 77 | +
|
| 78 | +6. Repeat step 4 to validate that the user has no sudo privilege on the vhd. |
| 79 | +
|
| 80 | +7. Unmount the image. |
| 81 | + ``` |
| 82 | + umount /mnt/dev/$imagedevice |
| 83 | + ``` |
| 84 | +
|
| 85 | +The image prepared does not include any sudo users that can be used for creating the confidential VMs. |
| 86 | +
|
| 87 | +Follow the steps [Create a custom image for Azure confidential VM](/azure/confidential-computing/how-to-create-custom-image-confidential-vm) to create an Azure confidential VM. |
| 88 | +Use the admin-less image in step 4 of [Create a custom image for Azure confidential VM](/azure/confidential-computing/how-to-create-custom-image-confidential-vm) while doing azcopy and the rest of the steps remains the same. |
0 commit comments