사용자 도구

사이트 도구


ssh

SSH

SSH timeout

/etc/ssh/sshd_config 수정(서버 쪽)

ClientAliveInterval 120  # 120초마다 확인하는 패킷을 보냄
ClientAliveCountMax 720  # 720번 이상 응답이 없을 때만 접속을 종료

/etc/ssh/ssh_config 수정(클라이언트 쪽), ~/.ssh/config도 설정 가능

Host *
    TCPKeepAlive yes
    ServerAliveInterval 120  # 120초마다 확인하는 패킷을 보냄
    ServerAliveCountMax 720  # 720번 이상 응답이 없을 때만 접속을 종료

password 없이 로그인

(클라이언트에서) 키 생성

ssh-keygen -t rsa

(클라이언트에서 서버에) 키 복사

# (1)
ssh-copy-id -i ${HOME}/.ssh/id_rsa.pub {server-id}@{server-ip}

# (2)
# 클라이언트의 id_rsa.pub 파일내용을 서버의 .ssh/authorized_keys 에 추가하면 되기 때문에,
# 클라이언트의 id_rsa.pub 파일을 서버에 복사하고 authorized_keys에 수동으로 추가해도 됨
scp ${HOME}/.ssh/id_rsa.pub {server-id}@{server-ip}:~/
ssh {server-id}@{server-ip}
cat id_rsa.pub >> .ssh/authorized_keys
rm id_rsa.pub

# (3)
# pem file 복사
cat ~/.ssh/id_rsa.pub | ssh -i "pair.pem" root@10.0.0.1 "cat - >> ~/.ssh/authorized_keys"

(클라이언트에서) 접속

ssh {server-id}@{server-ip}

Windows

1. 클라이언트의 {HOME}/.ssh/id_rsa.pub 내용을 서버의 {HOME}/.ssh/authorized_keys에 추가

2. C:\ProgramData\ssh\sshd_config 에서 다음 두 줄을 주석처리

Match Group administrators
       AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys

3. ssh 서버 재시작

restart-service sshd

SSHFS

SSH remote command

암호없이 로그인 설정 후 사용

ssh {ip} {command}
ssh -t {ip} {command}  # interactive terminal (Error opening terminal: unknown. 에러가 발생할 때 사용)

여러줄 명령어 실행

# 예) working directory 변경후, 명령어 실행
ssh {ip} 'cd /working/directory ; python main.py'

재귀적으로 명령어 실행

# 127.0.0.1에 접속해서, 127.0.0.1에 접속해서, ping 127.0.0.1 실행
ssh 127.0.0.1 "echo 1; ssh 127.0.0.1 ""echo 2; ping 127.0.0.1"""

원격 아나콘다 환경 활성화

ssh {user}@{host} "source /home/rex8312/anaconda3/etc/profile.d/conda.sh; conda activate {virenv}; which python"

python script에서 실행

import subprocess
import shlex
from pprint import pprint
 
 
def get_output(ssh):
    out, _ = ssh.communicate()
    lines = out.split('\n')
    return lines[-3], float(lines[-2].split(':')[1])
 
 
ip_addresses = ['192.168.0.xxx', '192.168.0.xxx']
cmd = "ssh {ip} 'cd /working/directory ; python main.py'"
 
# ssh_list = [subprocess.Popen(shlex.split(cmd.format(ip=ip_address))) for ip_address in ip_addresses]
# [ssh.wait() for ssh in ssh_list]
 
ssh_list = [subprocess.Popen(shlex.split(cmd.format(ip=ip_address)),
                             stdout=subprocess.PIPE, stderr=subprocess.PIPE) for ip_address in ip_addresses]
 
outputs = [(idx, get_output(ssh)) for idx, ssh in enumerate(ssh_list)]
pprint(outputs)

port 변경

  • /etc/ssh/sshd_config의 Port 22 변경

ssh config

~/.ssh/config 에 Host 설정을 저장

Host *
  TCPKeepAlive yes
  ServerAliveInterval 30
  ServerAliveCountMax 720

Host n2
  HostName 192.168.0.102
  User {id}
  Port {port}
  IdentityFile /home/{id}/.ssh/id_rsa.pem

ssh: port forward

ssh.txt · 마지막으로 수정됨: 저자 rex8312