1. 程式人生 > >三周第三次課(2月7日)

三周第三次課(2月7日)

及其 別名 title mii-tool pan -c ldap str 命令

3.7 su命令

3.8 sudo命令

3.9 限制root遠程登錄



3.7 su命令


#su 切換用戶

#su - aming 加“-”是方便徹底切換用戶,包括環境變量,用戶配置參數等等。(大多數用“-”切換)

#su aming

$pwd

/root 不加“-”,切換用戶會在/root下


*當然 也可以在su的時候 以某用戶的身份去執行一條命令

例如 以aming用戶的身份去執行一條命令,不登錄到aming用戶下面去,也可以執行。

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

#ls -lt /tmp/ |head

#-rw-r--r--. 1 aming grp2 aming.111

aming的組為grp2是因為本來就屬於grp2組,可以從#id aming的參數看出。

#id aming

uid=1000(aming) gid=1005(grp2) 組=1005(grp2),1007(user5)


#su - user5

密碼:

鑒定故障。

(此處是因為user5之前做測試被寫入了鎖定參數,修改密碼可以登錄 )

$登出(ctrl-d)

#passwd user5

#su - aming

#su - user5

密碼:

.

.

su:警告:無法更改到/home/user5 目錄:沒有那個文件或目錄

-bash-4.2$ pwd

/home/aming此處是因為當時建立user5的時候沒有創建家目錄,沒有用戶的加載文件加載,導致前綴變成-bash-4.2$。

創建家目錄是沒法改變-bash-4.2.


改變方法是將/etc/skel的.bash_logout .bash_profile .bashrc 復制到user5的家目錄下,並且將其目錄及其子目錄、文件都修改user5的所有者跟所屬組。

#ls -la /etc/skel

#cp /etc/skel/.bash* /home/user5/

#chown -R user5:user5 !$

#su -user5

#pwd

/home/user5登錄成功



3.8 sudo命令


可以讓普通用戶臨時去執行一條命令,以指定用戶的身份去執行。通常是給普通用戶授權,以root的身份。以防普通用戶利用root密碼修改配置等等操作。


#visudo 可以打開打開sudo的配置文件

(*此處是最核心的一段語句參數)

## Allow root to run any commands anywhere

root ALL=(ALL) ALL

意思即:允許root用戶在任何地方去運行任何命令


test1

讓aming用戶用root身份去執行ls,mv,cat命令。

#visudo

## Allow root to run any commands anywhere

root ALL=(ALL) ALL

amingALL=(ALL)ls,mv,cat

:wq

退出,保存。


這裏發現有語法錯誤(命令應該寫絕對路徑參數),其實這裏是visudo的語法檢測功能作用。

# visudo

>>> /etc/sudoers: 語法錯誤 near line 93 <<<

現在做什麽?^[

選項有:

重新編輯 sudoers 文件(e)

退出,不保存對 sudoers 文件的更改(x)

退出並將更改保存到 sudoers 文件(危險!)(Q)


按e重新編輯

## Allow root to run any commands anywhere

92 root ALL=(ALL) ALL

93 aming ALL=(ALL) /usr/bin/ls,/usr/bin/mv,/usr/bin/cat


#su -aming

$ls /root

ls: 無法打開目錄/root: 權限不夠


用sudo 去ls /root,輸入當前用戶密碼(密碼只需要輸入一次),成功。

$ sudo /usr/bin/ls /root/


我們信任您已經從系統管理員那裏了解了日常註意事項。

總結起來無外乎這三點:


#1) 尊重別人的隱私。

#2) 輸入前要先考慮(後果和風險)。

#3) 權力越大,責任越大。


[sudo] aming 的密碼:

1.txt 1.xtx 2.txt a_(2).txtanaconda-ks.cfg anaconda-ks.cfg.1 a.txt temp.1


test2

添加user5進去,讓user5免密使用sudo命令

#visudo


## Allow root to run any commands anywhere

root ALL=(ALL) ALL

aming ALL=(ALL) /usr/bin/ls,/usr/bin/mv,/usr/bin/cat

user5 ALL=(ALL) NOPASSWD: /usr/bin/ls,/usr/bin/cat


[root@centos7 ~]# su - user5

[user5@centos7 ~]$ ls /root

ls: 無法打開目錄/root: 權限不夠

[user5@centos7 ~]$ sudo ls /root

1.txt 1.xtx 2.txt a_(2).txtanaconda-ks.cfg anaconda-ks.cfg.1 a.txt temp.1

結果表示,使用sudo,成功免密


設置命令別名

#visudo

## Networking

# Cmnd_Alias NETWORKING = /sbin/route, /sbin/ifconfig, /bin/ping, /sbin/dhclient, /usr/bin/net, /sbin/iptables, /usr/bin/rfcomm, /usr/bin/wvdial, /sbin/iwconfig, /sbin/mii-tool

Cmnd_Alias AMING_CMD = /usr/bin/ls,/usr/bin/mv,/usr/bin/cat


.

.

..


## Allow root to run any commands anywhere

root ALL=(ALL) ALL

aming ALL=(ALL) AMING_CMD

user5 ALL=(ALL) NOPASSWD: /usr/bin/ls,/usr/bin/cat


修改完畢後,su aming ,sudo 命令測試

[aming@centos7 ~]$ sudo ls /root

[sudo] aming 的密碼:

1.txt 1.xtx 2.txt a_(2).txtanaconda-ks.cfg anaconda-ks.cfg.1 a.txt temp.1

[aming@centos7 ~]$ sudo cat /root

cat: /root: 是一個目錄

[aming@centos7 ~]$ sudo cat /root/a.txt

111

222

333

44

55

sudo命令無問題,visudo參數修改成功。


針對一個組進行限制。(修改此命令參數)

## Allows people in group wheel to run all commands

%wheel ALL=(ALL) ALL



3.9限制root遠程登錄


免密登錄root

#visudo 修改user的別名為AMINGS,然後添加用戶

## User Aliases

## These aren't often necessary, as you can use regular groups

## (ie, from files, LDAP, NIS, etc) in this file - just use %groupname

## rather than USERALIAS

# User_Alias ADMINS = jsmith, mikem

User_Alias AMINGS = aming, user1, user5

修改sudo命令參數

## Allow root to run any commands anywhere

root ALL=(ALL) ALL

aming ALL=(ALL) AMING_CMD

user5 ALL=(ALL) NOPASSWD: /usr/bin/ls,/usr/bin/cat

AMINGS ALL=(ALL) NOPASSWD: /usr/bin/su

切換至普通用戶aming,#sudo su - ,成功免密切換root

[root@centos7 ~]# su - aming

上一次登錄:三 2月 7 18:41:20 CST 2018pts/0 上

[aming@centos7 ~]$ sudo su -

上一次登錄:三 2月 7 18:41:17 CST 2018從 192.168.189.1pts/0 上

[root@centos7 ~]# whoami

root



#[root@centos7 ~]# vi /etc/ssh/sshd_config

# Authentication:


#LoginGraceTime 2m

PermitRootLogin no(*原本是#PermitRootLogin yes,把#去掉,然後把yes改成no)

#StrictModes yes

#MaxAuthTries 6

#MaxSessions 10


[root@centos7 ~]# systemctl restart sshd.service修改完成後 重啟sshd服務

打開新的遠程登錄窗口,用passwd登錄失敗,說明Xshell被成功限制。

技術分享圖片

利用putty登錄普通用戶aming

技術分享圖片

#sudo su - 嘗試在putty免密登錄root

技術分享圖片

putty成功登錄。


三周第三次課(2月7日)