天天看點

Linux系統如何進行提權操作

使用者提權:

往往公司的伺服器對外都是禁止root使用者直接登入,是以我們通常使用的是普通使用者,那麼問題來了,當我們使用普通使用者執行/sbin目錄下的指令時,會發現沒有權限運作,這種情況下我們無法正常的管理伺服器,那如何才能不使用root使用者直接登入系統,同時又保證普通使用者能正常工作?

兩種方法:

1.su切換使用者,使用普通使用者登入,然後使用su指令切換到root;優點:簡單,缺點:需要讓使用者知道root密碼;

2.sudo提取,當需要使用root權限時進行提權,而無需切換至root使用者;優點:安全、友善;缺點:配置複雜;

1.su身份切換

在使用su切換前,說一下shell分類、環境變量配置檔案有哪些

1)linux shell主要分為如下幾類

- 互動式shell,等待使用者輸入執行的指令(終端操作,需要不斷提示)
- 非互動式shell,執行shell腳本,腳本執行結束後shell自動退出
- 登入shell,需要輸入使用者名和密碼才能進入shell
- 非登入shell,不需要輸入使用者和密碼就能進入shell,比如運作bash會開啟一個新的會話視窗

# 使用登入shell登入伺服器,然後輸入指令bash非登入式shell
差別于加載的環境變量不同;           

2.bash shell配置檔案介紹(檔案主要儲存使用者的工作環境)

個人配置檔案: ~/.bash_profile; ~/.bashrc

全局配置檔案:/etc/profile; /etc/profile.d/*.sh; /etc/bashrc

profile類檔案,設定環境變量,登入前運作的腳本和指令;

bashrc類檔案,設定本地變量,定義指令别名

注意: 如果全局配置和個人配置産生沖突,以個人配置為準

3.登入系統後,環境變量配置檔案的應用順序

(1)登入式shell:

/etc/profile --> /etc/profile.d/*.sh --> ~/.barh_profile  -->~/.bashrc --> /etc/bashrc           

(2)非登陸式shell:

~/.bashrc --> /etc/bashrc --> /etc/profile.d/*.sh           

驗證:

在每個shell配置檔案中添加echo指令,使用兩種登入方式驗證:

Last login: Fri Nov  6 18:09:54 2020 from 100.0.0.100
/etc/profile.d/1.sh
/etc/profile
/etc/bashrc
~/.bashrc
~/.bash_profile
[root@proxy1 ~]# 
[root@proxy1 ~]# bash
/etc/profile.d/1.sh
/etc/bashrc
~/.bashrc           
su - username    #屬于登陸式shell
su username        #屬于非登陸式shell
# 差別在于加載的環境變量不一樣           

以某個使用者的身份執行某個服務,使用su - username -c "command"

# 不會登入到使用者
[root@proxy1 ~]# su - aaa -c "pwd"
/home/aaa           

sudo提權

su指令在切換使用者身份時,如果每個普通使用者都能拿到root使用者的密碼,當其中某個使用者不小心洩露了root的密碼,那系統會變得非常不安全;

其實sudo就相當于給某個普通使用者埋下了浩克的種子,當需要執行一些進階操作時,進行發怒,但正常情況下還是普通人,還是會受到限制;

1)快速配置sudo方式

[root@proxy1 ~]# usermod aaa -G wheel
# 将aaa加入到wheel附加組中
# 而wheel組在sudo的配置檔案中定義為可以執行任何指令
[root@proxy1 ~]# grep "wheel" /etc/sudoers
## Allows people in group wheel to run all commands
%wheel  ALL=(ALL)       ALL
# %wheel        ALL=(ALL)       NOPASSWD: ALL
# 驗證
[aaa@proxy1 root]$ rm anaconda-ks.cfg
rm: cannot remove ‘anaconda-ks.cfg’: Permission denied
[aaa@proxy1 root]$ sudo rm anaconda-ks.cfg

# sudo的審計日志
/var/log/secure           

2)正常配置

visudo指令(有文法驗證)

方式一:

# 1.使用sudo定義分組,和系統的組無關
User_Alias OPS = aaa,bbb
User_Alias DBA = ccc,ddd

# 2.定義可執行的指令組,便于後續的調用
Cmnd_Alias NETWORKING = /sbin/ifconfig,/bin/ping
Cmnd_Alias SOFTWARE = /bin/rpm,/usr/bin/yum
Cmnd_Alias SERVICES = /sbin/server,/usr/bin/systemctl start

# 3.使用sudo開始配置設定權限
OPS ALL=(ALL) NETWORKING,SOFTWARE,SERVICES
DBA ALL=(ALL) SOFTWARE SERVICES

# 4.檢測配置
[root@proxy1 ~]# visudo -c
/etc/sudoers: parsed OK

# 使用者可以使用sudo -l指令檢視自己的權限           

方式二:

# 針對系統中真實存在的組進行操作
# 1.建立兩個組:OPS、DBA
[root@proxy1 ~]# groupadd OPS
[root@proxy1 ~]# groupadd DBA
# 2.将使用者的附加組修改為OPS或DBA
[root@proxy1 ~]# usermod aaa -G OPS
[root@proxy1 ~]# usermod bbb -G OPS   
[root@proxy1 ~]# usermod ccc -G DBA
[root@proxy1 ~]# usermod ddd -G DBA 
# 3.在sudo中配置規則
Cmnd_Alias NETWORKING = /sbin/ifconfig,/bin/ping
Cmnd_Alias SOFTWARE = /bin/rpm,/usr/bin/yum
Cmnd_Alias SERVICES = /sbin/server,/usr/bin/systemctl start

# 4.使用sudo開始配置設定權限
%OPS ALL=(ALL) NETWORKING,SOFTWARE,SERVICES
%DBA ALL=(ALL) SOFTWARE SERVICES

# 5.檢查配置并驗證權限
visudo -c
[ccc@proxy1 root]$ sudo -l
User ccc may run the following commands on proxy1:
    (ALL) /bin/rpm, /usr/bin/yum

# 差別在于真實的組定義時前邊需要添加%号           

繼續閱讀