"Bash 스크립트 root로 실행하게 하기"의 두 판 사이의 차이

 
(사용자 2명의 중간 판 8개는 보이지 않습니다)
1번째 줄: 1번째 줄:
==개요==
==개요==
;Bash 스크립트 root로 실행하게 하기
;Bash 스크립트 root로 실행하게 하기
;root로 스크립트 실행
* root 계정이 아니면 sudo 로 스크립트를 실행하게 함
* root 계정이 아니면 sudo 로 스크립트를 실행하게 함
* 물론 sudo를 수행할 수 있는 권한이 설정되어 있어야 함
* 물론 sudo를 수행할 수 있는 권한 설정(sudoer)되어 있어야 함
<source lang='bash'>
 
if [ $(id -u) -ne 0 ]; then exec sudo bash "$0" "$@"; fi
==예시 1==
<syntaxhighlight lang='bash'>
if [ $(id -u) -ne 0 ]; then exec sudo bash "$0" "$@"; exit; fi


# 여기부터 실제 스크립트 내용
# 여기부터 실제 스크립트 내용
</source>
whoami
<source lang='console'>
</syntaxhighlight>
<syntaxhighlight lang='console'>
jmnote@zetawiki:~$ sh root_action.sh  
jmnote@zetawiki:~$ sh root_action.sh  
root
root
</source>
</syntaxhighlight>
 
==예시 2==
<syntaxhighlight lang='bash'>
if [ "$UID" -ne 0 ]; then exec sudo bash "$0" "$@"; exit; fi
</syntaxhighlight>


==같이 보기==
==같이 보기==
* [[Bash 스크립트 root로 실행했는지 확인]]
{{z컬럼3|
* [[리눅스 sh 명령어로 bash 실행하기]]
* [[리눅스 id]]
* [[리눅스 sudo]]
* [[리눅스 sudo]]
* [[리눅스 root 계정]]
* [[리눅스 root 계정]]
* [[리눅스 id]]
* [[셀프 데몬화 스크립트]]
* [[리눅스 sh 명령어로 bash 실행하기]]
* [[Bash 스크립트 root로 실행했는지 확인]]
* [[리눅스 계정에 특정 명령어 root 실행 권한 주기]]
* [[리눅스 계정에 특정 명령어 root 실행 권한 주기]]
}}
==참고==
* https://askubuntu.com/questions/746350/request-root-privilege-from-within-a-script/748362


[[분류: Bash]]
[[분류: Bash 스크립트]]

2023년 3월 10일 (금) 13:03 기준 최신판

1 개요[ | ]

Bash 스크립트 root로 실행하게 하기
root로 스크립트 실행
  • root 계정이 아니면 sudo 로 스크립트를 실행하게 함
  • 물론 sudo를 수행할 수 있는 권한 설정(sudoer)되어 있어야 함

2 예시 1[ | ]

if [ $(id -u) -ne 0 ]; then exec sudo bash "$0" "$@"; exit; fi

# 여기부터 실제 스크립트 내용
whoami
jmnote@zetawiki:~$ sh root_action.sh 
root

3 예시 2[ | ]

if [ "$UID" -ne 0 ]; then exec sudo bash "$0" "$@"; exit; fi

4 같이 보기[ | ]

5 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}