Fixing SSH Wrong Home Directory Issue in Cygwin: $HOME Environment Variable Not Recognized


2 views

When running SSH in Cygwin, you might encounter an issue where it attempts to create or access the .ssh directory in an incorrect location, despite having the correct $HOME environment variable set. This typically manifests with errors like:

Could not create directory '/cygdrive/c/Documents and Settings/username/.ssh'
Host key verification failed

SSH in Cygwin sometimes falls back to Windows-specific paths rather than respecting Unix-style environment variables. Even when your shell shows:

HOME=/cygdrive/d/home/username
HOMEDRIVE=C:
HOMEPATH=/cygdrive/d/home/username

The SSH client might still look in the Windows default profile location. This happens because SSH checks multiple sources for home directory determination.

Before fixing, let's verify all relevant environment variables:

env | grep -i home
getent passwd $USER

Also check where SSH is actually looking:

strace -e trace=file ssh localhost 2>&1 | grep -i ssh

Option 1: Configure Cygwin Properly

Edit /etc/nsswitch.conf and ensure it contains:

db_home: /%H

Then regenerate user profiles:

mkpasswd -l -p "$(cygpath -H)" > /etc/passwd
mkgroup -l > /etc/group

Option 2: Force SSH Configuration

Create or modify ~/.ssh/config:

Host *
    UserKnownHostsFile ~/.ssh/known_hosts
    IdentityFile ~/.ssh/id_rsa

Option 3: Environment Variable Override

Add to your .bashrc or .bash_profile:

export CYGWIN="$CYGWIN winsymlinks:native"
export SSH_HOME=$(cygpath -u "$USERPROFILE")

For advanced troubleshooting:

which -a ssh
ssh -vvv localhost
cygcheck -c openssh

If the issue persists, consider:

  • Reinstalling Cygwin with proper permissions
  • Using Windows-native OpenSSH
  • Creating symlinks as a temporary workaround
ln -s /cygdrive/d/home/username/.ssh /cygdrive/c/Users/username/.ssh

When running SSH commands in Cygwin, you might encounter an error where it attempts to create or access the .ssh directory in an unexpected location (e.g., /cygdrive/c/Documents and Settings/username/.ssh) instead of your actual $HOME directory (e.g., /cygdrive/d/home/username/.ssh). This manifests as:

$ ssh username@host
Could not create directory '/cygdrive/c/Documents and Settings/username/.ssh'.

Meanwhile, your environment variables clearly show the correct path:

$ set | grep HOME
HOME=/cygdrive/d/home/username
HOMEDRIVE=C:
HOMEPATH=/cygdrive/d/home/username

Cygwin's SSH client sometimes inherits Windows environment variables instead of respecting Cygwin's Unix-style paths. The issue stems from:

  • SSH checking Windows registry-based home directories first
  • Inconsistent handling of mixed Windows/Cygwin paths
  • Environment variable propagation between Windows and Cygwin

Create or modify /etc/ssh/ssh_config in Cygwin:

Host *
    UserKnownHostsFile ~/.ssh/known_hosts
    IdentityFile ~/.ssh/id_rsa
    StrictHostKeyChecking no

Add these to your Windows environment variables (via System Properties):

HOME=/cygdrive/d/home/username
USERPROFILE=\\cygdrive\\d\\home\\username

If you can't modify system configurations, create a symlink:

$ ln -s /cygdrive/d/home/username/.ssh /cygdrive/c/Documents\ and\ Settings/username/.ssh

After applying any solution, test with:

$ ssh -v username@host

Check the debug output for which .ssh directory it's attempting to use.

For persistent changes, add this to your ~/.bashrc:

export HOME=/cygdrive/d/home/username
export SSH_HOME=$HOME/.ssh
alias ssh="env HOME=$HOME ssh"