#先准备一个脚本 [root@hadoop /]#vi /root/test.sh #里面加上内容 echo "" > ~/.bashrc cat <<EOF > ~/.bashrc # .bashrc # Source global definitions if [ -f /etc/bashrc ]; then . /etc/bashrc fi # Uncomment the following line if you don't like systemctl's auto-paging feature: # export SYSTEMD_PAGER= # User specific aliases and functions export JAVA_HOME=/usr/lib/jdk-11 export PATH=$JAVA_HOME/bin:$PATH EOF #root用户下执行命令 [root@hadoop /]#java -version openjdk version "1.8.0_282" OpenJDK Runtime Environment (build 1.8.0_282-b08) OpenJDK 64-Bit Server VM (build 25.282-b08, mixed mode) #查询到当前hadoop用户的java环境变量为1.8 [root@hadoop /]#su - hadoop -c "java -version" openjdk version "1.8.0_282" OpenJDK Runtime Environment (build 1.8.0_282-b08) OpenJDK 64-Bit Server VM (build 25.282-b08, mixed mode) #执行改变环境变量的脚本 [root@hadoop /]#su - hadoop -c "bash /root/test.sh" #再次查询到jdk变11了 [root@hadoop /]#su - hadoop -c "java -version" ls: cannot access /usr/lib/spark-current/yarn/spark-*-yarn-shuffle.jar: No such file or directory java version "11.0.10" 2021-01-19 LTS Java(TM) SE Runtime Environment 18.9 (build 11.0.10+8-LTS-162) Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.10+8-LTS-162, mixed mode)