Okay, here's how to create a new user on an existing Ubuntu VM running on Azure using the Azure Portal, along with important considerations and best practices:
**Important: The most secure and recommended way is to connect to the VM via SSH and execute the user creation commands directly within the VM. Using custom script extensions directly from the Portal to create users can introduce complexities and security risks (e.g., storing passwords in scripts). This method below provides a general guidance, but use caution. **
Steps (using custom script extension in the Azure Portal):
In the left-hand menu of your VM's page, under "Settings," find and select "Extensions + applications".
Click "+ Add".
Search for and select "Custom Script Extension". Click "Next".
Configure the Custom Script Extension:
Name: Give your extension a descriptive name (e.g., "CreateNewUser").
Script file: This is where things get a little more involved. You have two basic options:
Option 1: Upload a Script:
Create a shell script (e.g., create_user.sh) on your local machine with the user creation commands (see example below).
Upload the script file here.
Option 2: Enter the Script inline:
You can enter the script directly in the "Inline Script" box. However, this is generally NOT recommended if you need to provide a password, as it can expose the password within the Azure Portal configuration.
Arguments (Optional): If your script takes arguments (e.g., username, password), specify them here. Again, be very careful about including passwords directly in the arguments.
Advanced Settings (Important):
Protected Settings: This is CRITICAL if you absolutely must pass a password in your script. The "Protected Settings" allow you to encrypt values. However, even using protected settings, consider alternative password management strategies like SSH keys (explained below). The structure of the protected settings is a JSON object. Example:
Timeout Settings: Set a reasonable timeout (e.g., 300 seconds).
Click "Review + create" then "Create".
Monitor the Deployment:
After creating the extension, monitor its status in the "Extensions + applications" section of your VM. It should show as "Succeeded" or "Failed". If it failed, check the logs for details.
Example create_user.sh Script (with caution about passwords):
bash
#!/bin/bash
# Script to create a new user on Ubuntu
# Set username and password (DANGEROUS: Avoid storing passwords directly!)
username="$1"
password="$2"
# Check if the username is provided
if [ -z "$username" ]; then
echo "Error: Username is required."
exit 1
fi
# Create the user
sudo useradd -m "$username"
# Set the password (highly discouraged - use SSH keys instead!)
echo "$username:$password" | sudo chpasswd
# Add the user to the sudo group (optional)
sudo usermod -aG sudo "$username"
echo "User '$username' created successfully."
Important Security Considerations and Recommendations:
AVOID PASSWORDS IN SCRIPTS: Seriously, don't do it if you can avoid it. Storing passwords in plain text, even in protected settings, is a security risk.
Use SSH Keys (Strongly Recommended): The best way to create users is to generate an SSH key pair on your local machine and add the public key to the user's ~/.ssh/authorized_keys file. This allows passwordless SSH login. The script would look something like this:
bash
#!/bin/bash
username="$1"
public_key="$2" # Passed as an argument or read from a file
if [ -z "$username" ] || [ -z "$public_key" ]; then
echo "Error: Username and public key are required."
exit 1
fi
sudo useradd -m "$username"
sudo mkdir -p /home/"$username"/.ssh
echo "$public_key" | sudo tee -a /home/"$username"/.ssh/authorized_keys
sudo chown -R "$username":"$username" /home/"$username"/.ssh
sudo chmod 700 /home/"$username"/.ssh
sudo chmod 600 /home/"$username"/.ssh/authorized_keys
You would pass the username and the content of the public key file (~/.ssh/id_rsa.pub on your local machine) as arguments to the custom script extension. Remember to properly format the public key for the authorized_keys file.
Restrict Access: After creating the user, review the VM's Network Security Group (NSG) rules to ensure that only necessary ports are open.
Least Privilege: Only grant the user the minimum necessary permissions. Avoid adding them to the sudo group unless absolutely required.
Regular Audits: Regularly review user accounts on your VMs and remove any that are no longer needed.
Consider Configuration Management: For more complex deployments, consider using tools like Ansible, Chef, or Puppet to manage user accounts and system configuration in a more automated and consistent way. These tools are designed for managing infrastructure at scale and can greatly improve security and reliability.
Steps using SSH Connection and local user creation:
Connect to the VM: Using an SSH client (e.g., PuTTY on Windows, or the ssh command in a terminal on Linux/macOS), connect to your Ubuntu VM. You'll need the VM's public IP address (from the Azure Portal) and the username and password (or SSH key) you used when creating the VM.
bash
ssh <username>@<public_ip_address>
Create User:
bash
sudo adduser <new_username>
Ubuntu will prompt you to enter and confirm the password for the new user, and then ask for some optional information (full name, room number, etc.). You can leave these blank.
Add User to Sudo Group (Optional): If the user needs administrative privileges, add them to the sudo group:
bash
sudo usermod -aG sudo <new_username>
Verify: Try switching to the new user:
bash
su - <new_username>
Or, open a new SSH connection using the new user's credentials.
Explanation:
sudo adduser <new_username>: This is the standard command to create a new user on Ubuntu. It creates the user account, sets up a home directory, and prompts you to set a password.
sudo usermod -aG sudo <new_username>: This command adds the new user to the sudo group, allowing them to execute commands with root privileges using sudo.
Why SSH is Recommended:
Security: It avoids storing sensitive information (passwords) in the Azure Portal configuration.
Simplicity: It's a straightforward process that leverages standard Ubuntu commands.
Control: You have direct control over the user creation process and can easily customize it.
Remember to prioritize security when managing user accounts on your Azure VMs. Using SSH keys and avoiding passwords in scripts are crucial steps in securing your infrastructure.