Login/Logout:
We want to get started using Unix/Linux by connecting to and login/logout in to our
Unix/Linux system with a set of credentials. In Unix/Linux, there are different types of accounts. Some are more powerful than others, allowing you to do more or less depending on the rights and privileges assigned to them.
The root account and generic user accounts are the most common accounts seen on
Unix/Linux systems. Root is the administrator's user account. It has the most privileges available to the system and can do the most harm as well.
Unix is a multi-user platform. This allows for multiple users to log in to the system
simultaneously, set up their own environments, and so on. Because Unix/Linux enables multiple users to access the system simultaneously, you can be working on a large calculation on a
spreadsheet while another user on the system is running another type of calculation of some sort. Many different processes can run simultaneously on a single computer by hundreds of different users.
Text-based login takes us right to where we want to go: to the command line or shell prompt. Most text-based Unix/Linux login prompts look like this:
login:
password:
When wee see the login: prompt, we will type our username and press return. The password: prompt appears immediately thereafter. When we have successfully typed in both your username and password, we reach what is called a shell prompt. When we login on Unix/Linux environments with correct parameters, it come something like the following:
David login: sarge
Password:
Linux David 2.6.37-1-686 #1 SMP Tue Feb 15 18:21:50 UTC 2011 i686
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in
/usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
You have new mail.
sarge@David:~$
When working within the shell prompt, all you need to do to log out is type "logout."
The command would be seen as
unix-sarge@David:~$logout
Change your password by using passwd command
The passwd command changes passwords for user accounts. A normal user may only change the password for his/her own account, while the superuser may change the password for any account. Passwd also changes the account or associated password validity period.
To change your password run:
unix-sarge@David:~$passwd
Changing password for sarge.
(current) UNIX password:
(new) UNIX password:
(retype new) UNIX password:
Password successfully changed
Getting info about user by using finger/chfn/date commands:
By using finger command we will get information about users that they're local to our system. The syntax to run finger command is #finger username. To get information about remote users we can perform the syntax #finger username@remote host.
The chfn command allow us to change real user name and information. It runs an interactive process that enables us to set more personal information into our account:
unix-root@David:/home/sarge#chfn sarge
Changing the user information for sarge
Enter the new value, or press ENTER for the default
Full
Name [sarge]:
The date command print or set the system date and time. Running simply date command without option or
parameters printout:
root@David:/home/sarge#date
Sun Feb 27 16:09:13 GMT 2011
Knowing system up time and logged users by using uptime/who commands:
The uptime command gives a one line display of the: current time, how long the system has been running, how many users are currently logged on, and the system load averages for the past 1, 5, and 15 minutes:
unix-root@David:/home/sarge#uptime
16:12:11 up 3:03, 2
users, load average: 0.00, 0.11, 0.14
Consequently of this command we can run who command to see who is logged on:
unix-root@David:/home/sarge#who
sarge tty7 2011-02-27 13:10 (:0)
sarge pts/0 2011-02-27 13:21 (:0.0)
By the output we can see there is only one user that logged on 2 different session.
Creating aliases of commands by using alias command:
The alias command is highly useful to system administrator that can really reduce the time for doing routines job (for complex job automation
bash script is the best way). An alias is a file that represents another object in the file system. If we have some command with regular expressions that we running periodically is very useful to create an alias by correlating to command that we running.
For example, if we want to reboot our system at 23:15PM and we would like to put a short message to let know to other administrators about the reason of reboot (backup periodically, os patch application, etc.) :
unix-root@David:/home/sarge# shutdown -r 23:15 "I need to reboot the machine because the OS patch applied successfully right now"
And if this process should have run aperiodically on discretion of mine, is better to create an alias and run anytime needed:
unix-root@David:/home/sarge# alias patchosreboot='shutdown -r 23:15 "I need to reboot the machine because the OS patch applied successfully right now"'
Or if I want to call the normal shutdown command in “spegniti”(that is a sympathetic way to say in my language, in Italian, shutdown):
unix-root@David:/home/sarge#alias shutdown='spegniti'
And starting from now if I run spegniti my pc will shutdown.
Setting environment variable and paths by using setenv command and PATH:
The setenv command change or add an environment variable in our system. Environment variables are used by programs to pick up specific
pieces of information that are needed when the program is run. To set an environment variable we can perform the following sytax:
unix-root@David:/home/sarge#setenv variablename value
The path tells the shell where to look for programs that we want to execute. Sometimes running some command by terminal are given result like “command not found”. If we are sure that this command exist and no mistake that mean we missing some path we need. If we want to set the path that the current directory /usr/local/bin, we can do that by using the following instruction:
unix-root@David:/home/sarge#set path=($path /usr/local/bin .)
Searching whatever in system by using the find command:
The find command search for files in a directory hierarchy. Using find command to find files and directories. This command would appear as find <starting directory> -name <filename> -print, or if we wanted to find a specific file in the root directory:
unix-sarge@David:~$ find / -name *.log print
In some cases, we may not be able to recall what a file is named or what its extension is, but we may know what day we created it. Creation date is another searchable criterion that we can select. To search, we will need to adapt a "how many days ago" mentality because Unix will
search for files that have been made since the time that you specify.
Use the find command with the -ctime option: find <starting directory> -ctime <how many days old> -print. For example: unix-sarge@David:~$ find ~/ -ctime 5 -print or finding file bye its size: find ~/ -size 1024k -print The first command specifies that Unix should find a file in our home directory that is less than five days old and print it to the screen.
The whereis command can be used to run a quick search on a specific number of directories for whatever we specify. For example, if we wanted to run a search on a file named "test", then we would get all instances of "test" that came up within that preset number of directories, such as paths to source code, binaries, and man pages. The whereis command performs a quick search for the file we specify.
unix-sarge@David:~$whereis test
Analysis of file contents by using grep command:
Grep searches the named input files (or standard input if no files are named, or if a single hyphen-minus (-) is given as file name) for lines containing a match to the given PATTERN. By default, grep prints the matching lines.
One of the most commonly used Unix/Linux commands is grep. Learning how to use grep will help us find files that contain a word or pattern. When the grep command is used properly, it can help us search through a file for something specific.
We will soon understand that grep is one of the most powerful built-in programs in Unix/Linux. The program grep is also subdivided into more commands: grep, egrep, and fgrep. These three commands differ in what regular expressions they can handle. A regular expression is a pattern that can match various text strings. Regular expressions define a pattern of text that can be used to search files when a specific word or phrase to be searched for might not be known.
unix-sarge@David:~$ grep "error" *.log
In this example, we see a quick grep of the word error in any log files. The grep
command is a tool that will become even more valuable when we learn more about regular expressions.
Working with files at the Shell Prompt
The command line is our primary interface to the Unix/Linux file system as well as our primary tool for manipulate data.
File content analysis and searching by using regular expressions:
Each Unix/Linux system administrator need to know and use regular expressions to simplify they're job. By using regular expressions like wildcard (*), (.), ([]), (^) and ($) symbols we can find anything in our system at a very granular level.
We are going to use on of file in our system to give an example that the data we will search
to learn how to use regular expressions:
unix-sarge@David:~$ grep "Sun..." clipdat2.rdf
Le support SMC.
Sun Microsystems.
SMC Support. Sun Microsystems.
In this example we are looking to find in this file all the term that come after word “Sun”.
It was able to do so, even though we left the last three letters "ems" off, and intentionally put in three periods so that Unix/Linux could come back to me with what it found in the clipdat2.rdf file as a match. This can be used in multiple ways, such as the following:
sarge@David:~$
grep "S*.ms" clipdat2.rdf
Le support SMC.
Sun Microsystems.
SMC Support. Sun Microsystems.
As we know there are some telephone number in this file and we would like to view the range of numbers only that allow us ti see quickly only numbers:
sarge@David:~$ grep "+[0-9]*" clipdat2.rdf
<NS1:clipping
RDF:about="rdf:#$+K6qG2"
+33 (0)1.34.03.00.61
Change/crate/update file timestamps by using touch command:
The short definition of touch command is that change
file timestamps. If we want to create a new empty file in Unix/Linux, the most easiest way that when we use the touch command. The touch command is used to update the last modified time of a file, setting it to the current time. The touch command is normally used with one particular area: backup and disaster recovery. As a Unix/Linux system administrator, you may be asked to do backup and restore jobs. There are a few different types and methods of backup
and restore we can choose from, one of which is called an "incremental backup” that the touch command can be used by helping to verify that a backup was in fact completed .
unix-sarge@David:~$touch backupfile
Performing the above command that if the backup file previously existed, its last-modified
date would now be set to the current time. If the backup file did not previously exist, it would now exist as an empty file with a last-modification date of the current time. It's that easy.
As with using touch, rm is easy: As long as you can find and specify the filename,
you can remove it.
Determine which file or files you want to delete and issue the rm command as rm <filename>.
Create and remove directories by using mkdir and rmdir commands:
The mkdir command create the directory(ies), if they do not already exist. The directories are basically used to organize data. We may have
experienced instances where we have one directory with hundreds of files. It would not be easy to find anything quickly unless we memorize every filename on our system.
To create directory issue the mkdir command as mkdir <directoryname> and to remove directory perform rmdir command as rmdir <directory>.
Removing the files and directories at the same time we can use rm -r option.
unix-sarge@David:~$mkdir test (creating test
directory)
unix-sarge@David:~$rmdir -r test (removing test directory and sub-directories/files)
Copying/renaming files by using cp and mv commands:
The cp command will allow us to copy a single file to a new destination file, or copy one or more files to a single destination directory.
Perform the cp command as cp <sourcefile>
<destinationfile>.
unix-sarge@David:~$
cp testfile /tmp/
The cp command has a recursive mode for copying directories. When it is used with the following syntax, the cp command with the -r option, you will be able to copy each source directory (as well as files) into whatever destination directory you specify.
Perform the cp command as cp -r <sourcedirectory1> <sourcedirectory2> [...]
<destinationdirectory>.
To move a file or directory, we need to use the mv command. The mv command will move or rename a file based on the destination. Perform the mv command as mv <sourcefile> <directory>.
unix-sarge@David:~$mv testfile /tmp/testfile ormv testfile /tmp/testfile.old
Creating symbolic link link by using ln command:
The literal definition of ln command that make links between files. Is used to build links or aliases to other files on our Unix/Linux system. We can create manageable links to other files so that they can appear in the ls command output when we
want the source file to appear to be in different locations, as well as have different names.
To create a symbolic link, issue the ln command as ln -s realfilename/dir alternatename/link
unix-sarge@David:~$ln -s /data/directory/accessible /everybody/public
Viewing file contents by using cat/less/more commands:
The cat commands is useful to view the file(s) for its current entries. It .concatenate files and print on the standard output. For example ti
view the file host we perform the following action on command line using cat command:
unix-sarge@David:~$cat /etc/hosts
127.0.0.1 localhost
127.0.1.1 David
…...............(the rest of the output removed)........................
The cat command is also know I/O redirection command to sending input/output to somewhere different than the default locations by combination of a couple special characters. The standard characters for redirection input/output respectively <> symbols.
For example, if we want to redirect the output of ps command on file “processes” because is so long to read on display
(vice verse for input):
unix-sarge@David:~$ps aux | cat > processes
The less command is similar to using the cat and more commands. Less
is a program similar to more, but which allows backward movement in the file as well as forward movement. As a matter of fact, the syntax is nearly identical. It's really what the tool does that makes it different. When using the less command, we have more control over the pager than ever before.
As you can see by command executed above we have pipe (|) symbol also. The pipe symbol allow us to combine the Unix/Linux commands and its very useful for system administrators..
Viewing the contents of files by using tail/head commands:
The tail command is used to view the end of the file and head command is used to view the top of the file. The tail command is powerful, quick, and simple to use. If we want to see the bottom 20
lines of a file, then we may want to just specify the tail command and the file you want to view the inside of and the vice verse for head command to view the top 20 lines.
unix-sarge@David:~$tail 20 /var/log/messages
unix-sarge@David:~$head 20 /var/log/messages
Editing files by using word processors “The vi Editor”:
When working within the shell prompt, we will need word processor for editing/writing files.
There are a lot of variety of editors in Unix/Linux but here we are going to use vi editor, which is nothing more than a Unix-based word processor. Personally I prefer vim (Vi Improved) editor:-)
To use the vi editor, we only need to open it up using the vi command:
unix-sarge@David:~$vi testfile
This will open up the vi editor and the file opened will be the new file that we called
testfile. Now that we have a file opened, we can use a plethora of commands to edit the file. So, we have a file open, we may be able to read a help file for a specific application installed on our system. No matter what we choose, we will find the vi editor has a wide array of commands that can be used within it to work with the open files. One thing we have to consider is what mode the vi editor is working in. There are two modes that the vi editor
operates in. The vi editor either uses command mode or insert mode. In command mode we can control things such as cursor position, deleting characters, and saving files. Iin insert mode we can insert characters.
As we are working in shell environment, mastering the vi editor comes only from
mastering the keyboard shortcuts used to operate the vi editor. The most common manipulation keyboard shortcuts we will use are:
Command Mode | Key Combination | Description |
l | Move right | |
h | Move left | |
j | Move the next line | |
k | Move the previous line | |
x | Delete character | |
dd | Delete entire line | |
A | Append the end of line | |
i | Change the insert mode | |
:w Return | Save file | |
:w<filename> | Save the file to a new name | |
:q Return | To exit vi | |
:q! Return | To exit vi without sawing | |
Insert Mode | Esc | Changes to command mode |
Backspace/ Delete | Backspaces or deletes, but only for data just |
Some other basics and helpful commands in vi editor are: [Ctrl+d] that tells the computer
that you are done sending input to a command, [Ctrl+c] the Unix/Linux break character. [Ctrl+z] that suspends the process we are currently running and returns to a command prompt.
Fore more details and help about vi editor (and any other commands) just perform man pages, apropos or whatis commands (for ex: man vi giving us all the help that we need to use vi editor).
If you are using GUI like KDE, GNOME or others X-Window System, there are graphical editors like kate, kedit, gedit, etc.
Count word/line and byte of file content by using wc command:
The wc command (stand for word count) print newline, word and byte counts for each file, some kind of counter. When we want to see how many words are typed in a file, we simply need to execute the command and then the filename with its absolute or relative path. We will be shown the word count as well as the number of bytes, words, and lines in files.
The wc command is an excellent way to provide current statistics on a file you may want to work with. For example, if we wanted to issue the tail command on a file we think may be very large, we can use the wc command on a file to quickly see how many lines are in it.
unix-root@David:/home/sarge#wc /var/log/syslog
8 89 718 /var/log/syslog
As we can see by command that performed above a set of values returned by Unix/Linux when queried with the wc command. The first value is 8 and indicates the number of lines in the file. The second line is a count of the words in the file, and the third is the number of characters. We can limit the values to lines, words, or characters by using the -l, -w, or -c options, respectively.
Splitting, patching and updating files by using split/diff/patch commands:
Literally split a file into pieces. Following the wc
command interaction on some files let us know much data is in a file, and what that file is comprised of logically to build up its internal structure, we will look at how to take a large file and break it down. The split command will cut down a file into whatever length we specify. To use the split command we need only to know that a file is too large to work with or to send to someone. Once this is determined, we can execute the split command to break it
down.
So, we proceed to choose our input file that we want to break down and determine the number of lines we want stored in each output file and finally we chose a base output file name that we wish to record data.
unix-root@David:/home/sarge# split -l 6 /var/log/syslog filerecorddata
The syslog file has been run through split and has been divided into two files of six lines each. The -l option specified the 3 lines each. We can reassemble the file after we break and split it up. To do this we need to use cat command. In
case that we created files “filerecorddata00, filerecorddata01, filerecorddata02,.... we just need to perform the following command to reassemble our files like original one:
unix-root@David:/home/sarge#cat filerecorddata* > syslog-original
The diff and patch commands are doing exactly the same that we've done right now with cat command. They're useful to create patch files and then update the new patch file to existing one:
unix-root@David:/home/sarge#diff existingfile updatefile > patchapplied
unix-root@David:/home/sarge#patch existingfile patch
File compression by using tar command:
The tar command (stand for Tape Archive) in its simplest form either creates or unpacks archive files. If we want to create any archive, we should provide tar with a filename for the archive and a list of files that we want to archive. The tar program will collect
all the files we specify and put them into one single file that is commonly called a tarball.
To use tar to create an archive and extract the same, we can perform somethingsimilar like:
unix-root@David:/home/sarge# tar -cvf clipdat.tar clipdat2.rdf
unix-root@David:/home/sarge# tar -xvf clipdat.tar
File compression by compress/gzip/bzip2 tools:
One of the common issue that we encounter during our Unix/Linux systems that space problem and the sole reason for compressing files is to save space. There are 3 major compression formats we will use when working with Unix/Linux: compress, pkzip, gzip.
The compress command is an older Unix command that uses an older algorithm to make the compression (compress <filename>. So now the Unix/Linux environment it's been moved to the side and replaced compress by tools such as gzip.
Files created with the compress command have the file suffix .Z. The uncompress command like it's name uncompresses the results of a compress command (uncompress <filename.Z>).
The gzip command will work
essentially identically to the compress/uncompress/zcat suite and it is a better utility and less proprietary than the older tools in use such as compress. When gzip is combined with tar, the resulting file extensions may be .tgz, .tar.gz, or tar.Z.
There is also bzip2 that is freely available and high-quality data compressor. Compression with bzip2 follows the gzip format bzip2 <filename>, which produces the compressed file <filename.bz2>.
Decompression is simply bunzip2
<filename.bz2>.
Basic UNIX/Linux Processes:
The Unix/Linux systems allow us multitask in that it lets us run more then one process at a time. If we wish to run some process in background we can type ampersand (&) symbol to the end of command that we running.
Using
Ctrl+Z and bg commands we can manage the processes. By pressing Ctrl+Z we can stop any processes that running. If we wish to moving it into background some process before we have to suspend it and than use the bg command. When we press Ctrl+Z the system give us an output by assigned job number of the process. We just need to (see also jobs
command):
unix-root@David:/home/sarge#bg
%x
(where x is number of job suspended)
To bring back the process from background:
unix-root@David:/home/sarge#fg
%x
View running processes by using ps command:
The ps displays information about a selection of the active processes. If you want a repetitive update of the selection and the displayed information, use top instead. To see every process on the system using standard syntax:
unix-sarge@David:~$ps aux or for unix env
ps
-ef
USER
PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root
1 0.1 0.0 2080 688 ? Ss 13:08 0:01 init [2]
root
2 0.0 0.0 0 0 ? S 13:08 0:00
[kthreadd]
root
3 0.0 0.0 0 0 ? S 13:08 0:00
[ksoftirqd/0]
…...................(The rest of output removed)..............................
Terminating process by using kill/killall commands:
The kill command send a signal to a process by using default signal which is TERM. If we don't want to use default signal and if we want to view available signals we can use -l or -L options. Particularly useful signals include HUP, INT, KILL, STOP, CONT, and 0. Alternate signals may be
specified in three ways: -9 -SIGKILL -KILL.
For example if there is a process that causing some problem in our system, the first step to
do that run ps/top command to get PID of this process. So, considering that the PID is 2318:
unix-sarge@David:~$kill -9 2318
By killall command we can
proceed to kill the process by their name:
unix-sarge@David:~$killall firefox-bin
Automating processes by using at/cron commands:
There are two basic ways to set up our Unix/Linux system to run a process at a specific time; one way is to use the cron command, and the other is to use the at command.
The cron command, is also called cron daemon, provide to execute scheduled commands. cron also reads /etc/crontab, which is in a slightly different format. Additionally, cron reads the files in /etc/cron.d: it treats the files in /etc/cron.d as in the same way as the /etc/crontab file (they follow the special format of that file, i.e. they include the user field). An example would be:
unix-sarge@David:~$crontab -a cronfile
The -a option will install the cronfile as our crontab file and that will allow us to edit our crontabfile if we need to change processes. We can display our crontab files by using -l option.
Each entry in a crontab file consists of six fields, specifying the following information:
minute(s) hour(s) day(s) month(s) weekday(s) command(s)
The at command read commands from standard input or a specified file which are to be executed at a later time, using /bin/sh. When we type
the at command we will get into the environment of at> that allow us to establish run time that we need to run some processes. At allows fairly complex time specifications, extending the POSIX.2 standard. It accepts times of the form HH:MM to run a job at a specific time of day. (If that time is already past, the next day is assumed.) You may also specify midnight, noon, or teatime (4pm) and you can have a time-of-day suffixed with AM or PM for running in the morning or the evening:
unix-sarge@David:~$at midnight
warning:
commands will be executed using /bin/sh
at>
tar -cvf /home/sarge/Documents /backups/Documents-sarge.tar
at>
Ctrl+d
at>
<EOT>
job
1 at Mon Feb 28 00:00:00 2011
We are submitting a job that will run at midnight of the current day. As well, at will create a tarball of /home/sarge/Documents directory and call it Documents-sarge.tar . By Ctrl+d we break out of the at process and return to our shell prompt.
Data structure permission with chmod in UNIX/Linux
environment:
The chmod that allow us to change file mode bits. Here are some examples of what we can do with the chmod command:
root@David:/home/sarge#chmod 755 clipdat2.rdf
By running the above instructions we giving to the owner of file full read, write and execute permissions on file clipdat2.rdf.
root@David:/home/sarge#chmod u=rwx,g=rx,o=rx
clipdat2.rdf
By this command we are giving full read, write and execute permission to user, read and write permission to group and read and execute permission to other.
Permission Bits:
To figure this out we need to know how binary numbers are converted to decimal. We need only to know how to convert the first three numbers. From moving from right to left, start to count from 1 and move up by the power of 2 each time, so we would have 1, 2, and then 4. If that is the case, then we need to know that if we took the first bunch of three 001 and counted from left to right saying that 0 is "off" and a 1 is "on," the first one count from right to left by 1, 2, and 4 would be 1. Now take the next example010. This one is two because the middle column in our example of 1, 2, 4 from the right to left would mean that 0 is off, then 2 is on, then 4 is off. Get it now? How about the last one… 100.
With this example, we can quickly figure out the decimal number we need to use; instead of setting permissions. By this technique, we can easily set multiple permissions simultaneously. For example, it's easy to see that 110 is the combination of the read and write permissions.
The decimal value of this binary string is 6 (4+2). I now have my setting for read and write permissions. To use this method of setting a file's permissions, we set permissions for owner, group, and other simultaneously. Each of these digits is the sum of the permissions that we want to set. The first digit is the owner, the second is the group, and the third is other.
For example, suppose that you want to set the owner to have full permissions, and the group and other to have read and execute permissions. Full permissions are achieved by adding all the
permission values (4+2+1=7). Read and execute permissions are a combination of 4+1=5. The three numbers you'll use to set this are 7, 5, and 5, entered as a single three-digit number, 755.
Very nice blog! Thanks for sharing the best information. This blog is very amazing and helpful for me. Thank you again!!
ReplyDeleteDubai VPS
love the post!!!
ReplyDeleteThank you for your intelligent post and for helping others become more aware. You made more sense than others who speak within this same area of expertise and I am really glad I found your blog-website. I’ve joined your social networks and will keep an eye out for future great posts as well. Additionally, I have shared your site in my social networks as well. Thank you again!
Norton Tech Support Phone Number USA