Linux : Running sudo script on startup

Discussion in 'Computer Science & Culture' started by AlphaNumeric, Jan 2, 2012.

Thread Status:
Not open for further replies.
  1. AlphaNumeric Fully ionized Registered Senior Member

    Messages:
    6,702
    I use sshfs to mount a set of drives in my server onto my main computer. Server runs Ubuntu 10.10 and the main computer Mint 12 (I hate unity so went for something with Gnome when I updated but it's still very similar

    Please Register or Log in to view the hidden image!

    ), so essentially both Ubuntu. I've got a little script in my /usr/local/bin folder called 'mnt' so when I type mnt into the terminal once the computer gets to the desktop it mounts using fuse the drives. Since this involves altering the fstab it asks me for my password, though only once despite mounting multiple drives.

    Since I'm giving my password to log in (and to decrypt my /home drive) I feel it should be possible to get this mnt to be run on start up automatically and not to ask me for my password. How can I do this? Bear in mind the limits of my terminal capabilities is writing said scripts, which are 4 lines of 'fusermount...' and 'sshfs...' so something which involves me doing something crazy is going to go over my head.

    Thanks

    Please Register or Log in to view the hidden image!

     
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. Stryder Keeper of "good" ideas. Valued Senior Member

    Messages:
    13,105
    It's been a while since I've toyed with any linux build, I would guess what you'd likely require doing is setting the "options" of want device you want to mount to allow a specific user and generate a script that runs as that user to mount the partition/drive, at least that's the general line of inquiry I'd take.

    Any scripting shouldn't be too crazy, most shell scripting should cover what you want, unless of course you want to get really geeky and use Python/Perl or other flavour of interpreter language, be warned though, that will obfuscate it further.
     
  4. Google AdSense Guest Advertisement



    to hide all adverts.
  5. superstring01 Moderator

    Messages:
    12,110
    Something this low level will require you to write in C/C++. I'm only beginning to study C/C++ and I figure that such a configuration will require you to mess with the bootup process or the kernel altogether. Perhaps both.

    Edit: I just flipped open my "Linux for Dummies" book and noticed that Python is used a bit. That said, the bootup process is pretty low level and I'm certain it will require command codes in C.

    ~String
     
  6. Google AdSense Guest Advertisement



    to hide all adverts.
  7. steampunk Registered Senior Member

    Messages:
    278
    When I start Mint (Julia) and drives are connected at start up, they are auto mounted, unless the permissions dissalow it. Maybe try giving permission to your Mint user on the drives you want Mint to mount. It sounds like two os are mounting the same drive. Never needed to do that and permissions might not be the only thing to do.
     
  8. Chipz Banned Banned

    Messages:
    838
    As steampunk stated there are system configuration tools to allow for auto-mounting. If you insist on doing it through a command, add "sudo" to the commands in mnt script and add yourself to the SUDOERS file with no password request.
     
  9. AlphaNumeric Fully ionized Registered Senior Member

    Messages:
    6,702
    It's a little bit different from normal mounting. For example, if you want to do it on a network drive on Windows you would go through the network explorer thing and find something explicitly shared. This method doesn't require you to share the drives, they don't appear on the network unless I share them using Samba. I share some folders so my Windows HTPC can see them and it automounts those no problem. The sshfs method requires you to run an ssh server on the server (obviously) and then the sshfs command signs in as a user on the server (I use the same name and password for all accounts on all computers so its a little easier) and mounts the drive on my main computer as if its actually plugged into the motherboard. It's a lot less problematic than the Samba sharing.

    Doing something C or Python based is a little beyond my reach, I can just about manage R and Mathematica.

    I'll try some passwordless sudo user thingy, as suggested

    Please Register or Log in to view the hidden image!

     
  10. steampunk Registered Senior Member

    Messages:
    278
    If you dont mind would you post your solution.
     
  11. Chipz Banned Banned

    Messages:
    838
    In case you're unaware-- on most (all?) distros you can find the file in /etc/sudoers
    An added line would look something like this:

    Code:
    root    ALL=(ALL)       ALL
    user_name ALL=(ALL) NOPASSWD: ALL
     
  12. steampunk Registered Senior Member

    Messages:
    278
    Under Mint the directory is:

    Code:
     /etc/sudoers.d 
     
    Last edited: Jan 4, 2012
  13. steampunk Registered Senior Member

    Messages:
    278
    I ve been needing to do this mysef, so I went ahead found a solution. We both use Mint so it should work for you too. (Reqires a bit of python, but dont be intimidated, python is awesome and very usefull). This example makes a test directory at startup.

    1. Create a file containing this text (this is the python script):

    Code:
    #! /usr/bin/python
    
    import os
    
    unixCommand1 = "mkdir /home/mint/test". # replace mint with your user name.
    os.system(unixCommand1)
    
    unixCommand2 = ""
    os.system(unixCommand2) 
    
    
    2. Copy this script to the directory
    Code:
     /etc/init.d
    3. Create this link

    Code:
    sudo ln -s /etc/init.d/startup.py /etc/rc3.d
    4. Run update

    Code:
    sudo update-rc.d startup.py defaults
    5. Restart to confirm

    Code:
    sudo init 6

    To customize, just create more "unixCommand" variables with appropriate numbers. Assign them with unix command line arguments surrouned by quotes. Then call each command with os.system(unixCommand)
     
    Last edited: Jan 5, 2012
  14. Chipz Banned Banned

    Messages:
    838
    ? Sudoers.d directory is not the file to edit, are you saying sudoers is in sudoers.d for you? I'm not sure I believe that.

    This is just plain insanity, why would you create a python script which does nothing but -- 1. load the OS module 2. Make the OS open up a shell 3. execute a shell command.

    Code:
    #!/bin/bash 
    mkdir mkdir /home/mint/test
    
    Then
    Code:
    chmod +x that_file
    
     
  15. steampunk Registered Senior Member

    Messages:
    278
    I didn't say it was a file. I said it was a dir. I'm new to this thing, but I assume you place the scripts in that dir.

    Yeah, if all you have is unix command lines arguments, a python script adds some redundant wrapping. But the op sounded as if Python was something intimidating, and it's actually much easier to learn and read than Bash. It does everything Bash does, but far more. So, I thought I'd give it a Python flavor.

    But like I said, I've never done this. I did provide a step by step working solution with very little assumption.
     
  16. Chipz Banned Banned

    Messages:
    838
    Ah, didn't know you were new to it all.

    Truth is -- Bash isn't all that tough to learn and it is worth your time. It has an entirely different purpose than Python. Python, is a language tied to other Python modules -- Bash on the other hand has access to every program on your system.

    BASH notation doesn't really resemble any programming languages, the idea really -- bash just acts as a basic scripting language to execute other applications on your system, it's not meant to really perform anything substantial (outside of file system modifications).
     
  17. steampunk Registered Senior Member

    Messages:
    278
    Yeah. Never done it. I ran into one problem with this method too. It will launch command line arguments such as ls, mount, mkdir, but it won't launch programs like gedit, firefox.

    I thought it might be because the graphics libraries may not be loaded, so I set a timer in the python script so that I knew all dependencies for things like firefox were loaded. Still didn't work.
     
  18. Chipz Banned Banned

    Messages:
    838
    The Python OS module if I recall uses SH rather than BASH and so it will potentially have a different path and it certainly has different features,
    Code:
    echo $PATH
    Any executable found in those directories your terminal will be able to execute. However, running a script on init might run as root level, in which case any profile settings configured on your system will be non-existent.
     
Thread Status:
Not open for further replies.

Share This Page