Exercise - Connect to a Linux virtual machine with SSH
Let's connect to our Linux VM with SSH, and configure Apache, so we have a running web server.
Get the public IP address of the VM
In the Azure portal from the previous exercise, select Go to resource. The Overview pane for the virtual machine that you just created appears. Alternatively, you can find the VM under All Resources if you need to open it. The overview pane allows you to:
- See if the VM is running.
- Stop or restart the VM.
- Get the public IP address of the VM.
- See the activity of the CPU, disk, and network.
Select Connect > SSH at the top of the pane.
Under step 4, copy the command to the clipboard.
We used the default SSH private key file path when we created the SSH key pair. We don't need to specify the private key path in the command by using the flag
-i
with the private key path. If you entered a different path when you created the SSH key pair, you'd add that path to the command.
Connect with SSH
Paste the command from your clipboard into Azure Cloud Shell. Delete the
-i
flag and the private key path placeholder, and run the command. It should look something like the following sample with a different IP address and username.ssh azureuser@13.68.150.164
The first time we connect, SSH will ask us about authenticating against an unknown host. SSH is informing you that you've never connected to this server before. If that's true, it's perfectly normal, and you can respond with yes to save the fingerprint of the server in the known host file.
The authenticity of host '137.117.101.249 (137.117.101.249)' can't be established. ECDSA key fingerprint is SHA256:w1h08h4ie1iMq7ibIVSQM/PhcXFV7O7EEhjEqhPYMWY. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '137.117.101.249' (ECDSA) to the list of known hosts.
Enter the passphrase you used when you created the SSH key pair.
In the shell command prompt for Linux, try executing a few Linux commands:
ls -la /
: Shows the root of the diskps -l
: Shows all the running processesdmesg
: Lists all the kernel messageslsblk
: Lists all the block devices - here you'll see your drives
The more interesting thing to observe in the list of drives is what is missing. Notice that our Data drive (
sdc
) is present but not mounted into the file system. Azure added a VHD, but didn't initialize it.
Initialize data disks
Any additional drives you create from scratch need to be initialized and formatted. The process for initializing is identical to a physical disk.
First, identify the disk that we just created. You could also use
dmesg | grep SCSI
, which will list all the messages from the kernel for SCSI devices.After you know the drive (
sdc
) you need to initialize, you can usefdisk
to do that. You'll need to run the command withsudo
, and supply the disk you want to partition. We can use the following command to create a new primary partition.(echo n; echo p; echo 1; echo ; echo ; echo w) | sudo fdisk /dev/sdc
Next, we need to write a file system to the partition with the
mkfs
command.sudo mkfs -t ext4 /dev/sdc1
Finally, we need to mount the drive to the file system. Let's assume we'll have a
data
folder. Let's create the mount point folder and mount the drive.sudo mkdir /data && sudo mount /dev/sdc1 /data
We initialized the disk, and mounted it. If you want more details on this process, see the Add and size disks in Azure virtual machines module. This task is covered in more detail there.
Install software onto the VM
As you can see, SSH allows you to work with the Linux VM just like a local computer. You can administer this VM as you would any other Linux computer: installing software, configuring roles, adjusting features, and other everyday tasks. Let's focus on installing software for a moment.
You can also install software from the internet when you're connected to the VM via SSH. Azure machines are, by default, internet connected. You can use standard commands to install popular software packages directly from standard repositories. Let's use this approach to install Apache.
Install the Apache web server
Apache is available within Ubuntu's default software repositories, so we'll install it using conventional package management tools:
Start by updating the local package index to reflect the latest upstream changes:
sudo apt-get update
Next, install Apache:
sudo apt-get install apache2 -y
It should start automatically. We can check the status using
systemctl
:sudo systemctl status apache2 --no-pager
The
systemctl
command returns something like the following output:apache2.service - The Apache HTTP Server Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled) Drop-In: /lib/systemd/system/apache2.service.d └─apache2-systemd.conf Active: active (running) since Mon 2018-09-03 21:00:03 UTC; 1min 34s ago Main PID: 11156 (apache2) Tasks: 55 (limit: 4915) CGroup: /system.slice/apache2.service ├─11156 /usr/sbin/apache2 -k start ├─11158 /usr/sbin/apache2 -k start └─11159 /usr/sbin/apache2 -k start test-web-eus-vm1 systemd[1]: Starting The Apache HTTP Server... test-web-eus-vm1 apachectl[11129]: AH00558: apache2: Could not reliably determine the server's fully qua test-web-eus-vm1 systemd[1]: Started The Apache HTTP Server.
Finally, we can try retrieving the default page through the public IP address. However, even though the web server is running on the VM, you won't get a valid connection or response. Do you know why?
We need to perform one more step to be able to interact with the web server. Our virtual network is blocking the inbound request. We can change that through configuration. Let's look at allowing the inbound request next.
Need help? See our troubleshooting guide or provide specific feedback by reporting an issue.