Let's say we want to Zip files which are more than a week time under a folder /dir1/fldr1:
# for i in `find /dir1/fldr1 -mtime +7 -type f -print`; do gzip $i; doneThis Blog primarily focus on providing solutions for administering Linux Operating system in Enterprise environment. I have put my best effort to ensure the solutions provided in this are accurate, easy to follow, effective, and helpful for Linux Users & System Administrators.
Sunday, March 27, 2022
Thursday, December 20, 2018
LSOF command to list all Network connections in Listen mode
$ lsof -n | grep -i "TCP\|UDP" | grep -v "ESTABLISHED\|CLOSE_WAIT"
Monday, August 20, 2018
Monday, May 22, 2017
How to validate the Syntax of /etc/sudoers file ?
In Enterprise environment, the /etc/sudoers file is normally managed from a centralized server. Hence the modification to the files happens even before committing to actual server. We've an option to validate the Syntax before committing :
# visudo -cf /path_to_folder/sudoers.ver_num
# visudo -cf /etc/sudoers
Tuesday, September 13, 2016
How to count number of Files and Folders inside a given Directory
root@ashok:/opt # tree -iLf 1 puppetlabs/
puppetlabs
puppetlabs/bin
puppetlabs/facter
puppetlabs/file.txt
puppetlabs/mcollective
puppetlabs/puppet
4 directories, 1 file
root@ashok:/opt #
Tuesday, September 6, 2016
Sending HTML Mail using Mutt utility - Perl Syntax
my $current_date = `date +"%d-%b %H:%M"`;
chomp($current_date);
system("/usr/bin/mutt -e 'set content_type=text/html from=FromAddress\@domain.com realname=\"System Team\"' -s \"Report from $hstname at $current_date\" ToAddress\@domain.com -a FileName -c user1\@domain.com,user2\@domain.com < InputFile")
chomp($current_date);
Wednesday, August 24, 2016
List files that are modified recently with Timestamp
# find <PATH> -type f -printf '%TY-%Tm-%Td %TT %p\n' | sort -r | more
Example :
# find /var/www/html -type f -printf '%TY-%Tm-%Td %TT %p\n' | sort -r | more
Example :
# find /var/www/html -type f -printf '%TY-%Tm-%Td %TT %p\n' | sort -r | more
Monday, March 2, 2015
Reset only first level folder permissions
I had a requirement to reset all the home folders permission of FTP users to 750 (To disable Read/Browse permission to 'other' users).
Let's assume the FTP home folder structure is as follows: /ftp/home/<ftpusers> and the syntax to reset the folder permission would be :
# find /ftp/home -maxdepth 1 -type d -exec chmod 750 {} \;
Note: Using -maxdepth 1 wouldn't allow the find command to recurse though sub-folders.
Tuesday, December 10, 2013
Some useful SED command syntaxes
To delete trailing whitespaces at the end of each lines:
# sed 's/[ \t]*$//' filename > filename_notrailingspace
To remove all blank lines in a given file:
# sed '/^$/d' filename > filename_noblankspace
To remove all leading and trailing whitespaces of each lines in a given file:
$ cat filename | sed 's/^[ \t]*//;s/[ \t]*$//' > filename_nospace
Wednesday, December 4, 2013
Deleting files with special characters
In Linux, at times certain files are created with some special characters like hypen(-), single-quotes, blank character etc. It can happen either by accident or by some applications. Deleting those files would be difficult using regular 'rm' command options.
Hereby am describing certain methods to get rid of those files. For illustration purpose, I have taken 3 files with above mentioned special characters as example:
$ ls -ltr | tail -3
-rw-r--r-- 1 root root 0 Dec 4 05:30 file a
-rw-r--r-- 1 root root 0 Dec 4 05:31 '-fileb'
-rw-r--r-- 1 root root 1070 Dec 4 05:32 -filec
$ rm file a
rm: cannot lstat `file': No such file or directory
rm: cannot lstat `a': No such file or directory
$ rm '-filec'
rm: invalid option -- l
Try `rm ./-filec' to remove the file `-filec'.
Try `rm --help' for more information.
$ rm -filec
rm: invalid option -- l
Try `rm ./-filec' to remove the file `-filec'.
Try `rm --help' for more information.
Method 1 : Delete using inodes
$ ls -litr | tail -3
1511929 -rw-r--r-- 1 root root 0 Dec 4 05:30 file a
1511931 -rw-r--r-- 1 root root 0 Dec 4 05:31 '-fileb'
1511932 -rw-r--r-- 1 root root 1070 Dec 4 05:32 -filec
1511929 -rw-r--r-- 1 root root 0 Dec 4 05:30 file a
1511931 -rw-r--r-- 1 root root 0 Dec 4 05:31 '-fileb'
1511932 -rw-r--r-- 1 root root 1070 Dec 4 05:32 -filec
$ pwd
/opt
$ find /opt -inum 1511929 -exec rm -i {} \;
$ find /opt -inum 1511931 -exec rm -i {} \;
$ find /opt -inum 1511932 -exec rm -i {} \;
$ find /opt -inum 1511931 -exec rm -i {} \;
$ find /opt -inum 1511932 -exec rm -i {} \;
Others Methods: Using double-hypen "--" & double-quotes (depends on filenames)
$ rm -- -filec
rm: remove regular file `-filec'? y
$ rm -- file\ a
rm: remove regular empty file `file a'? y
$ file ./'-fileb'
./-fileb: ERROR: cannot open `./-fileb' (No such file or directory)
$ rm "'-fileb'"
rm: remove regular empty file `\'-fileb\''? y
$
Sunday, December 1, 2013
Rotating tcpdump logs
Syntax:
tcpdump -i <InterfaceName> -C 100 -s0 -W <No of files to rotate> -w /<tcpdump folderpath>
Option explanation:
-i : used to specify the Interface or Source IP Address
-C : specifies in size in MB
-c : number of count packets
-s : specifies the packet length to capture
-W : specifies the number of files to rotate through once the file size specified in -C is reached.
-w : Path to capture the tcpdump file with the extension .pcap.
Some examples using these options:
# tcpdump -i eth0 -C 100 -s0 -W 4 -w /tcpdumpfolder/filexyz.pcap
# tcpdump -i eth0 -c 1000 -s0 -W 4 -w /tcpdumpfolder/filexyz.pcap
# tcpdump -i eth0 -C 10 -s0 -W 3 -w /dump/server_$(date +%m-%d-%Y-%H:%M).pcap
# tcpdump -i any host 10.10.1.2 or host 10.10.1.5 -C 100 -s0 -W 5 -w /tcpdump/fileabc.pcap
Using RegEx in SUDO Access
We can use 'Regular expression' in Sudoers file !
Let's say we have a Command Alias in '/etc/sudoers' as follows:
Cmnd_Alias DBTASKS=/sbin/service mysqld start, /sbin/service mysqld stop, /sbin/service mysqld restart, /sbin/service mysqld status, /etc/init.d/mysqld start, /etc/init.d/mysqld stop, /etc/init.d/mysqld status, /etc/init.d/mysqld restart
This can be shrunken using RegEx as follows:
Cmnd_Alias DBTASKS
=/sbin/service mysqld [a-z]*, /etc/init.d/mysqld [a-z]*
(or)
Cmnd_Alias DBTASKS
=/sbin/service mysqld ?*, /etc/init.d/mysqld ?*
Finding last reboot, shutdown time and failed login attempts
Finding last reboot time:
# last reboot
# last -F
# who -b
Finding last shutdown time:
# last -x | grep '^shutdown'
Listing out failed login attempts:
# lastb
The other straight forward way to find the failed login attempts is to analyze the /var/log/secure file. I have written a Perl script exclusively to report the failed login attempts by parsing the /var/log/secure file.
Get the script from here: http://smart-scripts-for-sysadmins.blogspot.com/2012/05/perl-script-to-list-failed-login.html
Wednesday, November 27, 2013
Configuring RelayHost in Sendmail
Let's assume the Mail RelayHost name is "mailserver.domain.com".
Edit the following file /etc/mail/sendmail.mc
define(`SMART_HOST', `mailserver.domain.com')
# m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf
# Restart the Sendmail service
/etc/init.d/sendmail restart
Converting Time from PST to IST
Requirement:
I have some monitoring scripts
that captures the system statistics at a regular interval and writes it into a
Logfile with system time, which is in PST. When any incident happens
during the IST hours, the concern Team in India reports us the problem
with IST timing. As a subsequent action, when I try to analyze the
Logfile captured by the system, the timestamp in it was confusing me, as I need
to match with IST time-interval that was reported. So in order to easy my job, I came out with this solution of gathering the system statistics in
IST but without changing the Timezone setting on the server.
Solution:
TIME_IN_IST=`TZ='GMT-5:30' date +"%H:%M:%S %D IST"`
echo $TIME_IN_IST
Sample Execution:
[root@linuxhost]# date
Tue Nov 26 20:00:37 PST 2013
[root@linuxhost]# date +"%H:%M:%S %D IST"
20:00:42 11/26/13 IST
[root@linuxhost]# TIME_IN_IST=`TZ='GMT-5:30' date +"%H:%M:%S %D IST"`
[root@linuxhost]# echo $TIME_IN_IST
09:30:46 11/27/13 IST
[root@linuxhost]# date
Tue Nov 26 20:00:54 PST 2013
[root@linuxhost]#
Tue Nov 26 20:00:37 PST 2013
[root@linuxhost]# date +"%H:%M:%S %D IST"
20:00:42 11/26/13 IST
[root@linuxhost]# TIME_IN_IST=`TZ='GMT-5:30' date +"%H:%M:%S %D IST"`
[root@linuxhost]# echo $TIME_IN_IST
09:30:46 11/27/13 IST
[root@linuxhost]# date
Tue Nov 26 20:00:54 PST 2013
[root@linuxhost]#
How it works:
I have used GMT offset to match with IST, which
is 13.5 hours ahead. GMT for California is 8 hours behind "GMT-8.00" and GMT for India is "GMT+5.30". To match with India Timing, it has to be 13.5 (8+5.30) hours behind.
Tuesday, February 12, 2013
How to check if a file is being accessed by any process ?
Use 'lsof' command with the following syntax:
# lsof -f -- <path of file name>
Example:
linuxmach# lsof -f -- /var/log/secure
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
syslogd 3065 root 3w REG 253,0 40558 169083 /var/log/secure
linuxmach#
The file '/var/log/secure' is being accessed by "syslogd" dameon with the PID 3065 and it is being written now.
# lsof -f -- <path of file name>
Example:
linuxmach# lsof -f -- /var/log/secure
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
syslogd 3065 root 3w REG 253,0 40558 169083 /var/log/secure
linuxmach#
The file '/var/log/secure' is being accessed by "syslogd" dameon with the PID 3065 and it is being written now.
Monday, January 7, 2013
Using RegEx in find command
Below shown are the examples for using Regular Expression in 'find' command:
[ashok@linuxhost workdir]$ find . -type f -regex ".*[0-9]+.txt" -print 2> /dev/null
./1322143.txt
./anything_134.txt
./12345_837.txt
./something10984.txt
[ashok@linuxhost workdir]$ find . -type f -regex ".*/[0-9]+.txt" -print 2> /dev/null
./1322143.txt
[ashok@linuxhost workdir]$
[ashok@linuxhost workdir]$ find . -type f -regex ".*[0-9]+.txt" -print 2> /dev/null
./1322143.txt
./anything_134.txt
./12345_837.txt
./something10984.txt
[ashok@linuxhost workdir]$ find . -type f -regex ".*/[0-9]+.txt" -print 2> /dev/null
./1322143.txt
[ashok@linuxhost workdir]$
Wednesday, November 28, 2012
Argument list too long error
At times, you might get into a situation where you can't copy, move, remove or even list a huge number of files in a directory. When you attempt it, an error will be thrown saying "Argument list too long". This actually indicates that the OS has exceeded its Argument list limit. To know the Argument list limitation of your OS, run the following command "getconf ARG_MAX". It will show you the maximum number of arguments that can be passed to mv, cp, rm or ls command.
# getconf ARG_MAX
131072
To get rid of this problem, you must split your mv or cp command using wildcards.
Like instead of using "cp /folder1/* /folder2/ ", we should use "cp /folder1/a* /folder2/; cp /folder1/b* /folder2/"
(or)
you can use 'for' loop construct as follows:
# for i in {a..z}; do cp /folder1/$i* /folder2/ ; done &
Please note in 'for' loop, for every cp command it executes, it creates a new process id.
# getconf ARG_MAX
131072
To get rid of this problem, you must split your mv or cp command using wildcards.
Like instead of using "cp /folder1/* /folder2/ ", we should use "cp /folder1/a* /folder2/; cp /folder1/b* /folder2/"
(or)
you can use 'for' loop construct as follows:
# for i in {a..z}; do cp /folder1/$i* /folder2/ ; done &
Please note in 'for' loop, for every cp command it executes, it creates a new process id.
Thursday, November 8, 2012
Oracle ASM commands
Some Oracle ASM admin commands for my records :
oracleasm status
oracleasm listdisks
oracleasm start
oracleasm stop
oracleasm querydisk
oracleasm querydisk /dev/sd*
ls -l /dev/oracleasm/disks
oracleasm listdisks | xargs oracleasm querydisk -p
oracleasm scandisks
oracleasm scandisks -p
oracleasm restart
oracleasm enable
oracleasm disable
oracleasm configure
oracleasm createdisk ASMDISKName /dev/sdx
/usr/sbin/asmtool -C -l /dev/oracleasm -n ASMDISKName -s /dev/sdx -a force=yes
oracleasm deletedisk
oracleasm status
oracleasm listdisks
oracleasm start
oracleasm stop
oracleasm querydisk
oracleasm querydisk /dev/sd*
ls -l /dev/oracleasm/disks
oracleasm listdisks | xargs oracleasm querydisk -p
oracleasm scandisks
oracleasm scandisks -p
oracleasm restart
oracleasm enable
oracleasm disable
oracleasm configure
oracleasm createdisk ASMDISKName /dev/sdx
/usr/sbin/asmtool -C -l /dev/oracleasm -n ASMDISKName -s /dev/sdx -a force=yes
oracleasm deletedisk
Sunday, October 28, 2012
Creating complex password
There could be multiple ways to create Complex password using Linux OS. Here are couple of simple ways with sample execution to create complex password of length 8 characters:
linuxhost $ openssl rand -base64 6
NRwijHK9
linuxhost $ < /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c8; echo
7aSNmEot
linuxhost $
Scripted version, where you can define the characters that you require in the password:
http://smart-scripts-for-sysadmins.blogspot.com/2011/03/script-for-generating-complex-password.html
linuxhost $ openssl rand -base64 6
NRwijHK9
linuxhost $ < /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c8; echo
7aSNmEot
linuxhost $
Scripted version, where you can define the characters that you require in the password:
http://smart-scripts-for-sysadmins.blogspot.com/2011/03/script-for-generating-complex-password.html
Subscribe to:
Comments (Atom)