왜 .profile 파일이 적용되지 않을까

이 글은 ~/.profile 파일이 왜 로드되지 않는지 설명한다.


나는 컨테이너를 활용할 땐 항상 WSL2를 사용한다. 이 때 재밌는 이슈가 있다.

자주 사용하는 퍼블릭 클라우드의 SSH 머신의 IP를 ~/.profile 파일에 export A=B 와 같이 등록해두고 사용하는 등 여러 상수들을 환경 변수로 등록하고 사용했는데, WSL2를 새 터미널로 열면 해당 환경변수가 로딩되지 않았다. 그래서 항상 source ~/.profile을 실행했었는데, 이 글에선 이 이유를 알아보고 그 해결법까지 소개한다.


bash_profile, bash_login, profile 파일

[StackOverFlow]

문제는 생각보다 비직관적이고 간단했는데 ~/.bash_profile 혹은 ~/.bash_login 파일이 존재하면, ~/.profile 파일은 로드되지 않는다고 한다.

나의 경우 bash와 관련된 파일은 .bash_profile, .bashrc가 있었고 추가적으로 .bash_logout, .bash_history 파일이 존재했다.

재밌게도 ~/.bash_profile 파일의 마지막 라인은 source ~/.bashrc 이다. 기본적으로 로딩되는 파일이 아니기 때문이다. (사실 애초에 둘의 차이도 잘 모르겠다.)


Login Shell인지 아는 방법?

[StackOverFlow]

참고로 WSL2는 직접 로그인하는 과정이 생략되지만 Login Shell이다.

shopt -q login_shell && echo 'Login shell' || echo 'Not login shell'를 실행해보면 알 수 있다.


Login Shell의 설정 파일 읽기 루틴

Bash Startup Files

When a “login shell” starts up, it reads the file /etc/profile and then ~/.bash_profile or ~/.bash_login or ~/.profile (whichever one exists - it only reads ONE of these, checking for them in the order mentioned).

When a “non-login shell” starts up, it reads the file /etc/bashrc and then the file ~/.bashrc.

Note that when bash is invoked with the name sh, it tries to mimic the startup sequence of the Bourne shell (sh). In particular, a non-login shell invoked as sh does not read any dot files by default. See the bash man page for details.

충격적이게도, 가장 기본 옵션인 ~/.profile이 무시될 수도 있었다. (대신 모든 사용자를 대상으로 하는 /etc/profile 파일은 무시되지 않고 항상 가장 처음으로 로딩된다.)

현재 Bash가 나의 로그인 셸이고 해당 셸의 설정파일인 ~/.bash_profile이 존재하기 때문에 해당 파일을 로딩한다는 것이다. (~/.bash_profile > ~/.bash_login, ~/.profile 순으로 찾으며, 하나라도 찾으면 해당 파일만 로딩된다고 한다.)


솔루션

솔루션은 크게 두 가지가 있다.

아래 솔루션은 bash를 사용하는 경우로 한정되지만 애초에 bash를 사용하지 않았다면 .profile이 로딩됐을 것이므로 조건에서 생략한다.

  1. bash_profile 혹은 bashrc 파일만 사용하기

  2. bash_profile에 아래 내용 추가하기

    1
    2
    3
    4
    # Load user profile file
    if [ -f ~/.profile ]; then
    . ~/.profile
    fi

추가 참고자료

[StackOverFlow]

[StackOverFlow]

왜 .profile 파일이 적용되지 않을까

https://jsqna.com/linux-profile-not-loaded/

Author

Seongbin Kim

Posted on

21-09-19

Updated on

21-09-19

Licensed under

댓글