Linux环境变量设置
修改环境变量PATH
最近为root添加一个环境变量发现sudo su进去没有变化所以总结了一下所有设置环境变量的方法:
查看PATH:echo $PATH
- 直接在命令行修改,就可以使用,但是只有在当前的窗口和用户有用,
关闭以后就失效了,所以如果是临时使用可以这样设置
export PATH=/usr/test/bin:$PATH
- 修改家目录下.bashrc文件,只对当前用户有用,是永久的,除非删除或者修改该文件。
如果只是自己使用添加的命令的推荐使用。
修改文件以后有两种方法使其起作用:- 关闭当前终端窗口,重新打开一个新终端窗口就能生效
- 输入 source ~/.bashrc 命令,立即生效
vim ~/.bashrc
export PATH=/usr/test/bin:$PATH
修改profile文件,修改类似 .bashrc 但是作用范围不一样,该文件针对所有用户的环境变量。
如果要是所有的用户都是用该变量修改这个文件。
生效方法:系统重启或者输入 source /etc/profile 命令,立即生效
vim /etc/profile修改environment文件
vim /etc/environment
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"中加入 ":/usr/local/mongodb/bin"
* 生效方法:系统重启
* 有效期限:永久有效
* 用户局限:对所有用户
- 修改/etc/login.defs文件: 这个文件是设置用户帐号限制的文件,在这里我们可配置密码的最大过期天数,
密码的最大长度约束等内容,如果/etc/shadow文件里有相同的选项,则以/etc/shadow里的设置为准,也就是
说/etc/shadow的配置优先级高于/etc/login.defs,该文件里的配置对root用户无效,但是环境变量PATH有效
/*
*REQUIRED* The default PATH settings, for superuser and normal users.
*
*(they are minimal, add the rest in the shell startup files)
*/
ENV_SUPATH PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/ovs/bin:/ovs/sbin:/pica/bin
ENV_PATH PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/ovs/bin:/pica/bin
总结:
当我们通过ssh登录到系统的时候读取环境变量是有顺序的,也就是上面的几种设置方法可能会被覆盖。
/* linux登录时设置PATH的顺序从上到下 */
login.defs
/etc/environment
/etc/profile
~/.profile
~/.bashrc
当我们设置了PATH后,发现 sudo su 切换到root用户的PATH没有改变,只有login.defs和~/.bashrc设置生效
或者使用 sudo su - 就可以获得更新的环境变量
$ man su
SU(1)
NAME
su - change user ID or become superuser
DESCRIPTION
The su command is used to become another user during a login session. Invoked without a username, su defaults to becoming the superuser. The optional argument - may be used to
provide an environment similar to what the user would expect had the user logged in directly.
Additional arguments may be provided after the username, in which case they are supplied to the user's login shell. In particular, an argument of -c will cause the next argument
to be treated as a command by most command interpreters. The command will be executed by the shell specified in /etc/passwd for the target user.
You can use the -- argument to separate su options from the arguments supplied to the shell.
The user will be prompted for a password, if appropriate. Invalid passwords will produce an error message. All attempts, both valid and invalid, are logged to detect abuse of
the system.
The current environment is passed to the new shell. The value of $PATH is reset to /bin:/usr/bin for normal users, or /sbin:/bin:/usr/sbin:/usr/bin for the superuser. This may
be changed with the 'ENV_PATH' and 'ENV_SUPATH' definitions in /etc/login.defs.
A subsystem login is indicated by the presence of a "*" as the first character of the login shell. The given home directory will be used as the root of a new file system which
the user is actually logged into.
OPTIONS
-, -l, --login
Provide an environment similar to what the user would expect had the user logged in directly.
When - is used, it must be specified before any username. For portability it is recommended to use it as last option, before any username. The other forms (-l and --login)
do not have this restriction.
sudo su 后加在 /etc/profile 的变量不能继承
通常,为了防御别人攻击,我们一般会把root给禁止远程登录,只能通过普通用户登录然后切换root的方法,
双重验证来增加安全性,普通的做法有以下几种:
- 登录后用su切换
这种做法是最安全,当然也是最繁琐的,因为你需要记住两个密码,一个是普通用户的密码,
一个是root用户的密码,缺一个都进不去root权限
- 让普通用户拥有sudoer权限,登录后用sudo su切换
通过visudo增加一行
test ALL=(ALL:ALL) ALL
那么用户test登录后可以直接输入sudo su,再输入一次自己的密码即可转换为root用户,
这种方式是比较不安全的,因为只需要知道test用户的密码,其实就相当于拥有了root权限,
不过却是比较方便的进入方式,可以限制以下test变成root后的权限,不要像上面一样全部
给ALL即可保证比较高的安全性。当然这种方式也有一个弊端就是变成root后加在
/etc/profile的变量不能继承,所以通常我们会用下面的做法
sudo su - 让普通用户变成root后还能继承之前的环境变量
把环境变量写到 ~/.bashrc
实测发现,sudo su之后,/etc/profile和~/.profile的变量都不会被导入,但是 ~/.bashrc 会
Linux环境变量设置的更多相关文章
- 转:Linux环境变量设置方法总结 PATH、LD_LIBRARY_PATH
转: https://www.linuxidc.com/Linux/2017-03/142338.htm 文章写比较全 转载记录 Linux环境变量设置方法总结 PATH.LD_LIBRARY_P ...
- linux 环境变量设置方法总结(PATH/LD_LIBRARY_PATH)
linux 环境变量设置方法总结(PATH/LD_LIBRARY_PATH) http://blog.csdn.net/wangeen/article/details/8159500 设置 Linux ...
- [转帖]Linux环境变量设置方法总结 PATH、LD_LIBRARY_PATH
Linux环境变量设置方法总结 PATH.LD_LIBRARY_PATH 2018年01月17日 21:10:26 晨至曦 阅读数 7548 https://blog.csdn.net/qq_1900 ...
- linux环境变量设置错误后的恢复方法(转)
原文: http://blog.csdn.net/hoholook/article/details/2793447 linux环境变量设置错误后的恢复方法 中国自学编程网收集整理 发布日期:2008 ...
- 【转】linux环境变量设置
1. 显示环境变量HOME $ echo $HOME /home/terry 2. 设置一个新的环境变量WELCOME $ export WELCOME="Hello!" $ ec ...
- Linux环境变量设置中配置文件分析(/etc/profile,~/.bashrc等)(转)
说明:在研究中发现,对于不同版本的Linux系统有着不同的文件,但是总的入口是不变的/etc/profile,下面只是展示加载顺序的研究过程,所以会有些系统没有这个文件等问题. 一.配置文件与作用域: ...
- Linux 环境变量设置的几种方法
From:http://home.eeworld.com.cn/home.php?mod=space&uid=291513&do=blog&id=40557 环境变量是和She ...
- linux环境变量设置和默认执行语句设置
环境变量设置 1.export export ORACLE_HOME=/usr/local/instantclient_12_2export PATH=$ORACLE_HOME:$PATHexport ...
- Linux 环境变量 设置 etc profile
一.Linux的变量种类 按变量的生存周期来划分,Linux变量可分为两类: 1.永久的:需要修改配置文件,变量永久生效. 2.临时的:使用export命令声明即可,变量在关闭shell时失效. 二. ...
随机推荐
- [.NET] 怎样使用 async & await 一步步将同步代码转换为异步编程
怎样使用 async & await 一步步将同步代码转换为异步编程 [博主]反骨仔 [出处]http://www.cnblogs.com/liqingwen/p/6079707.html ...
- mac下安装及配置tomcat
mac下的软件不像windows下的程序那样写注册表,对于tomcat的安装来说,在mac下是名符其实的绿色软件,具体操作如下: 1.到 apache官方主页 下载完整 tar.gz文件包.(没有专门 ...
- MJRefresh 源码解读 + 使用
MJRefresh这个刷新控件是一款非常好用的框架,我们在使用一个框架的同时,最好能了解下它的实现原理,不管是根据业务要求在原有的基础上修改代码,还是其他的目的,弄明白作者的思路和代码风格,会受益匪浅 ...
- 【Reading Note】算法读书杂记
1 排序 排序基本信息 稳定性:排序前大的数在排序后,大的数依然保持不变就是稳定排序,反之不稳定 内外排序:根据待排序的记录是否放在内存里面区分的.诸如:插入排序(直接插入&希尔).交换排序( ...
- Android程序中--不能改变的事情
有时,开发人员会对应用程序进行更改,当安装为以前版本的更新时出现令人惊讶的结果 - 快捷方式断开,小部件消失或甚至根本无法安装. 应用程序的某些部分在发布后是不可变的,您可以通过理解它们来避免意外. ...
- Android中AlarmManager使用示例(持续更新,已经更改)
现在普遍的手机都会有一个闹钟的功能,如果使用Android来实现一个闹钟可以使用AtarmManager来实现.AtarmManager提供了一种系统级的提示服务,允许你安排在将来的某个时间执行一个服 ...
- 信息安全-1:python之playfair密码算法详解[原创]
转发注明出处: http://www.cnblogs.com/0zcl/p/6105825.html 一.基本概念 古典密码是基于字符替换的密码.加密技术有:Caesar(恺撒)密码.Vigenere ...
- Atitit.研发团队与公司绩效管理的原理概论的attilax总结
Atitit.研发团队与公司绩效管理的原理概论的attilax总结 1. 四个理念 1 1.1. 绩效管理的三个目的.四个环节.五个关键2 1.2. 绩效目标smart2 2. 考核对象2 3. 绩效 ...
- HTML5 标签 details 展开 搜索
details有一个新增加的子标签--summary,当鼠标点击summary标签中的内容文字时,details标签中的其他所有元素将会展开或收缩. 默认状态为 收缩状态 设置为展开状态为 <d ...
- Ubuntu手动设置DSL连接
在安装完Ubuntu之后,发现图形界面的DSL连接不管用了,郁闷了好几天,想想移动每个月120个小时的流量岂不是白白浪费了.正当我想重返Windows系统的时候,却发现了手动设置连接DSL的好方法,感 ...