Linux下实现SSH免密码登录和实现秘钥的管理、分发、部署SHELL脚本分享
环境:
ssh server: 192.168.100.29 server.example.com
ssh client: 192.168.100.30 client.example.com
通过root用户建立秘钥认证实现shell脚本管理,分发,部署
首先client端创建秘钥对,并将公钥分发给需要登录的ssh服务端
注:公钥相当于锁,私钥相当于钥匙,我们这里相当于在客户端创建一对钥匙和锁,想要做到ssh免密码登录,就相当于我们将锁分发到服务端并装锁,然后客户端就可以利用钥匙开锁。
一.建立秘钥认证
1.在客户端创建秘钥对:(ssh client)
# ssh-keygen -t dsa
一路回车即可
generating public/private dsa key pair.
enter file in which to save the key (/root/.ssh/id_dsa):
created directory '/root/.ssh'.
enter passphrase (empty for no passphrase):
enter same passphrase again:
your identification has been saved in /root/.ssh/id_dsa.
your public key has been saved in /root/.ssh/id_dsa.pub.
the key fingerprint is:
e9:5e:4a:7f:79:64:c5:ae:f2:06:a7:26:e4:41:5c:0e root@zabbix.example.com
the key's randomart image is:
+--[ dsa 1024]----+
| |
| e . |
| . + . |
| .o . o|
| s. o |
| . o . + .|
| oo.. b . |
| o +o * + |
| o .+ =. |
+-----------------+
2.查看生成的秘钥对:(ssh client)
# ls -lda .ssh
-----------------
drwx------ 2 root root 4096 6月 6 23:03 .ssh
-----------------
# cd .ssh
# ls -la
------------------
总用量 16
drwx------ 2 root root 4096 6月 6 23:03 .
dr-xr-x---. 26 root root 4096 6月 6 23:03 ..
-rw------- 1 root root 668 6月 6 23:03 id_dsa
-rw-r--r-- 1 root root 613 6月 6 23:03 id_dsa.pub
------------------
秘钥生成完毕
3.将公钥(锁)分发到ssh服务端:(ssh client)
# ssh-copy-id -i .ssh/id_dsa.pub 192.168.100.29
注:若非root用户,以及自定义ssh端口,则格式为:
输入yes,然后密码后回车:
the authenticity of host '192.168.100.30 (192.168.100.30)' can't be established.
rsa key fingerprint is fc:9b:2e:38:3b:04:18:67:16:8f:dd:94:a8:bd:08:03.
are you sure you want to continue connecting (yes/no)? yes
warning: permanently added '192.168.100.30' (rsa) to the list of known hosts.
address 192.168.100.30 maps to bogon, but this does not map back to the address - possible break-in attempt!
root@192.168.100.30's password:
now try logging into the machine, with "ssh '192.168.100.30'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
公钥分发完毕
4.服务端查看收到的分发文件:(ssh server)
# ll /root/.ssh
-------------
总用量 4
-rw------- 1 root root 613 6月 6 23:29 authorized_keys
-------------
成功收到
5.客户端验证登陆:(ssh client)
查看服务端ip地址:
# ssh 192.168.100.29 /sbin/ifconfig eth0
-----------------------
address 192.168.100.29 maps to bogon, but this does not map back to the address - possible break-in attempt!
eth0 link encap:ethernet hwaddr 00:0c:29:7a:4f:30
inet addr:192.168.100.29 bcast:192.168.100.255 mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe7a:4f30/64 scope:link
up broadcast running multicast mtu:1500 metric:1
rx packets:184297 errors:0 dropped:0 overruns:0 frame:0
tx packets:162028 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
rx bytes:163599380 (156.0 mib) tx bytes:51284830 (48.9 mib)
interrupt:19 base address:0x2000
注:这里遇到警告提示“address 192.168.100.29 maps to bogon, but this does not map back to the address - possible break-in attempt!”。
解决办法为修改客户端/etc/hosts文件,将服务端的ip地址与主机名对应关系写进去就可以了。
(ssh client)
# echo "192.168.100.29 server.example.com" >> /etc/hosts
重新查看
# ssh 192.168.100.29 /sbin/ifconfig eth0
无错误提示:
--------------------------
eth0 link encap:ethernet hwaddr 00:0c:29:7a:4f:30
inet addr:192.168.100.29 bcast:192.168.100.255 mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe7a:4f30/64 scope:link
up broadcast running multicast mtu:1500 metric:1
rx packets:184530 errors:0 dropped:0 overruns:0 frame:0
tx packets:162264 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
rx bytes:163618650 (156.0 mib) tx bytes:51304877 (48.9 mib)
interrupt:19 base address:0x2000
---------------------------
查看服务端内存
# ssh 192.168.100.29 free -m
--------------------------
total used free shared buffers cached
mem: 1006 991 14 0 177 308
-/+ buffers/cache: 506 500
swap: 1023 6 1017
---------------------------
二.创建shell脚本实现批量管理:(ssh client)
1.创建脚本:
# cd /etc/rc.d
# vi manager.sh
------------------
for ip in `cat iplist`
do
echo "---$ip---"
ssh $ip $1
done
------------------
2.生成ip列表:(若有多台ssh服务端需要管理,这里以此类推即可)
# echo 192.168.100.29 >> iplist
# echo 192.168.100.28 >> iplist
。。。。。
# cat iplist
---------------
192.168.100.29
---------------
3.执行脚本:
# sh manager.sh "df -h"
----------------
---192.168.100.29---
文件系统 容量 已用 可用 已用%% 挂载点
/dev/sda3 19g 6.7g 11g 38% /
tmpfs 504m 0 504m 0% /dev/shm
/dev/sda1 194m 27m 158m 15% /boot
----------------
管理成功
三.创建shell脚本实现批量分发:(ssh client)
1.创建脚本:
# cd /etc/rc.d
# vi distribute.sh
------------------
for ip in `cat iplist`
do
echo "---$ip---"
scp -r -p $1 $ip:$2
done
------------------
脚本ip列表已创建
执行脚本:
将本地/root下文件分发到ssh服务端主机
# sh distribute.sh /root /tmp
------------------
---192.168.100.29---
.iceauthority 100% 620 0.6kb/s 00:00
install.log.syslog 100% 10kb 10.2kb/s 00:00
preferred-web-browser.desktop 100% 2378 2.3kb/s 00:00
preferred-mail-reader.desktop 100% 257 0.3kb/s 00:00
.converted-launchers 100% 0 0.0kb/s 00:00
.bash_history 100% 3200 3.1kb/s 00:00
.bash_logout 100% 18 0.0kb/s 00:00
applet_dirlist 100% 0 0.0kb/s 00:00
saved_state 100% 65kb 64.5kb/s 00:00
8f329b0c645a51e018b765fa0000001a-0 100% 463 0.5kb/s 00:00
............
------------------
分发成功
四.批量部署:
这里的部署就结合了shell脚本批量管理和分发两个功能。
比如你要部署n台ssh服务端批量安装apache。
1.写好apache安装脚本。
2.将安装脚本分发到ssh服务端。
3.利用shell管理远端执行该脚本即可。
这里就不做过多演示,有机会我整理下我的lamp文档,写个apache脚本,在这里演示下。
注:因为涉及风险操作。所以不推荐线上利用root用户进行批量管理操作。
建议设置普通账户,再利用sudo提权操作。
通过普通用户建立秘钥认证并sudo提权进行管理,分发,部署
(ssh server)
# useradd user02
# echo "123456" | passwd --stdin user02
(ssh client)
# useradd user01
# echo "123456" | passwd --stdin user01
# su - user01
# ssh-keygen -t dsa
注:默认三个回车完成创建
# ssh-copy-id -i .ssh/id_dsa.pub user02@192.168.100.29
输入密码123456,分发完毕
验证:
# ssh user02@192.168.100.29 /sbin/ifconfig eth0
返回192.168.100.29端ip即表明秘钥验证成功。
分发:
注:客户端user01用户现在可以免密码分发到服务端user02所属文件夹,但若想分发到root所属文件夹,则需要sudo提权。
1.服务端sudo提权:
# su - root
# echo "user02 all=(all) nopasswd:/usr/bin/rsync,/bin/tar,/usr/bin/scp,/bin/cp" >> /etc/sudoers
登录user02账户
# su - user02
查看账户信息:
# sodo -l
----------------
............
user user02 may run the following commands on this host:
(all) nopasswd: /usr/bin/rsync, (all) /bin/tar, (all) /usr/bin/scp,(all) /bin/cp
----------------
2.客户端先分发到服务端user02用户家目录:
# scp -p22 -r -p /home/user01/ user02@192.168.100.29:/home/user02
-----------------------------
.bash_logout 100% 18 0.0kb/s 00:00
.bashrc 100% 124 0.1kb/s 00:00
known_hosts 100% 396 0.4kb/s 00:00
id_dsa 100% 672 0.7kb/s 00:00
id_dsa.pub 100% 615 0.6kb/s 00:00
.bash_profile 100% 176 0.2kb/s 00:00
-------------------------------
2.连接服务端后执行sudo cp命令执行本地拷贝:
# ssh -t user02@192.168.100.29 sudo cp /home/user02 /etc
-----------------------
connection to 192.168.100.29 closed.
-----------------------
拷贝成功
注:
# cp /test1 /test2/
是将/test1目录拷贝到/test2/目录下
# cp /test1/ /test2/
是将/test1目录下的所有文件拷贝到/test2/目录下
-------大功告成--------
上一篇: 爆笑小年轻的各种不着调
下一篇: 开心糗事逗你爆笑不止