Wednesday, May 29, 2013

Upload a file to FTPES server using CURL

Security is a major concern when transferring file to another server. Nowdays, most of the file transfer is conducted over SSL which adds extra layer of security by encrypting the data. There are two types of such connections are possible.

1. Explicit over TLS server, which is also called FTPES
2. Implicit over TLS server, which is also called FTPS.

In this article, I will demostrate how to use Linux built-in and very powerful utility CURL to connect with FTPES servers.

Here is an example:

curl -T testfile.txt -k -v --ftp-pasv --disable-epsv --ftp-ssl domainname.com/directory

Lets break down above example to understand how it works.

The first part
curl
is fairly self explanatory, it invokes the curl command.

We then supply the -T option and the file which we want to upload.
-T testfile.txt

We then add an optional -k switch, which ignores any certificate related error.
-k

the -v just used for verbose output so we can see any error which may occur more easily.
-v

--ftp-pasv forces the connection to be made in passive mode. It connects in Active mode by default.

--ftp-ssl or --ssl tries to use SSL/TLS for the connection. It reverts to normal mode if server does not support SSL/TLS

At last but not least, provide your server's FQDN followed by the directory name.

Thursday, May 16, 2013

How to find Linux Kernel and release information


There are several commands to check Linux Kernel version and release information.

1. To check the Kernel Version

  • uname -v
2. To check Kernel Release
  • uname -r
3. To check system architecture
  • getconf  WORD_BIT
  • uname -m
4. To know every detail about system
  • uname -a
  • lsb_release -a
  • getconf -a

Please note that if you are aware of individual switches with each command, then it will be easy to use them in scripts. 

Wednesday, May 8, 2013

FTP authentication using PAM on Linux

This article explains how to configure PAM with VSFTP for authentication. It requires a database file that contains all the users and passwords.
To create a db format file, first create a plain text file e.g. 'virtual-users' with the usernames and passwords on alternating lines: It should look like as shown below:

user1
password1
user2
password3

Once usernames and passwords are added to the file, its time to create the database. You man need to install db_load command if it is already not there. Install is using yum install db4-utils

Execute following command to convert plain file to db format.# db_load -T -t hash -f virtual-users /etc/vsftpd/virtual-users.db

Now, create a PAM file /etc/pam.d/vsftpd-virtual which users your database. Add following lines in this file.
auth required pam_userdb.so db=/etc/vsftpd/virtual-users
account required pam_userdb.so db=/etc/vsftpd/virtual-users

Once done, restart VSFTP service. service vsftpd restart

Now you don't need to create system accounts for FTP use. Just add the new user and password in the file, rebuild the database and restart the service.

Tuesday, May 7, 2013

Listing files and directories using Python

This is an alternative to Linux ls command. A python script to list files and directories on Linux.

#!/usr/bin/python
import os,sys
def getSize(name):
    st = os.stat(name)
    return st.st_size
try:
    dirPath=str(sys.argv[1])
except:
    dirPath=str(os.getcwd())

for i in os.listdir(dirPath):
    if os.path.isdir(i):
        print 'Directory ',getSize(i),os.path.getatime(i),i
    elif os.path.islink(i):
        print 'Link ',getSize(i),os.path.getatime(i),i
    else:
        print 'File ',getSize(i),os.path.getatime(i),i

Monday, May 6, 2013

Linux Directory Structure


  • /bin
    • System binaries, including the command shell
  • /boot
    • Boot-up routine
  • /dev
    • Device files for all your peripherals
  • /etc
    • System configuration files
  • /home
    • User directories
  • /lib
    • Shared libraries and modules
  • /lost+found
    • Lost-cluster files, recovered from a disk-check
  • /mnt
    • Mounted file-systems
  • /opt
    • Optional software
  • /proc
    • Kernel-processes pseudo file-system
  • /root
    • Administrator’s home directory
  • /sbin
    • System administration binaries
  • /usr
    • User-oriented software
  • /var
    • Various other files: mail, spooling and logging

Wednesday, May 1, 2013

MySQL DB backup and restore

Backup all databases

    mysqldump -u root -p --all-databases > /var/mysql/backup/all.sql

Backup single database

    mysqldump -u root -p dbname > /var/mysql/backup/db.sql

Backup multiple databases

    mysqldump -u root -p --databases db1 db2 db3 > /var/mysql/backup/dbs.sql

Backup only specific tables in a database

    mysqldump -u root -p dbname tablename > /var/mysql/backup/table.sql

Restore database

    mysql -u root -p dbname < /var/mysql/backup/db.sql