需求
- 一臺(tái)服務(wù)器安裝各種五花八門的應(yīng)用?
- 前端、后端、架構(gòu)、DBA、運(yùn)維都要維護(hù)服務(wù)器?
- 人員經(jīng)常變動(dòng),服務(wù)器密碼總需要修改?
經(jīng)歷過以上痛苦的小伙伴們一定都在想,如何才能保障安全的前提下,又方便管理呢,以下介紹一個(gè)方案供各位小伙伴參考。
方案規(guī)劃:在linux中規(guī)劃運(yùn)維用戶組,運(yùn)維人員每人都使用自己的賬號(hào),相關(guān)管理權(quán)限的用戶劃分到同一個(gè)組中。
以下以一個(gè)WEB業(yè)務(wù)運(yùn)維組(www)以及一個(gè)運(yùn)維用戶(www)為例。
環(huán)境
硬件
| 配置 | 測(cè)試配置 |
|---|---|
| CPU | 1.8GHz |
| 內(nèi)存 | 4GB |
| 核心 | 4核 |
| 帶寬 | 1000Mb |
軟件
- VMware? Workstation 16 Pro 16.1.1 build-17801498
- CentOS Linux release 7.6.1810 (Core)
權(quán)限規(guī)劃
服務(wù)管理
/usr/bin/systemctl start
/usr/bin/systemctl stop
/usr/bin/systemctl reload
/usr/bin/systemctl restart
/usr/bin/systemctl status
/usr/bin/systemctl enable
/usr/bin/systemctl disable
進(jìn)程管理
/usr/bin/nice
/usr/bin/kill
/usr/bin/killall
/usr/bin/ps
/usr/bin/top
網(wǎng)絡(luò)管理
/usr/sbin/route
/usr/sbin/ifconfig
/usr/bin/netstat
存儲(chǔ)管理
/usr/sbin/fdisk
/usr/sbin/sfdisk
/usr/sbin/parted
/usr/sbin/partprobe
/usr/bin/mount
/usr/bin/umount
關(guān)閉系統(tǒng)
/usr/sbin/reboot
/usr/sbin/halt
/usr/sbin/poweroff
軟件管理
/usr/bin/rpm
/usr/bin/yum
/usr/local/bin/pip
/usr/local/bin/pip3
文件管理
/usr/bin/ls
/usr/bin/cat
其它
根據(jù)實(shí)際情況維護(hù)維護(hù)人員需要執(zhí)行的其它命令。
visudo
介紹
sudo的配置文檔是/etc/sudoers ,我們能夠用他的專用編輯工具visudo對(duì)其進(jìn)行編輯 ,此工具的好處是在添加規(guī)則有誤時(shí),保存退出會(huì)提示給我們錯(cuò)誤信息。
如果沒有visudo命令 需要 yum install -y sudo
配置完畢后,如果想知道哪些命令是您當(dāng)前用戶能夠執(zhí)行或禁止的,可通過切換至該用戶身份,然后通過sudo -l 查看。
配置文件介紹
默認(rèn)內(nèi)容
## Sudoers allows particular users to run various commands as
## the root user, without needing the root password.
##
## Examples are provided at the bottom of the file for collections
## of related commands, which can then be delegated out to particular
## users or groups.
##
## This file must be edited with the 'visudo' command.
## Host Aliases
## Groups of machines. You may prefer to use hostnames (perhaps using
## wildcards for entire domains) or IP addresses instead.
# Host_Alias FILESERVERS = fs1, fs2
# Host_Alias MAILSERVERS = smtp, smtp2
## 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
## Command Aliases
## These are groups of related commands...
## 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
## Installation and management of software
# Cmnd_Alias SOFTWARE = /bin/rpm, /usr/bin/up2date, /usr/bin/yum
## Services
# Cmnd_Alias SERVICES = /sbin/service, /sbin/chkconfig, /usr/bin/systemctl start, /usr/bin/systemctl stop, /usr/bin/systemctl reload, /usr/bin/systemctl restart, /usr/bin/systemctl status, /usr/bin/systemctl enable, /usr/bin/systemctl disable
## Updating the locate database
# Cmnd_Alias LOCATE = /usr/bin/updatedb
## Storage
# Cmnd_Alias STORAGE = /sbin/fdisk, /sbin/sfdisk, /sbin/parted, /sbin/partprobe, /bin/mount, /bin/umount
## Delegating permissions
# Cmnd_Alias DELEGATING = /usr/sbin/visudo, /bin/chown, /bin/chmod, /bin/chgrp
## Processes
# Cmnd_Alias PROCESSES = /bin/nice, /bin/kill, /usr/bin/kill, /usr/bin/killall
## Drivers
# Cmnd_Alias DRIVERS = /sbin/modprobe
# Defaults specification
#
# Refuse to run if unable to disable echo on the tty.
#
別名類型包括如下四種
- Host_Alias 定義主機(jī)名別名;
- User_Alias 用戶別名,別名成員能夠是用戶,用戶組(前面要加%號(hào))
- Runas_Alias 用來定義runas別名,這個(gè)別名指定的是“目的用戶”,即sudo 允許轉(zhuǎn)換至的用戶;
- Cmnd_Alias 定義命令別名。
需要注意的是:
- 在每一種Alias后面定義的別名 NAME 可以是包含大寫字母、下劃線、數(shù)字,但必須以一個(gè)大寫字母開頭。
- 配置文件中的 Default env_reset 表示重置(就是去除)用戶定義的環(huán)境變量,也就是說,當(dāng)你用sudo執(zhí)行一個(gè)命令的時(shí)候,你當(dāng)前用戶設(shè)置的所有環(huán)境變量都是無效的。
放權(quán)格式
授權(quán)用戶/組 主機(jī)名(允許轉(zhuǎn)換至的用戶) NOPASSWD:命令動(dòng)作
- 粗體標(biāo)注的三個(gè)要素缺一不可,但在動(dòng)作之前也能夠指定轉(zhuǎn)換到特定用戶下,在這里指定轉(zhuǎn)換的用戶要用( )號(hào)括起來,
- 假如無需密碼直接運(yùn)行命令的,應(yīng)該加NOPASSWD:參數(shù),不需要時(shí)方可省略,下面介紹中會(huì)有NOPASSWD的使用示例。
創(chuàng)建運(yùn)維用戶
groupadd www # 添加用戶組
cut -d : -f 1 /etc/group # 確認(rèn)添加結(jié)果
useradd -g www -d /home/www www # 添加用戶,指定用戶組與根目錄
cut -d : -f 1 /etc/passwd # 確認(rèn)添加結(jié)果
運(yùn)維用戶配置
# 定義用戶組
User_Alias WEB = %www
# 服務(wù)管理
Cmnd_Alias SERVICES = /usr/sbin/service, /usr/sbin/chkconfig, /usr/bin/systemctl start *, /usr/bin/systemctl stop *, /usr/bin/systemctl reload *, /usr/bin/systemctl restart *, /usr/bin/systemctl status *, /usr/bin/systemctl enable *, /usr/bin/systemctl disable *
# 進(jìn)程管理
Cmnd_Alias PROCESS = /usr/bin/nice, /usr/bin/kill, /usr/bin/killall, /usr/bin/ps, /usr/bin/top
# 網(wǎng)絡(luò)管理
Cmnd_Alias NETWORKING = /usr/sbin/route, /usr/sbin/ifconfig, /usr/bin/netstat
# 存儲(chǔ)管理
Cmnd_Alias STORAGE = /usr/sbin/fdisk, /usr/sbin/sfdisk, /usr/sbin/parted, /usr/sbin/partprobe, /usr/bin/mount, /usr/bin/umount
# 關(guān)閉系統(tǒng)
Cmnd_Alias SHUTDOWN = /usr/sbin/reboot, /usr/sbin/halt, /usr/sbin/poweroff, /usr/sbin/shutdown
# 軟件管理
Cmnd_Alias SOFTWARE = /usr/bin/rpm, /usr/bin/yum, /usr/local/bin/pip, /usr/local/bin/pip3
# 文件管理
Cmnd_Alias FILE = /usr/bin/ls, /usr/bin/cat
# 其他
Cmnd_Alias NGINX = /usr/local/nginx/sbin/nginx, /usr/local/nginx/sbin/nginx -s *, /usr/local/nginx/sbin/nginx -t
# 規(guī)則
WEB ALL = (root) NOPASSWD: SERVICES, PROCESS, NETWORKING, STORAGE, SHUTDOWN, SOFTWARE, FILE, NGINX
驗(yàn)證效果

驗(yàn)證服務(wù)sudo