Home > Software engineering >  How to run a sudo command silently on the server?
How to run a sudo command silently on the server?

Time:05-29

I need to execute this command on Linux server.

string command = $"sudo iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport {port} -j ACCEPT";

When I run the app in VirtulBox, the terminal asks me for a password. Will this also happen on the server? I can't login to the server and don't know the password. How can I run the command so that it does not ask for password?

CodePudding user response:

If you whant to do this from Linux machine: You can writ a script with:

sshpass -p "PASSWORD" user@server

and give only execute premition.

CodePudding user response:

Login to the terminal as root. Make a backup of your /etc/sudoers file.
# cp /etc/sudoers /root/sudoers.bak

Then edit this file by using the visudo command:
# visudo

Edit or append this line, replacing username with the user that will be running your script:
username ALL = NOPASSWD: /usr/sbin/iptables

Save and exit the file.
Test it by executing sudo, from your user account:
$ sudo iptables -V

Now your user can use sudo to execute the iptables command.

  • Related