怎样在Ubuntu中设置环境变量
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎样在Ubuntu中设置环境变量相关的知识,希望对你有一定的参考价值。
首先启动终端。单击屏幕左上角的Ubuntu图标,在弹出的窗口中点击搜索栏,输入“terminal”,
稍等片刻,终端就会赫然在目!二话不说,直接点击!
然后打开环境设置文件。
Ubuntu的环境设置文件为/etc/profile。它本质上是一个Shell脚本,其中存储了每次启动系统或终端时都要运行的命令。
在终端中输入"sudo gedit /etc/profile",回车,然后输入管理员密码后回车,即可打开。
加入环境变量设置语句。
Linux系统中,设置环境变量的语句是:
export <环境变量名>=<变量的值>
我们只需在这里新开一行,按照这个格式填写即可。
例如:我新建一个名为"mynewenvironmentvar"的环境变量,它的值为“加入一个新的环境变量!我来了!”,那么就要这样写:
export mynewenvironmentvar=加入一个新的环境变量!我来了!
让我们的修改生效。
回到终端,输入"source /etc/profile",回车,即可让我们的环境变量生效。
最后,检验一下效果!
我们用echo回显命令,看看环境变量有没有生效。
只需输入
echo $<环境变量名>
回车即可。
对于我设置的变量,要输入:
echo $mynewenvironmentvar
看到效果了吧!
不过目前,环境变量还只是部分生效,这就意味着重启终端后,原先的设置又没了。
不必担心,重启系统,让环境变量的设置完全生效。 参考技术A 一:用于当前终端:
在当前终端中输入:export PATH=$PATH:<你的要加入的路径>
不过上面的方法只适用于当前终端,一旦当前终端关闭或在另一个终端中,则无效。
export NDK_ROOT=/home/jiang/soft/android-ndk-r8e 只能在当前终端使用。
二:用于当前用户:
在用户主目录下有一个 .bashrc 隐藏文件,可以在此文件中加入 PATH 的设置如下:
vi ~/.bashrc
加入:
export PATH=<你的要加入的路径>:$PATH
如果要加入多个路径,只要:
export PATH=<你要加入的路径1>:<你要加入的路径2>: ...... :$PATH
当中每个路径要以冒号分隔。
这样每次登录都会生效
三:用于所有用户:
sudo vi /etc/profile
加入:
export PATH=<你要加入的路径>:$PATH
就可以了。
终端输入:echo $PATH 可以查看环境变量
注意,修改环境变量后,除了第一种方法立即生效外,第二第三种方法要立即生效,可以source ~/.bashrc或者注销再次登录后就可以了! 参考技术B 首先启动终端。
在 opsworks 中的自定义 json 中设置环境变量
【中文标题】在 opsworks 中的自定义 json 中设置环境变量【英文标题】:Set an environment variables in the custom json in opsworks 【发布时间】:2017-05-04 09:20:51 【问题描述】:我正在开发一个创建 opsworks 堆栈的 AWS cloudformation 模板。 在我的堆栈中,我将有一个实例 (Ubuntu 14.04),将在实例中安装的所有应用程序和软件包都在 S3 存储桶提供的厨师食谱中进行配置。我需要在我的实例中设置一个环境变量,它的值是由 cloudformation 模板的一个参数设置的,这是模板:
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "AWS CloudFormation Template : Stack OpsWorks Slave pour deployer les instances script",
"Mappings":
"Region2Principal":
"us-east-1":
"EC2Principal": "ec2.amazonaws.com",
"OpsWorksPrincipal": "opsworks.amazonaws.com"
,
"us-west-2":
"EC2Principal": "ec2.amazonaws.com",
"OpsWorksPrincipal": "opsworks.amazonaws.com"
,
"us-west-1":
"EC2Principal": "ec2.amazonaws.com",
"OpsWorksPrincipal": "opsworks.amazonaws.com"
,
"eu-west-1":
"EC2Principal": "ec2.amazonaws.com",
"OpsWorksPrincipal": "opsworks.amazonaws.com"
,
"AWSInstanceType2Arch" :
"t1.micro" : "Arch" : "PV64" ,
"t2.micro" : "Arch" : "HVM64" ,
"m1.small" : "Arch" : "PV64" ,
"m1.large" : "Arch" : "PV64" ,
"m4.large" : "Arch" : "HVM64" ,
"m4.xlarge" : "Arch" : "HVM64" ,
"m4.2xlarge" : "Arch" : "HVM64" ,
"m4.4xlarge" : "Arch" : "HVM64" ,
"m4.10xlarge" : "Arch" : "HVM64" ,
"m4.16xlarge" : "Arch" : "HVM64" ,
"c3.large" : "Arch" : "HVM64" ,
"c3.xlarge" : "Arch" : "HVM64" ,
"c3.2xlarge" : "Arch" : "HVM64" ,
"c3.4xlarge" : "Arch" : "HVM64" ,
"c3.8xlarge" : "Arch" : "HVM64" ,
"c4.large" : "Arch" : "HVM64" ,
"c4.xlarge" : "Arch" : "HVM64" ,
"c4.2xlarge" : "Arch" : "HVM64" ,
"c4.4xlarge" : "Arch" : "HVM64" ,
"c4.8xlarge" : "Arch" : "HVM64"
,
"AWSRegionArch2AMI" :
"us-east-1" : "PV64" : "ami-5fb8c835", "HVM64" : "ami-60b6c60a" ,
"us-west-1" : "PV64" : "ami-56ea8636", "HVM64" : "ami-d5ea86b5" ,
"eu-west-1" : "PV64" : "ami-95e33ce6", "HVM64" : "ami-bff32ccc" ,
"us-west-2" : "PV64" : "ami-d93622b8", "HVM64" : "ami-f0091d91"
,
"Parameters":
"OpsWorksStackColor":
"Description": "RGB Color to use for OpsWorks Stack",
"Type": "String",
"Default": "rgb(38, 146, 168)"
,
"Region" :
"Type":"String",
"Description": "Region location of the template resources",
"Default": "eu-west-1",
"AllowedValues" : [ "us-east-1", "us-west-1", "us-west-2", "eu-west-1" ]
,
"SecurityGroupIds":
"Description": "Security groups that can be used to access the EC2 instances, do not select more than 5 SG",
"Type": "List<AWS::EC2::SecurityGroup::Id>",
"ConstraintDescription": "must be list of EC2 security group ids"
,
"VpcId":
"Type": "AWS::EC2::VPC::Id",
"Description": "VPC associated with the provided subnets",
"Default": "vpc-69e3320c",
"ConstraintDescription": "must be an existing VPC ID"
,
"SubnetId":
"Type": "String",
"Default": "subnet-6820eb31",
"ConstraintDescription": "must be an existing subnet ID"
,
"InstanceType":
"Type": "String",
"Default": "c3.large",
"AllowedValues" : ["t2.micro", "m1.small", "m1.large","m4.large","m4.xlarge","m4.2xlarge","m4.4xlarge","m4.10xlarge","m4.16xlarge","c4.large" , "c4.xlarge" ,"c4.2xlarge" , "c4.4xlarge","c4.8xlarge" , "c3.large" , "c3.xlarge", "c3.2xlarge", "c3.4xlarge" ,"c3.8xlarge"],
"ConstraintDescription": "must be a valid EC2 instance type"
,
"KeyPairName":
"Type": "AWS::EC2::KeyPair::KeyName",
"Default": "test-generic-ec2",
"ConstraintDescription": "must be the name of an existing EC2 KeyPair"
,
"CookbookS3" :
"Type": "String",
"Default": "https://s3-eu-west-1.amazonaws.com/MybucketJRBLO.cookbooks/cookbook-v2.tar.gz",
"ConstraintDescription": "the Url to the cookbook"
,
"CookbookS3AccessID":
"Type": "String",
"ConstraintDescription": "username to the appropriate IAM access key ID"
,
"CookbookS3AccessKey":
"Type": "String",
"NoEcho" : "true",
"ConstraintDescription": "password to the appropriate IAM secret access key"
,
"Environnement" :
"Description": "The Environnement variable ",
"Type": "String",
"Default": "test",
"AllowedValues" : ["dev", "test", "int", "prod"]
,
"Conditions" :
"CreateProdResources" : "Fn::Not" : [ "Fn::Equals" : [ "Ref" : "Environnement","test" ] ]
,
"Resources":
"MabStack":
"Type": "AWS::OpsWorks::Stack",
"Properties":
"AgentVersion" : "LATEST",
"Name": "Ref": "AWS::StackName" ,
"Attributes": "Color": "Ref": "OpsWorksStackColor" ,
"ChefConfiguration": ,
"ConfigurationManager": "Name": "Chef", "Version": "12" ,
"CustomCookbooksSource":
"Type": "s3",
"Password" : "Ref": "CookbookS3AccessKey" ,
"Username" : "Ref": "CookbookS3AccessID" ,
"Url": "Ref": "CookbookS3"
,
"CustomJson": "awscli": "profils":
"default": "role_arn": "Fn::GetAtt": [ "OpsWorksInstanceProfile", "Arn" ]
,
"chef_environment": "Ref" : "Environnement"
,
"DefaultInstanceProfileArn": "Fn::GetAtt": [ "OpsWorksInstanceProfile","Arn" ] ,
"DefaultOs": "Ubuntu 14.04 LTS",
"DefaultRootDeviceType": "ebs",
"DefaultSshKeyName": "Ref": "KeyPairName" ,
"DefaultSubnetId" : "Ref" : "SubnetId" ,
"ServiceRoleArn": "Fn::GetAtt": ["OpsWorksServiceRole", "Arn"] ,
"UseCustomCookbooks": true,
"UseOpsworksSecurityGroups" : true,
"VpcId" : "Ref" : "VpcId"
,
"MabLayer":
"Type": "AWS::OpsWorks::Layer",
"DependsOn" : "OpsWorksServiceRole",
"Properties":
"AutoAssignElasticIps" : false,
"AutoAssignPublicIps" : true,
"CustomRecipes" :
"Configure" : ["cassandra-php-driver::configure"],
"Setup" : ["cassandra-php-driver::setup", "security::setup"]
,
"CustomSecurityGroupIds" : "Ref" : "SecurityGroupIds" ,
"EnableAutoHealing" : true,
"InstallUpdatesOnBoot": false,
"LifecycleEventConfiguration":
"ShutdownEventConfiguration":
"DelayUntilElbConnectionsDrained": false,
"ExecutionTimeout": 120
,
"Name": "script-node",
"Shortname" : "node",
"StackId": "Ref": "MabStack" ,
"Type": "custom",
"UseEbsOptimizedInstances": true,
"VolumeConfigurations": [
"Iops": 10000,
"MountPoint": "/dev/sda1",
"NumberOfDisks": 1,
"Size": 20,
"VolumeType": "gp2"
]
,
"OpsWorksServiceRole":
"Type": "AWS::IAM::Role",
"Properties":
"AssumeRolePolicyDocument":
"Version" : "2012-10-17",
"Statement": [
"Effect": "Allow",
"Principal": "Service": [ "Fn::FindInMap": [ "Region2Principal", "Ref": "AWS::Region" ,"OpsWorksPrincipal" ] ] ,
"Action" : [ "sts:AssumeRole" ]
]
,
"Path": "/",
"Policies": [
"PolicyName": "opsworks-service",
"PolicyDocument":
"Version" : "2012-10-17",
"Statement": [
"Effect": "Allow",
"Action": "*",
"Resource": "*"
]
]
,
"OpsWorksInstanceRole":
"Type": "AWS::IAM::Role",
"Properties":
"AssumeRolePolicyDocument":
"Version" : "2012-10-17",
"Statement": [
"Effect": "Allow",
"Principal": "Service" : [ "Fn::FindInMap": [ "Region2Principal", "Ref": "AWS::Region" ,"EC2Principal" ] ] ,
"Action" : [ "sts:AssumeRole" ]
]
,
"Path": "/",
"Policies": [
"PolicyName": "aws-opsworks-instance",
"PolicyDocument":
"Statement": [
"Effect": "Allow",
"Action": "*",
"Resource": "*"
]
]
,
"OpsWorksInstanceProfile":
"Type": "AWS::IAM::InstanceProfile",
"Properties":
"Path": "/",
"Roles": [ "Ref": "OpsWorksInstanceRole" ]
,
"MabInstance":
"Type": "AWS::OpsWorks::Instance",
"Properties":
"Hostname": "Script",
"RootDeviceType": "ebs",
"StackId": "Ref": "MabStack",
"LayerIds": ["Ref": "MabLayer"],
"InstanceType": "Ref" : "InstanceType"
,
"MabApp":
"Type": "AWS::OpsWorks::App",
"Properties":
"AppSource" :
"Type" : "git",
"Url" : "git://github.com:soumab/JRLapplication.git",
"Revision" : "develop"
,
"Description": "Dataimport and connectors",
"Name" : "JRL-App",
"Shortname" : "JRL_app",
"StackId" : "Ref": "MabStack",
"Type" : "other"
,
我希望我的实例有这个环境变量:
export $Apps_ENV = "test" / "int" / "prod"
因此该值取决于用户将选择的内容。如何设置这个变量?可以通过CustomJson
吗?还是直接在菜谱里?请帮忙。谢谢。
【问题讨论】:
CustomJson
是使用 Chef 的资源 AWS::Opsworks::Stack
的属性。
【参考方案1】:
我通过以下设置成功实现了我想要的:
我在我的模板中将此添加到 CustomJson :
"settings" : "var" :
"default": "Apps_ENV": "Ref" : "Environnement"
我添加了一本如下的食谱
settings/recipes/defaults.rb :
template '/etc/environment' do
source 'env.erb'
owner 'root'
group 'root'
mode '0755'
end
和settings/templates/default/env.erb
:
<% node['settings']['var'].each do |var_name, var_att| %>
<%= sprintf("[%s]", var_name) %>
<% var_att.each do |attr_name, attr_value| %>
<%= sprintf("%s=%s", attr_name, attr_value) %>
<% end
end %>
【讨论】:
以上是关于怎样在Ubuntu中设置环境变量的主要内容,如果未能解决你的问题,请参考以下文章