Skip to main content

AI for Linux

Chapter #5: How to Automate Linux Admin Tasks with ShellGPT AI

ShellGPT is an AI-powered CLI tool that helps you generate commands, code, and docs right from your Linux terminal.

ShellGPT is another tool that we can use to make our terminal supercharged and powerful using the power of the OpenAI model.

ShellGPT, unlike other tools such as Fabric and TerminalGPT, is an automation and Linux sysadmin tool that helps manage and use Linux simply, offering lots of functionality and capabilities.

What is ShellGPT

ShellGPT is a Python library that uses, behind the scenes, the OpenAI model to achieve all the tasks assigned to it. Easy to install and use, all you need is one command, and you won’t need to leave the terminal again.

This tool, by default, uses OpenAI and doesn’t support other LLMs like the other tool we used before, but there is an option to use a local LLM hosted locally.

However, as stated in the documentation, the tool is not optimized for this purpose, making it the best choice to use an OpenAI key.

ShellGPT has many functionalities like other tools, offering text analysis, command creation, explanation, code debugging, and so on.

The cool thing this tool offers is that it can be integrated into the terminal, so you don’t need to type the name followed by the command each time.

Just type what you want directly in the terminal and use a hotkey, and the tool will execute it.

Installing ShellGPT in Linux

As we mentioned earlier, the tool uses Python, so you need to have Python installed on your machine to install it.

Next, use the pip installer to install the tool like this:

pip3 install shell-gpt

This will download and install the tool on your system. In case you don’t have pip installed, use the following command to install it:

sudo apt install python3-pip  [On Debian-based]
sudo dnf install python3-pip  [On RHEL-based]

In case you are using pipx, install the tool using the following command:

pipx install shell-gpt

After the installation, you may see an output indicating that you need to make the tool available system-wide by adding the path to the .bashrc file.

pipx ensurepath
source ~/.bashrc

After installing the tool, you’ll need an OpenAI key. If you don’t have one, you can create it using this link.

To check if the installation worked, run the command with the --help flag to see all the available options, like this:

sgpt --help

This will display the help menu of the tool, similar to the following:

ShellGPT Help Menu

Add API Key

As we mentioned earlier, sgpt uses OpenAI's LLMs by default to handle requests, so we need to provide an API key for it to work.

If you add the key to your environment variables, the tool will detect it automatically. To add the key to the environment variable, run the following command:

echo 'export OPENAI_API_KEY="your-api-key"' >> ~/.bashrc
source ~/.bashrc

Replace your-api-key with the key you obtained from the OpenAI website. That’s it! Whenever you use a tool that requires the API key, it will be detected automatically.

Running Docker with ShellGPT

One of the example use cases of sgpt is helping you create and manage Docker containers. Suppose you have a task where you need to run Apache in a Docker container but don’t know how to do it.

ShellGPT can help! First, you can ask for the steps using this prompt:

sgpt "Give me the steps to run Apache in Docker"

This will output the steps you need to follow, along with explanations for each of them. If you want sgpt to run the command directly, use the --shell or -s and --execute flag like this:

sgpt --shell --execute "run an Apache Docker image"

This will run the Apache Docker image.

To list all the running Docker containers, you can use the prompt:

sgpt --shell --execute "list all the running Docker containers"

You are using simple English commands to manage Docker containers.

User Management with ShellGPT

Another use case where you might need sgpt is managing a Linux system. Suppose you need to add a new user called tecmint to your system with a specific password, for example, tecmint2025, and you don’t know how to do it - sgpt is here for you!

The prompt will be:

sgpt --shell --execute "add a new user to the system called 'tecmint' with password 'tecmint2025'"

This will add a new user to your system.

When you set up sgpt for the first time, it detects your operating system and distribution, so all commands will work correctly for your specific distro, no matter which one you use.

You can shorten the command instead of using the flags --shell and --execute by doing it like this:

sgpt -se "your prompt"

Code Generation with ShellGPT

Like other tools, sgpt can also generate code, including Bash scripts or any other programming language.

To create code, use the --code or -c flag like this:

sgpt --code "generate a Bash script to create a backup for the /var/www directory"

The output will be a shell script that performs the backup.

sgpt can also take the output of another command as input, for example:

cat mybash.sh | sgpt "Check the code, find errors, and provide possible solutions"

Or use redirection like this:

sgpt "Explain this code" < mybash.sh

Here, we pass the file mybash.sh, which contains shell code, and ask sgpt to explain it.

Integrate sgpt into the Terminal

One of the best features of sgpt is its ability to integrate with the shell, so you don’t need to call the sgpt command manually each time.

Simply type your prompt directly, press the hotkey Ctrl + L, and sgpt will generate a command for you, replacing the text you entered with the actual command. All you need to do is press Enter to execute it.

To enable this feature, run the following command:

sgpt --install-integration

This feature supports both Bash and Zsh.

After installation, restart your terminal or run:

source ~/.bashrc    # For Bash
# OR
source ~/.zshrc     # For Zsh

Now you’re ready to use plain English directly in the terminal!

Using Linux Commands with ShellGPT

Once integration is enabled, try these practical examples by typing the prompt in your terminal and pressing Ctrl + L:

Let's find large files in a directory.

how to find files larger than 500MB in /var

Output Command:

find /var -type f -size +500M

Let's monitor real-time disk usage.

show real-time disk usage in human readable format

Output Command:

watch -n 2 df -h

This shell integration turns sgpt into a powerful Linux assistant, right inside your terminal. No more switching between browser tabs or man pages - just ask what you want to do, and let AI handle the syntax.

Conclusion

ShellGPT is another tool you may want to add to your AI toolset on Linux to boost productivity and assist in the learning curve if you are still in the learning stage.

In this article, we learned how to install ShellGPT on Linux and how to use it with various use cases and scenarios.