Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Tuesday, January 15, 2013

Playing with ESX: Setting up NAT in Linux (Basic Setup)

This post is about how to setup NATing in a Linux environment. If you have one central Gateway or Public IP and want all the internal machines to communicate to the outside world using Gateway's IP or the public IP, you need to setup a NAT for the same. Below mentioned steps will tell you how to configure the same.

Requirements:-
1. Gateway should have 2 nics, one to access internal network and other to access public network.
2. Internal machines should be connected via switch/hub to private nic of Gateway/Machine A.

In the setup mentioned below, I am using 2 VMs, config of each VM is as shown below:-

Config of Gateway:-
2 nics attached to VM.
First nic attached port-group "VM Network" which can access the public network.
Second nic added port-group "TMP" which allows only VLAN 85 traffic. Hence its a private network.



Config of internal VM:-
Internal VM is having only 1 nic. This nic is assigned a port-group TMP such that its a part of private network and can access nic1 of Gateway VM.



Network setup for the above is as follows(Note that Gateway VM is named as RST1 and internal VM is named as RST2):-


Assign IP address to Nic1 on the Gateway machine which is a part of the private network.

 # This file describes the network interfaces available on your system  
 # and how to activate them. For more information, see interfaces(5).  
   
 # The loopback network interface  
 auto lo  
 iface lo inet loopback  
   
 # The primary network interface  
 auto eth0  
 iface eth0 inet dhcp  
   
 auto eth1  
 iface eth1 inet static  
 address 10.3.1.1  
 netmask 255.255.255.0   

To enable routing functionality on Linux, one needs to enable IP forwarding. You can do the same by entering the following command:-

 # echo “1″ > /proc/sys/net/ipv4/ip_forward  

To make the changes persistent across reboots, just add the following line in "/etc/sysctl.conf". Open this file in your favourite editor and add the following line:-

 net.ipv4.ip_forward = 1  

Reboot your machine.

Now you need to add Gateway functionality to this VM. For this, you need to add IPTable rules such that traffic coming on nic1 is sent out through nic 0. You can enter the following commands on the Linux terminal:-

 /sbin/iptables -P FORWARD ACCEPT  
 /sbin/iptables --table nat -A POSTROUTING -o eth0 -j MASQUERADE  

You can also add the above two lines at the bottom of "/etc/rc.local" file so that changes are present even after reboot. Now your basic NAT gateway is up and running.

To test the functionality of the Gateway, just provide the gateway address as the address of eth1 of Gateway and you will be able to connect to the outside world using Gateway. Below mentioned is the "/etc/network/interfaces" file of inside VM.

 # This file describes the network interfaces available on your system  
 # and how to activate them. For more information, see interfaces(5).  
   
 # The loopback network interface  
 auto lo  
 iface lo inet loopback  
   
 # The primary network interface  
 auto eth0  
 iface eth0 inet static  
 address 10.3.1.2  
 netmask 255.255.255.0  
 gateway 10.3.1.1  
   

Now if you will try to ping to outside world using this internal VM, you would be able to do so using the gateway.

Somethings you can try:-
Try configuring the Gateway such that you can SSH to the VMs behind the Gateway. You need to enable Port-Forwarding for the same. Happy configuring. :)

Last but not the least, I would like to thank my special friend for coming up with the idea to learn together and do this setup. Hopefully we both would do a lot more funny experiments in future. Thanks again to that friend. :)

Tuesday, June 19, 2012

Running VMs in XEN if HVM support is not there

I have seen this problem a lot many times when people try to run linux VMs on Xen host which does not have Intel VT feature. Once Xen is installed, they are unable to run any VM since by default Xen keeps the virtualization type to HVM.

However you can run VMs on this host by following the following trick:-
You need to get VMs which are Paravirtualized. You can get them by creating them on a host which has VT feature, then making them Paravirtualized and then exporting them from that host and importing on the host which does not have intel VT feature. Using this, you will be able to run Paravirtualized VMs on hosts which does not have Intel VT feature. I have tried this and it worked in my case. Thought of sharing this with larger audience as it might help someone in need. To create paravirtualized Ubuntu VM, see my post link: Creating Paravirtualize Ubuntu VM

Tuesday, May 29, 2012

Setup FTP Server on Ubuntu and precautions to take

Most of the times I face issue in configuring FTP on Linux platform as a lot many types of FTP servers are available and each one has different properties. In this post, I am going to share info about one such FTP server which is most common and using simple steps, we can configure it. So here it goes.

I am going to discuss about vsftpd. First you need to install it onto your ubuntu box. For that, enter the following command:-
rahul@ubuntu$ sudo apt-get install vsftpd

Once its installed, you can see its conf file in /etc with name vsftpd.conf . There you can specify the access_type ie, whether you want to allow anonymous access or local_access. By default, local access is enabled, so anyone with the username "anonymous" can login to ftp server. To allow only authenticated users to be able to access FTP server, you need to change the access_type to local_access. One can do this by editing the vsftpd.conf file, commenting the "anonymous_enable=NO" and adding "local_enable=YES".
rahul@ubuntu$ sudo vi /etc/vsftpd.conf


Once you are done with the changes, restart the service.

rahul@ubuntu$ sudo /etc/init.d/vsftpd restart

Once its done, are done with the setup of FTP server. Now you need to keep following things in mind:-
You cannot connect to FTP server by using the username as "root" as it might give error.
For that, use username "ftp" or any user which is present on that machine except root.
Use the default credentials of that account to log in.

Default Directory:-
Default directory in this case is the home directory of the user with whose name you have logged in. So if you have a user "rahul" and you have logged in via that username, then the default directory of ftp will be "/home/rahul" . If you will do "ls" once you are connected to that server, it will list all the files present in that directory.

If you want to allow user to write to that directory, then you have to accordingly change the setting in vsftpd.conf file and enable that flag.

Hope this will solve some of the problems which users face due to vsftpd.

Friday, May 25, 2012

Creating Ubuntu 12.04 Paravirtualized VM for Xen

Creating an Ubuntu VM:-
First of all, you need to create an Ubuntu VM. For that, there is no default template in XENServer 6.0. Therefore, select the "Other Install Media" template. Make sure that the VM-Name which you write should not contain any spaces. This is because we are going to use a script at later stages and it gives error if vmname contains space.

XenServer 5.5's pygrub(the bootloader for PV) is unable to read Ext4 filesystem. This could be a limitation in XenServer 6.0 as well although I am not sure about it. Therefore to be on the safer side, make sure that your /boot partition uses Ext3 or Ext2 filesystem. Here is one sample partition table which you can make. Make 3 partitions and keep the rest of space unallocated which we can allocate later.

Partition Table:-
Type: /boot              Filesystem:   Ext3/Ext2                Size:    5 GB
Type: /                     Filesystem:   Ext4/Ext3/Ext2        Size:  10 GB
Type: Swap                                                                Size: 2 * Size of RAM
Unallocated Space: XX GB

If you don't want to take advantage of Ext4, then you can format using entire disk and using filesystem type as anything except Ext4.

Once done with this, just install Ubuntu12.04 on the VM. Also install openssh-server on that machine.
rahul@ubuntu$ sudo apt-get install openssh-server

Xen uses hvc0 console. So one needs to change tty1 to hvc0. For that perform the following steps:-
step1:-
rahul@ubuntu$ sudo cp /etc/init/tty1.conf /etc/init/hvc0.conf

Step2:-
Open hvc0.conf and replace all tty1 occurences by hvc0.
rahul@ubuntu$ sudo vi /etc/init/hvc0.conf

That file looked like this on my Ubuntu12.04 VM.


Now there are a fixed set of commands which need to be run to make the VM paravirtualized. Here on this link, they have listed those commands.

These commands are used(are present) in the script which we are going to run at later stage. Now it requires some parameters as input which are present in the grub.cfg file. So we will copy that part from grub.cfg and keep it in some text file with us so that we can refer to it at later stages when required.Copy these parameters from VM and keep with you so that its available when the VM is off.
We need to copy the "Default Menu Entry" and copy its entire stuff(kernel and ramdisk name) in a different text file for later use. Here is example of the grub.cfg of my machine "Ubuntu12.04". Please note that the location of kernel and initrd will change in your cases as it depends on which partition your /boot is. So just copy the default menu entry and its whole content till the start of next entry.


Xen modules are not loaded into initramfs. We can correct that by adding the entry in modules file. Enter the following command for that:
rahul@ubuntu$ sudo echo "xen-blkfront" >> /etc/initramfs-tools/modules 
rahul@ubuntu$ sudo update-initramfs -u
Shutdown the VM. Create a snapshot if you want to revert back if some error occurs.

 Now you need to SSH to XenServer ie. to Dom0 machine and configure the properties of DomU machine which you want to paravirtualize.
Perform "xe vm-list" to list all the vm's present on xen host. Identify the name of vm which you want to paravirtualize.
root@xen# xe vm-list


  Now download the script available here.

The above mentioned script contains nothing but the steps which we have pointed out above on the blog with the link to that site.

Change the execution permissions of the script.
root@openstack-xen1# chmod +x makepv.sh

Execute the script. It will ask for vmname. Provide the name of VM which you want to para-virtualize.
root@openstack-xen1# ./makepv.sh <vmname>

Then it will ask for other parameters like kernel path and kernel parameters.
Provide them from the boot_config we have earlier copied and saved in a file. Copy the parameters from that file and provide them here.
If you don't have the script, then manually execute the commands as listed in the blog-post provided above.

Once its done, you need to install xentools onto the VM which you are paravirtualizing.
So connect xen-tools.iso to the ubuntu12.04 vm and power-on the VM.
SSH to the vm and install xen-tools on it.

Steps to install xentools:-
ssh <vm_ip>
sudo mount /dev/cdrom1 /media/cdrom
cd /media/cdrom/Linux
sudo dpkg -i xe-guest-utilities_6.0.0-743_amd64.deb

If you try to access vm-console using xencenter and the default console is hvc0, then you might see the error that you are unable to type anything  there. This is one of the bugs of xencenter. You just need to close xencenter and reopen it again to get it fixed.

Reboot the VM.
You need to change the run-level of the services. For that, enter the following commands:-

ssh <vm ip address>
sudo update-rc.d -f xe-linux-distribution remove
sudo update-rc.d xe-linux-distribution defaults

Reboot the VM.
Now your ubuntu12.04 VM. Your VM is paravirtualized now.
Run xe vm-param-list command and you will see that now it uses pygrub instead of HVM.

Thursday, January 12, 2012

Setting up passwordless ssh




One can create passwordless-ssh between two machines so that one can access the other one without needing to enter the password. To achieve this, you need to first install sshserver on your machine. You can try the following commands:-
user@ubuntu32:~/$ sudo apt-get install openssh-server

Once its installed, you need to generate public and private keys. For that use the following command:-
user@ubuntu32:~/$ ssh-keygen –t rsa

It will prompt you for filename and passphrase. Skip it or enter something if you want to use some passphrase. Similarly do this on the second machine as well.



Now you will have a public, private keypair (id_rsa, id_rsa.pub) for the user from whose account you generated the keys. They are present in /home/<username>/.ssh directory.



Now you need to copy the id_rsa.pub to authorized_keys present in /home/<username>/.ssh on server with which you want to setup passwordless ssh. You can enter the following command on the terminal:-
scp ~/.ssh/id_rsa.pub username@servername:.ssh/authorized_keys

 

Once its copied, now you can ssh to the server to which you copied the id_rsa.pub key without giving the password.

Kindly note here that scp will override the authorized_keys file on server to whom you want to setup passwordless ssh. So if you want to have more clients using passwordless ssh to same server, you need to first copy id_rsa.pub to that server, then append the file authorized_keys to contain id_rsa.pub too and then delete id_rsa.pub from that server.

Now you don’t need password to ssh to that server.



One thing to note here is that in “ssh username@machinename”, username is the name of the user on machine to which you are doing passwordless ssh and to whose .ssh directory you have added your public key.

Sometimes what happens is that even after following the above steps, you are unable to login to other machine without password. If you face this issue, then you can check for logs in /var/log/auth.log file to see the kind of error you are getting.
If you face the authentication error(Authentication refused: bad ownership or modes for directory), then you need to change the permissions of the directory as defined below:

chmod go-w ~/
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

Now you will be able to successfully login to the other machine without using the password.

Monday, November 21, 2011

Installing and Configuring RSH on Linux

I installed and configured RSH on CentOS release 5.5 . The steps would be almost same for all the other linux distros.

First you need to have xinetd installed. For that fire the following command:-

user@centos# yum install xinetd

Once its installed, you need to install rsh-server onto your machine. For that enter the following command:-

user@centos# yum install rsh-server

Now you need to Edit /root/.rhosts to add lists of hosts that can access without password. If you want to allow access to everyone without password, you need to put '+' in the file. A '+' indicates allowing everyone.
To do this, you can either modify the file by opening it or just enter the following command:-

user@centos# echo + > /root/.rhosts

Now you need to modify the permissions of .rhosts file. Enter the following command:-

user@centos# chmod 600 /root/.rhosts


Now you need to allow RSH , RLogin and Rexec to be used. For that you need to modify the /etc/xinetd.d/rsh , /etc/xinetd.d/rlogin  and /etc/xinetd.d/rexec file. Open these files one by one and then set the "disable=no"

Here is an example for modifying the rsh file:-

user@centos# vi /etc/xinetd.d/rsh


Similarly do for the rlogin and rexec file.
Once these are done, now you need to modify the /etc/pam.d/rsh file to allow passwordless rsh to the machine.

You need to modify the following line:-

     BEFORE: auth    required        pam_rhosts_auth.so
     AFTER : auth    required        pam_rhosts_auth.so promiscuous
Now you are done. You need to restart the xinetd service.
 
user@centos# /etc/init.d/xinetd restart
Now run the rsh command with the other commands and you will be able to successfully
execute them.
 If there are any warnings or errors as you are trying to access using the root 
privileges, then you need to modify the /etc/securetty file. Add rsh and rlogin 
to the bottom of that file.
Now you would be able to successfully perform rsh to the machine. 

Wednesday, November 9, 2011

Installing Lua on Linux

For installation, download the binaries, build them and install them. The steps for building and installing are listed below.

Download the Lua binaries from the site:-
1. Download the source: http://www.lua.org/ftp/lua-5.1.4.tar.gz
2. Extract the tar ball
3. Run this command from lua-5.1.4 directory: "make linux test"
------------------------------------------------------------------------------------------------------------------------
If you get an error that readline or history.h not found, then try installing the following package:-

sudo apt-get install libreadline6-dev
------------------------------------------------------------------------------------------------------------------------
To check what files exist in a package, use the following command:-
dpkg-query -L <package name>
Like:-
dpkg-query -L libreadline6

If you don’t find any header files and only .so files, then it means you are having the shared libraries and not the source. So you need to install the source files as well.

------------------------------------------------------------------------------------------------------------------------
To get the list of packages and their names if you don’t know, then one can search using the following command:-
sudo apt-get cache search readline

This will give you the list of all the packages containing the name readline
------------------------------------------------------------------------------------------------------------------------

How to build and install binaries from the package:-
Once the binaries are downloaded and the command “make linux test” is run, you need to now install the generated files.

For that enter the following command:-
sudo make install

This will install the Lua binaries to the required path.

Now you can run the files using the terminal.
user@ubuntu32$ which lua
/usr/local/bin/lua
user@ubuntu32$ lua
>

Wednesday, August 24, 2011

Using tftp in Ubuntu 10.10

I tried to use tftp in ubuntu 11.04 but it was giving error. Maybe the support for ubuntu at the time of writing this blog is not available. So I tried using tftp in ubuntu 10.10 and it worked. So what you need to use tftp.

First you need to install tftp. For that use this command:-
$sudo apt-get install tftpd-hpa

Now the default directory which will be accessed while doing tftp to the server is having location address as :-
/var/lib/tftpboot
You can get this address by viewing the contents of tftpd-hpa file present at location "/etc/default/tftpd-hpa"

Now add all the files which you want to be accessed via tftp in this directory. This directory will appear as the parent directory.
Make sure you have given proper permissions to the files to be accessed.

Now simply goto command prompt and type "tftp <server_ip_address>" like "tftp localhost"
Now you will get a prompt as "tftp>""
Here you can write the commands.

For getting the file, type "get <filename_on_server>  <filename_to_be_used_o_client>" like:-
tftp> get rahul.txt new.txt

Now this file will be copied to you computer. It will be in the directory in which you are currently in. I.E. when you do tftp <host>, the directory in which you are there at that time, the file is copied to that directory.

Similarly to put the files on the tftp server, you need to use the command "put "filename" "server_path" "

To exit from the tftp command prompt, simply type "quit" and you will exit from it.

One thing to be kept in mind is that the path for tftp server starts from the tftpboot directory. So if you have rr directory inside tftpboot directory and want to access any file inside rr, then you have to specify the path as /rr/<filename>. Therefore, the default path starts from the tftpboot directory.

If you have any queries, then do comment, I would be happy to help...... :)

Tuesday, August 23, 2011

How to enable DHCP in Ubuntu 11.04

For DHCP, you will need to first install DHCP3-SERVER.
Open terminal and write "sudo apt-get install dhcp3-server

Once its installed, you need to check in few things:-
If your system is having more than one NIC, then you need to specify which nic is to be used for the DHCP service.
For that, type "sudo vi /etc/default/isc-dhcp-server"

Now in this, goto the line 'INTERFACES="" ' and enter the name of ethernet you want to use for dhcp there..... like 'INTERFACES="eth0" '

Now if your network adapter is getting IP via some other DHCP server, then you need to specify the subnet mask. So for that you need to modify the dhcpd.conf file. For that type "sudo vi /etc/dhcp/dhcpd.conf"
In that add this line "subnet <ip address> netmask <netmask_value>{} " like "subnet 192.168.233.129 netmask 255.255.255.0 {}"

Now restart the dhcp server and you will have it running. Type "/etc/init.d/isc-dhcp-server restart"

Now your dhcp server is running. You need to provide range that can be allocated in the dhcpd.conf file.....