Command Line Interface

OpenShift ships with a feature rich web console as well as command line tools to provide users with a nice interface to work with applications deployed to the platform. The OpenShift tools are a single executable written in the Go programming language and is available for the following operating systems:

  • Microsoft Windows

  • Mac OS X

  • Linux

You might already have the OpenShift CLI available on your environment. You can verify it by running an oc command:

oc version

You should see the following (or something similar):

Client Version: openshift-clients-4.5.0-202006231303.p0-9-ge40bd2dd9
Server Version: 4.5.11
Kubernetes Version: v1.18.3+b0068a8

If the oc doesn’t exist or you have an older version of the OpenShift CLI, follow the next sections to install or update the OpenShift CLI. Otherwise, skip to the next lab.

From top right menu bar, click to the little white question mark icon, then click to Command Line Tools:

Download CLI

Scroll down to oc - OpenShift Command Line Interface (CLI)

CLI Binaries

Download and Install OpenShift CLI on Linux

Once the file has been downloaded, you will need to extract the contents as it is a compressed archive. I would suggest saving this file to the following directories:

~/OpenShift

Open up a terminal window and change to the directory where you downloaded the file. Once you are in the directory, enter in the following command:

The name of the oc packaged archive may vary. Adjust accordingly.
tar zxvf oc-linux.tar.gz

The tar.gz file name needs to be replaced by the entire name that was downloaded in the previous step.

Now you can add the OpenShift CLI tools to your PATH.

export PATH=$PATH:~/openShift

At this point, we should have the oc tool available for use. Let’s test this out by printing the version of the oc command:

oc version

You should see the following (or something similar):

Client Version: openshift-clients-4.5.0-202006231303.p0-9-ge40bd2dd9

If you get an error message, you have not updated your path correctly. If you need help, contact your lab instructor for assistence.

Download and Install OpenShift CLI on Mac

Once the file has been downloaded, you will need to extract the contents as it is a compressed archive. I would suggest saving this file to the following directories:

~/OpenShift

Open up a terminal window and change to the directory where you downloaded the file. Once you are in the directory, enter in the following command:

The name of the oc packaged archive may vary. Adjust accordingly.
tar zxvf oc-macosx.tar.gz

The tar.gz file name needs to be replaced by the entire name that was downloaded in the previous step.

Now you can add the OpenShift CLI tools to your PATH.

export PATH=$PATH:~/openShift

At this point, we should have the oc tool available for use. Let’s test this out by printing the version of the oc command:

oc version

You should see the following (or something similar):

Client Version: openshift-clients-4.5.0-202006231303.p0-9-ge40bd2dd9

If you get an error message, you have not updated your path correctly. If you need help, contact your lab instructor for assistence.

Download and Install OpenShift CLI on Windows

The CLI for Windows is provided as a zip archive. Download and unzip the archive with a ZIP program and move the oc binary to a directory on your PATH. To check your PATH, open the Command Prompt and run:

path

Checkout this blog post if you’d like to set it up with Powershell

At this point, we should have the oc tool available for use. Let’s test this out by printing the version of the oc command:

oc version

You should see the following (or something similar):

Client Version: openshift-clients-4.5.0-202006231303.p0-9-ge40bd2dd9

If you get an error message, you have not updated your path correctly. If you need help, contact your lab instructor for assistence.

Tab Completion

The OpenShift command line tool supports the ability to use tab completion for the popular zsh and bash shells. This suits the needs of users using either Linux or OS X. If you are using Microsoft Windows, never fear, we will discuss some ways to get tab completion working on that operating system as well.

Tab completion on Mac and Linux

If you are on the Mac operating system, you will need to ensure that you have the bash-completion project installed. This can be accomplished using the popular brew system:

brew install bash-completion

If you’re on Linux, ensure you have bash-completion installed using your package manager (dnf, yum apt-get,…​)

Once bash-completion package is available in your machine, to enable tab completion in your shell, you can simply enter in the following command from your terminal

oc completion bash >> oc_completion.sh
source oc_completion.sh

Alternatively, you can add this to your .bashrc file.

If you are using zsh, you can run the following command:

source <(oc completion zsh)

Alternatively, you can add this to your .zshrc file.

Tab completion on Windows

For Windows users, things become a bit more tricky. You could of course use the Linux Subsystem for Windows but you may want to consider using a combination of babun and cmder. For a full list of instructions, you can check out the following blog post:

Working with proxies

It might happen that you’re behind a corporate proxy to access the internet. In this case, you’ll need to set some additional environment variables for the oc command line to work.

Windows: Follow previous section’s instructions on how to set an Environment Variable on Windows. The variables you’ll need to set are:

Replace the proxy server with the one for your environment/machine.
https_proxy=http://proxy-server.mycorp.com:3128/
HTTPS_PROXY=http://proxy-server.mycorp.com:3128/

macOS:

Replace the proxy server with the one for your environment/machine.
export https_proxy=http://proxy-server.mycorp.com:3128/
export HTTPS_PROXY=http://proxy-server.mycorp.com:3128/

Linux:

Replace the proxy server with the one for your environment/machine.
export https_proxy=http://proxy-server.mycorp.com:3128/
export HTTPS_PROXY=http://proxy-server.mycorp.com:3128/

If the proxy is secured, make sure to use the following URL pattern, replacing the contents with the appropriate values:

export https_proxy=http://USERNAME:PASSOWRD@proxy-server.mycorp.com:3128/

Special Characters: If your password contains special characters, you must replace them with ASCII codes, for example the at sign @ must be replaced by the %40 code, e.g. p@ssword = p%40ssword.