問題描述
Nginx 安裝在/home/dev/share_dev/usr_local/nginx/sbin/nginx
,想在bash中直接用’nginx’來啟動服務。
在用戶目錄下touch .bash_profile ,添加PATH:
PATH=$PATH:/home/dev/share_dev/usr_local/nginx/sbin
export PATH
刷新:$ source .bash_profile
此時,$ which nginx
可以輸出正確的路徑。
直接$ nginx
,權限不夠,需要用root權限執行(說明已經可以找到nginx的正確位置了)。
$ sudo nginx
顯示command not found。
類似的問題還有 sudo ll
。
問題解決
不采用修改PATH的方式,而是為nginx做一個軟連接。
$ sudo ln -s /home/dev/share_dev/usr_local/nginx/sbin/nginx /usr/local/sbin/nginx
問題解釋
在命令前加 sudo 表示以root權限執行,但這樣做的前提是sudo 后面跟的命令為”系統命令"(/etc/environment)。在.bash_profile 里定義的是當前用戶的shell配置。
$ cat /etc/environment
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
參考
sudo使一般用戶不需要知道超級用戶的密碼即可獲得權限。首先超級用戶將普通用戶的名字、可以執行的特定命令、按照哪種用戶或用戶組的身份執行等信息,登記在特殊的文件中(通常是/etc/sudoers
),即完成對該用戶的授權(此時該用戶稱為“sudoer”)[3]
;在一般用戶需要獲取特殊權限時,其可在命令前加上“sudo
”,此時sudo將會詢問該用戶自己的密碼(以確認終端機前的是該用戶本人),回答后系統即會將該命令的進程以超級用戶的權限運行。之后的一段時間內(默認為5分鐘[4]
,可在/etc/sudoers
自定義),使用sudo不需要再次輸入密碼。
https://zh.wikipedia.org/wiki/Sudo
Since environment variables can influence program behavior, sudo provides a means to restrict which variables from the user's environment are inherited by the command to be run. There are two distinct ways sudoers can be configured to handle with environment variables.
By default, the env_reset option is enabled. This causes commands to be executed with a new, minimal environment. On AIX (and Linux systems without PAM), the environment is initialized with the contents of the/etc/environment file.
https://www.sudo.ws/man/1.7.10/sudo.man.html