Linux 操作系统下的环境变量设置

Linux下的环境变量设置

by:授客 QQ1033553122

1、  问题描述

linux输入命令时经常会出现提示:xxx:Command not found

 

2、  原因分析

Command not found,即找不到命令,可能原因:

1、你没有装相应的软件包

2、环境变量问题,比如未设置PATH路径。

 

3、  解决方法

1.  如果没安装软件则进行安装

2.  如果已安装,那就设置环境变量

 

4、  设置linux环境变量

变量简介

Linux是一个多用户的操作系统。每个用户登录系统后,都会有一个专用的运行环境。通常每个用户默认的环境都是相同的,这个默认环境实际上就是一组环境变量的定义。用户可以对自己的运行环境进行定制,其方法就是修改相应的环境变量。

 

设置环境变量

a)   临时生效

当前问题:输入tsung命令,提示找不到命令(前提:我已经安装了tsung)

[root@localhost ~]# tsung

-bash: tsung: command not found

 

#查看shell赋予程序的环境变量PATH

[root@localhost ~]# echo $PATH

/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

 

说明

PATH:用于保存用冒号分隔的目录路径名,shell将按PATH变量中给出的目录,按目录顺序搜索这些目录,shell将执行搜索到的,第一个与命令名称一致的可执行文件(注意:不会嵌套搜索,也就是仅在给定的目录下搜索,不会在子目录下搜索)

 

#查找tsung bin程序

[root@localhost ~]# find / -name tsung

/usr/local/tsung

/usr/local/tsung/bin/tsung

...

 

对比PATHtsung所在位置可知,PATH给出的路径不包含tsung,所以找不到tsung,进而报错

 

#tsung bin程序所在的目录路径添加到PATH环境变量

[root@localhost ~]# export PATH=$PATH:/usr/local/tsung/bin

[root@localhost ~]# echo $PATH

/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/tsung/bin

 

说明:

PATH定义格式为:

PATH=$PATH::::------:

你可以自己加上指定的路径,中间用冒号隔开。

 

#再次测试程序,OK

[root@localhost ~]# tsung

Usage: tsung start|stop|debug|status

Options:

    -f      set configuration file (default is ~/.tsung/tsung.xml)

                   (use - for standard input)

    -l    set log directory where YYYYMMDD-HHMM dirs are created (default is ~/.tsung/log/)

    -i        set controller id (default is empty)

    -r   set remote connector (default is ssh)

    -s            enable erlang smp on client nodes

    -p       set maximum erlang processes per vm (default is 250000)

    -m      write monitoring output on this file (default is tsung.log)

                   (use - for standard output)

    -F            use long names (FQDN) for erlang nodes

    -w            warmup delay (default is 1 sec)

    -v            print version information and exit

    -6            use IPv6 for Tsung internal communications

    -x      list of requests tag to be excluded from the run (separated by comma)

    -h            display this help and exit

 

注意:

1.这种设置仅是临时的,关闭当前shell后,设置失效,下次使用必须再次设置

2.这里以PATH环境变量为例,其它变量的设置,以此类推

 

b)   永久生效

通过修改配置文件来修改环境变量

需要注意的是,一般情况下,这仅仅对于普通用户适用,避免修改根用户的环境定义文件,因为那样可能会造成潜在的危险。

一、对所有用户生效

正确写法一:

[root@localhost ~]# vi /etc/profile

# /etc/profile

...

unset i

unset pathmunge

#######文件最末尾处添加自己定义的环境变量#####

TSUNG=/usr/local/tsung

export PATH=$PATH:$TSUNG/bin

##########################################

 

正确写法二:

[root@localhost ~]# vi /etc/profile

# /etc/profile

...

unset i

unset pathmunge

#######文件最末尾处添加自己定义的环境变量#####

export PATH=$PATH:/usr/local/tsung/bin

##########################################

 

[root@localhost ~]# source /etc/profile #等同# . /etc/profile 注意.后空格

说明:通过配置文件更改的环境变量,仅在用户重新登陆后生效,如果想立刻生效,必须执行source命令,

 

验证:

[root@localhost ~]# tsung

Usage: tsung start|stop|debug|status

Options:

    -f      set configuration file (default is ~/.tsung/tsung.xml)

...

 

二、对单一用户生效

同上,仅是要编辑的文件不同,从以下给定文件中选取一个,进行编辑,保存

~/.bash_profile(优先考虑)~/.bash_login(次之)~/.profile(最次)

[laiyu@localhost ~]$ pwd #确保进入某用户的用户主目录

/home/laiyu

[laiyu@localhost ~]$ vi .bash_profile   #文件内容默认如下

Linux <wbr>操作系统下的环境变量设置



修改内容如下

# .bash_profile

 

# Get the aliases and functions

if [ -f ~/.bashrc ]; then

        . ~/.bashrc

fi

 

# User specific environment and startup programs

 

TSUNG=/usr/local/tsung

#PATH=$PATH:$HOME/bin

PATH=$PATH:$HOME/bin:$TSUNG/bin

 

export PATH

 

其它文件的修改以此类推...

[laiyu@localhost ~]$ source .bash_profile #与该语句等效的是下面的运行脚本语句:. ./.bash_profile

[laiyu@localhost ~]$ . .bash_profile

[root@localhost ~]# tsung

Usage: tsung start|stop|debug|status

Options:

    -f      set configuration file (default is ~/.tsung/tsung.xml)

...

 

注:这里以PATH环境变量为例,其它变量的设置,以此类推

 

原文地址:https://www.cnblogs.com/shouke/p/10158122.html