ManyCodes.com – codes & scripts Get free programming codes and tutorials!

11Jan/120

How to create a symlink in the Linux command console?

 ln -s <destination> <linkname> 

If the desired link filename is the same as the destination's filename, and the current working directory is the desired location for the link, then you only need:

  ln -s <destination> 

A symbolic link (also symlink or soft link) is a special type of file that contains a reference to another file or directory in the form of an absolute or relative path and that affects pathname resolution. Symbolic links were already present by 1978 in mini-computer operating systems from DEC and Data General's RDOS.

Today they are supported by the POSIX operating-system standard, most Unix-like operating systems such as FreeBSD, GNU/Linux, and Mac OS X, and also Windows operating systems such as Windows Vista, Windows 7 and to some degree in Windows 2000 and Windows XP.Symbolic links operate transparently for most operations: programs which read or write to files named by a symbolic link will behave as if operating directly on the target file. However, programs that need to handle symbolic links specially (e.g., backup utilities) may identify and manipulate them directly.

6Jun/111

How to Open rar file or Extract rar files under Linux or UNIX

RAR files are in compressed archive format, if you have downloaded rar files from the Internet, you need to unpack or unrar them (extract rar files).RAR is a proprietary file format for data compression and archiving, developed by Eugene Roshal.Under Linux and UNIX, use command called unrar. By default unrar is not being installed on Linux, FreeBSD or UNIX oses. You can install unrar command with the help of apt-get or yum command.

Install unrar command

Under Debian Linux, you need to type apt-get as follows to install unrar program:
# apt-get install unrar

If you are using Fedora core Linux then use yum command as follows (see discussion below):
# yum install unrar

If you are using FreeBSD, use:
# pkg_add -v -r unrar

If any of above, methods is not working for you, download binary package from official rarlab site:
$ cd /tmp
$ wget http://www.rarlab.com/rar/rarlinux-3.6.0.tar.gz

Untar file
$ tar -zxvf rarlinux-3.6.0.tar.gz

Both unrar and rar commands are located in rar sub-directory. Just go to rar directory:
$ cd rar
$ ./unrar

Now copy rar and unrar to /bin directory:
# cp rar unrar /bin

How to use unrar

unrar command supports various options below are common options that you need to use everyday.

Task: To open rar (unpack) file in current directory type command:

$ unrar e file.rar

Please note that replace file.rar filename with your actual filename.

Task: List (l) file inside rar archive:

$ unrar l file.rar

Task: To extract (x) files with full path type command:

$ unrar x file.rar

(D) To test (t) integrity of archive, file type command:
$ unrar t file.rar

23May/110

How to Copy, move, rename, and remove files in Linux

< Copying >

To copy files, you use the cp command. The following will copy file to file2. Note that if file2 doesn't exist, it'll be created, but if it exists, it'll be overwritten:
$ cp file file2

There aren't any undo commands in the Linux CLI, so accidentally overwriting an important file would probably make you pull your head off. The risk of doing so is smaller if you use the -i option ("interactive") with cp. The following does the same as the above, but if file2 exists, you'll be prompted before overwriting:

$ cp -i file file2
cp: overwrite `file2'? n
$

So it's a good idea to use the -i option whenever you're dealing with important files you don't want to lose!

If you want to copy file into directory dir1:
$ cp file dir1

The following would do the same as the above, copy file into dir1, but under a different name:
$ cp file dir1/file2

You can also copy multiple files into one directory with a single command:
$ cp file1 file2 file3 dir1

Note that if the last argument isn't a directory name, you'll get an error message complaining about it.

 

< Moving and renaming >

The mv command can be used for moving or renaming files. To rename a file, you can use it like this:
$ mv file file2

If file2 doesn't exist, it'll be created, but if it exists, it'll be overwritten. If you want to be prompted before overwriting files, you can use the -i option the same way as with cp:

$ mv -i file file2
mv: overwrite `file2'? y
$

To move the file into another directory:
$ mv file dir1

If you want to rename the file to file2 and move it into another directory, you probably already figured out the command:
$ mv file dir1/file2

 

< Removing files >

The rm command is used for removing files and directories. To remove a file:
$ rm file

If you use the -i option, you'll be prompted before removing the file:
$ rm -i file

You can also delete more files at once:
rm file1 file2

Be careful with the rm command! As I already told you, Linux doesn't have any undo commands, and it doesn't put files into Trash where you can save them later. Once you've deleted a file.

Ubuntu, fedora, centos, linux

25Aug/090

How to determine which sevices are enabled at boot time in linux

How do I find out which services are enabled at Boot under Ubuntu/CentOS/RHEL/Fedora Linux? How can I disable a service which is not needed or I dont want to run every time the linux machine starts?

Open terminal and login as root user.

Type the following command to list all services which are enabled at boot:

#chkconfig --list | grep $(runlevel  | awk '{ print $2}'):on

Sample output:

acpid          	0:off	1:off	2:off	3:on	4:on	5:on	6:off
anacron        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
atd            	0:off	1:off	2:off	3:on	4:on	5:on	6:off
auditd         	0:off	1:off	2:on	3:on	4:on	5:on	6:off
cpuspeed       	0:off	1:on	2:on	3:on	4:on	5:on	6:off
crond          	0:off	1:off	2:on	3:on	4:on	5:on	6:off
dkms_autoinstaller	0:off	1:off	2:on	3:on	4:on	5:on	6:off
haldaemon      	0:off	1:off	2:off	3:on	4:on	5:on	6:off
hidd           	0:off	1:off	2:on	3:on	4:on	5:on	6:off
irqbalance     	0:off	1:off	2:on	3:on	4:on	5:on	6:off
kudzu          	0:off	1:off	2:off	3:on	4:on	5:on	6:off
lighttpd       	0:off	1:off	2:on	3:on	4:on	5:on	6:off
lm_sensors     	0:off	1:off	2:on	3:on	4:on	5:on	6:off
lvm2-monitor   	0:off	1:on	2:on	3:on	4:on	5:on	6:off
mcstrans       	0:off	1:off	2:on	3:on	4:on	5:on	6:off
mdmonitor      	0:off	1:off	2:on	3:on	4:on	5:on	6:off
messagebus     	0:off	1:off	2:off	3:on	4:on	5:on	6:off
microcode_ctl  	0:off	1:off	2:on	3:on	4:on	5:on	6:off
mysqld         	0:off	1:off	2:on	3:on	4:on	5:on	6:off
named          	0:off	1:off	2:on	3:on	4:on	5:on	6:off
netfs          	0:off	1:off	2:off	3:on	4:on	5:on	6:off
network        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
ntpd           	0:off	1:off	2:on	3:on	4:on	5:on	6:off
pcscd          	0:off	1:off	2:on	3:on	4:on	5:on	6:off
psacct         	0:off	1:off	2:on	3:on	4:on	5:on	6:off
readahead_early	0:off	1:off	2:on	3:on	4:on	5:on	6:off
restorecond    	0:off	1:off	2:on	3:on	4:on	5:on	6:off
rhnsd          	0:off	1:off	2:on	3:on	4:on	5:on	6:off
rpcgssd        	0:off	1:off	2:off	3:on	4:on	5:on	6:off
rpcidmapd      	0:off	1:off	2:off	3:on	4:on	5:on	6:off
sendmail       	0:off	1:off	2:on	3:on	4:on	5:on	6:off
setroubleshoot 	0:off	1:off	2:off	3:on	4:on	5:on	6:off
smartd         	0:off	1:off	2:on	3:on	4:on	5:on	6:off
snmpd          	0:off	1:off	2:on	3:on	4:on	5:on	6:off
xfs            	0:off	1:off	2:on	3:on	4:on	5:on	6:off
xinetd         	0:off	1:off	2:off	3:on	4:on	5:on	6:off
yum-updatesd   	0:off	1:off	2:on	3:on	4:on	5:on	6:off

The first column of above output is the name of a service which is currently enabled at boot. You need to review each service.

Task: Disable services

To stop service, enter:

# service {service-name} stop
 # service vmware stop

To disable service, enter:

# chkconfig {service-name} off
 # chkconfig vmware off

You may also use ntsysv command to manage all services.

A note about outdated insecure service

All of the following services must be disabled to improve server security:

  1. Inetd and Xinetd (inetd xinetd) - Use direct services configured via SysV and daemons.
  2. Telnet (telnet-server) - Use ssh
  3. Rlogin, Rsh, and Rcp ( rsh-server ) - Use ssh and scp.
  4. NIS (ypserv) : Use OpenLDAP or Fedora directory server.
  5. TFTP (tftp-server) : Use SFTP or SSH.

To delete all of the service enter:

# yum erase inetd xinetd ypserv tftp-server telnet-server rsh-serve
25Aug/090

How to view or display a logfile in real time on screen in linux

How do I see the log file in real time including all incoming logs as well?

You can use the tail command in linux command line which outputs the last part of files in real time including all incoming logs to a file. So you can view the last parts of your logs file (like access logs for the server) using this in real time!

Note: you may need to login as root user to view log files.

Command

tail -f file-name command

If your log file name is /var/log/lighttpd/access.log, enter:

tail -f /var/log/lighttpd/access.log

If your php log file name is /var/log/lighttpd/scripts.log, enter

tail -f /var/log/lighttpd/scripts.log

You will get a scrolling view of the /var/log/lighttpd/scripts.log for all incoming entries on screen. To stop simply hit CTRL+C.

21Aug/093

Ubuntu / Debian Linux: Install Monit Linux Server Monitoring Utility

How do I install monit to monitor my server under Debian / Ubuntu Linux?

Monit is a utility for managing and monitoring processes, files, directories and devices on a Debian / Ubuntu Linux server system. Here a few common uses of monit:

  1. Monit can start a process if it does not run
  2. Restart a process if it does not respond
  3. Stop a process if it uses to much resources etc

How do I install monit utility for monitoring services?

Type the following command as the root user:

$ sudo apt-get update
 $ sudo apt-get install monit

Configure monit

Open monit configuration file /etc/monit/monitrc using vi text editor or nano command in linux:

# vi /etc/monit/monitrc

OR

# nano /etc/monit/monitrc

You need to set following parameters:

set daemon  120
 set logfile syslog facility log_daemon
 set mailserver localhost               # primary mailserver
 set alert vivek@nixcraft.com                       # receive all alerts

The next step is to save and close this file. Where,

  • set daemon 120 : Start monit in background as daemon and check the services at 2-minute intervals.
  • set logfile syslog facility log_daemon : Log messages in /var/log/messsages file
  • set mailserver localhost : Send email alert via localmail server such as sendmail. Set list of mailservers for alert delivery. Multiple servers may be specified using comma separator. By default monit uses port 25 - it is possible to override it with the PORT option.
  • set alert vivek@nixcraft.com : You can set the alert recipients here, which will receive the alert for each service. The event alerts may be restricted using the list.

Now open /etc/default/monit file to turn on monit service:

# vi /etc/default/monit

OR

# nano /etc/default/monit

Set startup to 1, so monit can start:

startup=1

Save and close the file.

Start the monit Linux monitor tool / service:

# /etc/init.d/monit start
10Aug/090

How to Stop/Restart Lighttpd Web Server in Debian / Ubuntu / FreeBSD linux

/etc/init.d/lighttpd is a script under Linux to stop / restart lighttpd web server.

To stop lighttpd:

Use the following command to stop lighttpd:

# /etc/init.d/lighttpd stop

To restart lighttpd:

Just type the following command to restart lighttpd:

# /etc/init.d/lighttpd restart

To start lighttpd:

# /etc/init.d/lighttpd start

Debian / Ubuntu Linux Start lighttpd

# /etc/init.d/lighttpd start

Debian / Ubuntu Linux - Stop lighttpd

# /etc/init.d/lighttpd stop

Debian / Ubuntu Linux - Restart lighttpd

# /etc/init.d/lighttpd restart

FreeBSD Start lighttpd web server

# /usr/local/etc/rc.d/lighttpd start

FreeBSD - Stop lighttpd webserver

# /usr/local/etc/rc.d/lighttpd stop

FreeBSD - Restart lighttpd webserver

# /usr/local/etc/rc.d/lighttpd restart

In case if you don't have init script, type the following:

# killall lighttpd
10Aug/090

Ubuntu Linux Start / Restart / Stop Apache 2.2 Web Server

Q. How to restart or stop Apache 2.2 web server under Ubuntu Linux?

Task: Start Apache 2.2 Server

# /etc/init.d/apache2 start

or

$ sudo /etc/init.d/apache2 start

Task: Restart Apache 2.2 Server

# /etc/init.d/apache2 restart

or

$ sudo /etc/init.d/apache2 restart

Task: Stop Apache 2.2 Server

# /etc/init.d/apache2 stop

or

$ sudo /etc/init.d/apache2 stop
10Aug/090

How to install webmin on your Linux web server (Redhat Fedora Caldera Mandrake SuSE MSC)

Installing webmin on your linux webserver is easy. With webmin, you can use ispconfig which is a web hosting script that you can use to host many website in one server.
Webmin website: http://www.webmin.com/

Since I have fedora I can install like this:
(this tutorial is suitable for Redhat, SuSE, Caldera, Mandrake or MSC Linux, 13M)

Login to your shell, i will be using ssh so i send this command:

wget http://prdownloads.sourceforge.net/webadmin/webmin-1.350-1.noarch.rpm

Once it has finished downloadin i send this command:

rpm -U webmin-1.350-1.noarch.rpm

(make sure to use upper case U above)

The rest of the install will be done automatically to the directory /usr/libexec/webmin, the administration username set to root and the password to your current root password. You should now be able to login to Webmin at the URL http://localhost:10000/ .

Webmin install complete. You can now login to https://hostname.domain:10000/
as root with your root password.

NOTE: the default login and password is your root and root password. this is the same login you used with you ssh to your server or whatever your root password is, so your login will be like this:

Username: root
Password: xxxx (what ever your root password is)

5Aug/090

Install istat On a CentOS Linux to Monitor Server from iPhone or Touch

The istat application is a monitoring application that can be used to obtain basic Linux server information from your iPhone or iPod Touch phone. This application has the ability to monitor CPU Usage, Memory Usage, Disk Usage, CPU Load, and Server  Uptime among many others.

istat: Linux Server Monitor From iPhone

To install istat on CentOS Linux follow the directions below.

  1. Download istat: Download istat for Linux here.
  2. Configure istat: Unpack the istat package you downloaded from Google Code and change the directory into the newly created istat directory. Once in this directory type the below command to configure istat for your server.
    1. ./configure
    ./configure
  3. Install istat: Now issue the make and make install commands to compile and install istat on your server.
    1. make
    2. make install
    make
    make install
  4. Create istat User: Now issue the below command to create a istat user to run istat.
    1. useradd istat
    useradd istat
  5. Create istat Directory: Use the below syntax to create a directory to store the istat.pid file which needs to be readable/writeable by the istat user you just created.
    1. mkdir /var/run/istat
    2. chown istat.istat /var/run/istat
    mkdir /var/run/istat
    chown istat.istat /var/run/istat
  6. Configure istat: Now make necessary configuration changes to the /usr/local/etc/istat.conf configuration file. The typical changes include modifying the server_code which is the access code used by your iDevice to obtain the server information, monitor_net which is the interface istat will listen for incoming connections, and monitor_disk which needs to be changed to the proper mounts such as /dev/hdb1, /dev/hdc1, etc.
  7. Launch istat: Now launch the istat daemon by issuing the below command.
    1. /usr/local/bin/istatd &
    /usr/local/bin/istatd &

    The server will automatically launch with the istat user and listen on port 5109.

  8. Connect From iPhone: Now configure the new server within istat on your iPod Touch or iPhone. Once you do so click on the server, type in the password you set in step 6, and verify you are collecting all of the server statistics properly.

The istat application is a nifty little application to provide you a quick view of servers on your network or servers that you monitor outside of your network. More information about the iPhone version of istat can be located here.