¡°sudo¡±ÊÇUnix/Linuxƽ̨ÉϵÄÒ»¸ö·Ç³£ÓÐÓõŤ¾ß£¬ËüÔÊÐíϵͳ¹ÜÀíÔ±·ÖÅä¸øÆÕͨÓû§Ò»Ð©ºÏÀíµÄ¡°È¨Àû¡±£¬ÈÃËûÃÇÖ´ÐÐһЩֻÓг¬¼¶Óû§»òÆäËûÌØÐíÓû§²ÅÄÜÍê³ÉµÄÈÎÎñ£¬±ÈÈ磺ÔËÐÐһЩÏñmount£¬halt£¬suÖ®ÀàµÄÃüÁ»òÕß±à¼Ò»Ð©ÏµÍ³ÅäÖÃÎļþ£¬Ïñ/etc/mtab£¬/etc/samba/smb.confµÈ¡£ÕâÑùÒÔÀ´£¬¾Í²»½ö¼õÉÙÁËrootÓû§µÄµÇ½´ÎÊýºÍ¹ÜÀíʱ¼ä£¬Ò²Ìá¸ßÁËϵͳ°²È«ÐÔ¡£
Ò». sudoµÄÌصã
sudo°çÑݵĽÇɫע¶¨ÁËËüÒªÔÚ°²È«·½Ãæ¸ñÍâ½÷É÷£¬·ñÔò¾Í»áµ¼Ö·Ƿ¨Óû§¾ðÈ¡ rootȨÏÞ¡£Í¬Ê±£¬Ëü»¹Òª¼æ¹ËÒ×ÓÃÐÔ£¬ÈÃϵͳ¹ÜÀíÔ±Äܹ»¸üÓÐЧ£¬¸ü·½±ãµØʹÓÃËü¡£sudoÉè¼ÆÕßµÄ×ÚÖ¼ÊÇ£º¸øÓû§¾¡¿ÉÄÜÉÙµÄȨÏÞµ«ÈÔÔÊÐíÍê³ÉËûÃǵŤ×÷¡£ËùÒÔ£¬sudoÓÐÒÔÏÂÌص㣺
1. sudoÄܹ»ÏÞÖÆÖ¸¶¨Óû§ÔÚÖ¸¶¨Ö÷»úÉÏÔËÐÐijЩÃüÁî¡£
2. sudo¿ÉÒÔÌṩÈÕÖ¾£¬ÖÒʵµØ¼Ç¼ÿ¸öÓû§Ê¹ÓÃsudo×öÁËЩʲô£¬²¢ÇÒÄܽ«ÈÕÖ¾´«µ½ÖÐÐÄÖ÷»ú»òÕßÈÕÖ¾·þÎñÆ÷¡£
3. sudoΪϵͳ¹ÜÀíÔ±ÌṩÅäÖÃÎļþ£¬ÔÊÐíϵͳ¹ÜÀíÔ±¼¯ÖеعÜÀíÓû§µÄʹÓÃȨÏÞºÍʹÓõÄÖ÷»ú¡£ËüĬÈϵĴæ·ÅλÖÃÊÇ/etc/sudoers¡£
4. sudoʹÓÃʱ¼ä´ÁÎļþÀ´Íê³ÉÀàËÆ¡°¼ìƱ¡±µÄϵͳ¡£µ±Óû§Ö´ÐÐsudo²¢ÇÒÊäÈëÃÜÂëºó£¬Óû§»ñµÃÁËÒ»ÕÅĬÈÏ´æ»îÆÚΪ5·ÖÖӵġ°È볡ȯ¡±£¨Ä¬ÈÏÖµ¿ÉÒÔÔÚ±àÒëµÄʱºò¸Ä±ä£©¡£³¬Ê±ÒÔºó£¬Óû§±ØÐëÖØÐÂÊäÈëÃÜÂë¡£
¶þ. sudoÃüÁî
sudo³ÌÐò±¾Éí¾ÍÊÇÒ»¸öÉèÖÃÁËSUIDλµÄ¶þ½øÖÆÎļþ¡£ÎÒÃÇ¿ÉÒÔ¼ì²éÒ»ÏÂËüµÄȨÏÞ£º
$ls -l /usr/bin/sudo
---s--x--x 2 root root 106832 02-12 17:41 /usr/bin/sudo
ËüµÄËùÓÐÕßÊÇroot£¬ËùÒÔÿ¸öÓû§¶¼¿ÉÒÔÏñrootÄÇÑùÖ´ÐиóÌÐò¡£ÉèÖÃÁËSUIDµÄ³ÌÐòÔÚÔËÐÐʱ¿ÉÒÔ¸øʹÓÃÕßÒÔËùÓÐÕßµÄEUID¡£ÕâÒ²ÊÇΪʲôÉèÖÃÁËSUIDµÄ³ÌÐò±ØÐëСÐıàд¡£µ«ÊÇÉèÖÃÒ»¸öÃüÁîÎļþµÄSUIDºÍÓÃsudoÀ´ÔËÐÐËüÊDz»Í¬µÄ¸ÅÄËüÃÇÆð×Ų»Í¬µÄ×÷Óá£
sudoµÄÅäÖö¼¼Ç¼ÔÚ/etc/sudoersÎļþÖУ¬ÎÒÃÇÏÂÃ潫»áÏêϸ˵Ã÷¡£ÅäÖÃÎļþÖ¸Ã÷ÄÄЩÓû§¿ÉÒÔÖ´ÐÐÄÄЩÃüÁҪʹÓÃsudo£¬Óû§±ØÐëÌṩһ¸öÖ¸¶¨Óû§ÃûºÍÃÜÂë¡£×¢Ò⣺sudoÐèÒªµÄ²»ÊÇÄ¿±êÓû§µÄÃÜÂ룬¶øÊÇÖ´ÐÐsudoµÄÓû§µÄÃÜÂë¡£Èç¹û²»ÔÚsudoersÖеÄÓû§Í¨¹ýsudoÖ´ÐÐÃüÁsudo»áÏò¹ÜÀíÔ±±¨¸æÕâһʼþ¡£Óû§¿ÉÒÔͨ¹ýsudo -vÀ´²é¿´×Ô¼ºÊÇ·ñÊÇÔÚsudoers Ö®ÖС£Èç¹ûÊÇ£¬Ëü»¹¿ÉÒÔ¸üÐÂÄãµÄ¡°È볡ȯ¡±ÉϵÄʱ¼ä£»Èç¹û²»ÊÇ£¬Ëü»áÌáʾÄ㣬µ«²»»á֪ͨ¹ÜÀíÔ±¡£
sudoÃüÁî¸ñʽÈçÏ£º
sudo -K | -L | -V | -h | -k | -l | -v
sudo [-HPSb] [-a auth_type] [-c class|-] [-p prompt] [-u username|#uid] {-e file [...] | -i | -s | command}
ÏÂÃæÎÒÃÇÔÙÀ´¿´Ò»ÏÂsudoÆäËü³£ÓõÄһЩ²ÎÊý£º
[table=85%][tr][td=1,1,30%]Ñ¡Ïî[/td][td=1,1,20%]º¬Òå[/td][td]×÷ÓÃ[/td][/tr][tr][td]sudo -h[/td][td]Help[/td][td]ÁгöʹÓ÷½·¨£¬Í˳ö¡£[/td][/tr][tr][td]sudo -V[/td][td]Version[/td][td]ÏÔʾ°æ±¾ÐÅÏ¢£¬²¢Í˳ö¡£[/td][/tr][tr][td]sudo -l[/td][td]List[/td][td]Áгöµ±Ç°Óû§¿ÉÒÔÖ´ÐеÄÃüÁî¡£Ö»ÓÐÔÚsudoersÀïµÄÓû§²ÅÄÜʹÓøÃÑ¡Ïî¡£[/td][/tr][tr][td]sudo -u username|#uid[/td][td]User[/td][td]ÒÔÖ¸¶¨Óû§µÄÉí·ÝÖ´ÐÐÃüÁî¡£ºóÃæµÄÓû§ÊdzýrootÒÔÍâµÄ£¬¿ÉÒÔÊÇÓû§Ãû£¬Ò²¿ÉÒÔÊÇ#uid¡£[/td][/tr][tr][td]sudo -k[/td][td]Kill[/td][td]Çå³ý¡°È볡¾í¡±ÉϵÄʱ¼ä£¬Ï´ÎÔÙʹÓÃsudoʱҪÔÙÊäÈëÃÜÂë¡£[/td][/tr][tr][td]sudo -K[/td][td]Sure kill[/td][td]Óë-kÀàËÆ£¬µ«ÊÇËü»¹ÒªËº»Ù¡°È볡¾í¡±£¬Ò²¾ÍÊÇɾ³ýʱ¼ä´ÁÎļþ¡£[/td][/tr][tr][td]sudo -b command[/td][td]Background[/td][td]ÔÚºǫִ́ÐÐÖ¸¶¨µÄÃüÁî¡£[/td][/tr][tr][td]sudo -p prompt command[/td][td]Prompt[/td][td]¿ÉÒÔ¸ü¸ÄѯÎÊÃÜÂëµÄÌáʾÓÆäÖÐ%u»á´ú»»ÎªÊ¹ÓÃÕßÕʺÅÃû³Æ£¬%h»áÏÔʾÖ÷»úÃû³Æ¡£·Ç³£ÈËÐÔ»¯µÄÉè¼Æ¡£[/td][/tr][tr][td]sudo -e file[/td][td]Edit[/td][td]²»ÊÇÖ´ÐÐÃüÁ¶øÊÇÐÞ¸ÄÎļþ£¬Ï൱ÓÚÃüÁîsudoedit¡£[/td][/tr][/table]
»¹ÓÐһЩ²»³£ÓõIJÎÊý£¬ÔÚÊÖ²áÒ³sudo(8)ÖпÉÒÔÕÒµ½¡£
Èý. ÅäÖÃsudo
ÅäÖÃsudo±ØÐëͨ¹ý±à¼/etc/sudoersÎļþ£¬¶øÇÒÖ»Óг¬¼¶Óû§²Å¿ÉÒÔÐÞ¸ÄËü£¬»¹±ØÐëʹÓÃvisudo±à¼¡£Ö®ËùÒÔʹÓÃvisudoÓÐÁ½¸öÔÒò£¬Ò»ÊÇËüÄܹ»·ÀÖ¹Á½¸öÓû§Í¬Ê±ÐÞ¸ÄËü£»¶þÊÇËüÒ²ÄܽøÐÐÓÐÏÞµÄÓï·¨¼ì²é¡£ËùÒÔ£¬¼´Ê¹Ö»ÓÐÄãÒ»¸ö³¬¼¶Óû§£¬ÄãÒ²×îºÃÓÃvisudoÀ´¼ì²éÒ»ÏÂÓï·¨¡£
visudoĬÈϵÄÊÇÔÚviÀï´ò¿ªÅäÖÃÎļþ£¬ÓÃviÀ´ÐÞ¸ÄÎļþ¡£ÎÒÃÇ¿ÉÒÔÔÚ±àÒëʱÐÞ¸ÄÕâ¸öĬÈÏÏî¡£visudo²»»áÉÃ×Ô±£´æ´øÓÐÓï·¨´íÎóµÄÅäÖÃÎļþ£¬Ëü»áÌáʾÄã³öÏÖµÄÎÊÌ⣬²¢Ñ¯ÎʸÃÈçºÎ´¦Àí£¬¾ÍÏñ£º
>>> sudoers file: syntax error, line 22 <<<
What now? e
´ËʱÎÒÃÇÓÐÈýÖÖÑ¡Ôñ£º¼üÈë¡°e¡±ÊÇÖØб༣¬¼üÈë¡°x¡±ÊDz»±£´æÍ˳ö£¬¼üÈë¡°Q¡±ÊÇÍ˳ö²¢±£´æ¡£Èç¹ûÕæÑ¡ÔñQ£¬ÄÇôsudo½«²»»áÔÙÔËÐУ¬Ö±µ½´íÎó±»¾ÀÕý¡£
ÏÖÔÚ£¬ÎÒÃÇÒ»ÆðÀ´¿´Ò»ÏÂÉñÃصÄÅäÖÃÎļþ£¬Ñ§Ò»ÏÂÈçºÎ±àдËü¡£ÈÃÎÒÃÇ´ÓÒ»¸ö¼òµ¥µÄÀý×Ó¿ªÊ¼£ºÈÃÓû§foobar¿ÉÒÔͨ¹ýsudoÖ´ÐÐËùÓÐroot¿ÉÖ´ÐеÄÃüÁî¡£ÒÔrootÉí·ÝÓÃvisudo´ò¿ªÅäÖÃÎļþ£¬¿ÉÒÔ¿´µ½ÀàËÆÏÂÃ漸ÐУº
# Runas alias specification
# User privilege specification
root ALL=(ALL) ALL
ÎÒÃÇÒ»¿´¾ÍÃ÷°×¸ö²î²»¶àÁË£¬rootÓÐËùÓÐȨÏÞ£¬Ö»Òª·ÂÕÕÏÖÓÐrootµÄÀý×Ó¾ÍÐУ¬ÎÒÃÇÔÚÏÂÃæ¼ÓÒ»ÐУ¨×îºÃÓÃtab×÷Ϊ¿Õ°×£©£º
foobar ALL=(ALL) ALL
±£´æÍ˳öºó£¬Çл»µ½foobarÓû§£¬ÎÒÃÇÓÃËüµÄÉí·ÝÖ´ÐÐÃüÁ
[foobar@localhost ~]$ ls /root
ls: /root: ȨÏÞ²»¹»
[foobar@localhost ~]$ sudo ls /root
Password:
anaconda-ks.cfg Desktop install.log install.log.syslog
ºÃÁË£¬ÎÒÃÇÏÞÖÆÒ»ÏÂfoobarµÄȨÀû£¬²»ÈÃËûΪËùÓûΪ¡£±ÈÈçÎÒÃÇÖ»ÏëÈÃËûÏñrootÄÇÑùʹÓÃlsºÍifconfig£¬°ÑÄÇÒ»ÐиÄΪ£º
foobar localhost= /sbin/ifconfig, /bin/ls
ÔÙÀ´Ö´ÐÐÃüÁ
[foobar@localhost ~]$ sudo head -5 /etc/shadow
Password:
Sorry, user foobar is not allowed to execute '/usr/bin/head -5 /etc/shadow' as root on localhost.localdomain.
[foobar@localhost ~]$ sudo /sbin/ifconfig
eth0 Link encap:Ethernet HWaddr 00:14:85:EC:E9:9B
...
ÏÖÔÚÈÃÎÒÃÇÀ´¿´Ò»ÏÂÄÇÈý¸öALLµ½µ×ÊÇʲôÒâ˼¡£µÚÒ»¸öALLÊÇÖ¸ÍøÂçÖеÄÖ÷»ú£¬ÎÒÃǺóÃæ°ÑËü¸Ä³ÉÁËÖ÷»úÃû£¬ËüÖ¸Ã÷ foobar¿ÉÒÔÔÚ´ËÖ÷»úÉÏÖ´ÐкóÃæµÄÃüÁî¡£µÚ¶þ¸öÀ¨ºÅÀïµÄALLÊÇָĿ±êÓû§£¬Ò²¾ÍÊÇÒÔ˵ÄÉí·ÝÈ¥Ö´ÐÐÃüÁî¡£×îºóÒ»¸ö ALLµ±È»¾ÍÊÇÖ¸ÃüÁîÃûÁË¡£ÀýÈ磬ÎÒÃÇÏëÈÃfoobarÓû§ÔÚlinuxÖ÷»úÉÏÒÔjimmy»òreneµÄÉí·ÝÖ´ÐÐkillÃüÁÕâÑù±àдÅäÖÃÎļþ£º
foobar linux=(jimmy,rene) /bin/kill
µ«Õ⻹ÓиöÎÊÌ⣬foobarµ½µ×ÒÔjimmy»¹ÊÇreneµÄÉí·ÝÖ´ÐУ¿ÕâʱÎÒÃÇÓ¦¸ÃÏëµ½ÁËsudo -uÁË£¬ËüÕýÊÇÓÃÔÚÕâÖÖʱºò¡£ foobar¿ÉÒÔʹÓÃsudo -u jimmy kill PID»òÕßsudo -u rene kill PID£¬µ«ÕâÑùͦÂé·³£¬ÆäʵÎÒÃÇ¿ÉÒÔ²»±Øÿ´Î¼Ó-u£¬°Ñrene»òjimmyÉèΪĬÈϵÄÄ¿±êÓû§¼´¿É¡£ÔÙÔÚÉÏÃæ¼ÓÒ»ÐУº
Defaults:foobar runas_default=rene
DefaultsºóÃæÈç¹ûÓÐðºÅ£¬ÊǶԺóÃæÓû§µÄĬÈÏ£¬Èç¹ûûÓУ¬ÔòÊǶÔËùÓÐÓû§µÄĬÈÏ¡£¾ÍÏñÅäÖÃÎļþÖÐ×Ô´øµÄÒ»ÐУº
Defaults env_reset
ÁíÒ»¸öÎÊÌâÊÇ£¬ºÜ¶àʱºò£¬ÎÒÃDZ¾À´¾ÍµÇ¼ÁË£¬Ã¿´ÎʹÓÃsudo»¹ÒªÊäÈëÃÜÂë¾ÍÏԵ÷³ËöÁË¡£ÎÒÃǿɲ»¿ÉÒÔ²»ÔÙÊäÈëÃÜÂëÄØ£¿µ±È»¿ÉÒÔ£¬ÎÒÃÇÕâÑùÐÞ¸ÄÅäÖÃÎļþ£º
foobar localhost=NOPASSWD: /bin/cat, /bin/ls
ÔÙÀ´sudoһϣº
[foobar@localhost ~]$ sudo ls /root
anaconda-ks.cfg Desktop install.log install.log.syslog
µ±È»£¬ÄãÒ²¿ÉÒÔ˵¡°Ä³Ð©ÃüÁîÓû§foobar²»¿ÉÒÔÔËÐС±£¬Í¨¹ýʹÓÃ!²Ù×÷·û£¬µ«Õâ²»ÊÇÒ»¸öºÃÖ÷Òâ¡£ÒòΪ£¬ÓÃ!²Ù×÷·ûÀ´´ÓALLÖС°ÌÞ³ö¡±Ò»Ð©ÃüÁîÒ»°ãÊÇûʲôЧ¹ûµÄ£¬Ò»¸öÓû§ÍêÈ«¿ÉÒÔ°ÑÄǸöÃüÁ±´µ½±ðµÄµØ·½£¬»»Ò»¸öÃû×ÖºóÔÙÀ´ÔËÐС£
ËÄ. ÈÕÖ¾Ó밲ȫ
sudoΪ°²È«¿¼ÂǵúÜÖܵ½£¬²»½ö¿ÉÒԼǼÈÕÖ¾£¬»¹ÄÜÔÚÓбØҪʱÏòϵͳ¹ÜÀíÔ±±¨¸æ¡£µ«ÊÇ£¬sudoµÄÈÕÖ¾¹¦Äܲ»ÊÇ×Ô¶¯µÄ£¬±ØÐëÓɹÜÀíÔ±¿ªÆô¡£ÕâÑùÀ´×ö£º
# touch /var/log/sudo
# vi /etc/syslog.conf
ÔÚsyslog.conf×îºóÃæ¼ÓÒ»ÐУ¨±ØÐëÓÃtab·Ö¸î¿ª£©²¢±£´æ£º
local2.debug /var/log/sudo
ÖØÆôÈÕÖ¾Êغò½ø³Ì
ps aux | grep syslogd
°ÑµÃµ½µÄsyslogd½ø³ÌµÄPID£¨Êä³öµÄµÚ¶þÁÐÊÇPID£©ÌîÈëÏÂÃ棺
kill -HUP PID
ÕâÑù£¬sudo¾Í¿ÉÒÔдÈÕÖ¾ÁË£º
[foobar@localhost ~]$ sudo ls /root
anaconda-ks.cfg Desktop install.log install.log.syslog
$cat /var/log/sudo
Jul 28 22:52:54 localhost sudo: foobar : TTY=pts/1 ; PWD=/home/foobar ; USER=root ; COMMAND=/bin/ls /root
²»¹ý£¬ÓÐÒ»¸öССµÄ¡°È±ÏÝ¡±£¬sudo¼Ç¼ÈÕÖ¾²¢²»ÊǺÜÖÒʵ£º
[foobar@localhost ~]$ sudo cat /etc/shadow > /dev/null
[foobar@localhost ~]$ cat /var/log/sudo
...
Jul 28 23:10:24 localhost sudo: foobar : TTY=pts/1 ; PWD=/home/foobar ; USER=root ; COMMAND=/bin/cat /etc/shadow
Öض¨ÏòûÓб»¼Ç¼ÔÚ°¸£¡ÎªÊ²Ã´£¿ÒòΪÔÚÃüÁîÔËÐÐ֮ǰ£¬shell°ÑÖض¨ÏòµÄ¹¤×÷×öÍêÁË£¬sudo¸ù±¾¾Íû¿´µ½Öض¨Ïò¡£ÕâÒ²ÓиöºÃ´¦£¬ÏÂÃæµÄÊֶβ»»áµÃ³Ñ£º
[foobar@localhost ~]$ sudo ls /root > /etc/shadow
bash: /etc/shadow: ȨÏÞ²»¹»
sudoÓÐ×Ô¼ºµÄ·½Ê½À´±£»¤°²È«¡£ÒÔrootµÄÉí·ÝÖ´ÐÐsudo -V£¬²é¿´Ò»ÏÂsudoµÄÉèÖá£ÒòΪ¿¼Âǵ½°²È«ÎÊÌ⣬һ²¿·Ö»·¾³±äÁ¿²¢Ã»Óд«µÝ¸øsudoºóÃæµÄÃüÁ»òÕß±»¼ì²éºóÔÙ´«µÝµÄ£¬±ÈÈ磺PATH£¬HOME£¬SHELLµÈ¡£µ±È»£¬ÄãÒ²¿ÉÒÔͨ¹ýsudoersÀ´ÅäÖÃÕâЩ»·¾³±äÁ¿¡£
ÈçÉÏËù¼û£¬sudo¶ÔÓÚ¿ØÖƺÍÉó²érootµÄ·ÃÎʺÜÓаïÖú£¬ËüÄÜÈÃϵͳ¹ÜÀíÔ±¸üÓÐЧ£¬°²È«µØ¹ÜÀíϵͳ¡£ÕÆÎÕsudoµÄÕýȷʹÓÃÒ²ÊǶÔÓÚϵͳ¹ÜÀíÔ±µÄÁ¼ºÃѵÁ·¡£
height ÓÚ 2008-10-23 15:59:14·¢±í:
ѧϰÁË£¬¸Õ¿ªÊ¼£¬¾õµÃºÃÂé·³£¬Ö»ºÃÂýÂýÀ´ÁË
xtay573269555 ÓÚ 2008-10-23 14:58:40·¢±í:
(q):s (q):s (q):s
Çå½ÆÖ ÓÚ 2008-09-26 14:19:12·¢±í:
LinuxÊǸöºÜ´óµÄѧÎÊ°¢ ÂýÂýѧϰºÍÌå»á
huahui ÓÚ 2008-09-24 21:22:20·¢±í:
ÖÕÓÚÕÒµ½ÁËsudoµÄÃüÁлл¥Ö÷¡£
simba_cn520 ÓÚ 2008-09-24 20:44:30·¢±í:
¿´Ã÷°×Á˵㣬лл
cqtnt ÓÚ 2008-09-22 11:12:30·¢±í:
лл£¬ÕæµÄºÜʵÓá£
zhgagen ÓÚ 2008-09-15 17:01:41·¢±í:
ÕæÊÇÓÐÐÄÈË£¬Åå·þ£¡
menyuqi ÓÚ 2008-09-05 11:26:11·¢±í:
½ñÌìÓÖ¶àѧÁËÒ»µã¡£
lzj65166 ÓÚ 2008-08-29 10:30:54·¢±í:
лл,½ñÌìÓÖÈÃÎÒÃ÷°×ÁËlinux£¬¸üÉîÈëµÄ°®ÉÏÁËËü¡£ºÇºÇ.
icestone ÓÚ 2008-08-28 10:29:19·¢±í:
²»´í£¬ÕýÔÚŬÁ¦Ñ§Ï°Linux£¬Ï£ÍûÔçÈÕÓзݲ»´íµÄнˮ£¬Ñø¼Òºý¿Ú
Ïà˼°®ÎÄ ÓÚ 2008-08-27 10:08:34·¢±í:
ºÜÏêϸ£¬ÎÒÒÔÇ°Ò»Ö±¶¼ÊǼӸöusername ALL=(ALL) ALLµÄ