Skip to content

Start user shells in proper PAM sessions #9213

Open

Description

@K900

Is your feature request related to a problem? Please describe.
In NixOS, we rely on PAM to set up some important environment variables, including some related to locales, via pam_env.so. Right now WSL starts a single non-interactive shell with login -f to trigger the user systemd instance, but all the interactive shells are spawned bypassing PAM, and therefore not importing the environment variables.

Describe the solution you'd like
WSL starts every interactive shell instance via login or a similar mechanism that runs PAM modules.

Describe alternatives you've considered
Right now we're considering wrapping the user-selected shell in a script that updates the environment manually, but this will not be correct if any other PAM modules are involved.

Additional context
The specific issue that led me to notice this was zsh failing to discover locales when spawned through WSL, as we require the LOCALE_ARCHIVE environment variable to be set for locale archives to be discovered.

Activity

  1. cerebrate commented on Nov 25, 2022

    @cerebrate

    Also an #8842 variant, I believe.

    Right now WSL starts a single non-interactive shell with login -f to trigger the user systemd instance,

    Huh. Is that what that's supposed to do? 'Cause on my system, at least, it doesn't, which is probably another race-condition-on-slower-systems issue similar to the one causing the issue described in the comments here.

    I have to rely on systemd-machined / machinectl shell to get a working user systemd session and its appurtenances.

  2. HarbingerNight commented on Jan 10, 2024

    @HarbingerNight

    I have had success working around the wsl pam_env and systemctl --user commands issue by changing the wsl startup command parameters to launch a shell through sudo i.e.

    wsl -u root sudo -u username zsh

  3. shmyer commented on Jul 18, 2024

    @shmyer

    I had a similar issue related to WSL not using PAM. I had issues with Ubuntu 24.04. having a hard and soft limit on open files of 4096, which was too little for my Java app to compile using Maven: Too many open files

    I tried raising the limits in /etc/security/limits.conf, but it didn't work and then I found this line in the config file:
    #This file sets the resource limits for the users logged in via PAM.
    This led me to this issue about WSL not using PAM.

    HarbingerNight your solution worked for me, thx. I changed the command line of my Windows Terminal according to your example and then the file limits got applied correctly in my terminal.

    Edit: I still had issues with other WSL Integrations, for example when using IntelliJ IDEA's WSL Integration. I couldn't find a way there for modifying the command that is used for launching shells, so after a lot of fiddling around I found a permanent solution for all terminals/shells/integrations:

    1. Allow your user to execute prlimit command with sudo without password. Add this to /etc/sudoers and replace your_username with your username:
      your_username ALL=(ALL) NOPASSWD: /usr/bin/prlimit --nofile=* --pid *
      The wildcard for number of files can also be changed to a fixed value. For the pid however, it needs to be a wildcard.
    2. Add this to your .profile:
      sudo /usr/bin/prlimit --nofile=65536 --pid $$; ulimit -n 65536
      This will set the hard and soft open file limit to 65536 for the current shell process, which is inserted via the double dollar sign. This is why you need the wildcard for the pid in /etc/sudoers

    Edit 2:
    Just found out, that my open file limit only was so low because my WSL kernel was outdated and didn't get updated properly. With the newer Kernel the open file limit is about 1M, which should be enough for anything... So first, maybe check your WSL kernel version :)

  4. YeeaaahMan commented on May 30, 2025

    @YeeaaahMan

    I faced with same problem while I was testing my external web service from WSL.
    [Errno 24] Too many open files: .pycharm_helpers/pycharm/teamcity/jb_local_exc_store.py

    shmyer thank you for update. Here is what helped me:

    > wsl --update
    Checking for updates.
    Updating Windows Subsystem for Linux to version: 2.5.7.
    
    > wsl --version
    WSL version: 2.5.7.0
    Kernel version: 6.6.87.1-1
    WSLg version: 1.0.66
    MSRDC version: 1.2.6074
    Direct3D version: 1.611.1-81528511
    DXCore version: 10.0.26100.1-240331-1435.ge-release
    Windows version: 10.0.22631.5189
    
    > wsl ulimit -a
    -t: cpu time (seconds)              unlimited
    -f: file size (blocks)              unlimited
    -d: data seg size (kbytes)          unlimited
    -s: stack size (kbytes)             8192
    -c: core file size (blocks)         0
    -m: resident set size (kbytes)      unlimited
    -u: processes                       63165
    -n: file descriptors                1048576
    -l: locked-in-memory size (kbytes)  65536
    -v: address space (kbytes)          unlimited
    -x: file locks                      unlimited
    -i: pending signals                 63165
    -q: bytes in POSIX msg queues       819200
    -e: max nice                        0
    -r: max rt priority                 0
    -N 15: rt cpu time (microseconds)   unlimited
    
  5. SamB commented on Jul 22, 2025

    @SamB

    Also an #8842 variant, I believe.

    Right now WSL starts a single non-interactive shell with login -f to trigger the user systemd instance,

    Huh. Is that what that's supposed to do? 'Cause on my system, at least, it doesn't, which is probably another race-condition-on-slower-systems issue similar to the one causing the issue described in the comments here.

    I have to rely on systemd-machined / machinectl shell to get a working user systemd session and its appurtenances.

    Well, https://wsl.dev/technical-documentation/systemd/#user-sessions sure seems to say so.

  6. YeeaaahMan commented on Dec 1, 2025

    @YeeaaahMan

    I see that file descriptor limit was decreased in 2.5.8 version again. And it's bothering me now.

    I was wondering what default value would suit you?
    Perhaps it is necessary to prepare a PR in which this value will be set through the .wslconfig file?

  7. taswyn commented on Jun 6, 2026

    @taswyn

    If it does work, then the correct fix for this is definitely to properly apply PAM.

    I feel like it's less requesting a feature and more of a bug fix. This isn't how Ubuntu works normally. It's incredibly frustrating to be trying to change a group of settings that are otherwise well documented for behavior and not have it function the same way in WSL, with no clear point of failure (even running strace).

    The change to the file descriptor limit only applies to file descriptors, and leaves other related operations in a completely inconsistent state to non WSL distributions.

    For example, ulimit -l to change locked-in-memory sizing is completely unusable for normal interactive sessions. The problem is larger than file descriptors.

    None of the standard changes for Ubuntu to allow higher locked-in-memory amounts work in WSL2 for normal user sessions. Changing limits.conf doesn't do anything. Changing user.conf doesn't do anything. Appropriate limits.d and user.conf.d directory files do nothing. Adding ulimit lines to the .bashrc just generates "not allowed" errors on every new session. There are esoteric workarounds that can work, but are either dangerous, cause issues of their own, or ultimately just shouldn't be needed.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions