termux ubuntu sshd 멈춤 해결 방법

Termux Ubuntu(proot-distro)에서 sshd가 주기적으로 멈추는 이유와 해결법입니다. proot 환경의 PID 1 제한 + Android 배터리 최적화가 원인입니다.

주요 원인

  1. proot 종료 시 sshd도 종료 (부모 프로세스 kill)
  2. Android PPK (백그라운드 프로세스 kill)
  3. 포트 충돌 (Termux 기본 8022와 겹침)
  4. 로그아웃 타임아웃 (KeepAlive 없음)

완전 해결 단계

1단계: 올바른 sshd 설정

Ubuntu 내 (proot 로그인 후):

bash
# 패키지 설치
sudo apt install openssh-server -y

# 호스트키 생성
sudo ssh-keygen -A

# 포트 변경 (Termux 8022와 구분)
sudo sed -i 's/#Port 22/Port 10022/' /etc/ssh/sshd_config
sudo sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config

# KeepAlive 활성화 (주요 해결!)
echo "ClientAliveInterval 60" | sudo tee -a /etc/ssh/sshd_config
echo "ClientAliveCountMax 3" | sudo tee -a /etc/ssh/sshd_config

sudo systemctl restart ssh # 또는 수동 시작

2단계: tmux로 sshd 데몬화 (프로세스 생존)

Ubuntu 내 전용 tmux 세션:

bash
apt install tmux -y
tmux new-session -d -s sshd
tmux send-keys -t sshd "/usr/sbin/sshd -D -p 10022" Enter
tmux list-sessions # 확인

tmux 세션은 proot 종료해도 살아있음.

3단계: Termux/Android 최적화

Termux 앱:

text
termux-wake-lock # 배터리 최적화 무시

Android 설정:

  • 설정 > 앱 > Termux > 배터리 > 최적화 안함
  • 개발자 옵션 > 백그라운드 프로세스 제한 없음

4단계: 접속 테스트

text
# 외부에서 (포트 10022)
ssh -p 10022 root@[폰IP]

# 내부 Termux에서
ssh -p 10022 root@localhost

모니터링 & 유지

bash
# Ubuntu 내 상태 확인
ss -tlnp | grep 10022
ps aux | grep sshd

# tmux 재연결 (sshd 멈췄을 때)
tmux attach -t sshd

자동 시작 스크립트 (~/.profile)

bash
#!/bin/bash
# Ubuntu 로그인 시 자동 sshd 재시작
if ! pgrep -f "sshd.*10022" > /dev/null; then
tmux new-session -d -s sshd "/usr/sbin/sshd -D -p 10022"
echo "sshd restarted on port 10022"
fi

chmod +x ~/.profile

tmux + KeepAlive 조합으로 99% 안정화됩니다. proot 종료해도 SSH 연결 유지! 포트 ip a로 확인

error: Content is protected !!