su 和 sudo,你用對了嗎?

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"之前一直對 su 和 sudo 這兩個命令犯迷糊,最近專門搜了這方面的資料,總算是把兩者的關係以及用法搞清楚了,這篇文章來系統總結一下。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"1. 準備工作","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因爲本篇博客中涉及到用戶切換,所以我需要提前準備好幾個測試用戶,方便後續切換。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Linux 中新建用戶的命令是 useradd ,一般系統中這個命令對應的路徑都在 PATH 環境變量裏,如果直接輸入 useradd 不管用的話,就用絕對路徑名的方式:/usr/sbin/useradd 。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"useradd 新建用戶命令只有 root 用戶才能執行,我們先從普通用戶 ubuntu 切換到 root 用戶(如何切換後文會介紹):","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"ubuntu@VM-0-14-ubuntu:~$ su -\nPassword: # 輸入 root 用戶登錄密碼\nroot@VM-0-14-ubuntu:~# useradd -m test_user # 帶上 -m 參數\nroot@VM-0-14-ubuntu:~# ls /home\ntest_user ubuntu # 可以看到 /home 目錄下面有兩個用戶了","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因爲還沒有給新建的用戶 test_user 設置登錄密碼,這就導致我們無法從普通用戶 ubuntu 切換到 test_user,所以接下來,我們需要用 root 來設置 test_user 的登錄密碼。需要用到 passwd 命令:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"root@VM-0-14-ubuntu:~# passwd test_user\nEnter new UNIX password: # 輸出 test_user 的密碼\nRetype new UNIX password: \npasswd: password updated successfully\nroot@VM-0-14-ubuntu:~#","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接着我們輸入 exit 退出 root 用戶到 普通用戶 ubuntu:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"root@VM-0-14-ubuntu:~# exit\nlogout\nubuntu@VM-0-14-ubuntu:~$","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以看到,命令提示符前面已經由 root 變成 ubuntu,說明我們現在的身份是 ubuntu 用戶。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"2. su 命令介紹及主要用法","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先需要解釋下 su 代表什麼意思。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"之前一直以爲 su 是 super user,查閱資料之後才知道原來表示 switch user。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"知道 su 是由什麼縮寫來的之後,那麼它提供的功能就顯而易見了,就是切換用戶。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"2.1 - 參數","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"su 的一般使用方法是:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"su ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"或者","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"su - ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"兩種方法只差了一個字符 -,會有比較大的差異:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果加入了 - 參數,那麼是一種 login-shell 的方式,意思是說切換到另一個用戶 之後,當前的 shell 會加載 對應的環境變量和各種設置;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果沒有加入 - 參數,那麼是一種 non-login-shell 的方式,意思是說我現在切換到了 ,但是當前的 shell 還是加載切換之前的那個用戶的環境變量以及各種設置。","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"光解釋會比較抽象,我們看一個例子就比較容易理解了。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們首先從 ubuntu 用戶以 non-login-shell 的方式切換到 root 用戶,比較兩種用戶狀態下環境變量中 PWD 的值(su 命令不跟任何 ,默認切換到 root 用戶):","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"ubuntu@VM-0-14-ubuntu:~$ env | grep ubuntu\nUSER=ubuntuPWD=/home/ubuntu # 是 /home/ubuntu\nHOME=/home/ubuntu\n# 省略......\nubuntu@VM-0-14-ubuntu:~$ su # non-login-shell 方式\nPassword: # 輸入 root 用戶登錄密碼\nroot@VM-0-14-ubuntu:/home/ubuntu# env | grep ubuntu\nPWD=/home/ubuntu # 可以發現還是 /home/ubuntu\nroot@VM-0-14-ubuntu:/home/ubuntu#","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們的確是切換到 root 用戶了,但是 shell 環境中的變量並沒有改變,還是用之前 ubuntu 用戶的環境變量。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接着我們從 ubuntu 用戶以 login-shell 的方式切換到 root 用戶,同樣比較兩種用戶轉檯下環境變量中 PWD 的值:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"ubuntu@VM-0-14-ubuntu:~$ env | grep ubuntu\nUSER=ubuntuPWD=/home/ubuntu # 是 /home/ubuntu\nHOME=/home/ubuntu\n# 省略.......\nubuntu@VM-0-14-ubuntu:~$ su - # 是 login-shell 方式\nPassword:\nroot@VM-0-14-ubuntu:~# env | grep root\nUSER=rootPWD=/root # 已經變成 /root 了\nHOME=/root\nMAIL=/var/mail/root\nLOGNAME=root\nroot@VM-0-14-ubuntu:~#","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以看到用 login-shell 的方式切換用戶的話,shell 中的環境變量也跟着改變了。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"總結:具體使用哪種方式切換用戶看個人需求:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果不想因爲切換到另一個用戶導致自己在當前用戶下的設置不可用,那麼用 non-login-shell 的方式;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果切換用戶後,需要用到該用戶的各種環境變量(不同用戶的環境變量設置一般是不同的),那麼使用 login-shell 的方式。","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"2.2 切換到指定用戶","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"前面已經介紹了,如果 su 命令後面不跟任何,那麼默認是切換到 root 用戶:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"ubuntu@VM-0-14-ubuntu:~$ su -\nPassword: # root 用戶的密碼\nroot@VM-0-14-ubuntu:/home/ubuntu#","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因爲我們在 1. 準備工作 部分已經新建了一個 test_user 用戶,並且我們也知道 test_user 用戶的登錄密碼(root 用戶設置的),我們就能從 ubuntu 用戶切換到 test_user 用戶:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"ubuntu@VM-0-14-ubuntu:~$ su - test_user\nPassword: # test_user 用戶的密碼$","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"2.3 -c 參數","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"前面的方法中,我們都是先切換到另一個用戶(root 或者 test_user),在哪個用戶的狀態下執行命令,最後輸入 exit 返回當前 ubuntu 用戶。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"還有一種方式是:不需要先切換用戶再執行命令,可以直接在當前用戶下,以另一個用戶的方式執行命令,執行結束後就返回當前用戶。這就得用到 -c 參數。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"另外,Linux 系列面試題和答案全部整理好了,微信搜索Java技術棧,在後臺發送:面試,可以在線閱讀。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"具體使用方法是:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"su - -c \"指令串\" # 以 root 的方式執行 \"指令串\"","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我麼看個例子:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"ubuntu@VM-0-14-ubuntu:~$ cat /etc/shadow\ncat: /etc/shadow: Permission denied # ubuntu 用戶不能直接查看 /etc/shadow 文件內容\nubuntu@VM-0-14-ubuntu:~$ su - -c \"tail -n 4 /etc/shadow\"\nPassword: # 輸入 root 用戶密碼\nubuntu:$1$fZKcWEDI$uwZ64uFvVbwpHTbCSgim0/:18352:0:99999:7:::\nntp:*:17752:0:99999:7:::\nmysql:!:18376:0:99999:7:::\ntest_user:$6$.ZY1lj4m$ii0x9CG8h.JHlh6zKbfBXRuolJmIDBHAd5eqhvW7lbUQXTRS//89jcuTzRilKqRkP8YbYW4VPxmTVHWRLYNGS/:18406:0:99999:7:::\nubuntu@VM-0-14-ubuntu:~$ # 執行完馬上返回 ubuntu 用戶而不是 root 用戶","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這種執行方式和後面要介紹的 sudo 很像,都是臨時申請一下 root 用戶的權限。但還是有差異,我們接着往後看。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3. sudo 命令介紹及主要用法","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先還是解釋下 sudo 命令是什麼意思。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"sudo 的英文全稱是 super user do,即以超級用戶(root 用戶)的方式執行命令。這裏的 sudo 和之前 su 表示的 switch user 是不同的,這點需要注意,很容易搞混。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們先介紹 sudo 命令能做什麼事情,然後說明爲何能做到這些,以及如何做到這些。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們開始。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"3.1 主要用法","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們在 Linux 中經常會碰到 Permission denied 這種情況,比如以 ubuntu 用戶的身份查看 /etc/shadow 的內容。因爲這個文件的內容是隻有 root 用戶能查看的。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那如果我們想要查看怎麼辦呢?這時候就可以使用 sudo :","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"ubuntu@VM-0-14-ubuntu:~$ tail -n 3 /etc/shadow\ntail: cannot open '/etc/shadow' for reading: Permission denied # 沒有權限\nubuntu@VM-0-14-ubuntu:~$ sudo !! # 跟兩個驚歎號\nsudo tail -n 3 /etc/shadowntp:*:17752:0:99999:7:::mysql:!:18376:0:99999:7:::test_user:$6$.ZY1lj4m$ii0x9CG8h.JHlh6zKbfBXRuolJmIDBHAd5eqhvW7lbUQXTRS//89jcuTzRilKqRkP8YbYW4VPxmTVHWRLYNGS/:18406:0:99999:7:::ubuntu@VM-0-14-ubuntu:~$","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"實例中,我們使用了 sudo !! 這個小技巧,表示重複上面輸入的命令,只不過在命令最前面加上 sudo 。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因爲我已經設置了 sudo 命令不需要輸入密碼,所以這裏 sudo !! 就能直接輸出內容。如果沒有設置的話,需要輸入當前這個用戶的密碼,例如本例中,我就應該輸入 ubuntu 用戶的登錄密碼。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"兩次相鄰的 sudo 操作,如果間隔在 5min 之內,第二次輸入 sudo 不需要重新輸入密碼;如果超過 5min,那麼再輸入 sudo 時,又需要輸入密碼。所以一個比較省事的方法是設置 sudo 操作不需要密碼。後面介紹如何設置。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"sudo 除了以 root 用戶的權限執行命令外,還有其它幾個用法,這裏做簡單介紹。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"切換到 root 用戶:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"sudo su -","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這種方式也能以 login-shell 的方式切換到 root 用戶,但是它和 su - 方法是有區別的:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"前者輸入 sudo su - 後,需要提供當前用戶的登錄密碼,也就是 ubuntu 用戶的密碼;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"後者輸入 su - 後,需要提供 root 用戶的登錄密碼。","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"還有一個命令:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"sudo -i","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這個命令和 sudo su - 效果一致,也是切換到 root 用戶,也是需要提供當前用戶(ubuntu 用戶)的登錄密碼。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們現在切換到 test_user 用戶,嘗試顯示 /etc/shadow 文件的內容:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"ubuntu@VM-0-14-ubuntu:~$ su - test_user\nPassword: # test_user 的密碼\n$ sudo cat /etc/shadow[sudo] password for test_user: # test_user 的密碼test_user is not in the sudoers file. This incident will be reported.$","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們會看到倒數第二行中的錯誤提示信息,我們無法查看 /etc/shadow 的內容,這是爲什麼?爲什麼 ubuntu 可以使用 sudo 但是 test_user 不行呢?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這就涉及到 sudo 的工作原理了。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"3.2 sudo 工作原理","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一個用戶能否使用 sudo 命令,取決於 /etc/sudoers 文件的設置。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"從 3.1 節中我們已經看到,ubuntu 用戶可以正常使用 sudo ,但是 test_user 用戶卻無法使用,這是因爲 /etc/sudoers 文件裏沒有配置 test_user。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"/etc/sudoers 也是一個文本文件,但是因其有特定的語法,我們不要直接用 vim 或者 vi 來編輯它,需要用 visudo 這個命令。輸入這個命令之後就能直接編輯 /etc/sudoers 這個文件了。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"需要說明的是,只有 root 用戶有權限使用 visudo 命令。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們先來看下輸入 visudo 命令後顯示的內容。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"輸入(root 用戶):","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"root@VM-0-14-ubuntu:~# visudo","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"輸出:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"# User privilege specificationroot ALL=(ALL:ALL) ALL\n# Members of the admin group may gain root privileges%admin ALL=(ALL) ALL# Allow members of group sudo to execute any command%sudo ALL=(ALL:ALL) ALL# See sudoers(5) for more information on \"#include\" directives:#includedir /etc/sudoers.dubuntu ALL=(ALL:ALL) NOPASSWD: ALL","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"解釋下每一行的格式:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"第一個表示用戶名,如 root 、ubuntu 等;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接下來等號左邊的 ALL 表示允許從任何主機登錄當前的用戶賬戶;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"等號右邊的 ALL 表示:這一行行首對一個的用戶可以切換到系統中任何一個其它用戶;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"行尾的 ALL 表示:當前行首的用戶,能以 root 用戶的身份下達什麼命令,ALL 表示可以下達任何命令。","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們還注意到 ubuntu 對應的那一行有個 NOPASSWD 關鍵字,這就是表明 ubuntu 這個用戶在請求 sudo 時不需要輸入密碼,到這裏就解釋了前面的問題。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"同時我們注意到,這個文件裏並沒有 test_user 對應的行,這也就解釋了爲什麼 test_user 無法使用 sudo 命令。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接下來,我們嘗試將 test_user 添加到 /etc/sudoers 文件中,使 test_user 也能使用 sudo 命令。我們在最後一行添加:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"test_user ALL=(ALL:ALL) ALL # test_user 使用 sudo 需要提供 test_user 的密碼","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接下來我們再在 test_user 賬戶下執行 sudo :","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"ubuntu@VM-0-14-ubuntu:~$ su - test_user\nPassword:\n$ tail -n 3 /etc/shadowtail: cannot open '/etc/shadow' for reading: Permission denied\n$ sudo tail -n 3 /etc/shadow # 加上 sudontp:*:17752:0:99999:7:::mysql:!:18376:0:99999:7:::test_user:$6$.ZY1lj4m$ii0x9CG8h.JHlh6zKbfBXRuolJmIDBHAd5eqhvW7lbUQXTRS//89jcuTzRilKqRkP8YbYW4VPxmTVHWRLYNGS/:18406:0:99999:7:::$","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以看到,現在已經可以使用 sudo 了。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"3.3 思考","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們已經看到了,如果一個用戶在 /etc/sudoers 文件中,那麼它就具有 sudo 權限,就能通過 sudo su - 或者 sudo -i 等命令切換到 root 用戶了,那這時這個用戶就變成 root 用戶了,那這不對系統造成很大的威脅嗎?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"實際上的確是這樣的。所以如果在編輯 /etc/sudoers 文件賦予某種用戶 sudo 權限時,必須要確定該用戶是可信任的,不會對系統造成惡意破壞,否則將所有 root 權限都賦予該用戶將會有非常大的危險。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當然,root 用戶也可以編輯 /etc/sudoers 使用戶只具備一部分權限,即只能執行一小部分命令。有興趣的讀者可以參考 Reference 部分第二條,這篇文章不再贅述。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"4. 二者的差異對比","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們已經看到:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用 su - ,提供 root 賬戶的密碼,可以切換到 root 用戶;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用 sudo su - ,提供當前用戶的密碼,也可以切換到 root 用戶","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"兩種方式的差異也顯而易見:如果我們的 Linux 系統有很多用戶需要使用的話,前者要求所有用戶都知道 root 用戶的密碼,這顯然是非常危險的;後者是不需要暴露 root 賬戶密碼的,用戶只需要輸入自己的賬戶密碼就可以,而且哪些用戶可以切換到 root,這完全是受 root 控制的(root 通過設置 /etc/sudoers 實現的),這樣系統就安全很多了。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一般都是推薦使用 sudo 方式。","attrs":{}}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章