1. 程式人生 > >Linux系列:Ubuntu/fedora實用小技巧—禁止自動鎖屏、設定免密碼自動登入、免密碼執行sudo操作

Linux系列:Ubuntu/fedora實用小技巧—禁止自動鎖屏、設定免密碼自動登入、免密碼執行sudo操作

首先宣告:該文雖以Ubuntu 13.04為例,同樣適用於Fedora 17(已測試),但在較低版本的Ubuntu下可能有所差異,具體看後面的注意事項。

技巧目錄:

  • 解決Ubuntu下每隔幾分鐘自動鎖屏,需要重新輸入密碼的問題
  • 無須輸入使用者名稱密碼讓Ubuntu自動登入到桌面
  • Ubuntu下如何使用sudo不用輸入密碼

1. 解決Ubuntu下每隔幾分鐘自動鎖屏,需要重新輸入密碼的問題

有時候幾分鐘不用Ubuntu,系統就自動鎖屏了,這是一種安全措施,防止別人趁你不在時使用你的系統。但對於大部分人而言,這是沒有必要的,尤其是Ubuntu虛擬機器,裡面沒啥重要的東西,每次鎖屏後需要重新輸入密碼解鎖實際上非常枯燥且浪費時間。

解決方案:

(1)開啟系統設定,找到“Brightness & Lock”(亮度和鎖屏)設定並開啟,如下圖:


(2)根據提示可以設定螢幕多久關閉和鎖屏時間設定,一個“Never”和“OFF”即可搞定。

這樣設定後螢幕就不會自動關閉和自動鎖屏了。

2. 無須輸入使用者名稱密碼讓Ubuntu自動登入到桌面

每個Linux使用者都必須設有密碼,這與Windows不同,對於非專業使用Linux的使用者來說,已經解決了自動鎖屏輸密碼問題,如果每次登入系統也不用輸入密碼 ,豈不是讓我們的工作更加輕鬆更加easy。注意這裡的免密碼登入系統並不是說使用者沒有密碼了,密碼還在只是系統把密碼記住了自動用你的密碼登入而已。

解決方案:

(1)系統設定裡找到“User Accounts”

(2)然後點選右上角的“Unlock”,輸入當前使用者的密碼解鎖認證

(3)解鎖成功後,開啟自動登入功能,即“Automatic Login”開關開啟,儲存設定即可。

注意事項:較低版本的Ubuntu可能沒有該設定選項,那麼可以通過配置檔案設定自動登入:
/etc/gdm/目錄下有個custom.conf或者gdm.confgdm.conf-custom檔案,比如我的Ubuntu9.11下的gdm下有這些檔案:



開啟gdm.conf-custom,擷取最前面的幾句註釋說明如下:
# GDM Configuration Customization file.
#
# This file is the appropriate place for specifying your customizations to the
# GDM configuration.   If you run gdmsetup, it will automatically edit this
# file for you
and will cause the daemon and any running GDM GUI programs to # automatically update with the new configuration.  Not all configuration # options are supported by gdmsetup, so to modify some values it may be # necessary to modify this file directly by hand. # # Older versions of GDM used the "gdm.conf" file for configuration.  If your # system has an old gdm.conf file on the system, it will be used instead of # this file - so changes made to this file will not take effect.  Consider # migrating your configuration to this file and removing the gdm.conf file.
檔案中後面說到如果有gdm.conf老配置檔案,那麼該檔案中的值就不會起作用。那麼我們就去gdm.conf裡配置吧,開啟後找到下面這一段:
[daemon]
# Automatic login, if true the first attached screen will automatically logged
# in as user as set with AutomaticLogin key.
AutomaticLoginEnable=false
AutomaticLogin=
上面的意思很好理解,將AutomaticLoginEnable變數值改為true就可以了。
當然,通過上面的提示我們知道還有一種方法修改該配置檔案,就是使用sudo gdmsetup這個命令,執行後會出現設定介面,進入“Security”選項卡:



將“Enable Automatic Login“選上,然後選擇你自己的使用者名稱即可。

3. Ubuntu下如何使用sudo不用輸入密碼

普通使用者登入Ubuntu一般使用的是普通賬戶而非管理員賬戶,因此執行有管理員許可權的程式時都要輸入sudo [command],這樣的命令輸入非常頻繁,關鍵是每次都要輸入一次使用者自己的密碼,非常煩人,下面就教你使用sudo不用輸入密碼的小技巧。

解決方案:

假設我的使用者名稱為hadoop,使用sudo不用密碼的方法如下:

執行命令:sudo visudo 或者sudo vi /etc/sudoers,如果vi來編輯,則儲存時記得用"wq!"強制儲存,否則會提示只讀不能儲存的。 上述命令就是用來編輯/etc/sudoers這個檔案的。
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults        env_reset
Defaults        mail_badpass
Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL:ALL) ALL

# Members of the admin group may gain root privileges

 預設情況我們會看到有"root    ALL=(ALL:ALL) ALL"這麼一句話,意思就是root使用者可以從任何主機連線進來以任意身份執行任意命令,但是需要輸入密碼,如果不想輸入密碼只要在後面的ALL之前加上NOPASSWD即可,關於該檔案的格式及引數詳解請看參考文獻或執行"man sudoers"看幫助文件吧。下面直接給出方法吧:
如果想讓hadoop使用者使用sudo不用輸入密碼只要在該檔案後面新增"hadoop ALL = NOPASSWD: ALL"這樣一行即可。 如果想讓 hadoop使用者sudo不用密碼即可執行某幾個命令,可這樣寫"hadoop  ALL = NOPASSWD: /usr/bin/abc.sh, /usr/sbin/adduser"。 注意:這裡有個問題需要注意,就是後面的配置可能會覆蓋前面的配置,所以你新增的配置最好是在最後一行。

參考文獻: