Hardcore Linux

Anything about Ubuntu, Centos, openSuSe and Fedora

Category Archives: centos

Recover Deleted or Corrupted Files in Linux

In most cases, the data stored in your memory cards or USB drives likely being corrupted and there are some files you accidentally deleted and removed in trash. Fear not, there is another chance to recover them back again.

I tried scalpel but didn’t work our right, then tried foremost, and it works like a charm. I’ve been always a victim of corrupted memory cards every time I extracted or deleted something using memory card readers or even using the delete utility of my digital camera.

Well, here’s the basic command to recover things in your memory cards  or USB thumb drives.

1. First, install foremost

$> sudo apt-get install foremost

2. Then, determine the target device, mines is /dev/sdb1

$> sudo fdisk -l

3. Finally, issue the foremost command\

$> sudo foremost -t all -i /dev/sdb1 -o output_dir

4. Done

Mounting FS via SSH in Windows

You have a Virtual Machine Guest on your Microsoft Windows 7/XP/Vista and it is running on Linux, might be a web server, your sandbox. Wish to mount the file system via SSH? Sure you can do ftp/cifs, but using minimal required services for the VM, you can just use the SSH to mount the Linux File System in Windows, using Dokan SSHFS.

In my case, doing a web application using RubyonRails, and my current VM is on Centos 5.5 with all the necessary services for the web application. To setup Dokan SSHFS on your Windows system, check the requirement first here,  Usually I just install the Dokan Library and Dokan SSHFS.

Mounting the FS via SSH, just open the Dokansshfs.exe and you’ll be prompted to this screen:

A new drive will immediately appear after the connection has been completed.

Done.

SAMBA PDC on Centos 5.5

This guide will help to install and configure a SAMBA Windows File Server that acts as a PDC using tbsam, Although it is suggested to have it configured with LDAP authtentication backend, many small office still cater this kind of configuration.

Here’s the details:

1. Install the Samba File Server and necessary packages.

#> yum groupinstall "Windows File Server"

2. Prepare the necessary directories needed for netlogon.

#> mkdir -p /home/samba/netlogon

3. For new users to have a directory called profile in their home directory, add a folder called “profiles” in the /etc/skel. For existing users, just add the folder “profiles” to their home directories and change the ownership to their corresponding owners.

4. Backup the existing /etc/samba/smb.conf file first before using the configuration below:


[global]
 #Server Declaration
 workgroup = MYDOMAIN
 netbios name = FILESERVER001
 server string = File Server  %v

 #Security Properties
 security = user
 domain master = yes
 preferred master = yes
 local master = yes
 domain logons = yes
 wins support = yes
 os level = 65
 name resolve order = wins bcast hosts

 #Login Configurations
 logon path = \\%L\%U\profiles
 logon drive = H:
 logon home = \\%L\%U
 logon script = logon.bat

 #User Scripts
 add group script = /usr/sbin/groupadd '%g'
 delete group script = /usr/sbin/groupdel '%g'
 add user to group script = /usr/sbin/usermod -a -G '%g' '%u'
 delete user from group script = /usr/bin/gpasswd -d '%u' '%g'
 add user script = /usr/sbin/useradd -m -G users '%u'
 add machine script = /usr/sbin/useradd -s /bin/false -d /tmp '%u'
 delete user script = /usr/sbin/userdel -r '%u'
 idmap uid = 1000 - 20000
 idmap gid = 1000 - 20000

 #Passwords
 passdb backend = tdbsam:/etc/samba/passdb.tdb
 passwd program = /usr/bin/passwd '%u'
 passwd chat = "*Enter\snew\sUnix\spassword:*" %n\n "*retype\snew\sUnix\spassword:" %n\n. "*updated successfuly*"
 passwd chat debug = yes
 encrypt passwords = yes
 unix password sync = yes
 enable privileges = yes
 username map = /etc/samba/smbusers

 # Log File
 log file = /var/log/samba/%m.log
 log level = 3
 max log size = 50

 #Other Configurations
 socket options = TCP_NODELAY IPTOS_LOWDELAY SO_SNDBUF=8192 SO_RCVBUF=8192
 printing = cups
 printcap name = cups
 show add printer wizard = No

#============================ Share Definitions ==============================

[netlogon]
 path = /home/samba/netlogon
 admin users = root
 guest ok = yes
 browsable = no
 valid users = %U
 read only = no
 admin users = Administrator

[profiles]
 path = /home/%U/profiles
 create mode = 0600
 directory mode = 0700
 profile acls = Yes
 read only = No

[homes]
 comment = Home Directories
 browseable = no
 writeable = yes
 valid users = %S
 create mode = 777
 directory mode = 777

[printers]
 comment = All Printers
 path = /var/spool/samba
 browseable = no
 guest ok = no
 writable = no
 printable = yes

5. Modify the /etc/nsswitch.conf, your hosts line should look like this:

hosts:  files wins dns

6. Modify the /etc/samba/smbusers, the root usermap should look like this:

root = administrator Administrator admin

7.  Link SAMBA and Linux user groups, from root access, execute the following commands:

#>  net groupmap add ntgroup="Domain Admins" unixgroup=root rid=512 type=d 
#>  net groupmap add ntgroup="Domain Users"  unixgroup=users rid=513 type=d
#>  net groupmap add ntgroup="Domain Guests" unixgroup=nobody rid=514 type=d

After each commands, the system should response with the following message.

Successfully added group Domain ... to the mapping db as a domain group

8. To additional groups, perform the following:

#> groupadd <linux group>
#> net groupmap add ntgroup="<windows group>" unixgroup=<linux group> type=d

Note: the rid value should be the succeeding number of the previously entered value.

9.  Add root to the samba users, to be used in domain authentication on windows workstations.

#> smbpasswd -a root
#> smbpasswd -e root

10. Check your configurations and verify that you have entered the correct settings.

#> testparm

11.Restart the samba service, also start the winbind service it not yet running.

#> service smb restart
#> service winbind start

12. Test the Administrator access first

#> smbclient -L localhost -U
   enter the root password

12. To add new users, you can use the basic commands:

#> useradd -m -G users <username>
#> passwd <username>
#> smbpasswd -a <username>

Also note that new users and groups should be in range from 1000 to 20000, else modify the idmap declarations in /etc/samba/smb.conf.

13. Restart the samba service again, and check of the new user will be authenticated when accessing the samba shares.

$> smbclient -L localhost -U <username>
enter the <username's> password

14. Configure the windows workstation and join them to your new samba file server using the details below:

Domainname: MYDOMAIN
Administrator Account: Administrator
Password: <your root password>

15. Done.

UDEV: Mount and Unmount External Drive

I got three sets of USB External Hard Drive I’m using for my system backup. Need it to have a hotplug capability to have multiple copies of backup. The backup operations are done midnight of the schedule date.

Here’s my list:

  1. Daily Backup – two (2) sets of hard drive of 500GB
  2. Weekly Backup – two (2) sets of hard drive with capacity of 1TB
  3. Monthly  Backup one (1) set capacity of 1TB

The previous problem I’ve encoutered is that I need to manually mount them as:

/backup/daily
/backup/weekly
/backup/monthly

My backup script tend to use them on that location. So I made this udev script that will fix them and automatically mount and umount them. Though the umount should be safely done, but in case you forgot to umount the drive, the script will umount them for you.

Here’s my /etc/udev/rules.d/10-backupdrives.rules

ACTION=="add",KERNEL=="sd[b-z][1-9]", PROGRAM="/sbin/blkid -s LABEL /dev/%k",RESULT=="*BACKUPD*", RUN+="/bin/mount /dev/%k /backup/daily",OPTIONS="last_rule"
ACTION=="add",KERNEL=="sd[b-z][1-9]", PROGRAM="/sbin/blkid -s LABEL /dev/%k",RESULT=="*BACKUPW*", RUN+="/bin/mount /dev/%k /backup/weekly",OPTIONS="last_rule"
ACTION=="add",KERNEL=="sd[b-z][1-9]", PROGRAM="/sbin/blkid -s LABEL /dev/%k",RESULT=="*BACKUPM*", RUN+="/bin/mount /dev/%k /backup/monthly",OPTIONS="last_rule"
ACTION=="remove",KERNEL=="sd[b-z][1-9]",RUN+="/bin/umount /dev/%k",OPTIONS="last_rule"


Automatically shutdown KVM Virtual Machines

Currently, there’s no built-in tool to properly shutdown VMs  when the KVM host does. So after couple of hour “googling” it I got some idea from this link, made some changes to make simplier. Here’s my version:


#!/bin/bash

LIST_VM=`virsh list | grep running | awk '{print $2}'`
TIMEOUT=90
DATE=`date -R`
LOGFILE="/var/log/shutdownkvm.log"

if [ "x$activevm" =  "x" ]
then
 exit 0
fi

for activevm in $LIST_VM
do
 PIDNO=`ps ax | grep $activevm | grep kvm | cut -c 1-6 | head -n1`
 echo "$DATE : Shutdown : $activevm : $PIDNO" >> $LOGFILE
 virsh shutdown $activevm > /dev/null
 COUNT=0
 while [ "$COUNT" -lt "$TIMEOUT" ]
 do
ps --pid $PIDNO > /dev/null
if [ "$?" -eq "1" ]
then
COUNT=110
else
sleep 5
COUNT=$(($COUNT+5))
fi
done
if [ $COUNT -lt 110 ]
then
echo "$DATE : $activevm not successful force shutdown" >> $LOGFILE
virsh destroy $activevm > /dev/null
fi
done
  1. Save the code in /etc/init.d/shutdownvm
  2. Then make it an executable file
    chmod 755 /etc/init.d/shutdownvm
  3. Create links to both rc0.d and rc6.d
    cd /etc/rc0.d ln -s ../init.d/shutdownvm K18shutdownvm cd /etc/rc6.d ln -s ../init.d/shutdownvm K18shutdownvm
  4. Done.

LXC on Ubuntu 10.04

LXC (LinuX Containers) is a  lightweight virtualization that lets you run an isolated processes and resources. You can run this so-called “containers” inside your Linux host and use the resources without the need of paravirt drivers, the performance is almost as native machine does.

How to install?

1. First install the necessary packages.

$> sudo apt-get install lxc bridge-utils debootstrap

2. Mount the cgroup, which lxc uses to regulate and limit resources for the containers.Modify your /etc/fstab and add the following:

none  /cgroup  cgroup  defaults  0 0

3. Then configure your network card to bridge mode.

auto lo
iface lo inet loopback

auto br0
iface br0 inet dhcp
    bridge_ports eth0
    bridge_stp off
    bridge_maxwait 5
    post-up /usr/sbin/brctl setfd br0 0

4. You can also set it to static IP.

auto lo
iface lo inet loopback

auto br0 
iface br0 inet static
   address 192.168.1.10
   netmask 255.255.255.0
   broadcast 192.168.1.255
   gateway 192.168.1.1
   bridge_ports eth0
   bridge_stp off
   bridge_maxwait 5
   post-up /usr/sbin/brctl setfd br0 0

5. Reboot and now your set. You can now create your own sets of containers or use the pre-configured from this site. I, myself still failed to create my own container templates, so for now I’m still using what bodhizazen have created.

VMWare Server works fine for Centos 5.5

Discard all the previous problem with VMWare Server 2.0.2 on Centos 5.4, with the release of Centos 5.5, all of those are gone. VMWare Server can now run smoothly again.

Updating to the 5.5 is also a breeze, just run this command on root console:

1. First check your current Centos version

 rpm -q centos-release

2. Then, check the list of packages to be updated

 yum list updates 
 yum list updates | grep centos-release

3. Then if all is good to go

 yum update

4. You just need to reconfigure VMWare server via

 vmware-config.pl

5. Follow the same installation pattern it’s done.

Creating Shortcut links from a mounted remote FS (CIFS, WebDAV etc)

I’m very used to this in KDE but in gnome it’s very recently, here’s how:

1. Open Nautilus and prompt to the remote folder (CIFS, WebDAV etc).

2. Click the folder and press CTRL+ALT+SHIFT and drag the folder to your Desktop or to any place where you wish to make a link.

3. Done.

Easy openVPN Server in CentOS 5.3

1. Install necessary library

yum install lzo lzo-devel zlib zlib-devel

2. At this moment openssl should already installed.

(A required package for a server)

3. Install the openvpn package

yum install openvpn

4. Copy necessary sample scripts and configuration file

cp -r /usr/share/doc/openvpn-2.0.9/easy-rsa/ /etc/openvpn/
cp /usr/share/doc/openvpn-2.0.9/sample-config-files/server.conf /etc/openvpn
cp /usr/share/doc/openvpn-2.0.9/easy-rsa/openssl.cnf /etc/openvpn

5. Before running scripts, make sure that it has the executable permission.
If not perform the following:

cd /etc/openvpn/easy-rsa
chmod +x clean-all
chmod +a build*

6. Modidy you CA configuration

vi /etc/openvpn/easy-rsa/vars

export KEY_COUNTRY=AU
export KEY_PROVINCE=VIC
export KEY_CITY=MELBOURNE
export KEY_ORG=”THROXVPN”
export KEY_EMAIL=”name@email.com”

7. Save your modified settings and run:

. ./vars
mkdir /etc/openvpn/keys
./clean-all

8. Now your configuration is ready, create your server CA authentication files

cd /etc/openvpn/easy-rsa
./build-ca

9. Build your server keys

./build-key-server vpnserver

10. Building Diffie Hellman file

./build-dh

11. Modify the sample /etc/openvpn/server.conf

mode server
client-to-client
port 1194
proto tcp
dev tun
ca /etc/openvpn/keys/ca.crt
cert /etc/openvpn/keys/server.crt
key /etc/openvpn/keys/server.key
dh /etc/openvpn/keys/dh1024.pem
server 10.10.10.0 255.255.255.0
ifconfig-pool-persist ipp.txt
keepalive 10 120
comp-lzo
user nobody
group nobody
persist-key
persist-tun
status /var/log/openvpn-status.log
log /var/log/openvpn.log
verb 3
mute 20

12.Please note of the ipp.txt which contains something like this

client1,10.10.10.4
client2,10.10.10.5

13. Configure the services to autostart and eventually start the service

service openvpn restart
/etc/init.d/openvpn start

14. Server is up at this moment. You can now create keys for each of your client.

15. Done.

Unofficial mirrors for Centos and Ubuntu in the Philippines