autossh 시스템 스크립트는 시작 시 실패하고 systemctl 재시작 시 작동합니다.

autossh 시스템 스크립트는 시작 시 실패하고 systemctl 재시작 시 작동합니다.

모니터링 서버가 시작될 때 원격 서버로 포트를 전달하도록 autossh 서비스를 설정하려고 합니다. 약간의 속임수를 쓴 끝에 결국 아주 기본적인 시스템 스크립트를 완성했지만 한 가지 문제가 있었습니다. 부팅 중에는 서비스가 제대로 시작되지 않지만 수동으로 시작하거나 재부팅 후 즉시 서비스가 정상적으로 시작됩니다.

Description=AutoSSH tunnel service for Grafana to access mariadb on remote; local port 33060
After=network-online.target

[Service]
Environment="AUTOSSH_GATETIME=0"
ExecStart=/usr/bin/autossh -M 0 -N [email protected] -i /home/myuser/.ssh/id_rsa -o \"ServerAliveInterval 30\" -o \"ServerAliveCountMax 3\" -L 172.17.0.1:33307:127.0.0.1:3306

[Install]
WantedBy=multi-user.target

시작 시 연결할 수 없습니다. Journalctl의 출력은 다음과 유사합니다.

-- 
-- The job identifier is 152.
Apr 07 15:25:31 monitoring autossh[752]: port set to 0, monitoring disabled
Apr 07 15:25:31 monitoring autossh[752]: starting ssh (count 1)
Apr 07 15:25:31 monitoring autossh[752]: ssh child pid is 759
Apr 07 15:25:31 monitoring autossh[759]: bind [172.17.0.1]:33307: Cannot assign requested address
Apr 07 15:25:31 monitoring autossh[759]: channel_setup_fwd_listener_tcpip: cannot listen to port: 33307
Apr 07 15:25:31 monitoring autossh[759]: Could not request local forwarding.

즉시 sudo systemctl restart autossh-mariadb-tunnel.service를 실행하면 연결이 제대로 작동합니다.

답변1

쉿. 여기 질문과 답변을 1시간 넘게 고민하고 덕덕고 결과를 살펴보았지만, 질문을 제출했을 때 그 답변이 저에게 주어졌습니다. 단서는 바인딩 주소에 있습니다. 172.17.0.1은 도커 인터페이스입니다. 문제는 내가 docker 인터페이스를 듣고 있지만 스크립트에 docker를 기다리라고 지시하지 않는다는 것입니다.

그래서 2번째 줄을 다음과 같이 수정했습니다.

After=network-online.target docker.service

다시 시작하면 모든 것이 예상대로 작동합니다.

관련 정보