简体   繁体   English

在ubuntu中不使用sudo的Shell脚本

[英]Shell script without using sudo in ubuntu

Totally new to shell script's and I have a script that execute the following command in order to export my database from Vagrant : 这完全是Shell脚本的新手,我有一个脚本可以执行以下命令,以便从Vagrant导出数据库:

export_folder="/var/www/projects/DatabaseBackup/local"

schema="wp"

tables="wp_8_options"

export_data="mysqldump -uroot -pmy-secret-password --opt $schema $tables > $file_name.sql"

sudo vagrant ssh --command "cd $export_folder && $export_data"

$export_folder : Is the path inside Vagrant, that corresponding on my host computer folder $export_folder是Vagrant内部的路径,与我的主机文件夹中的路径相对应

$scema : The database to export from Vagrant mySql server $scema要从Vagrant mySql服务器导出的数据库

$tables : The tables to export from the selected schema $tables要从选定模式导出的表

$export_data : The actual mysqldump command that exporting the data $export_data导出数据的实际mysqldump命令

sudo vagrant ssh --command "..." : The command that instruct the Vagrant to use the SSH in order to execute another command inside the Vagrant VM . sudo vagrant ssh --command "..."指示Vagrant使用SSH以便在Vagrant VM中执行另一个命令的命令

All the code above, run's from a shell script and until now it works properly. 上面的所有代码都是从Shell脚本运行的,直到现在它可以正常工作。 The reason I have write this script is to automate the database exporting when ever I commit something on my Git by using the Git hooks. 我之所以写这个剧本是自动化数据库当过我承诺我的东西出口Git使用Git挂钩。

The problem with the above script, is that I cannot run it via the Git hooks, because it is using sudo . 上面脚本的问题是,我无法通过Git钩子运行它,因为它正在使用sudo

Unfortunatelly the Vagrant requires sudo on my computer in order to get run. 不幸的是, Vagrant需要在我的计算机上安装sudo才能运行。 Also, you should know, that I am not a Vagrant expert, and the Vagrant got installed on my computer by another coworker, so I don't know how to modify it in order to run without sudo . 另外,您应该知道,我不是Vagrant专家,并且另一个同事在我的计算机上安装了Vagrant ,所以我不知道如何修改它才能在没有sudo情况下运行。

Additionally, I don't really care about this script security because will only run from inside my computer. 另外,我不太在乎此脚本的安全性,因为它只能在计算机内部运行。

So the question is, how to run this script, without using the sudo command. 所以问题是,如何在不使用sudo命令的情况下运行此脚本。 What are the options for that ? 有哪些选择?

Just give yourself power to sudo vagrant without a password: 只需赋予自己sudo vagrant权限即可,而无需输入密码:

Your /etc/sudoers file will have to include the line 您的/ etc / sudoers文件必须包含以下行

#includedir /etc/sudoers.d

you must use the visudo command to edit /etc/sudoers -- do not edit it manually. 必须使用visudo命令来编辑/ etc / sudoers- 请勿手动对其进行编辑。

Then, sudo visudo -f /etc/sudoers.d/<username> and add to that file 然后, sudo visudo -f /etc/sudoers.d/<username>并添加到该文件

<username> <hostname> = NOPASSWD: /usr/bin/vagrant

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM