2.log

Shell 과 bashrc 본문

HACKING/Bandit+

Shell 과 bashrc

2.log 2023. 4. 1. 21:57
728x90
반응형

 

Shell

사용자(user)와 커널(Kernel) 사이에서 매개체 역할하는 프로그램

사용자로부터 명령을 받아 프로세싱 하기 위해 커널에 넘겨주어 사용자가 커널에 직접 명령 내릴 수 있게 함

 

 

bashrc

Bourne Again Shell 의 축약어로, 리눅스에서 가장 널리 사용되는 쉘

보통 5개의 공통된 설정 파일 가지며 일반적으로 전역 파일은 /etc 디렉토리,

지역 파일(특정 사용자에게만 영향 끼치는 파일)은 사용자의 홈 디렉토리에서 숨김파일( ' . ' 으로 시작) 형태로 존재함

 

 

파일종류

  • /etc/profile
  • ~/.bash_profile
  • ~/.bashrc
  • /etc/bashrc
  • ~/.bash_logout

 

파일설명

/etc/profile

환경변수와 bash 가 수행될 때 실행되는 프로그램 제어하는 전역 시스템 설정과 관련된 파일

bashrc와 마찬가지로 로그인 시 수행되는 시스템 전체 환경 설정 파일

 

 

~/.bash_profile

환경변수와 bash 수행 시 실행되는 프로그램 제어하는 지역 시스템 설정과 관련된 파일

이 환경변수는 오직 해당 사용자에게 한정됨

전역 설정 파일인 /etc/profile 수행된 후 곧바로 수행 됨

 

 

~/.bashrc

alias(별칭)와 bash 수행 시 실행되는 함수를 제어하는 지역 시스템 설정과 관련된 파일

별칭과 함수는 오직 해당 사용자에게 한정됨

 

.bash_profile : bash 가 login shell 로 쓰일때 (처음 로그인 할 때) 수행 됨
.bashrc : bash 실행될 때마다 수행 됨

 

/etc/bashrc

~/.bashrc 와 달리 전체용(모든 사용자에게 영향 미침) 환경 설정 파일

 

 

~/.bash_logout

사용자가 로그아웃 하기 직전 실행하는 프로그램에 관한 bash의 지역 시스템 설정과 관련된 파일

오직 해당 프로그램 실행하는 사용자에게 영향 미침

 

 

 

호출순서

OS 는 /etc/profile 과 ~/.bashprofile 2개 순서대로 호출

각 파일은 내부적으로 다른 파일 호출

 

  • 리눅스(OS)  >>  /etc/profile  >> ...
  • 리눅스(OS)  >>  ~/.bash_profile  >>  ~/.bashrc  >> /etc/bashrc

 

 

실행순서

/etc/profile   >>   /.bash_profile   >>   /.bashrc   >>   /etc/bashrc     

 

/.bash_logout

 

 

 

코드분석

# /etc/profile

execute /etc/profile
IF ~/.bash_profile exists THEN
    execute ~/.bash_profile
ELSE
    IF ~/.bash_login exist THEN
        execute ~/.bash_login
    ELSE
        IF ~/.profile exist THEN
            execute ~/.profile
        END IF
    END IF
END IF

>> .bash_profile 있으면 실행

 

 

# /.bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

>> .bashrc 있으면 실행

 

 

# /.bashrc

# cat ~/.bashrc
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

>> /etc/bashrc 있으면 실행

 

 

# /etc/bashrc

# System wide functions and aliases
# Environment stuff goes in /etc/profile

... 중략 ...

    for i in /etc/profile.d/*.sh; do 
        if [ -r "$i" ]; then
            if [ "$PS1" ]; then
                . $i
            else
                . $i >/dev/null 2>&1
            fi
        fi
    done

	unset i
	unset pathmunge
fi
# vim:ts=4:sw=4

>> /etc/profile.d 폴더 내의 모든 sh 파일 실행

 

 

# when logout

IF ~/.bash_logout exists THEN
    execute ~/.bash_logout
END IF

 

 

 

 

※ 오류 지적 감사히 받겠습니다..

 

 

 

 

 

[출처]

 

https://dohk.tistory.com/191

 

쉘의 개념, bashrc의 개념

쉘(Shell) 쉘이란, 사용자와 커널 사이의 매개체 역할을 하는 프로그램이다. 사용자가 커널에 직접 명령을 내릴 수 있게하는 프로그램이다. 즉, 쉘은 사용자로부터 명령을 받아서 그것을 프로세싱

dohk.tistory.com

 

https://zetawiki.com/wiki/Profile_bashrc_bash_profile_%EC%8B%A4%ED%96%89_%EC%88%9C%EC%84%9C#cite_note-1

 

profile bashrc bash profile 실행 순서 - 제타위키

✔️ 페도라 11 (Leonidas)에서 테스트하였습니다. bash 쉘 초기화 파일 실행 순서 profile bashrc bash_profile 실행 순서 profile bashrc bash_profile 호출 순서 1 실행순서 (간단히 정리)[ | ] 기본 설정에서 실행 순

zetawiki.com

 

https://www.thegeekstuff.com/2008/10/execution-sequence-for-bash_profile-bashrc-bash_login-profile-and-bash_logout/

 

Execution sequence for .bash_profile, .bashrc, .bash_login, .profile and .bash_logout

Execution sequence for .bash_profile, .bashrc, .bash_login, .profile and .bash_logout by Ramesh Natarajan on October 6, 2008 This article will explain the sequence in which the following files are executed: /etc/profile ~/.bash_profile ~/.bashrc ~/.bash_lo

www.thegeekstuff.com

 

728x90
반응형
Comments