我的默认远程 shell 是个麻烦制造者吗?

Tim*_*Tim 6 ssh

通过finger tim在服务器上,我了解到我的默认 shell 是一些脚本,/usr/local/bin/bash-wrapper其内容是:

#!/bin/bash

USERNAME=`whoami`

if ! grep ^$USERNAME$ /etc/domain-users > /dev/null; then
   echo -e "You are not authorized to log into this server\n\n"
elif test -z "$2"; then
   echo -e "User authorized"
   /bin/bash
else #in case users are trying to send a command via ssh or use scp
   $2
fi
Run Code Online (Sandbox Code Playgroud)

我现在明白脚本是我之前在使用 ssh 登录时被踢出的原因。问题是现在没有了,因为我最近刚添加到该文件/etc/domain-users,其grep在搜索。

现在我的问题是:

  1. 如果我发送命令让服务器 shell 在与 相同的行中运行,脚本会做什么ssh,例如以下两个问题 Part 2 和 Part 3?

  2. 是不是我的公钥副本失败的原因:

    $ cat /home/tim/.ssh/id_rsa.pub | ssh tim@server 'cat >> .ssh/authorized_keys'
    Password: 
    cat: >>: No such file or directory
    cat: .ssh/authorized_keys: No such file or directory
    
    Run Code Online (Sandbox Code Playgroud)

    我也试过ssh-copy-id,但它也不起作用:

    $ ssh-copy-id tim@server
    Password: 
    /usr/local/bin/bash-wrapper: line 11: umask: 077;: octal number out of range
    
    Run Code Online (Sandbox Code Playgroud)
  3. 以下 ssh-tunneling 命令在我的本地也不能很好地运行:

    ssh -f -D 9999 tim@server "if [ -f ~/.tunnel ]; then rm ~/.tunnel; fi; while [ ! -f ~/.tunnel ]; do echo > /dev/null; done"
    
    Run Code Online (Sandbox Code Playgroud)

    这是我得到的错误

    /usr/local/bin/bash-wrapper: line 11: if: command not found
    
    Run Code Online (Sandbox Code Playgroud)

    我想知道为什么?

  4. 我的登录总是很慢。是因为grep需要时间吗?请注意,文件/etc/domain-usersgrep的搜索有199线,每一行是一个用户名。它应该花很长时间grep才能完成它的工作吗?

  5. 如何更改我的默认外壳?

感谢致敬!

dr *_*bob 6

遍历一个 200 行的文件应该需要几毫秒左右,所以我会怀疑它。

试着ssh -vvv tim@server看看是什么让它变慢了。

您的默认 shell 保存在/etc/passwd您行的末尾:

drjimbob:x:1000:1000:jim bob,,,:/home/drjimbob:/bin/bash
Run Code Online (Sandbox Code Playgroud)

并且可以通过chsh tim -s /bin/bash(change shell)进行更改,尽管您通常必须具有 root 权限才能更改默认 shell,如果您拥有用户的密码(您不需要 root 权限),则可以这样做。

请注意,这/usr/bin/ssh-copy-id只是一个脚本,让您陷入困境的行是:

{ eval "$GET_ID" ; } | ssh ${1%:} "umask 077; test -d .ssh || mkdir .ssh ; cat >> .ssh/authorized_keys" || exit 1
Run Code Online (Sandbox Code Playgroud)

如果你测试这个;使用像 test_script.sh 这样的脚本

#!/bin/bash

umask 077; echo "Works fine"
$2
Run Code Online (Sandbox Code Playgroud)

并运行它 bash -v -x test_script.sh "ignored_first_parameter" "umask 077; echo 'no'"

您将看到$IFS变量“umask 077; echo no”的 bash 标记化(基于环境变量)到第二个参数中,然后 shell 扩展告诉 bash 运行umask '077;'给出错误的命令。

这基本上是因为输入 bashcommand1; command2与设置变量some_var='command1; command2"然后运行$some_var.

请参阅:http : //www.gnu.org/software/bash/manual/bashref.html


编辑:解决您的具体问题;

(1) 我仍然认为这种访问控制方法是可疑的,如果用户具有其他类型的访问权限,则可能会被一种标准技巧所规避。假设他们可以通过 ftp 访问他们的用户目录;他们可能会上传一个允许他们登录的 .login 文件(包含一个假的 $PATH 和一个假的 whoami)。

至少脚本应该指定whoami(在我的 ubuntu 机器上它的完整路径/usr/bin/whoami;在不同的系统上可能会有所不同)。请参阅:Linux Ch 5.2 的安全编程

为无法登录的用户禁用 shell 似乎更干净(例如,将 shell 设置为不在列表/bin/false/etc/passwd的用户)。或者(或另外),如果您只担心远程登录,可以使用设置/etc/ssh/sshd_config来使用AllowUsers/AllowGroups将用户/组列入白名单或DenyUsers/DenyGroups将用户/组列入黑名单到 ssh 的某种组合(但请注意,如果这些用户具有物理权限,则可以登录)访问机器)。

但是,如果您的主管修改了他们的脚本以使用eval $2它应该可以解决 ssh-copy-id 的直接问题。请参阅Bash:为什么将 eval 与变量扩展一起使用

#!/bin/bash

USERNAME=`whoami`

if ! grep ^$USERNAME$ /etc/domain-users > /dev/null; then
 echo -e "You are not authorized to log into this server\n\n"
elif test -z "$2"; then
 echo -e "User authorized"
 /bin/bash
else #in case users are trying to send a command via ssh or use scp
 eval $2
fi
Run Code Online (Sandbox Code Playgroud)

对于问题 2-5;答案,(2)是,(3)是,(4)否,(5)chsh tim -s /bin/bash