Setting static ip in Linux/Centos/RHEL/

1. login as root

[fedo@dhcppc6 ~]$ su root
Password:
[root@dhcppc6 fedo]#


2. Stop network manager

[root@dhcppc6 fedo]# service NetworkManager stop
Stopping NetworkManager daemon:                            [  OK  ]


Daemonizing Server Process in Linux

If we have the server it has to handle all the request and response without the help of 
user. So better running server process as background is good idea.

We can run the Linux process as foreground or background. A process is called daemon when it running in background
and doing necessary work.

Steps to make Server process as daemon:

1. Create child process and separate from parent - fork()

create child process using fork() system call, then exit parent process. Once parent exits new child process
will child of init process and behave as background process.

pid = fork()
if (pid < 0)
exit(1);

if (pid > 0)
exit(0);

/* child continue as daemon */


2. Make new child process independent - setsid()

When terminal receives signal, all the process connected to this terminal get signal. 
child process created through fork() system call will inherit process group of parent process. All the signal passed
to process group will passed to inherited process. 

daemon server should not get signal from where was started. 

We can make daemon process independent from parent using setsid() system call

setsid() /* get a new process group */

3. Handle standard I/O stream - getdtablesize(), open(), dup(), dup2()

fork system call will inherit all open file descriptor of parent. so close all the unnecessary open file 
descriptor.

for( fds=getdtablesize(); fds >=0; fd--)
close(fds);

standard libraries use standard stream for some work, better close all three standard ( stdin - 0, stdout - 1, stderr - 2)
for safety purpose redirect the out put to /dev/null.

i = open("/dev/null", O_RDWR);
dup(i);
dup(i);

for ease understanding read the man page of 'open', 'dup' & 'dup2'.

4. Change file creation mask - umask()

Most of the server run as root user mode(not mandatory). So it should protect the files it created when it run.
we can change the process privilege using umask system call.

umask(027);

This will restrict file creation mode to 750

5. Change working directory - chdir()

Server should be run from root directory (not mandatory), then only it can see all the files 
from system.

chdir("/");

"/" means root directory.

6. Run single copy of server

For a system it requires only one copy server at a time. File locking is a good method to achieve this by making
other process wait on the file lock. when process terminate lock on this file automatically released then server
which waiting on the lock will start work.

write the pid of the process to file, which will help to know the running pid of the server. 

lfp = open("lockfile.lock", O_RDWR|O_CREAT, 0650);
if (lfp < 0)
exit(1);
if (lockf(lfp, F_TLOCK, 0) < 0)
exit(1);
sprintf(strpid, "%d\n", getpid());
write(lfp, strpid, strlen(strpid));

7. Logging

When server is running some messages should be logged. This will help for developer to debug the server
if any problem arise.

void logging(char *filename, char* log)
{
FILE *fp;
fp = open(filename, "a");
if (fp == NULL) 
return;
fprintf(logfile, "%s\n", log);
fclose(fp);
}

/* log messages as below */
logging("logfile.txt", "This is server log");


Program:

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>

#define WORKING_DIR "/home/sujin/c"
#define LOCK_FILE "lockfile.lock"
#define LOG_FILE "logfile.log"

void make_daemon();
void logging(char *file, char *log);

void logging(char *file, char *log)
{
        FILE *fp;
        if ((fp = fopen(file, "a")) == NULL)
                return;
        fprintf(fp, "%s\n", log);
        fclose(fp);
}

void make_daemon()
{
        int pid, lockfd, i;
        char pidstr[16];

        pid = fork();
        if (pid < 0) {
                printf("fork error.\n");
                exit(1);
        }
        if (pid > 0)
                exit(0);

        setsid();

        for (i = getdtablesize(); i >= 0; i--)
                close(i);

        i = open("/dev/null", O_RDWR);
        dup(i);
        dup(i);

        umask(027);

        chdir(WORKING_DIR);

        lockfd = open(LOCK_FILE, O_RDWR|O_CREAT, 0640);
        if (lockfd < 0)
                exit(1);

        if (lockf(lockfd, F_TLOCK, 0) < 0)
                exit(1);

        sprintf(pidstr, "%d\n", getpid());
        write(lockfd, pidstr, strlen(pidstr));

}

int main(int argc, char *argv[])
{
        make_daemon();
        sleep(5);
        while(1)
        {
                /* Do server work */
                logging(LOG_FILE, "This is write from main server.\n");
        }

        return 0;
}


I am not full owner of this article. I grabbed some details from other articles. Full credits goes to author of Unix Daemon Server Programming


Linux Command To Rename The Multiple Files

[sujin@localhost blog]$ ls -l
total 0
-rw-rw-r--. 1 sujin sujin 0 Oct 19 13:12 centos6file1
-rw-rw-r--. 1 sujin sujin 0 Oct 19 13:12 centos6file2
-rw-rw-r--. 1 sujin sujin 0 Oct 19 13:12 centos6file3
-rw-rw-r--. 1 sujin sujin 0 Oct 19 13:12 centos6file4
-rw-rw-r--. 1 sujin sujin 0 Oct 19 13:12 centos6file5
-rw-rw-r--. 1 sujin sujin 0 Oct 19 13:14 centos6file6
-rw-rw-r--. 1 sujin sujin 0 Oct 19 13:14 centos6file7
-rw-rw-r--. 1 sujin sujin 0 Oct 19 13:14 centos6file8


From the above  list of files i want change all the filename  starting with centos6 to centos7. I mean file centos6file2 changed to centos7file2. 

It is bit time consuming using mv command to change it one by one. Persong who using mv command for do this not a good linux user. 

By using a linux command called rename we can achieve this.


         rename centos6 centos7 centos6*


After applying above command file name list would be like below.

[sujin@localhost blog]$ rename centos6 centos7 centos6*
[sujin@localhost blog]$ 
[sujin@localhost blog]$ ls -l
total 0
-rw-rw-r--. 1 sujin sujin 0 Oct 19 13:12 centos7file1
-rw-rw-r--. 1 sujin sujin 0 Oct 19 13:12 centos7file2
-rw-rw-r--. 1 sujin sujin 0 Oct 19 13:12 centos7file3
-rw-rw-r--. 1 sujin sujin 0 Oct 19 13:12 centos7file4
-rw-rw-r--. 1 sujin sujin 0 Oct 19 13:12 centos7file5
-rw-rw-r--. 1 sujin sujin 0 Oct 19 13:14 centos7file6
-rw-rw-r--. 1 sujin sujin 0 Oct 19 13:14 centos7file7
-rw-rw-r--. 1 sujin sujin 0 Oct 19 13:14 centos7file8


!!!

Operating System Used : Fedora release 20 (Heisenbug)



How to change HOSTNAME in Linux ( CentOS6 / RHEL6 )

Hostname in Linux helps to identify the computer by name over network. When installing operating system hostname will set to default name. We can change the hostname later by changing configuration files.

We can check the current hostname by a command 


[fedo@localhost ~]$ hostname
localhost.localdomain


By editing "HOSTNAME" field in /etc/sysconfig/network file we can change the hostname.

default configuration 

[fedo@localhost ~]$ cat /etc/sysconfig/network
NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=localhost.localdomain
NTPSERVERARGS=iburst

To edit this file we need root permission. So change from normal user to root user using su command and open the file using vim or your favourite editor and change the HOSTNAME field.

[root@localhost fedo]# cat /etc/sysconfig/network
NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=centos89
NTPSERVERARGS=iburst

I updated the hostname to centos89

reboot the computer then login and check the hostname.

[fedo@centos89 ~]$ hostname
centos89

Now you can see the hostname changed to centos89.

!!!

Operating System Used : CentOS release 6.5 (Final)




Linux command to get page size


By using getconf command we can get the size of Virtual Memory page in bytes.


[root@host-7-41 fedo]# getconf  PAGESIZE
4096


Here PAGESIZE is the argument passed to the getconf to get virtual memory page size. It gives the output as 4096 bytes.

!!!

Operating System Used : CentOS release 6.5 (Final)