Skip to content

Instantly share code, notes, and snippets.

@sonulohani
Last active June 6, 2023 12:03
Show Gist options
  • Save sonulohani/fa45dad30288a782d3e43ebcf6b8cd15 to your computer and use it in GitHub Desktop.
Save sonulohani/fa45dad30288a782d3e43ebcf6b8cd15 to your computer and use it in GitHub Desktop.

Pull images

podman pull <image-name>

Example: podman pull docker.io/library/ubuntu:22.04


Create container

To create a container image and enter into it using Podman, you can follow these steps:

Step 1: Create a Dockerfile Create a file named Dockerfile (without any file extension) with the following contents:

FROM <base-image>
RUN yum -y update && \
    yum -y install <required-packages> && \
    yum clean all
CMD ["/bin/bash"]

Replace <base-image> with the base image you want to use as the starting point for your container, such as centos or ubuntu. Replace <required-packages> with the necessary packages needed to run your GUI application.

Step 2: Build the Container Image To build the container image, run the following command:

podman build -t <image-name> .

Replace <image-name> with the desired name for your image.

Step 3: Run the Container and Enter into It To run the container and enter into it, use the following command:

podman run -it --name <container-name> -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix <image-name>

Replace <container-name> with the desired name for your container, and <image-name> with the name you used in the previous step.

The options -e DISPLAY=$DISPLAY and -v /tmp/.X11-unix:/tmp/.X11-unix are used to enable GUI support and allow the container to access the X11 socket on your host machine.

After running this command, you will be inside the container, and you can execute GUI applications.

Note: Make sure you have an X server running on your host machine before attempting to run GUI applications inside the container.


Start the container

To enter a previously exited container in Podman, you can use the podman start and podman exec commands. Here's the step-by-step process:

Step 1: List the Exited Containers To view the list of exited containers on your system, run the following command:

podman ps --all --filter status=exited

This will display a list of containers that are in an "exited" state along with their container IDs.

Step 2: Start the Container Choose the container you want to enter and start it by running the following command:

podman start <container-id>

Replace <container-id> with the ID of the container you want to enter.

Step 3: Enter the Container Once the container is started, you can enter it using the podman exec command:

podman exec -it <container-id> /bin/bash

Again, replace <container-id> with the ID of the container you started. This command will open a new shell session inside the container, allowing you to interact with it.

That's it! You have now entered the previously exited container using Podman.


Start the container with user

To enter a Podman container as a specific user with sudo access, you can use the podman exec command with the -u flag to specify the user. Here's the command:

podman exec -u <username> -it <container-id> /bin/bash

Replace <username> with the desired username of the user you want to enter as, and <container-id> with the ID of the container.

For example, if you want to enter the container as a user named "myuser" with sudo access, you can use the following command:

podman exec -u myuser -it <container-id> /bin/bash

Make sure that the user "myuser" exists inside the container and has sudo privileges configured.

After executing this command, you will be inside the container, logged in as the specified user with sudo access.


Add the user

If the group 'sudo' does not exist, you can try using the group 'wheel' instead. Here's the modified command:

sudo usermod -aG wheel myuser

After running this command, verify that the user 'myuser' has sudo access by switching to the new user account:

su - myuser

Then, run a command with sudo to confirm that sudo access is granted:

sudo ls /root

If the command runs successfully without asking for a password, it means the user has sudo access.


Add user to sudoers

If the user is not in the sudoers file, you will need administrative access to add the user to the sudoers file or modify the sudoers configuration. Here's how you can grant sudo access to a user in Linux:

  1. Log in to your Linux system with administrative privileges or switch to the root user.

  2. Open a terminal or command prompt.

  3. Edit the sudoers file using a text editor like visudo (which performs syntax checking) by running:

    sudo visudo

    This command will open the sudoers file in the default editor.

  4. Locate the section that defines user privileges (User privilege specification) or the group privileges (User alias specification or Runas alias specification).

  5. Add the following line to grant sudo access to the user:

    myuser ALL=(ALL) ALL
    

    Replace myuser with the actual username of the user you want to grant sudo access to.

    Alternatively, if you want to grant sudo access to a group, you can use:

    %mygroup ALL=(ALL) ALL
    

    Replace mygroup with the name of the group.

  6. Save the file and exit the text editor.

Now, the user should have sudo access. You can verify this by switching to the user and running a command with sudo:

su - myuser
sudo ls /root

If the command runs successfully without asking for a password, it means the user has sudo access.

Note: Be cautious when editing the sudoers file, as incorrect modifications can lead to system issues. Always use visudo to edit the sudoers file, as it performs syntax checks to prevent errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment