# Linux sudo命令
Linux sudo命令以系統管理者的身份執行指令,也就是說,經由 sudo 所執行的指令就好像是 root 親自執行。
使用權限:在 /etc/sudoers 中有出現的使用者。
### 語法
```
sudo -V
```
```
sudo -h
```
```
sudo -l
```
```
sudo -v
```
```
sudo -k
```
```
sudo -s
```
```
sudo -H
```
```
sudo [ -b ] [ -p prompt ] [ -u username/#uid] -s
```
```
sudo command
```
**參數說明**:
* -V 顯示版本編號
* -h 會顯示版本編號及指令的使用方式說明
* -l 顯示出自己(執行 sudo 的使用者)的權限
* -v 因為 sudo 在第一次執行時或是在 N 分鐘內沒有執行(N 預設為五)會問密碼,這個參數是重新做一次確認,如果超過 N 分鐘,也會問密碼
* -k 將會強迫使用者在下一次執行 sudo 時問密碼(不論有沒有超過 N 分鐘)
* -b 將要執行的指令放在背景執行
* -p prompt 可以更改問密碼的提示語,其中 %u 會代換為使用者的帳號名稱, %h 會顯示主機名稱
* -u username/#uid 不加此參數,代表要以 root 的身份執行指令,而加了此參數,可以以 username 的身份執行指令(#uid 為該 username 的使用者號碼)
* -s 執行環境變數中的 SHELL 所指定的 shell ,或是 /etc/passwd 里所指定的 shell
* -H 將環境變數中的 HOME (家目錄)指定為要變更身份的使用者家目錄(如不加 -u 參數就是系統管理者 root )
* command 要以系統管理者身份(或以 -u 更改為其他人)執行的指令
### 實例
sudo命令使用
```
$ sudo ls
[sudo] password for hnlinux:
hnlinux is not in the sudoers file. This incident will be reported.
```
指定用戶執行命令
```
# sudo -u userb ls -l
```
顯示sudo設置
```
$ sudo -L //顯示sudo設置
Available options in a sudoers ``Defaults'' line:
syslog: Syslog facility if syslog is being used for logging
syslog_goodpri: Syslog priority to use when user authenticates successfully
syslog_badpri: Syslog priority to use when user authenticates unsuccessfully
long_otp_prompt: Put OTP prompt on its own line
ignore_dot: Ignore '.' in $PATH
mail_always: Always send mail when sudo is run
mail_badpass: Send mail if user authentication fails
mail_no_user: Send mail if the user is not in sudoers
mail_no_host: Send mail if the user is not in sudoers for this host
mail_no_perms: Send mail if the user is not allowed to run a command
tty_tickets: Use a separate timestamp for each user/tty combo
lecture: Lecture user the first time they run sudo
lecture_file: File containing the sudo lecture
authenticate: Require users to authenticate by default
root_sudo: Root may run sudo
log_host: Log the hostname in the (non-syslog) log file
log_year: Log the year in the (non-syslog) log file
shell_noargs: If sudo is invoked with no arguments, start a shell
set_home: Set $HOME to the target user when starting a shell with -s
always_set_home: Always set $HOME to the target user's home directory
path_info: Allow some information gathering to give useful error messages
fqdn: Require fully-qualified hostnames in the sudoers file
insults: Insult the user when they enter an incorrect password
requiretty: Only allow the user to run sudo if they have a tty
env_editor: Visudo will honor the EDITOR environment variable
rootpw: Prompt for root's password, not the users's
runaspw: Prompt for the runas_default user's password, not the users's
targetpw: Prompt for the target user's password, not the users's
use_loginclass: Apply defaults in the target user's login class if there is one
set_logname: Set the LOGNAME and USER environment variables
stay_setuid: Only set the effective uid to the target user, not the real uid
preserve_groups: Don't initialize the group vector to that of the target user
loglinelen: Length at which to wrap log file lines (0 for no wrap)
timestamp_timeout: Authentication timestamp timeout
passwd_timeout: Password prompt timeout
passwd_tries: Number of tries to enter a password
umask: Umask to use or 0777 to use user's
logfile: Path to log file
mailerpath: Path to mail program
mailerflags: Flags for mail program
mailto: Address to send mail to
mailfrom: Address to send mail from
mailsub: Subject line for mail messages
badpass_message: Incorrect password message
timestampdir: Path to authentication timestamp dir
timestampowner: Owner of the authentication timestamp dir
exempt_group: Users in this group are exempt from password and PATH requirements
passprompt: Default password prompt
passprompt_override: If set, passprompt will override system prompt in all cases.
runas_default: Default user to run commands as
secure_path: Value to override user's $PATH with
editor: Path to the editor for use by visudo
listpw: When to require a password for 'list' pseudocommand
verifypw: When to require a password for 'verify' pseudocommand
noexec: Preload the dummy exec functions contained in 'noexec_file'
noexec_file: File containing dummy exec functions
ignore_local_sudoers: If LDAP directory is up, do we ignore local sudoers file
closefrom: File descriptors >= %d will be closed before executing a command
closefrom_override: If set, users may override the value of `closefrom' with the -C option
setenv: Allow users to set arbitrary environment variables
env_reset: Reset the environment to a default set of variables
env_check: Environment variables to check for sanity
env_delete: Environment variables to remove
env_keep: Environment variables to preserve
role: SELinux role to use in the new security context
type: SELinux type to use in the new security context
askpass: Path to the askpass helper program
env_file: Path to the sudo-specific environment file
sudoers_locale: Locale to use while parsing sudoers
visiblepw: Allow sudo to prompt for a password even if it would be visisble
pwfeedback: Provide visual feedback at the password prompt when there is user input
fast_glob: Use faster globbing that is less accurate but does not access the filesystem
umask_override: The umask specified in sudoers will override the user's, even if it is more permissive
```
以root權限執行上一條命令
```
$ sudo !!
```
以特定用戶身份進行編輯文本
```
$ sudo -u uggc vi ~www/index.html
//以 uggc 用戶身份編輯 home 目錄下www目錄中的 index.html 文件
```
列出目前的權限
```
sudo -l
```
列出 sudo 的版本資訊
```
sudo -V
```
- Linux 基礎
- Linux 簡介
- Linux 安裝
- Linux 系統啟動過程
- Linux 系統目錄結構
- Linux 忘記密碼解決方法
- Linux 遠程登錄
- Linux 文件基本屬性
- Linux 文件與目錄管理
- Linux 用戶和用戶組管理
- Linux 磁盤管理
- Linux vi/vim
- Shell 編程
- Shell 教程
- Shell 變量
- Shell test命令
- Shell 流程控制
- Shell 函數
- Linux命令大全
- Linux命令大全 - 文件管理
- Linux cat命令
- Linux chattr命令
- Linux chgrp命令
- Linux chmod命令
- Linux chown命令
- Linux cksum命令
- Linux cmp命令
- Linux diff命令
- Linux diffstat命令
- Linux file命令
- Linux find命令
- Linux git命令
- Linux gitview命令
- Linux indent命令
- Linux cut命令
- Linux ln命令
- Linux less命令
- Linux locate命令
- Linux lsattr命令
- Linux mattrib命令
- Linux mc命令
- Linux mdel命令
- Linux mdir命令
- Linux mktemp命令
- Linux more命令
- Linux mmove命令
- Linux mread命令
- Linux mren命令
- Linux mtools命令
- Linux mtoolstest命令
- Linux mv命令
- Linux od命令
- Linux paste命令
- Linux patch命令
- Linux rcp命令
- Linux rm命令
- Linux slocate命令
- Linux split命令
- Linux tee命令
- Linux tmpwatch命令
- Linux touch命令
- Linux umask命令
- Linux which命令
- Linux cp命令
- Linux mcopy命令
- Linux mshowfat命令
- Linux rhmask命令
- Linux whereis命令
- Linux scp命令
- Linux awk 命令
- Linux命令大全 - 文檔編輯
- Linux col命令
- Linux colrm命令
- Linux comm命令
- Linux csplit命令
- Linux ed命令
- Linux egrep命令
- Linux ex命令
- Linux fgrep命令
- Linux fmt命令
- Linux fold命令
- Linux grep命令
- Linux ispell命令
- Linux jed命令
- Linux joe命令
- Linux join命令
- Linux look命令
- Linux mtype命令
- Linux pico命令
- Linux rgrep命令
- Linux sed命令
- Linux sort命令
- Linux spell命令
- Linux tr命令
- Linux expr命令
- Linux uniq命令
- Linux wc命令
- Linux命令大全 - 文件傳輸
- Linux lprm命令
- Linux lpr命令
- Linux lpq命令
- Linux lpd命令
- Linux bye命令
- Linux ftp命令
- Linux ncftp命令
- Linux tftp命令
- Linux uuto命令
- Linux uupick命令
- Linux uucp命令
- Linux uucico命令
- Linux ftpshut命令
- Linux ftpwho命令
- Linux ftpcount命令
- Linux命令大全 - 磁盤管理
- Linux cd命令
- Linux df命令
- Linux dirs命令
- Linux du命令
- Linux edquota命令
- Linux mlabel命令
- Linux mkdir命令
- Linux mdu命令
- Linux mdeltree命令
- Linux mcd命令
- Linux eject命令
- Linux mount命令
- Linux mmd命令
- Linux mrd命令
- Linux mzip命令
- Linux pwd命令
- Linux quota命令
- Linux mmount命令
- Linux rmdir命令
- Linux rmt命令
- Linux stat命令
- Linux tree命令
- Linux umount命令
- Linux ls命令
- Linux quotacheck命令
- Linux quotaoff命令
- Linux lndir命令
- Linux repquota命令
- Linux quotaon命令
- Linux命令大全 - 磁盤維護
- Linux badblocks命令
- Linux cfdisk命令
- Linux dd命令
- Linux e2fsck命令
- Linux ext2ed命令
- Linux mkbootdisk命令
- Linux fsck命令
- Linux fsck.minix命令
- Linux fsconf命令
- Linux fdformat命令
- Linux hdparm命令
- Linux mformat命令
- Linux mkdosfs命令
- Linux mke2fs命令
- Linux mkfs.ext2命令
- Linux mkfs.msdos命令
- Linux mkinitrd命令
- Linux mkisofs命令
- Linux mkswap命令
- Linux mpartition命令
- Linux swapon命令
- Linux symlinks命令
- Linux sync命令
- Linux mbadblocks命令
- Linux mkfs.minix命令
- Linux fsck.ext2命令
- Linux fdisk命令
- Linux losetup命令
- Linux mkfs命令
- Linux getty命令
- Linux sfdisk命令
- Linux swapoff命令
- Linux命令大全 - 網絡通訊
- Linux apachectl命令
- Linux arpwatch命令
- Linux nc命令
- Linux dip命令
- Linux mingetty命令
- Linux netconfig命令
- Linux ppp-off命令
- Linux uustat命令
- Linux uulog命令
- Linux wall命令
- Linux uux命令
- Linux telnet命令
- Linux netstat命令
- Linux dnsconf命令
- Linux mesg命令
- Linux httpd命令
- Linux ifconfig命令
- Linux minicom命令
- Linux traceroute命令
- Linux talk命令
- Linux ping命令
- Linux pppstats命令
- Linux samba命令
- Linux statserial命令
- Linux write命令
- Linux setserial命令
- Linux tty命令
- Linux newaliases命令
- Linux uuname命令
- Linux netconf命令
- Linux smbd命令
- Linux ytalk命令
- Linux tcpdump命令
- Linux cu命令
- Linux efax命令
- Linux pppsetup命令
- Linux testparm命令
- Linux smbclient命令
- Linux shapecfg命令
- Linux命令大全 - 系統管理
- Linux date命令
- Linux chfn命令
- Linux adduser命令
- Linux groupdel命令
- Linux useradd命令
- Linux groupmod命令
- Linux logname命令
- Linux logout命令
- Linux ps命令
- Linux exit命令
- Linux finger命令
- Linux fwhios命令
- Linux sleep命令
- Linux suspend命令
- Linux login命令
- Linux lastb命令
- Linux rlogin命令
- Linux last命令
- Linux reboot命令
- Linux kill命令
- Linux halt命令
- Linux nice命令
- Linux procinfo命令
- Linux top命令
- Linux pstree命令
- Linux shutdown命令
- Linux screen命令
- Linux sliplogin命令
- Linux rsh命令
- Linux rwho命令
- Linux sudo命令
- Linux gitps命令
- Linux uname命令
- Linux logrotate命令
- Linux tload命令
- Linux swatch命令
- Linux chsh命令
- Linux whoami命令
- Linux who命令
- Linux vlock命令
- Linux usermod命令
- Linux userdel命令
- Linux userconf命令
- Linux id命令
- Linux w命令
- Linux skill命令
- Linux su命令
- Linux renice命令
- Linux newgrp命令
- Linux whois命令
- Linux free命令
- Linux命令大全 - 系統設定
- Linux bind命令
- Linux aumix命令
- Linux dircolors命令
- Linux alias命令
- Linux clear命令
- Linux reset命令
- Linux enable命令
- Linux dmesg命令
- Linux depmod命令
- Linux declare命令
- Linux crontab命令
- Linux clock命令
- Linux chroot命令
- Linux insmod命令
- Linux rpm命令
- Linux grpconv命令
- Linux pwunconv命令
- Linux export命令
- Linux eval命令
- Linux set命令
- Linux minfo命令
- Linux lsmod命令
- Linux liloconfig命令
- Linux lilo命令
- Linux kbdconfig命令
- Linux modprobe命令
- Linux ntsysv命令
- Linux mouseconfig命令
- Linux passwd命令
- Linux pwconv命令
- Linux rdate命令
- Linux resize命令
- Linux rmmod命令
- Linux grpunconv命令
- Linux modinfo命令
- Linux time命令
- Linux setup命令
- Linux sndconfig命令
- Linux setenv命令
- Linux chkconfig命令
- Linux unset命令
- Linux ulimit命令
- Linux timeconfig命令
- Linux setconsole命令
- Linux mkkickstart命令
- Linux hwclock命令
- Linux apmd命令
- Linux fbset命令
- Linux unalias命令
- Linux SVGATextMode命令
- Linux命令大全 - 備份壓縮
- Linux bzip2recover命令
- Linux bzip2命令
- Linux bunzip2命令
- Linux ar命令
- Linux gunzip命令
- Linux unarj命令
- Linux compress命令
- Linux cpio命令
- Linux dump命令
- Linux uuencode命令
- Linux restore命令
- Linux lha命令
- Linux gzip命令
- Linux gzexe命令
- Linux zipinfo命令
- Linux zip命令
- Linux unzip命令
- Linux uudecode命令
- Linux tar命令
- Linux命令大全 - 設備管理
- Linux setleds命令
- Linux loadkeys命令
- Linux rdev命令
- Linux dumpkeys命令
- Linux MAKEDEV命令
- 免責聲明