博客首页 注册 建议与交流 排行榜 加入友情链接
推荐 投诉 搜索: 帮助

23号博客

  0023.cublog.cn

关于作者
bsd: OpenBSD+Fluxbox
linux: Fedroa+Gnome
tools: 
       GnomeTerminal/Qterm
       Opera/Firefox
       Gvim/Gedit
       
       Cc+Python+Java
       4gl/Abap

Email: No.0023@gmail.com
|| << >> ||
我的分类


Distribute SSH keys easily
You know it can be cumbersome to copy over your public ssh-key to every server you have ever been to, so you push the action "automate ssh login" further and further into your personal planning.

Now it's time to distribute keys for once and for all. Let's use a script to speed things up a bit.

Update, check out ssh-copy-id(1). It does the same thing but is packaged with a openssh, far better than custom scripts! Thanks Davee


#!/bin/sh

# A script to push a key to the only argument, a remote server.

# Check if an argument was given.
if [ ! "$1" ] ; then
echo "Please specify a hostname to distribute the key to."
exit 1
fi

# Check if all the local files are here.
if [ ! -f ~/.ssh/id_rsa.pub ] ; then
echo "The local file ~/.ssh/id_rsa.pub is missing. Please create it."
exit 1
fi

# This command send the key, create a .ssh dir if required and set the
# correct permissions.
cat ~/.ssh/id_rsa.pub | ssh -q "$1" "if [ ! -d ~/.ssh/ ] ; then mkdir ~/.ssh ; fi ; chmod 700 ~/.ssh/ ; cat - >> ~/.ssh/authorized_keys ; chmod 600 ~/.ssh/authorized_keys"



What this script does it copy over your public ssh key to a server and set the permissions of the directories correct. This can be difficult from time to time.

The script will ask you once for your password on the remote server and do everything in that one session.

Here is how you could use the script:

$ ./ssh-push-key.sh server.example.com


Or if you have a list of server (extract them from ~/.ssh/known_hosts) you could use this list like this:


$ cat list-of-servers.txt | while read hostname ; do
> ./ssh-push-key.sh "$hostname"
> done

 原文地址 http://meinit.nl/distribute-ssh-keys-easily
发表于: 2007-12-07,修改于: 2007-12-07 10:21,已浏览255次,有评论0条 推荐 投诉


网友评论
 发表评论