Skip to main content

Posts

Showing posts with the label unix

Preventing SSH attacks

One solution is to run sshd on a non-standard port. Most automated attacks only attempt to connect on port 22 and therefore, this can be an effective way to hide from many attackers. To configure this, just change the Port line in /etc/ssh/sshd_config and restart ssh as follows; vi /etc/ssh/sshd_config Port 922 <-- update port number and uncomment if the setting is commented /etc/init.d/sshd restart

setting up cron jobs

In order to setup cron jobs for a user other than the root, use the following command which should open a file in vi editor that schedules cron job for that particular user. crontab -u <user> -e Later, if you wanted to view a list of cron jobs scheduled by a user, use the following command. crontab -u <user> -l

Blocking port 8080

Linux firewall can be managed using iptables command. I setup tomcat with Apache webserver in front using mod_jk2. But tomcat was still accessbile from port 8080. In order to disable direct access to port 8080 I executed following command. iptables -A INPUT -p tcp --dport 8080 -j REJECT You can review the above rule and any other existing rules as follows; iptables -L -n --line-numbers Now, if later you wanted to undo or remove a rule, use the above command to figure out the line number of the rule. A sample output from the previous command looks like below; Chain INPUT (policy DROP) num target prot opt source destination 1 LOG tcp -- 0.0.0.0/0 0.0.0.0/0 tcp flags:!0x16/0x02 state NEW LOG flags 0 level 4 prefix `NEW NOT SYN: ' 2 DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp flags:!0x16/0x02 state NEW 3 DROP tcp -- 207.46.249.190 0.0.0.0/0 tcp 4 DROP tcp -- 0.0.0.0/0 ...

Creating no-reply@domain.com account in Postfix

If you wanted to send emails to users but did not want to receive any replies to the email, you would need to setup a no-reply@domain.com email account. These kind of email ids are useful when sending emails containing forgotten passwords or activation code. Below are the steps for creating such account in Postfix. 1. Identify the file containing alias for Postfix First, make sure the following line in the ALIAS DATABASE section of the /etc/postfix/main.cf is NOT commented out: alias_maps = hash:/etc/aliases 2. Create an alias that redirects messages to /dev/null Edit /etc/aliases and add following entry devnull: /dev/null 3. Create a virtual email id Edit /etc/postfix/virtual and add following entry no-reply@domain.com devnull 4. refresh postfix alias and postfix cache Execute following commands. (You may require root privileges) > newaliases > postfix reload

Extract a column from text file

In order to extract a column values from a text file where each line contains several words separated by space, following can be used; cut -d' ' -f6 file.txt > result.txt This would extract values from 6th column in a file named file.txt where each line contains words separated by white spaces.

Linux: Create New User Account

Creating a user account is a two step process. 1. Create a user account. prompt> useradd user-name 2. set a password for the new account. prompt>passwd user-name This will create a user with a home directory /home/ user-name and default shell set to bash.

Setting Clock

The clock can be set manually and using NTP service. 1. Manually: [root@server /root]# date 012621362007 Fri Jan 26 21:36:26 PST 2007 [root@server /root]# hwclock --utc --systohc [root@server /root]# 2. Using NTP service: Add the time server to /etc/ntp.conf and to /etc/ntp/step-tickers: /etc/ntp.conf: server 192.168.0.1 server 192.168.0.2 /etc/ntp/step-tickers 192.168.0.1 192.168.0.2 Then make sure that ntp will start at boot time: chkconfig --level 2345 ntpd on chkconfig --list ntpd And start the service: service ntpd start

Search and Replace String

A string consisting of one or more words can be replaced as follows. >cat file the black cat was chased by the brown dog >sed -e 's/black/white/g' file the white cat was chased by the brown dog A pattern can also be used for search string. For example following will replace entire string starting with 2007 until = symbol in a text. sed -e 's/^2007.*=/replacestring/' file