從別人網站抄過來,保留自用linux
場景: 對於須要管理的不少linux服務器,每次登陸都輸入密碼比較痛苦,配置一個跳板機,將本地公鑰推送帶各訪問節點上實現SSH登陸shell
登陸帳號:adminbash
本地祕鑰路徑:/home/admin/.ssh/id_rsa.pub服務器
=======================================================less
首先在跳板機上配置祕鑰ssh
ssh-keygen -t rsa -P "" -f ~/.ssh/id_dsa
而後建立ansible的playbook文件create_admin.yaml網站
- name: Linux Create User and Upload User Public keys
hosts: all
#remote_user: xxxx
#sudo: yes
vars:
create_user_name: admin
tasks:
- name: Make sure we have a 'admin' group
group:
name: admin
state: present
- name: Allow 'admin' group to have passwordless sudo
lineinfile:
dest: /etc/sudoers
state: present
regexp: '^%admin'
line: '%admin ALL=(ALL) NOPASSWD: ALL'
- name: Create user {{ create_user_name }}
user:
name: "{{ create_user_name }}"
shell: /bin/bash
groups: admin
createhome: yes
home: /home/{{ create_user_name }}
state: present
- name: create key directory
action: file path=/home/{{ create_user_name }}/.ssh/ state=directory owner={{ create_user_name }} group={{ create_user_name }} mode=0700
- name: create key file
action: file path=/home/{{ create_user_name }}/.ssh/authorized_keys state=touch owner={{ create_user_name }} group={{ create_user_name }} mode=0600
- name: Set authorized key took from file
authorized_key:
user: "{{ create_user_name }}"
state: present
key: "{{ lookup('file', '/home/admin/.ssh/id_rsa.pub') }}"
而後使用ansible執行推送spa
=======================================================code
登陸時直接使用ssh登陸,如:regexp
ssh 192.168.166.170
大神同事將下面腳本腳本封裝成go.sh,放入到/bin目錄下:
host=$1
if [ -z ${host} ]
then
host='127.0.0.1'
fi
ssh admin@${host} -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa
這樣就能夠快速地使用go.sh +ip登陸指定服務器
=======================================================