ssh configuration: override the default username

2019-01-20 22:02发布

Is it possible to configure ssh to know what my username should be? By default it uses the current user-name, which is not correct in my case.

I'm on a loaner laptop, and my username is loaner, but I want to tell ssh that my username is buck.

Bonus points: my username at home is bgolemon. If I could configure username per-host that would be even better.

标签: unix ssh
5条回答
成全新的幸福
2楼-- · 2019-01-20 22:10

Create a file called config inside ~/.ssh inside the file you can add:

Host *
    User buck

or add

Host example
    HostName example.net
    User buck

The second example will set a username and is Hostname specific while the first example sets a username only. And when you use the second one you don't need to use ssh example.net, ssh example will be enough.

查看更多
趁早两清
3楼-- · 2019-01-20 22:11

If you only want to ssh a few times, such as on a borrowed or shared computer, try:

ssh buck@hostname

or

ssh -l buck hostname
查看更多
Anthone
4楼-- · 2019-01-20 22:20

There is a ruby gem that interfaces your ssh config file which is called sshez.

All you have to do is sshez <alias> username@example.com -p <port-number> then you can connect using ssh <alias>. It is also useful since you can list your aliases using sshez list and easily remove them using sshez remove alias

查看更多
成全新的幸福
5楼-- · 2019-01-20 22:23

You can use a short cut. Create a .bashrc file in your home directory. In there, you can add the following

alias sshb="ssh buck@host"

To make the alias available in your terminal, you can either close and open your terminal, or run

source ~/.bashrc

Then you can connect by just typing in: sshb

查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-01-20 22:27

man ssh_config says

User

Specifies the user to log in as. This can be useful when a different user name is used on different machines. This saves the trouble of having to remember to give the user name on the command line.

查看更多
登录 后发表回答