서버관리 2014. 10. 7. 16:08

유닉스 리눅스 du 명령어 에서 sort 로 정렬하기

유닉스 리눅스에서 du 명령어는 폴더나 파일의 사용량을 보여 주는 명령어 이다. 

Summarize disk usage of each FILE, recursively for directories.


1.

기본적으로 모든 파일 폴더는 물론 서브폴더 까지 다 보여 주기 때문에 너무 많이 리스트 된다. 

그래서 --max-depth=N 이라는 옵션을 이용하여 보여주는 폴더 단계를 조절해서 사용한다. 


# du --max-depth=1 

이라고 하면 1단계 폴더만 리스트 된다. 


이 옵션을 별로로 옵션을 주지 않고 영구적으로 설정하여 계속 동작하게 하려면, 

/etc/profile.d/ 폴더에 파일 하나를 추가해 서 위 옵션을 적어 넣어 주면 된다. 

물론, bashrc 파일 등을 직접 수정할 수도 있지만, 확장성을 위해서 /etc/profile.d/ 를 이용하는 게 좋다.


/etc/profile.d/zzz.sh 파일의 내용은 아래 형식으로 넣어 준다. 

alias du='du -h --max-depth=1'


위 옵션들은 각각

-h : human-readable 보기 쉬운 형태로 보여 준다. eg, 1K 2345M 2G

--max-depth=1 : 디렉토리를 1단계만 보여 준다. 


 2.

du 의 결과물을 정렬을 해 보자. 


du 명령어 자체에는 정렬 옵션이 없어 보인다. 

그래서, sort 명령어를 추가해서 정렬을 할 수 있다. 


# du | sort -nr


이런식으로 du 의 결과물을 정렬할 수 있다. 

du 한 후에 sort 하기 때문에, du 의 결과물이 많을 경우 sort 에도 시간이 많이 걸릴 수 있다. 


sort 의 옵션들은 각각

-n : numeric-sort 문자열의 수치 값에 따라 비교한다. 

-r : reverse  역순으로 정렬

서버관리 2014. 10. 3. 10:21

CentOS 5.x 에서 curlftpfs 로 ftp filesystem mount 하기

CurlFtpFs 를 이용하여, 센토스 5.x 의 리눅스 박스에 ftp 를 로컬에 마운트 해서 사용하는 방법이다. 



1.

curlftpfs 는 yum시스템에 등록되어 있지 않기 때문에, yum install curlftpfs 로 설치할 수 없다. 

소스를 가져와서 설치해야 한다. 

curlftpfs 의 사이트는 http://curlftpfs.sourceforge.net/  이다. 



2.

curlftpfs 를 설치하기 위해서는 몇가지 패키지가 설치되어 있어야 한다. 

다음으로 그 패키지들을 설치할 수 있다. 이는 yum으로 설치할 수 있다. 


# yum install glib2-devel

# yum install fuse-devel

# yum install libcurl-devel


일부는 이미 설치되어 있을 수도 있다. 

필자의 경우는 fuse 가 설치되어 있지 않았다.

설치하면, fuse-devel 와 의존성 패키지인 fuse-libs 가 같이 설치된다.

libcurl 은 설치는 되어 있는데, 설치되어 있는 버전이 낮다. ㅡㅡ 

버전 7.17.0 이 필요한데. 설치된 버전은 7.15.5 이다. 



3.

이젠 curlftpfs 를 설치해 보자.

# cd /usr/local/src

# wget http://sourceforge.net/projects/curlftpfs/files/latest/download

# tar xvzf curlftpfs-0.9.2.tar.gz

# cd curlftpfs-0.9.2

# ./configure

# make

# make install


위의 wget 에서

# wget http://downloads.sourceforge.net/projects/curlftpfs/curlftpfs/0.9.2/curlftpfs-0.9.2.tar.gz

이렇게 지정을 해도 된다.


4. 
0.9.2 버전의 curlftpfs 를 ./configure 할때 오류가 난다. 
오류 내용은 위에 적힌 대로, libcurl 버전이 낮아서 설치가 안된다는 것이다. 
libcurl 7.15.5 버전과 7.16.0 버전의 문제점에 대해서는 위 curlftpfs 사이트에 나와 있다. 
하지만, 0.9.2 버전이 센토스 5.11 에서 설치가 안되니, 0.9.1 버전을 설치해 본다. 
어디까지나 테스트이다. 실서비스에 사용은 안될 듯 싶다. 

# cd /usr/local/src

# wget http://downloads.sourceforge.net/projects/curlftpfs/curlftpfs/0.9.1/curlftpfs-0.9.1.tar.gz

# tar xvzf curlftpfs-0.9.1.tar.gz

# cd curlftpfs-0.9.1

# ./configure

# make

# make install


일단 설치는 된다. ^^


5.
이제 curlftpfs 로 ftp 서버를 마운트 해 보자. 
# mkdir /mnt/ftpbackup
# curlftpfs -o allow_other ftp://userid:userpw@ftp.host.com /mns/ftpbackup
fuse: device not found, try 'modprobe fuse' first

에러가 난다. ㅡㅡ 시키는 대로 해 주자
# modprobe fuse

다시  mount 시켜 본다. 
# curlftpfs -o allow_other ftp://myuser:mypass@ftp.host.com /mns/ftpbackup

에러 없이 잘 떨어진다. ^^
일단은 성공이긴 한데, 문제가 좀 있다. 
프로세스 보기를 해 보면, 아이디와 비번이 노출이 된다. ㅡㅡ

아이디와 패스워드를 별도 파일로 저장할 수 있다. 홈디렉토리에 .netrc 파일을 만들어 넣으면 된다. 
/root/.netrc 파일의 내용은 다음과 같다. 

machine ftp.host.com
login myuser
password mypass

위 파일의 퍼미션은 600 으로 주면 된다. 

위 파일이 있을 경우 명령문은 아래와 같이 간단히 할 수 있다. 
# curlftpfs -o allow_other ftp://ftp.host.com /mns/ftpbackup


6.
자동 마운트를 위해서 /etc/fstab 에 추가해 보자.

curlftpfs#ftp.host.com /mnt/ftpbackup fuse rw,uid=500,user,noauto 0 0

/etc/fstab 파일에 추가하는 자동 마운트가 잘 안될 경우 
/etc/rc.d/rc.local 에 다음 명령문을 추가하여 부팅시마다 실행되게 할 수도 있다. 

curlftpfs -o allow_other ftp://ftp.host.com /mns/ftpbackup &



* 참고





서버관리 2014. 10. 3. 10:18

리눅스 logrotate 에서 로그파일 강제로 rotate 시키기



리눅스 계열 서버에서 각종 로그를 로테이트시키는 기능은 logrotate 가 담당한다. 

그런데, 이 logrotate 는 cron 데몬에 의해서 실행이 된다. ^^



1. logrotate 가 실행되는 과정을 살펴 보자.


/etc/crontab 파일에 보면 아래와 같은 구문으로 매일 실행되는 것들의 폴더 위치가 있다. 

02 4 * * * root run-parts /etc/cron.daily


위 폴더를 찾아가서 보면, logrotate 가 있다. 

그러므로, 로그로테이트는 4시02분에 하루에 한번씩 실행된다. 


logrotate 파일을 열어 보면.. 아래와 같은 구문이 있다. 

/usr/sbin/logrotate /etc/logrotate.conf


실제 실행파일의 위치와 설정파일의 위치를 알 수 있다. 


이제 /etc/logrotate.conf 파일을 열어 보자..

주요 설정들이 있는데, 역시나 아래와 같은 설정파일을 따로 빼 놓은 인클루드 구문이 있다. 

include /etc/logrotate.d


/etc/logrotate.d 폴더에 들어가 보면.. 이제 개별로 로테이트 시킬 로그에 대한 설정들이 들어 있다. ^^

오늘 살펴볼 아파치 서버의 로그를 로테이트 시키는 놈은 apache 나 httpd  와 같은 이름으로 되어 있다. 

물론 다른 이름으로 되어 있을 수도 있으나, 누가 보더라도 직관적인 이름이 좋다. 


파일을 열어 보면.. 아래는 필자의 설정파일 이다.

/var/log/httpd/*-access_log /var/log/httpd/*-error_log {

size 500k

rotate 19

missingok

compress

postrotate

/usr/bin/killall -HUP httpd

endscript

}


위 세팅에 대한 설명은..

virtual host 세팅으로 여러 도메인의 로그가 따로 저장되어 있으며, 

로그파일 사이즈가 500k 가 넘으면 로테이트 시키고 지난 19 건 이전 것은 삭제한다. 압축저장한다.


또다른 설정을 만들어 보면..

/var/log/httpd/*-access_log /var/log/httpd/*-error_log {

missingok

rotate 5

weekly

notifempty

sharedscripts

postrotate

/usr/local/apache/bini/apachectl restart

endscript

}


위 세팅의 조건은 매주 실행되고 빈파일은 로테이트 하지않으며 5건 이전 것은 삭제한다. 



2. 강제로 로테이트를 시켜 보자.. 


위 설정대로 일주일에 한번만 실행하라고 세팅하면, 오늘 실행후 일주일을 기다려야 한다. 

하지만, 잘 되는지 확인해 보고자 할 때가 있다. 


이때는, logrotate 가 실행된 날짜가 저장되어 있는 파일을 찾아 날짜를 조작하면 된다. 

해당 파일은 /var/lib/logrotate.status  이다. 

열어 보면 아래와 같은 구문이 보인다. 

"/var/log/httpd/www.domain.com-access_log" 2014-10-1


여기서 맨 뒤에 있는 날짜가 마지막 실행된 날짜 이므로 이 날짜를 일주일 전으로 수정하면 된다. 

그리고, logrotate 를 실행한다. 

# /etc/cron.daily/logrotate


로그 파일을 살펴보면, 로그가 로테이트 되었음을 볼 수 있다. 



* 참고 및 이미지 출처 : http://www.thegeekstuff.com/2010/07/logrotate-examples/#more-4826


서버관리 2014. 10. 1. 16:17

HowTo: The Ultimate Logrotate Command Tutorial with 10 Examples

리눅스 로그로테이트 설정 파일 예제


* 출처 : http://www.thegeekstuff.com/2010/07/logrotate-examples/#more-4826



1. Logrotate Configuration files


Following are the key files that you should be aware of for logrotate to work properly.


/usr/sbin/logrotate – The logrotate command itself.


/etc/cron.daily/logrotate – This shell script executes the logrotate command everyday.

$ cat /etc/cron.daily/logrotate

#!/bin/sh


/usr/sbin/logrotate /etc/logrotate.conf

EXITVALUE=$?

if [ $EXITVALUE != 0 ]; then

    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"

fi

exit 0


/etc/logrotate.conf – Log rotation configuration for all the log files are specified in this file.

$ cat /etc/logrotate.conf

weekly

rotate 4

create

include /etc/logrotate.d

/var/log/wtmp {

    monthly

    minsize 1M

    create 0664 root utmp

    rotate 1

}


/etc/logrotate.d – When individual packages are installed on the system, they drop the log rotation configuration information in this directory. For example, yum log rotate configuration information is shown below.

$ cat /etc/logrotate.d/yum

/var/log/yum.log {

    missingok

    notifempty

    size 30k

    yearly

    create 0600 root root

}

2. Logrotate size option: Rotate the log file when file size reaches a specific limit


If you want to rotate a log file (for example, /tmp/output.log) for every 1KB, create the logrotate.conf as shown below.

$ cat logrotate.conf

/tmp/output.log {

        size 1k

        create 700 bala bala

        rotate 4

}


This logrotate configuration has following three options:

size 1k – logrotate runs only if the filesize is equal to (or greater than) this size.

create – rotate the original file and create the new file with specified permission, user and group.

rotate – limits the number of log file rotation. So, this would keep only the recent 4 rotated log files.


Before the logrotation, following is the size of the output.log:

$ ls -l /tmp/output.log

-rw-r--r-- 1 bala bala 25868 2010-06-09 21:19 /tmp/output.log


Now, run the logrotate command as shown below. Option -s specifies the filename to write the logrotate status.

$ logrotate -s /var/log/logstatus logrotate.conf


Note : whenever you need of log rotation for some files, prepare the logrotate configuration and run the logroate command manually.

 After the logrotation, following is the size of the output.log:

$ ls -l /tmp/output*

-rw-r--r--  1 bala bala 25868 2010-06-09 21:20 output.log.1

-rwx------ 1 bala bala        0 2010-06-09 21:20 output.log


Eventually this will keep following setup of rotated log files.

output.log.4.

output.log.3

output.log.2

output.log.1

output.log


Please remember that after the log rotation, the log file corresponds to the service would still point to rotated file (output.log.1) and keeps on writing in it. You can use the above method, if you want to rotate the apache access_log or error_log every 5 MB.


Ideally, you should modify the /etc/logrotate.conf to specify the logrotate information for a specific log file.


Also, if you are having huge log files, you can use: 10 Awesome Examples for Viewing Huge Log Files in Unix

3. Logrotate copytruncate option: Continue to write the log information in the newly created file after rotating the old log file.

$ cat logrotate.conf

/tmp/output.log {

         size 1k

         copytruncate

         rotate 4

}


copytruncate instruct logrotate to creates the copy of the original file (i.e rotate the original log file) and truncates the original file to zero byte size. This helps the respective service that belongs to that log file can write to the proper file.


While manipulating log files, you might find the sed substitute, sed delete tips helpful.

4. Logrotate compress option: Compress the rotated log files


If you use the compress option as shown below, the rotated files will be compressed with gzip utility.

$ cat logrotate.conf

/tmp/output.log {

        size 1k

        copytruncate

        create 700 bala bala

        rotate 4

        compress

}


Output of compressed log file:

$ ls /tmp/output*

output.log.1.gz output.log

5. Logrotate dateext option: Rotate the old log file with date in the log filename

$ cat logrotate.conf

/tmp/output.log {

        size 1k

        copytruncate

        create 700 bala bala

        dateext

        rotate 4

        compress

}


After the above configuration, you’ll notice the date in the rotated log file as shown below.

$ ls -lrt /tmp/output*

-rw-r--r--  1 bala bala 8980 2010-06-09 22:10 output.log-20100609.gz

-rwxrwxrwx 1 bala bala     0 2010-06-09 22:11 output.log


This would work only once in a day. Because when it tries to rotate next time on the same day, earlier rotated file will be having the same filename. So, the logrotate wont be successful after the first run on the same day.


Typically you might use tail -f to view the output of the log file in realtime. You can even combine multiple tail -f output and display it on single terminal.

6. Logrotate monthly, daily, weekly option: Rotate the log file weekly/daily/monthly


For doing the rotation monthly once,

$ cat logrotate.conf

/tmp/output.log {

        monthly

        copytruncate

        rotate 4

        compress

}


Add the weekly keyword as shown below for weekly log rotation.

$ cat logrotate.conf

/tmp/output.log {

        weekly

        copytruncate

        rotate 4

        compress

}


Add the daily keyword as shown below for every day log rotation. You can also rotate logs hourly.

$ cat logrotate.conf

/tmp/output.log {

        daily

        copytruncate

        rotate 4

        compress

}

7. Logrotate postrotate endscript option: Run custom shell scripts immediately after log rotation


Logrotate allows you to run your own custom shell scripts after it completes the log file rotation. The following configuration indicates that it will execute myscript.sh after the logrotation.

$ cat logrotate.conf

/tmp/output.log {

        size 1k

        copytruncate

        rotate 4

        compress

        postrotate

               /home/bala/myscript.sh

        endscript

}

8. Logrotate maxage option: Remove older rotated log files


Logrotate automatically removes the rotated files after a specific number of days.  The following example indicates that the rotated log files would be removed after 100 days.

$ cat logrotate.conf

/tmp/output.log {

        size 1k

        copytruncate

        rotate 4

        compress

        maxage 100

}

9. Logrotate missingok option: Dont return error if the log file is missing


You can ignore the error message when the actual file is not available by using this option as shown below.

$ cat logrotate.conf

/tmp/output.log {

        size 1k

        copytruncate

        rotate 4

        compress

        missingok

}

10. Logrotate compresscmd and compressext option: Sspecify compression command for the log file rotation

$ cat logrotate.conf

/tmp/output.log {

        size 1k

        copytruncate

        create

        compress

        compresscmd /bin/bzip2

        compressext .bz2

        rotate 4

}


Following compression options are specified above:

compress – Indicates that compression should be done.

compresscmd – Specify what type of compression command should be used. For example: /bin/bzip2

compressext – Specify the extension on the rotated log file. Without this option, the rotated file would have the default extension as .gz. So, if you use bzip2 compressioncmd, specify the extension as .bz2 as shown in the above example.


서버관리 2014. 9. 30. 18:45

apache 아파치 웹서버에서 디렉토리 목록 보이지 않게 설정하기

일반적으로 아파치 서버를 설정할 때에, 

폴더에 index.php index.html 등의 파일이 없을 경우, 디렉토리 목록이 보이지 않게 하고자 한다면

Option 에서 indexes 항목을 빼면 된다. 


아래와 같은 세팅이 있으면, 


<Directory "/home/*/public_html">

   Options Indexes FollowSymLinks

</Directory>


Option 에서 Indexes 를 제거한 후, 아파치 서버를 다시 시작하면 디렉토리 목록이 보이지 않게 된다.  


<Directory "/home/*/public_html">

   Options FollowSymLinks

</Directory>



그리고, 아래 웹사이트와 같이 오히려 간단하게 목록을 보여 줘야 하는 경우가 있으면, 

Indexes 옵션을 이용해서 보여 줄 수도 있다. 


http://www.ibiblio.org/pub/