Setting up Samba
19 Nov 2010 Leave a Comment
in Uncategorized Tags: samba smb
To install SAMBA use sudo apt-get install samba, Irrespective of the OS, I always have a dave user. So what I usually do is share out my Linux home directory on the file server so it\’s available to any other machine on the network logged on as me.
The file you need to edit is:
/etc/samba/smb.conf
Look for [homes], you should see something like:
;[homes]
; comment = Home Directories
; browseable = no
The semicolons are commenting out the 3 lines, I replaced the above with:
[homes]\r\n
comment = Home Directories
path = %H/files
browseable = no
writeable = yes
create mask = 700
directory mask = 700
invalid users = root bin daemon nobody named sys tty disk mem kmem users
Rather than sharing out the /home/dave directory, this shares out a sub directory called files. This is just for tidiness as there are a lot of hidden files in the former that clutter up windows explorer if you have it set to show hidden files.
Use:
sudo /etc/init.d/samba restart
to restart the samba service with the new settings.
Now I need to create my files directory, this is simply a matter of:
cd ~
mkdir files
The cd ~ moves me back to my /home/dave directory so I create the files directory in the right place.
Finally:
sudo smbpasswd -a dave
adds a samba user for dave. Samba has it’s own set of users separate from the OS, this command just creates a new samba user. You will be prompted for a password, I use the same password for samba as I use for logging onto the machine (and my windows box for that matter).
From a windows box, I can now navigate to \\server IP\dave where server IP is the IP address of my server.
NOTE: There’s a chance that you might have to restart the samba service last. I forgot to create the files directory and one of my attempts to fix the problem was to restart the server after creating the samba user (it’s done before in the above instructions).
This setup is pretty basic. Things I’d like to do in addition would be:
- Create a read only share that’s available to anyone to store media for viewing on any machine on the network, irrespective of the user logged on. Although selected users should have permissions to upload,change and delete files.
- Create a share that can be updated by anyone. This would be useful for backing up files from pocket PC devices on the network that don’t seem to be able to log on as a particular user.
I’ve had a brief look at SME server which does all sorts of cool stuff, including being a domain controller for windows boxes. Currently I have to keep my windows, linux and samba passwords in sync, SME server would provide a single central mechanism for keeping everything in line. Hopefully I’ll cover this in a later post.
Setting up your file server for multiple users – Part 1
Ok, just a few points to make before we start:
The whole point of this post (and probably the next couple) is to make a useful network file server from an old PC running Ubuntu Linux. The version I’m now running is 7.10 (gutsy gibbon – I think!).
- You should have a Linux box already purring away nicley and you should have installed Samba and optionally SSH (for remote admin or if you’re running your box headless.
- You have an account that can use <strong>sudo</strong> to run root (administrative) commands.
- I’ll initially jump over the relatively simple matter of sharing your home folder and look at creating shares that can be accessed by many users.
- I couldn\’t have done this without the help of the guys at my local Linux User Group who seem to be on hand 24 hours a day to respond to my daft questions.
Ok, here goes:
Log onto your system, either at the console or remotely via SSH (e.g. using puTTY or another fine emulator).
- Create a suitable named group, for this example we’ll use “lowtowndogshome”.
sudo groupadd lowtowndogshome
- Create the required directory tree, again for this example, I’m going to create a directory called “shared” in the root of the filesystem.
sudo mkdir /shared
- Change group ownership of the directory (or directories) you just created to the group you created earlier, note that the owner is still root, this seems to be ok.
sudo chgrp lowtowndogshome /shared
- Change permissions so that the owner and group members have full access to the directory.
sudo chmod 770 /shared
Each Linux user that needs access to the new directory should be assigned to the new group, make sure that you use the -a option on the command below otherwise bad things can happen! I’m using a local account called dave. See below for the command line required to create a new user.
sudo usermod -a -G lowtowndogshome dave
If you’re logged on as the user you’ve just given group membership to, you’ll need to log off and back on again for the change to take effect.
Now when you’re logged on as the user(s) who are now in the new group you should be able to navigate to the new directory and create, modify and delete files.
cd /shared
touch fred.txt
rm fred.txt
None of the above commands should result in an error.
To create a new user use:
sudo useradd -m fred
fred is the name of the new user, the -m option makes Linux create a home directory for the user (e.g. /home/fred), this is useful if you also want to share the users home directory (see a subsequent post, coming soon).
Setting up your file server for multiple users – Part 2
Assuming you’ve done everything in the previous post, all you need to do now is set up your samba service:
- Backup your existing configuration.
sudo cp /etc/samba/smb.conf smb.conf.original
- Edit your smb.conf file, add something resembling the following on the line above the [homes] section.
[share]
comment = Shared files
browseable = yes
read only = no
writeable = yes
guest ok = no
path = /shared
invalid users = root bin daemon named sys tty disk mem kmem users
- Create a samba account for each Linux user that will want to see this directory from a windows (or other Linux) machine. You will be prompted for a password, for my dave user, I use the same password that I use on my windows box but this doesn’t have to be the case I suppose.
smbpasswd -a dave
- You may have to restart the samba service for changes to take effect, this is always a good idea if you\’re having problems.
sudo /etc/init.d/samba restart
Now from a windows machine, logged on as a user with the same username (and password in my case) I can navigate to \server_address\share and drop files ’till my hearts content.
Backup and Restore of MySQL
19 Nov 2010 Leave a Comment
Thought this might be tricky but it’s reasonably easy; to backup, it’s just one command:
mysqldump -u <user> -p<password> <database> > ~/dumpfile.sql
replace <user> with an appropriately authorised user.
replace <password> with their password (note there’s no space after the “-p”).
replace <database> with the name of the database you want to export.
Once completed, dumpfile.sql contains all the SQL required to rebuild the objects within the database, it’s not a wierd binary format.
To restore, again, it’s a single command:
mysql -u <user> -p<password> <database> < dumpfile.sql
The same substitution rules apply as before. I guess the database may need to exist beforehand.
MYSQL – Getting Started
01 Jul 2010 Leave a Comment
in Uncategorized Tags: autonumber, constraints, mysql, ri, table
Right,
Enough faffing about! This post will just cover a few key points in getting going with MySQL. I’m going to approach it from the point of view of an oracle developer so some of the terminology might not be spot on.
Basics
First thing’s first, you need to change the root password for your MySQL server. This is pretty simple and can be found easily via googling. For convenience, I’ve replicated this below:
For a fresh install use:
mysqladmin -u root password <NEWPASSWORD>
There are ways of doing this by directly updating system tables but that just sounds a bit dodgy to me with my oracle background.
Once you’ve done this, you should be able to log onto the database server using:
mysql –u root -p
then entering the password selected in the previous step.
At this point you can try:
show databases ;
to see a list of databases on your server (remember the semicolon), the output might look something like this:
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
+--------------------+
2 rows in set (0.00 sec)
Create a database
We’ll create a new database next, this is as simple as:
create database mydb ;
I dunno about you but I also like to create a user, instead of using the root account, to do my development. It helps identify problems with permissions at an early stage.
I’ve tried a few ways to do this and this one seems to work the best for me:
grant create, select, insert, update, delete, alter, lock tables on mydb.* to ‘myuser’@localhost identified by ‘Pa55word’ ;
If you now quit out of the MySQL command prompt (use \q) you should be able to log in with the new user details, e.g.:
mysql -u myuser -p
and enter the new password when prompted. Now enter:
use mydb
to start working with the new database, not sure if this is mandatory or whether it justs saves on a bit of subsequent typing.
Create some tables
A quick scan of the official documentation shows many options for table creation. I’m going to stick with a good old fashioned create statement that specifies the column names and types and I’ll also cover referential integrity (primary and foreign keys) and constraints (unique keys, permitted values). Ok, let’s create a couple of tables, we’ll leave out RI stuff for now.
create table cust ( id int, name varchar(50));
create table job (id int, notes varchar(200), cust_id int);
Ok, now add primary keys; these will be the id columns on both tables.
alter table cust add primary key(id) ;
alter table job add primary key(id) ;
If you use:
desc cust ;
..or
desc job ;
you’ll see that the key column has been populated with PRI for both id fields on both tables. Now to link the two tables together:
alter table job add constraint foreign key (cust_id) references cust (id) ;
..seems to work! The Key column when describing the job table contains “MUL” which seems to indicate that the column can contain multiple identical values (seems fair enough!).
Finally, let’s try and automatically generate the id column values. In oracle we’d use sequences and triggers but the solution here is more like Microsoft Access autonumbers:
alter table cust modify id int auto_increment ;
alter table job modify id int auto_increment ;
At some point it might be worth looking at using GUIDs instead of a linear sequence as this spreads the rows more evenly across the database and can have some benefits where rows are created by disconnected processes and then synced back to the main database (GUIDs are unique so you’ll never generate duplicates!!).
Let’s give it a quick check:
insert into cust (name) values (’Dave’) ;
insert into cust (name) values (’Tom’) ;
insert into cust (name) values (’Dick’) ;
insert into cust (name) values (’Harry’) ;
select * from cust ;
gives:
+----+-------+
| id | name |
+----+-------+
| 1 | Dave |
| 2 | Tom |
| 3 | Dick |
| 4 | Harry |
+----+-------+
4 rows in set (0.00 sec)
Incidentally, the inserts seem to have been commited as a rollback statement had no effect on the contents of the table after the inserts. I’ll need to look more at building transactions later on.
Let’s now check the referential integrity works:
insert into job (notes, cust_id) values (’Boiler Repair’,1) ;
insert into job (notes, cust_id) values (’Boiler Repair’,5) ;
Curiously, both statements work which is a shame as there’s no cust record with an id of 5!, back to the drawing board!!
Revised create tables
Right, the key to fix this problem appears to be to use the Engine=InnoDB clause when creating tables. I’ve dropped the tables and created a script to recreate them:
use mydb ;
create table cust (
id int auto_increment ,
name varchar(50),
primary key(id))
engine=innodb ;
create table job (
id int auto_increment,
notes varchar(200),
cust_id int,
primary key(id),
foreign key(cust_id) references cust(id))
engine=innodb ;
Now if I try to create a row on the job table that references a non existent customer, I get an error:
mysql> insert into job (notes, cust_id) values ('Fix Boiler',5) ;
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`mydb/job`, CONSTRAINT `job_ibfk_1` FOREIGN KEY (`cust_id`) REFERENCES `cust` (`id`))
You can specify behavours to follow when a parent row is deleted (e.g. cascade) but I’ve not bothered at this stage.
Scripting in the MySQL Command line environment
01 Jul 2010 Leave a Comment
in Uncategorized Tags: linux, mysql, scripting
In Oracle, I’m used to the ed command to create scripts and the @ operator to run them. You can work in a similar way with MySQL:
To create a script from within the MySQL environment use:
\! vi test.sql
.. there’s a space after the exclamation mark and this assumes that you like the vi editor. Essentially it opens up vi so you can create the test.sql file. If you store all of your scripts in a particular directory (e.g. /home/dave/sql) then it may help if you start mysql in that location.
\! Essentially lets you enter shell commands so if you use HOST from within oracle SQL Plus, you can get the same effect in MySQL by keying:
\! sh
Once you’ve keyed in your script, you can run it using the following syntax:
\. test.sql
..again, there’s a space after the full stop and you need to remember the .sql suffix when running and editing the script.
The script can contain multiple statements, for example the following works fine as the contents of the test.sql file.
use mydb ;select * from cust ;
Up And Running!
27 Jun 2010 Leave a Comment
I don’t for a second think that anyone was subscribing to this and for that matter I’m not that bothered! This is just for my personal use as I try to bluff my way in IT until I retire!
For anyone who is reading this, the story so far is; I originally self-hosted WordPress on my ISPs CGI server but it got hacked, so I trashed it. My ISP upgraded their security policies and I couldn’t get WordPress to work again!
Now I’m hosting it on WordPress.Com so hopefully I won’t have to bother about upgrades or hackers ever again!
Watch this space as I’ll be uploading content very soon.