# Can I save an SSH password in the config file?

ssh_config has no password option. Use a key in ssh-agent, ControlMaster for hosts that ask for a password, or sshpass for a device that takes one.

Source: https://valakes.com/learn/save-ssh-password/

Last updated: 2026-11-29

No. ssh_config has no option for a password, and ssh stops with an error when a config file contains one. Load a key into ssh-agent and nothing is asked; for a device that takes only a password, use ControlMaster to ask once per session or sshpass to answer for a script.

Commands checked on Ubuntu 24.04.5 LTS (the ubuntu:24.04@sha256:562111b88caa79c71391c0c4d3c2114a7d5c35915c20561fd893cb1e07929e6b container image), 29 November 2026.

No. `ssh_config` has no option for a password, and ssh stops with an error when it finds one. Load a key into `ssh-agent` and nothing is asked at all. For a device that takes only a password, `ControlMaster` asks once per session, and `sshpass` answers for a script.

## What ssh does with a Password line

Put the line in a test file and point ssh at it with `-F`:

```console
$ printf '%s\n' 'Host pi' '    HostName app.example.test' '    User pi' '    Password demo-pass' > /tmp/pw.conf
$ ssh -F /tmp/pw.conf pi true
/tmp/pw.conf: line 4: Bad configuration option: password
/tmp/pw.conf: terminating, 1 bad configuration options
```

ssh quits before it connects, with exit status 255. The nearest real option is `PasswordAuthentication`. The manual gives it the values `yes` (the default) and `no`, so it says whether ssh may try a password at all and holds none. `IgnoreUnknown Password` would silence the error, and the line would still do nothing.

## Use a key and let ssh-agent hold it

[The SSH keys article](https://valakes.com/learn/ssh-keys/) shows how to make a key and install it on a server. `ssh-agent` holds the unlocked key, so later logins need no passphrase either. The config of the test machine, from [the config file article](https://valakes.com/learn/ssh-config-file/), has `AddKeysToAgent yes`, which the manual describes as adding keys to a running agent automatically:

```console
$ cat ~/.ssh/config
Host app
    HostName app.example.test
    User deploy

Host pi
    HostName app.example.test
    User pi

Host *
    StrictHostKeyChecking accept-new
    AddKeysToAgent yes
    IdentityAgent /tmp/agent.sock
```

ssh finds the agent through the variable `SSH_AUTH_SOCK`, which ssh-agent sets when it starts. This container has no login that sets it, so the first command does:

```console
$ export SSH_AUTH_SOCK=/tmp/agent.sock
$ ssh-add -l | cut -d' ' -f1,3,4
256 valakes-learn-test-key (ED25519)
$ ssh app whoami
deploy
```

The key has a passphrase, and the setup of this run already loaded it. On your machine `ssh-add` asks for the passphrase once, or ssh asks when it first uses the key. I cut the fingerprint from the list. The login itself printed no prompt.

## A device that takes only a password: sshpass

The account `pi` on the test server is such a device. It has a password and no key. `sshpass` is a separate package. It starts ssh and types the password when ssh asks, and its manual offers three ways to hand it over: `-p` on the command line, `-f` from the first line of a file, and `-e` from the variable `SSHPASS`.

```console
$ sshpass -V | head -1
sshpass 1.09
$ sshpass -p demo-pass ssh pi whoami
pi
$ sshpass -p demo-pass ssh pi sleep 3 &
$ ps -eo args | grep '[s]shpass'
sshpass -p xxxxxxxxx ssh pi sleep 3
$ wait
```

The manual calls `-p` the least secure option and says that every user of the system can see the password with `ps`. This version prints x's in its place. Do not lean on that. The password is still in the line you typed, so it is in your shell history and in every script that holds the line.

The other two routes keep it out of the command:

```console
$ printf 'demo-pass\n' > ~/.ssh/pi.pass
$ chmod 600 ~/.ssh/pi.pass
$ sshpass -f ~/.ssh/pi.pass ssh pi whoami
pi
$ export SSHPASS=demo-pass
$ sshpass -e ssh pi whoami
pi
$ sshpass -e ssh pi sleep 3 &
$ ps -eo args | grep '[s]shpass'
sshpass -e ssh pi sleep 3
$ tr '\0' '\n' < /proc/$(pgrep -x sshpass)/environ | grep SSHPASS
SSHPASS=xxxxxxxxx
$ wait
```

Now `ps` shows no password, and this version overwrites the variable in its own process too. The manual still calls the environment route only "marginally less exposed", and the file route leaves the password on disk, so keep that file at mode 600.

sshpass does not accept a new host key for you. It exits with status 6 when the host key is unknown and with status 7 when it has changed. Connect once by hand and check the key, as in [the article on a changed host key](https://valakes.com/learn/host-key-changed/).

## Ask once per session with ControlMaster

When a host asks for the password on every connection, let the first connection do the asking and share it. `ControlMaster auto` opens a master connection when none exists. `ControlPath` names the socket where the next ssh finds it, and `ControlPersist 10m` keeps the master running in the background after the first session ends, and closes it after ten idle minutes. ssh reads every `Host` block that matches and takes the first value of each option, so a second block for `pi` is enough:

```console
$ mkdir -p ~/.ssh/sockets
$ chmod 700 ~/.ssh/sockets
$ printf '%s\n' 'Host pi' '    ControlMaster auto' '    ControlPath ~/.ssh/sockets/%r@%h-%p' '    ControlPersist 10m' >> ~/.ssh/config
$ sshpass -e ssh pi whoami
pi
$ ssh pi whoami
pi
$ ls ~/.ssh/sockets
pi@app.example.test-22
$ ssh -O exit pi
Exit request sent.
$ ls ~/.ssh/sockets
```

The first command is the one that asks for the password. On your keyboard ssh shows its prompt there. This run has no keyboard, so sshpass answers. The second command asked nothing, and the file in `sockets` is the shared connection. `ssh -O exit` closed the master and the file is gone. While the master runs, every new `ssh pi` from your account skips the password. That is why `ControlPersist` has a time limit.

## VS Code Remote-SSH

The VS Code documentation for Remote-SSH recommends key based authentication, and says that "passwords are not saved, so using key based authentication is typically more convenient". It keeps hosts in a local file in the SSH config file format, and the setting `remote.SSH.configFile` names a different file if you want one. For a key with a passphrase, its tips page says to make sure an SSH agent runs locally. On macOS and Linux the same page shows a `ControlMaster` block with `ControlPersist 600` to cut down how often you enter a password or token.

## Mistakes people make

You put the password in a script with `sshpass -p`. It sits in the file and in your history, and the manual says `ps` can show it. Use `-f` or `-e`, or better, a key.

You switch off host key checking so that sshpass stops exiting with status 6. That removes the only check that you reached the right machine. Accept the key once by hand.

You use sshpass on a server that could take a key. A key in an agent asks for nothing and stores no password. When the server is yours, [turning off password login](https://valakes.com/learn/disable-ssh-password-login/) stops every password login, sshpass included.

You expect ControlMaster to remember the password. It remembers a connection. When the master ends, the next `ssh pi` asks again.

## In Valakes

Valakes keeps passwords and key passphrases in an encrypted vault. When a saved credential is bound to `ssh` for a host, Valakes answers the password prompt from the vault, so you do not type the value and it does not appear on screen. The feature page is [password auto-fill](https://valakes.com/features/password-auto-fill/).

## In Valakes

Valakes keeps passwords and key passphrases in an encrypted vault, and when a saved credential is bound to ssh for a host, it answers the password prompt from the vault without showing the value. [Feature: Password auto-fill](https://valakes.com/features/password-auto-fill/)

[Read the docs: Credentials and auto-fill](https://docs.valakes.com/credentials/)

## References

- [ssh_config(5) on Ubuntu 24.04, PasswordAuthentication, IgnoreUnknown, AddKeysToAgent and the Control options](https://manpages.ubuntu.com/manpages/noble/man5/ssh_config.5.html)
- [ssh-agent(1) on Ubuntu 24.04](https://manpages.ubuntu.com/manpages/noble/man1/ssh-agent.1.html)
- [sshpass(1) on Ubuntu 24.04, the options -p, -f and -e and the exit status](https://manpages.ubuntu.com/manpages/noble/man1/sshpass.1.html)
- [Remote Development using SSH, in the VS Code documentation](https://code.visualstudio.com/docs/remote/ssh)
- [Remote Development Tips and Tricks, in the VS Code documentation (SSH agent and ControlMaster)](https://code.visualstudio.com/docs/remote/troubleshooting)

## Related articles

- [How do I generate an SSH key and put it on a server?](https://valakes.com/learn/ssh-keys/): Run ssh-keygen -t ed25519, then ssh-copy-id -i ~/.ssh/id_ed25519.pub user@host. The private key stays on your computer, the public key goes into ~/.ssh/authorized_keys on the server, and you log in without a password.
- [How do I disable SSH password login safely?](https://valakes.com/learn/disable-ssh-password-login/): Put PasswordAuthentication no in a file in /etc/ssh/sshd_config.d/ whose name sorts first, run sudo sshd -t, then reload ssh. Keep your current session open and test a new login in a second window before you close it.
- [SSH config file (~/.ssh/config): examples that work](https://valakes.com/learn/ssh-config-file/): The file ~/.ssh/config stores the address, user, port and key of each server under a short name. After that, ssh app does the work of a long command.
- [How do I run sudo over SSH when it says "a terminal is required"?](https://valakes.com/learn/sudo-over-ssh/): Add -t, as in ssh -t app sudo whoami, so ssh gives the remote command a terminal and sudo can ask for the password. In a script, give the account a NOPASSWD rule for that one command instead of sending the password into sudo -S.
- [How do I fix "REMOTE HOST IDENTIFICATION HAS CHANGED" in SSH?](https://valakes.com/learn/host-key-changed/): The server now shows a different host key than the one saved in your known_hosts file. Check the new fingerprint with the server's owner, remove the old entry with ssh-keygen -R, and connect again.
