Use SSH keys to connect to Linux VMs - Azure Virtual Machines (2024)

  • Article
  • 5 minutes to read

Applies to: ✔️ Linux VMs ✔️ Flexible scale sets

This article is for Windows users who want to create and use secure shell (SSH) keys to connect to Linux virtual machines (VMs) in Azure. You can also generate and store SSH keys in the Azure portal to use when creating VMs in the portal.

To use SSH keys from a Linux or macOS client, see the quick steps. For a more detailed overview of SSH, see Detailed steps: Create and manage SSH keys for authentication to a Linux VM in Azure.

Overview of SSH and keys

SSH is an encrypted connection protocol that allows secure sign-ins over unsecured connections. SSH is the default connection protocol for Linux VMs hosted in Azure. Although SSH itself provides an encrypted connection, using passwords with SSH still leaves the VM vulnerable to brute-force attacks. We recommend connecting to a VM over SSH using a public-private key pair, also known as SSH keys.

The public-private key pair is like the lock on your front door. The lock is exposed to the public, anyone with the right key can open the door. The key is private, and only given to people you trust because it can be used to unlock the door.

When you connect to your Linux VM, the VM tests the SSH client to make sure it has the correct private key. If the client has the private key, it's granted access to the VM.

Depending on your organization's security policies, you can reuse a single key pair to access multiple Azure VMs and services. You do not need a separate pair of keys for each VM.

Your public key can be shared with anyone, but only you (or your local security infrastructure) should have access to your private key.

Supported SSH key formats

Azure currently supports SSH protocol 2 (SSH-2) RSA public-private key pairs with a minimum length of 2048 bits. Other key formats such as ED25519 and ECDSA are not supported.

SSH clients

Recent versions of Windows 10 include OpenSSH client commands to create and use SSH keys and make SSH connections from PowerShell or a command prompt.

You can also use Bash in the Azure Cloud Shell to connect to your VM. You can use Cloud Shell in a web browser, from the Azure portal, or as a terminal in Visual Studio Code using the Azure Account extension.

You can also install the Windows Subsystem for Linux to connect to your VM over SSH and use other native Linux tools within a Bash shell.

Create an SSH key pair

The easiest way to create and manage your SSH keys is to use the portal to create and store them for reuse.

You can also create key pairs with the Azure CLI with the az sshkey create command, as described in Generate and store SSH keys.

To create an SSH key pair on your local computer using the ssh-keygen command from PowerShell or a command prompt, type the following:

ssh-keygen -m PEM -t rsa -b 2048

Enter a filename, or use the default shown in parenthesis (for example C:\Users\username/.ssh/id_rsa). Enter a passphrase for the file, or leave the passphrase blank if you do not want to use a passphrase.

Create a VM using your key

To create a Linux VM that uses SSH keys for authentication, provide your SSH public key when creating the VM.

Using the Azure CLI, you specify the path and filename for the public key using az vm create and the --ssh-key-value parameter.

az vm create \ --resource-group myResourceGroup \ --name myVM \ --image UbuntuLTS\ --admin-username azureuser \ --ssh-key-value ~/.ssh/id_rsa.pub

With PowerShell, use New-AzVM and add the SSH key to the VM configuration using`. For an example, see Quickstart: Create a Linux virtual machine in Azure with PowerShell.

If you do a lot of deployments using the portal, you might want to upload your public key to Azure, where it can be easily selected when creating a VM from the portal. For more information, see Upload an SSH key.

Connect to your VM

With the public key deployed on your Azure VM, and the private key on your local system, SSH to your VM using the IP address or DNS name of your VM. Replace azureuser and 10.111.12.123 in the following command with the administrator user name, the IP address (or fully qualified domain name), and the path to your private key:

ssh -i ~/.ssh/id_rsa azureuser@10.111.12.123

If you have never connected to this VM before you will be asked to verify the hosts fingerprint. It is tempting to simply accept the fingerprint presented, however, this exposes you to a possible person in the middle attack. You should always validate the hosts fingerprint. You only need to do this on the first time you connect from a client. To obtain the host fingerprint via the portal use the Run Command with the following: ssh-keygen -lf /etc/ssh/ssh_host_ecdsa_key.pub | awk '{print $2}'.

Use SSH keys to connect to Linux VMs - Azure Virtual Machines (1)

To run the command using CLI, use the az vm run-command invoke command.

If you configured a passphrase when you created your key pair, enter the passphrase when prompted.

If the VM is using the just-in-time access policy, you need to request access before you can connect to the VM. For more information about the just-in-time policy, see Manage virtual machine access using the just in time policy.

Next steps

Use SSH keys to connect to Linux VMs - Azure Virtual Machines (2024)

FAQs

Can you use SSH keys to connect to an Azure virtual machine? ›

Azure currently supports SSH protocol 2 (SSH-2) RSA public-private key pairs with a minimum length of 2048 bits. Other key formats such as ED25519 and ECDSA are not supported.

How to connect Linux VM using SSH? ›

Setting up SSH access to virtual machines
  1. Download and install an SSH client on your local machine. Linux and Mac: OpenSSH. ...
  2. Find the VM IP address and private key. ...
  3. Connect to the VM using your SSH client. ...
  4. Optional: After you connect to your VM, you can gain full administrative authority by switching to the root user.

How to generate SSH key for Azure Linux VM? ›

Generate keys automatically during deployment

If you use the Azure CLI to create your VM, you can optionally generate both public and private SSH key files by running the az vm create command with the --generate-ssh-keys option. The keys are stored in the ~/. ssh directory.

Which SSH key is stored in Azure for Linux virtual machines? ›

Azure currently supports SSH protocol 2 (SSH-2) RSA public-private key pairs with a minimum length of 2048 bits. Other key formats such as ED25519 and ECDSA are not supported.

How do I connect to Azure Linux VM with private IP? ›

Connect to VM

On the Bastion Connect page, for IP address, enter the private IP address of the target VM. Adjust your connection settings to the desired Protocol and Port. Enter your credentials in Username and Password. Select Connect to connect to your virtual machine.

Can you RDP to Azure Linux VM? ›

Linux Support for Remote Desktop (RDP)

Normally, when you setup a Linux VM (perhaps Ubuntu Server) in Microsoft Azure, you can only connect to and remote into it via SSH. The reason this differs from a Windows VM in Azure; as Linux VMs do not support Remote Desktop (RDP) natively.

How do I connect to Azure VM without RDP? ›

Another method of connection to azure VM is Bastion. Azure Bastion is a service you deploy that lets you connect to a virtual machine using your browser and the Azure portal.

How do I connect to Azure Linux VM using PuTTY? ›

Open PuTTY:
  1. For Connection type, make sure the SSH radio button is selected.
  2. In the Host Name field, enter azureuser@<public ip> (your admin username and IP will vary)
  3. On the left, expand the SSH section, and click on Auth.
  4. Click on Browse to look for your private key (. ...
  5. To launch the SSH session, click Open.

What are all the ways to connect to the VMS in Azure? ›

Connect to the virtual machine
  1. Go to the Azure portal to connect to a VM. ...
  2. Select the virtual machine from the list.
  3. At the beginning of the virtual machine page, select Connect.
  4. On the Connect to virtual machine page, select RDP, and then select the appropriate IP address and Port number.
Jul 7, 2022

Which Linux command is used to connect to Azure files? ›

Azure file shares can be mounted in Linux distributions using the SMB kernel client. The recommended way to mount an Azure file share on Linux is using SMB 3.1. 1.

How do I SSH into a Linux virtual machine from Windows? ›

To configure a VM for SSH access
  1. Verify that port 22 is open on the VM operating system firewall.
  2. Install and run an SSH server. Example: OpenSSH on an Ubuntu VM. Install open SSH: sudo apt-get install openssh-server. Confirm that SSH daemon ( sshd ) is running: ps -aef | grep sshd. Try to connect: ssh localhost.

How do I connect to a Linux virtual machine remotely? ›

Logging into a Linux Virtual Machine via SSH with a Username and Password. Using the console or command line, use the ssh user@host command to access the server. You will be prompted for your password: That's all there is to it really, so short and sweet!

How do I connect to a Linux virtual machine? ›

How to Connect to a Linux VM using Putty
  1. Start PuTTy.
  2. Fill in the host name or IP address of your VM from the Azure portal:
  3. Before selecting Open, click Connection > SSH > Auth tab. Browse to and select your PuTTY private key (.ppk file):
  4. Click Open to connect to your VM.
Sep 30, 2020

How do I enable SSH key authentication in Linux? ›

Procedure
  1. Use the ssh-keygen tool to create a key pair. ...
  2. Validate that the keys were generated. ...
  3. Enable key-based authentication in the /etc/ssh directory on the SSH server. ...
  4. Copy the rsa. ...
  5. If you have an existing authorized_keys file, edit it to remove any no-pty restrictions.

How SSH key authentication works in Linux? ›

Once an SSH server receives a public key from a user and considers the key trustworthy, the server marks the key as authorized in its authorized_keys file. Such keys are called authorized keys. A private key that remains (only) with the user. The possession of this key is proof of the user's identity.

How to add SSH key to VM? ›

On the Compute > Virtual machines > SSH keys tab, click Add key. On the SSH keys screen, click Add key. In the Add SSH key window, specify a key name and copy the key value from the generated public key located in /root/. ssh/id_rsa.

Where do I put SSH keys in Linux? ›

The public key is uploaded to a remote server that you want to be able to log into with SSH. The key is added to a special file within the user account you will be logging into called ~/. ssh/authorized_keys .

Where is the SSH key on Linux? ›

By default, the private key is stored in ~/. ssh/id_rsa and the public key is stored in ~/. ssh/id_rsa.

Where is the SSH public key on Linux? ›

By default, your private and public keys are saved in your ~/. ssh/id_rsa and ~/. ssh/id_rsa. pub files, respectively.

How do I make a Linux VM accessible from outside? ›

Under Settings -> Network -> Adapter1 change Attached to: Bridged Adapter and under Name: select the host network interface (e.g. eth0 ). That effectively exposes the guest (VM) Network Adapter to the same network as your host's eth0 . The VM can then get its own public IP (e.g. 80.80.

How do I connect to Azure VM remotely? ›

Log in by using Azure AD credentials to a Windows VM
  1. Go to the overview page of the virtual machine that has been enabled with Azure AD login.
  2. Select Connect to open the Connect to virtual machine pane.
  3. Select Download RDP File.
  4. Select Open to open the Remote Desktop Connection client.
Jan 5, 2023

How do I make my Azure VM accessible from outside? ›

Arguably, the preferred way to access Azure VM from outside is the Azure Bastion host PaaS service. This is a relatively newer service from Microsoft that allows users to access internal VMs without using an external IP address on the internal virtual machines.

Can you RDP into a Linux EC2? ›

Connecting via RDP

On a Linux machine, connect using vinagre . You'll need to specify the IP address for the EC2 host and the RDP port.

Can you RDP over SSH? ›

A common utility used to tunnel RDP sessions is PuTTY link, known as Plink. It can be used to establish secure shell (SSH) network connections to other systems using arbitrary source and destination ports.

How to use xrdp in Linux? ›

How to Install Remote Desktop (Xrdp) on Ubuntu 18.04
  1. Step 1: Log in to the server with Sudo access. ¶ ...
  2. Step 2: Install XRDP Packages. ...
  3. Step 3: Install your preferred desktop environment. ...
  4. Step 4: Allow RDP port in Firewall. ...
  5. Step 5: Restart the Xrdp application.

How do I access my Azure VM securely? ›

To secure remote access to virtual machines (VMs) that run in an Azure Active Directory Domain Services (Azure AD DS) managed domain, you can use Remote Desktop Services (RDS) and Network Policy Server (NPS). Azure AD DS authenticates users as they request access through the RDS environment.

Do I need public IP to connect to a Azure VM? ›

You don't need Public IPs to access your VMs over RDP/SSH. Additionally, Azure Bastion provides integrated connectivity using RDP/SSH directly from your browser and the Azure portal experience. You don't need an additional client, agent, or piece of software.

How do I access my Azure VM with public IP? ›

Azure portal
  1. Sign in to the Azure portal.
  2. Browse to, or search for the virtual machine that you want to add the public IP address to and then select it.
  3. Under Settings, select Networking, and then select the network interface you want to add the public IP address to, as shown in the following picture:
Oct 28, 2022

How do I log into SSH with a key? ›

  1. Use the Windows search box to find cmd and open the Command Prompt window.
  2. In the prompt, type: ssh-keygen. The command starts the program for generating the key pair. ...
  3. If you set up a specific location for the keys, type in the path now. ...
  4. Enter the passphrase to encrypt the private key.
Aug 10, 2021

How do I log into a server using SSH key? ›

How to Connect via SSH
  1. Open the SSH terminal on your machine and run the following command: ssh your_username@host_ip_address. ...
  2. Type in your password and hit Enter. ...
  3. When you are connecting to a server for the very first time, it will ask you if you want to continue connecting.
Sep 24, 2018

How to login to Azure Linux VM using Username and Password? ›

When you built your VM, you would have created an administrator username and a password, ensure you have those to hand. Next, login to the Azure Portal, click the VM you want to connect to. This will open the Overview blade. From here you can click the Connect button.

How do I log into my Azure virtual machine? ›

Connect to the virtual machine
  1. Go to the Azure portal to connect to a VM. ...
  2. Select the virtual machine from the list.
  3. At the beginning of the virtual machine page, select Connect.
  4. On the Connect to virtual machine page, select RDP, and then select the appropriate IP address and Port number.
Jul 7, 2022

Top Articles
Latest Posts
Article information

Author: Kimberely Baumbach CPA

Last Updated:

Views: 5678

Rating: 4 / 5 (41 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Kimberely Baumbach CPA

Birthday: 1996-01-14

Address: 8381 Boyce Course, Imeldachester, ND 74681

Phone: +3571286597580

Job: Product Banking Analyst

Hobby: Cosplaying, Inline skating, Amateur radio, Baton twirling, Mountaineering, Flying, Archery

Introduction: My name is Kimberely Baumbach CPA, I am a gorgeous, bright, charming, encouraging, zealous, lively, good person who loves writing and wants to share my knowledge and understanding with you.