본문 바로가기

server/linux

sudo 설정 파일 복구 방법

728x90

 

OS : ubuntu 18.04

[나무위키] sudo 명령어는 /etc/sudoers 설정 파일에 명시되어 있는 User만 사용 가능하다. 이를 수정하려면 visudo 명령어를 이용하는 것이 정석적이다. 이를 사용하지 않을 시 sudoers 파일에 보안상 문제가 생길 수 있다.

 

* vi로 임의 수정해서 sudo 명령어 자체에가 잠겼습니다...

>>> /etc/sudoers: syntax error near line 21 <<<
sudo: parse error in /etc/sudoers near line 21
sudo: no valid sudoers sources found, quitting
sudo: unable to initialize policy plugin
 

pkexec visudo 사용

equipment@equipment-virtual-machine:~$ pkexec visudo
==== AUTHENTICATING FOR org.freedesktop.policykit.exec ===
Authentication is needed to run `/usr/sbin/visudo' as the super user
Authenticating as: equipment,,, (equipment)
Password:
 

 

보통은 패스워드 입력후 수도파일 접근이 가능한데, 나같은경우는 이마저도 에러발생하였다.

equipment@equipment-virtual-machine:~$ pkexec visudo
==== AUTHENTICATING FOR org.freedesktop.policykit.exec ===
Authentication is needed to run `/usr/sbin/visudo' as the super user
Authenticating as: equipment,,, (equipment)
Password:
polkit-agent-helper-1: error response to PolicyKit daemon: GDBus.Error:org.freedesktop.PolicyKit1.Error.Failed: No session for cookie
==== AUTHENTICATION FAILED ===
Error executing command as another user: Not authorized
This incident has been reported.
 

침착하고 오버플로우 검색

(https://askubuntu.com/questions/799669/etc-sudoers-file-corrupted-and-i-cant-run-pkexec-visudo-over-ssh)

대상 서버에 대해 두 개의 ssh 세션을 엽니다. (같은계정)

첫 번째 세션에서 다음을 실행하여 bash의 PID를 가져옵니다.

echo $$
 

두 번째 세션에서 다음을 사용하여 인증 에이전트를 시작합니다.

pkttyagent --process <pid>
 

첫 번째 세션으로 돌아가서 다음을 실행합니다.

pkexec visudo
 

두 번째 세션에서는 암호 프롬프트가 표시됩니다. visudo는 첫 번째 세션에서 시작됩니다.

 

위와 같은 솔루션 후 다시 파일 수정

 

비밀번호를 맞게 입력하면 에디터가 나오는데 수정했던 부분을 다시 수정하고 /etc/sudoers파일로 저장합니다.

 

What now? 
Options are: 
(e)dit sudoers file again 
e(x)it without saving changes to sudoers file 
(Q)uit and save changes to sudoers file (DANGER!) 
What now? Q 
$
 

이때 x를 입력하면 저장이 되지 않습니다.

해결완료

 

728x90