반응형

grep 에서 정규식을 쓰려면 -E 옵션을 추가하면 됩니다.



tail -f debug.log |grep -"^(START|REMOTE)"



이런 식으로 사용할 수 있습니다.


debug.log 에서 START 시작하거나 REMOTE로 시작하는 줄만 tail 하겠다.

반응형
반응형
* * * * *  /bin/beHappy.sh
┬ ┬ ┬ ┬ ┬
│ │ │ │ │
│ │ │ │ │
│ │ │ │ └───────── 요일 (0 - 6) (0:일요일, 1:월요일, 2:화요일, 3:수요일, 4:목요일, 5:금요일, 6:토요일)
│ │ │ └───────── 월 (1 - 12)
│ │ └───────── 일 (1 - 31)
│ └───────── 시 (0 - 23)
└───────── 분 (0 - 59)
 
┌───────────────────────────────────────────────────┐
│ * * * * *            
│ 1분마다 실행                                        
├───────────────────────────────────────────────────┤
│ 18, 37 * * * *     
| 매시간 18분, 37분에 실행                        
├───────────────────────────────────────────────────┤
│ */27 * * * * 
│ 27분마다 실행 (00:27, 00:54, 01:27, 01:54)     
├───────────────────────────────────────────────────┤
│ 13 5 * * * 
│ 매일 오전 5시 13분에 실행                        
├───────────────────────────────────────────────────┤
│ 37 */9 * * *
| 9시간마다 실행 (00:37, 09:37, 18:37)            
├───────────────────────────────────────────────────┤
│ 41 1-23/9 * * *
| 1시부터 9시간마다 실행 (01:41, 10:41, 19:41) 
├───────────────────────────────────────────────────┤
│ 0 8 * * 1-5
| 평일 08:00 실행 (월,화,수,목,금,금,금)              
├───────────────────────────────────────────────────┤
│ 0 8 * * 0,6
| 주말 08:00 실행 (일요일, 토요일)               
└───────────────────────────────────────────────────┘



반응형
반응형

아파치 웹서버를 일반 계정으로 실행하고 싶거나 해야하는 경우가 있습니다.

이럴 경우 sudo 를 통해 실행하는 방법에 대해서 공유하겠습니다.


root 계정에서 visudo 를 실행한 후 마지막 라인에 아래 라인을 추가해 줍니다.


goni9071 ALL=NOPASSWD:/usr/sbin/httpd
#USER_ID ALL=NOPASSWD:APACHE_DIR/httpd



그리고 해당 계정으로 아래 명령어를 통해 아파치를 실행할 수 있습니다.


sudo /usr/bin/httpd -k start



반응형
반응형

ssh 로그인 시도(해킹 시도) 확인하는 스크립트 입니다.


이 명령어는 로그인을 시도한 IP 목록만 결과 값으로 출력됩니다.

 ls /var/log/secure | xargs grep -"[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+" -| sort | uniq



이 명령어는 어떤 아이디로 언제 어떤 IP로 로그인을 시도한 결과까지 모두 보여줍니다.

last -/var/log/btmp | more



반응형
반응형

리눅스에서 sftp 접속시 Received message too long 오류 메시지가 발생시 해결 방법을 공유합니다.



ssh config를 vi로 엽니다.

vi /etc/ssh/sshd_config




Subsystem sftp /usr/lib/openssh/sftp-server 을 주석처리하고 Subsystem sftp internal-sftp 를 삽입 후

#Subsystem sftp /usr/lib/openssh/sftp-server
Subsystem sftp internal-sftp




ssh 서비스를 재시작 합니다.

service sshd restart



반응형
반응형

보안상 root 계정은 ssh를 통해서 바로 접속하지 못하도록 막는게 좋습니다.


sshd_config 파일을 열어서

vi /etc/ssh/sshd_config



PermitRootLogin 찾아 주석을 해제하고 yes를 no로 바꿉니다.

PermitRootLogin no




마지막으로 ssh 서비스를 재시작하면 바로 적용됩니다.

service sshd restart



반응형
반응형

apache http server project를 yum이나 rpm 없이 compile을 통해서 install하는 방법을 공유합니다.


아파치를 컴파일 하기위해선 먼저 apr, apr-util, pcre가 설치되어 있어야 합니다.


그리고 pcre를 설치하기 위해선 expat-devel 이 설치되어 있어야합니다. 

(pcre는 정규식사용을 위한 라이브러리입니다.)


아파치 설치를 위해 역순으로 설치합니다.


1. expat-devel 설치는 yum을 통해서 설치합니다. ( 추후에 expat도 컴파일 설치하는 방법을 공유하도록 하겠습니다. )


yum install expat-devel





2. pcre 설치


다운로드 URL : http://www.pcre.org/

URL에 접속하면 아래 화면이 뜨는데, 빨간 박스 부분을 클릭합니다.



그러면, 아래 화면으로 넘어가는데 최신버전은 아래로 가도록 정렬됩니다.

2018년 2월 현재 pcre중에 최신버전인 pcre-8.41.tar.gz를 다운로드 받습니다.



다운받은 파일의 압축을 풉니다.

tar -xvzf pcre-8.41.tar.gz



압축을 푼 디렉토리로 이동해서 configure 명령어를 실행합니다. prefix 옵션을 통해 설치 위치를 지정합니다.

make && make install 을 하면 설치가 완료됩니다.


./configure --prefix=/svc/apache/pcre8.41
make
make install



만약에 pcre 를 설치하기전에 expat-devel 이 설치되어있지 않으면 아래 오류메시지를 만나게 됩니다.

xml/apr_xml.c:35:19: error: expat.h: No such file or directory
xml/apr_xml.c:66: error: expected specifier-qualifier-list before ‘XML_Parser’
xml/apr_xml.c: In function ‘cleanup_parser’:
xml/apr_xml.c:364: error: ‘apr_xml_parser’ has no member named ‘xp’
xml/apr_xml.c:365: error: ‘apr_xml_parser’ has no member named ‘xp’
xml/apr_xml.c: At top level:
xml/apr_xml.c:384: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token
xml/apr_xml.c: In function ‘apr_xml_parser_create’:
xml/apr_xml.c:401: error: ‘apr_xml_parser’ has no member named ‘xp’
xml/apr_xml.c:402: error: ‘apr_xml_parser’ has no member named ‘xp’
xml/apr_xml.c:410: error: ‘apr_xml_parser’ has no member named ‘xp’
xml/apr_xml.c:411: error: ‘apr_xml_parser’ has no member named ‘xp’
xml/apr_xml.c:412: error: ‘apr_xml_parser’ has no member named ‘xp’
xml/apr_xml.c:424: error: ‘apr_xml_parser’ has no member named ‘xp’
xml/apr_xml.c:424: error: ‘default_handler’ undeclared (first use in this function)
xml/apr_xml.c:424: error: (Each undeclared identifier is reported only once
xml/apr_xml.c:424: error: for each function it appears in.)
xml/apr_xml.c: In function ‘do_parse’:
xml/apr_xml.c:434: error: ‘apr_xml_parser’ has no member named ‘xp’
xml/apr_xml.c:438: error: ‘apr_xml_parser’ has no member named ‘xp’
xml/apr_xml.c:442: error: ‘apr_xml_parser’ has no member named ‘xp_err’
xml/apr_xml.c:442: error: ‘apr_xml_parser’ has no member named ‘xp’
xml/apr_xml.c: In function ‘apr_xml_parser_geterror’:
xml/apr_xml.c:500: error: ‘apr_xml_parser’ has no member named ‘xp_err’
xml/apr_xml.c:500: error: ‘apr_xml_parser’ has no member named ‘xp_err’
make[1]: *** [xml/apr_xml.lo] Error 1
make[1]: Leaving directory `/home/goni9071/apr-util-1.6.1'
make: *** [all-recursive] Error 1




3. apr, apr-util 설치


다운로드 URL : https://apr.apache.org/


URL로 이동하면 아래 화면이 나오는데 빨간박스 부분을 클릭합니다. (위,아래 모두 동일한 링크입니다.)


위의 링크를 클릭하면 아래화면이 나옵니다.

APR과 APR-util을 각각 최신버전으로 다운로드 받습니다.


모두 다운로드 받았으면 pcre와 동일하게 다음 절차를 진행합니다.

prefix를 통해 설치 위치를 지정하고  --with-apr을 통해 apr이 설치된 위치를 알려줍니다.


tar -xvzf  apr-1.6.3.tar.gz
cd apr-1.6.3
./configure --prefix=/svc/apache/apr1.6
make
make install
 
tar -xvzf apr-util-1.6.1.tar.gz
cd apr-util-1.6.1
./configure --with-apr=/svc/apache/apr1.6 --prefix=/svc/apache/aprutil1.6
make
make install




4. 마지막으로 apache를 설치합니다.

다운로드 URL : http://httpd.apache.org/download.cgi


URL로 이동해서 아래화면의 빨간박스를 클릭합니다.


마찬가지로 다운롤드 받은 파일의 압추을 풀고 다음 절차대로 진행합니다.

prefix를 통해 설치위치를 지정하고 with-apr을 통해 apr 설치위치를 알려줍니다.

with-apr-util을 통해 apr-util 설치 위치를 with-pcre를 통해 pcre 설치위치를 알려줍니다.

enable-proxy 는 ProxyPass 기능이 필요해서 추가하였습니다.

enable-rewrite  역시 RewriteRule 기능이 필요해서 추가하였습니다.

tar -xvzf httpd-2.4.29.tar.gz
 
cd httpd-2.4.29
 
./configure --prefix=/svc/apache/httpd2.4 --with-apr=/svc/apache/apr1.6 --with-apr-util=/svc/apache/aprutil1.6 --with-pcre=/svc/apache/pcre8.41 --enable-proxy --enable-so --enable-rewrite
 
make
 
make install
cs



다음 명령어를 통해 아파치를 시작할 수 있습니다.

/svc/apache/httpd2.4/bin/httpd -k start





다음은 apr을 설치하지 않고 아파치 configure를 진행했을 때 발생하는 오류입니다.

checking for chosen layout... Apache
checking for working mkdir -p... yes
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
configure: 
configure: Configuring Apache Portable Runtime library...
configure: 
checking for APR... no
configure: error: APR not found.  Please read the documentation.



반응형
반응형

리눅스에서 마운트 해제를 명령어를 실행했는데 device is busy 오류가 발생했을 때 처리방법입니다.


[root@goni] umount /disk
 
 
umount: /disk: device is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))
 




fuser 는 특정 파일시스템에 대해서 누가(사용자,프로세스 등) 사용하고 있는지 확인하거나 해당 프로세스를 죽이는 업무 등을 실행할 수 있습니다.

-ck 옵션을 통해 마운트 해제하려는 파일시스템을 사용하고 있는 프로세스를 죽인 후에 다시 umount 명령어를 실행하면 잘 됩니다.


fuser -ck /disk



반응형
반응형

서비스를 리눅스에서 운영하다보면 하염없이 로그가 쌓여만 가는 경우가 발생합니다. (디스크 팡팡)

어플리케이션에서 로그를 알아서 정리해주는 경우면 좋겠지만 그렇지 않은 경우도 있기 때문에 전체적으로 로그를 정리해주는게 필요합니다.


쉘 스크립트를 작성해서 정리해주는 방법도 있지만 이번엔 apache의 logrotate 을 이용해 로그를 정리하는 방법을 공유합니다.


전제 조건으로는 apache가 설치되어 있어야합니다.


그리고 다음과 같이 /etc/crontab 파일에 corn.시리즈들이 등록되어있어야 주기적으로 logrotate가 실행됩니다.

(/etc/cron.daily 아래에 logrotate 파일이 있어야 합니다.)


vi /etc/crontab


1
2
3
4
5
6
7
8
9
10
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
 
# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly




아래 위치로 이동해 봅니다.

1
cd /etc/logrotate.d/



여러 파일이 보일텐데 여기에 우리가 정리하고자 하는 로그들과 정책을 정의하는 파일을 생성합니다.


tomcat을 예를 들어보겠습니다.

/etc/logrotate.d에 tomcat 파일을 만듭니다.


1
vi tomcat



내용은 아래처럼 설정합니다.


1
2
3
4
5
6
7
8
9
10
11
/svc/tomcat/tomcat7/*/logs/*.out {
 copytruncate
 maxsize 500M
 daily
 rotate 30
 compress
 missingok
 notifempty
 dateext
}



copytruncate : 기존 파일을 백업해서 다른 파일로 이동하고 기존 파일은 지워버리는 옵션

(이옵션을 넣지 않으면 현재 사용중인 로그를 다른이름으로 move하고 새로운 파일을 생성한다.)

(이옵션을 활용하면 postrotate를 통한 서비스 재시작 없이 무중단 로깅이 가능하다.)

maxsize : 파일 최대크기로 최대크기가 넘으면 로테이션하고 최대크기가 넘지 않으면 daily로 로테이션

daily : 로그파일을 날짜별로 변환

compress : 지나간 로그파일들을 gzip으로 압축

dateext : 순환된 로그파일의 날짜확장자

missingok : 로그파일이 없더라도 오류를 발생시키지 않음

rotate 30 : 로그 파일은 30개만큼 저장된 다음 제거되거나 메일로 보내짐

notifempty : 파일의 내용이 없으면 새로운 로그 파일을 생성 안함

maxage 30(숫자) : 30일 이산된 로그 파일 삭제


테스트는 다음 명령어를 이용해서 하면됩니다.

1
2
3
4
5
#디버그 모드 실행 (실제로 실행되지 않는다.)
/usr/sbin/logrotate -/etc/logrotate.conf
 
#강제 실행
/usr/sbin/logrotate -f/etc/logrotate.conf



반응형
반응형

리눅스에서 파일의 날짜를 변경하는 방법입니다.


#빈 파일 생성
touch 파일명
 
#현재시간으로 변경
touch -c 파일명
 
#특정시간으로 변경
touch -t yyyyMMddHHmm 파일명
 
#다른 파일의 수정시간 복사
touch -r 복사원본파일명 복사대사파일명



반응형

+ Recent posts