Install VirtulBox and vagrant based on the description on the official website. We will actually set up a virtual machine, share (transfer) files between the host and the guest (virtual machine), and practice various other things.
The procedure for installing VirtualBox 7 in the Ubuntu 22.04 LTS CUI (command line interface) environment is as follows.
The APT source list file holds the list of repositories (package sources) from which Ubuntu downloads software packages. This file is usually located in the /etc/apt/sources.list
or /etc/apt/sources.list.d/
directory. According to the official page, you should put the following in /etc/apt/sources.list
: sudo vi Open a text editor by typing /etc/apt/sources.list and edit it.
deb [arch=amd64 signed-by=/usr/share/keyrings/oracle-virtualbox-2016.gpg] https://download.virtualbox.org/virtualbox/debian jammy contrib
This command is used to add the APT repository for VirtualBox to the repository list of the Ubuntu system. The meaning of each part of the command is as follows
deb
: This keyword represents an entry in the APT repository.deb
means Debian package repository, and since Ubuntu is Debian-based, this format is used.[arch=amd64 signed-by=/usr/share/keyrings/oracle-virtualbox-2016.gpg]
: This part provides two pieces of information:arch=amd64
indicates that this repository is a 64-bit AMD architecture (typically Modern PCs).signed-by=/usr/share/keyrings/oracle-virtualbox-2016.gpg
indicates that the packages in this repository are signed by the specified GPG key, and security.https://download.virtualbox.org/virtualbox/debian:
This is the URL of the repository where VirtualBox packages are hosted.jammy
: This refers to the release code name of Ubuntu, meaning Ubuntu 22.04 LTS (Jammy Jellyfish). This entry refers to the part of the repository that contains packages for Ubuntu 22.04.contrib
: This part refers to the section of the repository, in this case the “contrib” section. This is the section that contains software that is not free software, but works with free software.
This command line is usually added to the /etc/apt/sources.list
file or a dedicated list file in the /etc/apt/sources.list.d/
directory so that the system can get new versions and updates of VirtualBox The following command will do the job.
wget -O- https://www.virtualbox.org/download/oracle_vbox_2016.asc | sudo gpg --dearmor --yes --output /usr/share/keyrings/oracle-virtualbox-2016.gpg
This command is used to download the public key for the VirtualBox APT repository and register it as a trusted key for the system. Each part of the command is described below.
wget -O- https://www.virtualbox.org/download/oracle_vbox_2016.asc:
wget
is a command used to download files from the web.- -The
-O-
option tells it to send the downloaded file directly to standard output instead of saving it to disk. https://www.virtualbox.org/download/oracle_vbox_2016.asc
is the URL where the VirtualBox public key is hosted. This key is used to verify that VirtualBox packages are trustworthy.
|
:- This symbol is called a pipe and is used to pass the output of one command directly as input to another command. Here, the public key downloaded by
wget
is passed to the following command.
- This symbol is called a pipe and is used to pass the output of one command directly as input to another command. Here, the public key downloaded by
sudo gpg --dearmor --yes --output /usr/share/keyrings/oracle-virtualbox-2016.gpg
:sudo
is the command to run the command with administrative privileges (superuser).gpg
stands for GNU Privacy Guard, a tool for encryption and key management.- The
--dearmor
option converts keys in ASCII format (.asc files) to GPG keys in binary format. - The
--yes
option instructs the user to automatically answer “yes” to all prompts. --output /usr/share/keyrings/oracle-virtualbox-2016.gpg
instructs APT to save the converted binary key to the specified path. This path is one of the standard locations for APT to store keys it trusts.
The purpose of this entire command is to download the public key from VirtualBox’s APT repository so that the Ubuntu system can securely install VirtualBox packages.
Now we are ready to install VirtualBox. Now that we have added the new repository, update the package list and install
sudo apt-get update
sudo apt-get install virtualbox-7.0
Check the version after installation.
VBoxManage -v
Next, install Vagrant; Vagrant works very well with VirtualBox. In fact, VirtualBox is the default provider (backend) for Vagrant. This means that VirtualBox is one of the most commonly recommended and widely supported virtualization platforms when using Vagrant.
Reasons why VirtualBox and Vagrant are a good match
- Free of charge: VirtualBox is an open source virtualization software available for free; Vagrant is also open source and free. This allows you to build a development environment at no cost.
- Extensive platform support: VirtualBox runs on many host operating systems, including Windows, macOS, and Linux, and Vagrant also runs on these platforms, making it suitable for a wide range of development environments.
- Easy setup: With VirtualBox as the backend, Vagrant is very easy to set up. Basically, you just need to write the appropriate settings in the Vagrantfile, and with a single command, the virtual machine is up and running and the environment you need is built.
- Community ties: VirtualBox has been developed over many years and has extensive documentation and community support; Vagrant has a similarly strong community, and the combination of the two provides many resources and support.
- Abundance of Boxes: Vagrant uses “boxes” as templates for virtual machines; boxes for VirtualBox are very abundant and come pre-populated with a variety of operating systems and configurations.
The combination of VirtualBox and Vagrant is an attractive option for many developers because of its ease of use and accessibility, especially when building and managing development environments.
You can find the installation commands on the official page.
This sequence of commands is the procedure for installing Vagrant from the official HashiCorp repository on an Ubuntu system. The meaning of each command is as follows
- Download and add public key:
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
wget -O- https://apt.releases.hashicorp.com/gpg
: Download HashiCorp’s public key from the Internet.-The-O-
option redirects output to standard output.|
: The pipe symbol passes the output of the command on the left as input to the command on the right.sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
: Process the downloaded key with thegpg
command, dearmor (convert from ASCII format to binary format) and save it to/usr/share/keyrings/
. This allows APT to trust the HashiCorp package.
- Add HashiCorp’s APT repository:.
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
- This command adds HashiCorp’s APT repository information to the
/etc/apt/sources.list.d/hashicorp.list
file. lsb_release -cs
outputs the current Ubuntu release code name (e.g., “focal”). This will select the repository that matches your system version.
- This command adds HashiCorp’s APT repository information to the
- Update the package list and install Vagrant:.
sudo apt update && sudo apt install vagrant
sudo apt update
: Updates the APT package list and retrieves information about the newly added repositories.&&
: This symbol instructs the command on the right to be executed only if the command on the left succeeds.sudo apt install vagrant
: Installs Vagrant.
Running these commands will install Vagrant directly from the official HashiCorp repositories, ensuring that you have the latest and safest version of Vagrant available.
Check the version after the installation is complete.
vagrant version
Let’s actually create a virtual machine.
https://app.vagrantup.com/boxes/search
The general process of using Vagrant is to obtain a box (a template for a virtual machine) from Vagrant Cloud and then use the vagrant up
command to start the virtual machine. Here are the basic steps
- Create Vagrantfile:
First, create a directory for the Vagrant project and within that create a configuration file namedVagrantfile
. In this file, you will specify the name of the box to be used and the settings related to the VM (memory size, number of CPUs, etc.). - Selecting a box:
Select the boxes you need from Vagrant Cloud. A box is essentially a template for a virtual machine, offering a variety of operating systems and preconfigured environments. Examples include Ubuntu, CentOS, Windows, etc. - Specify a box in the Vagrantfile:
Specify the name of the box to be used for theVagrantfile
. For example, to use Ubuntu 18.04 LTS, writeVagrant.configure("2") do |config|
config.vm.box = "ubuntu/bionic64"
end
- Start the virtual machine:
Run the commandvagrant up
command from the command line in the Vagrant project directory. This command will start the virtual machine based on the settings in theVagrantfile
and download the box if necessary. - Connecting via SSH:
Once the virtual machine is up and running, you can connect to the virtual machine via SSH with thevagrant ssh
command. - Other operations:.
You can use commands such asvagrant halt
to stop the virtual machine,vagrant destroy
to destroy it, andvagrant reload
to reset its state.
Vagrant is very flexible and can also use provisioning scripts (Shell scripts, Ansible, Chef, Puppet, etc.) to perform additional setups when the virtual machine is started. This greatly simplifies the creation and management of the development environment.
The actual command is to execute the following in the appropriate directory This is not the Vagrantfile that I got from Vagrant Cloud, but I copied and pasted the one from the New tab.
mkdir ub22
cd ub22
vagrant init generic-x64/ubuntu2204
A Vagrantfile is then created. This file contains settings related to the VM (memory size, number of CPUs, etc.), but for now we will start the virtual machine with the default values.
vagrant up
If you have used Vagrant to launch a VirtualBox virtual machine, there are several commands that may be useful. These commands are useful for managing and manipulating the virtual environment. The main commands are listed below.
vagrant up
: Starts the virtual machine.vagrant halt
: Shut down the virtual machine.vagrant reload
: Restarts the virtual machine. This is especially useful when applying changes to a configuration file (e.g., Vagrantfile).vagrant ssh
: Connect to the virtual machine via SSH.vagrant status
: Checks the current status of the virtual machine (running, stopped, etc.).vagrant destroy
: Suspend and delete the virtual machine.vagrant suspend:
Suspend the virtual machine.vagrant resume
: Resume the suspended virtual machine.vagrant provision
: Runs a provisioning script to configure the virtual machine.vagrant box list
: Displays a list of available boxes (virtual machine templates).vagrant box update
: Updates the box to the latest version.
These commands are very useful for everyday Vagrant use. In particular, vagrant up
, vagrant halt
, vagrant ssh
, and vagrant destroy
are necessary commands for basic operations.
Once the virtual machine is up and running, you can then avoid contaminating the host machine’s environment, such as launching STABLE diffusion in CPU mode. Connect to the virtual machine via SSH with the command
vagrant ssh
From here, it is no different than if you were dealing with ubuntu 22.04. Once you are out of the virtual machine. When you leave the virtual machine, exit. I also checked the amount of memory in the virtual machine and it was 2GB.
As far as the Vagrantfile contents are concerned, the memory settings in the virtual machine are commented out.
# config.vm.provider “virtualbox” do |vb|
# # Display the VirtualBox GUI when booting the machine
# vb.gui = true
#
# # Customize the amount of memory on the VM:
# vb.memory = “1024”
# end
#
# View the documentation for the provider you are using for more
# information on available options.
In other words, this setting is not enabled, because the line vb.memory = "1024"
is commented out.
If the virtual machine is using 2GB of memory, this is due to the default setting of Vagrant or the virtual machine provider (in this case probably VirtualBox). In most cases, it is set to 1GB or 2GB.
To change the memory allocation, uncomment the appropriate section in the Vagrantfile and set it to the required amount of memory. For example, to set the memory to 4096 MB (4 GB), do the following
config.vm.provider "virtualbox" do |vb|
# Display the VirtualBox GUI when booting the machine
#vb.gui = true
# Customize the amount of memory on the VM:
vb.memory = "4096"
end
After making this change, the Vagrant virtual machine must be restarted. This is usually done with the vagrant reload
command. This will apply the new settings. You can confirm this by trying the free or lshw (which displays detailed system information (CPU, memory, motherboard, etc.)) command in the virtual machine.
From here, we will begin where we left off the other day. Install mpd on the music server. Install the client ncmpcpp on another computer to listen to music. You will learn port forwarding and music source settings.
Here is an SSH connection to the PC you are setting up. In the virtual machine, use the lspci
and lsmod
commands to see if any audio-related drivers are loaded and you will not find any.
vagrant@ubuntu2204:~$ lspci
00:00.0 Host bridge: Intel Corporation 440FX – 82441FX PMC [Natoma] (rev 02)
00:01.0 ISA bridge: Intel Corporation 82371SB PIIX3 ISA [Natoma/Triton II]
00:01.1 IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)
00:02.0 VGA compatible controller: InnoTek Systemberatung GmbH VirtualBox Graphics Adapter
00:03.0 Ethernet controller: Intel Corporation 82540EM Gigabit Ethernet Controller (rev 02)
00:04.0 System peripheral: InnoTek Systemberatung GmbH VirtualBox Guest Service
00:07.0 Bridge: Intel Corporation 82371AB/EB/MB PIIX4 ACPI (rev 08)
00:0d.0 SATA controller: Intel Corporation 82801HM/HEM (ICH8M/ICH8M-E) SATA Controller [AHCI mode] (rev 02)
Depending on the Vagrant Cloud box, there are differences even for the same OS. These differences are mainly due to the following factors
- Preset settings:
- Each box has presets for certain settings, environment variables, installed packages, etc. For example, some boxes are specialized for development environments and may come pre-installed with the necessary tools and libraries.
- Kernel Version and Drivers:
- Different boxes may use different kernel versions. This may affect, among other things, hardware support and system performance.
- Middleware and Applications:
- In certain boxes, databases, web servers, and other middleware and applications may be preconfigured and ready to use immediately.
- Customization and Optimization:
- Some box providers optimize their systems for specific use cases. This may include security enhancements, performance tuning, installation of specific tools, etc.
- VirtualBox and other provider extensions:
- Some boxes may be configured to use certain extensions (e.g., Guest Additions) of VirtualBox or other hypervisors.
- Default User Settings and Security Policies:
- Some boxes may have different default user accounts, password policies, SSH access settings, etc.
- Update Status:
- Some boxes are updated regularly, while others may not be. This may cause differences regarding security patches, bug fixes, new features, etc.
Understanding these differences is important in order to select the appropriate Vagrant box and avoid unexpected problems. When choosing the best box for a particular project or need, it is a good idea to check the box description, provider information, and update history.
Thus, there may be times when the sound is not recognized. When this happens, you will need to change the VirtualBox configuration to add audio support to your virtual machine. This is done by editing the Vagrantfile. Exit from the virtual machine. When you exit, it is EXIT.
Add the following settings to the Vagrantfile. This is the same as the audio state when the sound is successfully played in the GUI version of VirtulBox.
Vagrant.configure("2") do |config|
# ... Other settings ...
config.vm.provider "virtualbox" do |vb|
# ... Other VirtualBox settings ...
# Enable audio
vb.customize ["modifyvm", :id, "--audio", "alsa", "--audiocontroller", "ac97", "--audioout", "on"]
end
end
Here, change the "alsa"
part to the appropriate audio driver for the host OS (e.g., "alsa"
, "oss"
, "pulse"
). VirtualBox’s --audio
option allows you to specify a different audio driver for the host operating system. The available audio drivers depend on the type of host system. Common options are
- Windows:.
--audio dsound
: Uses DirectSound, the standard Windows audio system.--audio winmm
: uses Windows Multimedia API.
- Linux: -audio
--audio alsa
: Uses the Advanced Linux Sound Architecture (ALSA), the most commonly used audio system in Linux.--audio pulse
: Uses PulseAudio. This is the standard audio system in some Linux distributions.
- macOS:
--audio coreaudio
: uses Core Audio, the standard audio system for macOS.
This setting specifies that the VirtualBox virtual machine should enable audio output and use AC97 as the audio controller. Reboot using the vagrant reload
command as before.
Install the following tools and check the audio source. It was not installed by default.
sudo apt install alsa-utils
Installing alsa-utils
To install the alsa-utils
package, execute the following command
sudo apt install alsa-utils
This command will install the alsa-utils
package and its dependencies.
Checking the audio device
After installation, you can use the aplay -l
command to view a list of audio devices connected to the system. This command provides details on available sound cards and digital audio devices.
aplay -l
The output of this command includes the card number, device number, device name, and subdevice information for each audio device. This is useful information for audio configuration and troubleshooting. alsa-utils
package contains several useful command line tools for managing the ALSA (Advanced Linux Sound Architecture) system The alsa-utils package contains several useful command line tools for managing the ALSA (Advanced Linux Sound Architecture) system. These tools allow you to configure, test, and troubleshoot your audio devices. The following are the main commands among them
alsamixer
:- An interactive text-based mixer.
- It is used to adjust volume levels and unmute audio cards.
amixer
:- Used to adjust ALSA mixer settings from the command line.
- Useful for setting up audio in scripts or shell commands.
aplay
:- This command is used to play WAV or other audio files.
- Often used to test if the audio output is functioning properly.
arecord
:- This command is used to record sound from an audio input device.
- It can create a recording file in WAV format.
alsactl
:- This command is used to save and restore ALSA card settings.
- Helps maintain audio settings when the system is rebooted.
speaker-test
:- This command is used to test speakers and channels.
- It is useful for testing left and right speakers and multi-channel audio settings.
These commands are useful in a variety of scenarios, including fine-tuning audio settings, diagnosing problems, and audio testing. In particular, alsamixer
is a very useful tool for adjusting audio levels and unmuting settings. in many Linux distributions, such as Ubuntu, a command called aplay
is included in the alsa-utils
package. aplay
is part of the ALSA ( It is part of the Advanced Linux Sound Architecture (ALSA) system and is used to display information about audio devices and to play audio files from the command line.
What to do if you encounter the following problem here, which is also the case with the alsamixer command
vagrant@ubuntu2204:~$ aplay -l
aplay: device_list:274: no soundcards found…
vagrant@ubuntu2204:~$ sudo aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: I82801AAICH [Intel 82801AA-ICH], device 0: Intel ICH [Intel 82801AA-ICH]
Subdevices: 1/1
Subdevice #0: subdevice #0
The aplay command indicates that the sound card is not found by the normal user, but that the sound card is recognized when sudo
is used. This suggests that there may be a problem with the permissions to access the audio device. It indicates that the normal user does not have the necessary permissions to access the audio device. This often occurs when a user is not a member of the audio
group.
In Linux, access to certain hardware devices is controlled by specific groups. Access to audio devices is usually assigned to the audio
group. If a user is not a member of this group, the audio device cannot be accessed with normal privileges, but can be accessed using sudo
(superuser privileges).
To resolve this issue, the user must be added to the audio
group. The following command can be used to add the user to the audio
group
sudo usermod -a -G audio username
After executing this command, the system must be rebooted or the user must log out and log in again for the changes to take effect. After that, the user should be able to access the audio device using the aplay
command with normal user privileges. View a list of all groups on the system. after verifying that audio is present, the vagrant user is added to the audio group.
getent group
sudo usermod -a -G audio vagrant
The actual aplay -l command is entered as follows
vagrant@ubuntu2204:~$ aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: I82801AAICH [Intel 82801AA-ICH], device 0: Intel ICH [Intel 82801AA-ICH]
The device was also found in lspci.
vagrant@ubuntu2204:~$ lspci
00:00.0 Host bridge: Intel Corporation 440FX – 82441FX PMC [Natoma] (rev 02)
00:01.0 ISA bridge: Intel Corporation 82371SB PIIX3 ISA [Natoma/Triton II]
00:01.1 IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)
00:02.0 VGA compatible controller: InnoTek Systemberatung GmbH VirtualBox Graphics Adapter
00:03.0 Ethernet controller: Intel Corporation 82540EM Gigabit Ethernet Controller (rev 02)
00:04.0 System peripheral: InnoTek Systemberatung GmbH VirtualBox Guest Service
00:05.0 Multimedia audio controller: Intel Corporation 82801AA AC’97 Audio Controller (rev 01)
00:07.0 Bridge: Intel Corporation 82371AB/EB/MB PIIX4 ACPI (rev 08)
00:0d.0 SATA controller: Intel Corporation 82801HM/HEM (ICH8M/ICH8M-E) SATA Controller [AHCI mode] (rev 02)
Next, install mpd.
MPD Overview:
- Server/Client Model: MPD acts as a server and handles music playback, management, and playlist manipulation. The user, on the other hand, communicates with the server using a client application to select songs, adjust volume, control playback, etc.
- Diverse client support: MPD supports a variety of client application types, ranging from text-based, graphical, and web-based interfaces.
- Network capability: MPD allows you to control your music remotely within your local network or over the Internet. This allows, for example, the same music to be played in different rooms of the home at the same time.
- Support for a wide variety of music formats: MPD supports many music formats, including MP3, FLAC, OGG, and WAV.
- Playlist and database management: Users can create playlists and manage their music libraries; MPD maintains a database of music files for efficient searching and access.
apt update
and apt install mpd
are commands used by Debian-like Linux distributions (including Ubuntu and others). These commands are used to update the system package list ( apt update
) and then install mpd
(Music Player Daemon) ( apt install mpd
).
sudo apt update
sudo apt install mpd
The role of each command here is as follows
apt update
: This command updates the system package list. It downloads information about available packages and their latest versions from the repository to the system. It is common to run this command before installing or upgrading a package.apt install mpd
: This command installsmpd
(Music Player Daemon) on the system.mpd
is a server-based application for music playback, used to play and control music over the network.
Check the status of mpd, it is stopped immediately after installation.
sudo systemctl status mpd
Start it and also configure it for automatic startup.
sudo systemctl start mpd
sudo systemctl enable mpd
systemctl start mpd
: This command starts thempd
service immediately. Immediately after installation,mpd
does not start automatically and must be started manually with this command.systemctl enable mpd
: This command “enables” thempd
service. In other words, it sets upmpd
to start automatically at system startup. This only needs to be done once, andmpd
will start automatically on subsequent system startups.
When configuring mpd
(Music Player Daemon), you must edit the mpd.conf
file to specify that this audio device should be used.
sudo vi /etc/mpd.conf
Configure the audio output section of mpd.conf
based on the output of aplay -l
. Here is an example of a typical configuration
audio_output {
type "alsa"
name "Intel 82801AA-ICH"
device "hw:0,0" # card 0, device 0
mixer_type "software" # use software mixer
}
The important point here is the device
parameter. This is set based on the output of aplay -l
. In the example we use hw:0,0
, which refers to the first device on the first sound card. This corresponds to card 0:
and device 0:
as displayed in the output of aplay -l
.
Other parameters should be adjusted according to your environment and preferences. For example, format
sets the audio sampling rate, bit depth, and number of channels; mixer_type
, mixer_device
, and mixer_control
are settings related to volume control.
After setting the settings, mpd
must be restarted. This is usually done with the following command (may vary by system)
sudo systemctl restart mpd
Next, the music files must be placed in the virtual machine, ubuntu. However, if you are using Vagrant, there are several ways to transfer files to the VirtualBox virtual machine, but there is no direct “file transfer” command in Vagrant. Instead, you can use the following methods
1. Using a synchronization folder
Vagrant provides the ability to synchronize folders between the host and guest (virtual) machines. This can be done by writing settings in the Vagrantfile.
For example, to synchronize a specific folder on the host machine to a specific path in the virtual machine, add the following line to the Vagrantfile
config.vm.synced_folder "path on host", "/path on guest"
This configuration will synchronize the specified folder on the host machine with the specified path in the virtual machine.
2. Using SCP (Secure Copy)
Vagrant does not have a direct file transfer command, but since you are connecting to the virtual machine using SSH, you can use SCP (Secure Copy Protocol) to transfer files.
For example, to transfer a file from the host machine to the virtual machine, use the following command
scp -P [port_number] [path_to_file_to_transfer] vagrant@[VM_IP_address]:[destination_path]
Where [port number]
is the SSH port used by Vagrant (usually 2222
) and [VM_IP_address]
is the IP address of the virtual machine ( 127.0.0.1
by default). If you use Vagrant’s default SSH key, you must specify it with the -i option.
3. Using Vagrant’s SSH functionality
Vagrant has a vagrant ssh
command that can be used to SSH into the virtual machine. After connecting, you can manipulate files using the usual Linux commands. After connecting, use scp, rsync, or sftp to transfer files. In fact, rsync is used to transfer files between instances of cloud services.
For example, if you want to synchronize a specific folder on the host machine to a specific path in the virtual machine, add a line like the following to your Vagrantfile
Vagrant.configure("2") do |config|
# Other configurations...
config.vm.synced_folder "path on host", "/path on guest"
config.vm.provider "virtualbox" do |vb|
# ... Other VirtualBox settings ...
# other configurations ...
end
Since we are now in a local environment, we recommend setting up a synchronization folder. The following is the actual command entered. A restart of the virtual machine is required.
config.vm.synced_folder "/home/mamu/music", "/var/lib/mpd/music"
The /home/mamu/music
directory on the host machine is synchronized with the /var/lib/mpd/music
directory on the guest machine.
File changes on the host machine are reflected on the guest machine in real time, and vice versa. However, there are a few things to keep in mind
- Permissions and ownership: Problems can occur if file permissions and ownership differ between the host and guest. This is especially important if the service is running with specific user permissions.
- File system differences: Compatibility issues may arise if the host and guest use different file systems.
- Performance: Synchronized folders can affect performance, especially when dealing with large numbers of files.
If properly configured, this feature can be very useful and play an important role in software development and testing environments.
Memo:
When configuring the synchronization folder in the Vagrantfile, it is sufficient to add the config.vm.synced_folder
line. This setting must be outside the config.vm.provider "virtualbox"
block. This is because the synchronization folder setting is not dependent on a specific provider, in this case VirtualBox.
Next, consider the communication between the host and the virtual machine. When you connect to the virtual machine via SSH, you will see the following message. Here, the actual IP address of the host is 192.168.0.139.
root@g570:~/ub22# vagrant ssh
Last login: Thu Dec 14 03:23:59 2023 from 10.0.2.2
vagrant@ubuntu2204:~$
The message “Last login: [date] from 10.0.2.2” displayed during SSH connection by Vagrant indicates the IP address of the client (in this case, the host machine) that made the SSH connection. However, the following explains why the IP address displayed here is different from the actual IP address of the host machine (in this case, 192.168.0.139).
- NAT Networking:
- Vagrant uses NAT (Network Address Translation) networking mode by default. In this mode, the virtual machine is placed on a private internal network (in this case in the 10.0.2.x range) and the host machine acts as a NAT device.
- Therefore, from the virtual machine’s perspective, the SSH connection appears to come from the host machine’s NAT interface (10.0.2.2). This is different from the actual IP address of the host machine (192.168.0.139).
- Virtual network interface:
- 10.0.2.2 is the IP address of the virtual network interface created by VirtualBox (or other virtualization software). This represents the IP address of the host machine as seen from inside the virtual machine.
- Relation to the actual network:
- The actual IP address of the host machine (192.168.0.139) is for the physical network. However, from inside the Vagrant virtual machine, a different IP address is displayed because it is connected via NAT.
In essence, the IP address of 10.0.2.2 is the internal IP address of the host machine in NAT mode, which is different from the IP address on the host machine’s external network (192.168.0.139). This is a virtual address that manages communication between the virtual machine and the host machine.
In fact, to set up port forwarding between the host and guest (virtual machine) when using Vagrant, add a specific setting to your Vagrantfile
. In this case, we add a setting to forward port 6600 on the guest to port 6600 on the host, since MPD uses port 6600.
Here is an example Vagrantfile
configuration for this purpose
Vagrant.configure("2") do |config|
# Other settings...
# Configure port forwarding.
# Forward guest port 6600 to host port 6600
config.vm.network "forwarded_port", guest: 6600, host: 6600
# Other settings...
end
After adding this configuration to the Vagrantfile
, the virtual machine must be restarted ( vagrant reload
). You will now be able to access port 6600 on the guest machine through port 6600 on the host machine.
When port forwarding is configured in Vagrant, it appears to the virtual machine (guest) that communications from the host machine are coming from a specific internal IP address (usually 10.0.2.2
). This is because Vagrant uses the NAT network mode of VirtualBox (or other virtualization software).
In NAT (Network Address Translation) mode, the virtual machine is in an isolated network from the host machine. Within this isolated network, the host machine acts as a gateway with a specific IP address ( 10.0.2.2
) to the virtual machine.
As a result, from the virtual machine’s perspective, it is perceived as if the communication is coming from 10.0.2.2
, rather than the host machine’s actual IP address (in this case 192.168.0.139
). Using port forwarding, communication to a specific port on the host (6600 in this example) is automatically forwarded to the same port on the virtual machine, but the source IP address of this communication is recognized as 10.0.2.2.
In summary
- Host machine:.
- IP address: 192.168.0.139
- Network interface: physical network adapter
- Vagrant Virtual Machine: Vagrant Virtual Machine
- NAT Interface: NAT
- IP address: 10.0.2.15
- Connection: Host machine (via NAT)
- Features: Accessible via port forwarding from host machine
- Bridge interface: (via NAT)
- IP address: Same segment as host machine (e.g. 192.168.0.x)
- Connection: Same physical network as host machine
- NAT Interface: NAT
- Port Forwarding: same physical network as host machine
- Port 6600 on the host machine is forwarded to port 6600 on the Vagrant virtual machine
- Connection to external network:
- Host machine and Vagrant virtual machine can connect to external networks such as the Internet
[Host Machine]
IP: 192.168.0.139
|--- [NAT interface (Vagrant VM)]
|--- [NAT interface (Vagrant VM)
| IP: 10.0.2.15
| Port forwarding: 6600 -> 6600
| | |--- [Bridge interface (Vagrant VM)
|--- [Bridge interface (Vagrant VM)
IP: 192.168.0.x (same segment as host)
To access mpd, the music server of the virtual machine for the above reasons, from a non-virtual machine, consider the following
If the software running on the virtual machine (in this case MPD) is bound only to localhost
(i.e., 127.0.0.1)
, the service is not directly accessible from outside the virtual machine. This is because localhost
only allows connections from the local host (within the same machine).
To allow connections from outside (in this case, the host machine or other machines in the network), the software must be bound to the virtual machine’s actual network interface. This is usually accomplished by specifying a specific IP address or by binding to 0.0.0.0
, which represents all interfaces.
For example, even if it is a web server, changing its configuration file from Listen 127.0.0.1:80
to Listen 0.0.0.0:80
will make that server accessible through all network interfaces of the virtual machine.
The actual work done is to open the configuration file in the virtual machine and change bind_to_address from localhost. A reboot is also required (sudo systemctl restart mpd).
sudo vi /etc/mpd.conf
Change details
localhost -> 0.0.0.0
This change should be done carefully from a security standpoint; binding to 0.0.0.0
makes the service accessible from any machine on the network, so it is important to ensure proper firewall settings and authentication mechanisms.
You should now be able to connect to mpd from your host or another computer using the client. Install the replay client on the host.
sudo apt install ncmpcpp
Before I forget, allow traffic on port 6600 if you have a firewall enabled.
sudo ufw allow 6600/tcp
Now that we have the music file ready to go, let’s start the client on the host and play it.
ncmpcpp -h 192.168.0.139
If you omit the IP address, it will connect to localhost, so the following is OK
ncmpcpp
The host itself is connected via SSH, so the sound is played on the remote PC, not on the SSH-connected PC. So far, this is the case where the sound is successfully played on the host. The client is like a remote control, since the sound is emitted from the server. Now we want the remote PC to produce sound as well, using PulseAudio, but now we have the following situation.
As of 2023, many Linux distributions are migrating from PulseAudio to PipeWire, a new multimedia framework aimed at integrating audio and video processing. It is designed as an alternative to JACK (Jack Audio Connection Kit) for low-latency audio processing and professional audio needs.
Key features of PipeWire
- Low Latency: Provides low-latency audio processing suitable for professional audio workflows.
- Network Transparency: PipeWire is network transparent, allowing audio and video streaming over the network.
- Security and Sandboxing: PipeWire is compatible with Flatpak and other sandbox environments for added security.
- PulseAudio and JACK compatibility: Emulates PulseAudio and JACK APIs to maintain compatibility with existing applications.
MPD and PipeWire
When using MPD (Music Player Daemon) with PipeWire, MPD must be configured for PipeWire, as PipeWire emulates PulseAudio’s API, so in many cases, MPD configured for PulseAudio will be compatible with PipeWire. MPDs configured for PulseAudio are directly compatible with PipeWire. However, for optimal performance and compatibility, it is recommended that MPD settings be reviewed and adjusted as necessary.
Notes
- Status in Transition: As of 2023, many systems are being transitioned from PulseAudio to PipeWire. This transition will be done in phases, so PulseAudio may still be the default audio server for certain distributions and environments.
- Configuration Changes: As we transition to PipeWire, audio-related settings and troubleshooting methods may change. It is important to consult PipeWire documentation and community support, especially if you have custom audio settings or advanced audio processing.
PipeWire is an important step in strengthening the Linux multimedia ecosystem by centralizing audio and video processing. As such, it is expected to play a central role in future Linux-based audio/video applications and services.
As mentioned above, the trend seems to be turning to PipeWire to install and configure PulseAudio. PulseAudio is configured on a per-user basis and has the following features
It is not recommended to use PulseAudio as root user. Or rather, the commands were restricted as root. There are several reasons
- Security risk: Running an application as the root user gives that application full access to the entire system. This means that if the application is vulnerable, the entire system could be compromised.
- Permissions issues: PulseAudio is configured and runs on a per-user basis; running as root may not integrate properly with user-specific settings and environments.
- Compatibility Issues: Some programs and system components are designed to be run as a non-root user; running PulseAudio as root may cause compatibility issues with these components.
In general, running applications as root on Linux systems should be avoided unless necessary. This is an important principle for maintaining security and system stability, since commands are entered as a general user in the PulseAudio matter. PulseAudio is also installed within a virtual environment.
PulseAudio is a convenient sound server for the Linux operating system. A sound server is software that receives sound streams from various audio applications, manages them, and outputs them to appropriate audio devices (speakers, headphones, etc.).
PulseAudio’s main features include
- Network transparency: Audio streams can be sent and received over a network as well as locally.
- Device management: Many audio devices can be managed simultaneously, allowing users to easily switch output devices and adjust volume.
- Mixing and stream management: Audio streams from multiple applications can be handled simultaneously, and volume can be adjusted for each application individually.
- Advanced features: Provides a variety of advanced audio processing features such as equalizers and voice effects.
- Extensibility and flexibility: Extends functionality through plug-ins and modules to flexibly accommodate different audio environments.
Actual commands entered on the host.
sudo apt install alsa-utils pulseaudio
Install the same within the virtual environment. Within the virtual environment, since alsa-utils was installed earlier, we will install PulseAudio. the command to check the status of PulseAudio is the following, since it is configured and runs on a per-user basis.
systemctl --user status pulseaudio
However, something seems to be wrong.
mamu@g570:~/ub22$ systemctl –user status pulseaudio
Warning: The unit file, source configuration file or drop-ins of pulseaudio.ser>
○ pulseaudio.service
Loaded: masked (Reason: Unit pulseaudio.service is masked.)
Active: inactive (dead)
I tried stopping and starting the service with pulseaudio -k and pulseaudio –start, but it did not work. After restarting, the virtual machine is also started up.
mamu@g570:~$ systemctl –user status pulseaudio
● pulseaudio.service – Sound Service
Loaded: loaded (/usr/lib/systemd/user/pulseaudio.service; enabled; vendor >
Active: active (running) since Thu 2023-12-21 20:25:13 JST; 4s ago
TriggeredBy: ● pulseaudio.socket
Main PID: 1525 (pulseaudio)
Tasks: 4 (limit: 9321)
Memory: 10.5M
CPU: 261ms
CGroup: /user.slice/user-1000.slice/user@1000.service/session.slice/pulsea>
mq1525 /usr/bin/pulseaudio –daemonize=no –log-target=journal
Dec 21 20:25:09 g570 systemd
Dec 21 20:25:13 g570 systemd
The response on the net was that PulseAudio was installed from the beginning on the desktop for ubuntu22.04. However, it was not installed on ubuntu 22.04 Server. ubuntu 22.04 Desktop editions usually have PulseAudio pre-installed, but Ubuntu 22.04 Server editions do not have it by default This is because the desktop environment is typically used for audio. This is because desktop environments typically rely more heavily on audio capabilities, whereas server environments do not. First, we will discuss the PulseAudio configuration for setting up audio streaming via PulseAudio from a virtual machine (MPD Server) to a client PC.
The current configuration includes several commented-out modules ( module-esound-protocol-tcp
, module-native-protocol-tcp
, module-zeroconf-publish
) and the enabled module-tunnel-sink
is included.
We have considered two scenarios in which sound is emitted on a remote PC; the first is shown below.
Here is a description of the PulseAudio module used to set up audio streaming from a virtual machine (MPD server) to a client PC via PulseAudio.
- module-native-protocol-tcp: This module allows connection to the PulseAudio server over the network. It is used when the client needs to connect to the server remotely. However, if the MPD server only connects to the PulseAudio server on the client PC, this module is not needed.
- module-zeroconf-publish: Publish the PulseAudio server to the network using Zeroconf (or Bonjour). In this scenario, the MPD server connects directly to the client PC, so this module is also not needed.
- module-tunnel-sink: This is used for one PulseAudio server to connect to another PulseAudio server to send audio streams. In this scenario, this module is needed for the MPD server (PulseAudio on the virtual machine) to connect to the PulseAudio server on the client PC.
The second is as follows
- module-native-protocol-tcp: This module is used to allow PulseAudio connections via TCP. In this scenario, this module is enabled on the client PC (remote side), not on the MPD server (server side). The PulseAudio server on the client PC is ready to receive audio streams from the server side via this module. On the server side, MPD will forward the audio directly to the client PulseAudio server, since this module is not needed.
- module-zeroconf-publish: This module publishes the PulseAudio server on the network using Zeroconf (Bonjour) networking. In this scenario, no direct connection is made, so this module is also not needed on the server side.
- module-tunnel-sink: This module is used by one PulseAudio server to connect to another PulseAudio server. However, in this scenario, this module is not needed on the server side, since the MPD server will forward audio directly to the client PulseAudio server. If PulseAudio is configured on the client side,
module-tunnel-sink
is not needed.
In short, in this configuration, the MPD server connects directly to the client’s PulseAudio server and forwards the audio, so the commonly considered use of module-tunnel-sink
is not necessary. Instead, module-native-protocol-tcp
is enabled on the client side to accept connections from the server for audio streaming.
Compare the two scenarios.
1. Use of module-tunnel-sink
:
- Scenario: A PulseAudio client (virtual machine or server) connects to a remote PulseAudio server (e.g., desktop machine) to transmit audio.
- Advantages:
- Centralized management: multiple clients can connect to the remote server because the audio stream is managed from a single server location.
- Flexibility: Multiple clients can connect to the same remote PulseAudio server and send different audio streams.
2. MPD server connects directly to PulseAudio on client PCs:
- Scenario: MPD server sends audio directly to client’s PulseAudio server.
- Advantages:
- Simplicity: Relatively easy to set up for audio transfer across a network.
- Efficiency: Direct audio transfer without intermediate servers, potentially low latency.
In this case, since I am the only client, I will actually set up the latter to check the operation. Here is the image.
[Host machine (PulseAudio server)
IP: 192.168.0.139
|--- [Virtual machine (PulseAudio server)
|--- [Virtual machine (MPD server)
IP: 10.0.2.15
Port forwarding: 6600 -> 6600 (MPD Control)
[Client Machine (PulseAudio Client)
IP: IP address of the client
- Host machine: IP address of the host machine
- Running the PulseAudio server.
- Receives audio data from the virtual machine.
- Virtual Machine:.
- Runs MPD server and plays music.
- PulseAudio runs in the virtual machine and forwards the audio output from MPD to the PulseAudio server on the host machine by setting
export PULSE_SERVER="tcp: host's IP address"
. - Use port forwarding from the host machine to accept control to the MPD server at port 6600.
- Client Machine:.
- Receives audio data from the PulseAudio server on the host machine.
- Performs the actual audio output.
With this configuration, the audio is transferred from the MPD on the virtual machine to the client machine through the host machine’s PulseAudio server, and the host machine routes and relays the audio data in this process.
The use of the aplay command indicates that the sound card is not found by the normal user as shown below, but that the sound card is recognized when sudo
is used. This suggests that there may be a problem with the permissions to access the audio device. This problem also occurs in a virtual environment, but I just solved it.
mamushi@MB-T500:~$ aplay -l aplay: device_list:274: sound card not found… mamushi@MB-T500:~$ sudo aplay -l [sudo] mamushi’s password: **** list of hardware devices PLAYBACK **** card 0: PCH [HDA Intel PCH], device 0: ALC255 Analog [ALC255 Analog] subdevice: 1/1 subdevice #0: subdevice #0
Try the following steps to resolve this issue
- Check audio group membership:
- Try to check if the current user is a member of an audio group. This can be checked with the
groups
command. If the user is not included in an audio group, you may want to add the user to the group. After running the command, you will need to log out and log back in or reboot the system for the changes to take effect.sudo usermod -aG audio [username]
- Try to check if the current user is a member of an audio group. This can be checked with the
- Check PulseAudio permissions:
- Verify that PulseAudio is running correctly at the user level by running the
systemctl --user status pulseaudio
command to verify that the service is running correctly.
- Verify that PulseAudio is running correctly at the user level by running the
- Restart PulseAudio:
- Try restarting PulseAudio. To do this, use the following command
pulseaudio -k
pulseaudio --start
- Try restarting PulseAudio. To do this, use the following command
- Recheck the audio output:
- After reboot, run the
pactl list sinks
command again to verify that the physical audio device is recognized.
- After reboot, run the
- Loading of ALSA modules:
- Manually load ALSA modules into PulseAudio as needed.
pactl load-module module-alsa-card
- Manually load ALSA modules into PulseAudio as needed.
These steps will allow you to adjust user permissions and PulseAudio settings to ensure that the system correctly recognizes the physical audio device. If the problem persists, you should recheck the system’s audio driver settings and physical audio device connections. You should also consider the possibility of hardware problems or audio driver glitches.
In practice, the following command was entered within the virtual environment
sudo nano ~/.bashrc
Description
export PULSE_SERVER="tcp:IP address of the host"
Example
export PULSE_SERVER="tcp:192.168.0.139"
The meaning of the description is as follows
If you add export PULSE_SERVER="tcp:IP address of the host"
to the ~/.bashrc
file, the setting will be applied each time the Bash shell is invoked. The specific changes here are as follows
- Specify the PulseAudio server: The line
export PULSE_SERVER="tcp:IP address of the host"
specifies the server for the PulseAudio sound system. Here, “host IP address” is replaced by the IP address of the host where the PulseAudio server is running. - Sound streaming over network: This setting causes the local system (the system executing this command) to send sound output to a PulseAudio server on the specified network. This is useful, for example, when speakers are connected to another computer on the network. Now the virtual machine PulseAudio will send sound output to the machine running the virtual machine.
- Automation: By adding this line to
~/.bashrc
, this setting is automatically applied each time the shell is launched. This eliminates the need for the user to manually configure it each time.
By adding export PULSE_SERVER="tcp:host IP address"
to the ~/.bashrc
file, the guest system (virtual machine) will use the host system’s PulseAudio server as its sound source (audio output destination). This configuration allows the guest system to use the host system’s audio device directly, eliminating the need to configure an audio device on the guest system itself. So you can remove the description of the sound source in the Vagrantfile.
This method is especially useful when
- If the guest system does not directly support audio hardware.
- You want to do audio-related work on the guest system but want it to be output through the host system’s speakers or headphones.
- You want to keep the guest system lightweight and avoid installing audio drivers and related software.
Also, if you want to use the PulseAudio server over a network, you must open port 4713 in your server-side firewall configuration; PulseAudio by default uses TCP port 4713 to listen for connections from clients, so if this port is blocked by the firewall, connections from clients will be denied.
In addition, MPD needs to be configured for the virtual machine.
sudo nano /etc/mpd.conf
Description
audio_output {
type "pulse"
name "PulseAudio Output" server
server "Client IP address:4713"
}
If a Linux distribution such as Ubuntu uses ufw
(Uncomplicated Firewall), you can open port 4713 by running the following command
sudo ufw allow 4713/tcp
This command will allow access to port 4713 using the TCP protocol. It is the client machine that makes this setting, in this case. We also want to allow external connections to the PulseAudio server on the client machine, so the following steps are written in the configuration file.
sudo nano /etc/pulse/default.pa
Description
load-module module-native-protocol-tcp auth-ip-acl=192.168.0.0/24 auth-anonymous=1 port=4713
Meaning of the description.
load-module module-native-protocol-tcp
: Loads a module to connect to the PulseAudio server using the TCP protocol.auth-ip-acl=192.168.0.0/24
: Allow connections from any client in the subnet whose IP address is 192.168.0.0/24. This means connections from devices on the same local network as the host machine.auth-anonymous=1
: Allow connections without requiring authentication. This allows connections without a user name or password, but be aware that this may reduce security.port=4713
: Specify 4713 as the port on which the PulseAudio server listens. This is the default port for PulseAudio.
Restart PulseAudio.
pulseaudio -k
pulseaudio --start
If it does not work with the error as before, restart the computer. This this client is ubuntu desktop, so PulseAudio seemed to be already installed. Now when I connect and play on the client, the sound should not play on the server, but on the client. Install ncmpcpp on the client machine and verify that it works.
sudo apt install ncmpcpp
Connect to the host and playback.
ncmpcpp -h 192.168.0.139
The sound was successfully played on the connected client.
How do I use PulseAudio on Windows?
There are several ways to use PulseAudio on Windows, in addition to direct installation of the Windows version of PulseAudio
Windows Subsystem for Linux (WSL):
WSL allows you to run a Linux environment on Windows.
It is possible to install the Linux version of PulseAudio within WSL and use it to perform audio processing.
However, additional configuration may be required to access the Windows audio hardware via WSL.
Virtual Machines (VirtualBox, VMware, etc.):
Virtual machine software such as VirtualBox or VMware can be used to run a complete Linux system on Windows.
Within this Linux system, PulseAudio can be set up to stream audio to and from the host OS as needed.
Docker Container:.
Docker can also be used to create and run a Linux container with PulseAudio installed.
This way, audio processing is performed by PulseAudio running inside the container.
Additional configuration is required to exchange audio data between the container and the host system.
What if PulseAudio is not running properly as shown below?
vagrant@ubuntu2204:~$ journalctl –user -u pulseaudio -f Dec 17 04:37:20 ubuntu2204.localdomain systemd[1143]: Stopped Sound Service. Dec 17 04:37:20 ubuntu2204.localdomain systemd[1143]: Starting Sound Service… Dec 17 04:37:20 ubuntu2204.localdomain systemd[1143]: pulseaudio.service: Main process exited, code=exited, status=1/FAILURE Dec 17 04:37:20 ubuntu2204.localdomain systemd[1143]: pulseaudio.service: Failed with result ‘exit-code’. Dec 17 04:37:20 ubuntu2204.localdomain systemd[1143]: Failed to start Sound Service. Dec 17 04:37:20 ubuntu2204.localdomain systemd[1143]: pulseaudio.service: Scheduled restart job, restart counter is at 5. Dec 17 04:37:20 ubuntu2204.localdomain systemd[1143]: Stopped Sound Service. Dec 17 04:37:20 ubuntu2204.localdomain systemd[1143]: pulseaudio.service: Start request repeated too quickly. Dec 17 04:37:20 ubuntu2204.localdomain systemd[1143]: pulseaudio.service: Failed with result ‘exit-code’. Dec 17 04:37:20 ubuntu2204.localdomain systemd[1143]: Failed to start Sound Service.
The message pulseaudio.service: Main process exited, code=exited, status=1/FAILURE
indicates that the service is terminating with some error. Also, pulseaudio.service: Start request repeated too quickly. indicates that the service is trying to restart, but continuously failing.
What we have done to resolve this situation.
- Reset PulseAudio settings:
- Resetting the PulseAudio user configuration file may solve the problem. To do this, execute the following commands Or delete it.
mv ~/.config/pulse ~/.config/pulse_backup
- This will back up the old configuration file and restart PulseAudio with the new configuration.
- Resetting the PulseAudio user configuration file may solve the problem. To do this, execute the following commands Or delete it.
- Manual startup of PulseAudio:
- Use the
pulseaudio -k
command to exit the current PulseAudio session. - Then restart PulseAudio manually with the
pulseaudio --start
command.
- Use the
- Service status check:
- After restarting, use the
systemctl --user status pulseaudio
command to check the status of the PulseAudio service.
- After restarting, use the
- Check error messages:
- Run
journalctl --user -u pulseaudio -f
again to see if any new error messages appear.
- Run
- Check for dependencies:
- Check that all required dependencies for PulseAudio are installed. If any are missing, install them.
These steps may solve PulseAudio problems; PulseAudio is run at the user level, so the commands must be run without using sudo
.