Answers 100

From LXF Wiki

Answers 100

<title>Batch processing users</title>

<question>I have been running CentOS since version 3 in the school where I work. I have input 500 pupils into the system one by one. I have set up a domain logon with Samba. Is there any way to bulk input so many user accounts in one go? </question>

<answer>Of course, there is, this is one of the areas in which textual command line tools excel, in being able to use data from one file or program in another. Are you trying to create system users or Samba users, or both? Use the useradd command to create system users and smbpasswd to create Samba users. Both of these commands can be used in a script to read a list of user names from a file. Put your new user names in a file called newusers, one per line, like this

username1 password1 real name1
username2 password2 real name 2
...

You can create this file manually or extract the information from another source, such as using sed or awk on an existing file or running an SQL query to pull the information from your pupil database. Then run this to add them as system users.

 cat newusers | while read u p n
 do
 useradd --comment "$n" --password $(pwcrypt
 $p) --create-home $u
 done

You may need to install pwcrypt before you can do this, it is a command line interface to the system crypt() function. You could use the crypt command from the older cli-crypt package, but pwcrypt is more up to date and considered a better option nowadays. If you want to force the user to change their password immediately, which is generally a good idea, add

passwd -e $u

after the useradd command. This expires their password, so they need to change it when they next login. To set the Samba passwords, use smbpasswd like this

 echo -e "$p\n$p" | smbpasswd -as $u

The -s option tells smbpasswd to accept the password from standard input instead of prompting for it, the echo command is in that format because smbpasswd still requires the password to be given twice when used like this. So all you need to add the users to both the system and Samba is

cat newusers | while read u p n
do
useradd --comment "$n" --password $(pwcrypt
$p) --create-home $u
passwd -e $u
echo -e "$p\n$p" | smbpasswd -as $u
done

You must add the users to the system password file before trying to add them to Samba, or smbpasswd will complain. Similarly, when you delete users, run smbpasswd -x username before userdel username. </answer>

<title>Hidden Hostname</title>

<question>I have a Linux box which I use as a server for testing web development projects (it's in my DMZ, so I use a dedicated machine rather than my main machine). When it was running Fedora Core 4, Windows machines were able to access the machine by its hostname. All seemed well. Now, I upgraded the machine to Fedora 7 and its hostname is unavailable ­ pinging webdev (the server name) no longer works but I can still access it through its IP address. </question>

<answer>I suspect you're setting the address statically instead of using DHCP. When you let the computer request an address via DHCP, the DHCP server keeps track of the hostname and IP address, as DHCP servers generally act as local name servers too, so any other computer can resolve the hostname to an IP address. You have three choices, the first is to set the IP address by DHCP. I understand you may want a fixed address for this box, but many DHCP servers have an option to map specific MAC addresses or hostnames to given IP addresses, so you get an effectively static address but working within the DHCP framework. You set the hostname to be sent to the DHCP server in the network-config window. Your second option is to record the IP address and hostname in the hosts file of every other computer on the network. The file is /etc/hosts on the Linux systems and C:\windows\hosts on (yes, you guessed it) Windows. The format of the file is one line per IP address containing the address, full hostname and then any aliases, all separated by white space, like this

192.168.1.27 webserver.example.com webserver
192.168.1.43 mail.example.com              mail ftp.
example.com ftp

The third option is to run your own local DNS server. This is nowhere near as complicated as it sounds, as long as you don't try to set up a full-blown Internet DNS server like Bind. Dnsmasq, which I use on my home network, is available from www.thekelleys.org.uk/dnsmasq and is very easy to set up. Its default setup is to use the /etc/hosts file from the computer running it to provide local DNS resolution and pass other requests to the name servers listed in /etc/resolv.conf. Just install it on one computer on your network (not the one in the DMZ) and set all the others to use that computer as their DNS server. Dnsmasq can work as a DHCP server too (SmoothWall Express uses it) and it is a lot more configurable than built-in DHCP servers of most routers. If you have a computer that is always on, using this to provide DNS and DHCP for your network makes life easier; make sure you disable the DHCP server in the router to avoid conflicts. The dnsmasq configuration file is well documented (see man page and comments in the file) with sensible defaults. It may need no configuration at all for you, but if it does, the options are explained clearly. </answer>

<title>Hard grafting</title>

<question>I use Simple Linux Backup from LXF92, which produces daily backups in the following form: Backup.Mon.tar.gz Backup.Tue.tar.gz Backup.Wed.tar.gz ... Of these, the Monday backup is the full one, the others are incremental. I run two versions of Simple Linux Backup; one is run as root and backs up certain system files, the other is run as my user and backs up my data. They both produce the same file names ­ but one set is stored in /mnt/backup/ system/ and the other set is stored in /mnt/backup/data/. I am now trying to write a script to copy these files onto DVD. Of the files it is only the respective Backup.Mon.tar.gz (full backup) files that I want to keep ­ I intend to run this as a cron job each Monday after the backups have completed. The problem is that I can't just use

growisofs -Z /dev/dvd  R -J /mnt/backup/* 

as I get a load of files I don't want (and it wouldn't fit on one DVD). Nor can I use

growisofs -Z /dev/dvd -R -J /mnt/backup/system/Backup.Mon.tar.gz /mnt/backup/data/Backup.
Mon.tar.gz

because the file names are the same! I suppose I could rename one of the files in my script before writing the DVD, but that just seems like an ugly solution. As far as I can tell growisofs does not support the -o switch to write the files as different file names. Can you offer a more elegant solution? </question>

<answer>There are a number of ways you could do this, all of which use mkisofs arguments. growisofs passes most of it arguments to mkisofs to create the ISO data, only those arguments related to writing the data to the disc belong to growisofs. The one mkisofs argument that is not allowed is the one you mention, -o, because this causes mkisofs to write the ISO data to a file and the whole point of growisofs is to write data to a disc. One option is to use the -m option to exclude everything but the Monday files. This will exclude every backup where the day part of the name does not start with M.

growisofs -Z /dev/dvd -R -J -m `Backup.[^M]??.
tar.gz' /mnt/backup

A more flexible method is to use the graft-points argument. This can take a little getting used to, but it allows you to change the name or path of any file or directory you write to the disc.

growisofs -Z /dev/dvd -R -J -graft-points system.
tar.gz=/mnt/backup/system/Backup.Mon.tar.gz
data.tar.gz=/mnt/backup/data/Backup.Mon.tar.gz

will save the two files as system.tar.gz and data.tar.gz in the root of the DVD. An improved version of the command includes the date of the Monday backups in both the volume name of the DVD and each of the files, making it easier to see at a glance which DVD is current.

DATE=$(date --reference /mnt/backup/system/
Backup.Mon.tar.gz +%y%m%d)
growisofs -Z /dev/dvd -R -J -graft-points -V
BACKUP_$DATE system_$DATE.tar.gz=/mnt/
backup/system/Backup.Mon.tar.gz data_$DATE.
tar.gz=/mnt/backup/data/Backup.Mon.tar.gz

The mkisofs man page has (a lot) more details. Remember also that if your distro uses cdrkit rather than cdrtools (cdrkit's licencing is more acceptable to some distros, particularly those based on Debian) mkisofs is replaced by genisoimage, although there is a symlink for mkisofs to retain compatibility. </answer>

<title>Keeping junk</title>

<question>If I buy a new computer, is there a way I can transfer Thunderbird's Junk Filter `intelligence' by copying some files, rather than it having to re-learn from scratch? </question>

<answer> The junk filter data is in a file called training.dat. This in contained in the randomly named directory in ~/.mozilla-thunderbird. Thunderbird keeps the name of the directory in ~/.mozilla-thunderbird/profiles.ini. Why do you want to transfer only the junk mail filters and not the rest of your data? Even if you use IMAP and keep all your mail on the server, there are still configuration files and filter controls that you would want to keep. Otherwise you will have to recreate or reconfigure everything. You only have to transfer the .mozilla-thunderbird directory to your new computer to carry on where you left off. If the two computers are networked, you can do this with scp

scp -pr user@oldcomputer:.mozilla-thunderbird ~

Otherwise use sneakernet, with something like a USB flash drive, to transfer the files. In this case, you should tar the directory to preserve the file attributes. Copying directly to a FAT filesystem as used on most flash drives, will remove file ownerships and permissions.

# on old computer
tar czf /media/usbdrive/thunderbird.tar.gz
.mozilla-thunderbird
#on new computer
tar xf /media/usbdrive/thunderbird.tar.gz

Whichever method you use, do this before you run Thunderbird on the new system, otherwise it will create a .mozilla-thunderbird directory with new settings. Even if you are using the same user name on both systems, it is quite possible that the new distribution may be allocating a different numeric ID to the user. You can fix this by running this in a root terminal

chown -R myuser: ~myuser/.mozilla-thunderbird

</answer>

<title>Locked out</title>

<question>My main hard disk is partitioned as a dual boot system ­ Windows XP on one and Ubuntu (installed from LXFS07) on the other. I then added a second hard disk with three FAT32 partitions for data to be shared between the two systems. To use the second disk in Ubuntu, I have to use the System > Admin > Disk Manager at each login to access the FAT32 partitions. Thinking that changing a few file permissions would give me immediate user access to them, I experimented with the group management tools (groupadd, chgrp, usermod as in chapter 14 of Ubuntu Unleashed by Paul and Andrew Hudson, Sams, ISBN 0-672-32909-3). Unfortunately, the experiment failed for me. I can no longer use sudo ­ when I try I get a message saying that my user name is not on the sudoers file and my attempt to use it will be reported to Big Brother. In addition the items on the Ubuntu desktop System menus whose use requires root privileges (like Disk Manager and Apt-get) have disappeared. I can get root privileges by booting into the Recovery Mode but I do not know whether the problem can be overcome by editing /etc/sudoers with visudo. The explanation in Ubuntu Unleashed (p294-5) looks risky to me ­ I am getting too old for this sort of excitement. Perhaps I should reinstall Ubuntu, but that is a blunt instrument ­ it would be better to solve the problem with a few deft keystrokes. But what does a new installation do? Does it overwrite existing files and destroy the various configuration files? If it inherits the old config files I will be no further forward. </question>

<answer>A new installation will overwrite your system configuration files. Not only the ones you have inadvertently broken, but also the ones that are working perfectly, so it should be a last resort. The problem appears to be that your user is no longer a member of the admin group, probably due to incautious use of usermod, or that the admin group no longer has sudo rights. To check the latter, check that /etc/sudoers contains

%admin ALL=(ALL) ALL 

and add it with visudo if not. To see whether you are in the admin group, run id or groups in a terminal, as your normal user. If admin is not listed, you need to boot in Recovery Mode and add yourself to the admin group with

 gpasswd -a username admin

Unlike usermod, gpasswd adds groups to your user without affecting your existing groups. To make your FAT partitions available when you boot, you will need to add a line to /etc/fstab for each one.

/dev/hdb1 /mnt/shared1 vfat uid=john,gid=john,u mask=022 0 0

The first three fields should be clear, the device, mount point and filesystem type. The options field is the key: uid and gid set the user and group that will "own" the mounted filesystem, umask sets the permissions (remember that FAT has no concept of owners or permissions). The umask is subtracted from 666 for files and 777 for directories, so this sets files to 644 and directories to 755 (writable by you, readable by everyone). This assumes that your username and group are the same, which is how Ubuntu usually sets things up. If you changed your primary group when fiddling with usermod and friends ­ id shows your primary group ­ change it back with

usermod --gid groupname username

substituting the appropriate values for the user and group names. Incidentally, the Big Brother reference only means that the attempt to use sudo will be recorded in the system log file for the administrator to read. You won't be forced to spend three months in a camera-infested house with a dozen strangers who become even more strange by the day, being gawped at on live TV. </answer>

<title>Get Smart</title>

<question>Would you be able to suggest a couple of programs that read the S.M.A.R.T. signals from hard drives? Keeping an eye on hard drive health is a good thing. I run FreeBSD, so the ability to run natively would be nice, but as you know FreeBSD is able to run most Linux programs directly. </question>

<answer>The programs that you are looking for are in the smartmontools suite, and the instructions provided here for their operation apply to LInux as well as FreeBSD. The source is available from http://smartmontools.sourceforge.net and compiles on FreeBSD as well as Linux. The two programs are smartctl and smartd. Smartctl will run a number of tests on your drives, for example

smartctl --all /dev/hda

will show all S.M.A.R.T. information on the first drive (these tools are run with a drive name, not a partition name). Smartd is a daemon that will monitor your drives and report any problems to the system log, and mail you if you give it your address. S.M.A.R.T. ­ or Self-Monitoring Analysis and Reporting Technology to give it its full name ­can detect problems leading to failures and even report changes in temperature. The latter can cause excessive entries in the syslog as smartd reports every temperature fluctuation. Unfortunately, in the first few days of operation, I found this filled my daily logwatch mails with a lot of noise. Adding a suitable line to /etc/smartd.conf fixed this irritation, such as

/dev/sda -d sat -a -I 194 -I 231 -I 9 -W 5 -m
me@mydomain.com

The -I 194 -I 231 -I 9 -W 5 tells it to report only changes of five degrees and the minimum and maximum temperatures for the day. If changes of five degrees happen often, you have a potential problem, so this is a useful setting. The first part of the line, -d sat -a, specifies that this is a SATA drive and to run all tests. </answer>

<title>Tutorial licensing</title>

<question>I saw the tutorial for the web-based CD manager in LXF93. I thought that this would be a good way to start "coding" as such, as well as a good way to improve my Linux knowledge. My cousin runs an oriental rug shop and I thought I would try to make the manager, but tweak it for rugs to help him keep inventory of the rugs, on his shop computer. The article mentions converting the CD manager to another purpose, DVDs or hamsters. Would this include a simple inventory for rugs? However I wanted to check that I could use the coding tutorial provided as a base for the finished app? Would this violate any laws, if he uses the organiser in shop? He could have an easier time working and I could try my hand at coding. Someone told me that you can't use a program for personal financial gain. Would that mean I could make it, but not sell it? Or that I can make it but he would not be able to use it in shop? I don't want to break any laws, but not sure how this sort of thing would fall under anything. Since I'm not claiming to have designed it, nor would I sell it? Could I still get into trouble if I go ahead with it? </question>

<answer>The code could be adapted to most simple inventory tasks, it doesn't care whether the items it contains lie on the floor or spin round in a CD player (or a plastic wheel). All of our tutorial code is licensed under the GPL. This means you can do whatever you want with it, with one proviso: that anything you distribute based on this code is also licensed with the GPL. You can make any changes you want for your own use without worrying about anything other than whether it will work as you intended (although that can be the source of plenty of worry). If you want to use the code, or your own derivation of it, in a business then go for it. If you want to release your derived code as free software, then do so, create a project on Sourceforge (http://sourceforge.net), announce it on Freshmeat (http://freshmeat.net), and let others see, use and maybe improve on yourwork, which benefits you as well as them. If you want to sell your derived software, you can as long as it is still GPL licenced, but it may be wise to take legal advice in this case, or ask the advice of an organisation like the Free Software Foundation (www.fsf.org). In addition to the legalities of Open Source licensing, there are some niceties to consider. If you use someone's work to build something different or better, let them know. In the case of the magazine tutorials, the whole point is to help you develop your knowledge and skills, so we'd love to hear about any success stories. If you have benefited from the work of others, it is good to continue the process, so release your code. Even if you only intend to use it internally, why not let others benefit from it? Finally, if you benefit commercially from another project's efforts, it would be nice to make a donation. We get paid for writing those tutorials, so that doesn't really apply here, but feel free to take out a subscription, or donate backissues of LXF that you no longer need to the computing department of a local school or college, or even town library! </answer>

<title>Separate home partition</title>

<question>I've read several times in LXF about the benefits, but I didn't initially set up my hard disk like that. Is there a way to create a new home partition after the fact or do I need to wipe out everything, reinstall, and restore my home directory to the re-partitioned drive? </question>

<answer>It is possible, but care is needed. As always, backup first! The danger is of the process being interrupted by a power failure or other software crashing the computer. Working with in-use filesystems should be avoided too, so boot from the live CD distro like Knoppix for this task. There are three stages: resize your root partition, create a new home partition in the space made and move your data over. The process is a lot easier if you have plenty of free space; if your drive is nearly full, move some files to DVDs or an external disk. While it is possible to resize a partition from the command line with a combination of cfdisk (or fdisk) and appropriate resizing tools for your filesystem, it is easier with GParted or QTparted (on Ubuntu and Knoppix CDs respectively). The Knoppix live DVD has both. We use GParted here, but they work similarly. Partitions cannot be resized when mounted, so if a mount point shows alongside the partition in GParted, right-click the partition and select unmount. When resizing a partition, it is normally only possible to move the end point, so start by dragging the end of the root partition to the left until it is the size you want. It is considered good practice to not fill a partition beyond 80 per cent, filling that last 20 per cent can lead to fragmentation, but remember that you are going to remove the contents of /home after resizing, so you can probably pull that slider as far over as you want. Now create a partition in the vacated space and press Apply to start the process. This is the risky part, it is best to leave the computer undisturbed while it completes this. Once you have created the new partition, it is safest to reboot, to the live disc again, to make sure the kernel knows about the new partition layout. Now you need to move the files from the old home directory to the new partition. This must be done as the root user, so open a terminal and type su to become root - or sudo -i if using an Ubuntu disc. If the root partition is on /dev/hda1 and the new partition on /dev/hda2, the commands to make the copy are

mkdir -p /mnt/{root,home}
mount /dev/hda1 /mnt/root
mount /dev/hda2 /mnt/home
mv /mnt/root/home/* /mnt/home/

or you can replace the last command with

rsync -a /mnt/root/home/ /mnt/home/
rm -fr /mnt/root/home/*

which is a little slower but preserves all timestamps and can be interrupted and resumed is necessary. This assumes you have a relatively small amount of data in the home directory, otherwise you will not be able to shrink the root partition as much as you need and will have a lot of wasted space after moving the file. </answer>