Shared Directories with Samba

4, October 2009 by wobblybob9

Configuring the Samba conf.file [smb.conf]

First, create a shared folder by opening a [Terminal] and either typing or copy & pasting the following;

martin@linux:~$ sudo mkdir /home/my_share

make it available to all users;

martin@linux:~$ sudo chmod 0777 /home/my_share

install samba with;

martin@linux:~$ sudo apt-get install samba

stop samba running with;

martin@linux:~$ sudo /etc/init.d/samba stop

rename the current config file as a backup template with;

martin@linux:~$ sudo mv /etc/samba/smb.conf /etc/samba/smb.conf.backup

create a new config template;

martin@linux:~$ sudo touch /etc/samba/smb.conf

open new config file with;

martin@linux:~$ sudo gedit /etc/samba/smb.conf

now copy & paste in the details from the smb.conf file here, changing details of network name [my_network], computer name [my_linux_box] and the shared folder details [my_share] as required.

save and close the config file.

start samba again with;

martin@linux:~$ sudo /etc/init.d/samba start

Add yourself as a samba user with;

martin@linux:~$ sudo smbpasswd -L -a my_name

enter your admin password when asked.

martin@linux:~$ sudo smbpasswd -L -e my_name

Shutdown confirmation message

4, October 2009 by wobblybob9

Remove the 60 second to shutdown confirmation message in Karmic Beta.

Open a [Terminal] and type gconf-editor and press [Enter], now using the left-hand menu select [apps], then [indicator-sessions] and tick the box next to [ suppress_logout_restart_shutdown ]

Backup using rsync, SSH & cron

31, August 2009 by wobblybob9

Backup on Ubuntu.

Updated 09/11/09 to correct some errors!

This “How To” was written with the help of this article by Troy Johnson, here and amended were necessary to fit my needs.

I have 2 PC’s on a network and want to backup some files from one PC the [remote_client] to the other PC the [server] to do this I want to run a script from the server. The process can be atcheved using rsync & SSH and automated with a cron job on the server.

Step:1

We install on the server

open-ssh-server and rsync

and on the client openssh-client and rsync

This can be done via the Synaptic Package Manager or using the command line i.e.

~$ sudo apt-get install openssh-client

~$ sudo apt-get install rsync

you also may want to check that cron is installed and if you want a GTK version of rsync you could install gadmin-rsync.

So the files I want to backup are on the remote_client in the following folder; /home/martin/my_files

and I want to back them up on the server in the following folder; /home/martin/backups

Note: I have an account on both PC’s with the same name and password, and read access to both the source & destination folders.

On the server we test out a basic script called backup.sh and saved with the Permissions set to “allow executing as a program”, check out my post here for help with that, the content of the script is;

#!/bin/sh

## first script to copy files from remote_client to server ##

rsync -avz -e ssh martin@remote_client:/home/martin/my_files/ /home/martin/backups

save the script and then run it by typing;

~$ ./backup.sh

in a Terminal assuming you saved the script in your home directory /home/martin and called it backup.sh. You should be prompted for your user password on the remote_client then the process should start.

To make this more secure edit the ‘authorized_keys’ file in servers /home/martin/.ssh folder with a text editor and modify the line with ’server-rsync-key.pub’ information on it from this:

Step:2

Ok so that does the trick but if I’m to automate this script it needs to work without SSH asking me for my password each time. To do this I need to generate a private/public key pair, this is much more secure than adding the actual password in the text of the script or elsewhere linked to it.

We now need to be logged onto the server;

To generate the key pair I open a [Terminal] and issue the command;

~$ mkdir /home/martin/.key

to create a hidden folder in my home folder called .key [the dot makes it hidden], then;

~$ ssh-keygen -t dsa -b 1024 -f /home/martin/.key/server-rsync-key

Generating public/private dsa key pair.

Enter passphrase (empty for no passphrase): [press Enter]

Enter same passphrase again: [press Enter]

Your identification has been saved in /home/martin/.key/server-rsync-key.

Your public key has been saved in /home/martin/.key/server-rsync-key.pub.

The key fingerprint is:

etc. etc.

I now have 2 files in the .key folder in my home folder called;

server-rsync-key

and

server-rsync-key.pub

I now copy the server-rsync-key.pub key over to the remote_client into my home folder /home/martin and log onto the remote_client.

Now on the remote_client, I issue the following command in a [Terminal]

~$ if [ ! -d .ssh ]; then mkdir .ssh ; chmod 700 .ssh ; fi

[this checks to see if the folder .ssh exists and if not creates it]

~$ mv server-rsync-key.pub .ssh/

[this moves the key from my home folder to the .ssh folder, hidden in my home folder]

~$ cd .ssh/

[we now move to the .ssh folder]

~$ if [ ! -f authorized_keys ]; then touch authorized_keys ; chmod 600 authorized_keys ; fi

[this checks to see if there is an authorized_keys file and if not creates it with the correct permissions]

~$ cat server-rsync-key.pub >> authorized_keys

[this copies the contents of server-rsync-key.pub key into the file authorized_keys file.

If we now run our original script with the amendment below on the server it no longer asks for a password.

rsync -avz -e "ssh -i /home/martin/.key/server-rsync-key" martin@remote_client:/home/martin/my_files/ /home/martin/backups

Step:3

Now the key can be used to make connections to the remote_client, but these connections can be from anywhere (that the ssh daemon on remote_client allows connections from) and they can do anything (that remote_user can do), which could be dangerous.  To make this more secure edit the 'authorized_keys' file in remote_client /home/martin/.ssh folder with a text editor and modify the line ending user_name@server_name from this:

ssh-dss
yl6b2/cMmBVWO39lWAjcsKK/zEdJbrOdt/sKsxIK1/ZIvtl92DLlMhci5c4tBjCODey4yjLhApjWgvX9
D5OPp89qhah4zu509uNX7uH58Zw/+m6ZOLHN28mV5KLUl7FTL2KZ583KrcWkUA0Id4ptUa9CAkcqn/gW
kHMptgVwaZKlqZ+QtEa0V2IwUDWS097p3SlLvozw46+ucWxwTJttCHLzUmNN7w1cIv0w/OHh5IGh+wWj
V9pbO0VT3/r2jxkzqksKOYAb5CYzSNRyEwp+NIKrY+aJz7myu4Unn9de4cYsuXoAB6FQ5I8AAAEBAJSm
DndXJCm7G66qdu3ElsLT0Jlz/es9F27r+xrg5pZ5GjfBCRvHNo2DF4YW9MKdUQiv+ILMY8OISduTeu32
nyA7 etc....

to this:

from="10.1.1.1", command="/home/martin/.key/validate-rsync" ssh-dss bA402VuCsOLg
yl6b2/cMmBVWO39lWAjcsKK/zEdJbrOdt/sKsxIK1/ZIvtl92DLlMhci5c4tBjCODey4yjLhApjWgvX9
D5OPp89qhah4zu509uNX7uH58Zw/+m6ZOLHN28mV5KLUl7FTL2KZ583KrcWkUA0Id4ptUa9CAkcqn/gW
kHMptgVwaZKlqZ+QtEa0V2IwUDWS097p3SlLvozw46+ucWxwTJttCHLzUmNN7w1cIv0w/OHh5IGh+wWj
V9pbO0VT3/r2jxkzqksKOYAb5CYzSNRyEwp+NIKrY+aJz7myu4Unn9de4cYsuXoAB6FQ5I8AAAEBAJSm
DndXJCm7G66qdu3ElsLT0Jlz/es9F27r+xrg5pZ5GjfBCRvHNo2DF4YW9MKdUQiv+ILMY8OISduTeu32
nyA7 etc....

where "10.1.1.1" is the IP address of server, and "/home/martin/.key/validate-rsync" point to a script called validate-rsync as follows:

#!/bin/sh
case "$SSH_ORIGINAL_COMMAND" in
*\&*)
echo "Rejected"
;;
*\(*)
echo "Rejected"
;;
*\{*)
echo "Rejected"
;;
*\;*)
echo "Rejected"
;;
*\<*)
echo "Rejected"
;;
*\`*)
echo "Rejected"
;;
*\|*)
echo "Rejected"
;;
rsync\ --server*)
$SSH_ORIGINAL_COMMAND
;;
*)
echo "Rejected"
;;
esac

If server has a variable address, or shares its address (via NAT etc.) with hosts you do not trust, omit the 'from="10.1.1.1",' part of the line (including the comma), but leave the 'command' portion. This way, only the 'rsync' will be possible from connections using this key. Make certain that the 'validate-rsync' script is executable by remote_user on remote_client and test it.

So in my case the result looks like this:

command="/home/martin/.key/validate-rsync" ssh-dss bA402VuCsOLg9YS0NKxugT+o4UuIj
yl6b2/cMmBVWO39lWAjcsKK/zEdJbrOdt/sKsxIK1/ZIvtl92DLlMhci5c4tBjCODey4yjLhApjWgvX9
D5OPp89qhah4zu509uNX7uH58Zw/+m6ZOLHN28mV5KLUl7FTL2KZ583KrcWkUA0Id4ptUa9CAkcqn/gW
kHMptgVwaZKlqZ+QtEa0V2IwUDWS097p3SlLvozw46+ucWxwTJttCHLzUmNN7w1cIv0w/OHh5IGh+wWj
V9pbO0VT3/r2jxkzqksKOYAb5CYzSNRyEwp+NIKrY+aJz7myu4Unn9de4cYsuXoAB6FQ5I8AAAEBAJSm
DndXJCm7G66qdu3ElsLT0Jlz/es9F27r+xrg5pZ5GjfBCRvHNo2DF4YW9MKdUQiv+ILMY8OISduTeu32
nyA7 etc....

Our backup.sh script now looks like this:

#!/bin/sh

## second script to copy files from remote_client to server ##

rsync -avz -e "ssh -i /home/martin/.key/server-rsync-key" martin@remote_client:/home/martin/my_files/ /home/martin/backups

Step:4

I now need to run the script from a Cron job, first move the backup.sh script into your $PATH, for help with this check out my post here.

Now on the server open a [Terminal] and issue the command:

~$ crontab -e

This should open a text editor, normally vi, normally paste into it the following text as a reminder

# leave a space between each option #
# the * is a wildcard option #
# dayofmonth is 1 to 31 #
# dayofweek can be 0 – 6, 0 is Sunday #
# or use mon tue wed etc #
#
# mins hours dayofmonth month dayofweek command
# * * * * * * command

Then use Crontrol+o to save and press [Enter] to save, then Control+x to [Exit].

and test…

Cron jobs

31, August 2009 by wobblybob9

Cron tabs can be used to automate jobs on your system by running programs or scripts at prdefined times, dates or days, each user can have their own cron tabs or they can be run as root. To open a crontab, type the following in a [Terminal];

crontab -e

this will open in the vi text editor, but if you prefere the gedit text editor, run the following command;

export VISUAL=gedit

If you stick with vi then use Control+o followed by [Enter] to save your tab, then Control+x to [Exit].

When you run crontab -e, you will get an empty crontab with just the following [or similar] dispalyed;

# m h dom mon dow command

the headings are described below and I usually add these to the top of the file to make things easier to edit. So my crontab looks like this:

# m h dom mon dow command

# mins -> minutes (0-59)

# hours -> hour (0-24)

# DayOfMonth -> day of month (1-31)

# month -> month (1-12)

# DayOfWeek -> day of week (0-6) [Sunday is 0], or

# DayOfWeek -> day of week (mon – sun)

# You can use ‘,’ ‘-’ to give you 1,2,3,5 or 1-5 etc

# The * is used as a wildcard i.e every month

# m h dom mon dow command

# * * * * * /command

Examples;

each of the 6 command requires a space between them, here are a few examples;

### runs cron 2 mins past every hour ###

2 * * * * /home/martin/bin/play_sound.sh

### runs cron every 2 minutes ###

# m h dom mon dow command

*/2 * * * * /home/martin/bin/play_sound.sh

#01 * * * * root run-parts /etc/cron.hourly

#The run-parts script is simple enough: it just runs all the executables in the specified directory

### runs cron every fri at 15:30 ###

# m h dom mon dow command

30 15 * * * fri /home/martin/bin/backup.sh

Jaunty Ubuntu NBR – favourites

29, August 2009 by wobblybob9

Using Ubuntu NBR [Jaunty], When I right clicked on Firefox 3 icon in “Favorites” and selected “Remove”, nothing happend. Other icons can be removed and added but not this one. Could it be because I had aready removed Firefox 3 in synaptic in order to install Firefox 3.5? The answer was to remove the icon file firefox-fav.desktop from /usr/share/applications/ using the [terminal] as follows.

sudo rm /usr/share/applications/firefox-fav.desktop

Edit Grub

8, August 2009 by wobblybob9

I had too many entries on the Grub menu and decided to edit it, 1st I opened a Terminal and typed;

sudo cp /boot/grub/menu.lst sudo /boot/grub/menu.lst.back

and hit [Enter], which copies the menu file to a file called menu.lst.back in the same folder.
then I typed;

sudo gedit /boot/grub/menu.lst

This opened the boot menu in the gedit text editor, I then placed # at the start of any line I did not want to show on the menu, re saved it and re booted the laptop all done.

Scanner Support

27, May 2009 by wobblybob9

First the bad news, although some scanners are supported in ubuntu/linux many are either not supported at all or only basic scanning is supported, in my case my Visioneer 4800u is not supported at all! As I wanted to move completely over to ubuntu I decided to buy a second hand scanner from eBay. I checked for compatible scanners on the SANE [Scanner Access Now Easy] project page here by checking out the Supported Devices listed my Manufacturer page. I then bid on a Canon CanoScan N650U Flatbed Scanner and got is for £12.31 including postage [result!]

If your scanner is supported then you may need to add the SANE front and back ends to your system as follows: Open the [Synaptic Package Manager] and add the the following packages;

sane, sane-utls, xsane & xsane-common

You can now plug in your scanner and open the [XSane Image Scanner] from the [Applications], [Graphics] menu and if all goes well your scanner name should show in the dialogue title bar and if you press Scan you should be up and running.

Ubuntu Gusty v7.10 and possibly older versions may need to have scanbuttond (yes there is a d at the end) running for the scanner operation to be detected. To add the scanbuttond type the following in a [Terminal];

sudo apt-get install scanbuttoned

To run the program manually type the following after the prompt;

scanbuttond

To make this program run at start-up so you don’t have to do this each time you want to scan open [System], [Preferences], [Sessions] click on the [Start-up Programs] Tab then click the [+ Add] button. A new window will appear, add [Scanbutton] in the [Name] field and scanbuttond in the [Command] field [remember the "d" at the end of scanbuttond, then [OK] that and [Close] when you re start the PC scanbuttond will be running.

Some command line scanner options to try, type or copy and paste the following into the [Terminal] after the prompt;

This will produce a scan without using the XScane graphical interface

scanimage -x 210 -y 290 –resolution 400 –mode Color > /home/yourname/output.jpg

change [yourname] for the name of your [Home] folder and this should produce a scan called output.jpg.

These command line options will help identify your scanner for problem solving;

sane-find-scanner -v -v

scanimage -L

cat /proc/bus/usb/devices

redirect their output to a text file to make things easier by adding >> scanner_test.txt to the end of each line. The >> part appends the text to the end of the last so you will not overwrite the output from the last command.

sane-find-scanner -v -v >> scanner_test.txt

or use this script, create the script file using;

touch scanner_test.sh

open the file with;

gedit scanner_test.sh

then copy and paste the text below into it.

#!/bin/sh
## created by Martin Cooper 27 May, 2009 ##
##
echo “Scanner test script” >> scanner_test.txt
echo “sane-find-scanner -v -v output” >> scanner_test.txt
echo “*********************************************” >> scanner_test.txt
sane-find-scanner -v -v >> scanner_test.txt
echo “scanimage -L output” >> scanner_test.txt
echo “*********************************************” >> scanner_test.txt
scanimage -L >> scanner_test.txt
echo “cat /proc/bus/usb/devices” >> scanner_test.txt
echo “*********************************************” >> scanner_test.txt
cat /proc/bus/usb/devices >> scanner_test.txt

now save the file and make it executable with;

chmod +x ~/scanner_test.sh

and now run it by running this command

./scanner_test.sh

or double click on the file. The result will now be in the file scanner_test.txt in your home Directory.

Notes: Feisty v 7.04 onwards Gnome Desktop

Desktop launcher menu items

27, May 2009 by wobblybob9

Create a Gnome Desktop launcher for a program that does not create one of it’s own via the command line.

The basic format for a Desktop launcher is;

[Desktop Entry]
Name=Songbird
Comment=Media Player
Exec=/home/user/Songbird/songbird
Icon=/home/user/Icons/songbird.png
Terminal=false
Type=Application
Categories=Application;AudioVideo;

Open a text editor and copy & paste the above text into it, changing the following options;

Name: The name you wants to see in the menu for the program.
Comment: The message the comes up when you fover over the menu item.
Exec: the command that start the program.
Icon: the path to an icon of your choice.
Terminal: leave as false to not open in a [Terminal].
Type: leave as Application.
Categories: listed below;
Internet is Network
Sound & Vision is AudioVideo
Games is Game
Office is Office
Accessories is Utility
Graphics is Graphics
Programming is Development
System Tools is System
System preferences is settings

save it naming it your_name-name_of_program.desktop i.e. martin-songbird.desktop

The first part of the file name can be anything you like but must be there as the file must have the format name-prog_name.desktop. You should now have a desktop icon in you home Directory which you can then click on to open the program. If nothing happens and you need to edit the file the easiest way is to open it from a [terminal] using the following command;

gedit martin-songbird.desktop

or by opening your favorite test editor and then opening the file from it as nautilus cannot open these files and does not display the full file name. You can now move this icon to your Desktop if you wish or add it to your Applications menu using the following command;

xdg-desktop-menu install ./martin-songbird.desktop

Notes: Ubuntu Jaunty Gnome Desktop

creating a symlink [symbolic link]

27, May 2009 by wobblybob9

Creating a symlink [symbolic link, known as a shortcut to Windows users] using the command line. To create a symlink to a file called test_file in /home/martin on the desktop we open a [Terminal] and because it defaults to the home directory create the test file test_file with;

touch test_file

and add some text to it with

echo “This is a test file” > test_file

now we create the link with

ln -s /home/martin/test_file ~/Desktop

this will create a symbolic link to the file on you desktop, open the file using it and you should see the text we added above. Note the ~/ option is a shortcut of /home/martin i.e. to your home folder. Now lets create the symlink again with a different name

ln -s ~/test_file ~/Desktop/”My Test File”

The same command can be used to create symlinks to Directories and symlinks can be removed with rm -f, so our last symlink can be removed with;

rm -f ~/Desktop/”My Test File”

Using Ubuntu Jaunty on the Gnome Desktop


My First Script

26, May 2009 by wobblybob9

This is not my first script but I was asked by a friend to start him off in the right direction so after writing this out I thought I should at least post it!

Ubuntu, for some reason unknown to me uses the Dash shell which causes problem with some scripts written in the standard Bash shell. To make things easier from the start it is better to swap over to do this open a Terminal [Applications], [Accessories], [Terminal] and type or copy & paste the following;

echo $SHELL

and press [Enter], you should get a response like this;

/bin/dash

which is your current Shell, the [echo] means print the following to the screen. Now lets change it to the Bash shell, type the following in the [Terminal]

sudo rm -f /bin/sh

and press [Enter], you will then be asked to enter your password as the sudo option means you are working as the root [super user] and this requires a password to make it secure. now type the following;

sudo ln -s /bin/bash /bin/sh

the first command removes the symlink [symbolic link, which Windows users will know as a shortcut] which links Dash to the sh command and the second adds a link from the bash shell to the sh command. Now type this again;

echo $SHELL

your should now get;

/bin/bash

Now lets set-up a folder in your home directory to store your scripts in and allow the system to locate them by adding the new directory to your path. Create a new folder by typing;

mkdir bin

as the [Terminal] starts up in you [home] directory the bin directory will be created there. you can list the contents of your home by typing;

ls

and [Enter] which will list all the directories [in blue] & files [in black]. Now lets add the bin directory to your path, this is where the system looks for files you ask it to launch. To see your current path, type the following.

echo $PATH

which will give you something like;

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

PATH=$PATH:/home/martin/bin

and then repeating the command;

echo $PATH

will now give you;

/home/martin/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

Any executable scripts you place in your bin directory can run by typing their name in a [Terminal] as we will see later. We will now create our first script using the Touch command.

touch ~/bin/my_first_script.sh

the ~/ is short for the path to your home directory i.e. /home/martin/ and the rest adds the file called my_first_script.sh in the bin directory. We need to add some commands to the file so open it in gedit by typing;

gedit /home/martin/bin/my_first_script.sh

Then copy and paste the following into the file, save it and close gedit, the first line is know as the “shebang” and point the system at the shell you are using, the lines starting ## are comments and will not be run.

#!/bin/sh
## created by Martin May 2009 ##
## say hello ##
## we give the variable today a value which represents ##
## day, date & time and prints it later by adding a $ to it ##
today=`date +%a%t%d%t%B%t%Y%t`
## ask your name and read it into a variable called name ##
echo -n “Enter your name: “
read name
## clear the screen ##
clear
## we now give the variable time the current system time ##
time=`date +%T`
## now lets print it all out to the screen ##
echo “Hello World!”
echo
echo “Welcome ” $name
echo
echo “Today is “$today
echo
echo “and your logon time was : “$time
echo

We can now try and run the script by typing it’s name in the [Terminal];

my_first_script

which will result in;

bash: /home/martin/bin/my_first_script.sh: Permission denied

This is because the script is not yet executable, so type the following;

chmod +x ~/bin/my_first_script.sh

and run the script again with;

my_first_script.sh

It should then ask for your name and display the result with a welcome message including the time as follows;

Hello World!

Welcome Martin

Today is Tue 15 May 2009

and your logon time was : 22:33:24

Once you have this basic script running, you can start making changes and playing with the output.