root权限无法执行source/etc/profile让java环境生效
时间: 2023-08-05 09:02:05 浏览: 59
如果使用 `sudo` 命令执行 `source /etc/profile` 命令,可能会遇到权限不够的问题。这是因为 `source` 命令需要在当前 shell 中执行,而 `sudo` 命令会启动一个新的 shell 环境,导致 `source` 命令无法在当前 shell 中执行。
解决方法是在 `sudo` 命令前加上 `-i` 选项,这样 `sudo` 命令会以目标用户的身份启动一个新的 login shell,从而重新加载 `/etc/profile` 等环境配置文件。具体命令如下:
```
sudo -i source /etc/profile
```
这样就可以让 Java 环境生效了。
相关问题
在/etc/profile里修改环境变量
您的问题是关于如何在Linux系统中修改环境变量,对吗?
是的,您可以在Linux系统中通过编辑`/etc/profile`文件来修改全局环境变量。
1. 打开终端并以root用户身份登录
2. 输入以下命令打开`/etc/profile`文件:
```
sudo nano /etc/profile
```
3. 在文件末尾添加您想要设置的环境变量,例如:
```
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64/
export PATH=$JAVA_HOME/bin:$PATH
```
4. 按下`Ctrl + X`,然后输入`Y`以保存更改
5. 输入以下命令使更改生效:
```
source /etc/profile
```
现在,您已成功修改了全局环境变量。
source /etc/hosts 报错bash
引用\[1\]:问题描述: CentOS 执行 source /etc/profile命令时,报错:-bash: #: 未找到命令 \[root@localhost ~\]# source /etc/profile bash: unse: 未找到命令... 问题原因:/etc/profile文件内容错误,如下 修改/etc/profile文件内容 将unse tMAILCHECK修改为unset MAILCHECK后保存,运行命令可正常执行; 此问题多数情况是/etc/profile文件内容错误导致,耐心检查找到错误位置即可解决;。 引用\[2\]: 我们经常在下载安装一些软件工具后需要配置环境变量。例如jdk、erlang等。我们一般会在/etc/profile中配置一些环境变量参数,然后通过source /etc/profile触发配置生效。 但是我们在source /etc/profile时出现了报错的情况, \[root@aliyun home\]# source /etc/profile bash: id: command is not found 。 引用\[3\]: 这是因为在一些情况下,有很多命令其实是还不能使用的,或者因为某些其他操作把之前命令的关联性给取消了。 例如这里我们的bash: id: command is not found,这里我们可以通过/etc/profile中的配置看到id这个命令在/usr/bin/目录下,并且我们检查了这个命令是存在的。 这个时候我们可以直接去运行export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin加载这些命令后再通过source /etc/profile就可以正常加载新的配置了。一些其他的命令提示未找到也可以通过这种方式进行解决。。
问题:source /etc/hosts 报错bash: id: command is not found是什么原因?如何解决?
回答: 当执行source /etc/hosts命令时,出现bash: id: command is not found的报错,这通常是因为某些命令无法使用或之前的命令关联性被取消了。\[3\]解决这个问题的方法是通过运行export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin来加载这些命令,然后再执行source /etc/hosts命令即可正常加载新的配置。这种方式也适用于其他命令提示未找到的情况。
#### 引用[.reference_title]
- *1* [执行 source /etc/profile命令时,报错:-bash: #: 未找到命令](https://blog.csdn.net/xinguopeng/article/details/113183221)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* *3* [Linux - source /etc/profile报错未找到命令(command is not found)](https://blog.csdn.net/u013985664/article/details/107999898)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]