搭建ssh跳板机
环境介绍:
[[email protected] ~]# cat /etc/redhat-release CentOS release 6.6 (Final) [[email protected] ~]# uname -r 2.6.32-504.el6.x86_64
//准备2-3台虚拟机,至少2台。
1.在所有机器上创建跳板机用户
[[email protected] ~]# useradd tb [[email protected] ~]# echo "123.com"|passwd --stdin tb
2.在跳板机上创建密钥
[[email protected] ~]# su - tb [[email protected] ~]# ssh-keygen -t dsa -P -f ~/.ssh/id_dsa
//将生成密钥拷贝到需要通过跳板机登录的机器上。
[[email protected] ~]# ssh-copy-id -i .ssh/id_dsa.pub "-p 22 [email protected]" [[email protected] ~]# ssh-copy-id -i .ssh/id_dsa.pub "-p 22 [email protected]" [[email protected] ~]# ssh-copy-id -i .ssh/id_dsa.pub "-p 22 [email protected]"
3.创建跳板机脚本
[[email protected] ~]# cd /server/scripts [[email protected] ~]# vim tiaoban.sh #!/bin/bash function trapper(){ trap ‘‘ INT QUIT TSTP TERM HUP } function menu(){ cat <<-EOF <==前面是一个减号 =========Host List========== 1)192.168.75.141 2)192.168.75.140 3)192.168.75.139 4)exit ============================= EOF <===EOF前面是一个tab键,非4个空格。 } function host(){ case "$1" in 1) ssh [email protected] ;; 2) ssh [email protected] ;; 3) ssh [email protected] ;; 4|*) exit ;; esac } function main(){ while true do trapper clear menu read -p "Please select:" num host $num done } main :wq 保持退出
4.添加登录系统加载脚本
[[email protected] ~]# cd /etc/profile.d/ [[email protected] ~]# vim tiaoban.sh #添加如下内容 [ $UID -ne 0 ] && [ $UID -ne 500 ]&&. /server/scripts/tiaoban.sh #已知root用户UID为0,oldboy用户UID为500,这些用户不要进入跳板机系统。
5.下面测试。使用xshell登录跳板机。
时间: 2024-10-07 08:43:40