본문 바로가기
개발/에러해결

sudo: export: command not found, sudo export 하고 싶을 때

by amkorousagi 2022. 10. 31.

에러 내용


sudo: export: command not found

sudo: export: command not found

현재 환경 변수 설정을 유지하면서 루트 권한으로 명령어를 실행시키고 싶을 때가 있다.

이때 sudo export로 루트 유저에서의 환경 변수를 설정하려고 시도하면

sudo export ~ 시 위와 같은 오류를 출력하며 실행되지 않는다. 

에러 원인


export는 shell의 내부 상태에 영향을 주는 built-in command(내장 명령어)이다.

이와 같이 shell의 내부 상태에 영향을 주는 built-in command는 sudo로 실행할 수 없다.

예를 들면, 현재 작업 디렉터리를 바꾸는 cd 또한 sudo: cd: command not found로 오류를 출력한다.

(. 과 같이 현재 작업 디렉터리 같은 shell 내부 상태를 요구하는 것도 그러하다)

 

이는 sudo는 루트 권한으로 실행되는 프로세스를 생성할 뿐이지, 루트 유저가 사용할 새로운 shell을 만드는 것이 아니기 때문이다. 따라서 대상이 되는 shell이 존재하지 않으니 shell 내부 상태를 변경하거나 참조하는 명령어는 실행할 수 없는 것이다.

 

*

built-in command와 그렇지 않은 것의 차이

shell과 process의 차이

등에 대해서는 추후에 다시 정리하겠습니다.

해결 방법


 

크게 두 가지가 있다.

 

1. -E 옵션 사용하기

-E 또는 --preserve-env는 기존의 환경 변수 유지하기 위한 옵션이다.

sudo -E <원하는 명령어>

 

2. export 쓰지 않고 변수 설정

export s=a 대신 s=a

sudo <export 없이 변수 초기화> <원하는 명령어>

 

환경에 따라 둘 중 하나만 되는 경우도 있는 듯하다.

 

참고 자료


 

What is the difference between a builtin command and one that is not?

Is there any intrinsic difference between a builtin command and another command which can nominally do the same thing? eg. Do builtins get "special" treatement? ... is there less overhead running

unix.stackexchange.com

 

I have a problem when using "export" command

When I put sudo "export PROXY_LOCAL_NET_IP=10.113.35.108" I get the following error: sudo: export: command not found What should I do to resolve this problem?

askubuntu.com

 

How to export variable for use with sudo?

On Slackware, using sbopkg permits one to build a package from source. Repos is not big as Debian, but it's nice. Some software can use environment variables, for example on the VICE c64 emulator,...

unix.stackexchange.com

 

댓글