How to use the command machinectl (with examples)

How to use the command machinectl (with examples)

Machinectl is a command-line tool that allows you to control the systemd machine manager. It is used to manage virtual machines, containers, and images. With machinectl, you can start and stop machines, list running machines, and open an interactive shell inside a machine.

Use case 1: Start a machine as a service using systemd-nspawn

Code:

sudo machinectl start machine_name

Motivation: Starting a machine as a service using systemd-nspawn is useful when you want to create a new machine and run it as a background service. This allows you to easily manage and control the machine from the command line.

Explanation:

  • sudo: This command is used to run machinectl with root privileges.
  • machinectl: The command that is used to control the systemd machine manager.
  • start machine_name: This argument tells machinectl to start the machine with the specified name.

Example output:

Started machine_name.

Use case 2: Stop a running machine

Code:

sudo machinectl stop machine_name

Motivation: Stopping a running machine is useful when you want to shut down a machine that is no longer needed or to perform maintenance tasks on it.

Explanation:

  • sudo: This command is used to run machinectl with root privileges.
  • machinectl: The command that is used to control the systemd machine manager.
  • stop machine_name: This argument tells machinectl to stop the machine with the specified name.

Example output:

Stopped machine_name.

Use case 3: Display a list of running machines

Code:

machinectl list

Motivation: Displaying a list of running machines allows you to quickly check the status of all virtual machines, containers, and images managed by systemd.

Explanation:

  • machinectl: The command that is used to control the systemd machine manager.
  • list: This argument tells machinectl to list all the running machines.

Example output:

MACHINE CLASS      SERVICE         OS     VERSION ADDRESSES
machine_name      container       n/a    n/a     n/a

Use case 4: Open an interactive shell inside the machine

Code:

sudo machinectl shell machine_name

Motivation: Opening an interactive shell inside a machine allows you to directly access and control the machine’s environment. This is useful for debugging or performing administrative tasks within the machine.

Explanation:

  • sudo: This command is used to run machinectl with root privileges.
  • machinectl: The command that is used to control the systemd machine manager.
  • shell machine_name: This argument tells machinectl to open an interactive shell inside the machine with the specified name.

Example output:

Connected to machine_name. Press ^] three times within 1s to exit session.

Conclusion

Machinectl is a powerful command-line tool for managing virtual machines, containers, and images. By using machinectl, you can start and stop machines, list running machines, and open an interactive shell inside a machine. This allows you to easily manage and control your virtual environments from the command line.

Related Posts

How to use the command gotelemetry (with examples)

How to use the command gotelemetry (with examples)

This article will provide an overview of various use cases for the gotelemetry command, a tool used for managing Go telemetry data and settings.

Read More
How to use the command 'query' (with examples)

How to use the command 'query' (with examples)

The ‘query’ command is a powerful tool used to display information about user sessions and processes.

Read More
How to use the command 'apx subsystems' (with examples)

How to use the command 'apx subsystems' (with examples)

This command is used to manage subsystems in apx. Subsystems are container-like entities that can be created based on pre-existing stacks.

Read More