scrfix - Simplify Using Screen(1) on WSL
Doug Lee
Last Revised October, 2024

This small C program, when properly installed, simplifies use of screen(1) on the Windows Subsystem for Linux (WSL), version 1, by avoiding the need to run as root or via sudo(8). The directions in this guide, and in the comments atop the program itself, arrange for this utility to be installed setuid root. When run, this utility creates and sets required permissions on the folder used by screen(1) to store its communication sockets.

This utility is only likely to be of use under WSL 1. Under other environments where systemd runs as part of startup, including WSL 2, the problem addressed by this utility does not seem to occur.

For an alternative solution to this problem on WSL, along with an explanation of the problem's origin, see "Running Windows Subsystem For Linux Services At Startup" by Adrian Stoll. In short (and that page is at this writing very short), systemd does not run on WSL 1 and thus the normal initialization for Screen doesn't either. This author has not tested Adrian's solution and wrote this utility before noticing it.

This utility is covered by a BSD-style license; see the top of the source code file for details.

Table of Contents

System Requirements

This utility is intended for installation and use in Linux distributions run under the Windows Subsystem for Linux (WSL). It may run in other settings, with or without modification, but is by this author assumed to be unnecessary there.

Installation Instructions

To install this utility, perform these steps:

  1. Download the Zip of source and documentation, and unpack into a convenient place. Unpacking will not create a subfolder containing the files, but there are only two - scrfix.c and scrfix.htm. If you do not have the required unzip utility in your Linux distro, use apt install zip or apt install unzip, or your distro's equivalents. (This author chose the Zip format rather than the Tar format for its wider familiarity to native Windows users.)
  2. Pick a bin folder for the resulting scrfix utility; e.g., $HOME/bin or /usr/bin. Be sure that the folder you choose is checked for commands when you type commands; i.e., that it is referenced in your PATH environment variable. A good way to check this in most shells is to type this command and look for your chosen folder in the result:
    echo $PATH
  3. Compile with
    cc -o scrfix scrfix.c
    This should simply compile the utility without further output. If this fails, you may first need to apt install gcc (or similar depending on your Linux distribution). If you receive errors other than an error like "command not found," please report the error to this author.
  4. Move scrfix to the chosen folder and cd to that folder.
  5. Run these two commands as root or via sudo:
    chown root:root scrfix
    chmod 4755 scrfix
    These commands arrange for scrfix to run as root regardless of your user priveleges and without prompting for a password.
These steps must be repeated any time you install an update to this utility and possibly if you move it to a new folder.

Program Usage

After installation, run scrfix once every time wsl starts an instance. Then screen(1) will work without a sudo. Of course, if launching screen works without first running this utility, your WSL installation does not require this step.

It may be most convenient to arrange for this utility to be run once per launch of your WSL distribution. This can generally be done by calling it from within a shell script that launches exactly once per WSL login. For Bash users, this is the .profile script. For users of tcsh, this is the .login script. If you already launch Screen automatically, just call this utility first.

Theory of Operation

This utility simply creates and sets required permissions on a folder called /run/screen, which Screen uses to manage its communication sockets. In most environments, this folder can be created one time and reused by all users on the system. In WSL, however, the folder resides in an area that is recreated every time a WSL instance is spawned. As a result, the folder and its permissions must be re-asserted often.

It is of course necessary for this utility to run as root to make and set permissions on this folder. The program is written in C, as opposed to Python or another scripting language, to facilitate use of the setuid bit; that is, a permission bit set on the utility itself that allows it to execute with the priveleges of its file owner. The file must be owned by root for this to work as intended. The net result of all this is that the required setup can be established without requiring sudo or a root shell.

Revision History

Here is the revision history of this utility, most recent revision first:

Version 1.0, December 16, 2021