.zshrc file or an incorrect alias definition that overrides the standard binary path.
Understanding the Error Context
When you type ssh -Y user@host into the macOS Terminal, the zsh shell attempts to find the ssh command by searching through directories listed in the $PATH environment variable. If it fails, it throws the "command not found" error. Unlike Bash, Zsh is strict about how commands are resolved, especially when aliases or functions are involved. The presence of the -Y flag indicates you are trying to enable X11 forwarding for graphical applications, which requires a valid SSH client installation.
This issue often stems from a recent change in your shell configuration files. Users frequently add custom paths or aliases to their .zshrc file without realizing they have accidentally shadowed system binaries. Before diving into fixes, it is helpful to understand that macOS includes OpenSSH by default, so the binary should exist at /usr/bin/ssh. If you are seeing this error, the link between your shell and that binary is broken.
Checking Your PATH Configuration
The most common cause is an incomplete or malformed $PATH variable. To diagnose this, open your Terminal and run the following command:
echo $PATH
You should see a colon-separated list of directories. Ensure that /usr/bin is present in this list. If it is missing, your shell does not know where to look for standard utilities like ssh, ls, or git. You can temporarily test if SSH works by providing the full absolute path:
/usr/bin/ssh -V
If this command returns the version number (e.g., OpenSSH_8.x), then the software is installed correctly, and the problem is strictly environmental. If even this fails, you may need to reinstall Command Line Tools via xcode-select --install.
Inspecting .zshrc for Conflicts
If your PATH looks correct, the next step is to check for conflicting definitions in your .zshrc file. This file resides in your home directory and controls Zsh settings. Open it using a text editor like nano:
nano ~/.zshrc
Look for any lines that define an alias or function named ssh. For example, a line like alias ssh='/some/custom/script' would override the built-in command. Comment out any such lines by adding a # at the beginning. Additionally, check for any export PATH=... statements that might be overwriting the existing path instead of appending to it. A correct append usually looks like:
export PATH="$HOME/.local/bin:$PATH"
After making changes, save the file and reload the configuration with:
source ~/.zshrc
Then try running ssh -Y again. If the error persists, consider checking for similar issues in related configurations, such as those discussed in Fixing ZSH Permission Error on MacOS, as permission restrictions can sometimes mimic path errors.
Verifying Shell Type
Ensure you are actually running Zsh. Run echo $SHELL. If it returns /bin/bash, you might be editing the wrong config file (.bash_profile vs .zshrc). macOS Catalina and later use Zsh by default, so .zshrc is the primary target for these fixes.
Reinstalling Command Line Tools
In rare cases where /usr/bin/ssh is completely missing, you may need to reinstall Apple’s Command Line Tools. This package includes essential development tools and utilities. Open Terminal and execute:
xcode-select --install
A dialog box will appear asking you to confirm the installation. Follow the prompts. Once complete, restart your Terminal window. This process restores all default binaries to their expected locations, resolving any corruption that might have occurred during previous updates.
Testing the Connection
Once the command is recognized, verify that the -Y flag works as intended. Run:
ssh -Y [email protected]
If you encounter connection refused errors, ensure that X11 forwarding is enabled on the remote server in its /etc/ssh/sshd_config file by setting X11Forwarding yes. However, if you still get "command not found," return to checking your .zshrc for syntax errors that might prevent the file from loading correctly.
Frequently Asked Questions
Why does ssh work in bash but not zsh?
This happens because each shell has its own separate configuration files. Your .bash_profile might have the correct PATH settings, while your .zshrc is either empty or misconfigured. You must configure Zsh independently in .zshrc to match your Bash environment.
How do I clear cached aliases in zsh?
Zsh caches aliases for performance. After modifying .zshrc, always run source ~/.zshrc to refresh the session. Alternatively, you can start a new login shell with exec zsh to force a complete reload of all configuration scripts.
Can I use ssh -X instead of ssh -Y?
Yes, -X enables untrusted X11 forwarding, which is more secure but slightly less performant than -Y (trusted forwarding). If you only need basic graphical output, -X is often sufficient and avoids potential security warnings associated with trusted forwarding.
What if /usr/bin/ssh is missing entirely?
If the binary is gone, it means the Command Line Tools were removed or corrupted. Running xcode-select --install will restore it. Do not manually download the binary from third-party sites, as this can lead to security vulnerabilities and compatibility issues with macOS system libraries.
Does Homebrew install its own ssh?
Homebrew installs its own copy of SSH in /opt/homebrew/bin (Apple Silicon) or /usr/local/bin (Intel). If you prefer the Homebrew version, ensure that directory appears before /usr/bin in your PATH. However, the system version is generally recommended for stability unless you need specific newer features.