This is a cache of https://docs.openshift.com/container-platform/4.14/cli_reference/openshift_cli/getting-started-cli.html. It is a snapshot of the page at 2024-11-22T14:54:08.270+0000.
Getting started with the OpenShift <strong>cli</strong> - OpenShift <strong>cli</strong> (oc) | <strong>cli</strong> tools | OpenShift Container Platform 4.14
×

About the OpenShift cli

With the OpenShift cli (oc), you can create applications and manage OpenShift Container Platform projects from a terminal. The OpenShift cli is ideal in the following situations:

  • Working directly with project source code

  • Scripting OpenShift Container Platform operations

  • Managing projects while restricted by bandwidth resources and the web console is unavailable

Installing the OpenShift cli

You can install the OpenShift cli (oc) either by downloading the binary or by using an RPM.

Installing the OpenShift cli by downloading the binary

You can install the OpenShift cli (oc) to interact with OpenShift Container Platform from a command-line interface. You can install oc on Linux, Windows, or macOS.

If you installed an earlier version of oc, you cannot use it to complete all of the commands in OpenShift Container Platform 4.14. Download and install the new version of oc.

Installing the OpenShift cli on Linux

You can install the OpenShift cli (oc) binary on Linux by using the following procedure.

Procedure
  1. Navigate to the OpenShift Container Platform downloads page on the Red Hat Customer Portal.

  2. Select the architecture from the Product Variant drop-down list.

  3. Select the appropriate version from the Version drop-down list.

  4. click Download Now next to the OpenShift v4.14 Linux client entry and save the file.

  5. Unpack the archive:

    $ tar xvf <file>
  6. Place the oc binary in a directory that is on your PATH.

    To check your PATH, execute the following command:

    $ echo $PATH
Verification
  • After you install the OpenShift cli, it is available using the oc command:

    $ oc <command>

Installing the OpenShift cli on Windows

You can install the OpenShift cli (oc) binary on Windows by using the following procedure.

Procedure
  1. Navigate to the OpenShift Container Platform downloads page on the Red Hat Customer Portal.

  2. Select the appropriate version from the Version drop-down list.

  3. click Download Now next to the OpenShift v4.14 Windows client entry and save the file.

  4. Unzip the archive with a ZIP program.

  5. Move the oc binary to a directory that is on your PATH.

    To check your PATH, open the command prompt and execute the following command:

    C:\> path
Verification
  • After you install the OpenShift cli, it is available using the oc command:

    C:\> oc <command>

Installing the OpenShift cli on macOS

You can install the OpenShift cli (oc) binary on macOS by using the following procedure.

Procedure
  1. Navigate to the OpenShift Container Platform downloads page on the Red Hat Customer Portal.

  2. Select the appropriate version from the Version drop-down list.

  3. click Download Now next to the OpenShift v4.14 macOS client entry and save the file.

    For macOS arm64, choose the OpenShift v4.14 macOS arm64 client entry.

  4. Unpack and unzip the archive.

  5. Move the oc binary to a directory on your PATH.

    To check your PATH, open a terminal and execute the following command:

    $ echo $PATH
Verification
  • Verify your installation by using an oc command:

    $ oc <command>

Installing the OpenShift cli by using the web console

You can install the OpenShift cli (oc) to interact with OpenShift Container Platform from a web console. You can install oc on Linux, Windows, or macOS.

If you installed an earlier version of oc, you cannot use it to complete all of the commands in OpenShift Container Platform 4.14. Download and install the new version of oc.

Installing the OpenShift cli on Linux using the web console

You can install the OpenShift cli (oc) binary on Linux by using the following procedure.

Procedure
  1. From the web console, click ?.

    click question mark
  2. click Command Line Tools.

    CLI list
  3. Select appropriate oc binary for your Linux platform, and then click Download oc for Linux.

  4. Save the file.

  5. Unpack the archive.

    $ tar xvf <file>
  6. Move the oc binary to a directory that is on your PATH.

    To check your PATH, execute the following command:

    $ echo $PATH

After you install the OpenShift cli, it is available using the oc command:

$ oc <command>

Installing the OpenShift cli on Windows using the web console

You can install the OpenShift cli (oc) binary on Windows by using the following procedure.

Procedure
  1. From the web console, click ?.

    click question mark
  2. click Command Line Tools.

    CLI list
  3. Select the oc binary for Windows platform, and then click Download oc for Windows for x86_64.

  4. Save the file.

  5. Unzip the archive with a ZIP program.

  6. Move the oc binary to a directory that is on your PATH.

    To check your PATH, open the command prompt and execute the following command:

    C:\> path

After you install the OpenShift cli, it is available using the oc command:

C:\> oc <command>

Installing the OpenShift cli on macOS using the web console

You can install the OpenShift cli (oc) binary on macOS by using the following procedure.

Procedure
  1. From the web console, click ?.

    click question mark
  2. click Command Line Tools.

    CLI list
  3. Select the oc binary for macOS platform, and then click Download oc for Mac for x86_64.

    For macOS arm64, click Download oc for Mac for ARM 64.

  4. Save the file.

  5. Unpack and unzip the archive.

  6. Move the oc binary to a directory on your PATH.

    To check your PATH, open a terminal and execute the following command:

    $ echo $PATH

After you install the OpenShift cli, it is available using the oc command:

$ oc <command>

Installing the OpenShift cli by using an RPM

For Red Hat Enterprise Linux (RHEL), you can install the OpenShift cli (oc) as an RPM if you have an active OpenShift Container Platform subscription on your Red Hat account.

You must install oc for RHEL 9 by downloading the binary. Installing oc by using an RPM package is not supported on Red Hat Enterprise Linux (RHEL) 9.

Prerequisites
  • Must have root or sudo privileges.

Procedure
  1. Register with Red Hat Subscription Manager:

    # subscription-manager register
  2. Pull the latest subscription data:

    # subscription-manager refresh
  3. List the available subscriptions:

    # subscription-manager list --available --matches '*OpenShift*'
  4. In the output for the previous command, find the pool ID for an OpenShift Container Platform subscription and attach the subscription to the registered system:

    # subscription-manager attach --pool=<pool_id>
  5. Enable the repositories required by OpenShift Container Platform 4.14.

    # subscription-manager repos --enable="rhocp-4.14-for-rhel-8-x86_64-rpms"
  6. Install the openshift-clients package:

    # yum install openshift-clients
Verification
  • Verify your installation by using an oc command:

$ oc <command>

Installing the OpenShift cli by using Homebrew

For macOS, you can install the OpenShift cli (oc) by using the Homebrew package manager.

Prerequisites
  • You must have Homebrew (brew) installed.

Procedure
  • Install the openshift-cli package by running the following command:

    $ brew install openshift-cli
Verification
  • Verify your installation by using an oc command:

$ oc <command>

Logging in to the OpenShift cli

You can log in to the OpenShift cli (oc) to access and manage your cluster.

Prerequisites
  • You must have access to an OpenShift Container Platform cluster.

  • The OpenShift cli (oc) is installed.

To access a cluster that is accessible only over an HTTP proxy server, you can set the HTTP_PROXY, HTTPS_PROXY and NO_PROXY variables. These environment variables are respected by the oc cli so that all communication with the cluster goes through the HTTP proxy.

Authentication headers are sent only when using HTTPS transport.

Procedure
  1. Enter the oc login command and pass in a user name:

    $ oc login -u user1
  2. When prompted, enter the required information:

    Example output
    Server [https://localhost:8443]: https://openshift.example.com:6443 (1)
    The server uses a certificate signed by an unknown authority.
    You can bypass the certificate check, but any data you send to the server could be intercepted by others.
    Use insecure connections? (y/n): y (2)
    
    Authentication required for https://openshift.example.com:6443 (openshift)
    Username: user1
    Password: (3)
    Login successful.
    
    You don't have any projects. You can try to create a new project, by running
    
        oc new-project <projectname>
    
    Welcome! See 'oc help' to get started.
    1 Enter the OpenShift Container Platform server URL.
    2 Enter whether to use insecure connections.
    3 Enter the user’s password.

If you are logged in to the web console, you can generate an oc login command that includes your token and server information. You can use the command to log in to the OpenShift Container Platform cli without the interactive prompts. To generate the command, select Copy login command from the username drop-down menu at the top right of the web console.

You can now create a project or issue other commands for managing your cluster.

Logging in to the OpenShift cli using a web browser

You can log in to the OpenShift cli (oc) with the help of a web browser to access and manage your cluster. This allows users to avoid inserting their access token into the command line.

Logging in to the cli through the web browser runs a server on localhost with HTTP, not HTTPS; use with caution on multi-user workstations.

Prerequisites
  • You must have access to an OpenShift Container Platform cluster.

  • You must have installed the OpenShift cli (oc).

  • You must have a browser installed.

Procedure
  1. Enter the oc login command with the --web flag:

    $ oc login <cluster_url> --web (1)
    1 Optionally, you can specify the server URL and callback port. For example, oc login <cluster_url> --web --callback-port 8280 localhost:8443.
  2. The web browser opens automatically. If it does not, click the link in the command output. If you do not specify the OpenShift Container Platform server oc tries to open the web console of the cluster specified in the current oc configuration file. If no oc configuration exists, oc prompts interactively for the server URL.

    Example output
    Opening login URL in the default browser: https://openshift.example.com
    Opening in existing browser session.
  3. If more than one identity provider is available, select your choice from the options provided.

  4. Enter your username and password into the corresponding browser fields. After you are logged in, the browser displays the text access token received successfully; please return to your terminal.

  5. Check the cli for a login confirmation.

    Example output
    Login successful.
    
    You don't have any projects. You can try to create a new project, by running
    
        oc new-project <projectname>
    

The web console defaults to the profile used in the previous session. To switch between Administrator and Developer profiles, log out of the OpenShift Container Platform web console and clear the cache.

You can now create a project or issue other commands for managing your cluster.

Using the OpenShift cli

Review the following sections to learn how to complete common tasks using the cli.

Creating a project

Use the oc new-project command to create a new project.

$ oc new-project my-project
Example output
Now using project "my-project" on server "https://openshift.example.com:6443".

Creating a new app

Use the oc new-app command to create a new application.

$ oc new-app https://github.com/sclorg/cakephp-ex
Example output
--> Found image 40de956 (9 days old) in imagestream "openshift/php" under tag "7.2" for "php"

...

    Run 'oc status' to view your app.

Viewing pods

Use the oc get pods command to view the pods for the current project.

When you run oc inside a pod and do not specify a namespace, the namespace of the pod is used by default.

$ oc get pods -o wide
Example output
NAME                  READY   STATUS      RESTARTS   AGE     IP            NODE                           NOMINATED NODE
cakephp-ex-1-build    0/1     Completed   0          5m45s   10.131.0.10   ip-10-0-141-74.ec2.internal    <none>
cakephp-ex-1-deploy   0/1     Completed   0          3m44s   10.129.2.9    ip-10-0-147-65.ec2.internal    <none>
cakephp-ex-1-ktz97    1/1     Running     0          3m33s   10.128.2.11   ip-10-0-168-105.ec2.internal   <none>

Viewing pod logs

Use the oc logs command to view logs for a particular pod.

$ oc logs cakephp-ex-1-deploy
Example output
--> Scaling cakephp-ex-1 to 1
--> Success

Viewing the current project

Use the oc project command to view the current project.

$ oc project
Example output
Using project "my-project" on server "https://openshift.example.com:6443".

Viewing the status for the current project

Use the oc status command to view information about the current project, such as services, deployments, and build configs.

$ oc status
Example output
In project my-project on server https://openshift.example.com:6443

svc/cakephp-ex - 172.30.236.80 ports 8080, 8443
  dc/cakephp-ex deploys istag/cakephp-ex:latest <-
    bc/cakephp-ex source builds https://github.com/sclorg/cakephp-ex on openshift/php:7.2
    deployment #1 deployed 2 minutes ago - 1 pod

3 infos identified, use 'oc status --suggest' to see details.

Listing supported API resources

Use the oc api-resources command to view the list of supported API resources on the server.

$ oc api-resources
Example output
NAME                                  SHORTNAMES       APIGROUP                              NAMESPACED   KIND
bindings                                                                                     true         Binding
componentstatuses                     cs                                                     false        ComponentStatus
configmaps                            cm                                                     true         ConfigMap
...

Getting help

You can get help with cli commands and OpenShift Container Platform resources in the following ways:

  • Use oc help to get a list and description of all available cli commands:

    Example: Get general help for the cli
    $ oc help
    Example output
    OpenShift client
    
    This client helps you develop, build, deploy, and run your applications on any OpenShift or Kubernetes compatible
    platform. It also includes the administrative commands for managing a cluster under the 'adm' subcommand.
    
    Usage:
      oc [flags]
    
    Basic Commands:
      login           Log in to a server
      new-project     Request a new project
      new-app         Create a new application
    
    ...
  • Use the --help flag to get help about a specific cli command:

    Example: Get help for the oc create command
    $ oc create --help
    Example output
    Create a resource by filename or stdin
    
    JSON and YAML formats are accepted.
    
    Usage:
      oc create -f FILENAME [flags]
    
    ...
  • Use the oc explain command to view the description and fields for a particular resource:

    Example: View documentation for the Pod resource
    $ oc explain pods
    Example output
    KIND:     Pod
    VERSION:  v1
    
    DESCRIPTION:
         Pod is a collection of containers that can run on a host. This resource is
         created by clients and scheduled onto hosts.
    
    FIELDS:
       apiVersion	<string>
         APIVersion defines the versioned schema of this representation of an
         object. Servers should convert recognized schemas to the latest internal
         value, and may reject unrecognized values. More info:
         https://git.k8s.io/community/contributors/devel/api-conventions.md#resources
    
    ...

Logging out of the OpenShift cli

You can log out the OpenShift cli to end your current session.

  • Use the oc logout command.

    $ oc logout
    Example output
    Logged "user1" out on "https://openshift.example.com"

This deletes the saved authentication token from the server and removes it from your configuration file.