사용자 도구

사이트 도구


linux:ssh

문서의 이전 판입니다!


Linux SSH

Private Key 생성

ssh-keygen -t rsa
  • Passphrase를 항상 넣는 것이 좋다.
  • id_rsa 파일은 개인키로 자신의 PC(Client)에 둔다.
  • *.pub 파일은 공개키로 접속 대상 서버 계정의 ~/.ssh/authorized_keys 파일에 그 내용을 추가한다.
  • ~/.ssh에 있는 파일은 600 권한으로 설정해야한다. 해당 디렉토리도 권한 확인이 필요하다.
    chmod 700 ~/.ssh

ssh-agent

  • ssh-agent에 Private Key를 등록해서 SSH 터널링시 자동 접속 할 수 있다.
  • ssh-agentexec ssh-agent /bin/bash로 실행해야 한다.
exec ssh-agent /bin/bash
ssh-add ~/.ssh/id_dsa
  • 키 등록이 안 될 경우 키 파일의 권한이 0600이 맞는지 확인 해 본다.
chmod 600 ~/.ssh/id_rsa*

SSH 접속 인사 텍스트

  • /etc/motd 파일 편집하면 SSH 접속시 인사말을 지정할 수 있다.
  • 그런데 update-motd 애플리케이션이 작동하고 있을 때는 /etc/update-motd.d 의 스크립트가 순서대로 실행된 결과로 매번 /etc/motd 파일이 덮어써진다.
  • 따라서 update-motd 사용시에는 /etc/update-motd.d에 원하는 스크립트를 넣어둬야 한다.

~/.ssh/config

  • ~/.ssh/config를 통해 SSH 접속을 단순화할 수 있다.
  • ~/.ssh/config
    # 기본형태. ~/.ssh/id_rsa 를 사용할 경우 혹은 일반 비밀번호 인증의 경우
    Host dev
        HostName IP 혹은 hostname
        User 계정명
        IdentitiesOnly yes
     
    # 특정 key 지정
    Host company
        HostName IP 혹은 hostname
        User 계정명
        PreferredAuthentications publickey
        IdentityFile ~/.ssh/[private key]
        IdentitiesOnly yes
     
    # github.com git 계정 접속시에 대한 처리
    Host github.com
        HostName github.com
        User git
        IdentityFile ~/.ssh/[github private key]
        IdentitiesOnly yes
     
    # 동일 2차 도메인 사용시
    Host *.example.com
        User 계정명
        PreferredAuthentications publickey
        IdentityFile ~/.ssh/[private key]
        IdentitiesOnly yes
  • 동일 HostName, 동일 User 다중 Key Host github.com-SSHID 에서 Host 의 값을 github.com-myid 형태로 특정 값을 줘서 처리할 수 있고, 그 뒤 부터 ssh github.com-myid 같은 호출이 정상 작동한다.
    • gitolite - How could I stop ssh offering a wrong key? - Server Fault IdentitiesOnly yes 설정도 주는게 좋다. 그렇지 않으면 우선 순위상에 있는 다른 키 파일들 뒤에 IdentityFile에 설정한 파일이 뒤로 추가되는 형태로 시도된다.(~/.ssh/id_dsa, ~/.ssh/id_rsa, 등이 더 우선된다) ===== 접속 유지 Keep connection ===== * How to Keep SSH Connections Alive In Linux * 전역 설정은 /etc/ssh/ssh_config 파일에 다음을 추가<code sh> ServerAliveInterval 60 # 60은 60초를 의미함. 60초마다 null 패킷 전송 </code> * 현재 접속 사용자에 대한 설정은 ~/.ssh/config<code.sh> Host * ServerAliveInterval 60 # 혹은 특정 호스트 지정 Host *hostname.com ServerAliveInterval 60 </code> * 권한 설정<code sh> chmod 600 ~/.ssh/config </code> * ssh 데몬 재시작<code sh> sudo service ssh restart </code> 혹은 명령행 옵션을 직접 줘도 됨 <code sh> ssh -o ServerAliveInterval=60 hostname </code> ===== Text 파일 Gzip 압축 전송 : 다른 서버의 파일을 현재 서버로 전송 ===== * scp with compression. | commandlinefu.com <code sh> ssh 10.0.0.4 “cat /tmp/backup.sql | gzip -c1” | gunzip -c > backup.sql # or ssh 10.0.0.4 “gzip -c /tmp/backup.sql” |gunzip > backup.sql # 압축을 풀 생각이 없다면 ssh 10.0.0.4 “gzip -c /tmp/backup.sql” > backup.sql.gz </code> ===== 디렉토리/파일 압축전송 : 현재 서버의 파일을 다른 서버로 전송 ===== * Howto: Use tar Command Through Network Over SSH Session * /dev/blog » scp from stdin * 디렉토리를 통째로 전송하기<code sh> # gzip 보다 압축률 좋은 xz 사용. tar cJ a/dir | ssh user@remotehost.com “cat >outfile.tar.xz” </code> ===== sshpass ===== * ssh, scp 등의 비밀번호를 자동으로 입력해준다.<code sh> # 비밀번호가 'SSHPASS' 환경변수로 전달됨. # bash로 부터 받을 때는 export SSHPASS 명시 sshpass -e ssh -o StrictHostKeyChecking=no 아이디@호스트주소 명령어 # 비밀번호 직접입력 sshpass -p비밀번호 ssh -o StrictHostKeyChecking=no 아이디@호스트주소 명령어 # 비밀번호 파일. 일반 텍스트로 비밀번호 넣어둠. 파일 권한을 others, group은 읽고 쓰기 못하게 할 것. sshpass -f /path/to/passfile ssh -o StrictHostKeyChecking=no 아이디@호스트주소 명령어 # expect와 함께 sshpass -f 사용시 ~ 을 통한 사용자 디렉토리 지정은 작동하지 않았음. </code> * 리눅스 sshpass 사용법 ===== 명령 실행 execute command ===== 아래는 긴 명령을 ssh를 통해 실행하는 방법이다. 예제 자체는 jstat으로 GC 상황 모니터링하는 것. <code sh> ssh myhostname 'bash -s' «'ENDSSH' # commands… ENDSSH </code> ===== known_hosts 에 미리 호스트 추가 ===== SSH known_host 메모 <code sh> ssh-keyscan -t rsa host명 » ~/.ssh/known_host ssh-keyscan -t rsa -f host명들 이들어있는 파일명 » ~/.ssh/known_host </code> ===== no matching cipher found. ===== 아래와 같은 오류 발생시.. » no matching cipher found. Their offer: aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,arcfour,aes192-cbc,aes256-cbc,rijndael128-cbc,rijndael192-cbc,rijndael256-cbc,rijndael-cbc@lysator.liu.se ~/.ssh/config 마지막에 다음과 같은 설정 추가 <code> Ciphers aes128-cbc,aes192-cbc,aes256-cbc # 필요한 경우 KexAlgorithms +diffie-hellman-group1-sha1 </code> 혹은 명령행에 암호화 방식을 -c'' 옵션으로 직접 지정한다.
ssh -c aes128-cbc kwon37xi@xxx.xx.xx.xx
linux/ssh.1573653952.txt.gz · 마지막으로 수정됨: 2019/11/13 23:05 저자 kwon37xi