Fabric – Automate Your Linux Administration Tasks and Application Deployments Over SSH

When it comes to managing remote machines and deployment of applications, there are several command line tools out there in existence though many have a common problem of lack of detailed documentation.

In this guide, we shall cover the steps to introduce and get started on how to use fabric to improve on administering groups of servers.

Automate Linux Administration Tasks Using Fabric
Automate Linux Administration Tasks Using Fabric

Fabric is a python library and a powerful command line tool for performing system administration tasks such as executing SSH commands on multiple machines and application deployment.

Read Also: Use Shell Scripting to Automate Linux System Maintenance Tasks

Having a working knowledge of Python can be helpful when using Fabric, but may certainly not be necessary.

Reasons why you should choose fabric over other alternatives:

  1. Simplicity
  2. It is well-documented
  3. You don’t need to learn another language if you’re already a python guy.
  4. Easy to install and use.
  5. It is fast in its operations.
  6. It supports parallel remote execution.

How to Install Fabric Automation Tool in Linux

An important characteristic about fabric is that the remote machines which you need to administer only need to have the standard OpenSSH server installed. You only need certain requirements installed on the server from which you are administering the remote servers before you can get started.


  1. Python 2.5+ with the development headers
  2. Python-setuptools and pip (optional, but preferred) gcc

Fabric is easily installed using pip (highly recommended), but you may also prefer to choose your default package manager yum, dnf or apt-get to install fabric package, typically called fabric or python-fabric.

For RHEL/CentOS based distributions, you must have EPEL repository installed and enabled on the system to install fabric package.

# yum install fabric   [On RedHat based systems]  
# dnf install fabric   [On Fedora 22+ versions]

For Debian and it’s derivatives such as Ubuntu and Mint users can simply do apt-get to install the fabric package as shown:

# apt-get install fabric

If you want to install development version of fabric, you may use pip to grab the most recent master branch.

# yum install python-pip       [On RedHat based systems] 
# dnf install python-pip       [On Fedora 22+ versions]
# apt-get install python-pip   [On Debian based systems]

Once pip has been installed successfully, you may use pip to grab the latest version of fabric as shown:

# pip install fabric

How to Use Fabric to Automate Linux Administration Tasks

So lets get started on how you can use Fabric. During the installation process, a Python script called fab was added to a directory in your path. The fab script does all the work when using fabric.

Executing commands on the local Linux machine

By convention, you need to start by creating a Python file called fabfile.py using your favorite editor. Remember you can give this file a different name as you wish but you will need to specify the file path as follows:

# fabric --fabfile /path/to/the/file.py

Fabric uses fabfile.py to execute tasks. The fabfile should be in the same directory where you run the Fabric tool.

Example 1: Let’s create a basic Hello World first.

# vi fabfile.py

Add these lines of code in the file.

def hello():
       print('Hello world, Tecmint community')

Save the file and run the command below.

# fab hello
Fabric Tool Usage
Fabric Tool Usage

Let us now look at an example of a fabfile.py to execute the uptime command on the local machine.

Example 2: Open a new fabfile.py file as follows:

# vi fabfile.py

And paste the following lines of code in the file.

#!  /usr/bin/env python
from fabric.api import local
def uptime():

Then save the file and run the following command:

# fab uptime
Fabric: Check System Uptime
Fabric: Check System Uptime

Executing commands on remote Linux machines to automate tasks

The Fabric API uses a configuration dictionary which is Python’s equivalent of an associative array known as env, which stores values that control what Fabric does.

The env.hosts is a list of servers on which you want run Fabric tasks. If your network is and wish to manage host and with your fabfile, you could configure the env.hosts as follows:

#!/usr/bin/env python
from  fabric.api import env
env.hosts = [ '', '' ]

The above line of code only specify the hosts on which you will run Fabric tasks but do nothing more. Therefore you can define some tasks, Fabric provides a set of functions which you can use to interact with your remote machines.

Although there are many functions, the most commonly used are:

  1. run – which runs a shell command on a remote machine.
  2. local – which runs command on the local machine.
  3. sudo – which runs a shell command on a remote machine, with root privileges.
  4. Get – which downloads one or more files from a remote machine.
  5. Put – which uploads one or more files to a remote machine.

Example 3: To echo a message on multiple machines create a fabfile.py such as the one below.

#!/usr/bin/env python
from fabric.api import env, run
env.hosts = ['','']
def echo():
      run("echo -n 'Hello, you are tuned to Tecmint ' ")

To execute the tasks, run the following command:

# fab echo
Fabric: Automate Linux Tasks on Remote Linux
Fabric: Automate Linux Tasks on Remote Linux

Example 4: You can improve the fabfile.py which you created earlier on to execute the uptime command on the local machine, so that it runs the uptime command and also checks disk usage using the df command on multiple machines as follows:

#!/usr/bin/env python
from fabric.api import env, run
env.hosts = ['','']
def uptime():
def disk_space():
     run('df -h')

Save the file and run the following command:

# fab uptime
# fab disk_space
Fabric: Automate Tasks on Multiple Linux Systems
Fabric: Automate Tasks on Multiple Linux Systems

Automatically Deploy LAMP Stack on Remote Linux Server

Example 4: Let us look at an example to deploy LAMP (Linux, Apache, MySQL/MariaDB and PHP) server on a remote Linux server.

We shall write a function that will allow LAMP to be installed remotely using root privileges.

For RHEL/CentOS and Fedora
#!/usr/bin/env python
from fabric.api import env, run
env.hosts = ['','']
def deploy_lamp():
  run ("yum install -y httpd mariadb-server php php-mysql")
For Debian/Ubuntu and Linux Mint
#!/usr/bin/env python
from fabric.api import env, run
env.hosts = ['','']
def deploy_lamp():
  sudo("apt-get install -q apache2 mysql-server libapache2-mod-php5 php5-mysql")

Save the file and run the following command:

# fab deploy_lamp

Note: Due to large output, it’s not possible for us to create a screencast (animated gif) for this example.

Now you can able to automate Linux server management tasks using Fabric and its features and examples given above…

Some Useful Options to Use with Fabric

  1. You can run fab –help to view help information and a long list of available command line options.
  2. An important option is –fabfile=PATH that helps you to specify a different python module file to import other then fabfile.py.
  3. To specify a username to use when connecting to remote hosts, use the –user=USER option.
  4. To use password for authentication and/or sudo, use the –password=PASSWORD option.
  5. To print detailed info about command NAME, use –display=NAME option.
  6. To view formats use –list option, choices: short, normal, nested, use the –list-format=FORMAT option.
  7. To print list of possible commands and exit, include the –list option.
  8. You can specify the location of config file to use by using the –config=PATH option.
  9. To display a colored error output, use –colorize-errors.
  10. To view the program’s version number and exit, use the –version option.


Fabric is a powerful tool and is well documented and provides easy usage for newbies. You can read the full documentation to get more understanding of it. If you have any information to add or incase of any errors you encounter during installation and usage, you can leave a comment and we shall find ways to fix them.

Reference: Fabric documentation

If you read this far, tweet to the author to show them you care. Tweet a thanks
Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

7 thoughts on “Fabric – Automate Your Linux Administration Tasks and Application Deployments Over SSH”

  1. Interesting tool, I heard a lot from educational sites before but…

    suppose 2 virtual machines hosting many client sites, say one in New York (NY) and another in Berlin (B).
    Suppose only your NY machine provides visual root login and works as a shepherd. How would you manage your NY or B machine?

    Quite easy: 1) create the php login subsystem in both machnes, 2) sftp bash scripts in both machnes, 3) create the interface between php and bash scripts in NY machine, 4) call commands on shepard that will be forwarded to client server. Done!

    The shell is powerfull, the only danger comes from login hijacking but then you have https, ip checking, ip black/white listing etc.

    Tell you the truth it worths making a bash template script with extended use of the source bash command and there you have it: config files, parameters in files, utility files (is integer, is character), task specific files etc.

    Nothing helpful messing with another language on top of bash I think or…. you can mesh with just because you can but see it as another project on top of the previous giant list!

  2. That’s a great tool. Thanks for sharing. Found one error though.
    In your last example, the second line should read:
    from fabric.api import env, sudo


Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.