Answers 116

From LXF Wiki

Answers 116

<title>USB boot alternatives</title>

<question>I have tried to install Ubuntu on my old HP Pavilion Notebook. Since I was short of disk space (and still need Windows) I installed it on a USB disk drive. The install went well, but rebooting produced a Grub error. I now understand from reading around that some old machines do not allow booting from USB, indeed there is no USB option in the BIOS (or Smart Boot Manager). So I restored my Windows boot sector with the MS recovery disk, which is now fine, but leaves me with a USB disk that is loaded up with Linux (fully intact from what I can tell), without any possibility of use. As Linux advocates this must must annoy you as much as me! I was wondering – since my machine will boot from a floppy disk, is it possible to put some sort of Linux kernel on a floppy that would boot Linux but access the redundant disk for the bulk of the Linux applications (perhaps even the graphical user interface)? I see advantages to this over the Live DVD, in that I could maintain my changed preferences etc and augment the distro with other applications I would like to have. Is this a viable option? I would greatly appreciate some advice. </question>

<answer>This is not just annoying from a Linux perspective: if your computer won’t boot from USB, the operating system is irrelevant. However, USB booting is a source of great frustration, as not only is the computer’s BIOS a factor, but some USB devices work better than others, so there is much trial and error involved. It would appear that you installed Grub to your hard disk, so the system at the time of installation sees the hard disk as the first disk and the USB disk as the second, but when you try to boot up, the second (USB) disk is not there, resulting in the Grub error (which is almost certainly a “disk not found”). It looks unlikely that your computer supports USB booting if there’s nothing in the BIOS, but some computers do provide a boot menu if you press a key (usually delete, F12 and F2 are most commonly used) immediately after starting up. Unfortunately, there is no standard for this, so you’ll have the read the manual of watch the power on messages to see of there is such an option, or try holding down a different F key each time you boot until you strike lucky. You have ruled out the use of a Live DVD, but many of them have an option to mount a USB drive as your home directory, enabling you to save your documents and settings and even install extra software, although there isn’t much that something like a Knoppix DVD doesn’t include. If your computer really doesn’t support USB booting, this would seem a better option than using an unreliable floppy disk. Boot from a Knoppix CD or DVD, plug in a USB drive and select the Menu item Knoppix > Configure > Create A Persistent Knoppix Disk Image. Answer the questions, and it will create a file called knoppix.img on the disk that contains your home directory and settings. When you reboot, Knoppix should detect this file and ask if you want to use it, or you can specify the location by typing

knoppix home=/dev/sda1

at the boot prompt. </answer>

<title>Advanced file permissions</title>

<question>I would like to share files in a particular directory between certain (local) users. Those users should have read/write access to these files. I would like to leave the umask set at 022 for default permissions on all other files. I can create a group and set the permissions on the files in the shared directory to 660 with the group set correctly, but that doesn’t solve the problem for new files, which may be created by any user. Not all the users have Linux savvy, and anyway it feels unnecessary to have to change groups and permissions by hand every time. One option would be to write a daemon to watch for new files and change permissions. Is there a better way, and has anyone done it already to save me the coding practice? </question>

<answer>You have discovered one of the limits of standard Linux file permissions. You could setgid the directory, meaning that any member of its group could create files in it, but they would still be writable only by the user that created them. The answer lies in ACLs (Access Control Lists) which provide much finer control over file permissions. There are three prerequisites to using ACLs. First, your kernel must include ACL support for the filesystem you are using (standard distro kernels will already have this). Then you need to mount the filesystem with ACLs enabled, by editing etc/fstab and adding acl to the list of options, for example by changing

/dev/sda5 /home ext3 defaults,noatime 0 0

to

/dev/sda5 /home ext3 defaults,noatime,acl 0 0

and either remounting the filesystem with

mount /home -o remount

or rebooting. This step is not necessary if you use XFS, as it has ACL support by default, but if you use ext2/3 or ReiserFS you’ll need to change fstab and force a remount. Finally, install the acl package, which includes the userspace tools used to control ACLs. Now you can add the users to the same group, say, ‘project’, and set things up like this

mkdir shared
chmod 2775 shared
setfacl -m default:group:project:rwx shared

As long as your user has full write permissions in the directory where you do this, you do not need to be root. This creates the directory and makes it group-writable and executable. The last line does the clever stuff, setting a default access rule for the directory that all files have rwx permissions for all members of the project group. A default rule applies to all new files, so you need to issue this before you place any files in the directory. Or you can set existing files with

setfacl -R -m group:project:rwx shared

This has no default parameter, but -R makes it recurse over all existing files and directories. You can also set access controls for individual users, like

setfacl -m default:user:fred:r-x shared

which gives this user read-only permissions. While you are experimenting with setting ACLs, you will find these commands useful

getfacl shared
setfacl -x default:user:fred shared

The first lists the ACLs for a file or directory, the second removes them. The syntax for -x is the same as -m, except that you don’t give the permissions. </answer>

<title>Disk diagnostics</title>

<question>Lately Seti@home is giving a signal 11 error while it does its number crunching. This error has been related to problems with hard disks. The problem is that I don’t know what the best way to check my hard disk is. I use Kubuntu 8.04, and have four partitions: one for Windows (vfat), swap, root (ext3) and /home (ReiserFS). I had the ReiserFS partition because it was the default when I installed my first Linux (SUSE 7.2). I think, though I could be wrong, that I haven’t formatted or checked the /home partition since I created it six years ago, which I believe is quite impressive (for me, at least) – six years without any problem! What should I do? Boot using a Live CD? Just use some CLI magic? Or is there something graphical I should use? </question>

<answer>There are two separate entities to consider here: the physical disk and the filesystem residing on that disk. The filesystem is the most straightforward, as you can simply run fsck over it. Of course, nothing is quite that simple, and you should not try to fsck a mounted filesystem. You could unmount /home, but that would only work if you’ve set up a root login. Then you could logout of the desktop, switch to a virtual console with Ctrl+Alt+F1 and log in as root. You need to log out of the desktop because you cannot unmount /home while any user but root is logged in, this is why you need a separate root login and cannot use sudo. Now you can run fsck on that partition

fsck /dev/sda4

The alternative is to use a Live CD or DVD, which would also enable you to fsck the root partition. This is usually the simpler option if you do not mind rebooting the computer. Any Live CD distro will contain the fsck tools, but my current favourite for this type of work is GRML (www.grml.org). This is based on Debian and aimed at system administration and rescue. Another alternative would be SystemRescue CD (www.sysresccd.org). Fsck checks the filesystem for corruption but does not look at the underlying hardware. For this, Smartmontools is a good choice. It’s included in the software repositories of most distros. Smart (Self-Monitoring, Analysis, and Reporting Technology) is a way for hard disks to monitor their own performance, running a set of self-tests to detect and even predict failure. You may need to enable an option in your computer’s BIOS for Smart to work, then install Smartmontools and edit /etc/smartd.conf to tell it which drives to test and how. A good starting point is

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

This checks /dev/sda, a SATA drive, to ignore attributes 9 (power on time), 194 and 231 (temperature) but report temperature changes more than five degrees. The -a option says to monitor all other attributes, while -m gives an email address for errors and warnings. If you have a PATA/IDE drive, use d ata. Set smartd to start at boot in your services manager and your driv (s) will be continually monitored. You can run an immediate health check on the drive with

smartctl --health -d sat /dev/sda

and run various tests with

smartctl --test=TEST -d sat /dev/sda

where TEST is one of offline, short or long. See the smartd and smartctl man pages for (a lot) more information on the various options, attributes and tests you can use. </answer>

<title>What he sed</title>

<question>I have a large file containing 800–900 lines. Now I want to write a script that can find a particular expression in the file and then change or replace the one word in that line and another word in line below that particular line. For example, if a file contains

I am in London
Family is in Canada

I want to find the expression “in London” and replace “I am” with “You are” and in the below line I want to replace the “Family is” with “girlfriend in”. I took such expressions because my search may be a expression containing spaces and replacement also contain replacement of whole sentences. </question>

<answer>Sed (Stream Editor) is the program you need. This can replace, delete or otherwise manipulate data in a file using regular expressions. The simplest replacement is something like

sed ‘s/I am/You are/’ myfile >newfile

This is the most common usage of Sed, to replace occurrences of a string with another, but you have asked for something far more specific, to only replace on lines containing a string and the following lines. This can be done by limiting the command used to an address range. The command used here is s, the substitution command, and in the above example is applied to every line of the file. To limit its application, you prefix is with an address range, which can be a pair of line numbers, except you don’t know the line numbers in this case, so we use

sed ‘/in London/,+1s/I am/You are/’ myfile >newfile

Here the address range consists of two components, separated by a comma. The first is a pattern match, defined by the slashes surrounding the string, so it matches any line containing “in London”. The second component is +1, which extends the range to one line after the line that starts the range. You can use any number here, but each line can only be in one range. So if lines 3, 4 and 7 match, the above command would be applied twice, first to lines 3 and 4 and then to lines 7 and 8. All other lines are passed through unchanged. The address is followed by the s command. In this example, we make only one substitution, to apply multiple commands to the addresses range, enclose them in braces, separated with semicolons. This will do just what you are asking for.

sed ‘/in London/,+1{s/I am/You are/ ; s/Family
is/Girlfriend/}’ myfile >newfile

In each case, we have redirected the output to a new file. Sed has a -i option to edit the file in place, but don’t use this until you are sure your syntax is correct or you have a backup of the original file, as a slight typo could destroy your data. Sed doesn’t replace the file until the command is complete, so a syntax error will leave the file unchanged, but a valid command that doesn’t do what you expect it to would cause a problem. Sed is very powerful but the man page is basic, so read either the info docs or http://sed.sourceforge.net. </answer>

<title>Failsafe booting</title>

<question>Most distros use Grub nowadays and I can see how it is easier to use than Lilo. But there appears to be one very important feature missing: Lilo can use the -R option to boot a kernel once only, so the next time it goes back to the old default, which is handy for testing new kernels. If it fails, a reboot goes back to the old kernel. It is really important to be able to do this on a remote server and I can’t see how to do it in Grub. </question>

<answer>This is possible with Grub, although a little less intuitive. There are a couple of methods available, but the fallback is the most flexible.Here is an example configuration

root (hd0,0)
timeout 10
default saved
fallback 1 2
title new kernel
kernel /boot/vmlinuz root=/dev/
sda1 panic=10
savedefault fallback
title old kernel
kernel /boot/vmlinuz.old root=/
dev/sda1 panic=10
savedefault fallback
title rescue shell
kernel /boot/vmlinuz.old root=/
dev/sda1 init=/bin/bb
savedefault

The default saved line tells Grub to use whichever menu item was saved in /boot/grub/default (or the first item if that file is not present). So Grub boots the first item, your new kernel and the savedefault line now sets the default to 1, the second menu choice (remember Grub counts from zero). The panic=10 kernel option means that if the kernel panics, it will reboot after ten seconds, and now Grub will use your new default of the second That should be enough, but for belt and braces you can repeat the process with this kernel, which will then use your second fallback if things go wrong. This final choice simply uses savedefault which saves itself as the default. You may have noticed a slight flaw with this process in that your fallback is always saved as the new default, even if the boot succeeds. This is handled by running the command

grub-set-default 0

at the end of a successful boot, usually from your rc.local script, depending on your distro. Although this is a more complex system that Lilo’s -R (there is a slightly simpler alternative) is is far more useful as it is always in effect. Any time your computer reboots without completing the full boot process, it switches back to your old kernel, whereas Lilo requires a specific command to do this. </answer>

<title>Automated virtualisation</title>

<question>On start up I want Ubuntu to start without the need for me to input a username and password and then open VMware and start a virtual machine. I know how to do all of these things manually, but is it possible for me to write a small batch file (if that’s the right expression) to do it for me automatically? </question>

<answer>There are two steps here: logging in to your user’s desktop automatically, and running a program after logging in. The first is achieved in Ubuntu 8.10 by selecting the option to automatically log your user in during installation. If you have an earlier Ubuntu or you have already installed 8.10, you can set this option by running System > Administration > Login Window, go to the Security tab, tick Enable Automatic Login and select the user you want logged in. Gnome will start any program you tell it to when it starts up. Go to System > Preferences > Sessions, press Add and type the command you want to run, along with a name and description. To start VMware Workstation with a particular virtual machine, use

vmware -X /path/to/virtualmachine.vmx

Where -X tells VMware to both start the virtual machine and switch to full-screen mode and the rest is the path to the .vmx file for the virtual machine. If you’re using VMplayer, replace vmware with vmplayer in the above command. If you use VMware-server, you need to make sure that the server is running at startup (use the Ubuntu session manager for this), and then use vmrun to start the virtual machine. </answer>

<title>Protecting my data</title>

<question>I was burgled last week! They took two TVs and a load of PC equipment including all my external drives. This has made me really paranoid about keeping the data on my laptops and desktop safe. Is there a way to encrypt the hard drives of my PC and laptops without reformatting them? </question>

<answer>Sorry for your bad news. There are two main ways of encrypting a filesystem: one is to create an encrypted block device and then create a filesystem on top of that. This is the method I generally use, but the actual implementation depends on your chosen distro as it has to request a decryption key and unlock the filesystem before it can be mounted during the boot process. The commands (run as root) to implement this are

cryptsetup luksFormat /dev/sda2
cryptsetup luksOpen /dev/sda2 home

The first creates an encrypted block device on /dev/sda2, and only needs to be done once. The second opens the encrypted device, prompting for a passphrase when used like this, although it can also use a keyfile on a removable device. It creates the block device /dev/mapper/home, which you can format, mount and use just as you would any other block device. The main drawback here is that you have to reformat part of your disk to use it. The other option is a stacked filesystem, which is where one filesystem is layered on top of another. The most popular option for encrypted stacked filesystems used to be EncFS (a Fuse) filesystem, but now the Linux kernel has ecryptfs built in. You need to install ecryptfsutils, which should be in your distro’s repositories and may already be installed. This contains the tools to create and manage ecryptfs filesystems. Then you can create one with the following:

mkdir .private
mkdir private
sudo mount -t ecryptfs .private private

You will be asked some questions; set the key type to passphrase, the encryption cipher to AES, key bits to 16 and passthrough to off. You can use different settings if you wish, but these are a good starting point. The encrypted layer is created and anything you write to private is actually saved as an encrypted file in .private. Try copying some files to private and then reading them. Now try to read the same files in .private. Unmount private with

sudo umount private

and this directory will now be empty while the encrypted files are still present in .private. To mount the directory again, you need to specify the options you gave when you created it.

sudo mount -t ecryptfs .private private -o
key=passphrase,ecryptfs_cipher=aes,ecryptfs_
key_bytes=16,ecryptfs_passthrough=n

You will be asked for the passphrase again, and then your files will be readable. You can attach this command to a launcher icon or call it from your desktop’s session manager to have the directory mounted automatically when you log in. Make sure that you set the command to run in a terminal, as this will be needed if you want to input the password. You could also put the options in /etc/fstab, like this:

home/user/.private /home/user/private ecryptfs
oauto,user,key=passphrase,ecryptfs_
cipher=aes,ecryptfs_key_bytes=16,ecryptfs_
passthrough=n 0 0

The use of noauto prevents a mount attempt when booting while the user option lets any user mount it, provided they know the passphrase, without the need for sudo. You can either keep your confidential data in private and change the configuration of your programs, or move the various data directories into the private directories and set up symbolic links from their original locations. When making backups, copy .private instead of private, then your backed up data will remain encrypted. </answer>

<title>The fat lady crashes</title>

<question>I purchased Linux Format yesterday and tried Opera 9.26,9.27 and 9.52 from my Mandriva 2008.1pwp. I then installed some of the widgets that interested me. On trying to use it my PC [Dell Optiplex GX110] kept locking up. I rapidly removed it. Any suggestions? </question>

<answer>Did Opera work correctly before you installed the widgets? If so, try a clean installation of Opera, then install the widgets one at a time, running them for a while between each installation. When you find the one that causes the problem, file a bug report on the developer’s web site so they can fix it. You may find it more useful if you launch Opera from a terminal, just type

opera

to launch it. This won’t prevent any of the problems, but it will give more information about what went wrong. Even if the output means nothing to you, it could be helpful to the developers in tracking down the source of the failure. If the problem is with Opera itself, try to open it again from a terminal to see any error messages that might come up. In many cases, pasting the error message into your favourite search engine will bring up reports of others sharing the same problem, and often a solution. </answer>

<title>Video for Androids</title>

<question>I have recently treated myself to a G1 phone running Google’s Android operating system. I really like it but am faced with constant battles with my iPhone-owning friends “can it do this” or “why can’t it do that?”. So far it has only failed to acquit itself on two counts; I can live with it not imitating a lightsabre, but trying to get videos on to the thing in a playable format is driving me nuts. Is there a simple way to convert video files or DVDs to a format playable by the G1? For that matter, what formats does it support? I am using OpenSUSE 11.0, if that makes a difference. </question>

<answer>The G1 doesn’t have a native video player, but there are a few available on the Android market, like the imaginatively named Video Player or Cinema. The range of formats supported by these programs is extremely limited, so you have to be careful to use exactly the right settings when converting your videos or they simply will not play. A good program for performing these conversions is Avidemux (http://fixounet.free.fr/avidemux) but this is not in the default repositories for OpenSUSE 11.0. To get it, go into the Software Repositoriessection of Yast, press Add and select Community Repositories. Add Packman, which contains Avidemux. You should also enable the Main Repository (OSS), if this is not already done, for its dependencies. Now you can search for and install either avidemux-gtk or avidemux-qt in the Software Management section. These are basically two faces of the same program. Fire up Avidemux, press Open and select a video file. Set the Video type to ‘MPEG-4 ASP (lavc)’, press Configure and set the Encoding mode to either ‘Constant Bitrate’ or ‘Two Pass – Average Bitrate’. The latter gives better quality, at the expense of a doubling of processing time. Pick a bitrate, bearing in mind that you will be viewing this on a threeinch screen, so don’t go silly. I find 384kb/s gives decent quality and small files. In the Filters window, double-click on MPlayer Resize and set the width and height to 480x320, press OK then Close. The Audio should be set to ‘AAC (FAAC)’ at a bitrate of 96 or a little higher. Set the format to MP4 and you’re ready to go, but first you should select a small section of the video for a test run. Drag the slider to a suitable start point and press the A button, then move it on a minute and press B. Press Save, pick a filename and give it a few seconds to process the video, depending on the speed of your computer. Make sure you give the file a .mp4 extension, or Video Player will not find it. Copy the resulting file to the SD card of the phone (remember to disconnect afterwards to make the SD card visible to the phone again), and start Video Player. It should pick up your video wherever you saved it on the SD card. If it plays well, you can transcode the whole video. </answer>