gsudo

website·Indexed 30 days ago

https://gerardog.github.io/gsudo/

A sudo equivalent for Windows that allows users to run commands elevated in the current console window. It provides support for PowerShell, WSL, and various Bash for Windows environments (MinGW, MSYS2, Git-Bash, Cygwin), featuring a credentials cache and various elevation modes.

Tokens
6.9K
Snippets
43
Records
60
Agent score
68%

What's inside gsudo

  1. Overview of gsudo for Windows

    gsudo is a sudo equivalent for Windows that allows users to run commands with elevated permissions or elevate the current shell. It can execute commands in the current console window (avoiding the need to switch contexts) or in a new window using the -n flag. It supports I/O redirection, full console colors, keyboard input, and worldwide encodings. If no command is specified, gsudo starts an elevated shell.
  2. Build gsudo for .NET 7.0 x86 using Ready2Run

    Since NativeAOT is only supported on x64, use a self-contained build with Ready2Run for x86 platforms. This provides a performance boost over standard builds while remaining compatible with 32-bit architecture, though the binary size is larger (approx 16MB).
    # Clean the project
    dotnet clean .\src\gsudo\gsudo.csproj -f net7.0 -r win-x86
    
    # Publish with Ready2Run and SingleFile
    dotnet publish .\src\gsudo\gsudo.csproj -c Release -f net7.0 -r win-x86 --sc -p:PublishReadyToRun=true -p:PublishSingleFile=true -v minimal -p:WarningLevel=0
  3. Understand gsudo internal elevation mechanisms

    gsudo achieves elevation by launching an elevated instance of itself in 'service mode' (using Verb='RunAs'). It uses a combination of an IProcessRenderer (non-elevated) and an IProcessHost (elevated service) that communicate via two Named Pipes in the ProtectedPrefix\Administrators namespace—one for data and one for control.
  4. Understand the gsudo Credentials Cache

    The Credentials Cache is an optional feature that allows a parent process to elevate multiple times with only one UAC pop-up. It works by keeping an elevated instance of gsudo running in the background. Note: This can be a security risk if the parent process (e.g., Cmd or PowerShell) is compromised, as a malicious process could potentially trick the cache instance into elevating silently.
  5. gsudo shell compatibility and portability

    gsudo is a portable console application that does not require a Windows service or system changes other than adding the executable to the system Path. It automatically detects and supports the following shells: CMD, PowerShell, WSL, MinGW/Git-Bash, MSYS2, Cygwin, Nushell, Yori, and Take Command.
  6. Run elevated commands in Bash for Windows

    To run commands with elevated privileges in Bash environments on Windows (such as MinGW, MSYS2, Git-Bash, or Cygwin), prepend the gsudo command to your bash command. If you intend to elevate CMD commands specifically, use the -d flag.
    gsudo <command>
    # To elevate CMD commands
    gsudo -d <command>
  7. Create an elevated tab in Windows Terminal using gsudo

    To create a Windows Terminal profile that always starts with administrative privileges, edit the profile's command to prepend gsudo . For example, setting the command to gsudo cmd or gsudo pwsh will launch an elevated Command Prompt or PowerShell tab.
    // Example profile command
    "commandline": "gsudo pwsh"
  8. Install gsudo using a package manager

    You can install gsudo using WinGet, Chocolatey, or Scoop.
    # Using WinGet
    winget install gerardog.gsudo
    
    # Using Chocolatey
    choco install gsudo
    
    # Using Scoop
    scoop install gsudo
  9. Build gsudo for .NET Framework v4.6

    To build gsudo targeting .NET Framework v4.6 (which is bundled with Windows 10/11), use MSBuild to restore packages and rebuild the project. This target produces a small binary (approx 100-200KB) and does not require additional runtime redistribution.
    # Restore packages
    msbuild /t:Restore /p:RestorePackagesConfig=true src\gsudo.sln /v:Minimal /p:TargetFrameworkVersion=v4.6
    
    # Rebuild the project
    msbuild /t:Rebuild /p:Configuration=Release src\gsudo\gsudo.csproj /v:Minimal /p:WarningLevel=0 /p:TargetFrameworkVersion=v4.6
  10. Install gsudo on Windows

    gsudo is a portable console application for Windows 10/11, 8.1, and 7 SP1. It does not require a Windows service or system changes other than adding the application to the PATH environment variable. After installation, restart your consoles to refresh the PATH.
  11. Run elevated commands with gsudo

    To run a command with elevated permissions, prepend gsudo (or its alias sudo) to the command. By default, the command runs in the current user-level console. A UAC popup will appear unless the credentials cache is enabled. To elevate the most recently executed command, use gsudo !! (supported in CMD, Git-Bash, MinGW, MSYS2, Cygwin, and PowerShell with the gsudo module).
    gsudo <command>
    # Example: elevate a directory creation
    gsudo mkdir x
    
    # Elevate the last ran command
    gsudo !!
  12. Install gsudo via package managers

    gsudo can be installed using several popular Windows package managers: Chocolatey, Scoop, or WinGet.
    # Using Chocolatey
    choco install gsudo
    
    # Using Scoop
    scoop install gsudo
    
    # Using WinGet
    winget install gerardog.gsudo