Ssh

오픈소스 비즈니스 컨설팅
둘러보기로 가기 검색하러 가기

ssh 사용법을 정리 합니다.

SSH 개요

SSH Key 관리

  • RSA 암호화 방식으로 공개키와 비밀키를 생성 합니다.
  • 암호화 방식 : rsa, dsa, rsa1
  • ~/.ssh/authorized_keys : Client의 공개키를 저장
  • ~/.ssh/id_rsa.pub : 공개키
  • ~/.ssh/id_rsa : 비밀키
  • ~/.ssh/known_hosts : SSH 방식으로 접속한 사용자 정보 저장
cd
### ssh-keygen -t rsa   : DSA 암호화 방식을 사용할 경우
ssh-keygen -t rsa
### Enter file in which to save the key (/home/hduser/.ssh/id_rsa): 비밀키를 저장할 파일 경로 입력
### Enter passphrase (empty for no passphrase): 비밀번호 입력 (자동 로그인을 원할 경우 enter 키를 누르세요)

chmod 700 ~/.ssh
chmod 644 ~/.ssh/*
chmod 600 ~/.ssh/id_rsa
  • Key를 다른 서버로 복사
ssh-copy-id -i ~/.ssh/id_rsa.pub  hduser@node201.hadoop.com
### ssh hduser@node201.hadoop.com 명령을 사용하여 암호 입력 없이 로그인 가능
  • Key를 다른 서버로 복사 2
scp ~/.ssh/id_rsa.pub hduser@node201.hadoop.com:aaa
### 다른 서버에서 아래 작업을 진행
cat aaa >> ~/.ssh/authorized_keys

사용자 매뉴얼

환경 설정 파일

  • /etc/ssh/sshd_config
  • /etc/hosts.allow
  • /etc/hosts.deny


명령어 실행

  • 원격 명령어 실행
ssh hduser@node201.hadoop.com "ls -alF"

파일 전송

  • 파일/폴더 전송
  • -P 포트
  • -r 하위 폴더도 모두 전송
scp -P 22 ~ hduser@node201.hadoop:/home/hduser/~

접속 차단 1

vi /etc/ssh/sshd_config
​    PermitRootLogin yes
    # PermitRootLogin forced-commands-only
    PasswordAuthentication yes

service ssh restart

접속 차단 2

authorized_keys

no-port-forwarding,no-agent-forwarding,no-X11-forwarding,command="echo 'Please login as the user \"ec2-user\" rather than the user \"root\".';echo;sleep 10" ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCRxVopA1d+44k4WYto7pYoEwdbOeOEnANLmoqcN+BrpGAbxMoGyhBJa1bx+q+Bst/33s0R03D1TA/Xzwe92QWcEDKA/toY2eQXG1rcOoji+wgbPem1OMu2iPd7+yax5BvOVM2pFUjzpX2xwuo2OpH9oYqUquFq2rxSla3HlqMzWiCBVFnHvIvPYw1dra/9sQA7bED4PeFf8uYRM8OqLKR0/AdPspe5e3grfM8crdxVEpaXNbKZQm/dZ5cUH05OnxC+XqdzNd7z5mh/WcRdX2eKOH6bQxfS1Nq7EUk8DkNZ/FHCLo0uSGxs0O/ozYGkcWH8Trb696Ge7ZjixyZxP4UH KeyPairPnusPublic

Putty에서 사설키로 자동 접속

.ssh/id_rsa 파일을 ~.pem 파일로 복사

puttygen.exe 파일을 실행

Conversions -> Import Key 메뉴를 선택한 후 ~.pem 파일을 지정

Save private key 메뉴를 선택하여 ~.ppk 파일로 저장

Putty에서 접속 -> SSH -> 인증 메뉴에서 ~.ppk 등록한 후 세션 메뉴에서 user@server 로 접속


FileZilla에서 사설키로 자동 접속

"편집 -> 설정 -> 연결 -> FTP -> SFTP -> Add key file" 메뉴에서 ~.pem 파일을 등록 합니다.

SSH Tunnel

ssh ${user}@{server_ip} -p ${port} -L localhost:80:{server_ip}:80
예) ssh vagrant@127.0.0.1 -p 2222 -L 110.10.129.50:80:127.0.0.1:80 -o IdentityFile="/work/contributhon2018/vagrant_CentOS7/.vagrant/machines/default/virtualbox/private_key"

참고 문헌