{"id":192331,"date":"2020-06-08T10:45:20","date_gmt":"2020-06-08T02:45:20","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=192331"},"modified":"2020-05-25T09:54:04","modified_gmt":"2020-05-25T01:54:04","slug":"bash-initialization-mechanism","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/bash-initialization-mechanism.html","title":{"rendered":"bash\u521d\u59cb\u5316\u673a\u5236"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86\u8be6\u89e3bash\u4e2d\u7684\u521d\u59cb\u5316\u673a\u5236\uff0c\u6587\u4e2d\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u4ecb\u7ecd\u7684\u975e\u5e38\u8be6\u7ec6\uff0c\u5bf9\u5927\u5bb6\u7684\u5b66\u4e60\u6216\u8005\u5de5\u4f5c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u5b66\u4e60\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u4eec\u4e0b\u9762\u968f\u7740\u5c0f\u7f16\u6765\u4e00\u8d77\u5b66\u4e60\u5b66\u4e60\u5427\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
Bash\u521d\u59cb\u5316\u6587\u4ef6<\/strong><\/div>\n
\u4ea4\u4e92\u5f0flogin shell<\/strong><\/span><\/div>\n

\u5728\u4e0b\u5217\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u83b7\u5f97\u4e00\u4e2alogin shell\uff1a<\/p>\n

\u767b\u5f55\u7cfb\u7edf\u65f6\u83b7\u5f97\u7684\u9876\u5c42shell\uff0c\u65e0\u8bba\u662f\u901a\u8fc7\u672c\u5730\u7ec8\u7aef\u767b\u5f55\uff0c\u8fd8\u662f\u901a\u8fc7\u7f51\u7edcssh\u767b\u5f55\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\u83b7\u5f97\u7684login shell\u662f\u4e00\u4e2a\u4ea4\u4e92\u5f0fshell\u3002
\n\u5728\u7ec8\u7aef\u4e0b\u4f7f\u7528--login\u9009\u9879\u8c03\u7528bash\uff0c\u53ef\u4ee5\u83b7\u5f97\u4e00\u4e2a\u4ea4\u4e92\u5f0flogin shell\u3002
\n\u5728\u811a\u672c\u4e2d\u4f7f\u7528--login\u9009\u9879\u8c03\u7528bash\uff08\u4f8b\u5982\uff1a#!\/bin\/bash --login\uff09\u53ef\u4ee5\u5f97\u5230\u4e00\u4e2a\u975e\u4ea4\u4e92\u5f0f\u7684login shell\u3002
\n\u4f7f\u7528su -\u5207\u6362\u5230\u6307\u5b9a\u7528\u6237\u65f6\uff0c\u83b7\u5f97\u6b64\u7528\u6237\u7684login shell\u3002\u5982\u679c\u4e0d\u4f7f\u7528-\uff0c\u5219\u83b7\u5f97non-login shell\u3002
\nlogin shell\u542f\u52a8\u65f6\u9996\u5148\u8bfb\u53d6\/etc\/profile\u7cfb\u7edf\u5168\u5c40\u914d\u7f6e\uff0c\u7136\u540e\u4f9d\u6b21\u67e5\u627e~\/.bash_profile\u3001~\/.bash_login\u3001~\/.profile\u4e09\u4e2a\u914d\u7f6e\u6587\u4ef6\uff0c\u5e76\u4e14\u8bfb\u53d6\u9996\u4e2a\u627e\u5230\u7684\u5e76\u4e14\u53ef\u8bfb\u7684\u6587\u4ef6\u3002<\/p>\n

login shell\u9000\u51fa\u65f6\u8bfb\u53d6\u5e76\u6267\u884c~\/.bash_logout\u4e2d\u7684\u547d\u4ee4\u3002 \u5982\u679c\u914d\u7f6e\u6587\u4ef6\u5b58\u5728\u4f46\u4e0d\u53ef\u8bfb\uff0c\u5219\u4f1a\u663e\u793a\u9519\u8bef\u6d88\u606f\uff1b\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0cbash\u5c06\u81ea\u52a8\u641c\u7d22\u4e0b\u4e00\u4e2a\u6587\u4ef6\u3002<\/p>\n

\u9ed8\u8ba4\u5728\/etc\/profile\u6587\u4ef6\u4e2d\u4f1a\u5b9a\u4e49PATH\u3001USER\u3001MAIL\u3001HOSTNAME\u3001HISTSIZE\u7b49\u5168\u5c40\u73af\u5883\u53d8\u91cf\uff0c\u8fd8\u4f1a\u81ea\u52a8\u5bfc\u5165\/etc\/bash.bashrc\u6587\u4ef6\uff08\u5305\u542b\u7cfb\u7edf\u7ea7shell\u51fd\u6570\u548c\u522b\u540d\uff09\uff0c\u4ee5\u53ca\/etc\/profile.d\u8def\u5f84\u4e0b\u88ab\u7528\u4e8e\u9488\u5bf9\u7279\u5b9a\u7a0b\u5e8f\u8fdb\u884c\u521d\u59cb\u5316\u7684\u6240\u6709*.sh\u6587\u4ef6\u3002<\/p>\n

\u4ea4\u4e92\u5f0fnon-login shell<\/strong><\/span><\/div>\n

\u975e\u767b\u5f55shell\u610f\u5473\u7740\u5728\u542f\u52a8\u65f6\u4e0d\u5fc5\u901a\u8fc7\u7cfb\u7edf\u8eab\u4efd\u9a8c\u8bc1\u3002 GUI\u4e2d\u7528\u6237\u6253\u5f00\u7684\u7ec8\u7aef\u9ed8\u8ba4\u4e3a\u975e\u767b\u5f55shell\uff0c\u53ef\u4ee5\u901a\u8fc7logout\u547d\u4ee4\u5224\u65ad\uff1a<\/p>\n

\r\n# \u5728Ubuntu GUI\u684c\u9762\u6253\u5f00\u4e00\u4e2a\u7ec8\u7aef\r\n> logout\r\nbash: logout: not login shell: use `exit'\r\n> bash --login\r\n> logout # \u6b63\u5e38\u767b\u51fa \u4ec0\u4e48\u4e5f\u4e0d\u4f1a\u8f93\u51fa\r\n<\/pre>\n

\u975e\u767b\u5f55shell\u5728\u521d\u59cb\u5316\u65f6\u4ec5\u8bfb\u53d6~\/.bashrc\u8d44\u6e90\u6587\u4ef6, \u800c~\/.bashrc\u6587\u4ef6\u4f1a\u81ea\u52a8\u88ab~\/.bash_profile\u6216~\/.profile\u52a0\u8f7d\uff0c\u56e0\u6b64\u4e3a\u4e86\u4fdd\u8bc1login shell\u548c\u4ea4\u4e92\u5f0fnon-login shell\u5f97\u5230\u76f8\u540c\u7684\u914d\u7f6e\uff0c\u4e00\u822c\u5c06\u73af\u5883\u53d8\u91cf\u5b9a\u4e49\u5728~\/.bashrc\u6587\u4ef6\u4e2d\u3002<\/p>\n

\r\n> echo \"export sflag=\\\"login shell will see this message\\\"\" >> ~\/.profile  \r\n> bash\r\n> echo $sflag \r\n          # \u627e\u4e0d\u5230\u8fd9\u4e2a\u53d8\u91cf \u4f1a\u6253\u5370\u4e00\u4e2a\u7a7a\u884c\r\n> exit\r\n> bash --login \r\n> echo $sflag \r\nlogin shell will see this message \r\n> logout\r\n<\/pre>\n
\u975e\u4ea4\u4e92\u5f0fshell<\/strong><\/span><\/div>\n

\u901a\u8fc7bash\u547d\u4ee4\u6267\u884c\u811a\u672c\u65f6\u4f1a\u4ee5\u975e\u4ea4\u4e92\uff08non-interactively\uff09\u7684\u65b9\u5f0f\u542f\u52a8shell\uff0c\u8fd9\u4fdd\u8bc1\u4e86\u5728\u811a\u672c\u6267\u884c\u8fc7\u7a0b\u4e2d\u4e0d\u4f1a\u88ab\u7528\u6237\u5e72\u6270\u3002\u5728\u975e\u4ea4\u4e92\u5f0f\u811a\u672c\u542f\u52a8\u65f6\uff0c\u4ec5\u4f1a\u52a0\u8f7dBASH_ENV\u53d8\u91cf\u6307\u5411\u7684\u6587\u4ef6\u3002\u4f46\u8981\u6ce8\u610f, \u7531\u4e8ePATH\u53d8\u91cf\u9ed8\u8ba4\u4e0d\u4f1a\u88ab\u975e\u4ea4\u4e92\u5f0fshell\u52a0\u8f7d\uff0c\u56e0\u6b64\u53d8\u91cfBASH_ENV\u7684\u503c\u5e94\u8be5\u4e3a\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n

\u901a\u8fc7\u7279\u6b8a\u53d8\u91cf-\u53ef\u4ee5\u67e5\u770b\u5f53\u524dshell\u7684\u6a21\u5f0f\uff1a<\/p>\n

\r\n> echo $-\r\nhimBHs # \u5e26\u6709'i'\u5c31\u662f\u4ea4\u4e92\u5f0fshell\r\n<\/pre>\n

\u53e6\u4e00\u4e2a\u7b80\u5355\u7684\u65b9\u5f0f\u662f\u68c0\u67e5\u5f53\u524dshell\u4e2d\u662f\u5426\u5b58\u5728\u63d0\u793a\u7b26\u73af\u5883\u53d8\u91cfPS1.<\/p>\n

\r\nif [ -z \"$PS1\" ]; then echo \"\u975e\u4ea4\u4e92\u5f0f\";else echo \"\u4ea4\u4e92\u5f0f\";fi\r\n<\/pre>\n
\u7279\u6b8a\u60c5\u51b5<\/strong><\/div>\n
\u517c\u5bb9\u6a21\u5f0f<\/strong><\/span><\/div>\n

\u5982\u679c\u4f7f\u7528\u547d\u4ee4sh\u8c03\u7528bash\uff0c\u5219\u4e3a\u4e86\u4fdd\u8bc1\u517c\u5bb9\u6027\u4f1a\u6309\u7167sh\u7684\u65b9\u5f0f\u5bf9bash\u8fdb\u884c\u521d\u59cb\u5316\u3002\u4f5c\u4e3alogin shell\u542f\u52a8\u65f6\uff0cbash\u4f9d\u6b21\u8bfb\u53d6\/etc\/profile\u548c~\/.profile\u914d\u7f6e\u6587\u4ef6\u3002\u4f5c\u4e3anon-login shell\u542f\u52a8\u65f6\uff0cbash\u4ec5\u4f1a\u8bfb\u53d6\u73af\u5883\u53d8\u91cfENV\u6307\u5411\u7684\u6587\u4ef6\u3002<\/p>\n

POSIX\u6a21\u5f0f<\/strong><\/span><\/div>\n

\u5f53\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u542f\u52a8bash\u65f6\uff1a<\/p>\n

\r\n\u8bbe\u7f6eset -o posix \u6216 export POSIXLY_CORRECT=1\r\nbash --posix\r\n<\/pre>\n

bash\u4f1a\u5c3d\u53ef\u80fd\u6309\u7167POSIX\u6807\u51c6\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u4ec5\u4f1a\u8bfb\u53d6\u73af\u5883\u53d8\u91cfENV\u6307\u5411\u7684\u6587\u4ef6\u3002<\/p>\n

\u8fdc\u7a0b\u542f\u52a8\u811a\u672c<\/strong><\/span><\/div>\n

\u4f7f\u7528rshd\u8fdc\u7a0b\u542f\u52a8\u811a\u672c\u65f6\u4ec5\u4f1a\u52a0\u8f7d ~\/.bashrc\u6587\u4ef6\uff0c\u4f46\u8981\u6ce8\u610f\u7684\u662f\u5c3d\u91cf\u4e0d\u8981\u4f7f\u7528rlogin, telnet, rsh, rcp\u7b49\u8fdc\u7a0b\u547d\u4ee4\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u547d\u4ee4\u4f1a\u4f20\u8f93\u672a\u52a0\u5bc6\u7684\u660e\u6587\u4fe1\u606f\u3002\u5982\u679c\u6709\u8fdc\u7a0b\u8bbf\u95ee\u9700\u6c42\u5c3d\u91cf\u4f7f\u7528SSH\u3002<\/p>\n

UID\u4e0eEUID\u4e0d\u5339\u914d<\/strong><\/span><\/div>\n

\u5728\u521b\u5efa\u8fdb\u7a0b\u65f6\u4f1a\u5728task_struct\u4e2d\u8bb0\u5f55\u8fdb\u7a0b\u8fd0\u884c\u65f6\u6240\u9700\u8981\u7684\u4fe1\u606f\u3002\u5176\u4e2dUID\uff08\u771f\u5b9e\u7528\u6237ID\uff09\u7528\u4e8e\u8bb0\u5f55\u521b\u5efa\u8fdb\u7a0b\u7684\u7528\u6237\u7684ID\uff0cEUID\uff08\u6709\u6548\u7528\u6237ID\uff09\u7528\u4e8e\u5224\u65ad\u5f53\u524d\u8fdb\u7a0b\u5bf9\u6587\u4ef6\u7684\u8bbf\u95ee\u7ea7\u522b\uff0c\u4e00\u822c\u60c5\u51b5\u4e0bUID = EUID\u3002\u5982\u679c\u53ef\u6267\u884c\u6587\u4ef6\u7684set-user-ID: SUID\u4f4d\u6709\u6548\uff08\u4f8b\u5982\uff1a-rwsr-xr-x\uff0c\u7528\u6237\u7684x\u88ab\u66ff\u6362\u4e3as\uff09\uff0c\u8868\u793a\u5f53\u8be5\u6587\u4ef6\u88ab\u6267\u884c\u65f6\uff0c\u8fdb\u7a0b\u5177\u6709\u6587\u4ef6\u6240\u6709\u8005\u7684\u6743\u9650\u800c\u4e0d\u662f\u6267\u884c\u8005\u7684\u6743\u9650\uff08EUID\u7684\u503c\u4e3a\u6587\u4ef6\u6240\u6709\u8005\u7684ID\uff09\u3002<\/p>\n

\u5982\u679c\u6211\u4eec\u7ed9bash\u53ef\u6267\u884c\u6587\u4ef6\u8bbe\u7f6e\u4e86set-user-id\u6807\u5fd7\uff0c\u90a3\u4e48\u7531\u4e8e\u5176\u9ed8\u8ba4\u6240\u6709\u8005\u4e3aroot\uff0c\u5f53\u5176\u4ed6\u975eroot\u7528\u6237\u8fd0\u884cbash\u65f6\uff0c\u8be5\u8fdb\u7a0b\u7684UID\u5c06\u4e0d\u7b49\u4e8eEUID\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4e3a\u4e86\u4fdd\u8bc1\u5b89\u5168\u6027\uff0cbash\u5728\u521d\u59cb\u5316\u9636\u6bb5\u4e0d\u4f1a\u52a0\u8f7d\u4efb\u4f55\u6587\u4ef6\u3002<\/p>\n

\u53d7\u9650\u5236\u7684shell<\/strong><\/span><\/div>\n

\u901a\u8fc7rbash\u6216bash --restricted\u6216bash -r\u542f\u52a8\u65f6\u4f1a\u751f\u6210\u529f\u80fd\u53d7\u9650\u5236\u7684shell\uff0c\u5177\u4f53\u8868\u73b0\u4e3a\uff1a<\/p>\n

\u4e0d\u80fd\u4f7f\u7528cd\u547d\u4ee4\u5e76\u4e14\u547d\u4ee4\u4e2d\u4e0d\u80fd\u5305\u542b\/
\n\u4e0d\u80fd\u66f4\u6539SHELL\u3001PATH\u3001ENV\u548cBASH_ENV\u73af\u5883\u53d8\u91cf
\nsource\u547d\u4ee4\u7684\u53c2\u6570\u4e5f\u4e0d\u80fd\u5305\u542b\u5e26\u6709\/\u7684\u6587\u4ef6
\nhash \u2013p \u7528\u4e8e\u7ed9\u8def\u5f84\u8d77\u522b\u540d\u7684\u547d\u4ee4\u7684\u53c2\u6570\u4e2d\u4e5f\u4e0d\u80fd\u5305\u542b\/
\n\u521d\u59cb\u5316\u65f6\u4e0d\u4f1a\u5bfc\u5165\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\u5e76\u4e14\u4f1a\u5ffd\u7565SHELLOPTS
\n\u4e0d\u80fd\u4f7f\u7528\u91cd\u5b9a\u5411
\n\u4e0d\u80fd\u4f7f\u7528exec\u547d\u4ee4
\n\u4e0d\u80fd\u4f7f\u7528enable -f\/-d\u589e\u52a0\u5220\u9664\u547d\u4ee4
\n\u4e0d\u80fd\u4f7f\u7528command -p\u6307\u5b9a\u8fd0\u884c\u547d\u4ee4\u9700\u8981\u7684\u8def\u5f84
\n\u4e0d\u80fd\u4e3b\u52a8\u5173\u95ed\u9650\u5236\u6a21\u5f0f
\n\u8fd9\u4e2a\u529f\u80fd\u7406\u8bba\u4e0a\u53ef\u4ee5\u8ba9\u7528\u6237\u5728\u6307\u5b9a\u7684\u6587\u4ef6\u5939\u5185\u6267\u884c\u6307\u5b9a\u7684\u6587\u4ef6\u6765\u5b8c\u6210\u6709\u9650\u7684\u529f\u80fd\uff0c\u4f46\u662f\u5982\u679c\u73af\u5883\u53d8\u91cf\u8bbe\u7f6e\u4e0d\u5f53\u4f1a\u5bfc\u81f4\u7528\u6237\u5f88\u8f7b\u677e\u5730\u5c31\u80fd\u89e3\u9664\u9650\u5236\uff1a<\/p>\n

\r\n> rbash\r\n> cd \/etc\r\nrbash: cd: restricted\r\n> bash\r\n> cd \/etc # \u53ef\u4ee5\u6210\u529f\u6267\u884c\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u65f6\u5019\u6211\u4eec\u5728bash\u73af\u5883\u4e2d\uff0c\u6ca1\u6709\u4efb\u4f55\u9650\u5236\r\n<\/pre>\n

\u4e00\u79cd\u6709\u6548\u7684\u505a\u6cd5\u662f\u7ed9\u65b0\u5efa\u7684\u7528\u6237\u7684\u80fd\u6267\u884c\u7684\u547d\u4ee4\u4f5c\u51fa\u9650\u5236\uff0c\u4f8b\u5982\u6211\u4eec\u53ef\u4ee5\u65b0\u5efa\u4e00\u4e2a\u53ea\u80fd\u6267\u884cftp\u547d\u4ee4\u7684ruser\uff1a<\/p>\n

\r\n> useradd -s \/bin\/rbash ruser # \u8bbe\u7f6e\u7528\u6237\u767b\u5f55\u65f6\u63d0\u4f9b\u7684shell\r\n> chown -R root:ruser \/home\/ruser\/.bashrc \/home\/ruser\/.bash_profile\r\n# \u8bbe\u7f6eroot\u4e3a\u62e5\u6709\u8005\uff0cruser\u7ec4\u4e3a\u7ec4\u62e5\u6709\u8005\uff08\u65b0\u5efa\u7684ruser\u9ed8\u8ba4\u8f93\u5165ruser\u7ec4\uff09\r\n> chmod 640 \/home\/ruser\/.bashrc \/home\/ruser\/.bash_profile\r\n# root\u53ef\u4ee5\u8bfb\u5199\uff0cruser\u7ec4\u91cc\u7684\u7528\u6237\u53ea\u8bfb\uff0c\u5176\u4ed6\u7528\u6237\u4ec0\u4e48\u4e5f\u4e0d\u80fd\u5e72\r\n> mkdir \/home\/ruser\/bin # \u5b58\u50a8\u7528\u6237\u7684\u53ef\u6267\u884c\u6587\u4ef6\u6216\u94fe\u63a5\r\n> echo \"export PATH=\/home\/ruser\/bin\" >> \/home\/ruser\/.bash_profile\r\n> ln -s \/user\/bin\/ftp \/home\/ruser\/bin\/ftp\r\n<\/pre>\n

\u5230\u6b64\u8fd9\u7bc7\u5173\u4e8e\u8be6\u89e3bash\u4e2d\u7684\u521d\u59cb\u5316\u673a\u5236\u7684\u6587\u7ae0\u5c31\u4ecb\u7ecd\u5230\u8fd9\u4e86\u3002<\/name><\/path>\n","protected":false},"excerpt":{"rendered":"

\u5728\u4e0b\u5217\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u83b7\u5f97\u4e00\u4e2alogin shell\uff1a \u767b\u5f55\u7cfb\u7edf\u65f6\u83b7\u5f97\u7684\u9876\u5c42shell\uff0c\u65e0\u8bba\u662f\u901a\u8fc7\u672c\u5730\u7ec8\u7aef\u767b\u5f55 […]<\/p>\n","protected":false},"author":63,"featured_media":192332,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-192331","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/192331","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=192331"}],"version-history":[{"count":1,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/192331\/revisions"}],"predecessor-version":[{"id":192333,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/192331\/revisions\/192333"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/192332"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=192331"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=192331"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=192331"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}