Home > Articles > Operating Systems, Server > Linux/UNIX/Open Source

  • Print
  • + Share This
This chapter is from the book

Logging In to and Working with Linux

You can access and use a Linux system in a number of ways. One way is at the console with a monitor, keyboard, and mouse attached to the PC. Another way is via a serial console, either by dial-up via a modem or a PC running a terminal emulator and connected to the Linux PC via a null modem cable. You can also connect to your system through a wired or wireless network, using the telnet or ssh commands. The information in this section shows you how to access and use the Linux system, using physical and remote text-based logins.

Text-Based Console Login

If you sit down at your PC and log in to a Linux system that has not been booted to a graphical login, you see a prompt similar to this one:

Ubuntu 10.10 maverick seymour ttyl
seymour login:

Your prompt might vary, depending on the version of Ubuntu you are using and the method you are using to connect. In any event, at this prompt, type in your username and press Enter. When you are prompted for your password, type it in and press Enter.

Logging Out

Use the exit or logout command or Ctrl+D to exit your session. You are then returned to the login prompt. If you use virtual consoles, remember to exit each console before leaving your PC. (Otherwise, someone could easily sit down and use your account.)

Logging in and Out from a Remote Computer

Although you can happily log in on your computer, an act known as a local login, you can also log in to your computer via a network connection from a remote computer. Linux-based operating systems provide a number of remote access commands you can use to log in to other computers on your local area network (LAN), wide area network (WAN), or the Internet. Note that not only must you have an account on the remote computer, but the remote computer must be configured to support remote logins—otherwise, you won't be able to log in.

The best and most secure way to log in to a remote Linux computer is to use ssh, the Secure Shell client. Your login and session are encrypted while you work on the remote computer. The ssh client features many command-line options, but can be simply used with the name or IP address of the remote computer, like this:

matthew@seymour:~$ ssh 192.168.0.41
The authenticity of host '192.168.0.41 (192.168.0.41)' can't be established.
RSA key fingerprint is e1:db:6c:da:3f:fc:56:1b:52:f9:94:e0:d1:1d:31:50.
Are you sure you want to continue connecting (yes/no)?

yes

The first time you connect with a remote computer using ssh, Linux displays the remote computer's encrypted identity key and asks you to verify the connection. After you type yes and press Enter, you are warned that the remote computer's identity (key) has been entered in a file named known_hosts under the .ssh directory in your home directory. You are also prompted to enter your password:

Warning: Permanently added '192.168.0.41' (RSA) to the list of known hosts.
matthew@192.168.0.41's password:
matthew@babbage~$

After entering your password, you can work on the remote computer, which you can confirm by noticing the changed prompt that now uses the name of the remote computer on which you are working. Again, because you are using ssh, everything you enter on the keyboard in communication with the remote computer is encrypted. When you log out, you will return to the shell on your computer.

matthew@babbage~$ logout
matthew@seymour:~$

Using Environment Variables

A number of in-memory variables are assigned and loaded by default when you log in. These variables are known as shell environment variables and can be used by various commands to get information about your environment, such as the type of system you are running, your home directory, and the shell in use. Environment variables are used to help tailor the computing environment of your system and include helpful specifications and setup, such as default locations of executable files and software libraries. If you begin writing shell scripts, you might use environment variables in your scripts. Until then, you need to be aware only of what environment variables are and do.

The following list includes a number of environment variables, along with descriptions of how the shell uses them:

  • PWD—provides the full path to your current working directory, used by the pwd command, such as /home/matthew/Documents
  • USER—declares the user's name, such as matthew
  • LANG—sets the default language, such as English
  • SHELL—declares the name and location of the current shell, such as /bin/bash
  • PATH—sets the default location(s) of executable files, such as /bin, /usr/bin, and so on
  • TERM—sets the type of terminal in use, such as vt100, which can be important when using screen-oriented programs, such as text editors
  • You can print the current value of any environment variable using echo $VARIABLENAME, like this:
matthew@seymour:~$ echo $USER

matthew
matthew@seymour:~$

You can use the env or printenv command to display all environment variables, like so:

matthew@seymour:~$ env
ORBIT_SOCKETDIR=/tmp/orbit-matthew
SSH_AGENT_PID=1729
TERM=xterm
SHELL=/bin/bash
WINDOWID=71303173
GNOME_KEYRING_CONTROL=/tmp/keyring-qTEFTw
GTK_MODULES=canberra-gtk-module
USER=matt
hew
SSH_AUTH_SOCK=/tmp/keyring-qTEFTw/ssh
DEFAULTS_PATH=/usr/share/gconf/gnome.default.path
SESSION_MANAGER=local/seymour:/tmp/.ICE-unix/1695
USERNAME=matthew
XDG_CONFIG_DIRS=/etc/xdg/xdg-gnome:/etc/xdg
DESKTOP_SESSION=gnome
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
PWD=/home/matt
hew
GDM_KEYBOARD_LAYOUT=us
LANG=en_US.utf8
GNOME_KEYRING_PID=1677
MANDATORY_PATH=/usr/share/gconf/gnome.mandatory.path
GDM_LANG=en_US.utf8
GDMSESSION=gnome
HISTCONTROL=ignoreboth
SPEECHD_PORT=7560
SHLVL=1
HOME=/home/matt
hew
LOGNAME=matt
hew
LESSOPEN=| /usr/bin/lesspipe %s
DISPLAY=:0.0
LESSCLOSE=/usr/bin/lesspipe %s %s
XAUTHORITY=/var/run/gdm/auth-for-matthew-PzcGqF/database
COLORTERM=gnome-terminal
OLDPWD=/var/lib/mlocate
_=/usr/bin/env

This abbreviated list shows some of the common variables. These variables are set by configuration or resource files contained in the /etc, /etc/skel, or user /home directory. You can find default settings for bash, for example, in /etc/profile and /etc/bashrc as well as .bashrc or .bash_profile files in your home directory. Read the man page for bash for details about using these configuration files.

One of the most important environment variables is $PATH, which defines the location of executable files. For example, if, as a regular user, you try to use a command that is not located in your $PATH (such as the imaginary command command), you will see something like this:

matthew@seymour:~$ command
-bash: command: command not found

If the command that you're trying to execute exists in the Ubuntu software repositories, but is not yet installed on your system, Ubuntu will respond with the correct command to install the command.

matthew@seymour:~$ command

The program 'command' is currently not installed. You can install it by typing:

sudo apt-get install command

However, you might know that command is definitely installed on your system, and you can verify this by using the whereis command, like so:

$ whereis command
command: /sbin/command

You can also run the command by typing its full pathname or complete directory specification, like this:

$ /sbin/command

As you can see in this example, the command command is indeed installed. What happened is that by default, the /sbin directory is not in your $PATH. One of the reasons for this is that commands under the /sbin directory are normally intended to be run only by root. You can add /sbin to your $PATH by editing the file .bash_profile in your home directory (if you use the bash shell by default, like most Linux users). Look for the following line:

PATH=$PATH:$HOME/bin

You can then edit this file, perhaps using the vi editor (discussed later in this chapter), to add the /sbin directory like so:

PATH=$PATH:/sbin$HOME/bin

Save the file. The next time you log in, the /sbin directory is in your $PATH. One way to use this change right away is to read in the new settings in .bash_profile by using the bash shell's source command like so:

$ source .bash_profile

You can now run commands located in the /sbin directory without the need to explicitly type the full pathname.

Some Linux commands also use environment variables, for example, to acquire configuration information (such as a communications program looking for a variable such as BAUD_RATE, which might denote a default modem speed).

To experiment with the environment variables, you can modify the PS1 variable to manipulate the appearance of your shell prompt. If you are working with bash, you can use its built-in export command to change the shell prompt. For example, if your default shell prompt looks like this:

matthew@seymour:~$

You can change its appearance by using the PS1 variable like this:

$ PS1='$OSTYPE r00lz ->'

After you press Enter, you see

linux-gnu r00lz ->
  • + Share This
  • 🔖 Save To Your Account