在虚拟机里安装了一个Ubuntu,在局域网下,需要设置代理才能访问网络和安装程序。很自然的给sudo vim /etc/bash.bashrc
添加代理。
1
2
3
4
|
# 设置代理
export
http_proxy=http:
//web-proxy
.oo.com:8080
export
https_proxy=http:
//web-proxy
.oo.com:8080
export
no_proxy=localhost,.oo.com,.
local
|
然后很愉快的sudo apt-get update
发现根本无法连接服务器。如果切换到sudo su
的root用户下,是可以的执行的。
研究了半天,原来是sudo在切换成root用户的时候,env
并不会去保留这些环境变量,需要特别的指明才可以。
通过visudo
命令来设置,需要保留的环境变量,新增下面的第10行。Defaults env_keep="http_proxy https_proxy ftp_proxy no_proxy DISPLAY XAUTHORITY"
。这样就可以继续愉快的访问网络了。
01
02
03
04
05
06
07
08
09
10
11
12
|
#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults env_reset
Defaults env_keep=
"http_proxy https_proxy ftp_proxy no_proxy DISPLAY XAUTHORITY"
Defaults mail_badpass
Defaults secure_path=
"/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
|
友情提示。只能通过
visudo
命令来更新,不能修改/etc/sudoers
文件,会被覆盖的。
原创文章,转载请注明: 转载自东东东 陈煜东的博客
本文链接地址: sudo保持前用用户的env环境变量 – https://www.chenyudong.com/archives/sudo-keep-env.html