Linux-Bash登录到一台机器,然后成为root用户
我正试图与我的普通用户一起登录Linux box,然后使用bash脚本成为root用户。我的用户位于sudoers文件中,因此它可以成为root用户 正常过程如下:Linux-Bash登录到一台机器,然后成为root用户,bash,ssh,sudo,sshpass,Bash,Ssh,Sudo,Sshpass,我正试图与我的普通用户一起登录Linux box,然后使用bash脚本成为root用户。我的用户位于sudoers文件中,因此它可以成为root用户 正常过程如下: $ssh regularuser@machine regularuser@machine's password: [regularuser@machine ~]$ [regularuser@machine ~]$ sudo su - [sudo] password for regularuser: [root@machine ~]$
$ssh regularuser@machine
regularuser@machine's password:
[regularuser@machine ~]$
[regularuser@machine ~]$ sudo su -
[sudo] password for regularuser:
[root@machine ~]$
我到处寻找来做这件事,但我能找到的并不能完全满足我的需要。我也试过用#/usr/bin/expect,但它不起作用
===========================================================================
我的工作bash(access.sh)如下所示,但它在发出“sudo su-”命令后要求我输入密码:
#!/bin/bash
machine=$1
ssh -t regularuser@$machine "sudo su -"
$ sh access.sh machine
regularuser@machine's password:
[sudo] password for regularuser:
[root@machine ~]# exit
logout
Connection to machine closed.
===========================================================================
尝试#1-当我尝试以下操作时,我得到“/usr/bin/expect:没有这样的文件或目录”
尝试#2-相同的结果
#!/bin/bash
machine=$1
echo "Password?"
read -s password
/usr/bin/expect << EOF
spawn ssh regularuser@$machine
expect "Password: "
send "${password}\r"
EOF
我也无法执行“sshpass-p”$password“ssh root@$machine”,因为我的普通用户的密码与root用户的不同
我几乎什么都试过了,但没有成功
有什么想法吗 有趣的是,您将
sshpass
打开sshpass
旨在绕过ssh
要求以交互方式提供密码的要求。您所面临的问题,以及expect
不起作用的原因,是sudo
出于完全相同的原因,使用了完全相同的技巧
潜在的解决方案,按我推荐的数量降序排列:
sudo
进行身份验证sshpass
在sudo
提示符上执行其常规技巧。我还没有尝试过,但它可能很简单,只需执行sshpass-p password sudo something
。如果没有,我似乎记得最新版本的sshpass
有一种方法可以更改预期的密码提示(类似于expect
正在做的事情)。sshpass
svn中的最新提交似乎就是它,但我相当确定我没有真正发布具有此功能的版本sudo
一起使用,并包含密码提示说明符以提供帮助。仍然不推荐这种解决方案
:- you can try these commands in your linux terminal --:
如果你知道密码
如果您不知道[记住/设置]密码
为什么使用密码而不是ssh密钥?默认情况下,sshd允许root登录,因此您可以在该计算机上设置ssh密钥,并将其添加到
ssh.config
或使用-i
开关。除非,像我一样,你阻止了任何通过SSH的root登录。恐怕这是要求,root登录是不允许的。不必使用ssh密钥。感谢您的评论。恐怕我什么也做不了。
#!/bin/bash
machine=$1
echo "Password?"
read -s password
ssh -t regularuser@$machine "echo $password | sudo -S su -"
$ sh access.sh machine
Password?
regularuser@machine's password:
bash: -c: line 0: unexpected EOF while looking for matching `''
bash: -c: line 1: syntax error: unexpected end of file
Connection to vm-all-all-ad-test-packages closed.
:- you can try these commands in your linux terminal --:
$ su
OR
$ su -
$ sudo su
OR
$ sudo su -