3.7 su命令 3.8 sudo命令 3.9 限制root远程登录
2019獨角獸企業重金招聘Python工程師標準>>>
mkpass ?解釋:make password?
[root@cham2 ~]# mkpasswd ? ?make password ?^C
[root@cham2 ~]# mkpasswd ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 直接輸入不行的
-bash: mkpasswd: 未找到命令
[root@cham2 ~]# yum install -y expect ? ? ? ? ? ? ? 要安裝一個包。
已加載插件:fastestmirror
?
隨手生成一個密碼
#mkpasswd
指定密碼長度12位
#mkpasswd -l 12
還可以指定3個特殊符號
#mkpasswd -l 12 -s 3
也可以不要特殊符號,只要小些大寫
#mkpasswd -l 12 -s 0
?
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 /CDROMsudo -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保存,重啟一下服務
用普通用戶去登陸一下。
轉載于:https://my.oschina.net/u/3708120/blog/1789585
總結
以上是生活随笔為你收集整理的3.7 su命令 3.8 sudo命令 3.9 限制root远程登录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#静态构造函数
- 下一篇: Exception in thread