ubuntu sudo自动切换root 无需输入密码

一、

echo password | sudo su



二、

个人PC 用ubuntu12.04 lts版  在终端操作的时候切换root 每次都需要输入密码, 这个是个很烦人的事情, 在网上找发现了一个更好的方法  通过执行脚本 自动切换root.

这个使用的软件是expect.

sudo apt-get install expect

然后开始写我们的代码.

vi changeRoot.sh

#!/usr/bin/expect  -f  

spawn sudo -s   
send "123456\r"       
interact

这个的密码是123456, 可以根据个人修改.

给文件加可执行权限

chmod +x changeRoot.sh
./changeRoot.sh   //执行这个 切换root.

每次执行./changRoot.sh 也挺麻烦的 ,可以下在快捷命令里.

.bashrc 文件里最后一行填写以下代码

alias ss='./home/geiao/changeRoot.sh';
source .bashrc

下次在终端 直接 ss 回车 就切换root 了  so easy!  

 

另外提供自动ssh连接的方法:

复制代码
autossh.sh:

#!/usr/bin/expect -f  

 set user [lindex $argv 0 ]
 set ip [lindex $argv 1 ] 
 set password [lindex $argv 2 ]               
 spawn ssh $user@$ip
 expect {
 "*yes/no" { send "yes\r"; exp_continue}
 "*password:" { send "$password\r" }
 }
 interact

执行./autossh.sh username ip password

如果主机IP固定 可以个性定制:

#!/usr/bin/expect -f 

 set ip 192.168.3.29  
 set password 123456    
 spawn ssh jghost@$ip  
 expect {  
 "*yes/no" { send "yes\r"; exp_continue}  
 "*password:" { send "$password\r" }  
 }  
 interact

 也可以将这写执行的命令添加到.bashrc中  快捷执行.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值