设置Ubuntu环境变量 PATH 的方法 及配置备份

在Ubuntu中有如下几个文件可以设置环境变量 

/etc/profile:在登录时,操作系统定制用户环境时使用的第一个文件,此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行. 

/etc/environment:在登录时操作系统使用的第二个文件,系统在读取你自己的profile前,设置环境文件的环境变量 

~/.profile:在登录时用到的第三个文件是.profile文件,每个用户都可使用该文件输入专用于自己使用的shell信息,当用户登录时,该文件仅仅执行一次!默认情况下,他设置一些环境变量,执行用户的.bashrc文件. 

/etc/bashrc:为每一个运行bash shell的用户执行此文件.当bash shell被打开时,该文件被读取. 

~/.bashrc:该文件包含专用于你的bash shell的bash信息,当登录时以及每次打开新的shell时,该该文件被读取. 

通常设置环境变量有三种方法: 
一、临时设置 
export PATH=/home/yan/share/usr/local/arm/3.4.1/bin:$PATH 

二、当前用户的全局设置 
打开~/.bashrc,添加行: 
export PATH=/home/yan/share/usr/local/arm/3.4.1/bin:$PATH 

使生效 

source .bashrc 
三、所有用户的全局设置 
$ vim /etc/profile 
在里面加入: 
export PATH=/home/yan/share/usr/local/arm/3.4.1/bin:$PATH 
使生效 
source profile 

测试 echo $PATH 
有两种设置环境变量 PATH 的方法。第一种适用于为单一用户设置 PATH,第二种是为全局设置 PATH。 

1.在用户主目录下有一个 .bashrc 文件,可以在此文件中加入 PATH 的设置如下: 
用gedit /root/.bashrc打开 
export PATH=”$PATH:/your path1/:/your path2/…..” 
注意:每一个 path 之间要用 “:“ 分隔。 

2.在/etc/profile中增加。 
PATH="$PATH:/home/zhengb66/bin" 
export PATH 
如果要使得即时生效,使用 source 命令 
source .bashrc 
完成。 

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

【以上转自】http://blog.mcuol.com/User/For_ATIW/Article/11709_1.htm,另以下所介绍的方法只在一个终端中生效,另外打开新的终端发现PATH还是原来的PATH。要永久改变PATH 参见本文末的介绍~ 

PS1:直接修改文件内容的方法没有尝试过——针对方法1,因为在虚拟机下安装的,没有root权限修改/root下的文件; 
针对方法2,在Ubuntu中找到的/etc/profile写得很诡异,看不懂,没找到类似“PATH=***”的内容。 

PS2:自己琢磨成功——在终端中用命令的形式修改: 
~$ echo $PATH      # 显示当前环境变量 
/usr/local/sbin:/usr/local/bin    # 显示举例,多个路径中间用“:”隔开 
~$ PATH="$PATH:/home/workdir"    # 添加“/home/workdir”目录 
/usr/local/sbin:/usr/local/bin:/home/workdir   # 显示举例,多个路径中间仍用“:”隔开 

则添加成功!(最后当然可以重启或者输出使其即时生效:) 
~$ export PATH 
~$ cd ~ 
~$ source .bashrc     #该命令需在主目录下才能运行,否则提示找不到.bashrc命令 
结束! 

PS3:写命令的时候严格按此形式,在“=”前后千万不要有空格,否则提示“bash: PATH: command not found”错误。笔者曾吃过这样的狠亏,主要严格遵照上边的方法就可以解决问题了。 

【永久性改变PATH的方法】 
参考http://hi.baidu.com/youjim/blog/item/da912cd98475b3ef39012fad.html 

打开~/.bashrc文件最末添加命令: 
PATH=$PATH:/home/workdir 
注意与上文中各式不同,没有双引号,重启解决。 
为了您的安全,请只打开来源可靠的网址 

打开网站    取消 
来自: http://hi.baidu.com/%D6%AA%D3%EA%CF%FE%C7%E7/blog/item/11962a9690944815d31b7069.html 

roo@ubuntu:~$ cat /etc/profile 
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1)) 
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...). 

if [ -d /etc/profile.d ]; then 
  for i in /etc/profile.d/*.sh; do 
    if [ -r $i ]; then 
      . $i 
    fi 
  done 
  unset i 
fi 

if [ "$PS1" ]; then 
  if [ "$BASH" ]; then 
    PS1='\u@\h:\w\$ ' 
    if [ -f /etc/bash.bashrc ]; then 
. /etc/bash.bashrc 
    fi 
  else 
    if [ "`id -u`" -eq 0 ]; then 
      PS1='# ' 
    else 
      PS1='$ ' 
    fi 
  fi 
fi 

umask 022 
roo@ubuntu:~$ 

roo@ubuntu:~$ cat /etc/environment 
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:" 
roo@ubuntu:~$ 

roo@ubuntu:~$ cd ~ 
roo@ubuntu:~$ cat /.profile 
cat: /.profile: 没有那个文件或目录 
roo@ubuntu:~$ cat .profile 
# ~/.profile: executed by the command interpreter for login shells. 
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login 
# exists. 
# see /usr/share/doc/bash/examples/startup-files for examples. 
# the files are located in the bash-doc package. 

# the default umask is set in /etc/profile; for setting the umask 
# for ssh logins, install and configure the libpam-umask package. 
#umask 022 

# if running bash 
if [ -n "$BASH_VERSION" ]; then 
    # include .bashrc if it exists 
    if [ -f "$HOME/.bashrc" ]; then 
. "$HOME/.bashrc" 
    fi 
fi 

# set PATH so it includes user's private bin if it exists 
if [ -d "$HOME/bin" ] ; then 
    PATH="$HOME/bin:$PATH" 
fi 
roo@ubuntu:~$ 

roo@ubuntu:~$ cat .bashrc 
# ~/.bashrc: executed by bash(1) for non-login shells. 
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc) 
# for examples 

# If not running interactively, don't do anything 
[ -z "$PS1" ] && return 

# don't put duplicate lines in the history. See bash(1) for more options 
# ... or force ignoredups and ignorespace 
HISTCONTROL=ignoredups:ignorespace 

# append to the history file, don't overwrite it 
shopt -s histappend 

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1) 
HISTSIZE=1000 
HISTFILESIZE=2000 

# check the window size after each command and, if necessary, 
# update the values of LINES and COLUMNS. 
shopt -s checkwinsize 

# make less more friendly for non-text input files, see lesspipe(1) 
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)" 

# set variable identifying the chroot you work in (used in the prompt below) 
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then 
    debian_chroot=$(cat /etc/debian_chroot) 
fi 

# set a fancy prompt (non-color, unless we know we "want" color) 
case "$TERM" in 
    xterm-color) color_prompt=yes;; 
esac 

# uncomment for a colored prompt, if the terminal has the capability; turned 
# off by default to not distract the user: the focus in a terminal window 
# should be on the output of commands, not on the prompt 
#force_color_prompt=yes 

if [ -n "$force_color_prompt" ]; then 
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then 
# We have color support; assume it's compliant with Ecma-48 
# (ISO/IEC-6429). (Lack of such support is extremely rare, and such 
# a case would tend to support setf rather than setaf.) 
color_prompt=yes 
    else 
color_prompt= 
    fi 
fi 

if [ "$color_prompt" = yes ]; then 
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ' 
else 
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ ' 
fi 
unset color_prompt force_color_prompt 

# If this is an xterm set the title to user@host:dir 
case "$TERM" in 
xterm*|rxvt*) 
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1" 
    ;; 
*) 
    ;; 
esac 

# enable color support of ls and also add handy aliases 
if [ -x /usr/bin/dircolors ]; then 
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)" 
    alias ls='ls --color=auto' 
    #alias dir='dir --color=auto' 
    #alias vdir='vdir --color=auto' 

    alias grep='grep --color=auto' 
    alias fgrep='fgrep --color=auto' 
    alias egrep='egrep --color=auto' 
fi 

# some more ls aliases 
alias ll='ls -alF' 
alias la='ls -A' 
alias l='ls -CF' 

# Add an "alert" alias for long running commands.  Use like so: 
#   sleep 10; alert 
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"' 

# Alias definitions. 
# You may want to put all your additions into a separate file like 
# ~/.bash_aliases, instead of adding them here directly. 
# See /usr/share/doc/bash-doc/examples in the bash-doc package. 

if [ -f ~/.bash_aliases ]; then 
    . ~/.bash_aliases 
fi 

# enable programmable completion features (you don't need to enable 
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile 
# sources /etc/bash.bashrc). 
if [ -f /etc/bash_completion ] && ! shopt -oq posix; then 
    . /etc/bash_completion 
fi 
roo@ubuntu:~$ 

echo $PATH 
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games: 

时间: 2024-09-16 06:09:14

设置Ubuntu环境变量 PATH 的方法 及配置备份的相关文章

java环境变量path和classpath的配置_java

在Java的学习中,涉及到两个系统环境变量path和classpath 一. path环境变量 path环境变量是系统环境变量的一种,它用于保存一系列的路径,每个路径之间用分号分隔.当在命令行窗口运行一个可执行文件时,操作系统首先会在当前目录下查找是否存在该文件,如果不存在会继续在path环境变量中定义的路径下寻找这个文件,如果仍未找到,系统会报错. 在Windows7系统下配置环境变量: 1.右击[计算机],选择[属性],出现如下界面: 图1 2.点击[高级系统设置],出现如下界面: 图2 3

Linux(Ubuntu)下设置golang环境变量

1).源代码安装go语言 Go的工具链采用C语言编写,要构建它,你需要安装一个C编译器,本例采用gcc工具; 进入linux 终端,输入命令行 sudo apt-get install build-essential Go使用Mercurial进行版本管理,首先你必须安装了Mercurial,对于 Ubuntu/Debian 系统先安装easy_install; sudo apt-get install mercurial 出现Processing triggers for python-sup

Linux系统下修改环境变量PATH路径的三种方法

  电脑中必不可少的就是操作系统.而Linux的发展非常迅速,有赶超微软的趋势.这里介绍Linux的知识,让你学好应用Linux系统.比如要把/etc/apache/bin目录添加到PATH中,方法有三: 1.#PATH=$PATH:/etc/apache/bin 使用这种方法,只对当前会话有效,也就是说每当登出或注销系统以后,PATH 设置就会失效 2.#vi /etc/profile 在适当位置添加 PATH=$PATH:/etc/apache/bin (注意:= 即等号两边不能有任何空格)

win7系统怎么设置环境变量path

  1.通过单击"我的电脑→属性→高级"标签的"环境变量"按钮设置; 温馨提示:第一种设置环境变量的方式只对当前运行窗口有效,关闭运行窗口后,设置就不起作用了.     方法二: 1.在"开始→运行"框中输入"cmd"后按"确定"按钮;   2.在命令提示符下输入"path"即可查看环境变量设置;   更改环境变量有两种情况: 一是追加方式,即在不改变环境变量现有设置的情况下,增加变量的

Ubuntu设置shell环境变量

Ubuntu设置shell环境变量 打开配置文件: vim ~/.bashrc 在文件的末尾,添加如下 export 变量名=变量 如:export JAVA_HOME=/usr/lib/jvm/java-6-sunexport PATH=$PATH:~/mybin 注销并重新登录,新的环境变量就生效了. 在http://www.aliyun.com/zixun/aggregation/13835.html">Ubuntu中有如下几个文件可以设置环境变量 /etc/profile:在登录时

Linux下如何设置环境变量PATH

Linux系统环境变量配置文件: /etc/profile : 在登录时,操作系统定制用户环境时使用的第一个文件 ,此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行. /etc /environment : 在登录时操作系统使用的第二个文件, 系统在读取你自己的profile前,设置环境文件的环境变量. ~/.profile : 在登录时用到的第三个文件 是.profile文件,每个用户都可使用该文件输入专用于自己使用的shell信息,当用户登录时,该文件仅仅执行一次!默认情

Ubuntu系统下设置Java环境变量图文教程

我们安装Ubuntu 后,需要安装和配置 Java 环境,以支持其它基于 jre 的软件的运行.所以安装好 Ubuntu 后立马安装和配置好 Java 环境.下面就教大家如何一步一步达成此目的. 下载 jdk,当前的版本是 jdk-8u45.选择适合自己系统的版本进行下载,下载好之后放在 Download 目录下 然后使用 Ctrl + Alt + t 打开终端进入 Download 目录下,使用 tar -xf jdk-8u45-linux-x64.tar.gz 命令进行解压,接着再使用命令

Win7系统中的环境变量PATH怎么修改

  方法一.通过命令设置环境变量 1.打开"开始→运行",在运行框中"cmd",回车打开命令提示符窗口; 2.在打开的命令提示符窗口中,输入"path",回车就可查看环境变量设置; 3. 更改环境变量有两种情况: 一是追加方式,就是在不改变环境变量现有设置的情况下,增加变量的值,比如要给环境变量增加一个值为"D:Python"的设置,可以输入"path=%path%;D:Python";而另一种是完全修改方

Linux 环境变量(PATH)

1.什么是环境变量 环境变量是一个具有特定名字的对象,它包含了一个或者多个应用程序所将使用到的信息.例如path,当要求系统运行一个程序而没有告诉它程序所在的完整路径时,系统除了在当前目录下面寻找此程序外,还应到path中指定的路径去找.用户通过设置环境变量,来更好的运行进程. 简单地说这里的"Path"就是一个变量,里面存储了一些常用命令所存放的目录路径. 环境变量相当于给系统或用户应用程序设置的一些参数, 具体起什么作用这当然和具体的环境变量相关. 比如path, 是告诉系统, 当