-2

I want to run the following script:

#!/bin/bash

if grep -qs 'MinecraftServer' /media/kiancross/MinecraftServer/MinecraftServer; then
    echo "Disk already mounted, server boot will continue"
else
     mount /dev/sda1 /media/kiancross/MinecraftServer
fi

sudo mount /dev/sda1 /media/kiancross/MinecraftServer
cd /media/kiancross/MinecraftServer/MinecraftServer/1.6.4
screen -d -m -S MinecraftServer ./start.sh
screen -r MinecraftServer   

My problem is, is that mount requires sudo and I want people who are not administrators to be able to run this script. When I run the script, it says mount: only root can do that then it asks me for my administrator password.

I followed instructions on other forums that recommended you go to sudo visudo then add the following line of code: myusername ALL = (root) NOPASSWD: /path/to/my/program. After trying this, even with a restart I was still asked to ewnter my password. I also tried changing the owner of the file to root by following the instructions in this post.

After doing that, I was still unable to run it without a password.

I am running Ubuntu 14.04.

  • What is the exact line that you added to your sudo rules? – Jenny D Jan 25 '15 at 18:55
  • @JennyD The exact line I added to my sudo rules was kiancross ALL = (root) NOPASSWD: /kiancross/bin/ – user100599 Jan 25 '15 at 18:57
  • One answer coming up, in a few minutes... – Jenny D Jan 25 '15 at 18:58
0

There are two ways that you can do this.

1: Using sudo

One is to allow everyone to use sudo to mount the disk. The sudo line should look like this:

ALL ALL = NOPASSWD: mount /dev/sda1 /media/kiancross/MinecraftServer

The first ALL means "all users"; the second means "on any server where this sudoers file is installed". You also need to have the full path to the actual command, not just the directory.

If you do that, you also need to change the script so that the mount command is always run using with sudo, like this:

 sudo mount /dev/sda1 /media/kiancross/MinecraftServer

2: Using mount options

Another way is to change the mount options for /dev/sda1 so that it can be mounted by any user - that way you won't need to use sudo at all. To do this, change your /etc/fstab so that the line with that disk reads something like

/dev/sda1 /media/kiancross/MinecraftServer YOURFSTYPE users,exec

Where it says YOURFSTYPE you should of course write what filesystemtype your disk is - probably ext4.

The alternatives users,exec means that the file system can be mounted by a regular user, and that programs on it can be executed. You have to specify this because usually if a file system is mounted by someone other than root, files on it won't be executed.

  • After adding the line in option 1 I am told that there is a syntax error. Also, I followed the instructions in option 2 and it didn't work for me, it still asked for the password. – user100599 Jan 25 '15 at 19:32
  • I changed the sudoers line, try again. – Jenny D Jan 25 '15 at 19:53
  • Nope, still getting the syntax error. Can I just confirm I am putting it in the right place? I am using sudo visudo then putting it in the file that opens? – user100599 Jan 25 '15 at 19:59
  • @JennyD IIRC user-mounting requires mount to be setuid. – peterph Jan 25 '15 at 19:59
  • @peterph What does that mean. How can the code be corrected? – user100599 Jan 25 '15 at 20:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy