1. 程式人生 > >sudo保持前用使用者的env環境變數

sudo保持前用使用者的env環境變數

在虛擬機器裡安裝了一個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檔案,會被覆蓋的。