February 26, 2018

How to Use WSL Like a Linux Pro

wsl-pro.png

WSL
Learn how to perform tasks like mounting USB drives and manipulating files in this WSL tutorial. (Image courtesy: Microsoft)

In the previous tutorial, we learned about setting up WSL on your Windows 10 system. You can perform a lot of Linux command like tasks in Windows 10 using WSL. Many sysadmin tasks are done inside a terminal, whether it’s a Linux based system or macOS. Windows 10, however, lacks such capabilities. You want to run a cron job? No. You want to ssh into your server and then rsync files? No way. How about managing your local files with powerful command line utilities instead of using slow and unreliable GUI utilities?

In this tutorial, you’ll see how to perform additional tasks beyond managing your servers using WSL - things like mounting USB drives and manipulating files. You need to be running a fully updated Windows 10 and the Linux distro of your choice. I covered these steps in the previous article, so begin there if you need to catch up. Let’s get started.

Keep your Linux system updated

The fact is there is no Linux kernel running under the hood when you run Ubuntu or openSUSE through WSL. Yet, you must keep your distros fully updated to keep your system protected from any new known vulnerabilities. Since only two free community distributions are officially available in Windows Store, out tutorial will cover only those two: openSUSE and Ubuntu.

Update your Ubuntu system:

# sudo apt-get update

# sudo apt-get dist-upgrade

To run updates for openSUSE:

# zypper up

You can also upgrade openSUSE to the latest version with the dup command. But before running the system upgrade, please run updates using the previous command.

# zypper dup

Note: openSUSE defaults to the root user. If you want to perform any non-administrative tasks, please switch to a non-privileged user. You can learn how to create a user on openSUSE in this article.

Manage local files

If you want to use great Linux command line utilities to manage your local files, you can easily do that with WSL. Unfortunately, WSL doesn’t yet support things like lsblk or mnt to mount local drives. You can, however, cd to the C drive and manage files:

/mnt/c/Users/swapnil/Music

I am now in the Music directory of the C drive.

To mount other drives, partitions, and external USB drives, you will need to create a mount point and then mount that drive.

Open File Explorer and check the mount point of that drive. Let’s assume it’s mounted in Windows as S:\

In the Ubuntu/openSUSE terminal, create a mount point for the drive.

sudo mkdir /mnt/s

Now mount the drive:

mount -f drvfs S: /mnt/s

Once mounted, you can now access that drive from your distro. Just bear in mind that distro running with WSL will see what Windows can see. So you can’t mount ext4 drives that can’t be mounted on Windows natively.

You can now use all those magical Linux commands here. Want to copy or move files from one folder to another? Just run the cp or mv command.

cp /source-folder/source-file.txt /destination-folder/

cp /music/classical/Beethoven/symphony-2.mp3 /plex-media/music/classical/

If you want to move folders or large files, I would recommend rsync instead of the cp command:

rsync -avzP /music/classical/Beethoven/symphonies/ /plex-media/music/classical/

Yay!

Want to create new directories in Windows drives, just use the awesome mkdir command.

Want to set up a cron job to automate a task at certain time? Go ahead and create a cron job with crontab -e. Easy peasy.

You can also mount network/remote folders in Linux so you can manage them with better tools. All of my drives are plugged into either a Raspberry Pi powered server or a live server, so I simply ssh into that machine and manage the drive. Transferring files between the local machine and remote system can be done using, once again, the rsync command.

WSL is now out of beta, and it will continue to get more new features. Two features that I am excited about are the lsblk command and the dd command that allow me to natively manage my drives and create bootable drives of Linux from within Windows. If you are new to the Linux command line, this previous tutorial will help you get started with some of the most basic commands.

Learn more about the Administering Linux on Azure (LFS205) course and sign up here.

Click Here!