{"id":122962,"date":"2026-11-06T03:57:36","date_gmt":"2026-11-05T19:57:36","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/122962.html"},"modified":"2026-11-06T03:57:36","modified_gmt":"2026-11-05T19:57:36","slug":"linux-sudo-%e4%b8%8e-su-%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/122962.html","title":{"rendered":"LInux | sudo \u4e0e su \u64cd\u4f5c"},"content":{"rendered":"<h2>sudo \u4e0e su \u64cd\u4f5c\u624b\u518c<\/h2>\n<h3>\u76ee\u5f55<\/h3>\n<li>sudoers \u914d\u7f6e<\/li>\n<li>sudo \u5e38\u7528\u64cd\u4f5c<\/li>\n<li>sudo \u5b89\u5168\u673a\u5236<\/li>\n<li>sudo \u9ad8\u7ea7\u7528\u6cd5<\/li>\n<li>\u5b89\u5168\u89c4\u8303<\/li>\n<li>\u6545\u969c\u6392\u67e5<\/li>\n<li>\u540c\u7c7b\u5de5\u5177\u5bf9\u6bd4<\/li>\n<li>su\u3001sudo \u73af\u5883\u4e0e PATH \u884c\u4e3a<\/li>\n<hr \/>\n<h3>1 sudoers \u914d\u7f6e<\/h3>\n<p>sudo \u7684\u6743\u9650\u63a7\u5236\u4f9d\u6258 \/etc\/sudoers&#xff0c;\u6587\u4ef6\u9ed8\u8ba4\u6743\u9650 440\u3002<\/p>\n<p>\u4e0d\u76f4\u63a5\u4f7f\u7528 vi\u3001vim \u7f16\u8f91&#xff0c;\u8bed\u6cd5\u9519\u8bef\u4f1a\u9020\u6210 sudo \u4e0d\u53ef\u7528&#xff1b;\u7f16\u8f91\u7edf\u4e00\u4f7f\u7528 visudo&#xff0c;\u8be5\u5de5\u5177\u5185\u7f6e\u8bed\u6cd5\u6821\u9a8c\u3002<\/p>\n<p><span class=\"token comment\"># \u666e\u901a\u7528\u6237\u6267\u884c&#xff0c;\u9700\u8981\u5df2\u6709 sudo \u6743\u9650<\/span><br \/>\n<span class=\"token function\">sudo<\/span> visudo<br \/>\n<span class=\"token comment\"># root \u76f4\u63a5\u6267\u884c<\/span><br \/>\nvisudo<\/p>\n<p>\u7f16\u8f91\u5b8c\u6210&#xff0c;vi \u6a21\u5f0f\u4e0b\u6309 Esc&#xff0c;\u8f93\u5165 :wq \u4fdd\u5b58\u9000\u51fa\u3002<\/p>\n<p>\u5b58\u5728\u8bed\u6cd5\u9519\u8bef\u65f6\u7ec8\u7aef\u8f93\u51fa syntax error&#xff0c;\u9700\u8981\u4fee\u6b63\u540e\u91cd\u65b0\u4fdd\u5b58\u3002<\/p>\n<h4>1.1 sudoers \u6587\u4ef6\u7ed3\u6784<\/h4>\n<p>\u6587\u4ef6\u5206\u4e3a Defaults\u3001\u7528\u6237\u6743\u9650\u3001\u7ec4\u6743\u9650\u4e09\u6bb5\u3002\u901a\u7528\u89c4\u5219\u683c\u5f0f&#xff1a;<\/p>\n<p>\u7528\u6237\/\u7ec4 \u4e3b\u673a &#061;&#xff08;\u8eab\u4efd&#xff09; \u53ef\u6267\u884c\u547d\u4ee4 # \u4e3a\u6ce8\u91ca\u6807\u8bb0\u3002<\/p>\n<p># Defaults \u6bb5&#xff1a;\u5168\u5c40\u884c\u4e3a\u63a7\u5236<br \/>\nDefaults    env_reset<br \/>\nDefaults    mail_badpass<br \/>\nDefaults    secure_path&#061;&#034;\/usr\/local\/sbin:\/usr\/local\/bin:\/usr\/sbin:\/usr\/bin:\/sbin:\/bin&#034;<br \/>\nDefaults    timestamp_timeout&#061;15<br \/>\nDefaults    env_keep &#043;&#061; &#034;SSH_AUTH_SOCK&#034;<\/p>\n<p># \u7528\u6237\u6743\u9650\u89c4\u5219<br \/>\nroot    ALL&#061;(ALL:ALL) ALL<\/p>\n<p># \u7ec4\u6743\u9650\u89c4\u5219&#xff0c;\u7ec4\u540d\u524d\u7f00 %<br \/>\n%wheel  ALL&#061;(ALL:ALL) ALL<br \/>\n%sudo   ALL&#061;(ALL:ALL) ALL<\/p>\n<p>\u5b57\u6bb5\u91ca\u4e49&#xff1a;<\/p>\n<ul>\n<li>\u4e3b\u673a&#xff1a;\u89c4\u5219\u751f\u6548\u4e3b\u673a\u8303\u56f4<\/li>\n<li>(ALL:ALL)&#xff1a;\u53ef\u5207\u6362\u7528\u6237:\u53ef\u5207\u6362\u7ec4<\/li>\n<li>ALL&#xff1a;\u4ee3\u8868\u5168\u90e8\u547d\u4ee4<\/li>\n<\/ul>\n<h4>1.2 \u914d\u7f6e\u573a\u666f<\/h4>\n<h5>1.2.1 \u6388\u4e88\u5b8c\u6574 sudo \u6743\u9650<\/h5>\n<p>\u65b9\u5f0f\u4e00&#xff1a;\u52a0\u5165\u7cfb\u7edf\u6388\u6743\u7ec4<\/p>\n<p><span class=\"token comment\"># Ubuntu<\/span><br \/>\n<span class=\"token function\">usermod<\/span> <span class=\"token parameter variable\">-aG<\/span> <span class=\"token function\">sudo<\/span> <span class=\"token builtin class-name\">test<\/span><br \/>\n<span class=\"token comment\"># CentOS \/ RHEL<\/span><br \/>\n<span class=\"token function\">usermod<\/span> <span class=\"token parameter variable\">-aG<\/span> wheel <span class=\"token builtin class-name\">test<\/span><\/p>\n<p>\u65b9\u5f0f\u4e8c&#xff1a;\u76f4\u63a5\u5199\u5165 sudoers<\/p>\n<p>test    ALL&#061;(ALL:ALL) ALL<\/p>\n<h5>1.2.2 \u9650\u5b9a\u53ef\u6267\u884c\u547d\u4ee4<\/h5>\n<p>\u547d\u4ee4\u5fc5\u987b\u586b\u5199\u7edd\u5bf9\u8def\u5f84&#xff0c;\u53ef\u643a\u5e26\u53c2\u6570\u5b9e\u73b0\u7cbe\u786e\u5339\u914d\u3002<\/p>\n<p>test    ALL&#061;(ALL:ALL) \/usr\/bin\/systemctl restart nginx, \/sbin\/ifconfig eth0, \/sbin\/ifup eth0<\/p>\n<p>\u4ec5\u5141\u8bb8\u6307\u5b9a\u547d\u4ee4\u514d\u5bc6\u6267\u884c&#xff1a;<\/p>\n<p>deploy    ALL&#061;(ALL:ALL) NOPASSWD: \/usr\/bin\/systemctl restart nginx<\/p>\n<h5>1.2.3 \u514d\u5bc6\u7801\u6388\u6743<\/h5>\n<p># \u5168\u90e8\u547d\u4ee4\u514d\u5bc6<br \/>\ntest    ALL&#061;(ALL:ALL) NOPASSWD: ALL<br \/>\n# \u6307\u5b9a\u547d\u4ee4\u514d\u5bc6<br \/>\ntest    ALL&#061;(ALL:ALL) NOPASSWD: \/usr\/sbin\/reboot, \/usr\/bin\/systemctl restart nginx<\/p>\n<h5>1.2.4 \u9650\u5b9a\u5207\u6362\u76ee\u6807\u7528\u6237<\/h5>\n<p>test    ALL&#061;(nginx:ALL) ALL<\/p>\n<p>\u8be5\u914d\u7f6e\u4e0b\u7528\u6237\u4ec5\u53ef\u5207\u6362\u81f3 nginx&#xff0c;\u65e0\u6cd5\u5207\u6362\u81f3 root\u3002<\/p>\n<h5>1.2.5 \u9ed1\u540d\u5355\u7ba1\u63a7<\/h5>\n<p>! \u6807\u8bb0\u7981\u6b62\u9879&#xff1b;\u9ed1\u540d\u5355\u6761\u76ee\u7f6e\u4e8e\u767d\u540d\u5355\u4e4b\u540e&#xff0c;\u89c4\u5219\u4ece\u4e0a\u81f3\u4e0b\u987a\u5e8f\u5339\u914d\u3002<\/p>\n<p>%sudo   ALL&#061;(ALL:ALL) ALL, !\/usr\/bin\/rm -rf *<br \/>\ntest    ALL&#061;(ALL:ALL) ALL, !\/usr\/sbin\/visudo<\/p>\n<h5>1.2.6 \u522b\u540d\u5b9a\u4e49<\/h5>\n<p>\u522b\u540d\u540d\u79f0\u4f7f\u7528\u5927\u5199&#xff0c;\u5206\u4e3a User_Alias\u3001Cmnd_Alias\u3001Host_Alias\u3002<\/p>\n<p>User_Alias  OPS &#061; user1, user2, user3<br \/>\nCmnd_Alias  PACKAGES &#061; \/usr\/bin\/apt, \/usr\/bin\/yum, \/usr\/bin\/dnf<br \/>\nCmnd_Alias  SERVICES &#061; \/usr\/bin\/systemctl, \/usr\/sbin\/service<br \/>\nOPS    ALL&#061;(ALL:ALL) PACKAGES, SERVICES<\/p>\n<h4>1.3 \u4e3b\u673a\u5b57\u6bb5\u5339\u914d\u89c4\u5219<\/h4>\n<p>\u4e3b\u673a\u5b57\u6bb5\u5728\u672c\u673a\u5b8c\u6210\u5339\u914d&#xff0c;\u5339\u914d\u5bf9\u8c61\u4e3a\u4e3b\u673a\u540d\u3001FQDN\u3001IP\u3002\u652f\u6301\u4ee5\u4e0b\u7c7b\u578b&#xff1a;<\/p>\n<li>\u77ed\u4e3b\u673a\u540d \/ FQDNtest    web01&#061;(ALL:ALL) NOPASSWD: \/usr\/bin\/systemctl restart nginx\n <\/li>\n<li>\u5355\u4e2a IPtest    192.168.1.10&#061;(ALL:ALL) NOPASSWD: \/usr\/bin\/systemctl restart nginx\n <\/li>\n<li>CIDR \u7f51\u6bb5test    192.168.1.0\/24&#061;(ALL:ALL) NOPASSWD: \/usr\/bin\/systemctl restart nginx\n <\/li>\n<li>Host_AliasHost_Alias WEBS &#061; web01, web02, web03, 192.168.1.10, 192.168.1.11<br \/>\ntest    WEBS&#061;(ALL:ALL) NOPASSWD: \/usr\/bin\/systemctl restart nginx\n <\/li>\n<li>Netgroup&#xff08;NIS\/\u57df\u73af\u5883&#xff0c;\u524d\u7f00 &#043;&#xff09;test    &#043;webservers&#061;(ALL:ALL) NOPASSWD: \/usr\/bin\/systemctl restart nginx\n <\/li>\n<li>\u53d6\u53cd !test    ALL,!web03&#061;(ALL:ALL) NOPASSWD: \/usr\/bin\/systemctl restart nginx\n <\/li>\n<li>\u9017\u53f7\u5206\u9694\u4e3b\u673a\u5217\u8868test    web01,web02,192.168.1.20&#061;(ALL:ALL) NOPASSWD: \/usr\/bin\/systemctl restart nginx\n <\/li>\n<p>\u4e3b\u673a\u540d\u89e3\u6790\u4f9d\u8d56 \/etc\/hosts \u6216 DNS&#xff0c;\u89e3\u6790\u5f02\u5e38\u4f1a\u9020\u6210\u5339\u914d\u5931\u8d25\u3002<\/p>\n<h4>1.4 \u591a\u4e3b\u673a\u90e8\u7f72\u65b9\u6848<\/h4>\n<ul>\n<li>\u5c0f\u89c4\u6a21\u96c6\u7fa4&#xff1a;\u5206\u53d1\u4e3b sudoers \u6587\u4ef6&#xff0c;\u6216\u5728 \/etc\/sudoers.d\/ \u4e0b\u653e\u72ec\u7acb\u89c4\u5219\u6587\u4ef6&#xff0c;\u4f7f\u7528 Ansible \u63a8\u9001<\/li>\n<li>\u5927\u89c4\u6a21\u57df\u73af\u5883&#xff1a;LDAP \u96c6\u4e2d\u7ef4\u62a4 sudoHost<\/li>\n<\/ul>\n<p>\u63a8\u8350\u5c06\u81ea\u5b9a\u4e49\u89c4\u5219\u653e\u5165 \/etc\/sudoers.d\/&#xff0c;\u4e0d\u4fee\u6539\u4e3b\u6587\u4ef6&#xff0c;\u4fbf\u4e8e\u5206\u53d1\u4e0e\u56de\u6eda\u3002<\/p>\n<p>visudo <span class=\"token parameter variable\">-f<\/span> \/etc\/sudoers.d\/90_custom_rules<\/p>\n<p>\u6a21\u677f&#xff1a;<\/p>\n<p>deploy    web01,web02&#061;(ALL:ALL) NOPASSWD: \/usr\/bin\/systemctl restart nginx, \/usr\/bin\/tar<\/p>\n<p>\u914d\u7f6e\u5b8c\u6210\u540e&#xff0c;\u5207\u6362\u5bf9\u5e94\u7528\u6237\u6267\u884c sudo -l \u6821\u9a8c\u6743\u9650\u3002<\/p>\n<h4>1.5 sudo \u6267\u884c\u6d41\u7a0b<\/h4>\n<p>  #mermaid-svg-GODb4VugpWdzvx7p{font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-GODb4VugpWdzvx7p .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-GODb4VugpWdzvx7p .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-GODb4VugpWdzvx7p .error-icon{fill:#552222;}#mermaid-svg-GODb4VugpWdzvx7p .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-GODb4VugpWdzvx7p .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-GODb4VugpWdzvx7p .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-GODb4VugpWdzvx7p .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-GODb4VugpWdzvx7p .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-GODb4VugpWdzvx7p .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-GODb4VugpWdzvx7p .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-GODb4VugpWdzvx7p .marker{fill:#333333;stroke:#333333;}#mermaid-svg-GODb4VugpWdzvx7p .marker.cross{stroke:#333333;}#mermaid-svg-GODb4VugpWdzvx7p svg{font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-GODb4VugpWdzvx7p p{margin:0;}#mermaid-svg-GODb4VugpWdzvx7p .label{font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;color:#333;}#mermaid-svg-GODb4VugpWdzvx7p .cluster-label text{fill:#333;}#mermaid-svg-GODb4VugpWdzvx7p .cluster-label span{color:#333;}#mermaid-svg-GODb4VugpWdzvx7p .cluster-label span p{background-color:transparent;}#mermaid-svg-GODb4VugpWdzvx7p .label text,#mermaid-svg-GODb4VugpWdzvx7p span{fill:#333;color:#333;}#mermaid-svg-GODb4VugpWdzvx7p .node rect,#mermaid-svg-GODb4VugpWdzvx7p .node circle,#mermaid-svg-GODb4VugpWdzvx7p .node ellipse,#mermaid-svg-GODb4VugpWdzvx7p .node polygon,#mermaid-svg-GODb4VugpWdzvx7p .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-GODb4VugpWdzvx7p .rough-node .label text,#mermaid-svg-GODb4VugpWdzvx7p .node .label text,#mermaid-svg-GODb4VugpWdzvx7p .image-shape .label,#mermaid-svg-GODb4VugpWdzvx7p .icon-shape .label{text-anchor:middle;}#mermaid-svg-GODb4VugpWdzvx7p .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-GODb4VugpWdzvx7p .rough-node .label,#mermaid-svg-GODb4VugpWdzvx7p .node .label,#mermaid-svg-GODb4VugpWdzvx7p .image-shape .label,#mermaid-svg-GODb4VugpWdzvx7p .icon-shape .label{text-align:center;}#mermaid-svg-GODb4VugpWdzvx7p .node.clickable{cursor:pointer;}#mermaid-svg-GODb4VugpWdzvx7p .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-GODb4VugpWdzvx7p .arrowheadPath{fill:#333333;}#mermaid-svg-GODb4VugpWdzvx7p .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-GODb4VugpWdzvx7p .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-GODb4VugpWdzvx7p .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-GODb4VugpWdzvx7p .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-GODb4VugpWdzvx7p .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-GODb4VugpWdzvx7p .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-GODb4VugpWdzvx7p .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-GODb4VugpWdzvx7p .cluster text{fill:#333;}#mermaid-svg-GODb4VugpWdzvx7p .cluster span{color:#333;}#mermaid-svg-GODb4VugpWdzvx7p div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-GODb4VugpWdzvx7p .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-GODb4VugpWdzvx7p rect.text{fill:none;stroke-width:0;}#mermaid-svg-GODb4VugpWdzvx7p .icon-shape,#mermaid-svg-GODb4VugpWdzvx7p .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-GODb4VugpWdzvx7p .icon-shape p,#mermaid-svg-GODb4VugpWdzvx7p .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-GODb4VugpWdzvx7p .icon-shape .label rect,#mermaid-svg-GODb4VugpWdzvx7p .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-GODb4VugpWdzvx7p .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-GODb4VugpWdzvx7p .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-GODb4VugpWdzvx7p :root{&#8211;mermaid-font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;}<\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/p>\n<p>\u5931\u8d25<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/p>\n<p>\u6210\u529f<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/p>\n<p>\u672a\u6388\u6743<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/p>\n<p>\u5df2\u6388\u6743<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"nodeLabel\"><\/p>\n<p>\u666e\u901a\u7528\u6237<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"nodeLabel\"><\/p>\n<p>\u8f93\u5165\u547d\u4ee4<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"nodeLabel\"><\/p>\n<p>sudo<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"nodeLabel\"><\/p>\n<p>\u9a8c\u8bc1\u7528\u6237\u8eab\u4efd<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"nodeLabel\"><\/p>\n<p>\u9a8c\u8bc1\u7ed3\u679c<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"nodeLabel\"><\/p>\n<p>\u5bc6\u7801\u9519\u8bef<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"nodeLabel\"><\/p>\n<p>\u9000\u51fa<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"nodeLabel\"><\/p>\n<p>\u68c0\u67e5\u6388\u6743\u89c4\u5219<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"nodeLabel\"><\/p>\n<p>\u6388\u6743\u7ed3\u679c<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"nodeLabel\"><\/p>\n<p>\u62d2\u7edd\u6267\u884c<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"nodeLabel\"><\/p>\n<p>\u4ee5\u6307\u5b9a\u7528\u6237\u6267\u884c<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"nodeLabel\"><\/p>\n<p>\u8fd4\u56de\u7ed3\u679c<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"nodeLabel\"><\/p>\n<p>\u7ed3\u675f<\/p>\n<p><\/span><\/p>\n<h4>1.6 sudoers \u914d\u7f6e\u7ea6\u675f<\/h4>\n<ul>\n<li>\u547d\u4ee4\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84&#xff0c;\u5e26\u53c2\u6570\u6761\u76ee\u4e3a\u7cbe\u786e\u5339\u914d<\/li>\n<li>NOPASSWD \u4ec5\u7528\u4e8e\u4fe1\u4efb\u4e3b\u4f53\u4e0e\u81ea\u52a8\u5316\u573a\u666f<\/li>\n<li>\u522b\u540d\u7edf\u4e00\u5927\u5199<\/li>\n<li>\u4fee\u6539\u540e\u6267\u884c sudo -l \u9a8c\u8bc1<\/li>\n<\/ul>\n<hr \/>\n<h3>2 sudo \u5e38\u7528\u64cd\u4f5c<\/h3>\n<p>\u901a\u7528\u8bed\u6cd5&#xff1a;sudo [\u9009\u9879] \u547d\u4ee4<\/p>\n<h4>2.1 \u5207\u6362 root shell<\/h4>\n<p><span class=\"token function\">sudo<\/span> <span class=\"token parameter variable\">-i<\/span><br \/>\n<span class=\"token function\">sudo<\/span> <span class=\"token function\">su<\/span> &#8211;<br \/>\n<span class=\"token function\">sudo<\/span> <span class=\"token parameter variable\">-s<\/span><br \/>\n<span class=\"token builtin class-name\">exit<\/span><\/p>\n<h4>2.2 \u6307\u5b9a\u8eab\u4efd\u6267\u884c<\/h4>\n<p><span class=\"token function\">sudo<\/span> <span class=\"token parameter variable\">-u<\/span> nginx \/usr\/sbin\/nginx<br \/>\n<span class=\"token function\">sudo<\/span> <span class=\"token parameter variable\">-u<\/span> www-data <span class=\"token function\">ls<\/span> <span class=\"token parameter variable\">-l<\/span> \/var\/www\/html<br \/>\n<span class=\"token function\">sudo<\/span> <span class=\"token parameter variable\">-u<\/span> nginx <span class=\"token function\">whoami<\/span><\/p>\n<h4>2.3 \u67e5\u770b\u6743\u9650\u6e05\u5355<\/h4>\n<p><span class=\"token function\">sudo<\/span> <span class=\"token parameter variable\">-l<\/span><\/p>\n<h4>2.4 \u5bc6\u7801\u7f13\u5b58\u63a7\u5236<\/h4>\n<p><span class=\"token comment\"># \u5237\u65b0\u7f13\u5b58\u8ba1\u65f6&#xff0c;\u91cd\u65b0\u6821\u9a8c\u5bc6\u7801<\/span><br \/>\n<span class=\"token function\">sudo<\/span> <span class=\"token parameter variable\">-v<\/span><br \/>\n<span class=\"token comment\"># \u6e05\u9664\u5bc6\u7801\u7f13\u5b58<\/span><br \/>\n<span class=\"token function\">sudo<\/span> <span class=\"token parameter variable\">-k<\/span><\/p>\n<h4>2.5 \u591a\u6761\u547d\u4ee4\u6279\u91cf\u6267\u884c<\/h4>\n<p><span class=\"token function\">sudo<\/span> <span class=\"token function\">bash<\/span> <span class=\"token parameter variable\">-c<\/span> <span class=\"token string\">&#039;apt update -y &amp;&amp; apt upgrade -y &amp;&amp; systemctl restart sshd&#039;<\/span><\/p>\n<h4>2.6 \u4fdd\u7559\u73af\u5883\u53d8\u91cf<\/h4>\n<p><span class=\"token function\">sudo<\/span> <span class=\"token parameter variable\">-E<\/span> \/usr\/local\/nginx\/sbin\/nginx <span class=\"token parameter variable\">-s<\/span> reload<br \/>\n<span class=\"token function\">sudo<\/span> <span class=\"token parameter variable\">-E<\/span> <span class=\"token function\">env<\/span> <span class=\"token operator\">|<\/span> <span class=\"token function\">grep<\/span> <span class=\"token environment constant\">PATH<\/span><\/p>\n<hr \/>\n<h3>3 sudo \u5b89\u5168\u673a\u5236<\/h3>\n<h4>3.1 \u5bc6\u7801\u8d85\u65f6<\/h4>\n<p>\u9ed8\u8ba4 timestamp_timeout&#061;15&#xff0c;\u5355\u4f4d\u5206\u949f\u3002<\/p>\n<p>Defaults    timestamp_timeout&#061;30<br \/>\nDefaults    timestamp_timeout&#061;0<br \/>\nDefaults    timestamp_timeout&#061;-1<\/p>\n<ul>\n<li>0&#xff1a;\u6bcf\u6b21\u8c03\u7528\u5747\u6821\u9a8c\u5bc6\u7801<\/li>\n<li>-1&#xff1a;\u6c38\u4e45\u7f13\u5b58<\/li>\n<\/ul>\n<h4>3.2 \u5ba1\u8ba1\u65e5\u5fd7<\/h4>\n<p>\u9ed8\u8ba4\u8def\u5f84&#xff1a;<\/p>\n<ul>\n<li>CentOS \/ RHEL&#xff1a;\/var\/log\/secure<\/li>\n<li>Ubuntu \/ Debian&#xff1a;\/var\/log\/auth.log<\/li>\n<\/ul>\n<p>\u81ea\u5b9a\u4e49\u65e5\u5fd7\u914d\u7f6e&#xff1a;<\/p>\n<p>Defaults    logfile&#061;&#034;\/var\/log\/sudo.log&#034;<br \/>\nDefaults    log_format&#061;&#034;%u : %h : %t : %p : %m&#034;<\/p>\n<p>\u68c0\u7d22\u547d\u4ee4&#xff1a;<\/p>\n<p><span class=\"token function\">grep<\/span> <span class=\"token function\">sudo<\/span> \/var\/log\/secure<br \/>\n<span class=\"token function\">grep<\/span> <span class=\"token function\">sudo<\/span> \/var\/log\/auth.log<br \/>\n<span class=\"token function\">tail<\/span> <span class=\"token parameter variable\">-f<\/span> \/var\/log\/sudo.log<br \/>\n<span class=\"token function\">grep<\/span> <span class=\"token string\">&#034;rm -rf&#034;<\/span> \/var\/log\/sudo.log<\/p>\n<h4>3.3 \u73af\u5883\u53d8\u91cf\u7ba1\u63a7<\/h4>\n<p>Defaults    env_reset<br \/>\nDefaults    env_keep &#043;&#061; &#034;SSH_AUTH_SOCK&#034;<br \/>\nDefaults    secure_path&#061;&#034;\/usr\/local\/sbin:\/usr\/local\/bin:\/usr\/sbin:\/usr\/bin:\/sbin:\/bin&#034;<\/p>\n<p>env_reset \u5f00\u542f\u65f6\u6e05\u7406\u7528\u6237\u73af\u5883&#xff1b;secure_path \u5355\u72ec\u8986\u76d6 PATH&#xff0c;\u9632\u6b62\u8def\u5f84\u52ab\u6301\u3002<\/p>\n<p>PATH \u76f8\u5173\u6545\u969c&#xff1a;\u7a0b\u5e8f\u4e0d\u5728 secure_path \u5185&#xff0c;sudo \u63d0\u793a command not found\u3002\u5904\u7406\u65b9\u5f0f&#xff1a;<\/p>\n<li>\u8c03\u7528\u7edd\u5bf9\u8def\u5f84<\/li>\n<li>sudo -E<\/li>\n<li>\u4fee\u6539 secure_path<\/li>\n<hr \/>\n<h3>4 \u9ad8\u7ea7\u7528\u6cd5<\/h3>\n<h4>4.1 \u8fdc\u7aef SSH \u63d0\u6743\u6267\u884c<\/h4>\n<p>\u524d\u7f6e&#xff1a;SSH \u514d\u5bc6\u901a\u9053\u3002<\/p>\n<p>test    ALL&#061;(ALL:ALL) \/usr\/bin\/ssh root&#064;192.168.1.100<\/p>\n<p><span class=\"token function\">sudo<\/span> <span class=\"token function\">ssh<\/span> root&#064;192.168.1.100<br \/>\n<span class=\"token function\">sudo<\/span> <span class=\"token function\">ssh<\/span> root&#064;192.168.1.100 <span class=\"token string\">&#039;systemctl restart nginx&#039;<\/span><\/p>\n<h4>4.2 \u81ea\u52a8\u5316\u811a\u672c<\/h4>\n<p>\u9075\u5faa\u6700\u5c0f\u6743\u9650&#xff0c;\u4ec5\u5f00\u653e\u811a\u672c\u6240\u9700\u547d\u4ee4\u514d\u5bc6&#xff1a;<\/p>\n<p>test    ALL&#061;(ALL:ALL) NOPASSWD: \/usr\/bin\/systemctl restart nginx, \/usr\/bin\/tar, \/bin\/rm<\/p>\n<hr \/>\n<h3>5 \u5b89\u5168\u89c4\u8303<\/h3>\n<li>\u9075\u5faa\u6700\u5c0f\u6743\u9650&#xff0c;\u907f\u514d NOPASSWD: ALL\u3001ALL \u5168\u6743\u9650<\/li>\n<li>SSH \u914d\u7f6e PermitRootLogin no&#xff0c;\u7981\u6b62 root \u76f4\u63a5\u5bc6\u7801\u767b\u5f55<\/li>\n<li>\u5b9a\u671f\u5ba1\u8ba1 sudo \u65e5\u5fd7<\/li>\n<li>\u4fdd\u6301 \/etc\/sudoers \u6743\u9650 440<\/li>\n<li>\u81ea\u52a8\u5316\u3001cron \u573a\u666f\u6ce8\u91ca Defaults requiretty<\/li>\n<li>\u811a\u672c\u7981\u6b62\u660e\u6587\u5199\u5165\u5bc6\u7801<\/li>\n<li>\u53ca\u65f6\u5347\u7ea7 sudo \u8f6f\u4ef6\u5305&#xff0c;\u4fee\u590d\u6f0f\u6d1e<\/li>\n<li>\u591a\u4e3b\u673a\u573a\u666f\u4f7f\u7528\u5821\u5792\u673a\u96c6\u4e2d\u5ba1\u8ba1<\/li>\n<li>\u5468\u671f\u6027\u6e05\u7406\u5931\u6548\u6743\u9650\u3001\u79bb\u804c\u7528\u6237\u89c4\u5219<\/li>\n<hr \/>\n<h3>6 \u6545\u969c\u6392\u67e5<\/h3>\n<h4>6.1 xxx is not in the sudoers file<\/h4>\n<p>\u7528\u6237\u672a\u52a0\u5165\u6388\u6743\u7ec4\u3001\u65e0\u5339\u914d sudoers \u89c4\u5219\u3002<\/p>\n<p><span class=\"token function\">su<\/span> &#8211; root<br \/>\n<span class=\"token function\">usermod<\/span> <span class=\"token parameter variable\">-aG<\/span> <span class=\"token function\">sudo<\/span> <span class=\"token builtin class-name\">test<\/span><br \/>\n<span class=\"token function\">usermod<\/span> <span class=\"token parameter variable\">-aG<\/span> wheel <span class=\"token builtin class-name\">test<\/span><br \/>\nvisudo<br \/>\n<span class=\"token builtin class-name\">test<\/span>    <span class=\"token assign-left variable\">ALL<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token punctuation\">(<\/span>ALL:ALL<span class=\"token punctuation\">)<\/span> ALL<\/p>\n<p>\u4fee\u6539\u540e\u91cd\u65b0\u767b\u5f55\u7528\u6237\u3002<\/p>\n<h4>6.2 must have a tty to run sudo<\/h4>\n<p>sudoers \u542f\u7528 requiretty&#xff0c;\u975e\u4ea4\u4e92\u5f0f\u573a\u666f\u62a5\u9519\u3002<\/p>\n<p># Defaults requiretty<\/p>\n<h4>6.3 unable to resolve host<\/h4>\n<p>\/etc\/hosts \u7f3a\u5c11\u4e3b\u673a\u540d\u6620\u5c04&#xff0c;\u8865\u5145\u6761\u76ee&#xff1a;<\/p>\n<p>127.0.0.1   localhost linux-server<br \/>\n::1         localhost linux-server<\/p>\n<h4>6.4 command not found<\/h4>\n<ul>\n<li>\u7a0b\u5e8f\u672a\u5b89\u88c5<\/li>\n<li>\u7a0b\u5e8f\u8def\u5f84\u4e0d\u5728 secure_path<\/li>\n<li>\u672a\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84<\/li>\n<\/ul>\n<h4>6.5 password incorrect<\/h4>\n<ul>\n<li>\u8f93\u5165\u5927\u5c0f\u5199\u3001\u7279\u6b8a\u5b57\u7b26\u9519\u8bef<\/li>\n<li>\u7528\u6237\u5bc6\u7801\u8fc7\u671f&#xff0c;chage -l test \u67e5\u770b&#xff0c;passwd test \u4fee\u6539<\/li>\n<li>sudoers \u8bed\u6cd5\u9519\u8bef&#xff0c;visudo -c \u6821\u9a8c<\/li>\n<li>\u7528\u6237\u540d\u3001\u7ec4\u540d\u62fc\u5199\u9519\u8bef<\/li>\n<\/ul>\n<h4>6.6 sudo \u6574\u4f53\u5931\u6548<\/h4>\n<p>\u4f18\u5148\u6267\u884c\u8bed\u6cd5\u6821\u9a8c&#xff1a;<\/p>\n<p>visudo <span class=\"token parameter variable\">-c<\/span><\/p>\n<ul>\n<li>\u53ef su &#8211; root&#xff1a;root \u767b\u5f55\u540e\u4fee\u590d\u914d\u7f6e<\/li>\n<li>su \u540c\u6837\u4e0d\u53ef\u7528&#xff1a;\u8fdb\u5165\u5355\u7528\u6237\u6a21\u5f0f\u4fee\u590d \u9644\u52a0\u68c0\u67e5\u9879&#xff1a;\u6587\u4ef6\u6743\u9650 440\u3001\u5305\u91cd\u88c5\u3001\u78c1\u76d8\u7a7a\u95f4\u3001\u6587\u4ef6\u7cfb\u7edf\u53ea\u8bfb\u72b6\u6001<\/li>\n<\/ul>\n<h4>6.7 \u4fee\u6539\u5bc6\u7801\u540e sudo \u9a8c\u8bc1\u5931\u8d25<\/h4>\n<p>\u7f13\u5b58\u65e7\u5bc6\u7801&#xff0c;\u6e05\u9664\u7f13\u5b58&#xff1a;<\/p>\n<p><span class=\"token function\">sudo<\/span> <span class=\"token parameter variable\">-k<\/span><\/p>\n<h4>6.8 sudo \u6267\u884c\u5361\u4f4f<\/h4>\n<ul>\n<li>Ctrl&#043;C \u7ec8\u6b62\u8fdb\u7a0b<\/li>\n<li>\u68c0\u67e5\u7cfb\u7edf\u8d1f\u8f7d top<\/li>\n<li>\u68c0\u67e5\u65e5\u5fd7\u6587\u4ef6\u5199\u5165\u6743\u9650<\/li>\n<li>SSH \u8fdc\u7aef\u6267\u884c\u6dfb\u52a0 -t \u5206\u914d TTY<\/li>\n<li>journalctl -u sudo \u67e5\u770b\u65e5\u5fd7<\/li>\n<\/ul>\n<hr \/>\n<h3>7 \u540c\u7c7b\u5de5\u5177\u5bf9\u6bd4<\/h3>\n<h4>7.1 su<\/h4>\n<p>\u539f\u751f\u5207\u6362\u7528\u6237\u5de5\u5177&#xff1b;su &#8211; root \u9700\u8981 root \u5bc6\u7801&#xff1b;\u65e0\u7ec6\u7c92\u5ea6\u6743\u9650\u63a7\u5236&#xff0c;\u5ba1\u8ba1\u80fd\u529b\u5f31\u3002\u9002\u5408\u5355\u4e3b\u673a\u5e94\u6025\u3002<\/p>\n<h4>7.2 doas<\/h4>\n<p>OpenBSD \u9879\u76ee&#xff0c;\u914d\u7f6e\u7b80\u6d01&#xff0c;\u4f53\u79ef\u66f4\u5c0f&#xff1b;\u4e3b\u6d41\u53d1\u884c\u7248\u4e0d\u9884\u88c5\u3002<\/p>\n<p><span class=\"token function\">apt<\/span> <span class=\"token function\">install<\/span> doas<br \/>\nyum <span class=\"token function\">install<\/span> doas<\/p>\n<p>\u914d\u7f6e\u6587\u4ef6 \/etc\/doas.conf<\/p>\n<p>permit test as root cmd nginx<\/p>\n<h4>7.3 pkexec<\/h4>\n<p>PolicyKit \u7ec4\u4ef6&#xff0c;\u9762\u5411\u56fe\u5f62\u4f1a\u8bdd\u63d0\u6743&#xff1b;\u670d\u52a1\u5668\u7ec8\u7aef\u573a\u666f\u4e0d\u9002\u7528\u3002<\/p>\n<p>pkexec gedit \/etc\/hosts<\/p>\n<hr \/>\n<h3>8 su\u3001sudo \u73af\u5883\u4e0e PATH \u884c\u4e3a<\/h3>\n<h4>8.1 \u57fa\u7840\u91ca\u4e49<\/h4>\n<ul>\n<li>su&#xff1a;switch user&#xff0c;\u5207\u6362\u7528\u6237<\/li>\n<li>sudo&#xff1a;superuser do&#xff0c;\u4ee5\u8d85\u7ea7\u7528\u6237\u8eab\u4efd\u6267\u884c\u547d\u4ee4<\/li>\n<li>-&#xff08;su -&#xff09;&#xff1a;&#8211;login&#xff0c;\u767b\u5f55\u5f0f\u5207\u6362&#xff0c;\u52a0\u8f7d\u76ee\u6807\u7528\u6237\u914d\u7f6e&#xff0c;\u8fdb\u5165\u76ee\u6807\u7528\u6237\u5bb6\u76ee\u5f55<\/li>\n<li>-i&#xff08;sudo -i&#xff09;&#xff1a;\u4ea4\u4e92\u5f0f\u767b\u5f55 shell<\/li>\n<li>-s&#xff08;sudo -s&#xff09;&#xff1a;\u542f\u52a8 shell&#xff0c;\u4e0d\u6267\u884c\u767b\u5f55\u521d\u59cb\u5316<\/li>\n<\/ul>\n<h4>8.2 root \u5207\u6362\u65b9\u5f0f\u5bf9\u6bd4<\/h4>\n<table>\n<tr>\u547d\u4ee4\u6a21\u5f0f\u5de5\u4f5c\u76ee\u5f55\u73af\u5883\u6765\u6e90PATH \u884c\u4e3a\u9700\u8981 root \u5bc6\u7801<\/tr>\n<tbody>\n<tr>\n<td align=\"left\">su<\/td>\n<td align=\"left\">\u975e\u767b\u5f55 shell<\/td>\n<td align=\"left\">\u4fdd\u6301\u539f\u76ee\u5f55<\/td>\n<td align=\"left\">\u7ee7\u627f\u5f53\u524d\u7528\u6237\u5927\u90e8\u5206\u73af\u5883\u53d8\u91cf&#xff1b;\u901a\u5e38\u52a0\u8f7d\u76ee\u6807\u7528\u6237 ~\/.bashrc<\/td>\n<td align=\"left\">\u4fdd\u7559\u539f\u7528\u6237 PATH<\/td>\n<td align=\"left\"><\/td>\n<\/tr>\n<tr>\n<td align=\"left\">su &#8211; \/ su -l \/ su &#8211; root<\/td>\n<td align=\"left\">\u767b\u5f55 shell<\/td>\n<td align=\"left\">\/root<\/td>\n<td align=\"left\">\u52a0\u8f7d root \u7684 \/etc\/profile\u3001~\/.profile\/~\/.bash_profile&#xff0c;\u901a\u5e38\u518d source ~\/.bashrc<\/td>\n<td align=\"left\">\u4f7f\u7528 root \u767b\u5f55\u73af\u5883 PATH<\/td>\n<td align=\"left\"><\/td>\n<\/tr>\n<tr>\n<td align=\"left\">sudo cmd<\/td>\n<td align=\"left\">sudo \u5355\u6b21\u6267\u884c<\/td>\n<td align=\"left\">\u4fdd\u6301\u539f\u76ee\u5f55<\/td>\n<td align=\"left\">\u53d7 env_reset \u9650\u5236&#xff0c;\u4ec5\u4fdd\u7559\u5c11\u91cf\u53d8\u91cf&#xff1b;\u53ef\u901a\u8fc7 env_keep \/ -E \u4fdd\u7559\u6307\u5b9a\u53d8\u91cf<\/td>\n<td align=\"left\">\u9ed8\u8ba4\u4f7f\u7528 secure_path<\/td>\n<td align=\"left\"><\/td>\n<\/tr>\n<tr>\n<td align=\"left\">sudo -i<\/td>\n<td align=\"left\">\u6a21\u62df root \u767b\u5f55 shell<\/td>\n<td align=\"left\">\/root<\/td>\n<td align=\"left\">\u6784\u9020 root \u767b\u5f55\u73af\u5883&#xff0c;\u8bfb\u53d6 root \u767b\u5f55\u914d\u7f6e\u6587\u4ef6<\/td>\n<td align=\"left\">\u63a5\u8fd1 root \u767b\u5f55 PATH&#xff0c;\u4f46\u53ef\u80fd\u53d7 secure_path \u5f71\u54cd<\/td>\n<td align=\"left\"><\/td>\n<\/tr>\n<tr>\n<td align=\"left\">sudo su &#8211;<\/td>\n<td align=\"left\">\u767b\u5f55 shell<\/td>\n<td align=\"left\">\/root<\/td>\n<td align=\"left\">\u5148\u901a\u8fc7 sudo \u63d0\u6743&#xff0c;\u518d\u6267\u884c su -&#xff0c;\u52a0\u8f7d root \u767b\u5f55\u914d\u7f6e<\/td>\n<td align=\"left\">\u4e0e su &#8211; \u57fa\u672c\u4e00\u81f4<\/td>\n<td align=\"left\"><\/td>\n<\/tr>\n<tr>\n<td align=\"left\">sudo -s<\/td>\n<td align=\"left\">\u975e\u767b\u5f55 root shell<\/td>\n<td align=\"left\">\u4fdd\u6301\u539f\u76ee\u5f55&#xff08;HOME \u901a\u5e38\u4ecd\u4e3a\u8c03\u7528\u8005&#xff09;<\/td>\n<td align=\"left\">\u53d7 sudoers \u73af\u5883\u89c4\u5219\u7ea6\u675f&#xff1b;\u901a\u5e38\u53ea\u52a0\u8f7d root ~\/.bashrc<\/td>\n<td align=\"left\">\u53d7 secure_path \/ env_reset \u7ea6\u675f<\/td>\n<td align=\"left\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6ce8&#xff1a;sudo su &#8211; \u4e0e sudo -i \u6700\u7ec8\u90fd\u80fd\u83b7\u5f97 root \u767b\u5f55\u73af\u5883&#xff0c;\u4f46 sudo -i \u66f4\u76f4\u63a5&#xff0c;\u53ea\u4ea7\u751f\u4e00\u5c42 sudo \u8fdb\u7a0b&#xff1b;<\/p>\n<p>sudo su &#8211; \u4f1a\u5148\u63d0\u6743\u518d\u8c03\u7528 su&#xff0c;\u5c5e\u4e8e\u5197\u4f59\u5199\u6cd5\u3002<\/p>\n<ul>\n<li>su \u7cfb\u5217\u9700\u8981\u77e5\u9053 \u76ee\u6807\u7528\u6237\u5bc6\u7801&#xff1b;sudo \u7cfb\u5217\u901a\u5e38\u9a8c\u8bc1 \u5f53\u524d\u7528\u6237\u5bc6\u7801&#xff0c;\u5e76\u4f9d\u8d56 \/etc\/sudoers \u6388\u6743\u3002<\/li>\n<li>sudo \u9ed8\u8ba4\u542f\u7528 env_reset&#xff0c;\u5e76\u901a\u8fc7 secure_path \u9650\u5236 PATH&#xff1b;\u8fd9\u662f sudo cmd \u627e\u4e0d\u5230\u81ea\u5b9a\u4e49\u547d\u4ee4\u7684\u5e38\u89c1\u539f\u56e0\u3002<\/li>\n<li>\u751f\u4ea7\u73af\u5883\u5efa\u8bae\u4f18\u5148\u4f7f\u7528 sudo \/ sudo -i&#xff0c;\u907f\u514d\u76f4\u63a5\u4f7f\u7528 su \u6216 sudo su -&#xff0c;\u4fbf\u4e8e\u5ba1\u8ba1\u548c\u6743\u9650\u63a7\u5236\u3002<\/li>\n<\/ul>\n<p>\u9000\u51fa\u547d\u4ee4\u7edf\u4e00\u4e3a&#xff1a;<\/p>\n<p><span class=\"token builtin class-name\">exit<\/span><\/p>\n<h4>8.3 PATH \u95ee\u9898\u5904\u7406\u65b9\u6848&#xff08;\u4fb5\u5165\u6027\u5347\u5e8f&#xff09;<\/h4>\n<li>\u5355\u6b21\u4f20\u9012 PATH<\/li>\n<p><span class=\"token function\">sudo<\/span> <span class=\"token function\">env<\/span> <span class=\"token assign-left variable\"><span class=\"token environment constant\">PATH<\/span><\/span><span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;<span class=\"token environment constant\">$PATH<\/span>&#034;<\/span> my_command<br \/>\n<span class=\"token function\">sudo<\/span> <span class=\"token function\">env<\/span> <span class=\"token assign-left variable\"><span class=\"token environment constant\">PATH<\/span><\/span><span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;<span class=\"token environment constant\">$PATH<\/span>&#034;<\/span> <span class=\"token function\">env<\/span> <span class=\"token operator\">|<\/span> <span class=\"token function\">grep<\/span> ^<span class=\"token environment constant\">PATH<\/span><\/p>\n<li>\u8c03\u7528\u7edd\u5bf9\u8def\u5f84<\/li>\n<p><span class=\"token function\">sudo<\/span> <span class=\"token variable\"><span class=\"token variable\">$(<\/span><span class=\"token function\">which<\/span> my_command<span class=\"token variable\">)<\/span><\/span><\/p>\n<li>\u8f6f\u94fe\u63a5\u81f3 secure_path \u76ee\u5f55<\/li>\n<p><span class=\"token function\">sudo<\/span> <span class=\"token function\">ln<\/span> <span class=\"token parameter variable\">-s<\/span> \/usr\/local\/bin\/cmd \/usr\/sbin\/cmd<\/p>\n<li>\u4fee\u6539 secure_path&#xff08;\u5168\u5c40&#xff09;<\/li>\n<p>Defaults  secure_path &#061; \/sbin:\/bin:\/usr\/sbin:\/usr\/bin:\/usr\/local\/bin<\/p>\n<p><span class=\"token builtin class-name\">echo<\/span> <span class=\"token string\">&#039;Defaults secure_path&#061;&#034;\/sbin:\/bin:\/usr\/sbin:\/usr\/bin:\/usr\/local\/bin&#034;&#039;<\/span> <span class=\"token operator\">|<\/span> <span class=\"token function\">sudo<\/span> <span class=\"token function\">tee<\/span> \/etc\/sudoers.d\/custom_path<\/p>\n<li>sudo -i&#xff0c;\u4f7f\u7528 root \u767b\u5f55\u73af\u5883<\/li>\n<li>env_keep \u4fdd\u7559 PATH&#xff08;\u5b58\u5728\u52ab\u6301\u98ce\u9669&#xff0c;\u4e0d\u63a8\u8350\u5e38\u89c4\u4f7f\u7528&#xff09;<\/li>\n<p>Defaults env_keep &#043;&#061; &#034;PATH&#034;<\/p>\n<li>shell \u522b\u540d<\/li>\n<p><span class=\"token builtin class-name\">alias<\/span> <span class=\"token assign-left variable\">psudo<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#039;sudo env PATH&#061;&#034;$PATH&#034;&#039;<\/span><br \/>\npsudo my_command<\/p>\n<h4>8.4 PATH \u52ab\u6301\u539f\u7406<\/h4>\n<p>\u7528\u6237\u53ef\u63a7 PATH \u4e2d\u5305\u542b\u53ef\u5199\u76ee\u5f55\u65f6&#xff0c;\u540c\u540d\u6076\u610f\u7a0b\u5e8f\u4f1a\u4f18\u5148\u5339\u914d\u5e76\u4ee5 root \u6743\u9650\u8fd0\u884c\u3002secure_path \u4ec5\u4f7f\u7528\u7ba1\u7406\u5458\u7ba1\u63a7\u76ee\u5f55&#xff0c;\u963b\u65ad\u8be5\u94fe\u8def\u3002<\/p>\n<h4>8.5 \u6392\u67e5\u9879<\/h4>\n<ul>\n<li>sudo -V | grep PATH \u67e5\u770b secure_path<\/li>\n<li>\u533a\u5206\u4e24\u7c7b\u95ee\u9898&#xff1a;sudo \u7a0b\u5e8f\u68c0\u7d22\u5931\u8d25\u3001\u7a0b\u5e8f\u5185\u90e8\u5b50\u8fdb\u7a0b PATH \u7f3a\u5931<\/li>\n<li>\u4f18\u5148\u4f7f\u7528\u5355\u6b21 env \u4f20\u9012\u6216\u7edd\u5bf9\u8def\u5f84&#xff0c;\u51cf\u5c11\u5168\u5c40\u53d8\u66f4<\/li>\n<\/ul>\n<p>\u5982\u679c\u4f60\u9700\u8981&#xff0c;\u6211\u53ef\u4ee5\u5355\u72ec\u5bfc\u51fa\u4e00\u4efd\u53ef\u76f4\u63a5\u6253\u5370\u7684\u7cbe\u7b80\u901f\u67e5\u7248&#xff08;\u53ea\u4fdd\u7559\u8868\u683c\u3001\u5e38\u7528\u547d\u4ee4\u3001\u6545\u969c\u6e05\u5355&#xff09;\u3002<\/p>\n<hr \/>\n<h2>Reference<\/h2>\n<ul>\n<li>command line &#8211; Environment variables when run with \u2018sudo\u2019 &#8211; Ask Ubuntu https:\/\/askubuntu.com\/questions\/57915\/environment-variables-when-run-with-sudo<\/li>\n<li>linux &#8211; Why are PATH variables different when running via sudo and su? &#8211; Unix &amp; Linux Stack Exchange https:\/\/unix.stackexchange.com\/questions\/8646\/why-are-path-variables-different-when-running-via-sudo-and-su<\/li>\n<li>environment variables &#8211; Why does sudo change the PATH? &#8211; Stack Overflow https:\/\/stackoverflow.com\/questions\/257616\/why-does-sudo-change-the-path<\/li>\n<li>How to make sudo preserve $PATH? &#8211; Unix &amp; Linux Stack Exchange https:\/\/unix.stackexchange.com\/questions\/83191\/how-to-make-sudo-preserve-path<\/li>\n<li>linux &#8211; Why are PATH variables different when running via sudo and su? &#8211; Unix &amp; Linux Stack Exchange https:\/\/unix.stackexchange.com\/questions\/8646\/why-are-path-variables-different-when-running-via-sudo-and-su<\/li>\n<li>security &#8211; Which is the safest way to get root privileges: sudo, su or login? &#8211; Unix &amp; Linux Stack Exchange https:\/\/unix.stackexchange.com\/questions\/8581\/which-is-the-safest-way-to-get-root-privileges-sudo-su-or-login<\/li>\n<li>sudo \u547d\u4ee4\u5b8c\u5168\u6307\u5357&#xff1a;\u4ece\u5165\u95e8\u5230\u7cbe\u901a https:\/\/mp.weixin.qq.com\/s\/CbTSZvrDQzDiBrc6wE_2Yg<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>sudo \u4e0e su \u64cd\u4f5c\u624b\u518c<br \/>\n\u76ee\u5f55<br \/>\nsudoers \u914d\u7f6esudo \u5e38\u7528\u64cd\u4f5csudo \u5b89\u5168\u673a\u5236sudo \u9ad8\u7ea7\u7528\u6cd5\u5b89\u5168\u89c4\u8303\u6545\u969c\u6392\u67e5\u540c\u7c7b\u5de5\u5177\u5bf9\u6bd4su\u3001sudo \u73af\u5883\u4e0e PATH \u884c\u4e3a 1 sudoers \u914d\u7f6e<br \/>\nsudo \u7684\u6743\u9650\u63a7\u5236\u4f9d\u6258 \/etc\/sudoers&#xff0c;\u6587\u4ef6\u9ed8\u8ba4\u6743\u9650 440\u3002<br \/>\n\u4e0d\u76f4\u63a5\u4f7f\u7528 vi\u3001vim \u7f16\u8f91&#xff0c;\u8bed\u6cd5\u9519\u8bef\u4f1a\u9020\u6210 sudo \u4e0d\u53ef\u7528&#xff1b;\u7f16\u8f91\u7edf\u4e00\u4f7f\u7528 visudo&#xff0c;\u8be5\u5de5\u5177\u5185\u7f6e\u8bed\u6cd5\u6821\u9a8c\u3002<br \/>\n# \u666e\u901a\u7528\u6237\u6267\u884c&amp;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[14048],"topic":[],"class_list":["post-122962","post","type-post","status-publish","format-standard","hentry","category-server","tag-sudo--su-"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>LInux | sudo \u4e0e su \u64cd\u4f5c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/122962.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"LInux | sudo \u4e0e su \u64cd\u4f5c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"sudo \u4e0e su \u64cd\u4f5c\u624b\u518c \u76ee\u5f55 sudoers \u914d\u7f6esudo \u5e38\u7528\u64cd\u4f5csudo \u5b89\u5168\u673a\u5236sudo \u9ad8\u7ea7\u7528\u6cd5\u5b89\u5168\u89c4\u8303\u6545\u969c\u6392\u67e5\u540c\u7c7b\u5de5\u5177\u5bf9\u6bd4su\u3001sudo \u73af\u5883\u4e0e PATH \u884c\u4e3a 1 sudoers \u914d\u7f6e sudo \u7684\u6743\u9650\u63a7\u5236\u4f9d\u6258 \/etc\/sudoers&#xff0c;\u6587\u4ef6\u9ed8\u8ba4\u6743\u9650 440\u3002 \u4e0d\u76f4\u63a5\u4f7f\u7528 vi\u3001vim \u7f16\u8f91&#xff0c;\u8bed\u6cd5\u9519\u8bef\u4f1a\u9020\u6210 sudo \u4e0d\u53ef\u7528&#xff1b;\u7f16\u8f91\u7edf\u4e00\u4f7f\u7528 visudo&#xff0c;\u8be5\u5de5\u5177\u5185\u7f6e\u8bed\u6cd5\u6821\u9a8c\u3002 # \u666e\u901a\u7528\u6237\u6267\u884c&amp;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/122962.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-11-05T19:57:36+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/122962.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/122962.html\",\"name\":\"LInux | sudo \u4e0e su \u64cd\u4f5c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-11-05T19:57:36+00:00\",\"dateModified\":\"2026-11-05T19:57:36+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/122962.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/122962.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/122962.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"LInux | sudo \u4e0e su \u64cd\u4f5c\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"LInux | sudo \u4e0e su \u64cd\u4f5c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/122962.html","og_locale":"zh_CN","og_type":"article","og_title":"LInux | sudo \u4e0e su \u64cd\u4f5c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"sudo \u4e0e su \u64cd\u4f5c\u624b\u518c \u76ee\u5f55 sudoers \u914d\u7f6esudo \u5e38\u7528\u64cd\u4f5csudo \u5b89\u5168\u673a\u5236sudo \u9ad8\u7ea7\u7528\u6cd5\u5b89\u5168\u89c4\u8303\u6545\u969c\u6392\u67e5\u540c\u7c7b\u5de5\u5177\u5bf9\u6bd4su\u3001sudo \u73af\u5883\u4e0e PATH \u884c\u4e3a 1 sudoers \u914d\u7f6e sudo \u7684\u6743\u9650\u63a7\u5236\u4f9d\u6258 \/etc\/sudoers&#xff0c;\u6587\u4ef6\u9ed8\u8ba4\u6743\u9650 440\u3002 \u4e0d\u76f4\u63a5\u4f7f\u7528 vi\u3001vim \u7f16\u8f91&#xff0c;\u8bed\u6cd5\u9519\u8bef\u4f1a\u9020\u6210 sudo \u4e0d\u53ef\u7528&#xff1b;\u7f16\u8f91\u7edf\u4e00\u4f7f\u7528 visudo&#xff0c;\u8be5\u5de5\u5177\u5185\u7f6e\u8bed\u6cd5\u6821\u9a8c\u3002 # \u666e\u901a\u7528\u6237\u6267\u884c&amp;","og_url":"https:\/\/www.wsisp.com\/helps\/122962.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-11-05T19:57:36+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"9 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/122962.html","url":"https:\/\/www.wsisp.com\/helps\/122962.html","name":"LInux | sudo \u4e0e su \u64cd\u4f5c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-11-05T19:57:36+00:00","dateModified":"2026-11-05T19:57:36+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/122962.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/122962.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/122962.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"LInux | sudo \u4e0e su \u64cd\u4f5c"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/122962","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=122962"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/122962\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=122962"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=122962"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=122962"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=122962"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}