0

I use a VM at home for work. My job consists of ssh'ing to several computers every day for various things... using the ssh user@servernotation is easy enough for this, but I've run into some issues with some scripts I'm working on. When my scripts run, it's attempting to ssh to boxes using my current user instead of the user associated with my LDAP account at work (and thus, not recognizing my private key). More specifically, my user at home is caleb, whereas my user at work is ctote.

Is there a way to modify my .bashrc or .ssh configs to force ssh to always use ctote when trying to connect?

ctote
  • 565
  • 1
  • 5
  • 13

2 Answers2

3

Yes, use SSH client configuration. Easiest way is to create a local user configuration in ~/.ssh/config with these two lines:

Host *
User ctote

This matches all hosts and applies the login name ctote to it.

To apply/override it for a single host you connect to ofen, use for example:

Host myhost
User myusername
Hostname server01.subdomain01.longnamehuh.tld
Port 2222

to make

ssh myusername@server01.subdomain01.longnamehuh.tld -p2222

into

ssh myhost

Bonus: also works for sftp/scp/git etc. And you can also do magic like this use case: How do I SSH to machine A via B in one command?.

Have a look at the manpage of SSH client configuration for more options you can specify and what the syntax is exactly.

man ssh_config
gertvdijk
  • 67,947
2

Sure, you can create a ~/.ssh/config file and in that:

Host laptop
    User another-user

You can specify most SSH options through host-matched options like this.

Oli
  • 293,335