Sunday, April 17, 2005

The watch command

watch - execute a program periodically, showing output fullscreen

SYNOPSIS
watch [-dhv] [-n ] [--differences[=cumulative]] [--help]
[--interval=] [--version]

DESCRIPTION
watch runs command repeatedly, displaying its output (the first screen-
full). This allows you to watch the program output change over time.
By default, the program is run every 2 seconds; use -n or --interval to
specify a different interval.

The -d or --differences flag will highlight the differences between
successive updates. The --cumulative option makes highlighting
"sticky", presenting a running display of all positions that have ever
changed.

watch will run until interrupted.

EXAMPLES
To watch for mail, you might do

watch -n 60 from

To watch the contents of a directory change, you could use

watch -d ls -l

Set up Linux to sync its time with a time server

ntpdate server

should do the trick from the command line or cron job.

e.g.

[root@server root]# ntpdate time.nist.gov
17 Apr 15:53:26 ntpdate[29455]: step time server 192.43.244.18 offset -0.715067 sec

Unzip all zip files in a directory

unzip *.zip won't work- use:

unzip \*.zip

Sort directories by size

du | sort -nr | less

Use the screen utility

Using the screen utility

If you quit one of the processes under screen's control (for instance, by typing q to top or typing exit at a shell prompt), that window will close. When its last window closes, screen, and its backend process, SCREEN, terminate.

If some screen windows are open, though, you can also leave screen temporarily. Here are two ways:

You can suspend screen by typing C-a z or C-a C-z from any window. You can restart it (before you log out!) by typing the shell's fg command. This uses Linux job control.

You can detach screen from your current terminal. The SCREEN backend process and the ptys it manages keep running. You can log out and log in again later -- even days or weeks later (as long as the system isn't rebooted) -- and reattach the SCREEN backend process to your new terminal. To detach screen from your terminal, type C-a d. You should see the message [detached]. Now you'll be back at a prompt from the shell where you first started (or attached) screen.

For example, if you're monitoring a long-running industrial process from a tty on your office workstation, and you're afraid that it won't finish before you have to go home, start that processes under screen control. When you leave, use C-a d to detach. Now you can turn off your display (but not your CPU!), go home, connect to your workstation by (for instance) ssh, and reattach. It's that easy!

To reattach, type the command screen ­r at a shell prompt. Your window should appear just as it was when you left it -- unless its contents have changed in the meantime, of course. You can see previous lines of the display by using screen's scrollback history.

Do replace confirmation in Vim search & replace

In vim search and replace, do a

/gc

to have it confirm replace.

Enable mouse positioning in Vim in text mode

put

:set mouse=a

in .vimrc

to enable mouse positioning in vim in text mode

hold down SHIFT to enable terminal copy-n-paste (e.g. putty)

Fix Vim syntax highlighting not working problem

put the following in your .vimrc (_vimrc on windows):

autocmd BufEnter * :syntax sync fromstart

Configure X manually

You can try to configure X manually with:

/usr/share/redhat-config-xfree86/redhat-config-xfree86

Linux Installing & Setting up New Hard Drive (EXT3)

[root]# fdisk /dev/hdb
Command (m for help): m (Enter the letter "m" to get list of commands)
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)

Command (m for help): n
Command action
e extended
p primary partition (1-4)
e
Partition number (1-4): 1
First cylinder (1-2654, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-2654, default 2654):
Using default value 2654

Command (m for help): p

Disk /dev/hdb: 240 heads, 63 sectors, 2654 cylinders
Units = cylinders of 15120 * 512 bytes

Device Boot Start End Blocks Id System
/dev/hdb1 1 2654 20064208+ 5 Extended

Command (m for help): w (Write and save partition table)

[root]# mkfs -t ext3 /dev/hdb1
mke2fs 1.27 (8-Mar-2002)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
2508352 inodes, 5016052 blocks
250802 blocks (5.00%) reserved for the super user
First data block=0
154 block groups
32768 blocks per group, 32768 fragments per group
16288 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000

Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 34 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root]# mkdir /opt2
[root]# mount -t ext3 /dev/hdb1 /opt2



Enter the drive into the fstab file so that it is recognized and mounted upon system boot.

File: /etc/fstab Red Hat 8.0

LABEL=/ / ext3 defaults 1 1
LABEL=/boot /boot ext3 defaults 1 2
none /dev/pts devpts gid=5,mode=620 0 0
none /proc proc defaults 0 0
none /dev/shm tmpfs defaults 0 0
/dev/hda2 swap swap defaults 0 0
/dev/hdb1 /opt2 ext3 defaults 1 2
/dev/cdrom /mnt/cdrom iso9660 noauto,owner,kudzu,ro 0 0
/dev/fd0 /mnt/floppy auto noauto,owner,kudzu 0 0

Help against DDOS attacks

echo 1 > /proc/sys/net/ipv4/tcp_syncookies
echo 4096 > /proc/sys/net/ipv4/tcp_max_syn_backlog
echo 1 > /proc/sys/net/ipv4/icmp_ignore_bogus_error_responses
echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
echo 1024 65535 > /proc/sys/net/ipv4/ip_local_port_range
echo 0 > /proc/sys/net/ipv4/tcp_sack

# stops anyone doing an OS finger print scan on the IP stack, 64 will show as linux, 61 will screw them up
echo 61 > /proc/sys/net/ipv4/ip_default_ttl

for device in `ls /proc/sys/net/ipv4/conf`
do
echo "2" > /proc/sys/net/ipv4/conf/$device/rp_filter
echo "0" > /proc/sys/net/ipv4/conf/$device/accept_redirects
echo "0" > /proc/sys/net/ipv4/conf/$device/send_redirects
echo "1" > /proc/sys/net/ipv4/conf/$device/secure_redirects
echo "0" > /proc/sys/net/ipv4/conf/$device/accept_source_route
echo "1" > /proc/sys/net/ipv4/conf/$device/log_martians
done

more ideas:

#Reduce DoS'ing ability by reducing timeouts
echo 30 > /proc/sys/net/ipv4/tcp_fin_timeout
echo 1800 > /proc/sys/net/ipv4/tcp_keepalive_time
echo 1 > /proc/sys/net/ipv4/tcp_window_scaling
echo 0 > /proc/sys/net/ipv4/tcp_sack
echo 1280 > /proc/sys/net/ipv4/tcp_max_syn_backlog

Resolving relaying denied message from sendmail in same domain

If getting relaying denied message from sendmail when trying to connect within same domain, add:

FEATURE(relay_entire_domain)

to:

/etc/mail/sendmail.mc

then:

make -C /etc/mail

then:

restart sendmail

for more info refer to:

http://www.sendmail.org/tips/relaying.html

[excerpt]

For more precise tuning, several FEATUREs have been added to control relaying:

FEATURE(relay_hosts_only). Normally domains are listed in /etc/mail/relay-domains; any hosts in those domains match. With this feature, each host in a domain must be listed.
FEATURE(relay_entire_domain). Setting this feature allows relaying of all hosts within your domain. For example, on the host gateway.A.COM, this feature allows mail to or from any host in the A.COM domain. More precisely, this relays any host listed in the $=m class. This is equivalent to listing the name of the domain in /etc/mail/relay-domains.
FEATURE(access_db). This enables the hash database /etc/mail/access to enable or disable access from individual domains (or hosts, if FEATURE(relay_hosts_only) is set). The database format is described below.
FEATURE(blacklist_recipients). If set, this feature looks up recipients as well as senders in the access database.
FEATURE(rbl). NOTE: Thie feature is obsolete. Use FEATURE(dnsbl) in newer versions of sendmail. Enables rejection of mail based on the Realtime Blackhole List maintained at mail-abuse.org.
FEATURE(dnsbl). Enables rejection of mail based on the Realtime Blackhole List maintained at mail-abuse.org.
FEATURE(accept_unqualified_senders). Normally, sendmail will not accept mail from a sender without a domain attached -- for example, user instead of user@B.NET. This feature allows such users.
FEATURE(accept_unresolvable_domains). Normally, sendmail will refuse to accept mail that has a return address with a domain that cannot be resolved using the regular host lookups (a technique commonly used by spammers). This feature permits acceptance of such addresses. Unresolvable domains can be selectively accepted using the access database.
FEATURE(relay_based_on_MX). Setting this feature permits relaying for any domain that is directed to your host.

Allow only certain hosts to ssh into your Linux box

sample /etc/hosts.allow file:

# hosts.allow This file describes the names of the hosts which are
# allowed to use the local INET services, as decided
# by the '/usr/sbin/tcpd' server.
#
sshd: 192.168.100. ALLOW
sshd: 192.168.10. ALLOW
sshd: ALL: DENY

GPG tutorial

from http://webber.dewinter.com/gnupg_howto/english/GPGMiniHowto-1.html

***Now the ***sender*** will crypt the message with the ***public key belonging to the receiver***.
Then decryption will be done with the secret key of the receiver.***

Classic methods for encryption only use one key for encryption. The sender encrypts the message
with this key. To be able to decrypt this the receiver needs to have this very same key. This key
must have been given to the receiver in a way, that others won't have had the opportunity to obtain
this key. If somebody else does have the key, this method of encryption is useless.


The use of so-called Public Keys can solve this problem. Public Keys is a concept where two keys
are involved. One key is a Public Key that can be spread through all sorts of media and may be
obtained by anyone. The other key is the Private Key. This key is secret and cannot be spread. This
key is only available to the owner. When the system is well implemented the secret key cannot be
derived from the public key. Now the sender will crypt the message with the public key belonging to
the receiver. Then decryption will be done with the secret key of the receiver.


Crucial in this concept is that the secret key remains a secret and should not be given away or
become available to anyone else but the owner of this key. YOU CANNOT SEND THIS KEY OVER THE
INTERNET. Also it is very unwise to use GnuPG over telnet (you might consider never to use telnet
based on the high security risks).


Creating a key With

gpg --gen-key



The command for exporting a key for a user is:

gpg --export [UID]



When you received someone's public key (or several public keys) you have to add them to your key database in order to be able to use them. To import into the database the command looks like this:

gpg --import [Filename]

e.g.:

C:\gnupg>
C:\gnupg>gpg --import maritz.asc
gpg: key A643961E: public key "Maritz Data Security " i
mported
gpg: Total number processed: 1
gpg: imported: 1

C:\gnupg>


4. Encrypt and decrypt
After installing everything and configuring everything in the way we want, we can start on encrypting and decrypting.

When encrypting or decrypting it is possible to have more than one private key in use. If this occurs you need to select the active key. This can be done by using the option -u UID or by using the option --local-user UID. This causes the default key to use to be replaced by wanted key.

If you want to change recipient this can be done by the option -r or by the option --recipient.


4.1 Encrypt

The command to encrypt is

gpg -e Recipient [Data]

or
gpg --encrypt Recipient [Data]

To avoid the risk that somebody else claims to be you, it is very useful to sign everything you encrypt, see signatures.

4.2 Decrypt
The command for decrypting is:

gpg [-d] [Data]

or
gpg [--decrypt] [Data]

Also here stdout is preset, but with the -o option you can redirect the output to a file.

Check mx records of a domain

dig domain mx

e.g.:

[root@server /home]# dig socal.rr.com mx

; <<>> DiG 9.1.0 <<>> socal.rr.com mx
;; global options: printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 21901
;; flags: qr rd ra; QUERY: 1, ANSWER: 6, AUTHORITY: 2, ADDITIONAL: 6

;; QUESTION SECTION:
;socal.rr.com. IN MX

;; ANSWER SECTION:
socal.rr.com. 3600 IN MX 20 kcmx02.mgw.rr.com.
socal.rr.com. 3600 IN MX 30 orange.mgw.rr.com.
socal.rr.com. 3600 IN MX 10 lamx01.mgw.rr.com.
socal.rr.com. 3600 IN MX 10 lamx02.mgw.rr.com.
socal.rr.com. 3600 IN MX 10 lamx03.mgw.rr.com.
socal.rr.com. 3600 IN MX 20 kcmx01.mgw.rr.com.

;; AUTHORITY SECTION:
socal.rr.com. 3600 IN NS dns-sec-01.socal.rr.com.
socal.rr.com. 3600 IN NS dns-pri-01.socal.rr.com.

;; ADDITIONAL SECTION:
lamx01.mgw.rr.com. 75198 IN A 66.75.160.12
lamx02.mgw.rr.com. 75198 IN A 66.75.160.13
lamx03.mgw.rr.com. 75198 IN A 66.75.160.11
kcmx01.mgw.rr.com. 75197 IN A 24.94.163.190
kcmx02.mgw.rr.com. 75198 IN A 24.94.165.190
dns-pri-01.socal.rr.com. 2514 IN A 66.75.160.39

;; Query time: 97 msec
;; SERVER: 192.168.100.26#53(192.168.100.26)
;; WHEN: Fri Oct 10 15:27:15 2003
;; MSG SIZE rcvd: 318

Linux Runlevels

Before you can configure access to services, you must understand Linux runlevels. A runlevel is a state, or mode, that is defined by the services listed in the directory /etc/rc.d/rc.d, where is the number of the runlevel.

Red Hat Linux uses the following runlevels:


0 — Halt

1 — Single-user mode

2 — Not used (user-definable)

3 — Full multi-user mode

4 — Not used (user-definable)

5 — Full multi-user mode (with an X-based login screen)

6 — Reboot

If you configured the X Window System during the Red Hat Linux installation program, you had the option of choosing a graphical or text login screen. If you chose a text login screen, you are operating in runlevel 3. If you chose a graphical login screen, you are operating in runlevel 5.

The default runlevel can be changed by modifying the /etc/inittab file, which contains a line near the top of the file similar to the following:

id:3:initdefault:


Change the number in this line to the desired runlevel. The change will not take effect until you reboot the system.

To change the runlevel immediately, use the command telinit followed by the runlevel number. You must be root to use this command.

Solution for "File argument list too long" message with grep

If get "File argument list too long" message with grep:

ls -1 > filelist
awk '{ printf("grep 1a1 %s\n", $1) }' filelist > script.sh
chmod +x script.sh
./script.sh

Setting the Linux Host Name

Checking your Linux host name
First, see if your host name is set correclty using the following commands:
uname -n
hostname -a
hostname -s
hostname -d
hostname -f
hostname

If the above commands return correctly with no errors then all may be well; however, you may want to read on to verify that all settings are correct.



--------------------------------------------------------------------------------
Configuring /etc/hosts
If your IP address is assigned to you by a DHCP server, then /etc/hosts is configured as follows:
127.0.0.1 mybox.mydomain.com localhost.localdomain localhost mybox

If you have a static IP address, then /etc/hosts is configured as follows:
127.0.0.1 localhost.localdomain localhost
192.168.0.10 mybox.mydomain.com mybox


--------------------------------------------------------------------------------
Setting the Host Name using "hostname"
After updating the /etc/hosts file correctly, the "hostname" command should be run as follows to set your hostname:


hostname mybox.mydomain.com

--------------------------------------------------------------------------------Checking /etc/HOSTNAME (if present)

You may or may not have the file /etc/HOSTNAME:

mybox.mydomain.com


--------------------------------------------------------------------------------Checking /etc/sysconfig/network
If you have a static IP address, then /etc/sysconfig/network is configured as follows:
NETWORKING=yes
HOSTNAME="mybox.mydomain.com"
...


If your IP address is assigned to you by a DHCP server, and you wish to update the local DNS server through Dynamic DNS, then /etc/sysconfig/network is configured as follows:
NETWORKING=yes
HOSTNAME="mybox.mydomain.com"
DHCP_HOSTNAME="mybox.mydomain.com"
...


--------------------------------------------------------------------------------Checking /proc/sys/kernel/hostname

This is checked with the following command:
cat /proc/sys/kernel/hostname


If you need to set this file, you can either reboot or set it now with the following command:
echo mybox.mydomain.com > /proc/sys/kernel/hostname


--------------------------------------------------------------------------------Dynamic DNS - Updating the local DNS server with your host name and DHCP IP
If you receive your IP address from a DHCP server, you may update the local DNS server by adding the following line to /etc/sysconfig/network for Red Hat:
DHCP_HOSTNAME="mybox.mydomain.com"

Fixing error message: Domain of sender address apache@localhost.localdomain does not exist

Hey all I have this problem with sendmail. I have a website that I have made for a friend, and it has a form on it. To bundle up the form data and send it out I use a simple perl script which works, but when my server tries to send the form via sendmail it gets sent back with an error message saying:

Final-Recipient: RFC822; klintonkerber@prodigy.net
Action: failed
Status: 5.1.8
Diagnostic-Code: SMTP; 553 5.1.8 ... Domain of sender address apache@localhost.localdomain does not exist
Last-Attempt-Date: Sun, 27 Jul 2003 16:07:09 -0700

Anyone have any suggestions on this? Thanx guys!

edit your sendmail.cf change the Cw entry appropriately. If you still have this problem, check your DNS, resolv.conf.

Sorting email in Pine

In Pine's generic configuration, messages are presented in the order in which they arrive. This default can be changed in the SETUP CONFIGURATION. You can also re-sort the folder on demand with the sort ($) command. Your sorting options are:

S Subject
A Arrival
F From
D Date
Z Size
O Ordered Subject
R Reverse