How to create and connect to a Linux VM using a Public Key
Tags: azure, linux, techtalks, virtualmachine
Published: 2024-06-20 15:26:37

[](url)Creating and connecting to a Linux virtual machine (VM) on Azure using SSH public key authentication involves several steps. Here’s a detailed guide to help you through the process:

### Step 1: Sign in to Azure Portal
1. **Sign in**: Go to the [Azure portal](https://portal.azure.com) and sign in with your Azure account.

### Step 2: Create a Resource Group (if needed)
1. **Resource Group**: If you don’t have a resource group where you want to deploy the VM, create one:
   - Click on **Resource groups** in the Azure portal's left-hand menu.
   - Click **+ Add** to create a new resource group.
   - Provide a name, choose a region, and click **Review + create** then **Create**.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w7vzju0edui32ddat6g9.png)
### Step 3: Create a Virtual Machine
1. **Create VM**: Now, create a new virtual machine:
   - In the Azure portal, click **+ Create a resource** at the top-left corner.
   - Search for **Virtual machine** and click **Create**.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f180z58pdzq2ylef81jb.png)
2. **Basics**:
   - **Subscription**: Choose your Azure subscription.
   - **Resource Group**: Select the resource group you created or an existing one.
   - **Virtual Machine Name**: Provide a name for your VM.
   - **Region**: Choose the Azure region where you want to deploy your VM.
   - **Image**: Choose a Linux distribution (e.g., Ubuntu, CentOS) from the list.
   - **Size**: Select an appropriate VM size based on your requirements.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vphkgqc1y06ecovxgphh.png)
3. **Administrator Account**:
   - **Username**: Choose a username for the administrator account (e.g., `azureuser`).
   - **Authentication type**: Select **SSH public key**.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lb1ve6t1uyc8oh258com.png)
   - **SSH public key**: Paste your SSH public key. If you don't have one, generate it using `ssh-keygen` on your local machine (`ssh-keygen -t rsa -b 4096`).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tzhttma55tqycgwcvtit.jpg)

4. **Disks** and other settings:
   - Customize the disk configuration, networking, management, and monitoring settings as per your requirements.

5. **Review + create**:
   - Review your configuration settings.
   - Click **Create** to start deploying the VM.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fcvq1vxwid4n487uzyox.png)
### Step 4: Connect to Your Virtual Machine
1. **SSH Connection**: Once the VM is deployed, you can connect to it using SSH:
   - In the Azure portal, go to **Virtual machines** and select your VM.
   - Under **Connect**, click on **SSH**.
   - Use the SSH command provided to connect to your VM from your local terminal or SSH client.
  - Another step is to go to command prompt,run as an administrator and type the SSH command shown below to connect.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7wd0lwnmycuq1bwxl8sm.png)
   Example SSH command:
   ```bash
   ssh -i "/path/to/your/private-key-file.pem" username@your-vm-public-ip
   ```

### Step 5: Manage and Monitor Your VM
1. **Management**: Use Azure portal or Azure CLI to manage and monitor your VM:
   - Start, stop, restart your VM.
   - Scale up or down to change VM sizes.
   - Monitor performance and set up alerts.

### Step 6: Delete Your VM (if no longer needed)
1. **Clean up**: If you no longer need the VM, delete it to avoid incurring charges:
   - In the Azure portal, go to **Virtual machines**, select your VM, and click **Delete**.


### Additional Tips:
- **Azure CLI**: You can also use Azure CLI for VM deployment and management if you prefer command-line tools.
- **Templates**: Consider using Azure Resource Manager (ARM) templates for consistent and repeatable deployments.

_By following these steps, you can create and connect to a Linux VM on Azure using SSH public key authentication securely. Adjust settings based on your specific needs and security requirements._