使用 sudo 委派权限
默认情况下,Cumulus Linux 有两个用户帐户:root 和 cumulus。cumulus 帐户是普通用户,并且在 sudo 组中。
您可以根据需要添加更多用户帐户。与 cumulus 帐户一样,这些帐户必须使用 sudo
来执行特权命令。
sudo 基础知识
sudo
允许您以超级用户或安全策略指定的另一个用户身份执行命令。
默认安全策略是 sudoers,您可以在 /etc/sudoers
文件中配置它。使用 /etc/sudoers.d/
添加到默认 sudoers 策略。
仅使用 visudo
编辑 sudoers
文件;不要使用像 vi
或 emacs
这样的其他编辑器。
在 /etc/sudoers.d
中创建新文件时,请使用 visudo -f
。此选项在写入文件之前执行健全性检查,以避免阻止 sudo 正常工作的错误。
sudoers
文件中的错误可能导致失去提升权限到 root 的能力。您只能通过重启交换机并启动到单用户模式来修复此问题。在修改 sudoers
之前,通过为 root 用户设置密码来启用 root 用户。
默认情况下,sudo 组中的用户可以使用 sudo
执行特权命令。要将用户添加到 sudo 组,请使用 useradd(8)
或 usermod(8)
命令。要查看哪些用户属于 sudo 组,请参阅 /etc/group
(man group(5)
)。
您可以以 sudo
身份运行任何命令,包括 su
。您必须输入密码。
下面的示例演示了如何使用 sudo
作为非特权用户 cumulus 来启动接口
cumulus@switch:~$ ip link show dev swp1
3: swp1: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master br0 state DOWN mode DEFAULT qlen 500
link/ether 44:38:39:00:27:9f brd ff:ff:ff:ff:ff:ff
cumulus@switch:~$ ip link set dev swp1 up
RTNETLINK answers: Operation not permitted
cumulus@switch:~$ sudo ip link set dev swp1 up
Password:
umulus@switch:~$ ip link show dev swp1
3: swp1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br0 state UP mode DEFAULT qlen 500
link/ether 44:38:39:00:27:9f brd ff:ff:ff:ff:ff:ff
sudoers 示例
以下示例说明了如何授予用户或用户组执行所需任务所需的尽可能少的权限。每个示例都使用系统组 noc;组包括前缀 %。
当非特权用户运行命令时,该命令必须包含 sudo
前缀。
类别 | 权限 | 示例命令 | sudoers 条目 |
---|---|---|---|
监控 | 交换机端口信息 | ethtool -m swp1 | %noc ALL=(ALL) NOPASSWD:/sbin/ethtool |
监控 | 系统诊断 | cl-support | %noc ALL=(ALL) NOPASSWD:/usr/cumulus/bin/cl-support |
监控 | 路由诊断 | cl-resource-query | %noc ALL=(ALL) NOPASSWD:/usr/cumulus/bin/cl-resource-query |
映像管理 | 安装映像 | onie-select http://lab/install.bin | %noc ALL=(ALL) NOPASSWD:/usr/cumulus/bin/onie-select |
软件包管理 | 任何 apt-get 命令 | apt-get update 或 apt-get install | %noc ALL=(ALL) NOPASSWD:/usr/bin/apt-get |
软件包管理 | 仅 apt-get update | apt-get update | %noc ALL=(ALL) NOPASSWD:/usr/bin/apt-get update |
软件包管理 | 安装软件包 | apt-get install vim | %noc ALL=(ALL) NOPASSWD:/usr/bin/apt-get install * |
软件包管理 | 升级 | apt-get upgrade | %noc ALL=(ALL) NOPASSWD:/usr/bin/apt-get upgrade |
Netfilter | 安装 ACL 策略 | cl-acltool -i | %noc ALL=(ALL) NOPASSWD:/usr/cumulus/bin/cl-acltool |
Netfilter | 列出 iptables 规则 | iptables -L | %noc ALL=(ALL) NOPASSWD:/sbin/iptables |
Layer 1 和 2 | 任何 LLDP 命令 | lldpcli show neighbors / configure | %noc ALL=(ALL) NOPASSWD:/usr/sbin/lldpcli |
Layer 1 和 2 | 仅显示邻居 | lldpcli show neighbors | %noc ALL=(ALL) NOPASSWD:/usr/sbin/lldpcli show neighbors* |
接口 | 修改任何接口 | ip link set dev swp1 {up|down} | %noc ALL=(ALL) NOPASSWD:/sbin/ip link set * |
接口 | 启动任何接口 | ifup swp1 | %noc ALL=(ALL) NOPASSWD:/sbin/ifup |
接口 | 关闭任何接口 | ifdown swp1 | %noc ALL=(ALL) NOPASSWD:/sbin/ifdown |
接口 | 仅启动/关闭 swp2 | ifup swp2 / ifdown swp2 | %noc ALL=(ALL) NOPASSWD:/sbin/ifup swp2,/sbin/ifdown swp2 |
接口 | 任何 IP 地址更改 | ip addr {add|del} 192.0.2.1/30 dev swp1 | %noc ALL=(ALL) NOPASSWD:/sbin/ip addr * |
接口 | 仅设置 IP 地址 | ip addr add 192.0.2.1/30 dev swp1 | %noc ALL=(ALL) NOPASSWD:/sbin/ip addr add * |
以太网桥接 | 任何 bridge 命令 | brctl addbr br0 / brctl delif br0 swp1 | %noc ALL=(ALL) NOPASSWD:/sbin/brctl |
以太网桥接 | 添加网桥和接口 | brctl addbr br0 / brctl addif br0 swp1 | %noc ALL=(ALL) NOPASSWD:/sbin/brctl addbr *,/sbin/brctl addif * |
生成树 | 设置 STP 属性 | mstpctl setmaxage br2 20 | %noc ALL=(ALL) NOPASSWD:/sbin/mstpctl |
故障排除 | 重启 switchd | systemctl restart switchd.service | %noc ALL=(ALL) NOPASSWD:/usr/sbin/service switchd * |
故障排除 | 重启任何服务 | systemctl cron switchd.service | %noc ALL=(ALL) NOPASSWD:/usr/sbin/service |
故障排除 | 数据包捕获 | tcpdump | %noc ALL=(ALL) NOPASSWD:/usr/sbin/tcpdump |
Layer 3 | 添加静态路由 | ip route add 10.2.0.0/16 via 10.0.0.1 | %noc ALL=(ALL) NOPASSWD:/bin/ip route add * |
Layer 3 | 删除静态路由 | ip route del 10.2.0.0/16 via 10.0.0.1 | %noc ALL=(ALL) NOPASSWD:/bin/ip route del * |
Layer 3 | 任何静态路由更改 | ip route * | %noc ALL=(ALL) NOPASSWD:/bin/ip route * |
Layer 3 | 任何 iproute 命令 | ip * | %noc ALL=(ALL) NOPASSWD:/bin/ip |
Layer 3 | 非模态 OSPF | cl-ospf area 0.0.0.1 range 10.0.0.0/24 | %noc ALL=(ALL) NOPASSWD:/usr/bin/cl-ospf |