Thursday, January 22, 2009

How to execute a sudo command in a script

The -S option for sudo makes sudo read the password from stdin instead of prompting. So it would look something like:

echo 'password' | sudo -S thecmd


Another option is to modify /etc/sudoers to specify NOPASSWD for certain users. Edit /etc/sudoeres with visudo. Then add the line

username ALL=(ALL) NOPASSWD: ALL

where username is the user to set.

How to copy files from a samba share from the command line

1) Mount the samba share. The command will look something like:

sudo mount -t cifs //servername/dir /home/username/mountdir -o password=password user=username

The -t option specifies the filesystem (cifs = samba). The servername/dir is the remote directory to mount and the /home/username is the local directory to mount to.

Note: smbfs needs to be installed for this to work: sudo apt-get smbfs

To learn more options, read man mount.cifs.

2) Copy files.

3) Unmount samba share:

sudo umount /home/username/mountdir