SSH Username macOS: How to Set It
In this article
To set an SSH username on macOS, you either specify it in the ssh command (like ssh username@host) or configure it permanently in your SSH config file (~/.ssh/config). The config file method saves you from typing the username every time and lets you define per-host settings. This guide covers both approaches, plus how to change the default username for all connections.
What You'll Need
- A Mac running macOS (any recent version).
- Terminal app (found in /Applications/Utilities/).
- The username you want to use for SSH connections (usually the account name on the remote server).
- Optional: a text editor (like nano or vim) to edit the config file.
Step-by-Step
- Open Terminal. You can find it in Finder > Applications > Utilities, or use Spotlight (Cmd+Space) and type “Terminal”.
- Check if you already have an SSH config file. Run
ls ~/.ssh. If you see a file namedconfig, you can edit it. If not, you'll create one. - Create or edit the SSH config file. Use a text editor. For example, to create or edit with nano, run:
nano ~/.ssh/config. If the file doesn't exist, nano will create it when you save. - Add a host entry with the username. In the config file, add lines like this:
Host myserver HostName example.com User yourusernameReplacemyserverwith an alias you'll use in the ssh command,example.comwith the actual hostname or IP, andyourusernamewith the remote username. Save the file (in nano: Ctrl+O, then Enter, then Ctrl+X). - Test the connection. Run
ssh myserver(using your alias). It should connect using the username you set. If you don't want an alias, you can also set a default username for all hosts by adding aHost *section at the end of the config file:Host * User yourusernameThis applies the username to any SSH connection that doesn't have a more specific Host entry. - Alternatively, specify the username directly in the command. If you don't want to edit config files, just type
ssh username@hostname. For example:ssh john@192.168.1.10. This works for one-off connections.
Common Mistakes
- Using the wrong config file path. The file must be
~/.ssh/config(the tilde represents your home directory). Some users mistakenly create it in/etc/ssh/, which is a system-wide file that requires sudo and is not needed for personal settings. - Forgetting to set correct permissions. SSH may ignore the config file if it's too permissive. Run
chmod 600 ~/.ssh/configto set the correct permissions (read/write for you only). - Putting the username in the wrong place. The
Userdirective must be inside aHostblock. If you put it outside, it may be ignored or cause an error. - Using an alias that matches an existing hostname. If you name a Host alias the same as a real hostname, SSH may get confused. Use a unique alias.
- Not saving the file properly. In nano, remember to press Ctrl+O (write out) and then Enter to confirm, then Ctrl+X to exit.
Tips
- Use SSH keys for passwordless login. Setting a username is only half the story. For convenience and security, generate an SSH key pair (
ssh-keygen) and copy the public key to the server withssh-copy-id username@host. Then you won't need to enter a password. - Organize multiple hosts. You can have multiple
Hostblocks in the config file, each with its own username, port, and other options likeIdentityFile. - Use the config file to set other options too. For example, you can set a non-default port (
Port 2222) or enable compression (Compression yes). - Check your current username. If you're unsure what username to use, run
whoamion the remote server (if you have access) or ask the server administrator.
Troubleshooting
If your SSH connection still asks for a username or fails, try these in order:
- Verify the config file syntax. Run
ssh -G myserver(replace with your alias) to see the effective settings. If there's a syntax error, SSH will often display it. Look for typos like missing spaces or uppercase letters. - Check file permissions. Run
ls -l ~/.ssh/config. It should show-rw-------(owner read/write only). If not, runchmod 600 ~/.ssh/config. - Make sure the Host line matches exactly. If you type
ssh myserver, the config must have a lineHost myserver(case-sensitive). - Test with verbose output. Run
ssh -v myserverto see debugging messages. Look for lines like “Reading configuration data” and “user” to confirm your username is being used. - If you're using a default username for all hosts, ensure it's in the right place. The
Host *section should be at the end of the file, and it applies to any host that doesn't have a more specific entry. - Restart SSH agent (if using keys). Sometimes the SSH agent caches old identities. Run
ssh-add -K(on older macOS) orssh-add --apple-use-keychain(on newer) to refresh.
Frequently Asked Questions
Can I set a different username for each server?
Yes, create separate Host blocks in the config file, each with its own User directive. For example, Host server1 with User alice, and Host server2 with User bob.
What if I don't have a ~/.ssh directory?
Create it with mkdir -p ~/.ssh and set permissions with chmod 700 ~/.ssh. Then create the config file as described.
Does the username in SSH config override the one in the command line?
No. If you specify a username in the command line (e.g., ssh other@myserver), it overrides the config file's User for that connection.
Can I use the same config file for both macOS and Linux?
Yes, the SSH config format is identical across Unix-like systems. You can copy the file to another machine and it will work.
Why does SSH still ask for a password if I set a username?
Setting a username only tells SSH which account to log into. The server still requires authentication (password or key). If you want passwordless login, set up SSH keys.
Is it safe to put my username in the config file?
Yes, the username is not a secret. The config file is readable only by you (if permissions are set correctly), so it's safe.