大网数据广告活动
服务器运维问答

How can I use different ssh keys for user1@host and user2@host, without resorting to aliased hostnames?

· 52CE 我爱测

I have a remote CentOS server, lets pretend it's called hostname.com hostname.com has several user accounts, and I want to ssh into this server as any one of these users (using ssh keys rather than password). It seems everyone re…

问题原文

原帖链接 · carpii · CC BY-SA 4.0

I have a remote CentOS server, lets pretend it's called hostname.com

hostname.com has several user accounts, and I want to ssh into this server as any one of these users (using ssh keys rather than password).

It seems everyone recommends just changing my local ssh config, add an alias the server for each remote user, and then providing a different IdentityFile for each alias.

So then I have to ssh bob@aliased_hostname1 and ssh alice@aliased_hostname2

Are there any other solutions? Id rather not be ssh'ing into aliased hostnames

Ideally I want to just ssh [email omitted] or ssh [email omitted] and automatically choose the correct ssh keyfile, based on the remote username i'm trying to log in as.

已采纳解答

原帖链接 · Wesley · CC BY-SA 4.0

Check out the Match keyword in your SSH config on the client machine. Specifically, the user and host criteria. The ssh_config(5) man page states (emphasis mine):

Match conditions are specified using one or more criteria or the single token all which always matches.

So in your case you'd be looking at something like:

Match user bob host "hostname.com"
  IdentityFile /path/to/bob.key

Match user alice host "hostname.com"
  IdentityFile /path/to/alice.key

There's one SuperUser and one Unix & Linux Q/A that helped tie it all together, so you should probably go upvote those participants as well.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-08-08)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。