ssh 공개 키를 여러 Linux 호스트에 복사 to start bash, but since

중앙 서버에서 여러 서버로 .ssh / id_rsa.pub를 복사하려고합니다. 일반적으로 변경 사항을 다른 서버로 푸시하는 데 사용하는 다음 스크립트가 있습니다.

#!/bin/bash


for ip in $(<IPs); do
    # Tell the remote server to start bash, but since its
    # standard input is not a TTY it will start bash in
    # noninteractive mode.
    ssh -q "$ip" bash <<-'EOF'



EOF

done

그러나이 경우 로컬 서버에 공개 키를 배치 한 다음 여러 서버에 추가해야합니다. 위의 here 문서 스크립트를 사용하여 다음을 실행하는 방법이 있습니까?

cat .ssh/id_rsa.pub |ssh tony@0.0.0.0 'cat > .ssh/authorized_keys'



답변

이 간단한 루프를 사용하면이를 자동화하고 모든 원격 서버로 확산시킬 수 있습니다.

#!/bin/bash
for ip in `cat /home/list_of_servers`; do
    ssh-copy-id -i ~/.ssh/id_rsa.pub $ip
done


답변

다음은 매번 암호를 묻지 않고 ssh-keygen을 여러 서버에 복사하는 간단한 스크립트입니다.

for server in `cat server.txt`;
do
    sshpass -p "password" ssh-copy-id -i ~/.ssh/id_rsa.pub user@$server
done


답변

다른 사람의 공개 키를 여러 컴퓨터에 배치해야하는 경우 허용되는 답변이 작동하지 않습니다. 그래서 나는 다음 해결책을 생각해 냈습니다.

cat add-vassal-tc-agents.sh

#!/bin/bash
set -x # enable bash debug mode
if [ -s vassal-public-key.pub ]; then # if file exists and not empty
    for ip in `cat tc-agents-list.txt`; do # for each line from the file
        # add EOL to the end of the file and echo it into ssh, where it is added to the authorized_keys
        sed -e '$s/$/\n/' -s vassal-public-key.pub | ssh $ip 'cat >> ~/.ssh/authorized_keys'
    done
else
    echo "Put new vassal public key into ./vassal-public-key.pub to add it to tc-agents-list.txt hosts"
fi

이 스크립트는 실행되는 환경에 액세스 권한이있는 경우 머신 목록의 사용자에게 새 키를 추가합니다.

tc-agents-list.txt:

root@10.10.0.1
root@10.10.0.2
root@10.10.0.3
root@10.10.0.4


답변

공개 키를 복사하기 위해 openssh 자체에 무언가가 내장되어 있습니다. 그래서 대신 cat하고 ssh이것을 사용 : –

ssh-copy-id -i ~/.ssh/id_rsa.pub YOUR-REMOTE-HOST


답변

다음과 같이 간단한 while 루프 및 내장 서버 목록을 사용하여이 작업을 수행 할 수 있습니다.

while read SERVER
do
    ssh-copy-id user@"${SERVER}"
done <<\EOF
server1
server2
server3
EOF

스크립트 내부에 목록이 있으면 잘못 배치 될 수있는 별도의 데이터 파일이 없어집니다.


답변

서버 IP 목록, 이름 SERVER 및 서버의 IP 주소 만있는 파일이 정의되었다고 가정하십시오.

for 루프도 작동합니다.

for user in $(awk '{print $1}' SERVER | awk '{printf "user1@""%s"(NR==0?RS:"\n"), $1}' ) ;
do
        ssh-copy-id -f -i id_rsa_k2.pub $user
done


답변