Cleaning Packages Laying Around in Linux

These commands might be different from the commands used in your Linux distribution. For Ubuntu: Use the commands below.

sudo apt-get autoremove
sudo apt-get clean
sudo apt-get autoclean

These commands are needed to remove packages (dependencies) laying on the system after a particular package has been uninstalled.

Administrating | Changing Your Linux Password Tip 4

To set or change a password, the passwd command is used. The command syntax looks like this.

root@pve:~#passwd user

To change your password, just enter the passwd command. You will be prompted for your old password and your new password.

root@pve:~# passwd
Enter new UNIX password:

If you have superuser privileges, you can specify a username as an argument to the passwd command to set the password for another user. Other options are available to the superuser to allow account locking, password expiration, and so on. See the passwd man page for details.

PROCESSES: Sometimes a computer will become sluggish, or an application will stop responding. In this chapter, we will look at some of the tools available at the command line that let us examine what programs are doing and how to terminate processes that are misbehaving.

ps - Report a snapshot of current processes.
top -  Display tasks.
jobs - List active jobs.
bg - Place a job in the background.
fg - Place a job in the foreground.
kill - Send a signal to a process.
killall - Kill processes by name.
shutdown - Shut down or reboot the system.

Viewing Processes with ps
The most commonly used command to view processes (there are several) is ps.

root@pve:~# ps
19902 pts/1    00:00:00 bash
19930 pts/1    00:00:00 ps

TTY is short for teletype and refers to the controlling terminal for the process.

ps x – If we add an option, we can get a bigger picture of what the system is and many more

root@pve:~# ps x
       1 ?        Ss     0:00 init [2]  
       2 ?        S      0:00 [kthreadd]

Since the system is running a lot of processes, ps produces a long list. It is often helpful to pipe the output from ps into less for easier viewing. A new column titled STAT has been added to the output. STAT is short for state and reveals the current status of the process, as shown

Process States(State Meaning)
R – Running. The process is running or ready to run.
S – Sleeping. The process is not running; rather, it is waiting for an event, such as a keystroke or network packet.
D – Uninterruptible sleep. Process is waiting for I/O such as a disk drive.
T – Stopped. The process has been instructed to stop (more on this later).
Z – A defunct or zombie process. This is a child process that has terminated but has not been cleaned up by its parent.
A – high-priority process. It is possible to grant more importance to a process, giving it more time on the CPU. This property of a process is called niceness. A process with high priority is said to be less nice because itís taking more of the CPUís time, which leaves less for everybody else.
N – A low-priority process. A process with low priority (a nice process) will get processor time only after other processes with higher priority have been serviced. The process state may be followed by other characters. These indicate various exotic process characteristics. See the ps man page for more detail.

Another popular set of options is aux (without a leading dash). This gives us even more information and many more.

root@pve:~# ps aux
 root           1  0.0  0.0  10612   844 ?        Ss   09:08   0:00 init [2]  
 root           2  0.0  0.0      0     0 ?        S    09:08   0:00 [kthreadd]

This set of options displays the processes belonging to every user. Using the options without the leading dash invokes the command with BSD-style

BSD-Style ps Column Headers (Header Meaning)
USER User ID. This is the owner of the process.
%CPU CPU usage as a percent.
%MEM Memory usage as a percent.
VSZ Virtual memory size.
RSS Resident Set Size. The amount of physical memory (RAM) the
process is using in kilobytes.
START Time when the process started. For values over 24 hours, a date
is used.

root@pve:~# top
 top - 14:24:56 up  5:16,  1 user,  load average: 0.00, 0.00, 0.00
 Tasks: 148 total,   1 running, 147 sleeping,   0 stopped,   0 zombie
 %Cpu(s):  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
 KiB Mem:   1013700 total,   429240 used,   584460 free,    24920 buffers
 KiB Swap:  1179644 total,        0 used,  1179644 free,    75512 cached

19900 root      20   0 71260 3708 2900 S   0.3  0.4   0:00.03 sshd                    
       1 root      20   0 10612  844  712 S   0.0  0.1   0:00.68 init                    
       2 root      20   0     0    0    0 S   0.0  0.0   0:00.00 kthreadd

The top program displays a continuously updating (by default, every 3 seconds) display of the system processes listed in order of process activity.

More about Linux environment
printenv: This is used in printing more about the environment variables e.g

root@pve:/# printenv
 SSH_CLIENT= 53305 22

Note: this output can be pipped into less like this (printenv | less)

What we see is a list of environment variables and their values. For example, we see a variable called USER, which contains the value me (root).

root@pve:/# printenv USER

Using a Text Editor
All text editors can be invoked from the command line by typing the name
of the editor followed by the name of the file you want to edit. Here is an example using gedit

[me@linuxbox ~]$ gedit some_file

This command will start the gedit text editor and load the file named
some_file if it exists.
Note: Whenever we edit an important configuration file, it is always a good idea to create a backup copy of the file first. This protects us in case we mess the file up while editing. To create a backup of the .bashrc file, do this.

root@pve:/# cp .bashrc .bashrc.bak

It doesnít matter what you call the backup file; just pick an understandable
name. The extensions .bak, .sav, .old, and .orig are all popular ways of
indicating a backup file. Oh, and remember that cp will overwrite existing files silently. Now finding a Package in a Repository by using the high-level tools to search repository metadata, one can locate a package based on its name or description.

Package Search Commands

root@pve:/#apt-get update
root@pve:/#yum search search_string
root@pve:/# apt-cache search apache

Installing a Package from a Repository
High-level tools permit a package to be downloaded from a repository and installed with full dependency resolution
Package Installation Commands

root@pve:/#apt-get update
root@pve:/#yum install package_name

Installing a Package from a Package File
If a package file has been downloaded from a source other than a repository, it can be installed directly (though without dependency resolution) using a low-level tool. Low-Level Package Installation Commands Style Command

root@pve:/#dpkg –install package_file
root@pve:/#rpm -i package_file

Removing a Package
Packages can be uninstalled using either the high-level or low-level tools.
Package Removal Commands

root@pve:/#apt-get remove package_name
root@pve:/# yum erase package_name

Listing Installed Packages
The commands can be used to display a list of all the packages installed on the system.

root@pve:/# dpkg –list
root@pve:/#rpm -qa

Determining Whether a Package Is Installed
The low-level tools shown in Table 14-10 can be used to display whether a specified package is installed. Package Status Commands

 root@pve:/#dpkg --status package_name
 root@pve:/#rpm -q package_name

Storage Media: We will look at the following commands:

mount -Mount a filesystem.
umount- Unmount a filesystem.
fdisk – Partition table manipulator.
fsck – Check and repair a filesystem.
fdformat – Format a floppy disk.
mkfs – Create a filesystem.
dd – Write block-oriented data directly to a device.
wodim (cdrecord) – Write data to optical storage media.
md5sum – Calculate an MD5 checksum.

Cont. – Linux Cheat Sheet – The Linux Command Line Tip 3

Cursor Movement Command

CTRL-A = Move cursor to the beginning of the line.
CTRL-E = Move cursor to the end of the line.
CTRL-F = Move cursor forward one character; same as the right arrow key.
CTRL-B = Move cursor backward one character; same as the left arrow key.
ALT-F =  Move cursor forward one word.
ALT-B =  Move cursor backward one word.
CTRL-L = Clear the screen and move the cursor to the top left corner. The "clear" command does the same thing.

Cutting and Pasting (Killing and Yanking) Text

The Readline documentation uses the terms of killing and yanking to refer to what we would commonly call cutting and pasting. Table 8-3 lists the commands for cutting and pasting. Items that are cut are stored in a buffer called the kill-ring.

I used shift plus control c for copying and shift plus control v for pasting.

Searching History: At any time, we can view the contents of the history list

root@pve:/#history | less
By default, bash stores the last 500 commands you have entered. 

We will see how to adjust this value in Example 2.
root@pve:/# history | grep /usr/bin
321  ls -l /usr/bin > ls-oztput.txt
349  ls -l /usr/bin | less
350  ls -l /usr/bin | less
351  ls -l /usr/bin | less

The number 90 is the line number of the command in the history list. We could use this immediately with another type of expansion called history expansion. To use our discovered line, we could do this.

root@pve:/# !90
cd ..

Note: bash will expand !88 into the contents of the 88th line in the history list.

id – Display user identity.
chmod – Change a fileís mode.
umask – Set the default file permissions.
su – Run a shell as another user.
sudo – Execute a command as another user.
chown – Change a fileís owner.
chgrp – Change a fileís group ownership.
passwd – Change a userís password.

In the Unix security model, a user may own files and directories. When a user owns a file or directory, the user has control over its access. Users can, in turn, belong to a group consisting of one or more users who are given access to files and directories by their owners. In addition to granting access to a group, an owner may also grant some set of access rights to everybody, which in Unix terms is referred to as the world.

To find out information about your identity, use the id command.

root@pve:~# id
uid=0(root) gid=0(root) groups=0(root)

The output of the user is, when a user account is created, users are assigned a number called a user ID, or uid. This is then, for the sake of the humans, mapped to a username. The user is assigned a primary group ID, or gid, and may belong to additional groups.

Note: As we can see, the uid and gid numbers are different. This is simply because Fedora starts its numbering of regular user accounts at 500, while
Ubuntu starts at 1000. So where does this information come from? Like so many things in Linux, it comes from a couple of text files. User accounts are defined in the /etc/passwd file, and groups are defined in the /etc/group file. When user accounts and groups are created, these files are modified along with /etc/shadow, which holds information about the userís password. For each user account, the /etc/passwd file defines the user (login) name, the uid, the gid, the accountís real name, the home directory, and the login shell. If you examine the contents of /etc/passwd and /etc/group, you will notice that besides the regular user accounts there are accounts for the superuser (uid 0) and various other system users.

Reading, Writing, and Executing
Access rights to files and directories are defined in terms of reading access, write access, and execution access. If we look at the output of the ls command, we can get some clue as to how this is implemented:

root@pve:~# ls -l arrow.txt
-rw-r–r– 1 root root 43 May 9 11:27 arrow.txt

Note: (The first 10 characters of the listing are the file attributes(-rw-r–r–))
The first of these characters is the file type. The remaining nine characters of the file attributes called the file mode, represent the read, write, and execute permissions for the fileís owner, the fileís group owner, and everybody else.

File type
-rw === Owner permission
-rw === Group Permission
-r ===World permission

Attribute File Type

A regular file.
d == A directory.

l == A symbolic link. Notice that with symbolic links, the remaining file attributes are always rwxrwxrwx and are dummy values. The real
file attributes are those of the file the symbolic link points to.

c == A character special file. This file type refers to a device that
handles data as a stream of bytes, such as a terminal or modem.

b == A block special file. This file type refers to a device that handles
data in blocks, such as a hard drive or CD-ROM drive.

Permission Attributes: Attribute Files Directories
r == Allows a file to be opened and read. Allows a directory is contents to be listed if the execute attribute is also set.

w == Allows a file to be written to or truncated; however, this attribute does not allow files to be renamed or deleted. The ability to delete or rename files is determined by directory attributes. Allows files within a directory to be created, deleted, and renamed if the execute attribute is also set.

x == Allows a file to be treated as a program and executed. Program files written in scripting languages must also be set as readable to be executed.
Allows a directory to be entered; e.g., cd directory

Permission Attribute Meaning
-rwx A regular file that is readable, writable, and executable by the fileís owner. No one else has any access.

A regular file that is readable and writable by the file is owner. No one else has any access.

A regular file that is readable and writable by the file is the owner. Members of the fileís owner group may read the file. The file is world-readable.

A regular file that is readable, writable, and executable by
the fileís owner. The file may be read and executed by
everybody else.

A regular file that is readable and writable by the fileís
owner and members of the fileís owner group only.

lrwxrwxrwx A symbolic link. All symbolic links have dummy permissions. The real permissions are kept with the actual file pointed to by the symbolic link.

A directory. The owner and the members of the owner group may enter the directory and create, rename, and remove files within the directory.

A directory. The owner may enter the directory and create, rename, and delete files within the directory. Members of the owner group may enter the directory but cannot create, delete, or rename files.

chmod – Change File Mode
To change the mode (permissions) of a file or directory, the chmod command is used. Be aware that only the fileís owner or the superuser can change the mode of a file or directory.

Note: chmod supports two distinct ways of specifying mode changes: octal number representation and symbolic representation. We will cover the octal number representation first.

A: Octal Representation: With octal notation, we use octal numbers to set the pattern of desired permissions. Since each digit in an octal number represents three binary digits, These maps nicely to the scheme used to store the file mode.

Octal Binary File mode
0 000 —
1 001 –x
2 010 -w-
3 011 -wx
4 100 r–
5 101 r-x
6 110 rw-
7 111 rwx

By using three octal digits, we can set the file mode for the owner, group owner, and world.

 example using octal representation
 root@pve:~# ls -l
 total 12
 -rw-r--r-- 1 root root 43 May  9 12:07 act.txt
 -rw-r--r-- 1 root root 43 May  9 11:27 arrow.txt
 -rw-r--r-- 1 root root 43 May  9 12:08 mados.txt *(of importance)
 root@pve:~# chmod 600 mados.txt 
 root@pve:~# ls -l
 -rw-r--r-- 1 root root 43 May  9 12:07 act.txt
 -rw-r--r-- 1 root root 43 May  9 11:27 arrow.txt
 -rw------- 1 root root 43 May  9 12:08 mados.txt   (of importance)

Note: Bypassing the argument 600, we were able to set the permissions of the owner to read and write while removing all permissions from the group owner and world. Though remembering the octal-to-binary mapping may seem inconvenient, you will usually have to use only a few common ones:
7 (rwx), 6 (rw-), 5 (r-x), 4 (r–), and 0 (—).

B. Symbolic Representation

chmod also supports a symbolic notation for specifying file modes. A symbolic notation is divided into three parts: whom the change will affect, which operation will be performed, and which permission will be set. To specify who is affected, a combination of the characters u, g, o, and a is used, chmod Symbolic Notation Symbol Meaning.

u == Short for user but means the file or directory owner.
g == Group owner.
o == Short for others but means world.
a == Short for all; the combination of u, g, and o.

If no character is specified, all will be assumed. The operation may be “a +” indicating that permission is to be added, “a -” indicating that a permission
is to be taken away, or “a =” indicating that only the specified permissions are to be applied and that all others are to be removed.

Note: Permissions are specified with the r, w, and x characters. chmod Symbolic Notation Examples, (Notation Meaning)

u+x Add executes permission for the owner.
u-x Remove execute permission from the owner.

x – Add execute permission for the owner, group, and world. (Equivalent to a+x).
o-rw – Remove the read and write permissions from anyone besides the owner and group owner.
go=rw – Set the group owner and anyone besides the owner to have read and write permission. If either the group owner or world previously had executed permissions, remove them.
u+x,go=rx Add execute permission for the owner and set the permissions for the group and others to read and execute. Multiple specifications may-be separated by commas.

Note: Some people prefer to use octal notation; some folks really like the symbolic. Symbolic notation does offer the advantage of allowing you to set a single-attribute without disturbing any of the others.

Changing Identities: At various times, we may find it necessary to take on the identity of another user. Often we want to gain superuser privileges to carry out some administrative tasks, but it is also possible to ìbecomeî another regular user to perform such tasks as testing an account. There are three ways to take on an alternate identity.

— Log out and log back in as the alternate user.
— Use the su command.
— Use the sudo command.

The choice of which command to use is largely determined by which Linux distribution you use. Your distribution probably includes both commands, but its configuration will favor either one or the other. su run a shell with Substitute User and Group IDs. The su command is used to start a shell as another user. The command syntax looks like this.

root@pve:~# su [-[l]] [user]
or simply
root@pve:~# sudo -i

As the case maybe!!!

Note: If the -l option is included, the resulting shell session is a login shell for the specified user. This means that the userís environment is loaded and the working directory is changed to the userís home directory. This is usually what we want. If the user is not specified, the superuser is assumed. Notice that (strangely) the -l may be abbreviated as -, which is how it is most often used. To start a shell for the superuser, we would do this.

(Note: -l may be abbreviated as shown low (su -) only

root@pve:~# su -l
root@pve:/# su –

This is referred to as the superuser.

sudo – Execute a Command as Another User
The sudo command is like su in many ways but has some important additional capabilities. The administrator can configure sudo to allow an ordinary user to execute commands as a different user (usually the superuser) in a very controlled way. In particular, a user may be restricted to one or more specific commands and no others. Another important difference is that the use of sudo does not require access to the superuserís password.

chown – Change File Owner and Group
The chown command is used to change the owner and group owner of a file Superuser privileges are required to use this command or directory.
Superuser privileges are required to use this command.

chown can change the file owner and/or the file group owner depending on the first argument of the command. Let’s say that we have two users: janet, who has access to superuser privileges, and tony, who does not. User janet wants to copy a file from her home directory to the home directory of user tony. Since user janet wants tony to be able to edit the file, janet changes the ownership of the copied file from janet to tony:

[janet@linuxbox ~] $ sudo cp myfile.txt ~tony

[janet@linuxbox ~]$ sudo ls -l ~tony/myfile.txt
-rw-r–r– 1 root root 8031 2012-03-20 14:30 /home/tony/myfile.txt

[janet@linuxbox ~]$ sudo chown tony: ~tony/myfile.txt

[janet@linuxbox ~]$ sudo ls -l ~tony/myfile.txt
-rw-r–r– 1 tony tony 8031 2012-03-20 14:30 /home/tony/myfile.txt