If you’re a Linux user who enjoys the efficiency and power of the Fish shell (Friendly Interactive Shell), you may have run into an issue where you’re repeatedly asked to enter your password every time you use sudo, even within a short period of time. Normally, after entering the password once, sudo should cache your credentials for a brief time (usually 5 or 15 minutes). However, for some Fish shell users, particularly those using Linuxbrew on Ubuntu, this behavior can get frustrating quickly.
In this post, I’ll walk you through an issue I encountered while using Fish shell on Ubuntu 24.04. The problem was that every time I executed a sudo command, I was asked for my password, regardless of whether I had just authenticated seconds earlier. Interestingly, this problem did not occur when using Bash or other shells, only with Fish. After some troubleshooting, I discovered that the culprit was related to my Linuxbrew configuration.
I’ll explain how I diagnosed and fixed the issue and provide a step-by-step guide to help others who might be facing a similar problem.
The Problem Everything started with a fresh installation of Ubuntu 24.04, Fish shell, and Linuxbrew. I quickly noticed an unusual behavior: every time I used sudo in Fish shell, I was prompted to enter my password. Even if I ran sudo commands back-to-back, I had to keep entering my password.
In a normal shell environment, after you authenticate once with sudo, your password should be cached for a while, so you don’t have to re-enter it for every command. This feature worked as expected in Bash but not in Fish.
I suspected there was something in my Fish shell configuration that was causing this behavior. To confirm this, I ran Fish with the –no-config option, which disables any custom configurations:
fish --no-config
To my surprise, sudo worked as it should in this mode, which led me to conclude that the issue was indeed related to something in my Fish configuration.
Investigating the Fish Configuration The first place I checked was my ~/.config/fish/config.fish file. This file contains user-specific configurations that are executed every time Fish starts. Here’s what my initial configuration looked like:
if status is-interactive
# Set up fzf key bindings
fzf --fish | source
# Commands to run in interactive sessions can go here
# AWS completion
set -gx fish_complete_path $fish_complete_path /usr/local/bin/aws_completer
complete -c aws --wraps aws_completer --no-files
end
# Starship prompt
starship init fish | source
# Initialize Linuxbrew environment
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
As you can see, I had set up a few key bindings, AWS autocompletion, and the Starship prompt. But the line that caught my attention was the last one: eval “$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)”. This command sets up the environment variables for Linuxbrew, including paths to its binaries, libraries, and other resources.
To test whether this was the cause, I temporarily commented out that line:
# eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
After restarting Fish, I tried running sudo again, and lo and behold, the problem was gone! I no longer had to re-enter my password for every sudo command. What went wrong? So why was this happening? When I ran the eval “$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)” command, it modified my environment variables, including the PATH variable. This placed Linuxbrew’s directories (/home/linuxbrew/.linuxbrew/bin and /home/linuxbrew/.linuxbrew/sbin) at the beginning of my PATH.
This caused Fish to prioritize Linuxbrew’s binaries over the system’s default binaries, including potentially overriding important utilities like sudo. It’s possible that Linuxbrew’s versions of some tools or libraries (such as those related to PAM or authentication) conflicted with the system defaults, leading to this strange sudo behavior.
When the system’s default sudo was being used (as in Bash or Fish without my custom configuration), everything worked fine. But with the modified PATH, some Linuxbrew binaries might have been causing sudo to ask for the password every time.
The Fix
To fix the issue, I had to manually configure the Linuxbrew environment without overriding critical system binaries like sudo. Here’s the solution I implemented in my ~/.config/fish/config.fish file:
if status is-interactive
# Set up fzf key bindings
fzf --fish | source
# Commands to run in interactive sessions can go here
# AWS completion
set -gx fish_complete_path $fish_complete_path /usr/local/bin/aws_completer
complete -c aws --wraps aws_completer --no-files
end
# Starship prompt
starship init fish | source
# Manually configure Linuxbrew environment taken from https://github.com/orgs/Homebrew/discussions/4412#discussioncomment-8651316
if test -d /home/linuxbrew/.linuxbrew # Linux
set -gx HOMEBREW_PREFIX "/home/linuxbrew/.linuxbrew"
set -gx HOMEBREW_CELLAR "$HOMEBREW_PREFIX/Cellar"
set -gx HOMEBREW_REPOSITORY "$HOMEBREW_PREFIX/Homebrew"
else if test -d /opt/homebrew # macOS
set -gx HOMEBREW_PREFIX "/opt/homebrew"
set -gx HOMEBREW_CELLAR "$HOMEBREW_PREFIX/Cellar"
set -gx HOMEBREW_REPOSITORY "$HOMEBREW_PREFIX/homebrew"
end
fish_add_path -gP "$HOMEBREW_PREFIX/bin" "$HOMEBREW_PREFIX/sbin"
! set -q MANPATH; and set MANPATH ''; set -gx MANPATH "$HOMEBREW_PREFIX/share/man" $MANPATH
! set -q INFOPATH; and set INFOPATH ''; set -gx INFOPATH "$HOMEBREW_PREFIX/share/info" $INFOPATH
This approach manually adds Linuxbrew’s paths without using eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)", preventing Linuxbrew from overriding system binaries like sudo. The key part is the conditional setup based on the system type (Linux or macOS), which ensures that the correct directories are added.
By setting PATH, MANPATH, and INFOPATH manually and in a controlled manner, I was able to maintain access to Linuxbrew tools without affecting the rest of the system.
Conclusion
This issue is a good example of how seemingly minor changes to your shell environment can cause unexpected behaviors. In my case, using eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)" in Fish caused sudo to behave incorrectly by prompting for a password every time. By manually configuring my environment, I was able to resolve the issue.
If you’re using Fish shell and Linuxbrew on Ubuntu or macOS, and you’re facing similar problems, try manually configuring your environment instead of relying on brew shellenv. It gives you more control over your system’s behavior and helps avoid conflicts with critical tools like sudo.
Hopefully, this guide helps others who might be struggling with similar issues. If you have any questions or run into more complex problems, feel free to reach out!
