Working with the Linux Command Line

 <  Day Day Up  >  

Test Objective Covered:

2. Use the Linux command-line interface.

There are two types of interfaces Linux provides for communicating with the operating systems: a graphical interface and a command-line interface.

Although graphical interfaces tend to be easier to use, many of the tasks you must perform later in this course are best done from the command line. In accordance with this, Novell has included an objective in the CLE certification that requires you to demonstrate that you know how to use the command line.

To allow users to interact with the kernel using the command line, Linux uses command-line interpreters called shells .

Linux Shells

If you've ever used MS-DOS or Novell's NetWare, you are already familiar with the concept of a command-line shell. The shell is simply a command interpreter that allows you to type commands from the keyboard to interact with the operating system kernel. A typical command-line interface is shown in Figure 2.10.

Figure 2.10. The Linux command-line interface.

graphics/02fig10.jpg


In MS-DOS, COMMAND.COM provided this functionality. As with many other aspects of Linux, you have several different shells to choose from. You can try out several of them and then settle on one that you like the best. Some of the more popular shells include the following:

  • sh (Bourne Shell)

  • bash (Bourne-Again Shell)

  • csh (C Shell)

  • tcsh (Tom's C Shell)

  • zsh (Z Shell)

Which shell you choose is really a matter of preference. If you try each one, you'll probably find that they each work pretty much in the same way.

When you boot to a command prompt or open a terminal session on your Linux system, a default shell is used. Most distributions use the bash shell by default.

To determine which shell you're using, enter the env command at the shell prompt. This command lists all the environment variables for the currently logged in user . One of the variables is the shell currently in use, as shown in Figure 2.11.

Figure 2.11. Checking the shell currently in use.

graphics/02fig11.jpg


In this example, you can see that the bash shell is currently in use, as shown by the SHELL variable. If you want to change the shell you're using, simply enter the shell command at the current shell prompt. For example, if you are running bash and want to use zsh, simply enter zsh at the shell prompt. To stop using the new shell and revert back to the original shell, enter exit .

Linux is fully capable of running the 80 x 86 CPU in Real mode, which allows you to run multiple command-line sessions at once, even if you're not using the XFree86 graphical interface. Each session can run its own programs, all simultaneously .

This can be very useful if you have one program running and then need access to the command prompt. For example, suppose you are installing NNLS and you reach the point where the installation program needs to access the license diskette. You realize that you forgot to insert and mount it. You can simply open another session, mount the diskette in the second session, change back to the first session, and continue the installation.

Before going any further, I need to point out that this can be done in two ways. First, from a text-based shell prompt, you can open alternate command-line screens, each capable of running its own programs independently of the others.

Second, from within X Window System, using multiple command-line interfaces can be done in two different ways. The first, and easiest , is to simply open multiple terminal sessions. Each terminal session can run its own set of programs independent of the others, as shown in Figure 2.12.

Figure 2.12. Running multiple terminal sessions.

graphics/02fig12.jpg


The second method is to press Ctrl+Alt+ function_key . This will switch you to a text-based shell prompt. This is very useful because some tasks, such as creating eDirectory users in NNLS, can only be done from a text-based shell prompt. Using Ctrl+Alt+F1 switches you to the first text-based console screen, Ctrl+Alt+F2 switches you to the second one, and so on up to F6 . To switch back, press Alt+F7 and you will return to your X Window System session.

If you're running your Linux system without X Window System running, you can switch between shell prompts by simply pressing Alt+ function_key . For example, to switch to the third alternate console screen, simply press Alt+F3 .

Using Linux Commands from the Command Line

To enter commands at the Linux command line, all you have to do is type the name of the command at the shell prompt.

If you are experienced with using a command prompt under Windows or DOS, there are some important differences you must be aware of. First, although Linux implements a PATH variable similar to the one used in DOS, it is used differently. The Windows or DOS command interpreter will check the current directory for a file specified at the command prompt as well as the directories specified in the PATH variable.

Note

The PATH variable is an environment variable that contains a list of directories .


Linux works in a similar manner; however, it doesn't check the local directory for the specified file. This represents an enormous stumbling block to those who are new to Linux. Because of their prior experience with DOS and Windows, they expect to be able to switch to the directory where an executable resides and run it from the command line.

Instead, Linux searches all the directories in its PATH variable. A typical Linux PATH environment variable is shown in Figure 2.13.

Figure 2.13. The PATH variable.

graphics/02fig13.jpg


There are three ways to get around this:

  • You can provide the full path to the executable file. For example, if you wanted to run an executable named helloworld located in /usr/myfiles , you could enter /usr/myfiles/helloworld at the shell prompt.

  • You can switch to the directory where the executable file resides. Then prepend ./ to the command. In the previous example, I would switch to the /usr/myfiles directory and then enter ./helloworld at the shell prompt. The ./ characters refer to the current directory. By prepending them to a command, you tell the shell program to look for the file in the command in the current directory.

  • You can add ./ to the PATH variable. This will direct the command interpreter to check the local directory regardless of the current directory.

Another issue you need to be aware of is one we briefly discussed earlier in this chapter. Linux file names and directory names are case sensitive. Hence, Linux commands are also case sensitive. If the executable file you are going to run is HelloWorld , you must use HelloWorld at the command prompt. Helloworld , HELLOWORLD , or helloWorld won't work. Linux sees each of these as different files.

This applies to directory names as well. If you're calling /usr/myfiles/helloworld , you must enter the command using the exact case. /Usr/Myfiles/helloworld will point the command interpreter to a completely different directory structure.

A final issue to be aware if is the fact that each user has his or her own PATH variable. If you log in as root, you will have directories in your PATH variable that won't exist if you log in as a different user.

Commonly Used Linux Commands

As you get to know the Linux operating system, you'll find that there are some very powerful commands that you use over and over. The following is a list of some commonly used commands:

Note

There are many other Linux commands. Unfortunately, covering all of them is beyond the scope of this study guide. If you want to learn more about Linux commands, see a Linux reference text .


  • halt ” This command shuts down the operating system, and can only be run by root.

  • init0 ” This command also shuts down the operating system, and can only be run by root.

  • reboot ” This command shuts down and restarts the operating system. It can only be run by root.

  • init6 ” This command shuts down and restarts the operating system. It can only be run by root.

  • man ” This command displays the manual (man) page for the specified command. This is a very useful learning tool. If you are unsure how to use any command, use man to display its manual page. You can exit a man page by pressing q .

  • info ” This command displays a help page for the specified command. The information displayed tends to be more in-depth than that displayed in the man page for the same command.

  • su ” This command switches the current user. For example, if you're logged in as fred and need to change to user account mary , you can enter su mary . This command is most frequently used to switch to the superuser account. If you don't supply a username (in other words, you simply enter su ), this utility assumes you want to change to the root account. If you enter su - , you switch to the root (superuser) user account and have all of its environment variables applied.

    Real World

    Because of this, the su command is frequently misconstrued to stand for "super user." In fact, it stands for "switch user."


  • env ” This command displays the environment variables for the currently logged in user. The output of env was shown in Figure 2.11. Notice that this includes items such as the user's name, default shell, and system host name.

  • echo ” This command is used to display a line of text on the screen. It's very useful for displaying environment variables. For example, if you want to see the current value of the HOSTNAME variable, you can enter echo $HOSTNAME . If you want to see the user name of the currently logged-in user, you can enter echo $USER .

    Real World

    The $ used in this command tells the echo utility that the string of text that comes after it is a variable, not a literal string of text. If you omitted the $ , the echo command would simply display the text HOSTNAME on the screen .


  • top ” This command is a very useful command that displays a list of all processes currently running on the system. You can sort them by several criteria, including CPU usage, memory usage, process ID, and so on. You can also use top to kill a process that is hung. To exit the top utility, press q .

  • which ” This command is used to display the full path to a shell command. For example, if you want to know the full path to the top command, you would enter which top .

  • whoami ” This command is used to display the username of the currently logged-in user.

  • netstat ” This command is used to display the status of the network, including current connections, routing tables, multicast memberships, and statistics.

  • route ” This command is used to manage the operating system's routing table. You can add, remove, and modify routing information.

  • ifconfig ” This command is used to manage network interfaces in the system. If you've spent much time working with Windows NT, 2000, or XP, you're probably familiar with the IPCONFIG command. With it you can release and renew DHCP connections, assign IP addresses, and so on. The ifconfig command is used in much the same way. If you enter ifconfig without any parameters, it will display the current configuration parameters for your network interfaces. The ifconfig utility is located in /sbin . This directory is included, by default, in the root user's PATH variable. You can still run it as a non-root user, but you will have to call it explicitly from the /sbin directory.

  • yast2 ” This command is used on a SUSE Linux system to provide a menu-driven interface to configure the system. It is available in text-only form from a text-based shell prompt and as a GUI utility under X Window System. This is a very useful tool. With it, you can do the following:

    • Install or remove software.

    • Run online updates.

    • Configure peripherals such as printers, sound boards , mouse devices, graphic cards, and monitors .

    • Configure network interfaces.

    • Manage user accounts.

    • Partition hard disk drives .

    • Run a system backup.

    Real World

    The equivalent utility on a Red Hat system is setup .


  • startx ” If your Linux system is configured to boot to a shell prompt, you can start the XFree86 graphical interface by entering this command at the prompt.

Real World

In Linux circles, booting to a shell prompt is referred to as runlevel 3 . Booting to a XFree86 graphical environment is referred to as runlevel 5 . Runlevels will be discussed in more detail in Chapter 3 .


As you implement NNLS, you'll find that you will need to use these tools on a frequent basis. To make sure you understand these tools, let's run though a lab to practice using them.

 <  Day Day Up  >  


Novell Certified Linux Engineer (CLE) Study Guide
Novell Certified Linux Engineer (Novell CLE) Study Guide (Novell Press)
ISBN: 0789732033
EAN: 2147483647
Year: 2004
Pages: 128
Authors: Robb H. Tracy

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net