3.7 su命令 3.8 sudo命令 3.9 限制root遠程登陸

mkpass  解釋:make password node

[root@cham2 ~]# mkpasswd    make password  ^C
[root@cham2 ~]# mkpasswd                               直接輸入不行的
-bash: mkpasswd: 未找到命令
[root@cham2 ~]# yum install -y expect               要安裝一個包。
已加載插件:fastestmirror
 python

隨手生成一個密碼linux

#mkpasswdweb

指定密碼長度12位shell

#mkpasswd -l 12安全

還能夠指定3個特殊符號bash

#mkpasswd -l 12 -s 3oracle

也能夠不要特殊符號,只要小些大寫eclipse

#mkpasswd -l 12 -s 0ssh

 

su命令

su命令是用於切換用戶   **普通用戶顯示$,root用戶顯示#**

su的優缺點;

  su的確爲管理帶來方便,經過切換到root下,能完成全部系統管理工具,只要把root的密碼交給任何一個普通用戶,他都能切換到root來完成全部的系統管理工做;但經過su切換到root後,也有不安全因素;好比系統有10個用戶,並且都參與管理。若是這10個用戶都涉及到超級權限的運用,作爲管理員若是想讓其它用戶經過su來切換到超級權限的root,必須把root權限密碼都告訴這10個用戶;若是這10個用戶都有root權限,經過root權限能夠作任何事,這在必定程度上就對系統的安全形成了威協;想一想Windows吧,簡直就是惡夢;「沒有不安全的系統,只有不安全的人」,咱們絕對不能保證這 10個用戶都能按正常操做流程來管理系統,其中任何一人對系統操做的重大失誤,均可能致使系統崩潰或數據損失;因此su 工具在多人蔘與的系統管理中,並非最好的選擇,su只適用於一兩我的參與管理的系統,畢竟su並不能讓普通用戶受限的使用;超級用戶root密碼應該掌握在少數用戶手中,這絕對是真理!因此集權而治的存在仍是有必定道理的;

su - cham 和 su cham ,是有所區別的。加-是完全的切換。

在cham家目錄下,就會去加載cham的本身的配置文件

普通用戶也能夠登陸到root,只要知道root 密碼就行

還能夠在su的時候以cham用戶的身份去執行一條命令,不登陸cham直接執行

# su - -c "touch /tmp/cham.111" cham

[root@cham2 ~]# su - -c "touch /tmp/cham.111" cham
[root@cham2 ~]# ls -lt /tmp/ |head
總用量 12
-rw-rw-r--. 1 cham  cham   0 10月 31 14:36 cham.111
drwx------. 3 root  root  17 10月 30 12:53 systemd-private-6606fcf724b44e09ad6d69ab6146ea2c-vmtoolsd.service-xzj3RY
drw-r--r--. 2 cham  cham  17 10月 26 20:47 yyy
-rwxrwxrwx. 1 cham  cham   6 10月 26 20:02 321321
drwxrwxrwx. 2 cham  cham   6 10月 26 16:59 cham11
-rwxrwxrwx. 1 cham  cham   1 10月 26 16:48 cham
drwxrwxrwx. 2 user1 user1  6 10月 26 16:27 user1
-rw-r--r--. 2 root  root   0 10月 25 17:31 1.txt.bak
drw-r--r-x. 2 user1 cham  19 10月 25 15:41 cham2
[root@cham2 ~]# id cham
uid=1000(cham) gid=1000(cham) 組=1000(cham),1005(grp2),1007(user5)
[root@cham2 ~]# date
2017年 10月 31日 星期二 14:37:37 CST

再登陸user5,發現有問題,缺乏可加載的用戶配置文件和家目錄,操做一下讓它恢復正常

[root@cham2 ~]# passwd user5
更改用戶 user5 的密碼 。
新的 密碼:
無效的密碼: 密碼少於 8 個字符
從新輸入新的 密碼:
passwd:全部的身份驗證令牌已經成功更新。
[root@cham2 ~]# su - cham
上一次登陸:二 10月 31 14:49:39 CST 2017pts/1 上
[cham@cham2 ~]$ su - user5
密碼:
最後一次失敗的登陸:二 10月 31 14:49:50 CST 2017pts/1 上
最有一次成功登陸後有 1 次失敗的登陸嘗試。
su: 警告:沒法更改到 /home/user5 目錄: 沒有那個文件或目錄
-bash-4.2$ 
-bash-4.2$ pwd
/home/cham
-bash-4.2$ 登出
[cham@cham2 ~]$ 登出
[root@cham2 ~]# id user5                        看一下user5的全部者和所屬組
uid=1007(user5) gid=1007(user5) 組=1007(user5)
[root@cham2 ~]# mkdir /home/user5               首先建立用戶家目錄 
[root@cham2 ~]# chown user5:
[root@cham2 ~]# chown user5:user5 /home/user5/  更改/home/user5/的全部者以及所屬組,加不加-R均可以,因
                                                爲僅僅是個目錄
[root@cham2 ~]# su - user5                      在登陸仍是不行
上一次登陸:二 10月 31 14:51:01 CST 2017pts/1 上
-bash-4.2$ pwd
/home/user5
-bash-4.2$ 登出                                 
[root@cham2 ~]# su - cham
上一次登陸:二 10月 31 14:50:54 CST 2017pts/1 上
[cham@cham2 ~]$ ls -la
總用量 16
drwx------. 2 cham grp2  83 10月 27 15:13 .
drwxr-xr-x. 9 root root  98 10月 31 14:52 ..
-rw-------. 1 cham grp2 633 10月 31 14:51 .bash_history
-rw-r--r--. 1 cham grp2  18 8月   3 2016 .bash_logout
-rw-r--r--. 1 cham grp2 193 8月   3 2016 .bash_profile
-rw-r--r--. 1 cham grp2 231 8月   3 2016 .bashrc
[cham@cham2 ~]$ 登出
[root@cham2 ~]# ls /etc/skel/              缺乏配置文件.bash開頭的文件,系統有個模板在這個路徑
[root@cham2 ~]# ls -la /etc/skel/          
總用量 24
drwxr-xr-x.  2 root root   62 10月 19 06:56 .
drwxr-xr-x. 77 root root 8192 10月 31 14:50 ..
-rw-r--r--.  1 root root   18 8月   3 2016 .bash_logout
-rw-r--r--.  1 root root  193 8月   3 2016 .bash_profile
-rw-r--r--.  1 root root  231 8月   3 2016 .bashrc
[root@cham2 ~]# cp /etc/skel/.bash*  /home/user5/  拷貝/etc/skel/.bash*(表明拷貝.bash通配)
                                                   到/home/user5/到user5的家目錄下去
[root@cham2 ~]# chown -R user5:user5 !$            更改全部者以及所屬組要加-R(目錄裏的目錄或者文件也全 
                                                   部更改)(!$表示上一條命令的最後參數)
chown -R user5:user5 /home/user5/
[root@cham2 ~]# 
[root@cham2 ~]# su - user5                         再試試,正常了
上一次登陸:二 10月 31 14:53:13 CST 2017pts/1 上
[user5@cham2 ~]$ pwd
/home/user5
[user5@cham2 ~]$ 登出
[root@cham2 ~]#

 

sudo

它可讓普通用戶臨時去執行一條命令。以指定用戶的身份去執行,這樣能夠避免把root用戶密碼給普通用戶

用來以其餘身份來執行命令,預設的身份爲root。在/etc/sudoers中設置了可執行sudo指令的用戶。若其未經受權的用戶企圖使用sudo,則會發出警告的郵件給管理員。用戶使用sudo時,必須先輸入密碼,以後有5分鐘的有效期限,超過時限則必須從新輸入密碼。 

語法:sudo /usr/bin/ls /root/   使用時能夠不加絕對路徑。

選項: (該部分只作瞭解)
-b:在後臺執行指令;
-h:顯示幫助;
-H:將HOME環境變量設爲新身份的HOME環境變量;
-k:結束密碼的有效期限,也就是下次再執行sudo時便須要輸入密碼;
-l:列出目前用戶可執行與沒法執行的指令;
-p:改變詢問密碼的提示符號;
-s:執行指定的shell;
-u<用戶>:以指定的用戶做爲新的身份。若不加上此參數,則預設以root做爲新的身份;
-v:延長密碼有效期限5分鐘;
-V :顯示版本信息。

讓用戶使用不在輸入密碼   NOPASSWD:

配置sudo必須經過編輯/etc/sudoers文件,並且只有超級用戶才能夠修改它。使用visudo命令編輯/etc/sudoers配置文件,操做方法同vi命令。當對多個命令設置速sudo權限時,須要用逗號加空格隔開。使用visudo有兩個緣由,一是它可以防止兩個用戶同時修改它;二是它也能進行有限的語法檢查。因此,即便只有你一個超級用戶,你也最好用visudo來檢查一下語法。  

來作一個實驗

執行#visudo  能夠打開sudo ,也就是sudo的配置文件/etc/sudoers.tmp,*visudo還能夠檢測錯誤*

挺重要的不建議用#vi sudo,直接打開

執行#visudo ,用方向鍵下找到root ALL=(ALL)           ALL   這是整個文件最核心的一句

它表示容許root用戶去運行全部的命令,。外面ALL值得是主機但實際不許確,括號(ALL)指的是用戶,好比root。咱們加一句:wq保存退出

提示92行語法錯誤,按e從新編輯,輸入:set nu查看行序號

要寫絕對路徑,用逗號分隔,要有一個空格,以下

再:wq保存退出

下面來看一下sudo的用法,登陸到cham用戶,su - cham ,正常呢,在cham用戶下使用ls /root/是不行的。

由於ls命令有個set_uid 和set_gid都把它去掉

[root@cham2 ~]# su - cham
上一次登陸:二 10月 31 15:23:47 CST 2017pts/1 上
[cham@cham2 ~]$ ls /root/
111     123     1_heard.txt  22.txt  2.txt.bak.bak  88_sorft.txt  anaconda-ks.cfg.1
11.txt  12.txt  1_sorft      234     88_heard.txt   88.txt
[cham@cham2 ~]$ 登出
[root@cham2 ~]# chmod u-s /bin/ls
[root@cham2 ~]# su - cham
上一次登陸:二 10月 31 15:57:44 CST 2017pts/1 上
[cham@cham2 ~]$ ls /root/
111     123     1_heard.txt  22.txt  2.txt.bak.bak  88_sorft.txt  anaconda-ks.cfg.1
11.txt  12.txt  1_sorft      234     88_heard.txt   88.txt
[cham@cham2 ~]$ 登出
[root@cham2 ~]# ls -l /usr/bin/ls
-rwxr-sr-x. 1 root root 117656 11月  6 2016 /usr/bin/ls
[root@cham2 ~]# chmod g-s /usr/bin/ls
[root@cham2 ~]# su - cham
上一次登陸:二 10月 31 15:58:39 CST 2017pts/1 上
[cham@cham2 ~]$ ls /root/                           正常的話是權限不夠的!
ls: 沒法打開目錄/root/: 權限不夠 
[cham@cham2 ~]$ sudo /usr/bin/ls /root/             sudo的用法,第一次用會提示輸入用戶的密碼

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

[sudo] password for cham: 
Sorry, try again.
[sudo] password for cham: 
Sorry, try again.
[sudo] password for cham: 
111	123	1_heard.txt  22.txt  2.txt.bak.bak  88_sorft.txt  anaconda-ks.cfg.1
11.txt	12.txt	1_sorft      234     88_heard.txt   88.txt
[cham@cham2 ~]$ sudo /usr/bin/ls /root/            第2次使用就不用了
111	123	1_heard.txt  22.txt  2.txt.bak.bak  88_sorft.txt  anaconda-ks.cfg.1
11.txt	12.txt	1_sorft      234     88_heard.txt   88.txt
[cham@cham2 ~]$ ls /root/
ls: 沒法打開目錄/root/: 權限不夠
[cham@cham2 ~]$ sudo /usr/bin/ls /root/
111	123	1_heard.txt  22.txt  2.txt.bak.bak  88_sorft.txt  anaconda-ks.cfg.1
11.txt	12.txt	1_sorft      234     88_heard.txt   88.txt
[cham@cham2 ~]$ cat /root/12.txt
cat: /root/12.txt: 權限不夠
[cham@cham2 ~]$ sudo /usr/bin/cat /root/12.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[cham@cham2 ~]$

再進入visudo一樣的方法給user5設定,而且第一次使用不輸入密碼,以下NOPASSWD

使用時能夠不加絕對路徑。

[cham@cham2 ~]$ 登出
[root@cham2 ~]# visudo
[root@cham2 ~]# visudo
visudo:/etc/sudoers.tmp 未更改
[root@cham2 ~]# visudo
[root@cham2 ~]# su - user5
上一次登陸:二 10月 31 14:56:14 CST 2017pts/1 上
[user5@cham2 ~]$ ls /root/
ls: 沒法打開目錄/root/: 權限不夠
[user5@cham2 ~]$ sudo /usr/bin/ls /root/
111	123	1_heard.txt  22.txt  2.txt.bak.bak  88_sorft.txt  anaconda-ks.cfg.1
11.txt	12.txt	1_sorft      234     88_heard.txt   88.txt
[user5@cham2 ~]$ sudo ls /root/
111	123	1_heard.txt  22.txt  2.txt.bak.bak  88_sorft.txt  anaconda-ks.cfg.1
11.txt	12.txt	1_sorft      234     88_heard.txt   88.txt

下面再來試試命令的Aliase,命令的別名

[cham@cham2 ~]$ sudo ls /root/
[sudo] password for cham: 
111	123	1_heard.txt  22.txt  2.txt.bak.bak  88_sorft.txt  anaconda-ks.cfg.1
11.txt	12.txt	1_sorft      234     88_heard.txt   88.txt
[cham@cham2 ~]$ cat /root/12.txt
cat: /root/12.txt: 權限不夠
[cham@cham2 ~]$ sudo cat /root/12.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin

還有用戶組

針對一個組去作限制,工做種可能會用到,但不會用的特別複雜。

sudo配置文件樣例

#
# Sample /etc/sudoers file.
#
# This file MUST be edited with the 'visudo' command as root.
#
# See the sudoers man page for the details on how to write a sudoers file.
#

##
# User alias specification
##
User_Alias	FULLTIMERS = millert, mikef, dowdy
User_Alias	PARTTIMERS = bostley, jwfox, crawl
User_Alias	WEBMASTERS = will, wendy, wim

##
# Runas alias specification
##
Runas_Alias	OP = root, operator
Runas_Alias	DB = oracle, sybase

##
# Host alias specification
##
Host_Alias	SPARC = bigtime, eclipse, moet, anchor:\
		SGI = grolsch, dandelion, black:\
		ALPHA = widget, thalamus, foobar:\
		HPPA = boa, nag, python
Host_Alias	CUNETS = 128.138.0.0/255.255.0.0
Host_Alias	CSNETS = 128.138.243.0, 128.138.204.0/24, 128.138.242.0
Host_Alias	SERVERS = master, mail, www, ns
Host_Alias	CDROM = orion, perseus, hercules

##
# Cmnd alias specification
##
Cmnd_Alias	DUMPS = /usr/sbin/dump, /usr/sbin/rdump, /usr/sbin/restore, \
			/usr/sbin/rrestore, /usr/bin/mt
Cmnd_Alias	KILL = /usr/bin/kill
Cmnd_Alias	PRINTING = /usr/sbin/lpc, /usr/bin/lprm
Cmnd_Alias	SHUTDOWN = /usr/sbin/shutdown
Cmnd_Alias	HALT = /usr/sbin/halt
Cmnd_Alias	REBOOT = /usr/sbin/reboot
Cmnd_Alias	SHELLS = /sbin/sh, /usr/bin/sh, /usr/bin/csh, /usr/bin/ksh, \
			 /usr/local/bin/tcsh, /usr/bin/rsh, \
			 /usr/local/bin/zsh
Cmnd_Alias	SU = /usr/bin/su
Cmnd_Alias	VIPW = /usr/sbin/vipw, /usr/bin/passwd, /usr/bin/chsh, \
		       /usr/bin/chfn

##
# Override built-in defaults
##
Defaults               syslog=auth
Defaults>root          !set_logname
Defaults:FULLTIMERS    !lecture
Defaults:millert       !authenticate
Defaults@SERVERS       log_year, logfile=/var/log/sudo.log

##
# User specification
##

# root and users in group wheel can run anything on any machine as any user
root		ALL = (ALL) ALL
%wheel		ALL = (ALL) ALL

# full time sysadmins can run anything on any machine without a password
FULLTIMERS	ALL = NOPASSWD: ALL

# part time sysadmins may run anything but need a password
PARTTIMERS	ALL = ALL

# jack may run anything on machines in CSNETS
jack		CSNETS = ALL

# lisa may run any command on any host in CUNETS (a class B network)
lisa		CUNETS = ALL

# operator may run maintenance commands and anything in /usr/oper/bin/
operator	ALL = DUMPS, KILL, SHUTDOWN, HALT, REBOOT, PRINTING,\
		sudoedit /etc/printcap, /usr/oper/bin/

# joe may su only to operator
joe		ALL = /usr/bin/su operator

# pete may change passwords for anyone but root on the hp snakes
pete		HPPA = /usr/bin/passwd [A-z]*, !/usr/bin/passwd root

# bob may run anything on the sparc and sgi machines as any user
# listed in the Runas_Alias "OP" (ie: root and operator)
bob		SPARC = (OP) ALL : SGI = (OP) ALL

# jim may run anything on machines in the biglab netgroup
jim		+biglab = ALL

# users in the secretaries netgroup need to help manage the printers
# as well as add and remove users
+secretaries	ALL = PRINTING, /usr/bin/adduser, /usr/bin/rmuser

# fred can run commands as oracle or sybase without a password
fred		ALL = (DB) NOPASSWD: ALL

# on the alphas, john may su to anyone but root and flags are not allowed
john		ALPHA = /usr/bin/su [!-]*, !/usr/bin/su *root*

# jen can run anything on all machines except the ones
# in the "SERVERS" Host_Alias
jen		ALL, !SERVERS = ALL

# jill can run any commands in the directory /usr/bin/, except for
# those in the SU and SHELLS aliases.
jill		SERVERS = /usr/bin/, !SU, !SHELLS

# steve can run any command in the directory /usr/local/op_commands/
# as user operator.
steve		CSNETS = (operator) /usr/local/op_commands/

# matt needs to be able to kill things on his workstation when
# they get hung.
matt		valkyrie = KILL

# users in the WEBMASTERS User_Alias (will, wendy, and wim)
# may run any command as user www (which owns the web pages)
# or simply su to www.
WEBMASTERS	www = (www) ALL, (root) /usr/bin/su www

# anyone can mount/unmount a cd-rom on the machines in the CDROM alias
ALL		CDROM = NOPASSWD: /sbin/umount /CDROM,\
		/sbin/mount -o nosuid\,nodev /dev/cd0a /CDROM

sudo -i 詳解

sudo : 暫時切換到超級用戶模式以執行超級用戶權限,提示輸入密碼時該密碼爲當前用戶的密碼,而不是超級帳戶的密碼。不過有時間限制,Ubuntu默認爲一次時長15分鐘。

su : 切換到某某用戶模式,提示輸入密碼時該密碼爲切換後帳戶的密碼,用法爲「su帳戶名稱」。若是後面不加帳戶時系統默認爲root帳戶,密碼也爲超級帳戶的密碼。沒有時間限制。

sudo -i: 爲了頻繁的執行某些只有超級用戶才能執行的權限,而不用每次輸入密碼,可使用該命令。提示輸入密碼時該密碼爲當前帳戶的密碼。沒有時間限制。執行該命令後提示符變爲「#」而不是「$」。想退回普通帳戶時能夠執行「exit」或「logout」 。

其實,還有幾個相似的用法:
sudo /bin/bash:
這個命令也會切換到root的bash下,但不能徹底擁有root的全部環境變量,好比PATH,能夠擁有root用戶的權限。這個命令和 sudo -s 是等同的。

sudo -s : 如上

sudo su : 這個命令,也是登陸到了root,可是並無切換root的環境變量,好比PATH。

sudo su - : 這個命令,純粹的切換到root環境下,能夠這樣理解,先是切換到了root身份,而後又以root身份執行了 su -,此時跟使用root登陸沒有什麼區別。此結果貌似跟sudo -i的效果是同樣的,可是也有不一樣,sudo只是臨時擁有了root的權限,而su則是使用root帳號登陸了linux系統。
因此,咱們再來總結一下:

sudo su - 約等於 sudo -i

sudo -s 徹底等於 sudo /bin/bash 約等於 sudo su
sudo 終究被一個"臨時權限的帽子"扣住,不能等價於純粹的登陸到系統裏。

限制root遠程登陸

直接登陸root用戶是有必定的危險性的。

作一個user aliase 

#visudo

# vi /etc/ssh/sshd_config 限制root遠程登陸
進入後用/Root去搜索

把#號去掉,把PermitRootLogin yes改爲no

:wq保存,重啓一下服務

用普通用戶去登錄一下。

相關文章
相關標籤/搜索