π»Tech/π§Linux
[리λ μ€] ssh ν¨μ€μλ μ λ ₯μμ΄ μ μ (passwordless)
_viper_
2023. 8. 21. 16:37
λ°μν
리λ μ€μμ ν¨μ€μλ μ λ ₯ μμ΄ ssh μ μνλ 2κ°μ§ λ°©λ² μ 리ν©λλ€.
1. ν¨μ€μλ μ λ ₯ μμ΄ μ μν μλ²μ μνΈν ν€λ₯Ό λ±λ‘νμ¬ μλλ‘κ·ΈμΈμ ν©λλ€.
βΎ rsa ν€ μμ±μ΄ μλμμ κ²½μ° μμ±ν΄ μ€λλ€.
ssh-keygen -t rsa
Enter file in which to save the key (/home/hadoop/.ssh/id_rsa): ## ()κ΄νΈ μμΉλ‘ μ¬μ©ν κ±°λ©΄ Enter
Enter passphrase (empty for no passphrase): ## ν¨μ€μλ μμ΄ μ¬μ©ν κ±°λ©΄ Enter
Enter same passphrase again: ## Enter
βΎ authorized_keys νμΌμ΄ μμΌλ©΄ μμ±νλ©΄ λ©λλ€.
chmod 700 ~/.ssh ## 700μΌλ‘ κΆν λ³κ²½
cd ~/.ssh ## .sshκ²½λ‘ μ΄λ
cat id_rsa.pub >> authorized_keys ## 볡μ¬ν 곡κ°ν€λ₯Ό μΈμ¦ν€ νμΌμ μ
λ ₯ν΄μ€
chmod 600 authorized_keys ## private keyμ νΌλ―Έμ
μ΄ λ무 openλμ΄ μμΌλ©΄ μ μν μ μμ΄μ λ³κ²½ν΄μ€
rsync -av .ssh server01:/root ## passwordless μ€μ ν μλ²λ€μ .ssh λλ ν 리 볡μ¬
rsync -av .ssh server02:/root
rsync -av .ssh server03:/root
## λ¬Έμ μ μλ νλͺ© νμΈ
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub
chmod 644 ~/.ssh/known_hosts
[root@localhost ~]# ll -d /root
dr-xr-x---. 5 root root 4096 Feb 28 16:22 /root ## root κΆνμ΄ 550μΌλ‘ λμ΄ μμ΄μΌν¨
βΎ passwordless μ€μ ν μ격 μμ
λ μ μ©ν λ°©λ²μ
λλ€.
2021.03.04 - [Tech/Linux] - [리λ
μ€] μ¬λ¬ μλ² μ격 μμ
(parallel-ssh, ssh remote shell)
2. sshpass λͺ λ Ήμ΄λ‘ ν¨μ€μλ μ λ ₯νμ¬ ssh μ μ (sshpass μ€μΉ νμ)
sshpass λͺ λ Ήμ΄λ₯Ό μ¬μ©νλ©΄ ν¨μ€μλλ μ λ ₯νλ λ³λμ ν¨μ€μλ νμΈ λ¨κ³ μμ΄ μ μ κ°λ₯ν©λλ€.
κ·Έλ¦¬κ³ sshpassλ₯Ό μ¬μ©νμ¬ μΈλΆ λ€λ₯Έ μλ²μ λͺ λ Ήμ΄λ₯Ό νΈμΆνμ¬ μμ λ ν μ μμ΅λλ€.
"command" > μ¬κΈ° μ λ ₯νμΈμ.
sshpass -p'password' ssh root@hostname "command"
π μ°Έκ³
μ¬λ¬ μλ²μ μ μ©νλ remote μμ λλ€.
- hosts νμΌμ μ μ©ν μλ² λͺ©λ‘λ€μ μ μ₯ν©λλ€
#!/bin/bash
for target_host in `cat hosts`
do
sshpass -p '$password' ssh -o StrictHostKeyChecking=no ${target_host} 'mkdir -p ~/.ssh' && cat ~/.ssh/id_rsa.pub | sshpass -p 'hadoop' ssh ${target_host} "cat > ~/.ssh/authorized_keys && chmod 700 ~/.ssh && chmod 0644 ~/.ssh/authorized_keys"
echo "---------------------------------------------------"
done