具體的實(shí)現(xiàn)方式是,ssh連接后,會(huì)自動(dòng)執(zhí)行source /etc/profile。
那么我們其實(shí)只要在/etc/profile追加幾行代碼,從1號(hào)進(jìn)程獲取容器本身的環(huán)境變量,然后循環(huán)將環(huán)境變量export一下即可。
以下是一個(gè)簡(jiǎn)單的for循環(huán)實(shí)現(xiàn)。
for item in `cat /proc/1/environ |tr '\0' '\n'` do export $item done
當(dāng)然,有更簡(jiǎn)潔的命令,就是export $(cat /proc/1/environ |tr ‘\0’ ‘\n’ | xargs),可以實(shí)現(xiàn)同樣的效果。