MustSee
MustSee

Reputation: 151

How to use sys/mount to mount a NFS system?

So, I am trying to connect two Ubuntu computers using a NFS connection On the server, I made the following

  1. Install NFS Server on Ubuntu

    $ sudo apt-get install nfs-kernel-server portmap

  2. Export shares over NFS

    $ sudo mkdir /opt/share $ sudo chown nobody:nogroup /opt/share

  3. Edit the NFS server exports configuration file

    $ sudo gedit /etc/exports

  4. Add the following settings

    /home 192.168.0.0/24(rw,sync,no_root_squash,no_subtree_check) /opt/share 192.168.0.0/24(rw,sync,no_subtree_check)

  5. Apply the new settings by running the following command. This will export all directories listed in /etc/exports file

    $ sudo exportfs -a

On the client side, I tried to use the sys/mount library to make the NFS connection

if(mount(":/mnt/share","/opt/share","nfs",0,"nolock,addr=192.168.0.101") == -1)
  {
    printf("NFS ERROR: mount failed: %s \n",strerror(errno));
  }
  else
  { 
    printf("NFS connected\n");
  }

But it returns

m@m-ThinkPad-L15-Gen-2:~/Desktop/teste$ sudo ./mountnfs
NFS ERROR: mount failed: Permission denied 

Does anybody have any clue of what is happening?

Upvotes: 1

Views: 741

Answers (1)

catnip
catnip

Reputation: 25388

According to the documentation:

Linux: the CAP_SYS_ADMIN capability is required to mount file systems.

And according to this page, you can add that capability to your program using something along the lines of the following command (each time after you build it, probably):

sudo setcap CAP_SYS_ADMIN+ep /path/to/your/binary

That's the gist of it anyway, but you might need to a bit more digging to find the optimal solution - in which case you can answer your own question.

Also, what's that leading : doing in your source parameter? Most likely I just don't understand the syntax, but it looks a bit weird to me.

Upvotes: 0

Related Questions