Pages

Friday, June 22, 2012

How to fix "Cannot Update ICEauthority" or .ICEauthority

The .ICEauthority file is a configuration file used by the Gnome desktop in Linux.

Sometimes the file can become corrupted or otherwise stop functioning properly. If this occurs, then a message will appear before the desktop has completed loaded stating that the ICEauthority or .ICEauthority file cannot be updated. The most common symptom that I recognize when this occurs is that Gnome only allows me to have one accessible workspace on the desktop (I usually like to have six available).

In trying to resolve this problem, myself, I have often encountered blogs that tell the user not to log into the GUI (graphical user interface) as root and then the file won't get corrupted. However, I have never logged into the GUI as the root user, but my .ICEauthority file still stops functioning, at time. There is another cause for this corruption, but I don't know what it is; I only know how to fix it once it occurs.

Command Sequence:

First, I looked to see where the file was located on my system (I found three locations), by opening a terminal and switching to the root user (or super user) and using the 'find' command.

1. su -

2. Enter root password

3. find / -name .ICEauthority

4. cd [path to .ICEauthority]

5. rm .ICEauthority

6. reboot


Explanation:

1. 'su' stands for 'change user' and the hyphen means that i want to change to the root user, with both root permissions and environment variables, instead of my personal environment variables. Using the 'find' command as the root user will allow me to search through files that otherwise would not be accessible as a regular user.

2. This is the system or root password. Access to this password will be required to search through directories that only permit root access.

3. 'find' is a very useful utility. For additional information on 'find', check the Linux manual page with "man find".

The syntax is "find [target directory] -name [filename or pattern]. Therefore, by designating '/' (root directory) for the target directory, we are able to search the entire file system for the desired file. Following the target directory, '-name' is the option that tells 'find' we will be designating an exact file name for which it is to search, within the target directory (-iname makes the search case-insensitive). The file name that we want to find is the final element of the command. 'find' will then print a list of paths and file names, matching the given parameters, as standard output to the screen, before returning us to the command prompt.

4. 'cd' stands for 'change directory'. We need to access the directory that contains the .ICEauthority file, as shown in the results list from the 'find' command in the prior step.

5. 'rm' stands for 'remove' and is used to delete the file. The .ICEauthority file will be re-created when the system restarts, so there isn't any harm in deleting this file, especially if it is already corrupted.

6. 'reboot' is a command-line command that will safely shutdown the system and immediate restart it. This process will re-create the .ICEauthority file for each of the users and the graphical desktop will once again be available.

Friday, March 23, 2012

Linux File Structure / Directories Description

Initially, the Unix/Linux file structure can be difficult to understand, for someone who is used to working in the extended- and full-name environment of Microsoft Windows. Many of the directories in the Unix-based operating systems are abbreviated, which helps reduce key strokes once the names can be deciphered, but have also been known to add a great deal of confusion for those who are exploring it for the first time. I experienced this first-hand!!

The directories below do not represent an exhaustive list of directory names in the Linux- and Unix-based operating systems, but it is the author's intent that this list will at least succeed in establishing a basic level of understanding, pertaining to the abbreviated names used and the purpose of the directories. Also, it is hoped that this information will act as a launching pad for newly introduced Unix/Linux users, who are still trying to understand the terminology, and lead them to many happy years of Linux loyalty!

********************************************************************************

. = current directory

.[directory] = hidden directory (Any directory or file with a dot [.] in front of the name is a hidden file or directory. Files and directory typically are not hidden for security purposes [that's what the permissions and file ownership are for], but rather as a matter of convenience for the average user. If the files or directories do not need to be accessed often, such as with system configurations, then they are hidden so as to prevent the file lists from appearing cluttered)

.. = parent directory to the current directory

/ = root (This is the primary directory, or 'root' of the file system)

bin = binary (This is where binary files/compiled code is located. Functions and commands that come with the base operating system, and that are needed by regular users, are typically located in this folder)

boot =bootup (These files are executed when the system is booting up; ie starting)

dev = device (These are where the regularly attached devices are mounted, such as internal drives and hardware)

etc = et cetera (This was initially intended as the directory for anything that didn't fit into one of the other specific directories)

home = home (This is where the home directories for the non-root/regular users will be created)

lib = library (This is where the resource files and data are kept for installed applications and system tools)

lost+found = orphaned data (This is where the unclaimed blocks of data or non-allocated files are placed. This data can become lost when the file system is unable to shut down properly, such as with a power-outage)

media = media storage (This is where additional storage devices are attached, like USB thumb drives or external DVRs)

mnt = mount (This directory can be used to mount external storage devices or remote file systems, such as a Windows drive, to the local file system)

opt = option

proc = processes (These files don't really exist. This portion of the operating system operates on an as-needed basis and these are actually virtual files, rather than real files. Do not include this directory in your system backup! Once when I tried doing a .tar.gz backup on my 320GB system and included this directory, it created a 1.5TB file, even with compression, before I canceled the backup!)

root = root user (This is the home directory for the root, or super user)

sbin = secure binary (System administrative functions and commands that come with the base operating system, that are used by the root user and other system resources, are typically located in this folder)

src = source (This is the directory for source code)

srv = service

sys = system

tmp = temporary (This is for any files that will only exist temporarily, such as files for currently running applications, that will be deleted once the associated processes are stopped)

usr = Unix system resources (Often pronounces as "user", but done so in error. This directory should be pronounced as the individual letters, u-s-r. This is where some of the primary system resources, drivers, and libraries are stored)

usr/bin = Unix system resources/binary (This is where binary files/compiled code is located, that the system has installed after the usr directory was mounted; i.e. after the operating system's initial installation. This includes functions and commands that are needed by regular users)

usr/sbin Unix system resources/binary (This directory includes system administrative functions and commands that the system has installed after the usr directory was mounted; i.e. after the operating system's initial installation. This includes functions and commands that are used by the root user and other system resources)

usr/local/bin Unix system resources/local/binary (Functions and commands that are installed by locally by the root user, but are intended for regular users, should be placed in this folder)

usr/local/sbin = Unix system resources/local/binary (System administrative functions and commands that are installed locally by the root user should be placed in this folder)

var = variables (This directory contains the main system logs, in additional to other data. For example, on a SUSE Linux system, the error log is found at /var/log/warn)

Thursday, January 26, 2012

Perl Script for Automatically Backing Up Log Files

This Perl script was written for backing up system log files in the /tmp/ directory and tomcat6 log files in the /usr/share/tomcat6/logs/ directory.

Modify the code to suite your personal needs. Hopefully this will provide a helpful platform to build of off!


**********Begin Perl Script for Log Backup***********

#!/usr/bin/perl -w

use strict;

########################################################################
#
# Created by Andrew Stout <stout.andrew@ymail.com>
# Origination Date: Jan 24, 2011
#
# Current Version: 1.1.2
# Release Date: Mar 13, 2012
#
# This Perl application archives Linux system and Apache Tomcat log files for
# system and network administrators. Each file is compressed and renamed with a
# timestamp, that identifies when the compression occured, appended as a prefix
# to the original filename and .tar.gz as the suffix, representing the compression
# methods used. These files are then relocated to the /home/logArchive/ directory
# for local-system storage
#
# To automate this archive script, as root user:
# 1. cp or mv this script (archiveLogs_1.1.2.pl) to /etc/bin/
# 2. Change the user and group ownership of this script to root, with chown, and
# set the permissions to allow read, write, and execute by the root user, with
# chmod.
# 3. Open /etc/crontab and add '1 0 * * *  root archiveLogs.pl', then save and close  # the file.
# 4. In 'yast runlevel', enable the cron service, so that it will automatically start in
# the event of a reboot. This will ensure the archival script remains automated.
#
########################################################################

# Designates and makes, if not already in existance, the target directory for archiving
# compressed log files
my $destination="/home/logArchive";
system "mkdir $destination";

#### .log Evaluation
# This opens the specified system directory and reads the files into the DIR filehandle,
# then finds the files ending in ".log" and places them into an array (a list). The contents
# of this array are then evaluated for size to determine whether or not they are large enough
# to archive
chdir "/tmp"
    or die "cannot chdir to ../tmp/: $!";
opendir DIR, "/tmp/";
my @logEntry = grep {/.+\.log$/} (readdir DIR);
foreach my $filename (@logEntry) {
        my $size = (stat($filename))[7];

    # Makes a copy of the orginal filename and then removes the extension,
    # while preserving the original. The truncated filename will be inserted
    # into the new archival name, once the file has been compressed
    my $filenameTrunc = $filename;
    $filenameTrunc =~ s/^(.+)\.log$/$1/;

        # Ensures that existing log files (ones created automatically by the system) have
        # the correct owner:group and permissions settings to grant the configured system
        # users writable access to the logs. This is generally only necessary before the log
        # file has grown large enough to be archived for the first time. After that, this
        # script will automatically replace the log files and configure the proper ownership
        # and permissions. The permissions will allow the ownwer to read and write to the file,
        # members of the same group to read and write to the file, and other users to only
        # read the file, but not write. This is based on a three-bit binary activation for
        # "rwx", with read = 4, write = 2 (r+w=6), and execute = 1.
        if ($size < 1_048_576) {
                system "chown ido.emf-fiscal.net:mercury $filename && chmod 664 $filename";
        }

    # EValuates the file size, then compresses and archives it if it is larger than 1MB.
    # If the file is smaller than 1MB, it leaves it alone.
    if ($size >= 1_048_576) {
        # Extracts the year, month, day, hour, minute, and second elements from the
        # localtime function, in that order, and formats the year to be four digits,
        # with every other element being two digits, with a preceding 0, if necessary.
        # This time stamp is then placed at the beginning of the new filename, when
        # the log file is compressed
        (my $Y, my $m, my $d, my $H, my $M, my $S) = (localtime)[5,4,3,2,1,0];
        my $timeStamp = sprintf
                "%4d%02d%02d%02d%02d%02d",
                ($Y+1900, $m+1, $d, $H, $M, $S);
           
                # Shorten the code by assigning a temporary filename for the original file to
        # $tmpFile. The original file will be moved to this temporary name, if it does
        # not exist, and the original file will be emptied for new entries. Compression
        # will then be performed on the new temp file
        my $tmpFile = "$timeStamp.$filenameTrunc.tmp";

        # Checks whether or not the temporary filename already exists. If not, it moves
        # the original file to the new filename, then empties the original log file.
        # The compression and renaming, for the log archive is performed on the new
        # temporary file, then the temporary file is deleted automatically. This prevents
        # the potential loss of data entries, which could occur if the compression were
        # performed on the original file at the same time as new data was being written.
        if (-e $tmpFile) {
            warn "Can't rename $filename to $tmpFile: $tmpFile already exists\n";
        } elsif (rename $filename, $tmpFile) {
            system "touch $filename && chown ido.emf-fiscal.net:mercury $filename && chmod 664 $filename";
            system "tar -czf $timeStamp.$filenameTrunc.tar.gz $tmpFile && echo $filename && rm $tmpFile";
            system "mv $timeStamp.$filenameTrunc.tar.gz $destination";
        } else {
            warn "rename $filename to $tmpFile failed: $!\n";
        }
        }
}

#### .out Evaluation
# This opens the specified system file directory and reads the files into the DIR filehandle,
# then finds the files ending in ".out" and places them into an array (a list). The contents
# of this array are then evaluated for size to determine whether or not they are large enough
# to archive
chdir "/usr/share/tomcat6/logs"
    or die "cannot chdir to ..tomcat6/logs/: $!";
opendir DIR, "/usr/share/tomcat6/logs/";
my @outFiles = grep {/.+\.out$/} (readdir DIR);
foreach my $filename (@outFiles) {
    my $size = (stat($filename))[7];
   
    # Makes a copy of the orginal filename and then removes the extension,
    # while preserving the original. The truncated filename will be inserted
    # into the new archival name, once the file has been compressed
    my $filenameTrunc = $filename;
    $filenameTrunc =~ s/^(.+)\.out$/$1/;

        # Ensures that existing log files (ones created automatically by the system) have
        # the correct owner:group and permissions settings to grant the configured system
        # users writable access to the logs. This is generally only necessary before the log
        # file has grown large enough to be archived for the first time. After that, this
        # script will automatically replace the log files and configure the proper ownership
        # and permissions. The permissions will allow the ownwer to read and write to the file,
        # members of the same group to read and write to the file, and other users to only
        # read the file, but not write. This is based on a three-bit binary activation for
        # "rwx", with read = 4, write = 2 (r+w=6), and execute = 1.
        if ($size < 1_048_576) {
                system "chown tomcat:tomcat $filename && chmod 664 $filename";
        }

    # Evaluates the file size, then compresses and archives it if it is larger than 1MB.
    # If the file is smaller than 1MB, it leaves it alone.
    if ($size >= 1_048_576) {
                # Extracts the year, month, day, hour, minute, and second elements from the
        # localtime function, in that order, and formats the year to be four digits,
        # with every other element being two digits, with a preceding 0, if necessary.
        # This time stamp is then placed at the beginning of the new filename, when
        # the log file is compressed
        (my $Y, my $m, my $d, my $H, my $M, my $S) = (localtime)[5,4,3,2,1,0];
        my $timeStamp = sprintf
                "%4d%02d%02d%02d%02d%02d",
                ($Y+1900, $m+1, $d, $H, $M, $S);
           
        # Shorten the code by assigning a temporary filename for the original file to
        # $tmpFile. The original file will be moved to this temporary name, if it does
        # not exist, and the original file will be emptied for new entries. Compression
        # will then be performed on the new temp file
        my $tmpFile = "$timeStamp.$filenameTrunc.tmp";

        # Checks whether or not the temporary filename already exists. If not, it moves
        # the original file to the new filename, then empties the original log file.
        # The compression and renaming, for the log archive is performed on the new
        # temporary file, then the temporary file is deleted automatically. This prevents
        # the potential loss of data entries, which could occur if the compression were
        # performed on the original file at the same time as new data was being written.
        if (-e $tmpFile) {
            warn "Can't rename $filename to $tmpFile: $tmpFile already exists\n";
        } elsif (rename $filename, $tmpFile) {
            system "touch $filename && chown tomcat:tomcat $filename && chmod 664 $filename";
            system "tar -czf $timeStamp.$filenameTrunc.tar.gz $tmpFile && echo $filename && rm $tmpFile";
            system "mv $timeStamp.$filenameTrunc.tar.gz $destination";
        } else {
            warn "rename $filename to $tmpFile failed: $!\n";
        }
    }
}

# Evaluate the age of files in the log archive and move those that are a year old to a new location
chdir "/home/logArchive"
        or die "cannot chdir to ..logArchive/: $!";
opendir DIR, "/home/logArchive/";
my @archiveFiles = grep {/.+\.out$/} (readdir DIR);
foreach my $filename (@archiveFiles) {
        my $size = (stat($filename))[7];
}

system "/etc/init.d/apache2 restart";
system "/etc/init.d/tomcat6 restart";


**************End Perl Script for Log Backup*************

Monday, January 9, 2012

Tutorial: Linux File Permissions and Ownership

            WARNING: Running system tasks as the root user is very dangerous! Files deleted from the command line cannot be recovered and serious system damage can occur! Use the root user account with extreme caution!

            This tutorial uses the yourFile file to explain Linux file permissions and ownership. This explanation can be applied to almost any Linux file system, because permissions and ownership formatting is pretty standardized across the Linux platform.

Identifying Permissions and Ownership

Example 1: Original
  • -rw------- 1 root root 56318 Sep 21 16:40 yourFile
Example 2: After changing permissions with chmod and ownership with chown
  • -rwxrwxrwx 1 tester users 56318 Sep 21 16:40 yourFile
            In Example 1, above, we see the file type (shown in the first position as a '-'); the access permissions (shown in the latter nine positions as 'rw-------'); the owner of the file, referred to as the user (root); the user's group (root); the file size in bytes (56318); the date and time the file was last modified (Sep 21 16:40); and the name of the file itself (yourFile).
            According to the details listed in Example 1, the file is a regular file (-) and the owner has permission to read and write to the file, but cannot execute it (rw-). However, no one else has permission to access the file ('---' and '---'), except for the root user, who automatically has access (regardless of who the file belongs to).
            Listed after the permissions, we see 'root root'. The first 'root' (on the left) represents the owner of the file. The owner is actually referred to as the user, in the context of assigning permissions (more on this later). The second 'root' (on the right) represents the group that the owner belongs to. This example can be confusing, because both the owner and the group have the same name.
            It is easier to understand ownership by looking at Example 2, in which the owner is 'tester' and the owner's group is 'users'.

File Type 
           As stated, the first position in Example 1, above, is a hyphen (-) and identifies the file type. Typically speaking, the file type will never be changed, so we will list the possible values for this position, but not discuss how to change them: 


Table 1: File Types

  • - = regular file
  • d = directory
  • l = symbolic link (similar to a shortcut that is used on a MS Windows desktop)
  • s = socket (configured IP address and port number combo)
  • b = block device
  • c = character device
  • p = named pipe

Permissions
            There are three sets of permissions, following the file type, and each set includes three letters that identify the individual permissions of read, write, and execute: 

Table 2: Permissions Sets
  • The first set of 'rwx' is for the user (u), also known as the owner of the file.
  • The second set of 'rwx' is for the user's group (g).
  • The third set of 'rwx' is for other (o), also known as “the rest of the world”.

There are four possible values associated with the file permissions: 

Table 3: Permissions Types
  • - = permission is turned off (the hyphen has a different definition when used with permissions, than it does for file types)
  • r = read (able to open the file to read its contents)
  • w = write (able to modify the contents of the file)
  • x = execute (able to cause the file to perform a task, such as running a utility or application).
            The presence of a letter (r, w, x) in one of the positions means that the permission is turned on within that specific set (but not necessarily within the other two), but a hyphen (-) means that the permission is turned off. In other words, if the file details show '-rwxrwxrwx', as in Example 2, then the file is fully accessible to everyone. However, if the file details show '-rw-------', as in Example 1, then only the owner (u) has permission to access the file, and then only to read it, or modify its contents. The rest of the users within the owners group (g), and every other individual (o), are barred from accessing the contents of the file or executing it.

Ownership (Users and Groups)
            Each user on a Linux system has their own user account (with log-in and home directory), but each user is also assigned to a group of similar users. The groups help to categorize users according to their system access levels. In fact, a new user is placed in the 'users' group by default, whereas the root user (i.e. super user or administrator) is in its own group called 'root'. A root user is allowed to have much more access to system files and processes than a standard user, which is why they are assigned to a different group.
            For example, if there is a group called 'developers' and a group called 'installers' on the same system, then the programmers can be assigned to the developers group, giving them full access to source code and development utilities, but the installers group may only have access to PDF files containing configuration and installation procedures for the same software. There is no concern about the installers modifying or deleting source code by accident, because the files are inaccessible to them.

Modifying Permissions and Ownership

Changing File Permissions by Lettered Designations: 


Example 3: chmod Syntax for Lettered Designations (to change file permissions)
  • Example 3a: Original File
  • -rw------ 1 root root 56318 Sep 21 16:40 yourFile
  • Example 3b: Basic Syntax
  • chmod [ugo][+-=][rwx] [filename]
  • Example 3c: Changing User (u), Group (g), and Other (o) Permissions to Allow Full Access for read (r), write (w), and execute (x)
  • chmod ugo=rwx yourFile
  • Example 3d: Results of Permissions Change, Part 1
  • -rwxrwxrwx 1 root root 56318 Sep 21 16:40 yourFile
  • Example 3e: Removing write and execute Permissions from User, Group, and Other
  • chmod ugo-wx yourFile
  • Example 3f: Results of Permissions Change, Part 2
  • -r--r--r-- 1 root root 56318 Sep 21 16:40 yourFile
  • Example 3g: Add write Permissions to User and Group
  • chmod ug+w yourFile
  • Example 3h: Results of Permissions Change, Part 3
  • -rw-rw-r-- 1 root root 56318 Sep 21 16:40 yourFile
            Example 3, above, shows syntax options for using chmod to change file permissions with lettered designations (see Examples 3a to 3h for sequential permissions changes). The letters before the equal sign (=) represent the permissions combinations for user (u), user's group (g), and other (o), in that order. The letters after the equal sign (=) represent the permissions, themselves: read (r), write (w), and execute (x).
To change file permissions using lettered designations:
1.    Type chmod at the command line.
2.    Choose the permissions sets to be included; either user (u), group (g), or other (o).
3.    Next, identify the function to be performed with the designated permissions; either add (+) them to the existing permissions, remove (-) them from the existing permissions, or choose to have the designated permissions equal (=) the final status of the chosen sets (additions or removals are performed automatically to match the designated permissions).
4.    After designating the task to be performed, designate the individual permissions (r, w, x) that the task will affect.
5.    Finally, add the name of the file for which the permissions will be modified.
            The challenge to modifying permissions with lettered designations is that if different sets (u, g, or o) need to be assigned different combinations of permissions, the chmod command would have to be executed for each unique set of permissions, which means it may have to be run three times in order to get all the sets configured correctly.
            There is an easier way to modify permissions, though, and that is by using binary designations, shown below.

Changing File Permissions by Binary Designations: 

Example 4: chmod Syntax for Binary Designations (to change file permissions)
  • Example 4a: Original File
  • -rw------- 1 root root 56318 Sep 21 16:40 yourFile
  • Example 4b: Basic Syntax
  • chmod [###] [filename]
  • Example 4c: Changing User permissions to 'rwx' (7), Group permissions to 'r-x' (5), and Other permissions to 'r--' (4)
  • chmod 754 yourFile
  • Example 4d: Results of Permissions Change
  • -rwxr-xr-- 1 root root 56318 Sep 21 16:40 yourFile
  • Example 4e: A Sampling of Other Binary Permissions Combinations
  • chmod 777 [filename]                       =          -rwxrwxrwx
  • chmod 500 [filename]                       =          -r-x------
  • chmod 751 [filename]                       =          -rwxr-x--x
  • chmod 711 [filename]                       =          -rwx--x--x
  • chmod 110 [filename]                       =          ---x--x---
            If the permissions for User and Group need to be different from Other, using lettered designations with chmod would require the command to be executed for each unique set of permissions (in other words, two times). So if User and Group had the same permissions, but Other had a different combination, then User and Group could be configured simultaneously with chmod, then chmod would have to be executed a second time, for Other.
            The repetitious use of chmod can be resolved by using binary designations to set permissions, instead of using lettered designations (see Examples 4a to 4e, above, for the chown syntax). Binary equivalents make it so that all three sets of permissions can receive unique settings, simultaneously. For example: 'chmod 754 [filename]' would set the permissions to '-rwxr-xr--', otherwise, you would have to run the chmod command three times for u=rwx, g=rx, and o=r. As you can see, the binary designations are much more efficient.

 Understanding Binary:

            Each number in the three-digit permissions parameter represents a three-bit designations for one of the 'rwx' permissions sets, seen as the lettered designations in the file details. Therefore, the three numbers represent the permissions combinations for user (u), user's group (g), and other (o), in that order. The permissions represent read (r), write (w), and execute (x).

           Each three-bit binary designation can range from 0 to 7: 

Table 4: Permissions with Binary Designations
  • 0 = --- (no permissions)
  • 1 = --x
  • 2 = -w-
  • 3 = -wx
  • 4 = r--
  • 5 = r-x
  • 6 = rw-
  • 7 = rwx (or full permissions)
            Example 4, above, shows syntax options for using chmod to change file permissions with binary designations, and Table 4 identifies the binary-to-letter conversions.

To change file permissions using binary designations:
1.    Type chmod at the command line.
2.    Determine the permissions-combination needed for each of the three sets and refer to Table 4 to determine the binary equivalent.
3.    Enter all three binary determinations after chmod, without spaces between them.
4.    Finally, add the name of the file for which the permissions will be modified.

Changing File Ownership: 

Example 5: chown syntax (to change file ownership)
  • Example 5a: Original File
  • -rw------- 1 root root 56318 Sep 21 16:40 yourFile
  • Example 5b: Basic Syntax
  • chown [user]:[group] [filename]
  • Example 5c: Changing User to 'tester' and Group to 'users'
  • chown tester:users yourFile
  • Example 5d: Result of Ownership Change
  • -rw------- 1 tester users 56318 Sep 21 16:40 yourFile
            There are times in which the user (owner) or group assigned to a file will need to be changed, such as when the root user (also known as the super user) pulls a file from one computer system to another (which can automatically set the file ownership to root:root, as in Example 1 or Example 5, above). If the file needs to be accessible by a test group, as in our examples, above, then it should not be owned by the root user. In this case the root user would use chown to reassign the file ownership so that the necessary people can access and modify it.
            In these examples, we are using 'tester' as the owner and 'users' as the group. To change ownership of a file, we use the chown command (see Examples 5a to 5d, above, for the chown syntax).

To change file ownership:
1.    Type chown at the command line.
2.    Enter the new user name, followed by a colon (:).
3.    After the colon (without a space to separate them), enter the new group name.
4.    Finally, add the name of the file for which the ownership will be modified.

            However, if you are logged in to the system as a regular user, then once you change the user or group ownership of a file, you may lose all access to that file, depending on the permissions that are set. Due to this, it may be necessary to modify permissions before modifying ownership, or it may be necessary to log in as the root user (super user) to modify the file, because the root user will retain the ability to the access the file even after the ownership has been changed.

            WARNING: Running system tasks as the root user is very dangerous! Files deleted from the command line cannot be recovered and serious system damage can occur! Use the root user account with extreme caution!

Wednesday, March 9, 2011

Linux OpenSuSE System Commands

The dollar sign ($) in the following examples simply represents the command prompt, designating that the text following it should be entered at the command prompt, also known as the command line interface (CLI).

Interface Management

To reset the graphical display, in case of a driver conflict:

1. Press [Ctrl]+[Alt]+[F1]
      # [F1] to [F6] will all change to non-graphical screens, so if [F1] is running some other function, select one of the other non-graphical screens. Outside of the graphical screen, [Alt]+[Function Key] is used to switch between screens; [Ctrl] is only required when exiting the GUI (Graphical User Interface).
2. Log in as the root user
3. $ ps -ef | grep gdm
      # This will show the processes currently running. We want the process at the top of the list, located at /usr/sbin/gdm
4. $ kill -9 [process #]
      # i.e. kill -9 1441
5. $ sax2
      # To rebuild the graphical display

To switch from the GUI (graphical user interface) to a non-graphical environment:

1. Press [Ctrl]+[Alt]+[F1]
      # [F1] to [F6] each represent a separate non-graphical screens. Outside of the graphical screen, [Alt]+[Function Key] is used to switch between screens; [Ctrl] is only required when exiting the GUI (the Graphical User Interface).
2. Log in with user name and password

To switch a virtual machine from the GUI (graphical user interface) to a non-graphical environment:

1. Hold down [Ctrl]+[Alt], then press and release [Spacebar], then press and release the function key ([F1] to [F7]) for the desired screen.
      # This is for switching between screens within a virtual environment, such as VMPlayer, where the regular [Ctrl]+[Alt] command will automatically return control to the host operating system
      # [F1] to [F6] will all change to non-graphical screens. [F7] will activate the graphical screen, or GUI (Graphical User Interface).
2. Log in with user name and password

Directories and Files

To copy linux manual (man) pages to a printable file:

1. $ man [target command] > [path/filename]

To list files with a particular extension in a local directory, with the files sizes in human readable format:

1. $ ls *.[ext] -lh

To list files with a particular extension in a remote directory, with the files sizes in human readable format:

1. $ ls /[path]/*.[ext] -lh
      # This is the full path starting from the root directory
2. $ ls [path]/*.[ext] -lh
      # This is the path starting from the local directory

Creating a link to a file:

1. $ ln [target path/filename] [link name]
      # Links in Linux are essentially the same as creating a copy of a file in one directory and pasting it into another directory, in Microsoft Windows(R). Although the file, itself, will be located in multiple directories, each instance of the file will be linked to the original file, so that if the contents of the file are modified in one location, all of the linked copies will be modified, as well.
      # The target path and file name represent the originating file, to be copied. If you are placing the link in your current working directory, it is not necessary to state the path for the link name.

Creating a symbolic link to a file:

1. $ ln -s [target path/filename] [link name]
      # Symbolic links in Linux serve the same function as a shortcut icon in Microsoft Windows(R); it directs the operating system to the location of the file or application, without moving any data from the original directory to the host directory of the link.
      # The benefit of symbolic links is that a file can be located in one directory and access can be given to execute that file from another directory, without duplicating disk space consumption. Also, permissions may be set on a symbolic link to allow other users or groups to execute a file, without sacrificing the security or integrity of the originating file.
      # The target path and file name represent the originating file, to be copied. If you are placing the link in your current working directory, it is not necessary to state the path for the link name.

Setting File Characteristics

Changing permissions for a file or directory:

1. $ chmod 755 [filename]
      # Permissions are based on 3-bit designations for the owner, owner's group, and other groups, in that order. Bit number one (the right-most bit) activates the execute (x) privilege, bit two (to the left of bit one) activates the write (w) privilege, and bit three activates the read (r) privilege.
      # Therefore, the chmod example above allows the owner of the file to read, write or execute it (each bit doubles the numerical value of the one to the right of it, so 4+2+1=7), but other users in the file owner's group, and other groups, are only allowed to read and execute the file, without the ability to write any modifications to it, because only the first and third bits are set (4+1=5).

Changing ownership of a file or directory:

1. $ chown [username]:[group] [filename]
      # This will affect who is allowed to read, modify, or execute the file or directory, depending on the permissions set (see "Changingg permissions for a file or directory:" above). If you are not the current owner of the file, reassignment of the user and group will need to be performed as the super user (root).

Tuesday, November 2, 2010

Bash Script for Backing Up Log Files

This bash script is used in OpenSUSE 11.2 for backing up and compressing system log files to reduce storage requirements. The script is intended to be run manually.

Open your favorite text editor (gedit, vi, awk, etc), copy the script over (with cut and paste), and save it. I used logtar for the filename. Place the file in the directory: /usr/local/bin/ and changed the permissions to be executable. Placing the file in this directory should place it in the default system path and allow the file to be executable from any directory in the system, and for any user.

The comments in the script explain how it works and what to modify for your personal system. The first line of the script "#!/bin/bash" is special and tells the system where the bash compiler is located. If you are not using and OpenSUSE system, the bash compiler may be in a different location. You can discover the path to your bash compiler by entering the following at the command prompt (without the '$'):
$ which bash

To change the permissions of the new file, enter the following at the prompt, from within the same directory as the logtar file (or whatever you chose to call it):
$ chmod 755 logtar

No warranty or guarantee is implied with the use of this script. It works great on my machine and the servers I maintain, but you will still use this at your own risk. I would suggest creating a new directory and copy some .log files over to it (keep your originals where they are) and practice with the script until you understand how it works and figure out what needs to be changed (if anything) for your own system.

Enjoy!


BEGIN CUT AND PASTE BELOW THIS LINE. DO NOT LEAVE ANY BLANK LINES AT THE TOP OF THE SCRIPT:

#!/bin/bash
#
#####################################################################
# Written by: Andrew Stout, July 2010
# No warranty or guarantee is implied with the use of this script. Use
# at your own risk.
#
# The logtar file is used to backup and compress log files. It will .tar
# and compress the designated file, then empty the original log and move
# the new file to the designated backup directory.
#
# This script will automatically add a current date and time stamp prefix
# to the backup filename, with the .tar.gz suffix.
#
# Command-line syntax: ./logtar foo foo.log
#
# Replace foo with the new file name. Do not include the automatic prefix
# and suffix. Replace foo.log with the full, original filename, including
# the path, if the file is not in the current directory.
######################################################################


# This is where the .tar'd and compressed file will be moved to.
DESTINATION="/home/logs"

# Changing this will change your date/ time stamp format. The current format
# is (Y)ear, (m)onth, (d)ay, (H)our, (M)inute, (S)econd. If this variable were
# not used, the tar and mv commands would reference different file names and
# the .tar'd file would be placed in the local directory, instead of the
# $DESTINATION directory.
DATE=$(date +"%Y%m%d%H%M%S")

# This will make the $DESTINATION directory if it does not already exist.
mkdir $DESTINATION

# This command compresses your .log file. The && means that the first command
# on the line will be executed, and (&&) the second command will only be
# executed if the first command is successful. The > empties the original file.
# $1 refers to the new name, or the second command-line element. $2 refers to
# the original name and path, or the third command-line element. $0 is the
# first command-line element, the logtar file being executed.
tar -czvf $DATE.$1.tar.gz $2 && > $2

# This moves the new file to the $DESTINATION directory.
mv $DATE.$1.tar.gz $DESTINATION

#######################################################################
## END OF SCRIPT

Thursday, October 14, 2010

Remote Network Access to Linux or MS Windows from Linux

Accessing a remote Linux computer using the Command Line Interface (CLI):

1. Open a terminal (command line interface) on your local computer (localhost) and switch to super user mode (log in as 'root').
2. [route@username]$ ssh root@192.168.1.xxx
  • This is the address of the remote computer
3. [password:] xxxxxxxxx
  • This is the password for the remote computer
You're in!
  • ssh means 'secure shell', or to access the remote computer through a secure (encrypted) connection. This is safer than telnet, which is not encrypted.

Accessing a remote Linux or MS Windows graphical desktop (GUI) using a Virtual Network Connection (VNC):

From the web browser:

1. vnc://xxx.xxx.xxx.xxx
  • This utilizes the linux program 'vinagre'

From the linux command line interface (CLI) to a remote Linux or MS Windows graphical user interface (GUI):

1. [route@username]$ vncviewer [remote IP address]
2. [password:] xxxxxxxx
  • This is the password for the remote system

Programs that allow VNC are: VNCViewer for Windows; the combination of vncviewer and vncserver for Linux; or vinagre for Linux

Accessing one remote computer through a second remote computer (tunneling), using the Command Line Interface (CLI):

    1. [route@username]$ ssh -L 10022:192.168.xxx.xxx:22 root@192.168.1.xxx
    2. [password:] xxxxxxxxxxx

    • This is the password for the "root@192.168.1.xxx" address, through which you will be accessing the other computer
    • ssh means 'secure shell', or to access the remote computer through a secure (encrypted) connection. This is safer than telnet, which is not encrypted.
    • -L means 'link'
    • 10022 is the port on the local machine that will be used for the link. Any port number above 1000 or 2000 should be free of conflicts with other programs and usable for this designation.
    • 22 means that you will access the remote computer through its own port 22.
    • root@ means you will be logging in as root to the remote computer that will be used as a tunnel for the 192.168.xxx.xxx remote computer. 'root@' can actually be left off, because this is assumed by ssh if no designation is made.
    3. Next, open a second terminal screen, or tab on the current terminal, and switch to super user mode (root), then:
    4. [route@username]$ ssh -p 10022 root@localhost
    • or you can use... ssh -p 10022 -l root localhost
    • 'root' and 'localhost' have to be entered as they are shown, because these reference the port usage for the local system
    5. [password:] xxxxxxxxxx 
    • This is the password of the 192.168.xxx.xxx:22 from the previous command
    You're in!