GrepBoard/매뉴얼
605 편 ·

sudo: unable to resolve host 경고 없애기

·시험 환경 Ubuntu 24.04 · sudo 1.9.15p5 (Docker) · 2026-10·시험 범위 ↓·5 단계

호스트 이름을 바꾼 뒤 sudo: unable to resolve host 가 뜨면 /etc/hosts 에 새 이름이 없는 것입니다. 127.0.1.1 의 이름을 hostname 과 맞추고 getent 로 봅니다.

output
sudo: unable to resolve host server1: Name or service not known

sudo 가 지금 호스트 이름(server1)을 주소로 바꾸지 못했다는 경고입니다. 거의 언제나 hostnamectl set-hostname 등으로 이름을 바꾸고 /etc/hosts 의 127.0.1.1 줄은 옛 이름으로 둔 경우입니다. 그 줄을 새 이름으로 고치면 사라집니다.

[1] 명령이 실제로 실행됐는지 먼저 봅니다. 경고만 찍히고 종료 코드는 0 이었습니다. 급한 작업은 그대로 해도 됩니다.

console
$ sudo true; echo "exit=$?"sudo: unable to resolve host server1: Name or service not knownexit=0

[2] 지금 이름과 /etc/hosts 를 나란히 봅니다.

console
# hostnameserver1# cat /etc/hosts127.0.0.1 localhost127.0.1.1 oldname ::1     ip6-localhost ip6-loopback

127.0.1.1 줄에 옛 이름(oldname)이 남아 있습니다. getent hosts 는 같은 /etc/nsswitch.conf 순서로 이름을 찾습니다. 아무것도 안 나오고 종료 코드가 2 면 이름이 어디에도 없다는 뜻입니다.

console
# getent hosts "$(hostname)"; echo "exit=$?"exit=2

[3] 127.0.1.1 줄의 이름을 hostname 과 같게 고칩니다. 먼저 파일을 백업하고 그 줄이 어떻게 생겼는지 봅니다. 잘못 고쳤으면 sudo cp -a /etc/hosts.bak /etc/hosts 로 되돌립니다.

console
$ sudo cp -a /etc/hosts /etc/hosts.bak$ grep -n '^127\.0\.1\.1' /etc/hosts2:127.0.1.1 oldname

줄 모양에 따라 고치는 법이 다릅니다.

grep 결과할 일
127.0.1.1 옛이름 하나뿐아래 sed 한 줄
FQDN·별칭이 같이 있음(127.0.1.1 old.example.com old)sudoedit /etc/hosts 로 열어 이름만 바꾼다 — sed 는 줄 전체를 바꿔 별칭을 지운다
아무것도 안 나옴아래 tee -a 로 줄을 더한다

sudo 는 경고만 내고 실행은 되므로 이 명령들은 그대로 됩니다. 둘 다 $(hostname) 으로 지금 이름을 넣으므로 그대로 복사해도 됩니다.

console
$ sudo sed -i "s/^127\.0\.1\.1[[:space:]].*/127.0.1.1 $(hostname)/" /etc/hosts

줄이 없을 때만 더합니다(있는데 더하면 같은 주소 줄이 둘이 됩니다).

console
$ echo "127.0.1.1 $(hostname)" | sudo tee -a /etc/hosts127.0.1.1 server1

고친 뒤 파일은 이렇게 됩니다.

console
# cat /etc/hosts127.0.0.1 localhost127.0.1.1 server1 ::1     ip6-localhost ip6-loopback

[4] 조회와 sudo 를 다시 봅니다.

console
# getent hosts "$(hostname)"127.0.1.1       server1$ sudo true; echo "exit=$?"exit=0

FQDN 을 같이 적은 경우에는 hostname -f 도 확인합니다.

console
# getent hosts server1; hostname -f127.0.1.1       server1.example.com server1server1.example.com

[5] 다음에 이름을 바꿀 때는 두 곳을 같이 바꿉니다. hostnamectl set-hostname 은 /etc/hostname 만 바꾸고 /etc/hosts 는 그대로 둡니다. 클라우드 VM 에서 고친 내용이 재부팅 뒤 되돌아가면 cloud-init 의 manage_etc_hosts 를 봅니다. true 면 부팅할 때마다 /etc/cloud/templates/hosts.tmpl 로 파일을 다시 만들므로, 고칠 곳은 /etc/hosts 가 아니라 그 템플릿입니다(cloud-init 모듈 문서 「Update Etc Hosts」).

이름을 바꾼 김에 SSH 설정도 손봤다면 다시 올리기 전에 sshd 설정 검사 글의 sshd -t 를 먼저 돌립니다.

#sudo#hostname#etc-hosts#hostnamectl