어제 작성한 geth 설치 명령어 집합과 조건문의 조합으로 새 컴퓨터에서 한방에 geth를 구축하는 스크립트를 만들어보자.
자, 우리는 s1과 s2의 root에 외부에서 로그인 할 수 있게 했고(permitrootlogin = yes) s1에서 s2에 루트가 아니게 로그인 할 때에는 공개키를 두고 왔기 때문에 비밀번호 없이 접속이 가능하다.
하지만 루트s1에서 루트s2로 로그인 할 대에는 비밀번호가 여전히 필요하다.
이를 sshpass라는 프로그램을 설치해 사용자 개입 없이 통과시켜보자.
일단, s1의 root 계정에서 _setting 폴더를 만들고 그 안에 아래와 같이 sh 파일을 만든다.
root@s1:~/_setting# touch installSshpass.sh
root@s1:~/_setting# chmod +x installSshpass.sh
x 권한을 루트에 부여하고, 서브라임텍스트의 file - browse server - s1 - _setting - installSshpass.sh - edit 을 누른다.
그리고 아래를 입력해준다.
#!/bin/bash
tmp='which sshpass'
if [[ ${#tmp}=0 ]]; then
echo ==========install_sshpass==========
apt-get install -y sshpass
fi
(sshpass를 설치해주는 스크립트)
root@s1:~/_setting# ./installSshpass.sh
==========install_sshpass==========
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
sshpass
0 upgraded, 1 newly installed, 0 to remove and 4 not upgraded.
Need to get 10.5 kB of archives.
After this operation, 56.3 kB of additional disk space will be used.
Fetched 10.5 kB in 0s (298 kB/s)
Selecting previously unselected package sshpass.
(Reading database ... 217999 files and directories currently installed.)
Preparing to unpack .../sshpass_1.05-1_amd64.deb ...
Unpacking sshpass (1.05-1) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up sshpass (1.05-1) ...
root@s1:~/_setting#
정상적으로 sshpass가 설치되는 것을 볼 수 있다.
이제, 우리는 비밀번호를 입력해 sshpass로 s2의 모든 파일들에 접근할 수 있다.
root@s1:~/_setting# sshpass -p 111111 ssh s2 ls /
bin
boot
cdrom
dev
etc
home
initrd.img
initrd.img.old
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
snap
srv
sys
tmp
usr
var
vmlinuz
vmlinuz.old
그러면, 이제 ls 대신에 geth를 구축하는 스크립트를 실행시키면 될 것이다.
먼저 그러려면 s2에 그 파일을 놓고 와야한다.
root@s1:~# ls
Sshpass -p 비밀번호 scp 파일명 보낼이:폴더
를 입력하면 파일을 전송할 수 있다.
root@s1:~# sshpass -p 111111 ssh s2 ls
이처럼 s2에 파일이 전송된 것을 확인할 수 있다.
본격적으로 스크립트를 넘기기 전에 먼저 s2에 공개키를 넘기는 스크립트를 짜자.
ssh-keygen -N '' -f ~/.ssh/id_rsa
를 입력하면, 공개키가 생성되어 /.ssh/id_rsa라는 파일에 저장이 된다.
root@s1:~/_setting# ssh-keygen -N '' -f ~/.ssh/id_rsa
Generating public/private rsa key pair.
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:sO/Y4xn3M3+HY69wNGNEONODuJiMHrb6RoIO2FEpCbU root@s1
The key's randomart image is:
+---[RSA 2048]----+
|.o.. . . +. |
| o.o . =.o |
| Eo .o o . o.. |
| . +o+ . . |
|.. o o.oS = |
|o o . +. o o |
| o + o . . .. |
| . . .+.+ .oo+ o|
| oo.=. .=o=o|
+----[SHA256]-----+
먼저 ssh_keygen.sh 를 만들어 공개키를 저장하고, 공개키를 보내는 스크립트 sendKeys.sh를 만들자.
root@s1:~/_setting# touch ssh_keygen.sh
root@s1:~/_setting# chmod +x ssh_keygen.sh
root@s1:~/_setting# touch sendKeys.sh
root@s1:~/_setting# chmod +x sendKeys.sh
root@s1:~/_setting# ls
Ssh_keygen.sh 안에는 공개키를 저장할 것인데, 서브라임텍스트를 통해 저장할 스크립트는 아래와 같다.
#!/bin/bash
rm -rd ~/.ssh
mkdir ~/.ssh
ssh-keygen -N '' -f ~/.ssh/id_rsa
자, 다음은 우리가 작업할 대상인 두대의 가상머신의 ip를 hosts에 저장하자.
root@s1:~/_setting# cat >> hosts << EOF
> 192.168.56.70
> 192.168.56.77
> EOF
root@s1:~/_setting# cat hosts
192.168.56.70
192.168.56.77
다음은 서브라임텍스트에서 sendKeys.sh를 열어주자.
#!/bin/bash
~/_setting/installSshpass.sh
~/_setting/ssh_keygen.sh
for ip in `cat ~/_setting/hosts`; do
sshpass -p 111111 ssh-copy-id -oStrictHostKeyChecking=no $ip
done
이러면, 키 값을 넘길 떄 호스트키의 핑거프린트를 만들겠냐는 질문 없이 공개키를 잘 넘긴다.
실행해보자.
root@s1:~/_setting# ./sendKeys.sh
==========install_sshpass==========
Reading package lists... Done
Building dependency tree
Reading state information... Done
sshpass is already the newest version (1.05-1).
0 upgraded, 0 newly installed, 0 to remove and 4 not upgraded.
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
Number of key(s) added: 1
Now try logging into the machine, with: "ssh -o 'StrictHostKeyChecking=no' '192.168.56.70'"
and check to make sure that only the key(s) you wanted were added.
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
Number of key(s) added: 1
Now try logging into the machine, with: "ssh -o 'StrictHostKeyChecking=no' '192.168.56.77'"
and check to make sure that only the key(s) you wanted were added.
잘 넘어간 듯 보인다. 확인해보자.
root@s1:~# ls -a
. .bash_history .cache .nano _setting .viminfo
root@s2:~/.ssh# vi authorized_keys
ssh-rsa ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDrs6M3/80EYZ6qvoiPC/H/bIxsZJEqXdEKfy9sy+yCyEgFteidGXeq/4+8Tb0QMAJ+I8Ia6uEFk1hjPCEdnlwNAuIV9GQfi//kHJOTmlPdDoOEvd9w20dfjGvqBmB2nIIMuq+fNeU7GV6C3tGWNc9nTuH+DFZEqmcT1tpp9gxocnJ5Lo/4xvPkzjSSPC9W2H6BxhOkp8V4Tpj7oA8a3OhIUaIyyrMjcUkpXTrWffDDWqdpDU1yxCh2xhV+ddP/bPJEhu52cmWDXnn5m5+xB48NQAMNOfb/Go3bPPW2hLCMazAj7T+WHq3HbSAKE8U291DnkT2UuSCE3jOFzpQ9Lc9V root@s1
root@s1의 공개키가 잘 입력되었음을 알 수 있다.
이제 geth 환경을 한번에 구축해주는 스크립트를 짠 뒤, 추후 본 포스트에 업데이트 할 것이다.
'프로그래밍 & IT > Linux' 카테고리의 다른 글
리눅스(linux) 익히기 - 쉘 스크립트 (0) | 2018.09.02 |
---|---|
리눅스(linux) 익히기 - 리눅스 쉘(bash), 기본 명령어 이해 및 실습 (0) | 2018.09.02 |
리눅스 기본 이해 (0) | 2018.09.02 |
리눅스-우분투(Linux Ubuntu)) ssh와 sublimeText를 이용한 쉘스크립트 동작 (0) | 2018.09.01 |
sublime에서 host key verification failed 에러 해결방법! (0) | 2018.09.01 |