Deployment procedure of Glass fish Server on Ubuntu


System Requirements

server requires the following dependencies to be installed.

1. Ubuntu 11.04 OS
2. Jdk 7
3. Mysql server 5.1.54
4. Glassfish Server version 3.1


Step 1: Login to the server

1.1 Remote login server by ‘SSH Secure Shell Client’

1.2 Login directly as root user
After logging into the server directly, open the terminal window.

Write following command sequentially in the terminal window.

JDK
1. sudo add-apt-repository --yes ppa:webupd8team/java
2. Write your root user password
3. sudo apt-get update
4. sudo apt-get install --yes oracle-java7-installer

MYSQL SERVER
5. sudo apt-get install --yes mysql-server
Glassfish
6. cd /root/
7. wget http://download.java.net/glassfish/3.1.2.2/release/glassfish-3.1.2.2.zip
8. unzip glassfish-3.1.2.2.zip


Step 2: Database

Requirements:
1. databasebackup.sql
2. Mysql server installed


3.1 Create a new Database
1. Open the terminal window

2. Type: MYSQL –uusername –ppassword;” (-uroot –ppassword123) to login to mysql server
3. Type: CREATE DATABASE databasebackup CHARACTER SET utf8 COLLATE utf8_general_ci;
4. Type: SHOW DATABASES; (to list the existing database and check if the new DB is there.)
5. Type: “exit” to logout from the mysql server

3.2 Restore the database
1. Upload the database backup file (i.e databasebackup.sql) to a location on the server.
2. Open the terminal window.
3. Goto the directory where the main changescript is uploaded
4. Type: MYSQL -uusername -ppassword “name of database” < databasebackup.sql
5. Type: MYSQL –uusername –ppassword; to login to mysql server
6. Type: USE databasebackup; to select the database
7. Type: SELECT * FROM table_name; (if the databasebackup.sql was successfully restored then this will display the table_name table information)



Step 3: How to start glassfish server
After you have unzipped the glassfish server successfully, go into the glassfish bin directory to start the server

Start or stop glassfish server.

1. Open the terminal window
2. Type: cd /root/glassfish3/bin/
This command will take you to the Glassfish installer directory.
3. Type: ./asadmin start-domain domain1
This command will start the Glassfish server.
4. Type: ./asadmin stop-domain domain1
This command will start the Glassfish server.


Step 4: How to deploy Server

Requirements:
1. server.war file
2. Glassfish server version 3.1
3. Mysql server installed
Please collect the latest server WAR file from the developer. If you have the latest war file, use the following steps to deploy the server. Please also ensure that the server database is setup before you proceed.

4.1 Deploy WAR File:
1. To check if the Glassfish server is up and running . Open a web browser and type the URL to access the the admin console for Glassfish. You will need to specify the port number as well (example 'http:// 192.168.1.105:4848') the default port number of glashfish3 is 4848.
2. Log into the Glasshfish admin panel (default username:password)
3. Click on ‘Application’ link from side bar
4. Click on ‘deploy’ button
5. Select the location radio button. If the server WAR file is located your local hard disk, select the first option. If it is on the server hard disk then select the second option.
6. Click on ‘Ok’ button
7. If the server is successfully deployed then you will see it in the applications panel


Step 5: How to setup the server storage directory

Requirements:
1. Newly installed server WAR file in glassfish server
2. The databasebackup.sql is running on a mysql server
Once the server is successfully deployed and configured to the correct database you will need to specify the image repository in glassfish and the mysql database. This is the directory where all the images will be stored.


5.1 Create the storage directory
1. Open the terminal window.
2. Type: MKDIR <foldername> (/imagedir)
3. Type: CD /imagedir (go inside the /imagedir and create the following subdirectories /abc, /def)
4. Type: MKDIR abc
5. Type: MKDIR def
6. Type: CHMOD 777 /imagedir (to apply the read write execute permission)

5.2 Setup repository path in Glassfish
The first step is to add the directory as a property in Glassfish Server
1. Open a browser and login to the glassfish admin console (https://192.168.1.105:4848)
2. Extend 'Server-config' in side menu bar (Configurations>server-config)
3. Extend in 'Virtual Service' (Configurations>server-config> virtual service)
4. Click on 'Server' (Configurations>server-config> virtual service> server)
5. Click the “Add Property” button in Additional Properties .
6. Enter name and values
7. Example name=”alternatedocroot_1”
8. Example value= “from=/ imagedir /* dir=/root/glassfish/”
9. Click on 'Save' button

How to edit web.xml file at Apache Tomcat or GlassFish server on Ubuntu


1. Setup 'SSH Secure Shell Client'
2. Open 'SSH Secure Shell Client' and  'Quick Connect'


3. Connect to Remote Host
   

 Enter Password and Click 'Ok' button

4. Open file web.xml
# vi the path of web.xml file
Example:  vi glassfish3/glassfish/domains/domain1/applicatio
ns/customer/WEB-INF/web.xml
  • press i    (for editing)   
  • Esc:q!      (for without change)
  • Esc:x       (for change)

How to file transfer one machine from other machine in ubuntu by Windows XP, Windows 7 and Windows 8

1. Setup 'SSH Secure Shell Client'
2. Open 'SSH Secure Shell Client' and  'Quick Connect'


3. Connect to Remote Host
   

 Enter Password and Click 'Ok' button

4. create a tar ( if file or directory do not tar )
  # tar -cvf archive.tar file1 0r dir1
Example: tar -cvf card.tar card (the card.tar will be create on  current directory )

5. Files transfer one machine to other machine
# scp filename username@ip:dir
Example:  scp card.tar root@192.168.1.126:/home/root/
    Enter other Ubuntu machine password
6. Remote other Ubuntu machine
# ssh username@ip
Example: ssh root@192.168.1.126
Enter other Ubuntu machine password

7.  Go to Files transfer directory and extract the tar file
# tar -xvf filename 
Example: tar -xvf  card.tar (file extract on  current directory)

8. Delete old file
# rm filename
Example: rm  card.tar






Important MySQL Commands


To login (from SSH Secure Shell Client ) use -h only if needed.

* mysql -h hostname -u root -p password;

Create a database on the sql server.

* mysql> create database [databasename];
Example:  create database onlineshopmanagementdb;

Create a database on the sql  server any launguage

* mysql> create database [databasename] CHARACTER SET utf8 COLLATE utf8_general_ci;
Example:  create database onlineshopmanagementdb CHARACTER SET utf8 COLLATE utf8_general_ci;

List all databases on the sql server.

* mysql> show databases;
Example: show databases;

Switch to a database.

* mysql> use [db name];
Example: use  onlineshopmanagementdb2;<br>
To see all the tables in the db.
* mysql> show tables;
Example: show tables;

To see database's field formats.

* mysql> describe [table name];
 Example:  describe customer;

To delete a db.

mysql> drop database [database name];
Example: drop database onlineshopmanagementdb3;

To delete a table.

* mysql> drop table [table name];
Example: drop table product;

Show all data in a table.

* mysql> SELECT * FROM [table name];
Example: select * from customer;

Returns the columns and column information pertaining to the designated table.

* mysql> show columns from [table name];
Example:  show columns from customer;

Use a regular expression to find records. Use "REGEXP BINARY" to force case-sensitivity. This finds any record beginning with a.

mysql> SELECT * FROM [table name] WHERE rec RLIKE "^a";

Show unique records.

mysql> SELECT DISTINCT [column name] FROM [table name];

Show selected records sorted in an ascending (asc) or descending (desc).

mysql> SELECT [col1],[col2] FROM [table name] ORDER BY [col2] DESC;

Return number of rows.

mysql> SELECT COUNT(*) FROM [table name];

Sum column.

mysql> SELECT SUM(*) FROM [table name];

Creating a new user. Login as root. Switch to the MySQL db. Make the user. Update privs.

*  mysql -u root -p
mysql> use mysql;
mysql> INSERT INTO user (Host,User,Password) VALUES('%','username',PASSWORD('password'));
mysql> flush privileges;

Change a users password from SSH Secure Shell Client

* mysqladmin -u username -h hostname.blah.org -p password 'new-password';

Change a users password from MySQL prompt. Login as root. Set the password. Update privs.

# mysql -u root -p
mysql> SET PASSWORD FOR 'user'@'hostname' = PASSWORD('passwordhere');
mysql> flush privileges;

Recover a MySQL root password. Stop the MySQL server process. Start again with no grant tables. Login to MySQL as root. Set new password. Exit MySQL and restart MySQL server.

# /etc/init.d/mysql stop
# mysqld_safe --skip-grant-tables &
# mysql -u root
mysql> use mysql;
mysql> update user set password=PASSWORD("newrootpassword") where User='root';
mysql> flush privileges;
mysql> quit
# /etc/init.d/mysql stop
# /etc/init.d/mysql start

Set a root password if there is on root password.

# mysqladmin -u root password newpassword

Update a root password.

# mysqladmin -u root -p oldpassword newpassword

Allow the user "bob" to connect to the server from localhost using the password "passwd". Login as root. Switch to the MySQL db. Give privs. Update privs.

# mysql -u root -p
mysql> use mysql;
mysql> grant usage on *.* to bob@localhost identified by 'passwd';
mysql> flush privileges;

Give user privilages for a db. Login as root. Switch to the MySQL db. Grant privs. Update privs.

# mysql -u root -p
mysql> use mysql;
mysql> INSERT INTO db (Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv) VALUES ('%','databasename','username','Y','Y','Y','Y','Y','N');
mysql> flush privileges;
or
mysql> grant all privileges on databasename.* to username@localhost;
mysql> flush privileges;

Update database permissions/privilages.

mysql> flush privileges;

Delete a column.

mysql> alter table [table name] drop column [column name];

Add a new column to db.

mysql> alter table [table name] add column [new column name] varchar (20);

Change column name.

mysql> alter table [table name] change [old column name] [new column name] varchar (50);

Make a unique column so you get no dupes.

mysql> alter table [table name] add unique ([column name]);

Make a column bigger.

mysql> alter table [table name] modify [column name] VARCHAR(3);

Delete unique from table.

mysql> alter table [table name] drop index [colmn name];

Load a CSV file into a table.

mysql> LOAD DATA INFILE '/tmp/filename.csv' replace INTO TABLE [table name] FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (field1,field2,field3);

Dump all databases for backup. Backup file is sql commands to recreate all db's.

# [mysql dir]/bin/mysqldump -u root -ppassword --opt >/tmp/alldatabases.sql;

Dump one database for backup.

# [mysql dir]/bin/mysqldump -u username -ppassword --databases databasename >/tmp/databasename.sql;

Dump a table from a database.

# [mysql dir]/bin/mysqldump -c -u username -ppassword databasename tablename > /tmp/databasename.tablename.sql

Restore database (or database table) from backup.

# [mysql dir]/bin/mysql -u username -ppassword databasename < /tmp/databasename.sql

Create Table Example 1.

mysql> CREATE TABLE [table name] (firstname VARCHAR(20), middleinitial VARCHAR(3), lastname VARCHAR(35),suffix VARCHAR(3),officeid VARCHAR(10),userid VARCHAR(15),username VARCHAR(8),email VARCHAR(35),phone VARCHAR(25), groups VARCHAR(15),datestamp DATE,timestamp time,pgpemail VARCHAR(255));

Create Table Example 2.

mysql> create table [table name] (personid int(50) not null auto_increment primary key,firstname varchar(35),middlename varchar(50),lastnamevarchar(50) default 'bato');

How to backup & restore database to MySQL server on Ubuntu

Backup Only Table from MySQL Database Using mysqldump
# mysqldump  -u[uname] -p[pass] [dbname] > [backupfile.sql];
Example
 mysqldump -uroot -pabcd  shopmanagementdb > shopmanagementdbback.sql ;

Backup Only Table structure from  MySQL Database Using mysqldump
# mysqldump -n  -d  -u[uname] -p[pass] [dbname] > [backupfile.sql];
Example
 mysqldump -n  -d  -uroot -pabcd  shopmanagementdb > shopmanagementdbback.sql ;

Backup Only Store procedure and function  from MySQL Database Using mysqldump
# mysqldump -n -t -d --routines  -u[uname] -p[pass] [dbname] > [backupfile.sql];
Example
 mysqldump -n -t -d --routines  -uroot -pabcd  shopmanagementdb > shopmanagementdbback.sql;

Backup Table and Store Produce from  MySQL Database Using mysqldump
#  mysqldump --routines -u [uname] -p[pass] [dbname] > [backupfile.sql];
Example
mysqldump  --routines -uroot -pabcd  shopmanagementdb > shopmanagementdbback.sql ;

Backup Table and Store Produce and function  from MySQL Database Using mysqldump for any language
#  mysqldump --routines -u [uname] -p[pass] --default-character-set=utf8  [dbname] > [backupfile.sql];
Example
mysqldump  --routines -uroot -pabcd --default-character-set=utf8 shopmanagementdb > shopmanagementdbback.sql;


Backup only Table data  from  MySQL Database Using mysqldump
#  mysqldump  -u [uname] -p[pass] -t [dbname] > [backupfile.sql];
Example
mysqldump  -uroot -pabcd  -t shopmanagementdb > shopmanagementdbback.sql ;


Backup specific Table data  from  MySQL Database Using mysqldump
#  mysqldump  -u [uname] -p[pass] -t [dbname] table1 table2 > [backupfile.sql];
Example
mysqldump  -uroot -pabcd  -t shopmanagementdb customer branch > shopmanagementdbback.sql ;

Restore MySQL Database
# mysql -u[uname] -p[pass] [dbname] < [backupfile.sql] ;
Example
mysql -uroot -pabcd  shopmanagementdb < shopmanagementdbback.sql;

How to start/stop MySql server on Ubuntu, RedHat Linux and OSX

Debian / Ubuntu

# To install MySql database:
sudo apt-get install mysql-server

# To check the status of  MySql server:
/etc/init.d/mysql status

# To start MySql server:
/etc/init.d/mysql start

# To stop MySql server:
/etc/init.d/mysql stop

# To restart MySql server:
/etc/init.d/mysql restart

RedHat Linux (Fedora Core/Cent OS)

# To start mysql server:
/etc/init.d/mysqld start

# To stop mysql server:
/etc/init.d/mysqld stop

# To restart mysql server
/etc/init.d/mysqld restart
OR
service:
# service mysqld start
# service mysqld stop
# service mysqld restart

OSX

# To check the status of  MySql server:
sudo /usr/local/mysql/support-files/mysql.server status

# To start mysql server:
sudo /usr/local/mysql/support-files/mysql.server start

# To stop mysql server:
sudo /usr/local/mysql/support-files/mysql.server stop

# To restart mysql server
sudo /usr/local/mysql/support-files/mysql.server restart

Important linux Command


1. a. Eject CD ROM drive: eject or eject /dev/cdrom
   b. Insert CD Tray: mount /dev/cdrom

2. Copy file: cp <source location> <destination location>

3. Delete file: rm 'file location' such that: rm '/media/SOFTWARE/ubuntu 8.04(deb packages)/a.txt'

4. To remove a directory along with all of its contents: rm -fr 'directory location'

5. To uninstall packages: apt-get remove <package name>

6. To install packages: apt-get install <package name>]

7. The mkdir command will allow you to create directories. Example: mkdir '/home/rubel/Desktop/music' will create a directory called "music" on desktop.
 
8. The cd command will allow you to change directories. When you open a terminal you will be in your home directory. To move around the file system you will use cd. Examples:

    * To navigate into the root directory, use "cd /"
    * To navigate to your home directory, use "cd" or "cd ~"
    * To navigate up one directory level, use "cd .."
    * To navigate to the previous directory (or back), use "cd -"
    * To navigate through multiple levels of directory at once, specify the full directory path that you want to go to. For example, use, "cd /var/www" to go directly to the /www subdirectory of /var/. As another example, "cd ~/Desktop" will move you to the Desktop subdirectory inside your home directory. 


9. The mv command will move a file to a different location or will rename a file.
  a. rename a file: mv /home/rubel/Desktop/a.txt /home/rubel/Desktop/b.txt
  b. move a file: mv /home/rubel/Desktop/a.txt /home/rubel/a.txt

10. The df command displays filesystem disk space usage for all mounted partitions. "df -h" is probably the most useful - it uses megabytes (M) and gigabytes (G) instead of blocks to report. (-h means "human-readable") 

11. Searching and Editing Text Files:
   a. The grep command allows you to search inside a number of files for a particular search pattern and then print matching lines. Example:  grep easy /home/rubel/a.txt search easy from file a.txt
   b. The sed (or Stream EDitor) command allows search and replace of a particular string in a file. For example, sed s/easy/hard/g /home/rubel/a.txt which replace easy to hard from the file a.txt

12. Compression:
   a. zip -r name_of_file.zip files ; example: zip -r b.zip /home/rubel/Desktop/build.xml
   b. unzip filename.zip ; example: unzip /home/rubel/Desktop/a.txt.zip

13. To Create an ISO Image from a CD or DVD: cat /dev/cdrom > /home/rubel/slax.iso 

14. Burning a CD on the Command Line with wodim
    a. See where the CDROM/DVDROM is located: wodim --devices
    b. To burn an audio cd from wav files : wodim dev=/dev/cdrw driveropts=burnfree -v -audio [wav files...]
    c. Blanking a CD/RW :  wodim -vv dev=/dev/cdrw blank=all

15.Shutdown, Restart and Logout of a system:
            a. # init 0          shutdown system(2)
            b. # logout        leaving session
            c. # reboot        reboot(2)
            d. # shutdown -h now              shutdown system(1)
            e. # shutdown -h 16:30 &        planned shutdown of the system at 16:30
            f. # shutdown -c           cancel a planned shutdown of the system
            g. # shutdown -r now               reboot(1)
            h. # telinit 0      shutdown system(3)

16. System information:
            a. # arch            show architecture of machine(1)   [man]
            b. # cal 2009    show the timetable of 2009   [man]
            c. # cat /proc/cpuinfo show information CPU info   [man]
            d. # cat /proc/interrupts           show interrupts   [man]
            e. # cat /proc/meminfo            verify memory use   [man]
            f. # cat /proc/swaps     show file(s) swap   [man]
            g. # cat /proc/version   show version of the kernel   [man]
            h. # cat /proc/net/dev show network adpters and statistics   [man]
            i. # cat /proc/mounts    show mounted file system(s)   [man]
            j. # clock -w    save date changes on BIOS   [man]
            k. # date          show system date   [man]
            l. # date 041217002007.00     set date and time -MonthDayhoursMinutesYear.Seconds   [man]
            m. # dmidecode -q      show hardware system components - (SMBIOS / DMI)   [man]
            n. # hdparm -i /dev/hda           displays the characteristics of a hard-disk   [man]
            o. # hdparm -tT /dev/sda         perform test reading on a hard-disk   [man]
            p. # lspci -tv    display PCI devices   [man]
            q. # lsusb -tv    show USB devices   [man]
            r. # uname -m show architecture of machine(2)   [man]
            s. # uname -r    show used kernel version   [man] 

17. Files and Directory:
            1. # cd /home               enter to directory '/ home'   [man]
            2. # cd ..          go back one level   [man]
            3. # cd ../..       go back two levels   [man]
            4. # cd             go to home directory   [man]
            5. # cd ~user1             go to home directory   [man]
            6. # cd -           go to previous directory   [man]
            7. # cp file1 file2         copying a file   [man]
            8. # cp dir/* .   copy all files of a directory within the current work directory   [man]
            9. # cp -a /tmp/dir1 .    copy a directory within the current work directory   [man]
            10. # cp -a dir1 dir2    copy a directory   [man]
            11. # cp file file1         outputs the mime type of the file as text   [man]
            12. # iconv -l   lists known encodings   [man]
            13. # iconv -f fromEncoding -t toEncoding inputFile > outputFile   converting the coding of characters from one format to another   [man]
            14. # find . -maxdepth 1 -name *.jpg -print -exec convert     batch resize files in the current directory and send them to a thumbnails directory (requires convert from Imagemagick)   [man]
            15. # ln -s file1 lnk1    create a symbolic link to file or directory   [man]
            16. # ln file1 lnk1        create a physical link to file or directory   [man]
            17. # ls             view files of directory   [man]
            18. # ls -F        view files of directory   [man]
            19. # ls -l         show details of files and directory   [man]
            20. # ls -a         show hidden files   [man]
            21. # ls *[0-9]*            show files and directory containing numbers   [man]
            22. # lstree       show files and directories in a tree starting from root(2)   [man]
            23. # mkdir dir1          create a directory called 'dir1'   [man]
            24. # mkdir dir1 dir2   create two directories simultaneously   [man]
            25. # mkdir -p /tmp/dir1/dir2 create a directory tree   [man]
            26. # mv dir1 new_dir            rename / move a file or directory   [man]
            27. # pwd        show the path of work directory   [man]
            28. # rm -f file1           delete file called 'file1'   [man]
            29. # rm -rf dir1          remove a directory called 'dir1' and contents recursively   [man]
            30. # rm -rf dir1 dir2   remove two directories and their contents recursively   [man]
            31. # rmdir dir1           delete directory called 'dir1'   [man]
            32. # touch -t 0712250000 file1         modify timestamp of a file or directory - (YYMMDDhhmm)   [man]
            33. # tree        show files and directories in a tree starting from root(1)   [man] 

18. File search:
            i. # find / -name file1        search file and directory into root filesystem from '/'   [man]
            ii.# find / -user user1   search files and directories belonging to 'user1'   [man]
            iii.# find /home/user1 -name \*.bin     search files with '. bin' extension within directory '/ home/user1'   [man]
            iv. # find /usr/bin -type f -atime +100             search binary files are not used in the last 100 days   [man]
            v. # find /usr/bin -type f -mtime -10   search files created or changed within 10 days   [man]
            vi. # find / -name *.rpm -exec chmod 755 '{}' \;        search files with '.rpm' extension and modify permits   [man]
            vii. # find / -xdev -name \*.rpm          search files with '.rpm' extension ignoring removable partitions as cdrom, pen-drive, etc.…   [man]
            viii. # locate \*.ps        find files with the '.ps' extension - first run 'updatedb' command   [man]
            ix. # whereis halt         show location of a binary file, source or man   [man]
            x. # which halt            show full path to a binary / executable   [man]
19. Mounting a Filesystem:
            a. # fuser -km /mnt/hda2          force umount when the device is busy   [man]
            b. # mount /dev/hda2 /mnt/hda2         mount disk called hda2 - verify existence of the directory '/ mnt/hda2'   [man]
            c. # mount /dev/fd0 /mnt/floppy         mount a floppy disk   [man]
            d. # mount /dev/cdrom /mnt/cdrom    mount a cdrom / dvdrom   [man]
            e. # mount /dev/hdc /mnt/cdrecorder mount a cdrw / dvdrom   [man]
            f. # mount /dev/hdb /mnt/cdrecorder mount a cdrw / dvdrom   [man]
            g. # mount -o loop file.iso /mnt/cdrom           mount a file or iso image   [man]
            h. # mount -t vfat /dev/hda5 /mnt/hda5          mount a Windows FAT32 file system   [man]
            i. # mount /dev/sda1 /mnt/usbdisk      mount a usb pen-drive or flash-drive   [man]
            j. # mount -t smbfs -o username=user,password=pass //WinClient/share /mnt/share             mount a windows network share   [man]
            k. # umount /dev/hda2            unmount disk called hda2 - exit from mount point '/ mnt/hda2' first   [man]
            l. # umount -n /mnt/hda2        run umount without writing the file /etc/mtab - useful when the file is read-only or the hard disk is full   [man] 

20. Disk space:
            a. # df -h          show list of partitions mounted   [man]
            b. # dpkg-query -W -f='${Installed-Size;10}t${Package}n' | sort -k1,1n     show the used space by installed deb packages, sorting by size (debian, ubuntu and alike)   [man]
            c. # du -sh dir1            estimate space used by directory 'dir1'   [man]
            d. # du -sk * | sort -rn show size of the files and directories sorted by size   [man]
            e. # ls -lSr |more          show size of the files and directories ordered by size   [man]
            f. # rpm -q -a --qf '%10{SIZE}t%{NAME}n' | sort -k1,1n   show the used space by rpm packages installed sorted by size (fedora, redhat and alike)   [man] 


20. Users and Groups:
            a. # chage -E 2005-12-31 user1           set deadline for user password   [man]
            b. # groupadd [group]             create a new group   [man]
            c. # groupdel [group] delete a group   [man]
            d. # groupmod -n moon sun    rename a group from moon to sun   [man]
            e. # grpck        check correct syntax and file format of '/etc/group' and groups existence   [man]
            f. # newgrp - [group]   log into a new group to change default group of newly created files   [man]
            g. # passwd     change password   [man]
            h. # passwd user1        change a user password (only by root)   [man]
            i. # pwck         check correct syntax and file format of '/etc/passwd' and users existence   [man]
            j. # useradd -c "User Linux" -g admin -d /home/user1 -s /bin/bash user1       create a new user "user1" belongs "admin" group   [man]
            k. # useradd user1       create a new user   [man]
            l. # userdel -r user1      delete a user ( '-r' eliminates home directory)   [man]
            m. # usermod -c "User FTP" -g system -d /ftp/user1 -s /bin/nologin user1     change user attributes as description, group and other   [man] 


21. Permits on Files:
a. # chgrp group1 file1             change group of files   [man]
b. # chmod ugo+rwx directory1   set permissions reading (r), write (w) and (x) access to users owner (u) group (g) and others (o)   [man]
c. # chmod go-rwx directory1     remove permits reading (r), write (w) and (x) access to users group (g) and others (or   [man]
d. # chmod u+s /bin/file1        set SUID bit on a binary file - the user that running that file gets same privileges as owner   [man]
e. # chmod u-s /bin/file1         disable SUID bit on a binary file   [man]
f. # chmod g+s /home/public set SGID bit on a directory - similar to SUID but for directory   [man]
g. # chmod g-s /home/public   disable SGID bit on a directory   [man]
h. # chmod o+t /home/public set STIKY bit on a directory - allows files deletion only to legitimate owners   [man]
i. # chmod o-t /home/public    disable STIKY bit on a directory   [man]
j. # chown user1 file1 change owner of a file   [man]
k. # chown -R user1 directory1          change user owner of a directory and all the files and directories contained inside   [man]
l. # chown user1:group1 file1             change user and group ownership of a file   [man]
m. # find / -perm -u+s             view all files on the system with SUID configured   [man]
n. # ls -lh         show permits on files   [man]
o. # ls /tmp | pr -T5 -W$COLUMNS divide terminal into 5 columns   [man]

22. Special Attributes on files:
# chattr +a file1            allows write opening of a file only append mode   [man]
# chattr +c file1           allows that a file is compressed / decompressed automatically by the kernel   [man]
# chattr +d file1          makes sure that the program ignores Dump the files during backup   [man]
# chattr +i file1           makes it an immutable file, which can not be removed, altered, renamed or linked   [man]
# chattr +s file1           allows a file to be deleted safely   [man]
# chattr +S file1          makes sure that if a file is modified changes are written in synchronous mode as with sync   [man]
# chattr +u file1          allows you to recover the contents of a file even if it is canceled   [man]
# lsattr             show specials attributes   [man]

23. Archives and compressed files
# bunzip2 file1.bz2      decompress a file called 'file1.bz2'   [man]
# bzip2 file1    compress a file called 'file1'   [man]
# gunzip file1.gz         decompress a file called 'file1.gz'   [man]
# gzip file1      compress a file called 'file1'   [man]
# gzip -9 file1 compress with maximum compression   [man]
# rar a file1.rar test_file           create an archive rar called 'file1.rar'   [man]
# rar a file1.rar file1 file2 dir1             compress 'file1', 'file2' and 'dir1' simultaneously   [man]
# rar x file1.rar          decompress rar archive   [man]
# tar -cvf archive.tar file1     create a uncompressed tarball   [man]
# tar -cvf archive.tar file1 file2 dir1 create an archive containing 'file1', 'file2' and 'dir1'   [man]
# tar -tf archive.tar      show contents of an archive   [man]
# tar -xvf archive.tar   extract a tarball   [man]
# tar -xvf archive.tar -C /tmp extract a tarball into / tmp   [man]
# tar -cvfj archive.tar.bz2 dir1            create a tarball compressed into bzip2   [man]
# tar -xvfj archive.tar.bz2        decompress a compressed tar archive in bzip2   [man]
# tar -cvfz archive.tar.gz dir1 create a tarball compressed into gzip   [man]
# tar -xvfz archive.tar.gz         decompress a compressed tar archive in gzip   [man]
# unrar x file1.rar        decompress rar archive   [man]
# unzip file1.zip          decompress a zip archive   [man]
# zip file1.zip file1      create an archive compressed in zip   [man]
# zip -r file1.zip file1 file2 dir1           compress in zip several files and directories simultaneously   [man]

24. RPM Packages ( Fedora, Red Hat and like):
# rpm -ivh [package.rpm]         install a rpm package   [man]
# rpm -ivh --nodeeps [package.rpm]   install a rpm package ignoring dependencies requests   [man]
# rpm -U [package.rpm]          upgrade a rpm package without changing configuration files   [man]
# rpm -F [package.rpm]           upgrade a rpm package only if it is already installed   [man]
# rpm -e [package]      remove a rpm package   [man]
# rpm -qa         show all rpm packages installed on the system   [man]
# rpm -qa | grep httpd             show all rpm packages with the name "httpd"   [man]
# rpm -qi [package]     obtain information on a specific package installed   [man]
# rpm -qg "System Environment/Daemons"   show rpm packages of a group software   [man]
# rpm -ql [package]     show list of files provided by a rpm package installed   [man]
# rpm -qc [package]    show list of configuration files provided by a rpm package installed   [man]
# rpm -q [package] --whatrequires      show list of dependencies required for a rpm packet   [man]
# rpm -q [package] --whatprovides     show capability provided by a rpm package   [man]
# rpm -q [package] --scripts    show scripts started during installation / removal   [man]
# rpm -q [package] --changelog          show history of revisions of a rpm package   [man]
# rpm -qf /etc/httpd/conf/httpd.conf verify which rpm package belongs to a given file   [man]
# rpm -qp [package.rpm] -l      show list of files provided by a rpm package not yet installed   [man]
# rpm --import /media/cdrom/RPM-GPG-KEY         import public-key digital signature   [man]
# rpm --checksig [package.rpm]          verify the integrity of a rpm package   [man]
# rpm -qa gpg-pubkey             verify integrity of all rpm packages installed   [man]
# rpm -V [package]     check file size, permissions, type, owner, group, MD5 checksum and last modification   [man]
# rpm -Va        check all rpm packages installed on the system - use with caution   [man]
# rpm -Vp [package.rpm]        verify a rpm package not yet installed   [man]
# rpm -ivh /usr/src/redhat/RPMS/`arch`/[package.rpm]          install a package built from a rpm source   [man]
# rpm2cpio [package.rpm] | cpio --extract --make-directories *bin* extract executable file from a rpm package   [man]
# rpmbuild --rebuild [package.src.rpm]           build a rpm package from a rpm source   [man]


25. YUM packages tool (Fedora, RedHat and alike):
# yum -y install [package]        download and install a rpm package   [man]
# yum localinstall [package.rpm]        That will install an RPM, and try to resolve all the dependencies for you using your repositories.   [man]
# yum -y update          update all rpm packages installed on the system   [man]
# yum update [package]          upgrade a rpm package   [man]
# yum remove [package]         remove a rpm package   [man]
# yum list        list all packages installed on the system   [man]
# yum search [package]           find a package on rpm repository   [man]
# yum clean [package]            clean up rpm cache erasing downloaded packages   [man]
# yum clean headers    remove all files headers that the system uses to resolve dependency   [man]
# yum clean all            remove from the cache packages and headers files   [man]

26. DEB packages (Debian, Ubuntu and like):

# dpkg -i [package.deb]           install / upgrade a deb package   [man]
# dpkg -r [package]     remove a deb package from the system   [man]
# dpkg -l          show all deb packages installed on the system   [man]
# dpkg -l | grep httpd show all deb packages with the name "httpd"   [man]
# dpkg -s [package]     obtain information on a specific package installed on system   [man]
# dpkg -L [package]    show list of files provided by a package installed on system   [man]
# dpkg --contents [package.deb]        show list of files provided by a package not yet installed   [man]
# dpkg -S /bin/ping     verify which package belongs to a given file   [man]

27. APT packages tool (Debian, Ubuntu and alike):
# apt-cache search [package]               returns list of packages which corresponds string "searched-packages"   [man]
# apt-cdrom install [package] install / upgrade a deb package from cdrom   [man]
# apt-get install [package]       install / upgrade a deb package   [man]
# apt-get update          update the package list   [man]
# apt-get upgrade        upgrade all of the installed packages   [man]
# apt-get remove [package]     remove a deb package from system   [man]
# apt-get check            verify correct resolution of dependencies   [man]
# apt-get clean             clean up cache from packages downloaded   [man]

28. View file content:
# cat file1         view the contents of a file starting from the first row   [man]
# head -2 file1             view first two lines of a file   [man]
# less file1       similar to 'more' command but which allows backward movement in the file as well as forward movement   [man]
# more file1     view content of a file along   [man]
# tac file1        view the contents of a file starting from the last line   [man]
# tail -2 file1    view last two lines of a file   [man]
# tail -f /var/log/messages        view in real time what is added to a file   [man]

29. Text Manipulation:
# cat example.txt | awk 'NR%2==1'   remove all even lines from example.txt   [man]
# echo a b c | awk '{print $1}'             view the first column of a line   [man]
# echo a b c | awk '{print $1,$3}'        view the first and third column of a line   [man]
# cat -n file1   number row of a file   [man]
# comm -1 file1 file2   compare contents of two files by deleting only unique lines from 'file1'   [man]
# comm -2 file1 file2   compare contents of two files by deleting only unique lines from 'file2'   [man]
# comm -3 file1 file2   compare contents of two files by deleting only the lines that appear on both files   [man]
# diff file1 file2          find differences between two files   [man]
# grep Aug /var/log/messages         look up words "Aug" on file '/var/log/messages'   [man]
# grep ^Aug /var/log/messages   look up words that begin with "Aug" on file '/var/log/messages'   [man]
# grep [0-9] /var/log/messages          select from file '/var/log/messages' all lines that contain numbers   [man]
# grep Aug -R /var/log/*         search string "Aug" at directory '/var/log' and below   [man]
# paste file1 file2        merging contents of two files for columns   [man]
# paste -d '+' file1 file2           merging contents of two files for columns with '+' delimiter on the center   [man]
# sdiff file1 file2         find differences between two files and merge interactively alike "diff"   [man]
# sed 's/string1/string2/g' example.txt             replace "string1" with "string2" in example.txt   [man]
# sed '/^$/d' example.txt         remove all blank lines from example.txt   [man]
# sed '/ *#/d; /^$/d' example.txt          remove comments and blank lines from example.txt   [man]
# sed -e '1d' exampe.txt          eliminates the first line from file example.txt   [man]
# sed -n '/string1/p'      view only lines that contain the word "string1"   [man]
# sed -e 's/ *$//' example.txt    remove empty characters at the end of each row   [man]
# sed -e 's/string1//g' example.txt        remove only the word "string1" from text and leave intact all   [man]
# sed -n '1,5p' example.txt      print from 1th to 5th row of example.txt   [man]
# sed -n '5p;5q' example.txt    print row number 5 of example.txt   [man]
# sed -e 's/00*/0/g' example.txt           replace more zeros with a single zero   [man]
# sort file1 file2           sort contents of two files   [man]
# sort file1 file2 | uniq             sort contents of two files omitting lines repeated   [man]
# sort file1 file2 | uniq -u         sort contents of two files by viewing only unique line   [man]
# sort file1 file2 | uniq -d        sort contents of two files by viewing only duplicate line   [man]
# echo 'word' | tr '[:lower:]' '[:upper:]'             convert from lower case in upper case   [man]

30. Character set and Format file conversion:
# dos2unix filedos.txt fileunix.txt       convert a text file format from MSDOS to UNIX   [man]
# recode ..HTML < page.txt > page.html       convert a text file to html   [man]
# recode -l | more        show all available formats conversion   [man]
# unix2dos fileunix.txt filedos.txt      convert a text file format from UNIX to MSDOS   [man]

31. Filesystem Analysis:
# badblocks -v /dev/hda1         check bad blocks on disk hda1   [man]
# dosfsck /dev/hda1    repair / check integrity of dos filesystems on disk hda1   [man]
# e2fsck /dev/hda1      repair / check integrity of ext2 filesystem on disk hda1   [man]
# e2fsck -j /dev/hda1 repair / check integrity of ext3 filesystem on disk hda1   [man]
# fsck /dev/hda1         repair / check integrity of linux filesystem on disk hda1   [man]
# fsck.ext2 /dev/hda1             repair / check integrity of ext2 filesystem on disk hda1   [man]
# fsck.ext3 /dev/hda1             repair / check integrity of ext3 filesystem on disk hda1   [man]
# fsck.vfat /dev/hda1 repair / check integrity of fat filesystem on disk hda1   [man]
# fsck.msdos /dev/hda1          repair / check integrity of dos filesystem on disk hda1   [man]

32. Format a Filesystem:
# fdformat -n /dev/fd0             format a floppy disk   [man]
# mke2fs /dev/hda1    create a filesystem type linux ext2 on hda1 partition   [man]
# mke2fs -j /dev/hda1             create a filesystem type linux ext3 (journal) on hda1 partition   [man]
# mkfs /dev/hda1        create a filesystem type linux on hda1 partition   [man]
# mkfs -t vfat 32 -F /dev/hda1            create a FAT32 filesystem   [man]
# mkswap /dev/hda3   create a swap filesystem   [man]

33. Filesystem SWAP:
# mkswap /dev/hda3   create a swap filesystem   [man]
# swapon /dev/hda3    activating a new swap partition   [man]
# swapon /dev/hda2 /dev/hdb3           activate two swap partitions   [man]

34. Backup:
# find /var/log -name '*.log' | tar cv --files-from=- | bzip2 > log.tar.bz2          find all files with '.log' extention and make an bzip archive   [man]
# find /home/user1 -name '*.txt' | xargs cp -av --target-directory=/home/backup/ --parents find and copy all files with '.txt' extention from a directory to another   [man]
# dd bs=1M if=/dev/hda | gzip | ssh user@ip_addr 'dd of=hda.gz'   make a backup of a local hard disk on remote host via ssh   [man]
# dd if=/dev/sda of=/tmp/file1           backup content of the harddrive to a file   [man]
# dd if=/dev/hda of=/dev/fd0 bs=512 count=1         make a copy of MBR (Master Boot Record) to floppy   [man]
# dd if=/dev/fd0 of=/dev/hda bs=512 count=1         restore MBR from backup copy saved to floppy   [man]
# dump -0aj -f /tmp/home0.bak /home            make a full backup of directory '/home'   [man]
# dump -1aj -f /tmp/home0.bak /home            make a incremental backup of directory '/home'   [man]
# restore -if /tmp/home0.bak   restoring a backup interactively   [man]
# rsync -rogpav --delete /home /tmp   synchronization between directories   [man]
# rsync -rogpav -e ssh --delete /home ip_address:/tmp           rsync via SSH tunnel   [man]
# rsync -az -e ssh --delete ip_addr:/home/public /home/local             synchronize a local directory with a remote directory via ssh and compression   [man]
# rsync -az -e ssh --delete /home/local ip_addr:/home/public             synchronize a remote directory with a local directory via ssh and compression   [man]
# tar -Puf backup.tar /home/user         make a incremental backup of directory '/home/user'   [man]
# ( cd /tmp/local/ && tar c . ) | ssh -C user@ip_addr 'cd /home/share/ && tar x -p' copy content of a directory on remote directory via ssh   [man]
# ( tar c /home ) | ssh -C user@ip_addr 'cd /home/backup-home && tar x -p'           copy a local directory on remote directory via ssh   [man]
# tar cf - . | (cd /tmp/backup ; tar xf - )           local copy preserving permits and links from a directory to another   [man]

35. CDROM:
# cd-paranoia -B          rip audio tracks from a CD to wav files   [man]
# cd-paranoia --           rip first three audio tracks from a CD to wav files   [man]
# cdrecord -v gracetime=2 dev=/dev/cdrom -eject blank=fast -force            clean a rewritable cdrom   [man]
# cdrecord -v dev=/dev/cdrom cd.iso             burn an ISO image   [man]
# gzip -dc cd_iso.gz | cdrecord dev=/dev/cdrom -     burn a compressed ISO image   [man]
# cdrecord --scanbus   scan bus to identify the channel scsi   [man]
# dd if=/dev/hdc | md5sum    perform an md5sum on a device, like a CD   [man]
# mkisofs /dev/cdrom > cd.iso            create an iso image of cdrom on disk   [man]
# mkisofs /dev/cdrom | gzip > cd_iso.gz        create a compressed iso image of cdrom on disk   [man]
# mkisofs -J -allow-leading-dots -R -V          create an iso image of a directory   [man]
# mount -o loop cd.iso /mnt/iso          mount an ISO image   [man]

36. Networking (LAN / WiFi):
# dhclient eth0             active interface 'eth0' in dhcp mode   [man]
# ethtool eth0 show network statistics of eth0   [man]
# host www.example.com      lookup hostname to resolve name to ip address and viceversa   [man]
# hostname      show hostname of system   [man]
# ifconfig eth0            show configuration of an ethernet network card   [man]
# ifconfig eth0 192.168.1.1 netmask 255.255.255.0 configure IP Address   [man]
# ifconfig eth0 promisc           configure 'eth0' in promiscuous mode to gather packets (sniffing)   [man]
# ifdown eth0             disable an interface 'eth0'   [man]
# ifup eth0       activate an interface 'eth0'   [man]
# ip link show show link status of all network interfaces   [man]
# iwconfig eth1           show wireless networks   [man]
# iwlist scan    wifi scanning to display the wireless connections available   [man]
# mii-tool eth0             show link status of 'eth0'   [man]
# netstat -tup   show all active network connections and their PID   [man]
# netstat -tupl show all network services listening on the system and their PID   [man]
# netstat -rn     show routing table alike "route -n"   [man]
# nslookup www.example.com           lookup hostname to resolve name to ip address and viceversa   [man]
# route -n         show routing table   [man]
# route add -net 0/0 gw IP_Gateway             configure default gateway   [man]
# route add -net 192.168.0.0 netmask 255.255.0.0 gw 192.168.1.1 configure static route to reach network '192.168.0.0/16'   [man]
# route del 0/0 gw IP_gateway           remove static route   [man]
# echo "1" > /proc/sys/net/ipv4/ip_forward    activate ip routing temporarily   [man]
# tcpdump tcp port 80            show all HTTP traffic   [man]
# whois www.example.com    lookup on Whois database   [man]

37. Microsoft Windows networks (samba)
# mount -t smbfs -o username=user,password=pass //WinClient/share /mnt/share     mount a windows network share   [man]
# nbtscan ip_addr       netbios name resolution   [man]
# nmblookup -A ip_addr        netbios name resolution   [man]
# smbclient -L ip_addr/hostname       show remote shares of a windows host   [man]
# smbget -Rr smb://ip_addr/share       like wget can download files from a host windows via smb   [man]

38. IPTABLES (firewall):
# iptables -t filter -L     show all chains of filtering table   [man]
# iptables -t nat -L       show all chains of nat table   [man]
# iptables -t filter -F    clear all rules from filtering table   [man]
# iptables -t nat -F       clear all rules from table nat   [man]
# iptables -t filter -X   delete any chains created by user   [man]
# iptables -t filter -A INPUT -p tcp --dport telnet -j ACCEPT          allow telnet connections to input   [man]
# iptables -t filter -A OUTPUT -p tcp --dport http -j DROP             block HTTP connections to output   [man]
# iptables -t filter -A FORWARD -p tcp --dport pop3 -j ACCEPT   allow POP3 connections to forward chain   [man]
# iptables -t filter -A INPUT -j LOG --log-prefix      Logging on input chain   [man]
# iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE    configure a PAT (Port Address Traslation) on eth0 masking outbound packets   [man]
# iptables -t nat -A PREROUTING -d 192.168.0.1 -p tcp -m tcp --dport 22 -j DNAT --to-destination 10.0.0.2:22             redirect packets addressed to a host to another host   [man]

39. Monitoring and debugging:
# free -m           displays status of RAM in megabytes   [man]
# kill -9 process_id      force closure of the process and finish it   [man]
# kill -1 process_id      force a process to reload configuration   [man]
# last reboot    show history reboot   [man]
# lsmod           display kernel loaded   [man]
# lsof -p process_id     display a list of files opened by processes   [man]
# lsof /home/user1       displays a list of open files in a given path system   [man]
# ps -eafw       displays linux tasks   [man]
# ps -e -o pid,args --forest       displays linux tasks in a hierarchical mode   [man]
# pstree            Shows a tree system processes   [man]
# smartctl -A /dev/hda            monitoring reliability of a hard-disk through SMART   [man]
# smartctl -i /dev/hda check if SMART is active on a hard-disk   [man]
# strace -c ls >/dev/null           display system calls made and received by a process   [man]
# strace -f -e open ls >/dev/null           display library calls   [man]
# tail /var/log/dmesg    show events inherent to the process of booting kernel   [man]
# tail /var/log/messages           show system events   [man]
# top    display linux tasks using most cpu   [man]
# watch -n1 'cat /proc/interrupts'         display interrupts in real-time   [man]

40. Others useful commands:
# alias hh='history'       set an alias for a command - hh = history   [man]
# apropos ...keyword display a list of commands that pertain to keywords of a program , useful when you know what your program does, but you don't know the name of the command   [man]
# chsh change shell command   [man]
# chsh --list-shells        nice command to know if you have to remote into another box   [man]
# gpg -c file1   encrypt a file with GNU Privacy Guard   [man]
# gpg file1.gpg            decrypt a file with GNU Privacy Guard   [man]
# ldd /usr/bin/ssh         show shared libraries required by ssh program   [man]
# man ping      display the on-line manual pages for example on ping command - use '-k' option to find any related commands   [man]
# mkbootdisk --device /dev/fd0 `uname -r`    create a boot floppy   [man]
# wget -r www.example.com download an entire web site   [man]
# wget -c www.example.com/file.iso             download a file with the ability to stop the download and resume later   [man]
# echo 'wget -c www.example.com/files.iso' | at 09:00          start a download at any given time   [man]
# whatis ...keyword    displays description of what a program does   [man]
# who -a          show who is logged on, and print: time of last system boot, dead processes, system login processes, active processes spawned by init, current runlevel, last system clock change   [man]

Linux has 7 different run levels (or operating modes):
  • rc0.d - System Halted
  • rc1.d - Single User Mode
  • rc2.d - Single User Mode with Networking
  • rc3.d - Multi-User Mode - boot up in text mode
  • rc4.d - Not yet Defined
  • rc5.d - Multi-User Mode - boot up in X Windows
  • rc6.d - Shutdown & Reboot