Archive for category Command Post Series

Command Post: fdisk

Command Post is a series designed increase your understanding of a given command, it’s configuration files, usage and tips for exam questions about the command.

Understanding fdisk

The fdisk tool is line oriented and somewhat cryptic. When you issue commands to the fdisk tool, your choices and the output scroll upward over time and disappear from view, making it hard to see large sets of disk information and even to remember where you are in a complex configuration.  (This is in marked contrast to tools like cfdisk, where everything is screen-oriented and menu-driven.  We’ll be covering cfdisk separately in another Command Post.)

It’s very important to remember that fdisk destructively alters the partition table, with the writing taking place after you press the W key and Enter. Up to that point, pressing the Q key and Enter helps you escape an fdisk configuration gone bad.

Using fdisk

A typical fdisk session that creates a typical workstation disk layout is described in the following, step-by-step list. The partition layout consists of 200MB /boot, 750MB swap, and 6GB / (root). It then makes a single extended partition and a 1GB data partition in that extended partition:

Please note that output from the fdisk program is shown to support what the user will see. Anywhere you need to type or enter something, I’ve noted it.

1. Start the fdisk tool, specifying the disk to configure the following.  (For example, if you have a single IDE drive, you would use the target /dev/hda.)

# fdisk /dev/hda
The number of cylinders for this disk is set to 2088.
... (text deleted)
(e.g., DOS FDISK, OS/2 FDISK)
Command (m for help):

2. Type m for the menu:

Command action
  a toggle a bootable flag
  b edit bsd disklabelc toggle the dos compatibility flag
  d delete a partition
  l list known partition types
  m print this menu
  n add a new partition
  o create a new empty DOS partition table
  p print the partition table
  q quit without saving changes
  s create a new empty Sun disklabel
  t change a partition’s system id
  u change display/entry units
  v verify the partition table
  w write table to disk and exit
  x extra functionality (experts only)
Command (m for help):

3. Issue the p command to see the partition table, and assume it’s blank:

Command (m for help): p
Disk /dev/hda: 17.1 GB, 17179803648 bytes
255 heads, 63 sectors/track, 2088 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
    Device Boot Start End Blocks Id System
Command (m for help):

4. To create a 200MB /boot partition, issue the following commands:

n
p
1
(Press Enter)
+200MB
(Press Enter)

5. Check your new partition with the p command.

6. To create the 750MB swap partition, issue the following commands:

n
p
2
(Press Enter)
+750MB
(Press Enter)

7. To create the 6GB root partition, issue the following commands:

n
p
3
(Press Enter)
+6000MB
(Press Enter)

8. Enter the p command again to check the layout again:

Device Boot Start End Blocks Id System
/dev/hda1 1 25 200781 83 Linux
/dev/hda2 26 117 738990 83 Linux
/dev/hda3 118 847 5863725 83 Linux
Command (m for help):

9. To create the extended partition, issue the following keystrokes:

n
e
4
(Press Enter)
(Press Enter)

10. To create the data logical partition, issue the following keystrokes:

n
(Press Enter)
+1024MB
(Press Enter)

11. Lastly, set the 750MB partition to the type 82, which is used for Linux
swap partitions with the following:

t
2
L
82

12. Check your partition layout with the p command:

Device Boot Start End Blocks Id System
/dev/hda1 1 25 200781 83 Linux
/dev/hda2 26 117 738990 82 Linux swap
/dev/hda3 118 847 5863725 83 Linux
/dev/hda4 848 2088 9968332+ 5 Extended
/dev/hda5 848 972 1004031 83 Linux

13. Write the changes to the disk with the w command followed by pressing Enter.

It’s important to note that while the fdisk command may now indicate that the system might require a reboot at this point because you’ve altered the drive on which the system’s root (/) partition exists. Also note how the first logical partition becomes /dev/hda5 for IDE drives.

Summary

Partitioning properly is an important skill for a system administrator to have, and you’ll find that servers and workstations partitioning needs are quite different.  A workstation can have a very simple and direct partitioning structure, there’s usually a single user and most data will be in the /home/username directory.  If the user fills up their home directory and consequently the / of the drive, it will likely affect only the user.  (Well, and the poor helpdesk staffer who takes _that_ call.)

Servers are a completely different matter, there are many situations for a server that necessitate putting a section of the filesystem on it’s own partition.  For example, putting /home on it’s own partition is key, this limits the system’s users disk usage to that partition, leaving the root partition unaffected.  An additional high-traffic/volume directory tree is /var.  Putting /var on it’s own partition limits the damage that unusual amounts of log entries (/var/log) or mail (/var/spool/mail) or for some distributions web (/var/www) or FTP (/var/ftp) can cause.  An FTP upload session gone bad, or an attempted denial of service by uploading ISO images to the FTP upload directory can disable an improperly-partitioned server by quickly filling the drive, including the /.

You can get more information about the use of fdisk from a variety of sources, the first of which would be Chapter 1 of the current version of my book, LPIC 1 Exam Cram 2 (look for it in the sidebar ->), starting on Pg 10, the second would be the man pages for the fdisk command (man fdisk).

Enjoy,

RossB

No Comments

Command Post: Using the Shell *UPDATED*

Command Post is a series designed increase your understanding of a given command, it’s configuration files, usage and tips for exam questions about the command.

Understanding Shells

A shell is a program designed to interpret the commands users type, parse for expansions and wildcards, and then produce instructions to the computer to accomplish those tasks.

Unless you change the defaults, the Linux shell is normally the bash shell. Of course, many other shells exist. A partial list is shown in the following table.

linux_shells_table

Among other pieces of information, the user’s default shell is specified in the /etc/passwd entry for that user. If the shell that is specified does not exist, the user is not permitted to log in.

Special shells can be specified, such as /bin/false (which returns a nonzero error code, effectively blocking access by a user attempting to log in) or /etc/nologin (which is used to block logins for accounts that have been disabled and echo a message that login is denied). The /etc/nologin file might not exist on some systems.

Deconstructing a Login Shell Session

A login shell is one that is executed when logging in to the system. The /etc/profile file is the global configuration file that affects all users’ environments if they use the bash shell. It’s sourced (read) every time a user performs a login shell. This file is a script and is executed right before the user’s profile script.  The user’s ~/.bash_profile script, if it exists, is the next script that’s sourced.  This file contains variables, code, and settings that directly affect that user’s—and only that user’s—environment. This script calls, or sources, the next script, which is .bashrc.

The .bash_profile script can also be referred to or named as the .bash_login or .profile script. If all three exist, the .bash_profile is sourced alone; otherwise, if it doesn’t exist, the .bash_login is sourced. Finally, if the first two are nonexistent, the .profile script is sourced. This functionality is used almost entirely by Bourne shell users upgrading to a bash system as a way to keep their settings in the same .profile file.

The .bashrc file is called by the .bash_profile or one of the profile’s aliases and is used to set various shell settings and options, set the prompt, and define aliases and functions for command-line execution. The last script sourced during a user’s login session is the .bash_logout file. This file is used to issue the clear command, so text from any previous command is not left on the user’s screen after he logs out.

TIP: Be careful on the exam because a lot of test-takers do not pick the .bash_logout file as part of the user’s login session. It’s definitely one of the more missed elements in the shell section.

An example of the user’s login session might be the following:

1. The user logs in with a username and password.
2. The /etc/profile is sourced.
3. The user’s .bash_profile is sourced.
4. The user’s .bashrc is sourced from within the .bash_profile.
5. The user conducts his business.
6. The user initiates a logout with the logout or exit command or by
pressing Ctrl+D.
7. The user’s .bash_logout script is sourced.

Summary

Remember these important things:  1.  Executing a script file only loads the settings contained in it within the life of the sub-shell the script executes in, they are gone when the sub-shell exits.  2.  Sourcing a script file ( preceeding it with dot, such as ” .  /etc/bashrc” will load the variables and settings in that file into the current shell.

For more information on how to use the shell, you can turn to many multiple resources, including Chapter 2 of the current version of my LPIC 1 Exam Cram 2 book (look for it in the sidebar ->), starting on Pg 28, the second would be the bash shell’s man page (man bash), still another would be the Guides section of The Linux Document Project’s site, particularly the Bash Guide for Beginners.

Any comments, suggestions or questions are welcome.

RossB

Copyright 2000-2009 Ross Brunson

No Comments