Unix Tips
Perl (Practical Extraction and Reporting Language) is a useful Unix and Windows programming language that is suited well to data migrations and text processing.
| Tip | Details |
| ADSM - Restore a file Geoff |
To restore a file from ADSM backup: dsmc res -pick -subdir=yes "/usr/home/*" |
| Analyse Core Dumps Acacia |
To debug a core dump - gdb application_name core.<PID> thread apply all (shows all thread activity) thread apply all where (shows where it failed) |
| Automated file clean up. Geoff |
Use the following command in your crontab file to automatically cleanup files older than 7 days. find /var/spool/mylogfiles -mtime +7 -exec rm {} \; |
| Automatic proxy config for Internet Explorer Geoff |
To set up an automatic configuration script on your network, create a file called proxy.pac in the root of you web server containing the following: function FindProxyForURL(url, host) { if ((dnsDomainLevels(host) == 0) || dnsFomainIs(host, ".mydomain.com.au") || isInNet(host, "192.16.60.0", "255.255.255.0") || shExpMatch(host, "192.16.60.*")) return "DIRECT"; else return "PROXY 192.16.60.240:3128"; } Now just check the "Use automatic configuration script" and provide the URL of the above file. |
| Backspace Key Problems Bob |
To fix your terminal backspace key: 1. stty erase [space] [backspace key] , or 2. stty erase [space] [CTRL v] [space] [CRTL h] Note that the [CRTL v] sequence is used to specify that the next character is a control key, in this case [CRTL h] or backspace. Often used in vi and at the shell prompt. |
| Build a Linux system Geoff |
Create boot floppy for installation mount /dev/cdrom cd /mnt/cdrom/images dd if=boot.img of=/dev/fd0 bs=1440k Reboot to install Redhat Login as root cd /etc mv securetty securetty.save vi hosts.allow ALL : 192.16.22.0/255.255.255.0 127.0.0.1 vi hosts.deny ALL : ALL chkconfig iptables off chkconfig sshd off chkconfig lpd off vi /etc/xinetd.d/telnet disable = no ps -ef | grep xinet kill -hup xinet vi /etc/aliases root: root@zues.mydomain.com.au newaliases |
| Burn CD from ISO Image Geoff |
Copy your ISO image into a location such as /data5/linux # If you want to check your image, you can # mount it as follows (optional) # Create a directory to mount the image mkdir /mnt/iso # Mount the image (not req for burn) mount -t iso9660 -o loop /data5/linux/yarrow-i386-disc1.iso /mnt/iso # Find the ID of your burner for the cdrecord parameter cdrecord -scanbus # Burn the iso image cdrecord -v -eject speed=6 dev=0,0,0 /data5/linux/yarrow-i386-disc1.iso # Sit back and wait |
| Capturing shell output from a script Acacia |
#!/bin/bash exec > operations.log 2>&1 echo "Hi there" All output to stdout and stderr (2) is captured to the log file without any other redirection commands. |
| cd ~ Acacia |
Change to your home directory. |
| cd - Acacia |
Change to your previous directory |
| Check for unresolved dynamic library Geoff |
Use the following command to check shared object libraries used by an application. Any problems will be reported in the output such as missing library files etc. ldd a.out |
| Check Running Process Operations Acacia |
Get the process ID <PID> using: ps -ef | grep processname Then list what the process is doing except seek operations using: truss -t!seek <PID> |
| Clean up old files Geoff |
find /var/spool/ -name \*.log -mtime +7 -exec rm {} \; |
| C++ Makefile Example Geoff Collett |
CFLAGS= -I. -I$(ORACLE_HOME)/precomp/public -I$(ORACLE_HOME)/rdbms/public -I$(ORACLE_HOME)/rdbms/demo -I$(ORACLE_HOME)/plsql/pub lic -I$(ORACLE_HOME)/network/public -I/opt/SUNW/spro/WS6/include/CC -I./ LDFLAGS= -L./ -L$(ORACLE_HOME)/lib/ -L/usr/ccs/lib -L/usr/lib -lclntsh $(ORACLE_HOME)/lib/scorept.o $(ORACLE_HOME)/lib/sscoreed. o $(ORACLE_HOME)/rdbms/lib/kpudfo.o -lclntsh $(ORACLE_HOME)/lib/nautab.o $(ORACLE_HOME)/lib/naeet.o $(ORACLE_HOME)/lib/naect.o $ (ORACLE_HOME)/lib/naedhs.o -lmm -L./ -lsocket -lnsl main: testsvr.cpp objapi.h CC -w -o testsvr $(LDFLAGS) $(OBJS) $(CFLAGS) testsvr.cpp libobjapi.a CFLAGS= -I. -I$(ORACLE_HOME)/precomp/public -I$(ORACLE_HOME)/rdbms/public -I$(ORACLE_HOME)/rdbms/demo -I$(ORACLE_HOME)/plsql/pub lic -I$(ORACLE_HOME)/network/public -I/opt/SUNW/spro/WS6/include/CC -I./ LDFLAGS= -L./ -L$(ORACLE_HOME)/lib/ -L/usr/ccs/lib -L/usr/lib -lclntsh $(ORACLE_HOME)/lib/scorept.o $(ORACLE_HOME)/lib/sscoreed. o $(ORACLE_HOME)/rdbms/lib/kpudfo.o -lclntsh $(ORACLE_HOME)/lib/nautab.o $(ORACLE_HOME)/lib/naeet.o $(ORACLE_HOME)/lib/naect.o $ (ORACLE_HOME)/lib/naedhs.o -lmm -L./ -lsocket -lnsl main: testsvr.cpp objapi.h CC -w -o testsvr $(LDFLAGS) $(OBJS) $(CFLAGS) testsvr.cpp libobjapi.a |
| Configure X Windows on Fedora 4 Geoff |
Use the configuration tool. Note that it has a bug for dual head configurations. Hit Ctrl-Alt-F1 (Ctrl-Alt-F7 to switch back to X) to display a console and login to that. Edit /etc/X11/xorg.conf Restart X using: init 3 init 5 Note for dual AGP cards, you need to type lspci and note the first column showing the address of each card. Add a line such as the following to the "Device" section of the xorg.conf file. BusID "2:0:0" If all else fails, view the log file at /var/log/Xorg.0.log |
| Control core dump location on Solaris Geoff |
Try these alternatives: coreadm –i /corefilesystem/obj/%n.%f.%p" . coreadm –e process vi /etc/coreadm.conf coreadm -p /corefilesystem/obj/%n.%f.%p $$ |
| Core dump info Geoff |
file core.1234 This will tell you the name of the executable that dumped core. strings core.1234 This will show readable text in the core dump |
| Count file handles used by a process geoff |
/usr/proc/bin/pfiles [PID] | grep "ino:" | wc -l |
| Counting open file handles Acacia |
/usr/proc/bin/pfiles <PID> | grep "ino:" | wc -l Counts the number of open file handles for the <PID>. Note that this routine may only display correct results up to 2048 file handles. |
| Count TCP connections from a PC Geoff |
netstat -an | \ grep 80 | \ awk '{print $2;}' | \ sort | \ uniq -c | \ sort | \ less |
| Create a file system Geoff |
To create a linux file system, add the drive. On the primary IDE controller, devices are /dev/hda then /dev/hdb. The secondary IDE has /dev/hdc and /dev/hdd. Use hdparm to check that the device was detected and dmesg | less to check that the drivers loaded. You may need to add boot parameters to /etc/lilo.conf as follows: append="hdc=idc-scsi" etc... Create a partition using fdisk Create a file system using mkfs -t ext3 /dev/hdx Create a mount point using mkdir /mnt/bigdisk Edit /etc/fstab to ensure that it mounts correctly using the following syntax: /dev/hdx1 /mnt/bigdisk ext3 defaults 1 2 Check the file system using fsck /dev/hdx1 |
| Crontab Acacia |
Use a line in your crontab file such as */5 * * * * /root/checklogs > /dev/null 2>&1 to run an application every 5 min and dump all stdout and stderr output (2) to the bit bucket. |
| CVS Add Directory Geoff |
To import a whole directory into CVS: cd [DIR] cvs im a/b/c [TAGHEAD] [TAGNAME} |
| CVS - Add Single File Geoff |
To add a single file to CVS: cd [DIR] cvs add [filename] cvs commit |
| Debugging DNS Geoff |
To debug problems with DNS (named daemon): rndc -? for options rndc status rndc trace 9 tail -f /var/named/named.run |
| df -kd Acacia |
Show the disk space used (k = in kB, d = same file system) in the current file system from the current directory downwards. |
| df -k Acacia |
Show the disk space used and available on all file systems. |
| Discover Linux devices and settings Geoff |
To find PCI devices lspci (or cat /proc/pci) To list all devices lsdev To see USB devices and hubs usbview To see loaded device drivers lsmod (or cat /proc/modules) To see mounted drives df -k (also cat /proc/mounts) To see details of your CPU cat /proc/cpuinfo To see your current boot command line cat /proc/cmdline To list devices using DMA cat /proc/dma To list available devices ls /proc/ide To show configured interrupts cat /proc/interrupts To show configured device memory settings cat /proc/iomem To show configured device IO ports cat /proc/ioports To see the current load average cat /proc/loadavg To see overview of installed memory usage cat /proc/meminfo To see drive partitions cat /proc/partitions To see the current IO statistics cat /proc/stat To determine the swap device cat /proc/swaps To see tty drivers cat /proc/tty/drivers To see how long your system has been running (secs) cat /proc/uptime (or uptime command) To see your version of linux uname -a (or cat /proc/version) To list your video devices cat /proc/video/dev hdparm To list hard drive details |
| Display text in large files Acacia |
grep -n "search string" largefile -- returns 112322 This is the text cat largefile | sed -n '112322,112333p' |
| Dual LAN Cards Geoff |
To setup 2 identical lan cards in the one system, boot on a dos boot disk, load your test.exe to allocate known io and irq to the cards,boot and have a look at the log using dmesg then edit /etc/modules.conf alias eth0 ne alias eth1 ne alias usb-controller usb-uhci options eth0 -o ne-0 io=0x300 irq=3 options eth1 -o ne-1 io=0x340 irq=12 Now ifup eth0 ifup eth1 or try modprobe ne eth0 io=300 irq=10 |
| <ESC> k Acacia |
Use all the normal editing keystrokes in the Korn shell. This one will recall the previous command. Use all the normal editing keystrokes in the Korn shell. This one will recall the previous command. Use <ESC> / <search string> to find a previously executed command. |
| Ethereal Packet Capture Geoff |
Use the following filter to capture traffic between servers on a port 53: iphost 192.16.62.17 and dst 192.16.62.5 and dsp port 53 |
| Find details of installed PCI devices on Linux Geoff |
Find details of installed PCI devices on Linux using the following commands: lspci -vvv cat /proc/pci |
| Find the Unix host name Acacia |
Enter the "hostname" command to show the name of the host that you are using. |
| Forced Core Dump Acacia |
To force a core dump of a running application - gcore <PID> This pauses the process for a moment while the memory image is dumped to core.<PID> and then it continues running. |
| Hex dump Geoff |
Dump a file in hex using od -x file.bin Show as octal backslash notation od -c file.bin |
| High Capacity Floppies Geoff C |
You can format a floppy to higher or lower than normal capacity with Linux as follows: # To see options cat /etc/fdprm # To set fd0 format to 1760kB (half of 3520) setfdprm /dev/fd0 3520 22 2 80 0 0x1b 0x00 0xcf 0x6c # To format the floppy using these settings fdformat /dev/fd0 |
| If then tests wc -l if [ $n -lt 3 ] then echo "Less than 3 lines in file" else echo "3 or more lines in file" fi |
Bash script... n=cat testfile |
| Import directory into CVS Geoff |
To import an entire directory into a specific location in a cvs repository (a/b/c) cd __target_dir___ cvs im a/b/c KEY HEAD To add a single file cd __target_dir____ cvs add __filename__ cvs commit |
| Kill many named processes Acacia |
To kill many processes called <name> kill 'ps -ef | grep <name> | grep -v grep | awk '{ print $2 }'' |
| Looping in ksh Acacia |
#!/bin/ksh DIRS='ls *.log' for DIR in $DIRS do echo "Processing $DIR" don |
| ls -l Acacia |
Show the contents of the current directory in long format sorted by file size. |
| Monitor memory, CPU and I/O statistics Acacia |
vmstat 1 or iostat or sar |
| Mount disk image Geoff |
In order to mount and view a disk image: mkdir /mnt/tmp mount -o loop /tmp/boot.img /mnt/tmp ls -ltr /mnt/tmp |
| Mount windows disk on unix Geoff |
# Mount windows share on Unix system smbmount //MYWINNT/Docs /mnt/unixdirname -o username=windozeuser,password="fred" # Copy document from Unix to Windows cp /tmp/obj/published/A444 /mnt/unixdirname # Unmount windows box umount /mnt/unixdirname |
| netstat -an | grep 8030 Acacia |
Show what is happening on TCPIP port 8030. |
| Network Port Scanning Geoff |
If you have a Linux system, you can easily locate all computers on your network using nmap as follows: nmap -sP 192.16.60.* |
| Network timeouts a |
To set the TCP/IP network timeout: /usr/sbin/ndd -set /dev/tcp tcp_fin_wait_2_flush_interval 30000 /usr/sbin/ndd -set /dev/tcp tcp_time_wait_interval 30000 |
| nslookup 1.2.3.4 Acacia |
Show the DNS name of the IP address. |
| Operating system settings Acacia |
ulimit -a will show the current file handle and memory limits per process. To change the limits, alter the /etc/system file or place entries in the script file used to start the application. |
| pgrep Mark |
Instead of using ps -ef | grep <name> | grep -v grep | awk '{print $2}' you could try pgrep <name> pgrep will return the PIDs of the processes that match the selection criteria |
| Reset root password geoff |
In order to reset the root password on a linux system that you have physical access to: - reboot - Ctrl-X (at the kernel selection screen) - linux 1 (to boot to single user mode) - passwd root - reboot |
| Running jobs from cron Geoff |
Cron jobs have no initial environment. The best way to run a task is to have cron call a job.ksh file containing the following that will establish the environment and log the output. . ~/.profile exec > $0.log 2>&1 cd /mydir ./myjob ./myini.ini |
| Secure Copy Geoff |
scp fromuser@fromhost:frompath touser@tohost:topath |
| Set file handle limit Geoff |
ulimit -Sn 8192 |
| Show commands executing in a shell script Geoff |
#!/usr/bin/ksh set -x |
| Show CPU Details on Solaris Geoff |
kstat -n cpu_info0 |
| Signals Geoff |
SIGHUP 1 --> hangup SIGINT 2 --> interrupt (rubout) SIGQUIT 3 --> quit (ASCII FS) SIGILL 4 --> illegal instruction (not reset when caught) SIGTRAP 5 --> trace trap (not reset when caught) SIGIOT 6 --> IOT instruction SIGABRT 6 --> used by abort, replace SIGIOT in the future SIGEMT 7 --> EMT instruction SIGFPE 8 --> floating point exception SIGKILL 9 --> kill (cannot be caught or ignored) SIGBUS 10 --> bus error SIGSEGV 11 --> segmentation violation SIGSYS 12 --> bad argument to system call SIGPIPE 13 --> write on a pipe with no one to read it SIGALRM 14 --> alarm clock SIGTERM 15 --> software termination signal from kill SIGUSR1 16 --> user defined signal 1 SIGUSR2 17 --> user defined signal 2 SIGCLD 18 --> child status change SIGCHLD 18 --> child status change alias (POSIX) SIGPWR 19 --> power-fail restart SIGWINCH 20 --> window size change SIGURG 21 --> urgent socket condition SIGPOLL 22 --> pollable event occured SIGIO SIGPOLL --> socket I/O possible (SIGPOLL alias) SIGSTOP 23 --> stop (cannot be caught or ignored) SIGTSTP 24 --> user stop requested from tty SIGCONT 25 --> stopped process has been continued SIGTTIN 26 --> background tty read attempted SIGTTOU 27 --> background tty write attempted SIGVTALRM 28 --> virtual timer expired SIGPROF 29 --> profiling timer expired SIGXCPU 30 --> exceeded cpu limit SIGXFSZ 31 --> exceeded file size limit SIGWAITING 32 --> process's lwps are blocked SIGLWP 33 --> special signal used by thread library SIGFREEZE 34 --> special signal used by CPR SIGTHAW 35 --> special signal used by CPR SIGCANCEL 36 --> thread cancellation signal used by libthread SIGLOST 37 --> resource lost (eg, record-lock lost) |
| SMTP and POP Syntax Geoff |
If you want to debug your mail server, you can use telnet as follows. For POP use: telnet yourpophostname 110 user geoff pass xxxxxx stat list retr msg# top msg# #lines dele msg# rset quit For SMTP telnet yoursmtphost 25 help helo mydomain.com rcpt from geoff@mydomain.com rcpt to tony@otherdomain.com data This is the message and ends with the following line. . quit |
| Solaris process details Geoff |
pgrep -f searchitem -l |
| Solaris Processor Groups geoff |
# Query the current processor sets echo "List of currently active processor sets" psrset -i # Create a processor set for this application psrset -c # Determine the number of the created set GROUP='psrset -i | grep empty | head -1 | awk '{print $4;}' | cut -d":" -f1' # Add CPUs to the set echo "Adding processors to set [$GROUP]" psrset -a $GROUP 0 psrset -a $GROUP 1 # Launch application on this processor set psrset -e $GROUP ./myapp ...... |
| TCP/IP Timeouts Acacia |
Use the following settings to set the network timeout to 30 sec on Solaris /usr/sbin/ndd -set /dev/tcp tcp_fin_wait_2_flush_interval 30000 /usr/sbin/ndd -set /dev/tcp tcp_time_wait_interval 30000 See http://director.enhydra.org/software/documentation/README-TUNING.html |
| Tivolli Backup and Restore Geoff |
Once you are in the dsmc interface you can type help, or I have been using the command: res <filename> -fromnode=dogmatix -sub=yes "<output file spec>" where <filename> is the fully qualified path of the file to be restored and <output file spec> is the fully qualified name of the output file. Note that the output file spec is in double quotes. and res -fromnode=dogmatix -sub=yes "/<dirname>/*" "/<output dirname>/" where <dirname> is the name of the directory to be restored and <output dirname> is the name of the output directory. N.B. It is important to have the quotes and the "/" in place to ensure the command works. |
| top Acacia |
Get this great utility to monitor CPU performance from http://www.groupsys.com/topinfo here |
| top Acacia |
Get this great utility to monitor CPU performance from http://www.groupsys.com/topinfo here |
| top Acacia |
Get this great utility to monitor CPU performance from http://www.groupsys.com/topinfo here |
| Translate reserved characters geoff |
To remove ascii 240 characters from a file use: cat sourcefile | tr '\240' ' ' > filteredfile |
| Unix O/S version information Acacia |
uname -a |
| Use ksh with history geoff |
VISUAL=vi export VISUAL exec ksh |
| Using Acacia Tips RSS Feed Geoff |
Go to http://www.acacialt.com.au/cgi/showtip.cgi?tipid=1 to show tip 1. Change the tip number to see other tips or hook up our news feed at http://www.acacialt.com.au/cgi/rss.cgi Read more about RSS at http://www.acacialt.com.au/Projects/rss.htm |
| VI Configuration Acacia |
To set 2 char tabs and auto indent for vi, add a .exrc file to your home directory that contains: "Default tab for 2 set ts=2 set ai set sw=2 Note that comments start with double quotes. |
| view filename Acacia |
This is a read-only version of the popular vi utility. This is a read-only version of the popular vi utility. Use it to ensure that you do not change the source file accidentally. |
| Virtual memory size of a process Geoff |
ps -o vsz -p <PID> |
| vncview on Linux Geoff |
When using tightvnc on linux to attach to a screen locked Windows system that is running vncserver, you cannot hit Ctrl-Alt-Del to get a login prompt. Try also holding the shift key. This works. |
| who am i Acacia |
Show who you are logged in as. |
| X-Display Setup Geoff |
To display the output of an X terminal session such as xload, xterm or xcalc, determine your PCs IP address using ipconfig. On the Unix box, type the following: export DISPLAY=10.0.5.64:0.0 xload & |
| X Windows Geoff |
Start X windows using "startx" or "X -xf86config myxf86config-4.file" Disable X windows from starting automatically by commenting out the following line in /etc/inittab "x:5:respawn:/etc/X11/prefdm -nodaemon" Stop X windows by changing to init level 3 init 3 Email me to get a sample XF86Config-4 file that supports multiple cards and monitors on Linx |
Click here to add your own tips.