Setting up auto-start xrdp for WSL2 Linux VM
Enabling `systemd` is the most standard and cleanest Linux-native approach.
Rendering...
Because WSL 2 does not use the traditional `systemd` initialization process (by default), Linux services do not automatically run in the background when the system starts, unlike on a physical machine.
To **automatically start `xrdp`** every time WSL is launched or a terminal is opened, there are several elegant and practical solutions:
### Solution One: Enable `systemd` within WSL (Microsoft's Official Recommendation, Most Convenient)
In newer versions of WSL 2 (latest Windows 10/11), Microsoft has natively supported `systemd`. Once enabled, Linux background services (including `xrdp`, Docker, etc.) will **automatically run with WSL startup** just like on a regular Linux system, without any additional scripts.
#### Steps:
1. **Modify WSL Configuration File**: In the Ubuntu terminal within WSL, run the following command to enable `systemd`:
Bash
```
sudo bash -c 'cat <<EOF > /etc/wsl.conf
[boot]
systemd=true
EOF'
```
2. **Configure xrdp Service for Autostart**: In the WSL terminal, run:
Bash
```
sudo systemctl enable xrdp
```
3. **Set Passwordless Startup (to prevent xrdp from getting stuck on password prompt)**: Since `xrdp` requires root privileges to start, to ensure smooth background autostart, simply allow the current user to start `xrdp` without a password (after enabling it above, systemd will execute it as root during system initialization).
4. **Restart WSL to Apply Changes**: In Windows PowerShell, execute the command to shut down WSL:
PowerShell
```
wsl --shutdown
```
Then reopen the Ubuntu terminal and enter the following command to check the `xrdp` status:
Bash
```
sudo systemctl status xrdp
```
If it shows **`active (running)`**, the configuration is successful! From now on, whenever WSL starts, `xrdp` will automatically run in the background, and you can connect via remote desktop at any time.
### Solution Two: Configure `sudoers` for Passwordless + `.bashrc` Autostart (Suitable for Older WSL Versions)
If you prefer not to enable `systemd`, or if your WSL version is older, you can have the system check and automatically start `xrdp` in the background every time you open a WSL terminal.
#### Steps:
1. **Set `xrdp` Service to Run Passwordless**: In the WSL terminal, enter the following command to edit sudoers permissions:
Bash
```
sudo visudo
```
Add the following line to the very bottom of the file (replace `your_username` with your WSL username):
Plaintext
```
your_username ALL=(ALL) NOPASSWD: /usr/sbin/service xrdp *
```
*Press `Ctrl + O` to save, `Enter` to confirm, `Ctrl + X` to exit.*
2. **Add Startup Command to User Configuration File**: Edit the `~/.bashrc` file:
Bash
```
nano ~/.bashrc
```
Add the following logic to the **very end** of the file (its purpose is: if `xrdp` is detected as not running, silently start it):
Bash
```
# Automatically start xrdp service
if ! service xrdp status > /dev/null 2>&1; then
sudo service xrdp start > /dev/null 2>&1
fi
```
*Save and exit.*
This way, every time you launch a WSL terminal, it will automatically bring up the `xrdp` service in the background, completely eliminating the need for you to manually type `sudo service xrdp start`.
### Solution Three: Automatically Start WSL Desktop Service Silently on Windows Boot
If you want **Linux remote desktop to be directly connectable as soon as Windows boots** (without even needing to open a WSL terminal), you can create a silent startup script on the Windows side.
1. Press `Win + R`, type `shell:startup`, and press Enter (this will open the Windows "Startup" folder).
2. In this directory, create a new text file and rename it to `start-wsl-xrdp.vbs` (note the extension change to `.vbs`).
3. Right-click to edit this file and paste the following code:
VBScript
```
Set ws = CreateObject("Wscript.Shell")
ws.run "wsl -u root service xrdp start", 0
```
4. Save the file.
**Effect**: From now on, every time your Windows PC boots and logs in, the system will implicitly start the WSL `xrdp` service in the background with `root` privileges, completely without pop-ups. You can open Windows "Remote Desktop Connection (`localhost:3390`)" at any time to directly access your Linux desktop!
### Summary and Recommendations
- **Preferred: [Solution One]**: Enabling `systemd` is the most standard and cleanest native Linux approach.
- **Combine with [Solution Three]**: If you want instant access upon boot, using it with the Windows startup script will provide the ultimate experience!Comments
Please login to view and post comments
Go to Login