用例是我有一个1024位RSA密钥,多年来我一直使用它来访问许多远程服务器。
(1024位被认为不够强。截至2022年,目前的最佳实践表明RSA应该至少有2048位)。
因此,我想停止使用它(不想将它加载到我的ssh代理中),而是使用更新的ED25519键。
因为我可以为50+远程帐户这样做,所以我不想手动
ssh-add -d
或-D
会这样做吗?!见https://unix.stackexchange.com/questions/330569/ssh-add-d-refused-to-remove-identity)相反,我想要一种方便的方法,就像"ssh-copy-id“的增强替代。
发布于 2022-08-31 09:14:52
我编写了下面的脚本来处理这个问题,我称之为ssh-update-key
。
它暂时将我的废弃密钥加载到ssh-agent中,然后使用ssh- copy复制首选密钥。然后使用awk/sed从远程服务器中删除不推荐的密钥。
# Filename: ssh-update-key
# Instructions:
# Set the two variables below (deprecated_key, preferred_key).
# Invoke using: ssh-update-key account@someserver.com
### Configuration
# Create ~/.ssh/deprecated directory and move your deprecated key (private and pub) into there. Then set this variable.
deprecated_key=id_rsa
# Set this variable to your preferred key. Should be located in ~/.ssh
preferred_key=id_ed25519
### Script
# create symlink, so that ssh-agent will find and load the deprecated key
# (this happens automatically for me on Ubuntu 20.04)
(cd ~/.ssh &&
ln -s deprecated/$deprecated_key . &&
ln -s deprecated/$deprecated_key.pub .
)
# copy the preferred key to the remote server
ssh-copy-id -i ~/.ssh/$preferred_key "$@"
# now remove symlinks (only if they are symlinks - don't want an accident)
(cd ~/.ssh &&
[ -L $deprecated_key ] && rm $deprecated_key &&
[ -L $deprecated_key.pub ] && rm $deprecated_key.pub
)
# ssh-agent does not reliably remove keys if the files are removed.
# kill it to make sure. For me on Ubuntu 20.04 it respawns automatically.
killall ssh-agent
# Now remove deprecated key from remote authorized_keys file
# the awk regex escapes / characters which would otherwise mess up sed.
ssh "$@" -o PasswordAuthentication=no "sed -i.bak '/$(awk '{gsub(/\//, "\\/"); print $2}' ~/.ssh/deprecated/$deprecated_key.pub)/d' ~/.ssh/authorized_keys"
我创建了一个有此要旨。欢迎反馈/叉子/改进。
https://stackoverflow.com/questions/73559578
复制