//
//
Note: This post has been updated on 10/01/2014 (dd/mm/yyyy) to reflect the changes of Kali Linux 1.0.6 and the fix for the bug 575 I’ve submitted (ext4 file system issues with Hyper-V).
Greetings everyone,
Backtrack was a wonderful Pentest distro, you already should be aware that Backtrack R3 was the last release and it became deprecated. Kali Linux is the new Pentest distro from the same team, more details about the changes between BackTrack and Kali Linux go here:
http://www.kali.org/news/kali-linux-whats-new/
Following the blog post How to Install BackTrack 5 R3 on Hyper-V 3.0 – Windows Server 2012 and Window 8 it is about time to come up with Kali Linux installation process on Hyper-V, as said on my previous post and as the same as Backtrack, installing Kali on VMware or Vbox is a no brainer and you can either download a pre-installed VMware VM, doing the installation on Hyper-V has some special considerations which I will cover in detail here.
You may ask, why do I care with Hyper-V. The world is changing and Hyper-V became the most reliable Hypervisor in the market since Windows 8 and Windows Server 2012, comparing feature by feature with VMWare some features are almost equal, some others are better and there’s a few that are a way too much better (almost double the specs). Windows 8.1 and Windows Server 2012 R2 are already RTM and I am running them on my environments, I can tell you that if Hyper-V was already a top technology, on the new OS versions it became even better, if you are running Windows it’s a free ride so worth’s the try.
[Before you start]
– I will show you how to install Kali Linux on Hyper-V with LVM encryption partition to protect your data. There was a bug (see below bug 443) with LVM Encryption on Kali Linux that was fixed on the release 1.05, so make sure that you download the latest release or else you will be unable to boot your Machine without fixing GRUB.
http://bugs.kali.org/view.php?id=443
– Kali Linux installation process will configure by default the ext4 file system. If you are still using Kali 1.0.5 and below on ext4 file system the installation process of Kali Linux will hang while copying the OS installation files to the HDD, this was caused by a regression on the Storvsc driver that uses the method “WRITE_SAME” which is not supported on Hyper-V, the behavior is described on the following bug entry.
//
//
http://bugs.kali.org/view.php?id=223
There was a patch for the Storvsc driver that fixes this issue to Disable and blacklist the “WRITE_SAME” method: https://patchwork.kernel.org/patch/2172871/
During the Kali Linux 1.0.5 I’ve submitted the bug 575 which is now fixed on Kali Linux 1.0.6, you can see the details here:
http://bugs.kali.org/view.php?id=575
Hopefully this bug was brought to the attention of Kali’s team and now the ext4 file system is fully supported on Hyper-V.
For the purpose and success of this installation you need to download and install Kali Linux 1.0.6 and beyond, the installation is now reflecting the full compatibility of ext4 file system.
[Installation process]
1. Install the Hyper-V role on your machine, follow these links if you are not familiar yet with the process:
Windows Server 2008 R2 Hyper-V Getting Started Guide
http://technet.microsoft.com/en-us/library/cc732470(v=WS.10).aspxWindows Server 2012 Install the Hyper-V Role and Configure a Virtual Machine
http://technet.microsoft.com/en-us/library/hh846766.aspx
2. Go to the Kali website Download section and download the Kali Linux ISO file 1.05 or higher. I prefer Kali Linux 64 bit so this Wizard is focused on this distribution installation.
Kali Linux Download section:
http://www.kali.org/downloads/
3. On the Hyper-V Manager create a new Virtual Machine and Virtual Hard Disk:
On the right pane Click “New” –>Virtual Machine
On the New Virtual Machine Wizard –> Next
Define the Machine Machine Name and the location of the Virtual Machine configuration files
If your are running a Windows 8.1 or Windows Server 2012 R2 host Select the Virtual Machine “Generation 1”
Set the Memory size “2048” MB (depending on your needs, this is my setup memory)
Select your Internal or External Virtual Switch Network created on Step 3.
Create a Virtual Hard Disk (default: differential) with 30GB (depending on your needs)
Select “Install Operating System from a Boot CD/DVD” and Select the “image file (ISO)” downloaded on Step 2.
Review the VM configuration ao click “Finish” to complete the VM setup.
4. Start the Kali Linux Virtual Machine and from the Boot Menu select the “Graphical Install” option as shown in the pic
5. On the Language screen select your preferred language, e.g. “English – English” and your Location, e.g. “Other –> Europe –> Portugal”
6. Select your Locals, e.g. “United States – en_US.UTF-8” and your Keyboard type, e.g. “Portuguese”
7. On the Network Configuration Screen select “ Configure Network Manually” (if you have a DHCP Server available on the same network you may use the automatic configuration, on my lab I’ll do a manual network config)
8. Define your IP address and network mask, e.g. “10.0.0.100/24” and your gateway, e.g. “10.0.0.254”
9. Configure your name server, e.g. “192.168.0.1” and your hosname, e.g. “kalim3r0”
10. Define the domain name, e.g. “lab.local”
11. Define the root user password
12. Configure the clock, e.g. “Lisbon”
13. On the Partition Disks setup window select “Guided – Use entire disk and set up encrypted LVM”
Select the appropriate partition disk and click continue, e.g. “SCSI 3 (0,0,0) (sda) – 32,2 GB Msft Virtual Disk”
14. On the Partition Disks partitioning window select “All files in one partition (recommended for new users)”, for the purpose of this demo this is the simplest option, you can define separate partitions for /home, /usr, /var, etc, etc
15. Confirm the Logical Volume Manager configuration, on “Write the changes to disks and configure LVM” Select “Yes”
16. Define a Passphrase for the Encryption, don’t forget this passphrase because you will need to type it every time you boot up Kali Linux
17. On the Partition disks window with the Logical Volume Manager, partitions and mount point details, keep the default file system configuration (Kali 1.0.6 and above you can use ext4 file system) and click on the “Continue” button.
18. Confirm the changes and configuration of the Partition Disk on “Write the changes to disks” and select “Yes”
19. On the Configure the package manager window on “Use a network mirror” select “Yes”.
The intent of this process is to update the Kali Linux packages during the OS install, so make sure that you have Internet connectivity to accomplish this task
20. When you go ahead with the packages updates with the last step you may configure a proxy server ip address. For the purpose of this lab I have NAT and access to the internet, so I don’t need a proxy server and I will leave it blank
21. On the Install the Grub boot loader on a hard disk on “Install the GRUB boot loader to the master boot record?” select “Yes”
After GRUB boot loader installation the Kali Linux installation is complete and you should see the Finish Installation window, click on “Continue” to boot up Kali Linux
22. When you boot Kali Linux you will be prompted with the GRUB boot loader option and it will automatically boot in normal mode
23. After the boot you will be prompted for the Encryption Passphrase you defined on step 21
24. After
boot you will be prompted for credentials to Log in:
Username: root
Password: The password you defined on step 15
25. Finally you will have access to the so waited Kali Linux desktop
[EXTRAS]
No matter if you are a Linux newbie or you had forgotten the appropriate syntax for some of the common Linux commands, here are some helpful terminal commands:
1. Define a new password
passwd
2. Configure network settings:
Temporary configuration (will disappear after reboot):
ifconfig
(to verify which network interface is available, possibly “eth0”)Ifconfig eth0 10.0.0.100 netmask 255.255.255.0 up
route add default gw 10.0.0.254 eth0
Static configuration:
ifconfig
(to verify which network interface is available, possibly “eth0”)sudo nano /etc/network/interfaces
On the appropriate interface line (e.g. confirmed in ifconfig “eth0″) edit and append as follow:
auto eth0
iface eth0 inet static
address 10.0.0.100
gateway 10.0.0.254
netmask 255.255.255.0
network 10.0.0.0
broadcast 10.0.0.255
3 . Configure the DNS resolver
sudo nano /etc/resolv.conf
On the resolv.conf file insert the following entry:
nameserver 192.168.0.1
(IP of your nameserver)
4. Reload the new network configuration by restarting the network daemon
sudo /etc/init.d/networking restart
5. Download the list of the updated packages
apt-get update
6. Compare and update installed packages
apt-get upgrade
7. Install a new package
apt-get install <packagename>
R-Tape Loading error,
Luís Rato
//
//
Publicado em Hyper-V, Linux, Security
Etiquetas: Hyper-V, Linux, Pentesting, Security
Previously, I blogged about:
- Getting Ubuntu distro environment on Windows for DevOps,
- Getting openSUSE distro environment on Windows for DevOps,
- Getting SLES distro environment on Windows for DevOps, and
- Getting Debian distro environment on Windows for DevOps
Today, I will be sharing an additional distro for Windows Subsystem for Linux
(WSL) that targets SecOps more than DevOps and most likely will be my favourite
distro that will be a permanent resident on my Windows.
Let me introduce to you, Kali Linux a Debian-derived
linux distribution primarily for hacking and penetration testing. And probably
one of the most advanced penetration testing distribution, ever.
This walk-through demonstrates on how you can enable the Windows Subsystem for
Linux (WSL), maintain your linux distro environment and upgrade the linux
distro instance to stay ahead. Let’s get started with command lines.
Getting Started with Kali on Windows Subsystem for Linux (WSL)
In this blog post, I will be documenting the basic steps in getting Bash and
other common linux tools from Kali distro working on Windows 10 using Windows
Subsystem for Linux (WSL) feature.
What is actually Windows Subsystem for Linux? In short, it is a Windows feature
on Windows 10 that allows developers to run linux environment directly on
Windows without deploying a virtual machine. That means you get to use Bash
and many other tools that focus primarily on linux first to work on Windows.
↑Top
Pre-requisite requirements
- Windows 10 Build
16215
or later - Windows Server 1709 or
later
↑Top
Enable Windows Subsystem for Linux feature on Windows 10
To enable the Windows Subsystem for Linux Feature on Windows 10 and reboot the
Windows 10 using PowerShell.
Note: A reboot of the Windows 10 operating system is required after enabling
the Windows Subsystem for Linux feature
- Launch Windows PowerShell with elevated privileges
- Use the
Enable-WindowsOptionalFeaturePowerShell cmdlet to enable the
feature
1
2
3
4
Enable-WindowsOptionalFeature `
-FeatureName "Microsoft-Windows-Subsystem-Linux" `
-Online `
-NoRestart:$False ;
↑Top
How to obtain Kali distro instance for Windows
Once you have verified that your current environment meets the pre-requisite
requirements and you have enabled the Windows Subsystem for Linux feature.
Let’s get started with obtaining Kali distro instance with Windows
Subsystem for Linux for Windows 10 or Windows Server.
↑Top
Downloading Kali distro instance
In this example, we will use Invoke-WebRequest PowerShell cmdlet to download
the linux distro application package to your home folder.
1
2
3
4
5
# Download Kali application for WSL
Invoke-WebRequest `
-Uri "https://aka.ms/wsl-kali-linux" `
-OutFile "~\Kali.appx" `
-UseBasicParsing ;
↑Top
Installation for Windows 10
Natively, you can use Add-AppxPackage PowerShell cmdlet to add the linux
distro application package to your Windows 10.
1
2
3
# Install the Kali 2018.3 WSL application
Add-AppxPackage `
-Path "~\Kali.appx" ;
During your initial launch of Kali on Windows 10, you will be requested to
configure your new UNIX username and password.
Note: This setup a normal non-administrative user account that will
login by default when you launch the distro. The username and password does not
require to be same as your Windows user account. To elevate privileges in the
distro instance, usesudoand input your password. For more information, you
are refer to this documentation
here.
↑Top
Installation for Windows Server
In this particular Kali distro package release, the process is slightly
different from the other linux distro packages because it is packaged
differently and you will immediately notice the differences in the compressed
file content structure.
In order install into Windows Server, you will need to repeat the use of
Rename-Item to rename the application package extension and execute
Expand-Archive to uncompress the file process twice.
To start of on Windows Server, you will have use Rename-Item PowerShell
cmdlet to rename the linux distro application package extension to a compressed
file extension.
After the file has been renamed to a compressed file extension, you will use
Expand-Archive PowerShell cmdlet to expand the compressed file to your
temporary folder or ~\AppData\Local\Temp\Kali user temporary folder location.
Next, you will have use Rename-Item PowerShell cmdlet to rename the
DistroLauncher-Appx_1.1.4.0_x64.appx application package extension to a
compressed file extension again and use Expand-Archive PowerShell cmdlet to
expand the compressed file to your home folder or ~\.wsl\distro\ custom home
folder location.
After the file has been expanded to the destination, use the Start-Process
PowerShell cmdlet to launch the executable to begin the initial configuration
of the linux distro instance.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Rename the file extension to compressed file extension
Rename-Item `
-Path "~\Kali.appx" `
-NewName "Kali.zip" ;
# Expand the compressed file to destination
Expand-Archive `
-Path "~\Kali.zip" `
-DestinationPath "~\AppData\Local\Temp\Kali" ;
Rename-Item `
-Path "~\AppData\Local\Temp\Kali\DistroLauncher-Appx_1.1.4.0_x64.appx" `
-NewName "Kali.zip" ;
# Expand the compressed file to destination
Expand-Archive `
-Path "~\AppData\Local\Temp\Kali\Kali.zip" `
-DestinationPath "~\.wsl\distro\Kali" ;
# Launch the distro setup
Start-Process `
-FilePath "~\.wsl\distro\Kali\kali.exe" ;
Once the kali.exe is running, the installation will begin and you will be
requested to configure your new UNIX username and password.
Note: This setup a normal non-administrative user account that will
login by default when you launch the distro. The username and password does not
require to be same as your Windows user account. To elevate privileges in the
distro instance, usesudoand input your password. For more information, you
are refer to this documentation
here.
↑Top
How to update the Kali distro instance
When you switched into the linux distro for the first time, you will need to
use the linux distro’s preferred package manager to update and upgrade those
installed packages. This is because most of the linux distro are shipped with
an empty/minimal package catalog.
For Debian-derived Kali Linux distribution, we will use apt update command to
update the packages index and perform apt upgrade to upgrade those packages
based on the up to date packages index. For more information about apt, you
can refer to the documentation
here.
Note: Microsoft does not maintain those linux distro and the linux distro
instance running on Windows Subsystem for Linux are not maintained by Windows
Updates.
1
2
# Update and upgrade Kali
sudo apt update && sudo apt upgrade
↑Top
How to validate Kali distro instance version
Because Kali Linux is a Debian-derived Linux distribution, you can check the
Kali distro instance version, use the cat command on /etc/debian_version
file.
1
2
# Display Kali version
cat /etc/debian_version
By using cat command on /etc/debian_version file, you will get an output of
the Kali release name.
If you are interested on the operating system release information, use the
cat command on /etc/os-release file.
1
2
# Display Kali release
cat /etc/os-release
And you will obtain the the operating system release information as below.
1
2
3
4
5
6
7
8
9
10
PRETTY_NAME="Kali GNU/Linux Rolling"
NAME="Kali GNU/Linux"
ID=kali
VERSION="2018.3"
VERSION_ID="2018.3"
ID_LIKE=debian
ANSI_COLOR="1;31"
HOME_URL="https://www.kali.org/"
SUPPORT_URL="https://forums.kali.org/"
BUG_REPORT_URL="https://bugs.kali.org/"
↑Top
How to perform an in-place upgrade of Kali
If you are still using Kali 2018.2 distro instance and wanted to test
Kali 2018.3, you can use apt-get dist-upgrade command to upgrade your
distro instance to Kali.
Note: At the time of this blog post publication, Kali Linux 2018.3 is the
latest release.
Firstly, check your current sources list by using cat command on the
/etc/apt/sources.list file.
1
2
# Check your current sources list
cat /etc/apt/sources.list
The cat command will returns the current configured sources and validate the
source list contains the following below.
1
deb http://http.kali.org/kali kali-rolling main non-free contrib
Finally, perform an apt update to update the package lists followed by
apt full-upgrade to install the packages and upgrade Kali older rolling
version to Kali latest rolling version.
1
2
# Upgrade Kali older release to Kali 2018.3
sudo apt update && sudo apt -y full-upgrade
Once the upgrade has completed, check the Kali version.
1
2
# Display Kali version
cat /etc/debian_version
You should get an output like below.
Next, you can check the release information.
1
2
# Display Kali release
cat /etc/os-release
And operating system release information should contains the followings below.
1
2
3
4
5
6
7
8
9
10
PRETTY_NAME="Kali GNU/Linux Rolling"
NAME="Kali GNU/Linux"
ID=kali
VERSION="2018.3"
VERSION_ID="2018.3"
ID_LIKE=debian
ANSI_COLOR="1;31"
HOME_URL="https://www.kali.org/"
SUPPORT_URL="https://forums.kali.org/"
BUG_REPORT_URL="https://bugs.kali.org/"
↑Top
Conclusion
This definitely a lovely addition to Windows Subsystem for Linux (WSL) for
those security folks out there.
Now, you can have a Kali Linux derived from Debian GNU/Linux
operating system running on Windows Subsystem for Linux in Windows for hacking
and penetration testing tool in your IT security arsenal.
If you find that this information useful, feel free to bookmark this or share
it with your colleagues and friends.
↑Top
References
- Microsoft Blogs: Kali Linux for WSL now available in the Windows Store
- Microsoft Docs: Initializing a newly installed distro
- Kali News: Kali Linux in the Windows App Store
- Kali Git: Live Build repository
↑Top
↑Top
Skip to content
How to Install Kali Linux on a Hyper-V VM
Table of Contents
- Introduction
- What is Kali Linux?
- Requirements:
- Create a new VM on the Hyper-V Server for Kali Linux Installation
- Kali Linux Installation
- Packages and Tools available in Kali Linux:
- Conclusion
Introduction:
Protect Your Data with BDRSuite
What is Kali Linux?
Kali Linux is a Debian-based Linux distribution aimed at advanced Penetration Testing and Security Auditing. Kali contains several hundred tools that are geared towards various information security tasks, such as Penetration Testing, Security Research, Computer Forensics, and Reverse Engineering. Kali Linux was first released on March 13, 2013.
Currently installing Kali Linux on most of the Hypervisors are supported. The supported Hypervisors are as follows :
Hyper-V, VMWare, Parallels, Vagrant, Oracle VirtualBox, and UTM as Guest VM. Also, you can make the Kali Linux as the hypervisor host in VMWare and Oracle VirtualBox.
In this How-to article, we will look at the process of installing Kali Linux on a Hyper-V server as VM and how to configure some of the penetration tools required for the basic setup.
Here, we have downloaded the latest ISO file from the Kali Linux website and used the ISO image for creating a new VM on the Hyper-V Server.
Requirements:
- Windows Server 2019 with Hyper-V role
- Enough storage and RAM (Used 1 GB RAM and 140 GB of Disk for Kali Linux)
- Network connectivity on the Hyper-V Server
- Latest Kali Linux ISO file
Create a new VM on the Hyper-V Server for Kali Linux Installation
Open Hyper-V Manager on the Windows 2019 server, right-click Server → New → Virtual Machine
- Specify Name and location — Provide a name for the VM and location for the VM on the server
- Specify Generation – Generation 2 is used
- Assign Memory – Allocate memory for the VM – 1024 MB is used
- Configure Networking – Select Existing Virtual switch
- Connect Virtual Hard Disk – Create a new virtual hard disk, provide the location and size of the disk attached to the VM
- Installation options – Select Install an operating system from a bootable CD/DVD-ROM. Then, choose Media as an Image file and provide the path for the ISO file
- Finally, click the Finish button on the Summary page to create the VM
Kali Linux Installation
After creating a new VM, power ON and connect to the console, the installation menu will appear as below:
We are installing Kali Linux with Graphical mode.
Step 1: Choose Graphical Install and click Enter
Step 2: On the next screens – Select Language, Select Location, Select Keyboard and click the continue option to proceed.
In the next screen, you will see network configuration options. By default, machines will be configured with DHCP IPs. You also have the option to configure the network manually.
Step 3: Enter VM IP Address, Netmask and Default Gateway details of your network environment and click continue option to proceed.
Step 4: After configuring the network details, you need to enter the hostname and domain name for the machine.
Step 5: Now, set up users and passwords for the VM. Enter a new username and password to login to the VM and click on continue.
Step 6: On the next steps, you will be asked to configure the Disk Partition.
You can manually partition the disk or you can use the different standard disk partitioning schemes listed as Guided.
In Guided partition, you can use the entire disk or entire disk with Logical Volume Manager (LVM) or entire disk with Encrypted LVM.
Note: In Linux, Logical Volume Manager is a device-mapper target that provides logical volume management for the Linux kernel. Most modern Linux distributions are LVM-aware.
We have chosen Guided partition with LVM for this process.
Further, in the Partition Disks configuration, you also have the option to divide the partition into several partition schemes, such as separate size for /home /tmp and /var partitions.
We are selecting “All files in one partition”, and hence the operating system is created by default partition scheme, with / and /boot partition only.
Step 7: Now, provide the total disk size for the partition and click continue to proceed.
Step 8: On the next screen, you will be asked to confirm whether you want to write changes to the disk. Select the option Yes, and click Continue.
The below screen shows these changes with the displayed partition and will be formatted with ext4 file-system.
After formatting the disk, basic packages will be installed.
During installation, you can configure the package manager. This will allow you to install all additional packages from the nearby repository online. You can select the country nearby repository to find the packages and install them from there using apt-get command
Step 9: If your system is connected to the Internet through web-proxy, you can provide the details in this section below. Else, leave the proxy information page and click continue.
Step 10: The next screen is Software Selection. Since in the first stage, only the base system is installed you can add additional software with the base system in this section. Select “Install tools by purpose” to add Kali Linux tools.
Step 11: After the successful installation of basic packages and Kali Linux tools, the Grub boot loader will be installed on the hard disk from where the Linux OS is booted. Click Yes to proceed.
Step 12: Then, select the partition where the base system is installed for Installing the boot-loader and click continue.
Step 13: After the successful installation of the GRUB boot loader, the total OS installation is completed. Now, you will be asked to reboot the system. Click continue to proceed.
Step 14: After reboot, Kali Linux Installation is successfully completed. Now, you can log in with the created user credentials.
For adding additional packages, you can use the Terminal emulator at the top of the screen.
By default, the created user is a privileged administrator. By using sudo su command, you will get root privileges and can install any available packages using apt-get packet manager.
Packages and Tools available in Kali Linux:
Some of the useful packages and tools available in Kali Linux is listed in this URL below. These tools & packages can be installed using apt-get package manager.
https://tools.kali.org/tools-listing
Example:
- Cmospwd – CmosPwd is a cross-platform tool to decrypt the password stored in CMOS used to access a computer’s BIOS setup. This application works out of the box on most modern systems
- SSLsplit – SSLsplit is a tool for man-in-the-middle attacks against SSL/TLS encrypted network connections
- HTTPTunnel – HTTPTunnel is a tunneling software that can tunnel network connections through restrictive HTTP proxies
- Hashcat – hashcat is the world’s fastest and most advanced password recovery utility, supporting five unique modes of attack for over 200 highly-optimized hashing algorithms
- Webshells – A collection of web shells for ASP, ASPX, CFM, JSP, Perl, and PHP servers
- YARA – Tool to identify and classify malware samples
- Valgrind – Valgrind is a system for debugging and profiling Linux programs. With this tool suite, you can automatically detect many memory management and threading bugs
Conclusion:
Deploying Kali Linux on a physical machine or on a virtual machine in any hypervisor is straightforward and simple. Also, as a System Administrator or Linux Administrator, you can easily learn about the tools on the Kali Linux platform that give you knowledge on the penetration testing, vulnerability analysis, preventing sniffing & spoofing, password attacks, and hardware hacking.
Ensure the security of your crucial data with a reliable Linux backup solution, acting as a resilient barrier to uphold the integrity of your vital information. Experience its capabilities at no cost!
Effortlessly fortify your Linux environment and attain a state of tranquility by downloading BDRSuite today.
Delve deeper into the realm of Linux backup with BDRSuite, gaining a firsthand understanding of its impressive effectiveness.
Follow our Twitter and Facebook feeds for new releases, updates, insightful posts and more.
Try BDRSuite for Free!
Schedule a live demo with one of our product experts
Start your full-featured 30-day free trial
Explore detailed pricing, editions & features
When I first began familiar with WSL 1.0 , the network interfacing was simpler. But since WSL2 uses dynamic virtual IP address for each VM, now the things has got little trickier. Here, we will learn to install Postgres and MariaDB server and client in both Windows and WSL2 Linux and access them across the virtual Ethernet connection. By the term using interchangeably we mean accessing database in Windows from WSL2 Linux and vice versa.
PROBLEM STATEMENT: WSL2 IPv4 has access to PostgreSQL from localhost in Linux but not in Windows requires virtual ethernet adaptor’s assigned IP to communicate between each other and also other issue found was them requiring the firewall.
REQUIREMENTS: Database clients should be able to access database servers both in same OS and in other. WSL Client should access server in Windows and Windows clients should access database in WSL2.
To begin, one must need a good installation of Windows and WSL2 based distro in their machine.
Working with databases interchangeably
Source: https://dev.to/hymanzhan/setting-up-wsl-2-for-web-development-3202
Please be aware that you need to identify the IP address of the virtual machine (WSL) and the host machine you are working with, which might be different from the IP address of your local physical machine. You can find the IP address using Task Manager, and also using this command in WSL:
ip route # or
ip addr | grep eth0
Enter fullscreen mode
Exit fullscreen mode
You might also need to enable firewall on port 3306 for MariaDB and 5432 for Postgres for interconnectivity.
If you require port forwarding in WSL for reason, this article has it covered well. For Postgres vs MySQL comparison, you can check this blog from EDB, so you can choose the database system based on your need.
Database Servers
Database Servers in Windows
For Windows, you can get PostgreSQL from EDB and pgAdmin 4 from their download page. Simply install them and all add their bin directories to path folder if necessary.
For MariaDB, I recommend using XAMPP since it is robust and simple for beginners. But, if you like to install standalone version, you can download and install from official MariaDB site. You can use both MySQL Workbench and phpmyadmin to access such them as a client.
You must be able to access normally in Windows by psql and mysql commands.
Enabling/Disabling database servers in Windows is done through ‘Windows Services’. It can be accessed by entering
service.mscin Windows Run (Win+R).
Database Servers in Linux
PostgreSQL Server in WSL
Most of the Linux distribution already have source for installing Postgres packages.
sudo apt update
sudo apt install postgresql postgresql-client postgresql-client-common postgresql-common postgresql-contrib -y
psql --version # to confirm installation
sudo service postgresql start # replace ending by stop to stop server, restart to restart server
sudo -u postgres psql # to access Postges via command line (both in Linux)
Enter fullscreen mode
Exit fullscreen mode
TIP: can check the status of your services in Linux using sudo service --status-all in the shell.
This is the expected output after proper installation.
To fix connection refused issue, here’s the workaround for it:
sudo -u postgres psql -c 'SHOW config_file' # to locate config file
Enter fullscreen mode
Exit fullscreen mode
Once you find the file, change the listen_addresses to * in postgresql.conf, the restart Postgres by sudo service postgresql restart command.
Also, update this in pg_hba.conf to allow remote auth from Windows host.
...
host all all 0.0.0.0/0 scram-sha-256
host all all ::/0 scram-sha-256
Enter fullscreen mode
Exit fullscreen mode
This is a less recommended method (for security) you can try in case above methods still don’t let you connect in Windows:
#newline
host all all all trust
Enter fullscreen mode
Exit fullscreen mode
Here’s the detail article if you like to dig deeper into this. Also, check this for better insights.
MariaDB Server in WSL
Here’s the command enough to setup MariaDB server in WSL:
sudo apt install mariadb-server
sudo service mariadb start
sudo mysql # to access the database CLI
Enter fullscreen mode
Exit fullscreen mode
To change port, edit the configuration file /etc/mysql/my.cnf that suits your needs.
This StackOverflow thread suggests to edit the file /etc/mysql/mariadb.conf.d/50-server.cnf, replace bind-address = 127.0.0.1 (localhost) by bind-address = 0.0.0.0 or * (all). After you restart the server by $ sudo service mariadb restart, you may be able to login your Linux database server in Windows client too with proper credentials (username, password) configuration.
Database Clients
Database Clients in Windows accessing database in both Windows & WSL
We need to enable firewall for both in and out bound in Windows (for working across). To access, we need to find IP of VM in which the database server is active, the we can get along by mentioning host, port, user and password from any database client. This process if enough for the well-configured server.
Here’s the sample psql command to log in for dev user, to avoid conflict:
psql -U dev -d postgres -h 192.168.176.181 -p 5432 # server host IP
Enter fullscreen mode
Exit fullscreen mode
In pgAdmin mention host IP address from ip route and add other credentials to access WSL based Postgres server.
To access MariaDB in Windows using XAMPP phpmyadmin, create a user in WSL MariaDB server (say wsl_user with no password) and grant ALL PRIVELEDGES. Now, you can go to login portal for phpmyadmin and find Debian in server version.
Here, mysql accesses the server in Windows in the first one, and in the second one, the server is natively installed within WSL. Therefore, both side accessed.
In phpMyAdmin, you also need to change its config.inc.php on $cfg['Servers'][$i]['auth_type'] = 'config'; to cookie to get login page.
Here’s a sample showing access in Command Line (Database server is in Windows):
Database Clients in WSL accessing database in both Windows and WSL
For psql in WSL to access Windows database, you need to find host IP (using ip route for example) and then hit the command below:
psql -h [HOST ADDRESS] -U [USER NAME]
Enter fullscreen mode
Exit fullscreen mode
For MariaDB client access, it is simple as mysql like mysql -u wsl_root -p -h 172.24.xxx.xxx:
To make things simpler, simply create new user and access through it:
CREATE USER 'wsl_root'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'wsl_root'@'localhost' WITH GRANT OPTION;
CREATE USER 'wsl_root'@'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'wsl_root'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
Enter fullscreen mode
Exit fullscreen mode
You can get more details in these blogs: https://dev.to/hymanzhan/setting-up-wsl-2-for-web-development-3202 and https://dev.to/qviper/connecting-mysql-server-in-windows-machine-from-wsl-4pf1
Tips
Complete Postgres removal
Here’s the solution in detail for the process:
- Remove Postgres from apt (or from other installation if any):
sudo apt-get --purge remove postgresql postgresql-*
Enter fullscreen mode
Exit fullscreen mode
- Verify if packages are uninstalled:
dpkg -l | grep postgres
Enter fullscreen mode
Exit fullscreen mode
Here’s the solution in detail for the process:
- Remove Postgres from apt (or from other installation if any):
sudo apt-get --purge remove postgresql postgresql-*
Enter fullscreen mode
Exit fullscreen mode
- Verify if packages are uninstalled:
dpkg -l | grep postgres
Enter fullscreen mode
Exit fullscreen mode
Here, you must find this to be empty. If not, remove them manually.
- Remove the configuration folders below:
sudo rm -rf /var/lib/postgresql/
sudo rm -rf /var/log/postgresql/
sudo rm -rf /etc/postgresql/
Enter fullscreen mode
Exit fullscreen mode
- Finally, remove
postgresuser:
sudo deluser postgres
Enter fullscreen mode
Exit fullscreen mode
Work-around for error in phpmyadmin
Grant all permissions to root and pma and remove password too:
CREATE USER 'pma'@'localhost' IDENTIFIED BY '';
SET PASSWORD FOR root@localhost='';
Enter fullscreen mode
Exit fullscreen mode
References
- https://docs.microsoft.com/en-us/windows/wsl/networking
- https://www.enterprisedb.com/postgres-tutorials/how-use-postgresql-django
- https://www.enterprisedb.com/blog/how-to-secure-postgresql-security-hardening-best-practices-checklist-tips-encryption-authentication-vulnerabilities
- https://www.enterprisedb.com/postgresql-tutorial-resources-training
- https://dev.to/hymanzhan/setting-up-wsl-2-for-web-development-3202#install-database-in-wsl
- https://stackoverflow.com/questions/56824788/how-to-connect-to-windows-postgres-database-from-wsl
