Linux 我们可以写一个简单的bash脚本来自动以root用户身份登录吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8015976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 07:02:39  来源:igfitidea点击:

can we write a simple bash script to automatically login as root user

linuxunixubuntu-10.04ubuntu-11.04ubuntu-10.10

提问by forum.test17

I usually have to login in 20 to 50 times daily as a super user, typing the long password again and again..

我通常每天必须以超级用户身份登录 20 到 50 次,一遍又一遍地输入长密码。

i have just created a simple bash script

我刚刚创建了一个简单的 bash 脚本

#!/bin/bash

sudo -s
echo password

./test

。/测试

output root@localhost: password when i execute it, it works like charm... but it shows my password on the screen.....

输出 root@localhost: password 当我执行它时,它就像魅力一样......但它在屏幕上显示了我的密码......

do some one have any other best solution...... for this small problem.......

有人有其他最佳解决方案吗......对于这个小问题......

i hope this is not all the solution in security standard...... can we have any other solution with out exposing my password.....

我希望这不是安全标准中的全部解决方案......我们可以有任何其他解决方案而不暴露我的密码......

采纳答案by Jens

You can configure sudoto require a password only every so many minutes. The default is 5 minutes. Read the sudoersman page and scroll down to this:

您可以配置sudo为每隔几分钟就要求输入密码。默认值为 5 分钟。阅读sudoers手册页并向下滚动到:

    timestamp_timeout
               Number of minutes that can elapse before sudo will ask
               for a passwd again.  The timeout may include a
               fractional component if minute granularity is
               insufficient, for example 2.5.  The default is 5.  Set
               this to 0 to always prompt for a password.  If set to a
               value less than 0 the user's timestamp will never
               expire.  This can be used to allow users to create or
               delete their own timestamps via sudo -v and sudo -k
               respectively.

回答by SuperTron

You can pipe the echo'd password into a command. Try something like this:

您可以将 echo 的密码通过管道传输到命令中。尝试这样的事情:

echo myPassword | sudo -S 

You can see come more info on this here.

你可以在这里看到更多信息。

Question is, do you REALLY want your password in a shell script file? (just emphasizing that its a terrible idea)

问题是,您真的希望在 shell 脚本文件中输入密码吗?(只是强调这是一个可怕的想法)

回答by Mike

Is there a reason that you can't sudo su -to just become the root user instead of prepending all of your commands with sudo blah?

是否有理由让您不能sudo su -成为 root 用户而不是在所有命令前加上sudo blah

回答by Benjamin Jeske

just change ownership of the script to root & set SUID-Bit in user the permissions

只需将脚本的所有权更改为 root 并在用户中设置 SUID-Bit 权限

chmod u=rws g+x o+x script123

chmod u=rws g+x o+x script123

the script will run as root for every user

该脚本将以 root 身份为每个用户运行

回答by forum.test17

simple solution is to use key base authentication Use ssh-copy-id instead following from this tutorialwhich is secure

简单的解决方案是使用密钥库身份验证使用 ssh-copy-id 而不是遵循本教程,这是安全的