copy partition table from one disk to another

When replacing a failed disk in a RAID 1 array, it’s necessary to get the new disk setup properly with an identical partition table matching the existing disk. In this case sda has failed and has been replaced with a spare. To copy the partition table from sdb to sda do this:

# sfdisk -d /dev/sdb | sfdisk /dev/sda --force

Make absolutely sure you know which disk is which, otherwise you will blow out the good partition table on the running disk, with whatever is on the spare (probably no partition table at all).

From time to time you might encounter a failure message about SSH and host identification. SSH remembers the fingerprint of keys of other hosts it connects to. It stores these keys in a file, so that if the fingerprint changes you will hear about it. This can happen with some DHCP addresses. For example, HostA has a DHCP address of 192.168.1.102. You change HostA’s IP address, to a static address, and the lease for 192.168.1.102 expires. Then you bring up HostB and it gets the DHCP address of 192.168.1.102. When you go to ssh to 192.168.1.102, you get an error. That’s because SSH recognizes that it’s a different host altogether. This helps prevent in MIM attacks, or IP spoofing. In this case we know what’s going on, so it’s safe to remove the old fingerprint for HostA and reconnect to HostB subsequently storing it’s fingerprint.

# ssh 192.168.1.102
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ECDSA key sent by the remote host is
34:6e:16:28:90:21:bd:6a:80:e4:97:41:85:ef:4a:ad.
Please contact your system administrator.
Add correct host key in /root/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /root/.ssh/known_hosts:15
ECDSA host key for 192.168.1.102 has changed and you have requested strict checking.
Host key verification failed.
#

One way to fix this is to use vi to edit the known_hosts file. Use the :15 command to navigate to line 15 and use dd keystroke to remove this entry, then :wq to save and quit vi.

Another alternative is to use sed to remove line 15:

# sed -i '15d' /root/.ssh/known_hosts
#

Also, the ssh-keygen utility comes with this built in:

# ssh-keygen -f '/root/.ssh/known_hosts' -R 192.168.1.102
/root/.ssh/known_hosts updated.
Original contents retained as /root/.ssh/known_hosts.old
# 

append file with wget

To append a file with one fetched from a URL, use wget and output to STDOUT then redirect and append where needed.

# wget -q 'http://10.11.178.141/all_pub_keys' -O - >>/root/.ssh/authorized_keys

In this case I wanted to add some public keys to an existing authorized_keys file.

chkconfig –list for Debian using sysv-rc-conf

In Redhat based distros you can use chkconfig to see what daemons are supposed to run at what run levels. In Debian you can use the sysv-rc-conf package.

Install it:

# apt-get install --yes sysv-rc-conf

Run it:



# sysv-rc-conf

┌ SysV Runlevel Config   -: stop service  =/+: start service  h: help  q: quit ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                                                                                                                                                                                                                           │
│ service      1       2       3       4       5       0       6       S                                                                                                                                                                    │
│ ----------------------------------------------------------------------------                                                                                                                                                              │
│ acpid       [ ]     [X]     [X]     [X]     [X]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ bootlogd    [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ bootlogs    [X]     [X]     [X]     [X]     [X]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ console-s$  [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ cron        [ ]     [X]     [X]     [X]     [X]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ fancontrol  [ ]     [X]     [X]     [X]     [X]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ halt        [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ hdparm      [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ ifupdown    [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ ifupdown-$  [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ kbd         [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ keyboard-$  [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ killprocs   [X]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ lm-sensors  [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ lvm2        [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ mdadm       [ ]     [X]     [X]     [X]     [X]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ mdadm-raid  [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ module-in$  [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ mountover$  [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ multipath$  [ ]     [X]     [X]     [X]     [X]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ multipath$  [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ mysql       [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ ndbd        [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ networking  [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ nfs-common  [ ]     [X]     [X]     [X]     [X]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ ntp         [ ]     [X]     [X]     [X]     [X]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ open-iscsi  [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ portmap     [ ]     [X]     [X]     [X]     [X]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ postfix     [ ]     [X]     [X]     [X]     [X]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ procps      [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ rc.local    [ ]     [X]     [X]     [X]     [X]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ reboot      [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ rmnologin   [ ]     [X]     [X]     [X]     [X]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ rsyslog     [ ]     [X]     [X]     [X]     [X]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ sendsigs    [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ single      [X]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ snmpd       [ ]     [X]     [X]     [X]     [X]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ ssh         [ ]     [X]     [X]     [X]     [X]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ stop-boot$  [ ]     [X]     [X]     [X]     [X]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ stop-boot$  [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ udev        [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ udev-mtab   [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]                                                                                                                                                                   │
│ umountfs    [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ umountroot  [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]                                                                                                                                                                   │
│ urandom     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [ ]     [X]  

This shows a list of Runlevel configuration for daemons. For most user land stuff, you will just want 2, 3, 4, and 5. You can edit the list, and changes will be made when you exit. This is a graphical alternative to update-rc.d

get memcache statistics

To obtain some stats about a memcached process, use nc to talk directly to it:


# echo "stats" | nc -w 1  11211
STAT pid 1750
STAT uptime 29481383
STAT time 1369781775
STAT version 1.4.5
STAT pointer_size 64
STAT rusage_user 6974.991909
STAT rusage_system 13000.624488
STAT curr_connections 10
STAT total_connections 132871296
STAT connection_structures 1674
STAT cmd_get 227296759
STAT cmd_set 113549712
STAT cmd_flush 0
STAT get_hits 221783239
STAT get_misses 5513520
STAT delete_misses 0
STAT delete_hits 0
STAT incr_misses 36444
STAT incr_hits 19304751
STAT decr_misses 3
STAT decr_hits 19367598
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 26222582271
STAT bytes_written 25905663432
STAT limit_maxbytes 67108864
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT bytes 27010093
STAT curr_items 366610
STAT total_items 113554333
STAT evictions 0
STAT reclaimed 5291352
END

#

mount windows disk image in linux

I was recently converting a Windows installation from a physical desktop to a virtual machine. Because the installation was an OEM installation the P2V conversion failed the “hardware” check and the target VM was unregistered. The only way to fix was to do a repair install changing the installation from an OEM to a Retail version of Windows. This needed to be done on the physical source desktop. There’s no way I was going to attempt this without a total backup of the system. I could have just copied important files, but I decided a disk clone was more appropriate. I booted off of a Debian rescue cd, mounted a NFS share, and dd’d a copy of the entire drive to a flat file.

# mkdir /mnt/nfsserver/diskclone
# mount nfsserver:/vol/diskclone /mnt/nfsserver/diskclone
# cd /mnt/nfsserver/diskclone
#
# dd if=/dev/sda of=desktop.img
156250000+0 records in
156250000+0 records out
80000000000 bytes (80 GB) copied, 7079.64601 seconds, 11.3 MB/s
#
# ls -l
total 78278496
-rw-r--r-- 1 root root 80000000000 2013-05-20 15:47 desktop.img

I then needed to mount the image and take a look at the contents. A flat disk image file is just a block for block copy of the actual disk, so the first step is to look at the partition table listed in sectors and find the offset. The offset will tell mount where the beginning of the file system is.

# losetup /dev/loop0 desktop.img
#
# fdisk -l -u  /dev/loop0

Disk /dev/loop0: 80.0 GB, 80000000000 bytes
255 heads, 63 sectors/track, 9726 cylinders, total 156250000 sectors
Units = sectors of 1 * 512 = 512 bytes 

      Device Boot      Start         End      Blocks   Id  System
/dev/loop0p1   *          63   156232124    78116031    7  HPFS/NTFS
#
#

Find the offset by multiplying the 63 (start sector) by 512 bytes ( 63*512 ) and the offset is 32256. Now we can tell mount where the start is.

# mkdir /mnt/diskimage
# mount -t ntfs -o offset=32256,ro desktop.img /mnt/diskimage
#

I mounted readonly so that we don’t mess anything up. You may need to install ntfsprogs to be able to read an NTFS file system (apt-get install ntfsprogs) if you don’t have it installed already

remove many empty directories

# find . -depth -mindepth 1 -maxdepth 3 -type d -exec rmdir {} \;

This finds directories, between 1 and 3 levels deep and attempts to remove them. The -depth flag finds the deepest child directories, before finding parents. This is great, because it tries to remove foo/bar/ before it will try to remove foo/. Without removing foo/bar/ first, rmdir foo/ would fail. Because rmdir will fail if there are any contents in a directory, the operation is safe to run without removing any files. You could redirect STDERR to a file, and capture all the directories that are not empty for processing later.