LoginSignup
2
0

More than 1 year has passed since last update.

環境変数を永続化させる方法(RHEL8)

Last updated at Posted at 2022-10-26

皆さんこんにちは!haruです。

環境変数は、export コマンド(UNIX)や set コマンド(Windows)によって、セッション単位で設定することができますが、セッションが切れてしまうと無効化されてしまいます。
そこで今回は、RHEL環境で環境変数を永続化させる方法について共有します。

記載するファイル

設定ファイルは、以下の 1 → 2 → 3 → 4 の順で読み込まれていきます。
今回は以下のうち、[.bash_profile]に記載をします。

  • 1. /etc/profile
    ・bashがログインシェルとして起動された際、最初に読み込まれる設定ファイルです。
    ・全ユーザに適用されます。

  • 2. ~/.bash_profile
    ・「/etc/profile」の次に読み込まれるファイルです。
    ・特定ユーザに適用されます(「~/」配下のため)

  • 3. ~/.bash_login
    ・「~/.bash_profile」が存在しない場合、「/etc/profile」から2番目に読み込まれるファイルです。
    ・特定ユーザに適用されます(「~/」配下のため)

  • 4. ~/.profile
    ・「~/.bash_profile」が存在しない場合、「/etc/profile」から3番目に読み込まれるファイルです。
    ・特定ユーザに適用されます(「~/」配下のため)
    ・②③と異なり、sh等の別のシェルからでも読み込まれます。

現状の確認

環境変数を設定していない場合は、コマンドが通りませんね。
コマンド情報が格納されている bin の位置がわからないためです。

[postgres@primary ~]$ pg_ctl
bash: pg_ctl: コマンドが見つかりませんでした...

環境変数の設定

vi コマンドで記載していきます。

[root@primary pgsql]# su - postgres
[postgres@primary ~]$ vi .bash_profile
[postgres@primary ~]$ cat .bash_profile
[ -f /etc/profile ] && source /etc/profile
PGDATA=/var/lib/pgsql/15/data
export PGDATA
# If you want to customize your settings,
# Use the file below. This is not overridden
# by the RPMS.
[ -f /var/lib/pgsql/.pgsql_profile ] && source /var/lib/pgsql/.pgsql_profile
export PATH=$PATH:/usr/pgsql-15/bin
export MANPATH=$MANPATH:/usr/pgsql-15/share/man
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/pgsql-15/lib
export PGDATA=/var/lib/pgsql/15/data

設定の反映

[postgres@primary ~]$ source ~/.bash_profile

セッションの再接続

無事認識されています。

[postgres@primary ~]$ pg_ctl
pg_ctl: 操作モードが指定されていません
詳細は"pg_ctl --help"を実行してください。

以上です!

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0