sudosh

Sudosh is designed specifically to be used in conjunction with sudo or by itself as a login shell. Sudosh allows the execution of a root or user shell with logging. Every command the user types within the root shell is logged as well as the output.

This is different from "sudo -s" or "sudo /bin/sh", because when you use one of these instead of sudosh to start a new shell, then this new shell does not log commands typed in the new shell to syslog; only the fact that a new shell started is logged.

If this newly started shell supports commandline history, then you can still find the commands called in the shell in a file such as .sh_history, but if you use a shell such as csh that does not support command-line logging you are out of luck.

Sudosh fills this gap. No matter what shell you use, all of the command lines are logged to syslog (including vi keystrokes). In fact, sudosh uses the script command to log all key strokes and output.

Setting up sudosh is fairly easy. For a Linux system, first download the RPM of sudosh, for example fromrpm.pbone.net. Then install it on your Linux server:

# rpm -ihv sudosh-1.8.2-1.2.el4.rf.i386.rpm
Preparing... ########################################### [100%]
1:sudosh ########################################### [100%]

Then, go to the /etc file system and open up /etc/sudosh.conf. Here you can adjust the default shell that is started, and the location of the log files. Default, the log directory is /var/log/sudosh. Make sure this directory exists on your server, or change it to another existing directory in the sudosh.conf file. This command will set the correct authorizations on the log directory:

# sudosh -i
[info]: chmod 0733 directory /var/log/sudosh

Then, if you want to assign a user sudosh access, edit the /etc/sudoers file by running visudo, and add the following line:

username ALL=PASSWD:/usr/bin/sudosh

Now, the user can login, and run the following command to gain root access:

$ sudo sudosh
Password:
# whoami
root

Now, as a sys admin, you can view the log files created in /var/log/sudosh, but it is much cooler to use thesudosh-replaycommand to replay (like a VCR) the actual session, as run by the user with the sudosh access.

First, run sudosh-replay without any paramaters, to get a list of sessions that took place using sudosh:

# sudosh-replay
Date Duration From To ID
==== ======== ==== == ==
09/16/2010 6s root root root-root-1284653707-GCw26NSq

Usage: sudosh-replay ID [MULTIPLIER] [MAXWAIT]
See 'sudosh-replay -h' for more help.
Example: sudosh-replay root-root-1284653707-GCw26NSq 1 2

Now, you can actually replay the session, by (for example) running:

# sudosh-replay root-root-1284653707-GCw26NSq 1 5

The first paramtere is the session-ID, the second parameter is the multiplier. Use a higher value for multiplier to speed up the replay, while "1" is the actual speed. And the third parameter is the max-wait. Where there might have been wait times in the actual session, this parameter restricts to wait for a maximum max-wait seconds, in the example above, 5 seconds.

For AIX, you can find the necessary RPMhere. It is slightly different, because it installs in /opt/freeware/bin, and also the sudosh.conf is located in this directory. Both Linux and AIX require of course sudo to be installed, before you can install and use sudosh.

在执行需要管理员权限的 Shell 脚本时,使用 `sudo` 是常见的做法。然而,若未正确配置或使用不当,可能会遇到权限问题、脚本无法执行或安全风险等问题。以下是对相关操作的详细说明及注意事项: ### 执行需要管理员权限的 Shell 脚本 1. **赋予脚本可执行权限** 在执行脚本之前,必须确保脚本文件具有可执行权限。可以使用以下命令为脚本添加执行权限: ```bash sudo chmod +x script.sh ``` 这样可以确保脚本能够被 `sudo` 执行[^1]。 2. **使用 `sudo` 执行脚本** 在命令行中使用 `sudo` 直接运行脚本,例如: ```bash sudo ./script.sh ``` 此命令会以超级用户权限运行脚本。如果遇到 `No such file or directory` 错误,可能是因为脚本的换行符格式问题,可以使用 `dos2unix` 工具进行转换: ```bash sudo dos2unix script.sh ``` 该工具可将 Windows 格式的换行符转换为 Unix 格式,确保脚本能被正确识别和执行[^1]。 3. **避免在脚本中硬编码 `sudo`** 不建议在脚本内部直接使用 `sudo`,因为这可能导致权限问题或安全漏洞。更好的做法是在调用脚本时使用 `sudo`,以明确控制权限范围。 4. **在自动化工具中使用 `sudo`** 如果在 Jenkins、Zabbix 等自动化工具中执行需要管理员权限的脚本,需确保这些工具以适当的权限运行。例如,在 Jenkins 中,可以通过以下方式配置: - 在构建步骤中使用 `sudo` 执行命令: ```bash sudo /path/to/script.sh ``` - 确保 Jenkins 用户具有 `sudo` 权限,可以通过编辑 `/etc/sudoers` 文件实现: ```bash jenkins ALL=(ALL) NOPASSWD: ALL ``` 这样可以避免 Jenkins 在执行 `sudo` 命令时被提示输入密码[^3]。 5. **处理超时问题** 在 Zabbix 等监控系统中执行脚本时,如果脚本执行时间过长,可能会导致超时错误。可以通过调整 Zabbix 的 `Timeout` 参数来延长脚本执行的最长时间限制: ```ini Timeout=300 ``` 此配置项通常位于 Zabbix Agent 的配置文件 `zabbix_agentd.conf` 中,可将 `300` 替换为你希望的超时时间(单位为秒)[^2]。 6. **环境变量问题** 在某些情况下,脚本在 `sudo` 下运行时可能无法访问预期的环境变量。可以通过在 `sudo` 命令中显式设置环境变量来解决此问题: ```bash sudo env "PATH=$PATH" ./script.sh ``` ### 示例:执行需要管理员权限的脚本 ```bash #!/bin/bash # 示例脚本:创建一个新用户并设置密码 read -p "请输入新用户名:" username sudo useradd -m "$username" sudo passwd "$username" ``` 保存为 `create_user.sh`,并赋予执行权限: ```bash chmod +x create_user.sh ``` 然后使用 `sudo` 执行: ```bash sudo ./create_user.sh ``` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值