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

Thursday, March 1, 2012

How to Create a Debian Package

In my previous blog, I've described how to create an RPM package. This time, I'm going to show you how to create a Debian package. I'm going to use the same code that's used in RPM tutorial blog. Creating a Debian package can be pretty complicated if we have to do it manually. Luckily there are some helper scripts that can make our lives easier.
  1. Install devscripts and debhelper
  2. sudo apt-get install devscripts debhelper
  3. Create a simple hello.cpp
  4. #include <iostream>
    using namespace std;
    
    int main() {
        cout << "Hello World" << endl;
        return 0;
    }
    
  5. Create a Makefile
  6. CC = g++ 
    CCFLAGS = -g -Wall
    SRC = hello.cpp
    TARGET = hello
    
    .PHONY: clean install
    
    all: hello
    
    install: $(TARGET)
        if [ -d ${DESTDIR} ]; then rm -rf ${DESTDIR}; fi
        mkdir -p ${DESTDIR}
        cp $(TARGET) ${DESTDIR} 
    
    hello: 
        $(CC) $(CCFLAGS) -o $(TARGET) $(SRC)
    
    clean:
        rm -rf $(TARGET)
    
  7. In the directory where we created the above files, create a new debian directory
  8. mkdir debian
  9. Create a debian/changelog file
  10. dch --create -v 0.1 --package hello
    We can create this changelog file without using dch script, but the changelog file requires a particular format and the dch script can help to create a changelog template. The changelog file should look like this.
    hello (0.1) UNRELEASED; urgency=low
    
      * Initial release. (Closes: #123)
    
     -- foo <foo@bar.com>  Thu, 01 Mar 2012 14:16:42 +0800
    
  11. Create a debian/compat
  12. The content of this file is very simple. It just contains a number 8. This file is used to tell debhelper to use compatibility helper version 8. The file should look like this.
    8
  13. Create a debian/control file
  14. This control file is used to describe the source and binary package and other information. This control file also has a particular format. Take a note that there's a space after the Description field. This to tell that it's an extended description. The description and extended description should be different; otherwise lintian (a tool to check the correctness of files rquired to create Debian packages) will complain. The control file should look like this.
    Source: hello
    Maintainer: foo <foo@bar.com>
    Section: misc
    Priority: optional
    Standards-Version: 3.9.1
    Build-Depends: debhelper (>= 8)
    
    Package: hello
    Architecture: any
    Depends: ${shlibs:Depends}, ${misc:Depends}
    Description: say hello
     say hello world
    
  15. Create a debian/rules file
  16. This file is basically a Makefile.
    #!/usr/bin/make -f
    %:
        dh $@
    
    override_dh_auto_install:
        $(MAKE) DESTDIR=$$(pwd)/debian/hello/usr/bin install
    
    The override_dh_auto_install is required because in our example, we have a custom make install target.

  17. Create a debian/copyright file
  18. For this tutorial, just create an empty copyright file.

  19. To summarize, we should've already created all these files
  20. /home/foo/testdeb/hello/hello.cpp
                           /Makefile
                           /debian/control
                           /copyright
                           /changelog
                           /compat
                           /rules
    
  21. Create a Debian package
  22. debuild -uc -us
    -uc means don't sign the .changes file
    -us means don't sign the source package
    For more information about the options, read man dpkg-buildpackage.

    You should now see your source and Debian packages in /home/foo/testdeb

  23. Install the newly created Debian package
  24. sudo dpkg -i hello_0.1_i386.deb
  25. Test if hello program was successfully installed
  26. hello
    You should see "Hello World" output

  27. Remove the hello package
  28. sudo dpkg -r hello

Tuesday, February 28, 2012

How to Create an RPM Package

In this blog, I'm going to teach you how to create an RPM package.
  1. Create a simple hello.cpp
  2. #include <iostream>
    using namespace std;
    
    int main() {
        cout << "Hello World" << endl;
        return 0;
    }
    
  3. Create a Makefile
  4. CC = g++ 
    CCFLAGS = -g -Wall
    SRC = hello.cpp
    TARGET = hello
    
    .PHONY: clean install
    
    all: hello
    
    install: $(TARGET)
        if [ -d ${DESTDIR} ]; then rm -rf ${DESTDIR}; fi
        mkdir -p ${DESTDIR}
        cp $(TARGET) ${DESTDIR} 
    
    hello: 
        $(CC) $(CCFLAGS) -o $(TARGET) $(SRC)
    
    clean:
        rm -rf $(TARGET)
    
  5. Put hello.cpp and the Makefile in a directory structure like this
  6. /home/foobar/hello/Makefile
                      /hello.cpp
    
  7. Create a tarball of hello project
  8. cd /home/foobar
    tar czvf hello-0.1.tar.gz hello/
    
  9. Create an rpmbuild directory
  10. mkdir -p /home/foobar/rpmbuild
    
  11. Create BUILD  BUILDROOT  RPMS  SOURCES  SPECS  SRPMS directories inside the /home/foobar/rpmbuild directory
  12. BUILD --> the source files are unpacked and compiled here
    BUILDROOT --> files are installed here
    RPMS --> binary RPMs are created here
    SOURCES --> source tarballs
    SPECS --> spec files
    SRPMS --> source RPMs

  13. Create a spec file in the /hom/foobar/rpmbuild/SPECS/hello.spec
  14. Summary             : Hello World program
    Name                : hello
    Version             : 0.1 
    Release             : 1%{?dist}
    License             : GPLv3+
    Source              : %{name}-%{version}.tar.gz
    
    %description
    A simple Hello World program
    
    %prep
    %setup -q -n %{name}
    
    %build
    make
    
    %install
    make install DESTDIR="%{buildroot}%{_bindir}"
    
    %files
    %{_bindir}/hello
    
    %prep --> this stage calls %setup to unpack hello-0.1.tar.gz to /home/foobar/rpmbuild/BUILD/hello. %{name} is a macro variable that substitutes to hello

    %build --> this stage calls "make" in the /home/foobar/rpmbuild/BUILD/hello to create a hello executable

    %install --> this stage calls "make install" to install the files into /home/foobar/rpmbuild/BUILDROOT/usr/bin. %{buildroot} is a macro variable that substitutes to /home/foobar/rpmbuild/BUILDROOT. Make sure the Makefile supports DESTDIR argument

    %files --> lists all the files to be installed. %{_bindir} is a macro variablesubstitutes to /usr/bin. In this example, only hello executable needs to be installed into /usr/bin

  15. Build the source and binary RPMs
  16. rpmbuild -ba /home/foobar/SPECS/hello.spec
    -ba option means build all (prep, build, install)
    The binary RPM will be created in /home/foobar/rpmbuild/RPMS
    The source RPM will be created in /home/foobar/rpmbuild/SRPMS

  17. Install the newly created binary RPM
  18. sudo rpm -ivh hello-0.1-1.fc16.i686.rpm
  19. Test if hello program was successfully installed
  20. hello
    You should see "Hello World" output

  21. Remove the hello package
  22. sudo rpm -e hello

Monday, September 20, 2010

SSH Login without Password

To login in SSH without password, we need to do the following:
1. Generate a key pair (private key and public key) in Server A
ssh-keygen -t rsa
2. Copy the public key from Server A to Server B.
scp id_rsa.pub testuser:[server_B_ip_address]:/home/testuser/.ssh
3. In Server B, add that public key into authorized_keys file.
cat id_rsa.pub >> authorized_keys
4. Verify it from Server A.
ssh testuser@[server_B_ip_address]

How to Setup NFS in Linux

Make sure we have the NFS (both client and server) package installed.
1. Edit /etc/exports
/home/myuser/share *rw(root_squash)
rw --> read write
ro --> read only
root_squash --> the remote root user is not allowed to have a root level access
no_root_squash --> the remote root user is allowed to have a root-level access
2. Verify it
showmount -e [nfs_server_address]
3. Open firewall port 2049
4. Restart the nfsserver deamon
/etc/init.d/nfsserver restart
5. Mount the remote export.
mount -t nfs [nfs_server_address]:/home/myuser/share nfs/

Friday, September 17, 2010

How to Setup Samba in Linux

Make sure we have the Samba package installed. For example, we want to share the /tmp directory in Linux so that we can access it from our Windows or from another Linux client (NFS is better for Linux to Linux file sharing), we need to do the following:
1. Edit /etc/samba.conf add the following lines
[tmp]
    comment = Test
    path = /tmp
    writable = Yes
    public = Yes
2. Add a Samba user.
smbpasswd -a sambauser
Enter the password.
3. We need to open firewall for the following ports:
- 137 (netbios-ns)
- 138 (netbios-dgm)
- 139 (netbios-ssn)
- 445 (microsoft-ds)
For more info, see /etc/services
3. Restart smb daemon.
/etc/init.d/smb restart

For Windows client to access the shared directory:
1. Go to Run and enter \\[ip_address]\tmp
2. Enter the Samba username and password.

For Linux client to access the shared directory:
mount -t cifs //172.16.242.129/tmp -o username=sambauser,password=password samba/

How to Setup DNS in Linux

To setup DNS in Linux, make sure we have BIND package installed.
1. Add these lines in /etc/resolv.conf
domain fw-geekycoder.com
nameserver 172.16.242.129
2. Add forward and reverse zones in /etc/named.conf
zone "fw-geekycoder" in {
    type master;
    file "master/fw-geekycoder.zone";
};

zone "242.16.172.in-addr.arpa" in {
    type master;
    file "master/242.16.172.zone";
};
3. As you we can see in step two, there are two zone files that need to be created.
In /var/lib/named/master/fw-geekycoder.zone file
$TTL 1W
@       IN SOA  ns1.fw-geekycoder.com. root.fw-geekycoder.com. (
                                2010191505      ; serial (d. adams)
                                2D              ; refresh
                                4H              ; retry
                                6W              ; expiry
                                1W )            ; minimum

fw-geekycoder.com.      IN NS           ns1.fw-geekycoder.com.
ns1                     IN A            172.16.242.129
www                     IN A            172.16.242.129
gateway                 IN A            172.16.242.2
In 242.16.172.zone file
$TTL 1W
@       IN SOA  ns1.fw-geekycoder.com. root.fw-geekycoder.com. (
                                2010191605      ; serial (d. adams)
                                2D              ; refresh
                                4H              ; retry
                                6W              ; expiry
                                1W )            ; minimum

129.242.16.172.in-addr.arpa.    IN NS           ns1.fw-geekycoder.com.
129                             IN PTR          ns1.fw-geekycoder.com.
129                             IN PTR          www.fw-geekycoder.com.
2                               IN PTR          gateway.fw-geekycoder.com.
4. Restart the named daemon.
/etc/init.d/named restart

Tuesday, September 7, 2010

Creating ISO Images

Creating ISO images in Linux is very straightforward.
If for example, we need to create an ISO image for a CD/DVD, we can use dd command.
dd if=/dev/cdrom of=cd.iso

If we need to create an ISO image out for a directory/file, we can use mkisofs command.
mkisofs -o ~/docs.iso -J -r -V MY_LABEL ~/Documents

To verify if the ISO image is correct, we need to mount that ISO image.
mount -t iso9660 -o loop /media

To umount:
umount /media
See man dd and mkisofs for more information on the above commands.

Thursday, June 17, 2010

Setting Up Quota in Linux

Setting up a quota in Linux is easy.
1. Install quota.

In Ubuntu:
sudo apt-get install quota

In openSUSE:
sudo yast2 -i quota

2. Add usrquota and grpquota parameters to the mount options in the /etc/fstab, e.g.
/dev/sda2  /  ext4  acl,user_xattr,usrquota,grpquota 1 1

For file systems that support journaling, such as ext3 and ext4, we need to add additional options, e.g.
/dev/sda2 / ext4 acl,user_xattr,usrjquota=aquota.user,
grpjquota=aquota.group,jqfmt=vfsv0 1 1

3. Run quotacheck -amvug
This command is used to perform a scan in the file system for disk usage as well as to create aquota.group and aquota.user files. In our example, the aquota.group and aquota.user files are created in / directory.

4. Run edquota -u <username>
This command is used to set a quota for a particular user. The block here means how many blocks the user can use and the inode here means how many files the user can own.

5. Run repquota -av
This command is used to give a summary on the quotas.

Sunday, June 6, 2010

Create, Modify, and Delete User in Linux

These steps require root privilege.

To create a new user:
useradd -m -c "User1" -s /bin/bash user1
The -m option will automatically create a user's home directory, i.e. /home/user1.
The -s option is to specify the user's default shell.

To modify an existing user:
Suppose we want to add an additional group newgroup to user1.
In Ubuntu,
usermod -a -G newgroup user1
In openSUSE
usermod -A newgroup user1
Be aware that, if you specify only with -G, you need to include the group that you wish to add as well as the groups that have already been added. That's why in Ubuntu, it has additional switch, which is -a to append the group and -A in openSUSE.

To delete an existing user:
userdel -r user1

Saturday, June 5, 2010

Changing Default Editor in Linux

Some tools, such as visudo use nano editor by default. This is especially true in a distro like Ubuntu while openSUSE uses vi. As a vi user, I'm more comfortable editing a file using vi. To change it, add this line in the .bashrc.
export EDITOR=vim

Thursday, May 27, 2010

Creating New Partition and File System in Linux

Creating a new partition and file system, a.k.a formatting in Windows world is very easy. We can do that in just few clicks (right click, format, and select the file system). Creating a new partition and file system in Linux, however, is not that easy. Here I'm gonna show you how to do it.

You need to run these steps as root.
1. Run 'fdisk '
Example:
# fdisk /dev/sdb
If you're creating a new partition in the first SATA/SCSI disk, the device name will be /dev/sda, if it's a second SATA/SCSI disk, it'll be /dev/sdb and so on. If the device is the first master IDE disk, it'll be /dev/hda and so on.
2. Choose n to add a new partition.
3. Choose e for extended or p for primary.
4. Accept the default values for first and last cylinder if you wish to partition the whole disk.
5. Choose w to write the table to disk.
If suppose you've created the first partition in the second SATA/SCSI disk, you should be able to see that /dev/sdb1 has been created in your file system.
6. The next step is to create a file system on that partition. The command is 'mkfs -t '
Example:
# mkfs -t ext4 /dev/sdb1
The here can be ext2, ext3, ext4, etc. The here should be /dev/sdb1 and NOT /dev/sdb.
7. The last step is to mount this partition. To do that, execute 'mount -t '
Example:
# mkdir -p /mnt/newdisk
# mount -t ext4 /mnt/newdisk
To see if your partition has been mounted successfully. Run 'mount' or 'cat /etc/mtab'.
To mount it permanently, you need to create a new entry in the /etc/fstab as shown below.
/dev/sdb1            /mnt/newdisk         ext4       acl,user_xattr        1 2
The value last second option 1 specifies whether or not the file system should be dumped. 1 means dump. 0 means don't dump.
The value last option 2 specifies the order in which fsck should check the file system at reboot. For root partition, it should be 1, other partitions should be 2.

Sunday, May 23, 2010

Create ISO Images on Linux

Creating ISO images on Linux is very straightforward. Unlike in Windows, you don't need any additional tool to install. You just need to type this in your terminal.

dd if=/dev/cdrom of=image.iso

Most Linux distros nowadays have a graphical utility to let you mount the ISO image. If you prefer to do it in command line, here is how to do it.

mkdir -p /mnt/myimage
mount -o loop -t iso9660 cd.iso /mnt/myimage