Creating a Linux Virtual Machine on Azure and Connecting it to Visual Studio Code
Introduction:
Microsoft Azure provides a robust cloud computing platform that allows developers to create, deploy, and manage virtual machines (VMs) in the cloud. In this article, we will guide you through the process of creating a Linux VM on Azure and connecting it to Visual Studio Code for seamless development and testing in a cloud environment.
Step 1: Set Up an Azure Account:
If you don't have an Azure account, you'll need to create one. Visit the Azure portal (https://portal.azure.com/) and sign in or create a new account. Follow the on-screen instructions to set up your subscription.
Step 2: Create a Linux VM on Azure:
Once logged into the Azure portal, follow these steps to create a Linux VM:
Click on the "Create a resource" button.
In the "Search the Marketplace" box, type "Ubuntu Server" (or your preferred Linux distribution) and select the desired version.
Click "Create" and follow the prompts to configure the VM settings, such as name, resource group, username, and authentication type (password or SSH key).
Specify the size of the VM based on your requirements.
Configure networking settings, such as virtual network and subnet.
Review your configuration, then click "Review + create" and "Create" to provision the VM.
After validation, you will click on create and a pop-up will show for you to download the generated key pair.
Azure will now deploy your Linux VM, which may take a few minutes.
Step 3: Connect to the VM using SSH:
Once the VM is deployed, open a terminal on your local machine and use the following SSH command to connect to your Azure VM:
ssh username@public-ip-address
OR
ssh -i <generated key pair download address> username@public-ip-address
Replace
"generated key pair address" with the address at which the generated key pair .pem file was downloaded,
"username" with the username you specified during VM creation and
"public-ip-address" with the public IP address of your Azure VM.
Step 4: Install Visual Studio Code:
If you haven't installed Visual Studio Code on your local machine, download and install it from the official website (https://code.visualstudio.com/).
Step 5: Install the Remote - SSH Extension:
Launch Visual Studio Code and install the "Remote - SSH" extension. This extension allows you to connect to a remote machine over SSH directly from the VS Code interface.
Step 6: Connect Visual Studio Code to Azure VM:
Press
Ctrl + Shift + P
to open the command palette in VS Code.Type "Remote-SSH: Connect to Host" and select it.
Enter the SSH command with the VM's public IP address:
ssh -i <generated key pair address> username@public-ip-address
Replace
- "generated key pair address" with the address at which the generated key pair .pem file was downloaded (e.g C:/Users/Admin/Downloads/mylinuxvm_key.pem),
"username" with your Azure VM username and
- "public-ip-address" with the actual public IP address of your Azure VM.
Visual Studio Code will prompt you to enter your password. Once authenticated, VS Code will connect to your Azure VM.
Checks:
You can run additional commands to further test the connection. For example, check the distribution and version of Linux:
Conclusion:
By following these steps, you've successfully created a Linux VM on Azure and connected it to Visual Studio Code. This setup enables you to leverage the power of the cloud for development and testing, providing flexibility and scalability. With your development environment in the cloud, you can easily collaborate with team members and work on projects from anywhere. Happy coding!