Skip to content

Instantly share code, notes, and snippets.

@dayne
Last active April 19, 2025 00:44
Show Gist options
  • Save dayne/335cbea9ca4f3bcebeee397694b8eeeb to your computer and use it in GitHub Desktop.
Save dayne/335cbea9ca4f3bcebeee397694b8eeeb to your computer and use it in GitHub Desktop.
Cleaning up WLS path to remove /mnt/c paths (world writable).

The Windows Linux Subsystem (WSL) comes with /win/c mounted world read-write and is setup for compatibility between windows and linux by putting all your windows paths into your linux system path. This is great for some usecases. Some tools get grumpy having these world writable directories a users path. While this doesn't break them but it can cause annoying warning messages.

On the other hand, when you don't care about the windows tools being in your path, it might make you more sane not to see those error messages, and also to limit the number of places tools look for supporting binaries not-relevant to themselves.

: warning: Insecure world writable dir /mnt/c in PATH, mode 040777

You can remove all those /mnt/c paths from your $PATH with the following line:

export PATH=`echo $PATH | tr ':' '\n' | awk '($0!~/mnt\/c/) {print} ' | tr '\n' ':'`

If you add that line to your .bashrc file it will automatically be run ... do that only if you are sure you don't want those windows bin dirs in your path. You can always remove the line to bring them back.

I've setup a ~/.bash.d/remove_win_path that checks for a /.keep_win_path file and if not found it cleans out any /mnt/c related entries from my PATH. I can temporarly overrride that behaivor for a shell by setting a environment variable KEEP_WIN_PATH=1

#!/bin/sh
# placed in my ~/.bash.d
# touch ~/.keep_win_path to keep /mnt/c entries in PATH or export KEEP_WIN_PATH=1
if [ ! -f ${HOME}/.keep_win_path ]; then
if [ -z "${KEEP_WIN_PATH}" ]; then
export PATH=`echo $PATH | tr ':' '\n' | awk '($0!~/mnt\/c/) {print} ' | tr '\n' ':'`
fi
fi
@7urkm3n
Copy link

7urkm3n commented Apr 19, 2025

No need to update PATH.

Just add this line into /etc/wsl.conf

#sudo nano /etc/wsl.conf
[interop]
appendWindowsPath = false

Extra:
If you need specific ENV from windows side, such as like VScode, explorer.exe etc..

#WINDOWS
#powerShell command to see available ENV in windows!!!
$Env:PATH -split ';'
# WSL
# WSL export for explorer.exe 
export PATH="/mnt/c/Windows:$PATH"

# WSL export for VScode, code
export PATH="/mnt/c/Users/rgurd/AppData/Local/Programs/Microsoft VS Code/bin:$PATH"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment