OpenSSH 소개와 설치 가이드

OpenSSH 설치와 운영 가이드입니다. sshd 서비스, Ed25519 공개키, ssh-copy-id, sshd_config 보안 설정, 문법 검사, reload, 클라이언트 config와 로그 분석을 정리했습니다.

OpenSSH는 원격 로그인, 파일 복사, 포트 포워딩, 자동화 실행에 가장 널리 쓰이는 SSH 구현체입니다. 서버 쪽 데몬은 sshd, 클라이언트 명령은 ssh, 파일 복사는 scp/sftp를 사용합니다. 설치보다 중요한 것은 안전한 인증 방식과 설정 변경 검증 순서입니다.

설치와 서비스 확인

# Ubuntu/Debian
sudo apt update
sudo apt install -y openssh-server openssh-client
sudo systemctl enable --now ssh
sudo systemctl status ssh --no-pager

# RHEL/Rocky/Alma
sudo dnf install -y openssh-server openssh-clients
sudo systemctl enable --now sshd
sudo systemctl status sshd --no-pager

listen 상태는 다음처럼 확인합니다.

sudo ss -lntp | grep -E '(:22|sshd)'
ssh -V

접속 기본형

ssh user@server.example.com
ssh -p 2222 user@server.example.com
scp./file.txt user@server.example.com:/tmp/
sftp user@server.example.com

공개키 생성

새 키를 만들 때는 RSA보다 Ed25519를 우선 사용합니다. RSA가 필요한 레거시 장비가 아니라면 다음 형태가 간단하고 안전합니다.

ssh-keygen -t ed25519 -a 100 -f ~/.ssh/id_ed25519 -C "user@workstation"

원격 서버에 공개키를 배포합니다.

ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server.example.com

ssh-copy-id를 쓸 수 없다면 직접 등록합니다.

install -d -m 700 ~/.ssh
cat id_ed25519.pub >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

서버 설정 변경

서버 설정 파일은 보통 /etc/ssh/sshd_config입니다. OpenSSH 공식 매뉴얼처럼 keyword와 argument를 한 줄씩 적는 구조입니다.

# 예시: 운영 정책에 맞게 조정
Port 22
PermitRootLogin no
PubkeyAuthentication yes
PasswordAuthentication no
KbdInteractiveAuthentication no
X11Forwarding no
AllowUsers user deploy

설정 변경 후에는 문법 검사를 먼저 합니다. 원격 서버에서 작업 중이면 기존 SSH 세션을 닫지 말고 새 접속이 되는지 확인한 뒤 종료합니다.

sudo cp -a /etc/ssh/sshd_config /etc/ssh/sshd_config.bak.$(date +%Y%m%d_%H%M%S)
sudo sshd -t
sudo systemctl reload sshd 2>/dev/null || sudo systemctl reload ssh
ssh -vvv user@server.example.com

클라이언트 설정

자주 접속하는 서버는 ~/.ssh/config에 정리합니다.

Host prod-web-01
 HostName 192.168.0.10
 User deploy
 Port 22
 IdentityFile ~/.ssh/id_ed25519
 IdentitiesOnly yes

로그와 문제 해결

# Ubuntu/Debian
sudo tail -n 100 /var/log/auth.log

# RHEL 계열
sudo tail -n 100 /var/log/secure

# systemd 공통
sudo journalctl -u ssh -n 100 --no-pager 2>/dev/null || sudo journalctl -u sshd -n 100 --no-pager

Permission denied는 계정, 키, PAM, AllowUsers를 봅니다. Connection timed out은 네트워크와 방화벽, Connection refused는 sshd listen 상태를 봅니다.

공식 문서

BGM EVER