Linux Networking Jump Start

22 11 2010

Configure ethernet interfaces using text files in the /etc/sysconfig/network-scripts/ directory:

[root@vvx7 ~]# cd /etc/sysconfig/network-scripts/
[root@vvx7 network-scripts]# vi ifcfg-eth0

Example configuration file shown below (static IP address):

# Intel Corporation 82575EB Gigabit Network Connection
DEVICE=eth0
HWADDR=00:21:28:57:42:02
ONBOOT=yes
BOOTPROTO=none
IPADDR=172.27.5.107
NETMASK=255.255.255.0
GATEWAY=172.27.5.1
USERCTL=no

Example configuration file shown below (dynamic IP address):

# Intel Corporation 82575EB Gigabit Network Connection
DEVICE=eth0
HWADDR=00:21:28:57:42:02
ONBOOT=yes
BOOTPROTO=dhcp
USERCTL=no

Update the DNS servers and the search domain using the /etc/resolv.conf file:

[root@vvx5 /]# vi /etc/resolv.conf 
search domain.co.uk.local
nameserver 172.27.2.2
nameserver 195.40.1.36
nameserver 212.158.248.6
nameserver 194.72.6.51

Print the current routing table using the route command:

[root@vvx6 ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
172.27.5.0      *               255.255.255.0   U     0      0        0 bond0
link-local      *               255.255.0.0     U     1004   0        0 bond0
default         172.27.5.1      0.0.0.0         UG    0      0        0 bond0
[root@vvx6 ~]#

The route command can also be used to add or delete a route to or from the routing table. Note this does not persist and lasts only to the next network restart.

[root@vvx6 ~]# route add -net 192.168.1.0 netmask 255.255.255.0 gw 172.27.5.10
[root@vvx6 ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.1.0     172.27.5.10     255.255.255.0   UG    0      0        0 bond0
172.27.5.0      *               255.255.255.0   U     0      0        0 bond0
link-local      *               255.255.0.0     U     1004   0        0 bond0
default         172.27.5.1      0.0.0.0         UG    0      0        0 bond0
[root@vvx6 ~]# route del -net 192.168.1.0 netmask 255.255.255.0
[root@vvx6 ~]#

It’s also possible to set the default gateway using the route command:

[root@vvx6 ~]# route add default gw 10.10.10.1

Add a persistent static route to an interface using a new text file in the same directory, called route-. Eg;

[root@vvx7 network-scripts]# cat route-eth0 
172.27.2.0/24 via 172.27.5.1
10.128.2.0/24 via 172.27.5.1
[root@vvx7 network-scripts]#

To look at the running network configuration for one or all the interfaces, use the ifconfig command:

[root@vvx6 network-scripts]# ifconfig
bond0     Link encap:Ethernet  HWaddr 00:21:28:57:41:22  
          inet addr:172.27.5.106  Bcast:172.27.5.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MASTER MULTICAST  MTU:1500  Metric:1
          RX packets:142897 errors:0 dropped:0 overruns:0 frame:0
          TX packets:296926 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:32370272 (30.8 MiB)  TX bytes:345828268 (329.8 MiB)

eth0      Link encap:Ethernet  HWaddr 00:21:28:57:41:22  
          UP BROADCAST RUNNING SLAVE MULTICAST  MTU:1500  Metric:1
          RX packets:142897 errors:0 dropped:0 overruns:0 frame:0
          TX packets:296926 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:32370272 (30.8 MiB)  TX bytes:345828268 (329.8 MiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:5570451 errors:0 dropped:0 overruns:0 frame:0
          TX packets:5570451 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:1171684633 (1.0 GiB)  TX bytes:1171684633 (1.0 GiB)

[root@vvx6 network-scripts]# ifconfig bond0
bond0     Link encap:Ethernet  HWaddr 00:21:28:57:41:22  
          inet addr:172.27.5.106  Bcast:172.27.5.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MASTER MULTICAST  MTU:1500  Metric:1
          RX packets:143026 errors:0 dropped:0 overruns:0 frame:0
          TX packets:296992 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:32384633 (30.8 MiB)  TX bytes:345837192 (329.8 MiB)

[root@vvx6 network-scripts]# 

The ifconfig command can also be used to bring an interface up or down.

[root@vvx7 network-scripts]# ifconfig eth3 down
[root@vvx7 network-scripts]# ifconfig eth3 up

It can also be used to set an IP address on an interface (again this won’t persist beyond a network reload):

[root@vvx6 ~]# ifconfig eth1 192.168.10.99 netmask 255.255.255.0 up
[root@vvx6 ~]# ifconfig eth1
eth1      Link encap:Ethernet  HWaddr 00:21:28:57:41:23  
          inet addr:192.168.10.99  Bcast:192.168.10.255  Mask:255.255.255.0
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

[root@vvx6 ~]#

Restart the networking service to reload changed ifcfg files. This will disconnect your SSH session but only temporarily unless you change the interface you’re connected to. It’s worth noting that if you delete a configuration file, and restart the networking service, the deleted interface will not go down, this can cause issues if you need to reuse the same IP address elsewhere. (In this instance use ifconfig to bring the interface down):

[root@vvx6 network-scripts]# service network restart
Shutting down interface bond0:                             [  OK  ]
Shutting down loopback interface:                          [  OK  ]
Bringing up loopback interface:                            [  OK  ]
Bringing up interface bond0:                               [  OK  ]
[root@vvx6 network-scripts]#

Combine the ifconfig command with grep to get only the details you’re interested in, for example show all active IP addresses:

[root@vvx6 network-scripts]# ifconfig | grep inet
          inet addr:172.27.5.106  Bcast:172.27.5.255  Mask:255.255.255.0
          inet addr:127.0.0.1  Mask:255.0.0.0
[root@vvx6 network-scripts]#

Linux has all the usual network utilities that you’d expect including ping for connectivity testing (Use ‘Ctl+c’ to stop the ping):

[root@vvx6 ~]# ping vvx1
PING vvx1.local (172.27.5.101) 56(84) bytes of data.
64 bytes from vvx1.local (172.27.5.101): icmp_req=1 ttl=64 time=1.02 ms
64 bytes from vvx1.local (172.27.5.101): icmp_req=2 ttl=64 time=0.175 ms
64 bytes from vvx1.local (172.27.5.101): icmp_req=3 ttl=64 time=0.161 ms
^C
--- vvx1.local ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.161/0.453/1.023/0.403 ms
[root@vvx6 ~]# 

As well as traceroute:

[root@vvx6 ~]# traceroute 172.27.3.2
traceroute to 172.27.3.2 (172.27.3.2), 30 hops max, 60 byte packets
 1  172.27.5.1 (172.27.5.1)  0.485 ms  0.965 ms  1.176 ms
 2  asterisk.domain.co.uk.local (172.27.3.2)  0.317 ms  0.317 ms  0.309 ms
[root@vvx6 ~]#

Another useful utility, especially if you’re not physically at the server, is the ethtool command. It can show you if the interface is up or if it hasn’t detected a link.:

[root@vvx6 ~]# ethtool eth0
Settings for eth0:
	Supported ports: [ TP ]
	Supported link modes:   10baseT/Half 10baseT/Full 
	                        100baseT/Half 100baseT/Full 
	                        1000baseT/Full 
	Supports auto-negotiation: Yes
	Advertised link modes:  10baseT/Half 10baseT/Full 
	                        100baseT/Half 100baseT/Full 
	                        1000baseT/Full 
	Advertised pause frame use: No
	Advertised auto-negotiation: Yes
	Speed: 1000Mb/s
	Duplex: Full
	Port: Twisted Pair
	PHYAD: 1
	Transceiver: internal
	Auto-negotiation: on
	MDI-X: Unknown
	Supports Wake-on: pumbg
	Wake-on: g
	Current message level: 0x00000007 (7)
	Link detected: yes
[root@vvx6 ~]#

Configure a VLAN interface by copying an existing ifcfg file, it’s normal to name the configuration file after the VLAN tag:

[root@vvx6 network-scripts]# cp ifcfg-eth1 ifcfg-eth1.100
[root@vvx6 network-scripts]# vi ifcfg-eth1.100

Sample VLAN 100 ifcfg file (after creating this file you’ll likely want to restart the network service):

DEVICE="eth1.100"
HWADDR="00:21:28:57:41:23"
ONBOOT="yes"
IPADDR="172.27.15.200"
NETMASK="255.255.255.0"
GATEWAY="172.27.15.1"
VLAN="yes"

Another useful utility is to be able to capture and display packets sent or received on a particular interface (the -q in this instance means quiet, essentially one line per packet, however you can use -vvv which produces verbose output):

[root@vvx6 network-scripts]# tcpdump -i eth0 -q
tcpdump: WARNING: eth0: no IPv4 address assigned
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
20:08:43.219785 IP vvx6.local.ssh > 10.128.2.6.55298: tcp 96
20:08:43.226684 IP vvx6.local.43536 > vvx.local.domain: UDP, length 41
20:08:43.226760 IP vvx6.local.ssh > 10.128.2.6.55298: tcp 192
20:08:43.241811 IP vvx.local.domain > vvx6.local.43536: UDP, length 41
20:08:43.242008 IP vvx6.local.60369 > vvx.local.domain: UDP, length 41
20:08:43.242225 IP vvx.local.domain > vvx6.local.60369: UDP, length 114
20:08:43.318535 IP 10.128.2.6.55298 > vvx6.local.ssh: tcp 0
20:08:43.318564 IP vvx6.local.ssh > 10.128.2.6.55298: tcp 864
20:08:43.319797 IP vvx6.local.ssh > 10.128.2.6.55298: tcp 272
20:08:43.336950 IP 10.128.2.6.55298 > vvx6.local.ssh: tcp 0
20:08:43.336976 IP vvx6.local.ssh > 10.128.2.6.55298: tcp 160
20:08:43.337788 IP vvx6.local.ssh > 10.128.2.6.55298: tcp 272
20:08:43.342973 IP 10.128.2.6.55298 > vvx6.local.ssh: tcp 0
20:08:43.343000 IP vvx6.local.ssh > 10.128.2.6.55298: tcp 160
20:08:43.343797 IP vvx6.local.ssh > 10.128.2.6.55298: tcp 272
20:08:43.397405 IP 10.128.2.6.55298 > vvx6.vvxlocal.ssh: tcp 48
^C
16 packets captured
16 packets received by filter
0 packets dropped by kernel
[root@vvx6 network-scripts]#

Network performance can be monitored using ifconfig however this only gives the rx and tx bytes since the service started, to find out the current bandwidth being used, use the nload command (Use the q key to exit). This command shows the load for eth0 and I’ve specified that it’s displayed in MB.

[root@vvx5 /]# nload eth0 -u m



Another useful utility to have in your toolkit is the nmap package. The nmap package is a very useful tool and can do some pretty complex jobs. I find it most useful for finding out what’s on the current LAN:

[root@ftp ~]# nmap -sP 172.27.2.101-150

Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2010-11-22 21:22 GMT
Host 172.27.2.107 appears to be up.
Host bti-9108.domain.local (172.27.2.108) appears to be up.
Host bti-9114.domain.local (172.27.2.114) appears to be up.
Nmap finished: 50 IP addresses (3 hosts up) scanned in 3.231 seconds
[root@ftp ~]#

Occasionally the local firewall gives network issues, so it’s useful to see what’s allowed and what’s not. I’ll not go into much detail here, but generally the INPUT policy is the one which monitors inbound connections and prevents connections when something doesn’t work:

[root@vvx6 ~]# iptables -S INPUT
-P INPUT ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT 
-A INPUT -p icmp -j ACCEPT 
-A INPUT -i lo -j ACCEPT 
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT 
-A INPUT -p udp -m state --state NEW -m udp --dport 68 -j ACCEPT 
-A INPUT -p udp -m state --state NEW -m udp --dport 161 -j ACCEPT 
-A INPUT -p udp -m state --state NEW -m udp --dport 162 -j ACCEPT 
-A INPUT -p tcp -m state --state NEW -m tcp --dport 199 -j ACCEPT 
-A INPUT -p tcp -m state --state NEW -m tcp --dport 2812 -j ACCEPT 
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT 
-A INPUT -p tcp -m state --state NEW -m tcp --dport 9998 -j ACCEPT 
-A INPUT -p tcp -m state --state NEW -m tcp --dport 10000 -j ACCEPT 
-A INPUT -p udp -m state --state NEW -m udp --dport 1620 -j ACCEPT 
-A INPUT -j REJECT --reject-with icmp-host-prohibited 
[root@vvx6 ~]#

When inserting a rule into the iptables package it’s probably easiest to add the rule to the top (that way your rule won’t be blocked by a higher priority rule, because yours is checked first). Note this rule will not persist and therefore not appear after an iptables reload (see the next command if you want to save the ruleset). This example allows UDP port 8888 to be connected to by any source IP address:

[root@vvx6 ~]# iptables -I INPUT -p udp -m state --state NEW -m udp --dport 8888 -j ACCEPT

To persist rules (if they appear correct) it’s best to run the command:

[root@vvx6 ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]
[root@vvx6 ~]#

It’s also possible to modify the rule list by editing the file /etc/sysconfig/iptables:

[root@vvx6 ~]# vi /etc/sysconfig/iptables




Linux Basics Jump Start

22 11 2010

Use tab to complete a command. If the text entered is ambiguous then press tab again to see the options, or just complete more of the command or filename.

[root@vvx6 ~]# less install.log
install.log         install.log.syslog  

Find out where you are in the filesystem using the pwd. Move around the filesystem using the cd command.

[root@vvx6 ~]# pwd
/root
[root@vvx6 ~]# cd /tmp/
[root@vvx6 tmp]# pwd
/tmp
[root@vvx6 tmp]#

To move upwards a directory use cd and move to ../

[root@vvx6 tmp]# pwd
/tmp
[root@vvx6 tmp]# cd ../
[root@vvx6 /]# pwd
/
[root@vvx6 /]#

Copy or backup files using the cp command.

[root@vvx6 ~]# cp install.log copied.log

Rename or move a file using the mv command.

[root@vvx6 ~]# mv copied.log moved.log

Delete a file using the rm command.

[root@vvx6 ~]# rm moved.log 
rm: remove regular file `moved.log'? y

Help can be found for a command using the man package.

[root@vvx6 ~]# man ping

The asterisk character is expanded by the command shell before the command is run, so it’s useful when dealing with files of similar name.

Eg; List all the files in the /etc/sysconfig/network-scripts/ directory that begin with the string “ifcfg-“:

[root@vvx6 ~]# ls -l /etc/sysconfig/network-scripts/ifcfg-*
-rw-r--r--  1 root root 125 Nov 11 17:36 /etc/sysconfig/network-scripts/ifcfg-bond0
-rw-r--r--  1 root root 109 Nov 11 17:36 /etc/sysconfig/network-scripts/ifcfg-eth0
-rw-r--r--. 1 root root  73 Nov 11 17:19 /etc/sysconfig/network-scripts/ifcfg-eth1
-rw-r--r--. 1 root root 254 Sep 27 20:45 /etc/sysconfig/network-scripts/ifcfg-lo
[root@vvx6 ~]#

The grep package is a very powerful search tool that accepts regular expressions and can do very complex searches, however it’s useful as a general finder tool when searching for simple text.

Eg; Find all instances of the text string “kernel” in the install.log file:

[root@vvx6 ~]# grep kernel install.log
Installing kernel-devel-2.6.35.6-45.fc14.x86_64
Installing kernel-headers-2.6.35.6-45.fc14.x86_64
Installing kernel-2.6.35.6-45.fc14.x86_64
[root@vvx6 ~]#

Eg; Find all instances of the text string “IPADDR” in the /etc/sysconfig/network-scripts/ directory where the filenames begin “ifcfg-“:

[root@vvx6 ~]# grep IPADDR /etc/sysconfig/network-scripts/ifcfg-*
/etc/sysconfig/network-scripts/ifcfg-bond0:IPADDR="172.27.5.106"
/etc/sysconfig/network-scripts/ifcfg-lo:IPADDR=127.0.0.1
[root@vvx6 ~]# 

Eg; Same as above but list the line number where the text string is found:

[root@vvx6 ~]# grep -n IPADDR /etc/sysconfig/network-scripts/ifcfg-*
/etc/sysconfig/network-scripts/ifcfg-bond0:6:IPADDR="172.27.5.106"
/etc/sysconfig/network-scripts/ifcfg-lo:2:IPADDR=127.0.0.1
[root@vvx6 ~]#

Use the | (pipe character) to concatenate the commands together. This allows the output of the first command to be the input to the next command. eg

[root@vvx6 ~]# ls -l | grep iso
-rw-r--r--  1 root root 3520802816 Nov 11 17:20 Fedora-14-x86_64-DVD.iso
[root@vvx6 ~]#

Use the commands head and tail to look at the top and bottom of a file (the default is to show 10 lines). eg;

[root@vvx6 ~]# head install.log
Installing libgcc-4.5.1-4.fc14.x86_64
warning: libgcc-4.5.1-4.fc14.x86_64: Header V3 RSA/SHA256 Signature, key ID 97a1071f: NOKEY
Installing setup-2.8.23-1.fc14.noarch
Installing filesystem-2.4.35-1.fc14.x86_64
Installing ca-certificates-2010.63-3.fc14.noarch
Installing basesystem-10.0-3.noarch
Installing poppler-data-0.4.3-1.fc14.noarch
Installing mailcap-2.1.34-1.fc14.noarch
Installing xkeyboard-config-1.9-6.fc14.noarch
Installing ncurses-base-5.7-8.20100703.fc14.x86_64
[root@vvx6 ~]# tail install.log
Installing dracut-006-3.fc14.noarch
Installing kernel-2.6.35.6-45.fc14.x86_64
Installing system-setup-keyboard-0.8.6-2.fc14.1.x86_64
Installing xorg-x11-server-Xorg-1.9.0-15.fc14.x86_64
Installing systemtap-runtime-1.3-2.fc14.x86_64
Installing systemtap-1.3-2.fc14.x86_64
Installing freenx-server-0.7.3-18.fc13.x86_64
Installing autofs-5.0.5-28.fc13.x86_64
Installing sysprof-1.1.6-1.fc14.x86_64
*** FINISHED INSTALLING PACKAGES ***
[root@vvx6 ~]#

Use the less command to read a whole file (use the q key to exit out of the file):

[root@vvx6 ~]# less anaconda.log

# Kickstart file automatically generated by anaconda.

#version=DEVEL
install
cdrom
# System services
services --disabled="autofs,avahi-daemon,cups,mdmonitor,netfs,portreserve,sendmail" --enabled="network"

lang en_US.UTF-8
keyboard uk
network --onboot no --device eth0 --noipv4 --noipv6
network --onboot no --device eth1 --noipv4 --noipv6
timezone Europe/Dublin
rootpw  --iscrypted $6$8k/y9KnwZPr3.r88$R5vbCD4SIadfadfaeee452gfsy44
selinux --disabled
authconfig --enableshadow --passalgo=sha512 --enablefingerprint
firewall --port=68:udp --port=161:udp --port=162:udp --port=199:tcp --port=2812:tcp --service=ssh
# The following is the partition information you requested
# Note that any partitions you deleted are not expressed
# here so unless you clear all partitions first, this is
# not guaranteed to work
#clearpart --all --drives=sda
#ignoredisk --only-use=sda
anaconda-ks.cfg 

It’s also possible to print out a complete file to the screen, this is useful if the file is only a few lines long. The cat command will print out multiple files but works just as well with a single file (it’s also really useful to cat and file and use the output in another command using the | character):

[root@vvx5 network-scripts]# cat ifcfg-eth0
# Intel Corporation 82575EB Gigabit Network Connection
DEVICE=eth0
BOOTPROTO=static
HWADDR=00:14:4F:CA:84:CC
ONBOOT=yes
IPADDR=172.27.5.105
NETMASK=255.255.255.0
[root@vvx5 network-scripts]#

Use the history to view previously entered command (including incorrect ones!). It’s useful to use the grep tool to search for a particular command. It’s also possible to rerun a particular command using the number beside the command.

Eg; Show the last 10 commands entered:

[root@vvx6 ~]# history | tail
  718  service monit stop
  719  service monit start
  720  vi /etc/monit.conf 
  721  service monit reload
  722  monit status
  723  date
  724  exit
  725  ls
  726  ls -l | grep iso
  727  history | tail
[root@vvx6 ~]#

Eg; Show the commands that include the string “install”:

[root@vvx6 ~]# history | grep install
  115  yum install genisoimage
  127  cd installdir/
  132  cd installdir/
  133  ./emsinstall.sh 
  135  mvn clean install
  137  mvn install clean
  144  cd installdir/
  147  ./emsinstall.sh 
  173  mvn install -Pproduction
  175  mvn clean install -Pproduction   
  732  history | grep install
[root@vvx6 ~]#

Eg; Show the commands that include the string “filesystems” and rerun the command at position 713 in the command history:

[root@vvx6 monit.d]# history | grep filesystems
  673  scp filesystems.inc crond.inc net-snmp.inc sshd.inc padmin@cache
  674  scp filesystems.inc crond.inc net-snmp.inc sshd.inc padmin@cache:/tftpboot/
  713  cat filesystems.inc 
  739  history | grep filesystems
  742  history | grep filesystems
[root@vvx6 monit.d]# !713
cat filesystems.inc 
check filesystem rootfs with path /
if space usage > 75% for 5 times within 10 cycles then alert
group filesystems

check filesystem backupfs with path /backups
if space usage > 75% for 5 times within 10 cycles then alert
group filesystems

check filesystem logfs with path /var/log
if space usage > 75% for 5 times within 10 cycles then alert
group filesystems
[root@vvx6 monit.d]#

Use the watch command to rerun a command periodically (the default is every 2 seconds):

Eg; Get the size of the Fedora-14.iso file every 2s:

[root@vvx6 ~]# watch du -h Fedora-14.iso

Every 2.0s: du -h Fedora-14.iso                                                                 Mon Nov 22 16:09:05 2010

1.2G    Fedora-14.iso

Eg; As above only changing the

Cancel a running command using “Ctl+c”.

[root@vvx6 ~]# cp Fedora-14-x86_64-DVD.iso Fedora-14.iso
^C
[root@vvx6 ~]#

Put a job in the background using the & character (the job PID number is shown along with the background job number)

[root@vvx6 ~]# cp Fedora-14-x86_64-DVD.iso Fedora-14.iso &
[1] 5240
[root@vvx6 ~]#

Alternatively you can pause the current job using “Ctl+z” and then put in the background using the bg command (Note if a command finishes in the background, it will automatically report that is the case when the next command is entered):

[root@vvx6 ~]# cp Fedora-14-x86_64-DVD.iso Fedora-14.iso
^Z
[1]+  Stopped                 cp -i Fedora-14-x86_64-DVD.iso Fedora-14.iso
[root@vvx6 ~]# bg
[1]+ cp -i Fedora-14-x86_64-DVD.iso Fedora-14.iso &
[root@vvx6 ~]#

You can also list running background jobs using the jobs command:

[root@vvx6 ~]# cp Fedora-14-x86_64-DVD.iso Fedora-14.iso &
[2] 5407
[root@vvx6 ~]# jobs
[1]+  Stopped                 cp -i Fedora-14-x86_64-DVD.iso Fedora-14.iso
[2]-  Running                 cp -i Fedora-14-x86_64-DVD.iso Fedora-14.iso &
[root@vvx6 ~]#

You can also bring a background job back into the foreground by running the fg command (this is useful if you want to stop a background job). Note that the fg command also can take a job number:

[root@vvx6 ~]# cp Fedora-14-x86_64-DVD.iso Fedora-14.iso &
[1] 5363
[root@vvx6 ~]# fg
cp -i Fedora-14-x86_64-DVD.iso Fedora-14.iso
^C
[root@vvx6 ~]#

[root@vvx6 ~]# cp Fedora-14-x86_64-DVD.iso Fedora-14.iso &
[2] 5407
[root@vvx6 ~]# jobs
[1]+  Stopped                 cp -i Fedora-14-x86_64-DVD.iso Fedora-14.iso
[2]-  Running                 cp -i Fedora-14-x86_64-DVD.iso Fedora-14.iso &
[root@vvx6 ~]# fg 1
cp -i Fedora-14-x86_64-DVD.iso Fedora-14.iso
^C
[root@vvx6 ~]#

The package vi is used to modify text files. It has 2 main modes of operation, insert mode (in which normal changes can be made) and command mode (where more complex searches and edits can be achieved). It starts in command mode by default, to move to insert mode press i. To leave insert mode and enter command mode press ESC. Open a file using vi:

[root@vvx6 ~]# vi test.file

To enter or modify text press i and enter insert mode.

testing one two
~                                                                                                                       
~                                                                                                                       
~ 
-- INSERT --

To save the file, move to command mode using ESC then type : followed by a w and then a q (this is write and quit) and press the enter key:

testing one two
~                                                                                                                       
~                                                                                                                       
:wq

To close a file without saving the changes enter the command :q!

testing one two
~                                                                                                                       
~                                                                                                                       
:q!

To search within a text file in vi using the / character.

testing one two                                                                                                               
~                                                                                                                
~                                                                                                                
/one

Useful keys to use in vi are (all are input in command mode but without the : character, case is also important):

'I' - insert at the start of the line in insert mode
'A' - append at the end of line in insert mode
'Ctl+f' - page forward
'Ctl+b' - page backwards
'w' - move forwards a word
'b' - move backwards a word
'cw' - change the word under the cursor
'dw' - delete the word under the cursor
'dd' - delete the line under the cursor
'.' - repeat the last text altering insert
'yy' - copy the line under the cursor to the clipboard
'p' - paste the contents of the clipboard on the line below the cursor
'P' - paste the contents of the clipboard on the line above the cursor
'G' - go to the last line in the file
'1G' - go to the first line (number in front of command repeats the command)
'10G' - go to line number 10
'/walrus' - search forwards for the word 'walrus'
'?walrus' - search backwards for the word 'walrus'
'n' - go to next instance of search text
'N' - go back to the last instance of search text

Install a new software package:

[root@vvx5 ~]# yum install yum-versionlock 
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * addons: mirrors.dedipower.com
 * base: mirrors.dedipower.com
 * extras: mirrors.dedipower.com
 * rpmforge: apt.sw.be
 * updates: mirrors.dedipower.com
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package yum-versionlock.noarch 0:1.1.16-14.el5.centos.1 set to be updated
--> Finished Dependency Resolution

Dependencies Resolved

========================================================================================================================
 Package                        Arch                  Version                                 Repository           Size
========================================================================================================================
Installing:
 yum-versionlock                noarch                1.1.16-14.el5.centos.1                  base                 15 k

Transaction Summary
========================================================================================================================
Install      1 Package(s)         
Update       0 Package(s)         
Remove       0 Package(s)         

Total download size: 15 k
Is this ok [y/N]: y
Downloading Packages:
yum-versionlock-1.1.16-14.el5.centos.1.noarch.rpm                                                |  15 kB     00:00     
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing     : yum-versionlock                                                                                  1/1 

Installed:
  yum-versionlock.noarch 0:1.1.16-14.el5.centos.1                                                                       

Complete!
[root@vvx5 ~]# 

Search for the name of a new software package:

[root@vvx5 ~]# yum search yum-versionlock
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * addons: mirrors.dedipower.com
 * base: mirrors.dedipower.com
 * extras: mirrors.dedipower.com
 * rpmforge: apt.sw.be
 * updates: mirrors.dedipower.com
=============================================== Matched: yum-versionlock ===============================================
yum-versionlock.noarch : Yum plugin to lock specified packages from being updated
[root@vvx5 ~]#

Better not forget how to reboot the server:

[root@vvx5 ~]# reboot

Or how to power the server off completely:

[root@vvx5 ~]# poweroff




Test Cache URLs

6 10 2010

Cached Videos

Cracking Contraptions

Motorcycle Stunts

Green Day

Pete Date and the Dealmakers

Rally Car Fun

Cached Images

Avon FR Football

NASA Image

Hubble Image

Clifton Beach

Moab Arch

Cached Flash Games

Game #1

Game #2

Cached App Downloads

Firefox for MacOSX





Useful greps (briefly)

8 07 2010

To look for a particular string in the current directory:

$grep <search-text> *

Using the ‘-l’ option the command only lists the filenames that have at least one instance of the string.

$grep -l <search-text> *

To look for a particular string in all the files in the current directory and sub-directories:

$grep -r <search-text>

Of course the ‘-l’ and ‘-r’ can be combined to give us the filenames that include at least one instance of the string in the current directory or sub-directories:

$grep -lr <search-text>




Extend an LVM root filesystem

12 04 2010

I’d previously posted how to extend a linux partition that wasn’t part of a logical volume, so I thought I’d probably best document what happens if it is (besides RH and CentOS both default to logical volumes, so it’s probably more useful).  It’s interesting to note that no reboot is necessary for this process to happen (in fact no services even need to be taken down).  Now down to business…

First we ensure we have something to extend over, either a partition on an existing drive or another hard disk.  To do this we can look at the command:

#fdisk -l

This should show a breakdown of all the physical disks in the system, as well as the partitions on those disks.  In my instance I wanted to extend my / filesystem over a new partition on an existing disk (I’d just increased the provisioned space on the disk in ESX as this was a VM).  My machine had only one disk /dev/sda which had 3 existing partitions, shown in fdisk as:

Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14        2610    20860402+  8e  Linux LVM
/dev/sda3            2611        5221    20972857+  8e  Linux LVM

As I’d extended the size of /dev/sda I knew that after the end of the third partition there was free space, so I needed to create a new partition for which to extend my / filesystem over.  To do this, I entered the following:

#fdisk /dev/sda

n (to create a new partition)

p (to create a primary partition, if you've 4 or more partitions you'll have to choose extended)

4 (the number of the new partition, this is important you'll need this later)

After this questions fdisk asks what cylinders to start and finish on, I just chose the defaults which are the first free cylinder to the last cylinder on the disk (ie; the free space).  At this point we have a new partition on the disk, but it’s not the right type of partition for LVM – to change the partition type we enter the following:

t (to change the partition type)

4 (enter the number of the partition to change, this should be the partition created earlier)

8e (this is the partition type code for 'Linux LVM')

After the partition is fully created we enter ‘w’ to write and quit the fdisk utility.  At this stage it’s common to get an error stating that the kernel will not see the new partition until after reboot, it’s safe to ignore this message.  To confirm the new partition enter “fdisk -l” again, for example:

Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14        2610    20860402+  8e  Linux LVM
/dev/sda3            2611        5221    20972857+  8e  Linux LVM
/dev/sda4            5222        9137    31455270   8e  Linux LVM

Notice the new partition /dev/sda4.

Now we’ve created the new partition I like to run ‘partprobe’ – this gets us round the fact that the kernel doesn’t know about the new partition.  At this point we need to check out our current LVM settings.  To do this we look at the following commands:

# mount 
# /dev/mapper/VolGroup00-LogVol00 on / type ext3 (rw)

The output of mount tells us in this instance that the logical volume LogVol00 is mounted as the / filesystem.  It also tells us the the logical volume LogVol00 is a member of the volume group VolGroup00.  If necessary you can look at the logical volume attributes using the following command:

#lvdisplay

You should be able to see the size of the logical volume as well as the volume group it’s a member of.

LVM is a bit like Russian dolls – the physical disk partitions are members of physical volume groups, the physical volume groups are then broken into logical volumes.  The logical volumes are presented to the operating system where they are used for filesystems.  We want to add our new partition (or disk) to the physical volume group so that we can extend our logical volume over it.  To do this we enter the following:

#pvcreate /dev/sda4 

(this creates a new physical volume that we can add to our physical volume group)

#vgextend VolGroup00 /dev/sda4 

(this associates the physical volume with our volume group VolGroup00)

At this point our new partition is available in our volume group as free space.  We use the following command to confirm that and also to record the amount of free space:

#vgdisplay

From the output of this command we should see an entry called “Free PE” – this is the free physical extents (in other words our free space).  We can use this value to extend our existing logical volume LogVol00 for example (this shows 959 free extents):

#lvextend -l +959 /dev/VolGroup00/LogVol00

After this command completes the logical volume LogVol00 will be extended by the amount of free space our new partition has.  You can display this using the command:

#lvdisplay

You should notice the “LV size” has increased.  At this point our / filesystem still doesn’t know about the new space, so the last piece of the jigsaw is to let it know about the new space:

#resize2fs /dev/VolGroup00/LogVol00

Once this command completes our / filesystem should see our new free space.  You can confirm this using the command:

#df -h /

Hopefully all is well and you’ve now got additional storage.





Find files that don’t contain certain text patterns

4 02 2010

Today I had to find a list of files that didn’t contain a particular piece of text.  At first I thought this would be relatively straightforward but a few attempts and googles yielded no success.  I tried to grep using the ‘-v’ option but this will return all the lines that don’t match the pattern in all files.  After dabbling with a for loop in which I grep’d each file individually, then noted which greps failed (by checking the exit status of grep $?) I asked a colleague.  We eventually found a one-liner that did what we needed:

find <directory_name> -name <file_name_pattern> -type f ! -exec grep -q <string_pattern> {} ‘;’ -print

Key to this was the ‘!’ which inverted the grep execution.  In my case I had to find out which ethernet interfaces where ‘not’ members of bond0, so I ended up with:

find /etc/sysconfig/network-scripts/ -name “ifcfg-eth*” -type f ! -exec grep -q “MASTER=bond0” ‘;’ -print

This printed a list of all the ifcfg-eth* files in /etc/sysconfig/network-scripts/ which do ‘not’ contain the line ‘MASTER=bond0’.





Install Linux onto a headless server without a cdrom drive

11 01 2010

Recently I got hold of three Sun Fire 2270 – until I opened the boxes I didn’t realise that they had no cd/dvd drives, VGA cards or iLOM’s installed.  I suppose it makes sense in these space-saving, energy-conscious, cost-sensitive times, although it did mean I had some work to do to get CentOS installed.  In the end it involved network booting and redirection to the serial console.  Unusually documentation was sparse, so here my attempt…

1. Publish the Installation OS on the Source Server

As I was installing CentOS 5.4 I retrieved the iso file from the web.   The next step is to loop mount the iso, using a command something like this:

#mount -o loop centos5.iso /mnt/iso (remember to create this directory before mounting)

I choose to publish this iso via my Apache webserver so I created a symbolic link called ‘centos’ in the www root folder (you can also publish via FTP or NFS):

#cd /var/www/html
#ln -s /mnt/iso centos

This should now mean that the iso image is available via your web server (browse to http://<webserver>/centos and make sure or else the next step will fail).

If your using kickstart (and I suggest you do for convenience) copy your kickstart file to the webserver and check the url to it (eg; http://<webserver>/kickstart/ks.cfg)

2. Setup the Network Boot Environment

Now we’ve our source OS and kickstart file published, we need to tell our server to boot from the network and retrieve the installation information.  To do this I configured the netboot and dhcpd services on the same box as my webserver.  For DHCP I set up a static entry in my /etc/dhcpd.conf file (my network is 10.0.5.0/24 – adjust as necessary):

default-lease-time                      30000;
max-lease-time                          30000;
option subnet-mask                      255.255.255.0;
option domain-name-servers              10.0.5.104;
option domain-name                      "mydomain.local";
ddns-update-style                       none;</pre>
<pre>subnet 10.0.5.0 netmask 255.255.255.0 {
option subnet-mask  255.255.255.0;
option routers 10.0.5.1;
next-server 10.0.5.104;</pre>
<pre>host x2270 {
hardware ethernet 00:aa:bb:cc:dd:ee;
fixed-address 10.0.5.105;
next-server 10.0.5.104;
filename "pxelinux.0";
}

This entry specifies the hardware address shown (taken from the sticker on the back of the x2270), it provisions a fixed address for it (10.0.5.105), it sets a ‘next-server’ option (10.0.5.104 – this is my netboot/web/dhcp server) as well as a ‘filename’ option (pxelinux.0 is the pxe bootstrap file we create on TFTP server next).

Next we need to set up our PXE boot server, this is the guy that allows the machine to be installed to boot from the network.  My preference is to use system-config-netboot tool, it’s very straight-forward to use and does pretty much everything we need.

Open system-config-netboot and choose Configure->Network Installation, then click Add to create a new installation source by setting the following fields:

Operating System Identifier – this is a unique name, in my case I just used CentOS32

Select Protocol for Installation – in this instance we’re using HTTP

Server IP Address – this is the IP of the HTTP server, in my instance 10.0.5.104

Location – this is the relative path to the installation directory ‘/centos’

After configured these setting click OK and close the ‘Configure Network Installations’ window.   At this stage we have the network installation configured we just need to get our server to use it.  To do this click ‘New’  and enter the following fields:

Hostname or IP Address – this is the ip or hostname of the machine to be installed (in my instance 10.0.5.105)

Operating System – if all went well with the network installation setup this should drop down to CentOS32

Make sure everything else on this windows is unchecked or not selected.  We’ll do our serial settings and kickstart settings by hand (it appears there’s a bug that has the kickstart page looping because it inserts an additional ‘/’ character).  You can now click OK and close the application.

Our final step is to adjust the network boot environment to use our kickstart file and use the serial console (as we’ve no VGA card).  To that end we edit the file /tftpboot/linux-install/pxelinux.cfg/<hex-code>.  The hex-code filename comes from pxelinux and can be found using the command:

#gethostip 10.0.5.105

In my environment that’s the IP of the address of the machine to be installed.  This file lists the pxelinux boot environment for that host – we need to append ks=http://<webserver>/kickstart/ks.cfg for our machine to be installed using our kickstart and also console=ttyS0 so our machine will redirect output to the serial console.  So my /tftpboot/linux-install/0A000569 file looks like (disregard the line break after the kickstart setting):

default CentOS32
label CentOS32
kernel CentOS32/vmlinuz
append initrd=CentOS32/initrd.img  ramdisk_size=8419 console=ttyS0 method=http://10.0.5.104/custom
ip=dhcp ks=http://10.0.5.104/kickstart/ks.cfg

3. Boot and Install Server

The Sun servers came with a RJ45 to Serial adapter, I connected this to a network cable and into the back of the new Sun x2270.  I stuck the other end in a USB serial adapter and then into another CentOS box.  This allowed me to use minicom to connect to the serial port (port settings are device=/dev/ttyUSB0 with 9600,8N1 and no flow control).  Next I hit the power switch and booted the new server – my servers were initially set to boot from the hard disk so I had to enter the BIOS (using Ctrl+E) and then set the boot device priority so that the network card was before the hard disk).  After setting this I rebooted and watched the installation progress nicely using the netboot server, HTTP source and kickstart file.  By the way I originally didn’t set console=ttyS0, so when anaconda kicks in it disappears from screen – this was key to these particular Sun boxes.

After the network installation has completed don’t forget to edit the boot device priority again or else the machine will netboot again.  Also once you’re done with network booting remove the next-server and filename options from DHCP – I’d also recommend switching off the netboot server so nothing else tries to boot from it (or abuse it).








Follow

Get every new post delivered to your Inbox.