DEV Community

Jamie Nordmeyer
Jamie Nordmeyer

Posted on • Originally published at jamienordmeyer.net on

Little WSL Things

I just rebuilt my personal laptop and was trying to remember how I’d set up WSL the last time so that I could develop within WSL. However, there were a couple of items that I had NOT done last time, and I was determined to figure out how to make them work this time around.

Setting up the ASP.NET Developer Certificate

First, I wanted to have a valid development certificate set up so that when I run my ASP.NET 6 API, it would show as a valid certificate in the browser. Before my rebuild, this wasn’t working; the API was working fine. However, the browser always showed the site as being insecure.

It looks like .NET 6 automatically sets up the ASP.NET developer certificate when it installs. When I ran the dotnet dev-certs commands to create the certificate, it said that the certificate already existed. And sure enough, when I navigate to /usr/local/share/ca-certificates/aspnet (I’m using the Ubuntu WSL install), I can see the https.crt and https.pfx files. However, when I launch the API and get the Swagger page, the site still shows as unsecured.

What I ended up doing is opening the Windows Certificate Manager outside of WSL and installed the certificate from there. You can open the Start Menu, then search for Manage User Certificates. Once that’s open, navigate to Trusted Root Certification Authorities / Certificates. You will then want to right-click on the Certificates node, select All Tasks from the menu, then Import.... On the screen asking you where the certificate is that you want to import, navigate to \\wsl.localhost\Ubuntu\usr\local\share\ca-certificates\aspnet, and import the https.crt file.

In my case, I had to reboot my computer at this point. I’m not sure if there’s a way to refresh everything without rebooting the machine, but whatever. This worked for me, and launching the API now shows that the site is secured.

Ensuring That the Postgres Service is Running on WSL Startup

I didn’t want to have to keep manually starting the Postgres service every time I restarted my computer, so I went looking to see if there was a way to have it launch automatically at least when I first open a WSL terminal so that it’s up and running before I type code .. I found the answer here, by Wan Zulkarnain, and give full credit to the original poster. I replicate it here for my own use, and just in case that site is ever shut down.

First, I added this code to my ~/.bashrc file:

if ! pgrep -x "postgres" >/dev/null; then
    sudo pg_ctlcluster 12 main start
    echo "postgres service started using: sudo pg_ctlcluster 12 main start"
fi
Enter fullscreen mode Exit fullscreen mode

I then created an sudoers file called skip_sudo_pg to make it so I didn’t need to enter the password on each start-up of WSL. In that file, I added the following one-liner:

%sudo ALL=(ALL) NOPASSWD:/usr/bin/pg_ctlcluster
Enter fullscreen mode Exit fullscreen mode

After stopping WSL and reopening a new terminal window, it successfully started up Postgres for me without me needing to manually do it.

It’s the little things that you come to appreciate… 😉

Top comments (0)