Monday, December 22, 2014

FTP (File Transfer Protocol) Command Examples

The FTP (file transfer program) utility is used to transfer files between a local machine and remote network machine Using the File Transfer protocol. In simple terms it transfers / copies files between two computers. You can transfer files between unix systems and also non-unix systems like windows operating system using FTP.

The FTP command is simple to use and easy to learn. Let see useful examples of FTP command in detail.

FTP Command Examples:

If you are using windows operating system, open the command prompt and practice the below FTP commands. If you are using unix or linux operating systems, just simply type the ftp command on the terminal.

1. Connecting to Remote Host

First you need to connect to a remote host before doing any operations. You can use any one of the following methods to connect to a remote host. First method is
> ftp remote-server-name
connected to remote-server-name

Once the ftp connects to the remote server name, it will prompt you to enter the user name and password. After successful login, your terminal or prompt changes to "ftp>".

Another method is to use the open option with ftp command. This is shown below:
ftp>open remote-server-name
connected to remote-server-name

If the ftp command fails to connect to the remote server, then you will get the below error:
ftp: connect: Connection refused

2. Copy file from remote machine to local machine.

The get option is used to download or transfer a file from the remote system to the local system.
ftp> get windows-cleveland.bat

This will download the specified file (windows-cleveland.bat) from the remote systems current directory.

3. Copying multiple files from remote machine to local machine.

You can use the get to transfer multiple files from the remote host to local host.
ftp>get *.png

This will download all the png images to the local machine.

4. Transferring file from local server to remote server

The put option is used to copy the file from the local host to the remote host.
ftp>put linux-virtual-server.rpm

This command puts the rpm file into the remote machine.

5. Transferring multiple files to the remote server.

You can use the put option to transfer more than one file from local system to the remote system.
ftp>put *.rpm

6. Executing commands in remote machine.

After connecting to the remote network machine using the ftp, you can run commands like ls to list the files, cd to change directory and many more.
ftp> ls

This will list the files and directories in the remote machines current directory.

7. Executing commands in local machine.

Once you have connected to the remote host, to run the commands on local machine you need to exit from the ftp connection. Instead of this, there is a way to run commands on local host without exiting from the ftp connection. Use the ! Symbol before the command you want to run.
ftp> !ls

Now this will list the files in the local machines current directory.

8. Changing the file transferring mode.

You can change the file transfer modes to ascii and binary modes. Use the below commands to change the mode.

9. Deleting files on remote machine

You can use the delete or mdelete to remove a single file or multiple files in the remote machine.
ftp>delete linux-dedicated-server.dat
ftp>mdelete *.dat

10. Disconnecting from ftp connection.

Use the quit command to close the ftp connection.

11. Using FTP command in batch scripts

The following script reads the instructions from the dat file and executes them on the remote machine.
echo "Ftp command batch script"
echo "start"
ftp -s:instructions.dat remote-host
echo "End"

The contents of the instructions.dat file is
cd /var/tmp
put oracle_storage.exe

12. Getting the help about ftp command.

To know more about the ftp command, just type the help on the prompt. It will display the options/commands that you can use with ftp command.
Commands may be abbreviated.  Commands are:

!       disconnect mdelete  preserve  runique
$       edit       mdir     progress  send
account exit       get     prompt    sendport
append  form       mkdir    proxy     site
ascii   ftp        mls      put       size
bell    get        mode     pwd       sndbuf
binary  gate       modtime  quit      status
bye     glob       more     quote     struct
case    hash       mput     rcvbuf    sunique
cd      help       msend    recv      system
cdup    idle       newer    reget     tenex
chmod   image      nlist    rename    trace
close   lcd        nmap     reset     type
cr      less       ntrans   restart   umask
debug   lpwd       open     rhelp     user
delete  ls         page     rmdir     verbose
dir     macdef     passive  rstatus   ?

Friday, December 19, 2014

Linux Password file explained in detail

/etc/passwd file stores essential information, which is required during login i.e. user account information. /etc/passwd is a text file, that contains a list of the system's accounts, giving for each account some useful information like user ID, group ID, home directory, shell, etc. It should have general read permission as many utilities, like ls use it to map user IDs to user names, but write access only for the superuser (root).

Password file format

  1. Username: It is used when user logs in. It should be between 1 and 32 characters in length. Shows what is the Login Id for a user.
  2. Password: An x character indicates that encrypted password is stored in /etc/shadow file.
  3. User ID (UID): Each user must be assigned a user ID (UID). UID 0 (zero) is reserved for root and UIDs 1-99 are reserved for other predefined accounts. Further UID 100-999 are reserved by system for administrative and system accounts/groups.
  4. Group ID (GID): The primary group ID (stored in /etc/group file)
  5. User ID Info: The comment field. It allow you to add extra information about the users such as user's full name, phone number etc. This field use by finger command.
  6. Home directory: The absolute path to the directory the user will be in when they log in. If this directory does not exists then users directory becomes /
  7. Command/shell: The absolute path of a command or shell (/bin/bash). Typically, this is a shell. Please note that it does not have to be a shell.

See User List

/etc/passwd is only used for local users only. To see list of all users, enter:
$ cat /etc/passwd

To search for a username called tom, enter:
$ grep tom /etc/passwd

/etc/passwd file permission
The permission on the /etc/passwd file should be read only to users (-rw-r--r--) and the owner must be root:
$ ls -l /etc/passwd

-rw-r--r-- 1 root root 2659 Sep 17 01:46 /etc/passwd

Your password is stored in /etc/shadow file

Your encrpted password is not stored in /etc/passwd file. It is stored in /etc/shadow file

Thursday, December 18, 2014

How to set path in Linux ?

What is a path?
A path is a unique location to a file or a folder in a file system of an OS. A path to a file is a combination of / and alpha-numeric characters.

Why to set PATH variable?
PATH variable is system variable or environment variable used to store binaries/commands location/folder so that we no need to type complete path to execute the commands/binaries. If the PATH variable is not set you may get errors like “Command not found”. PATH variable can store multiple folders names with : as separator.

How to see PATH variable value?
$echo $PATH
How to set PATH variable?
Setting the path in bash or ksh or csh are one and the same. At the command prompt type below command to add new_folder_contain_binaries. 

Note: We have to retain previous PATH values to make sure all the commands work without any issue. That is the reason We given $PATH in front of new path. 

Example1: You just install java and want to set path to java executables or binaries stored in /usr/share/java
Example2: I have my scripts located in /home/linux/scripts. I want to execute my scripts with out running sh or bash before executing it.
For this we can set the path so that your shell scripts will now run as a shell script.
Once we set the path to our scripts we no need to run bash scriptname or ksh scriptname we can just write scriptname to execute it.

How to set PATH variable permanently?
When you set path variable as mentioned above it will just set the PATH variable for that session. If you want to keep this PATH variable after reboots too then you have to set it permanently. 

For single user:
Edit ~/.profile(for KSH shell ) or ~/.bashrc (for Bash shell) for adding PATH variable in it as shown below. 

export PATH=$PATH:/usr/share/java
Save the file and exit
once the PATH variable is set, we have to source these files to make this new PATH available, otherwise we have to reboot the machine if you don't want to source it. 

source ~/.profile
source ~/.bashrc
Some of the applications we have to set the path for them to work properly. We will see this in other post on how to set different ENV variables for different applications

if u like this topic please share on google+