Can I save an SSH password in the config file?
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.
Last updated
Commands checked on Ubuntu 24.04.5 LTS (the ubuntu:24.04@sha256:562111b88caa79c71391c0c4d3c2114a7d5c35915c20561fd893cb1e07929e6b
container image), .
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:
$ 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 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, has AddKeysToAgent yes, which the manual describes as adding keys to a running agent automatically:
$ 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:
$ 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.
$ 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:
$ 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.
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:
$ 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 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.
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
References
- ssh_config(5) on Ubuntu 24.04, PasswordAuthentication, IgnoreUnknown, AddKeysToAgent and the Control options manpages.ubuntu.com
- ssh-agent(1) on Ubuntu 24.04 manpages.ubuntu.com
- sshpass(1) on Ubuntu 24.04, the options -p, -f and -e and the exit status manpages.ubuntu.com
- Remote Development using SSH, in the VS Code documentation code.visualstudio.com
- Remote Development Tips and Tricks, in the VS Code documentation (SSH agent and ControlMaster) code.visualstudio.com
Related articles
The app is free.
Get notified at launchmacOS is coming to the Mac App Store. iOS and Android later.