歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> CentOS修改系統環境變量

CentOS修改系統環境變量

日期:2017/2/28 15:46:47   编辑:Linux教程

我這裡拿php作為一個例子,我的php安裝在/usr/local/webserver/php下,沒有把php加入環境變量時,你在命令行執行

#查看當前php的版本信息
[root@CentOS ~]# php -v

會提示你此命令不存在。

下面詳細說說linux下修改環境變量的方法

方法一: 在/etc/profile文件中添加變量【對所有用戶生效(永久的)】
用VI在文件/etc/profile文件中增加變量,該變量將會對Linux下所有用戶有效,並且是“永久的”。

[root@CentOS ~]# vi /etc/profile
在文件末尾加上如下兩行代碼
PATH=/usr/local/webserver/php/bin:$PATH export PATH
# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

pathmunge () {
    case ":${PATH}:" in
        *:"$1":*)
            ;;
        *)
            if [ "$2" = "after" ] ; then
                PATH=$PATH:$1
            else
                PATH=$1:$PATH
            fi
    esac
}


if [ -x /usr/bin/id ]; then
    if [ -z "$EUID" ]; then
        # ksh workaround
        EUID=`id -u`
        UID=`id -ru`
    fi
    USER="`id -un`"
    LOGNAME=$USER
    MAIL="/var/spool/mail/$USER"
fi

# Path manipulation
if [ "$EUID" = "0" ]; then
    pathmunge /sbin
    pathmunge /usr/sbin
    pathmunge /usr/local/sbin
else
    pathmunge /usr/local/sbin after
    pathmunge /usr/sbin after
    pathmunge /sbin after
fi

HOSTNAME=`/bin/hostname 2>/dev/null`
HISTSIZE=1000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
    export HISTCONTROL=ignoreboth
else
    export HISTCONTROL=ignoredups
fi

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL

# By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
    umask 002
else
    umask 022
fi

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

unset i
unset pathmunge

PATH=/usr/local/webserver/php/bin:$PATH
export PATH

要是剛才的修改馬上生效,需要執行以下代碼

[root@CentOS ~]# source /etc/profile

這時再查看系統環境變量,就能看見剛才加的東西已經生效了

[root@CentOS ~]# echo $PATH
/usr/local/webserver/php/bin:/usr/lib/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

現在就能直接使用php命令了(而不是像之前寫很長一串/usr/local/webserver/php/bin/php -v),例如查看當前php的版本

[root@CentOS ~]# php -v
PHP 5.3.8 (cli) (built: Jun 27 2012 14:28:20) 
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies

方法二:在用戶目錄下的.bash_profile文件中增加變量【對單一用戶生效(永久的)】
用VI在用戶目錄下的.bash_profile文件中增加變量,改變量僅會對當前用戶有效,並且是“永久的”。具體操作和方法1一樣,這裡就不在列舉代碼了。

方法三:直接運行export命令定義變量【只對當前shell(BASH)有效(臨時的)】

在shell的命令行下直接使用[export變量名=變量值]定義變量,該變量只在當前的shell(BASH)或其子shell(BASH)下是有效的,shell關閉了,變量也就失效了,再打開新shell時就沒有這個變量,需要使用的話還需要重新定義。例如

export PATH=/usr/local/webserver/php/bin:$PATH
Copyright © Linux教程網 All Rights Reserved