https://wiki.archlinux.org/api.php?action=feedcontributions&user=Cuihao&feedformat=atom
ArchWiki - User contributions [en]
2024-03-28T17:51:37Z
User contributions
MediaWiki 1.41.0
https://wiki.archlinux.org/index.php?title=Android&diff=652695
Android
2021-02-17T18:42:08Z
<p>Cuihao: Using SDK manager and pacman in parallel is bad /* Making /opt/android-sdk group-writeable */</p>
<hr />
<div>[[Category:Android]]<br />
[[Category:Development]]<br />
[[es:Android]]<br />
[[fi:Android]]<br />
[[it:Android]]<br />
[[ja:Android]]<br />
[[zh-hans:Android]]<br />
{{Related articles start}}<br />
{{Related|Android tethering}}<br />
{{Related|Android Debug Bridge}}<br />
{{Related articles end}}<br />
== Transferring files ==<br />
<br />
There are various ways to transfer files between a computer and an Android device:<br />
<br />
* USB cable<br />
** [[Media Transfer Protocol]] for modern Android devices<br />
** [[Wikipedia:USB Mass Storage|USB mass storage]] for older devices<br />
** [[Android Debug Bridge]]<br />
* special USB sticks / regular USB stick with adapter<br />
* [[Bluetooth]]<br />
* Arch Linux software with Android counterparts<br />
** client or server for protocols that can be used to transfer files (eg. [[SSH]], [[FTP]], [[Samba]] or HTTP)<br />
** [[KDE Connect]] ({{Pkg|kdeconnect}}) – integrates your Android device with the KDE desktop (featuring synced notifications & clipboard, multimedia control, and file/URL sharing).<br />
** [[cloud synchronization clients]]<br />
** [[Syncthing]]<br />
** {{AUR|sendanywhere}} – cross-platform file sharing<br />
<br />
== App development ==<br />
<br />
The officially supported way to build Android apps is to use [[#Android Studio]].[https://developer.android.com/training/basics/firstapp/creating-project]<br />
<br />
=== Android Studio ===<br />
<br />
[https://developer.android.com/studio/index.html Android Studio] is the official Android development environment based on [[Wikipedia:IntelliJ IDEA|IntelliJ IDEA]]. It provides integrated Android developer tools for development and debugging.<br />
<br />
You can [[install]] it with the {{AUR|android-studio}} package.<br />
<br />
{{Note|1=<nowiki></nowiki><br />
* Make sure you properly [[Java#Change_default_Java_environment|set the Java environment]] otherwise android-studio will not start.<br />
* If Android Studio shows up as a blank window try [[export]]ing {{ic|1=_JAVA_AWT_WM_NONREPARENTING=1}}, see [https://code.google.com/p/android/issues/detail?id=57675 issue #57675].<br />
}}<br />
<br />
The Android Studio Setup Wizard installs the required [[#SDK packages]] and places the SDK by default in {{ic|~/Android/Sdk}}.<br />
<br />
To build apps from the command-line (using e.g. {{ic|./gradlew assembleDebug}}) set the [https://developer.android.com/studio/command-line/variables#android_sdk_root ANDROID_SDK_ROOT] [[environment variable]] to your SDK location.<br />
<br />
=== SDK packages ===<br />
<br />
Android SDK packages can be installed directly from upstream using [[#Android Studio]]'s [https://developer.android.com/studio/intro/update#sdk-manager SDK Manager] or the [https://developer.android.com/studio/command-line/sdkmanager sdkmanager] command line tool (part of the Android SDK Tools). Some Android SDK packages are also available as [[AUR]] packages, they generally install to {{ic|/opt/android-sdk/}}.<br />
<br />
The [https://developer.android.com/studio/intro/update#recommended required SDK packages] are:<br />
<br />
{| class="wikitable"<br />
! Android SDK Package !! SDK-style path !! AUR package !! AUR dummy !! CLI tools<br />
|-<br />
| [https://developer.android.com/studio/releases/sdk-tools SDK Tools] || tools || {{AUR|android-sdk}} || {{AUR|android-sdk-dummy}} || sdkmanager, apkanalizer, avdmanager, mksdcard, proguard<br />
|-<br />
| [https://developer.android.com/studio/releases/build-tools SDK Build-Tools] || build-tools;''version'' || {{AUR|android-sdk-build-tools}} || {{AUR|android-sdk-build-tools-dummy}} || apksigner, zipalign<br />
|-<br />
| [https://developer.android.com/studio/releases/platform-tools SDK Platform-Tools] || platform-tools || {{AUR|android-sdk-platform-tools}} || {{AUR|android-sdk-platform-tools-dummy}} || [[adb]], [[#Fastboot|#fastboot]] and systrace<br />
|-<br />
| [https://developer.android.com/studio/releases/platforms SDK Platform] || platforms;android-''level'' || {{AUR|android-platform}}, [https://aur.archlinux.org/packages/?K=android-platform- older versions] || unnecessary<br />
|}<br />
<br />
The {{Pkg|android-tools}} package provides [[adb]], [[#Fastboot|#fastboot]], {{ic|e2fsdroid}} and {{ic|mke2fs.android}} from the SDK Platform-Tools along with {{ic|mkbootimg}} and {{ic|ext2simg}}.<br />
<br />
{{Note|1=&nbsp;<br />
* Since the Android SDK contains 32-bit binaries, you must enable the [[multilib]] repository. Otherwise you will get {{ic|error: target not found: lib32-*}} error messages.<br />
* If you choose to directly install SDK packages from upstream, install the AUR packages of the ''AUR dummy'' column to pull in the required dependencies.<br />
* If you get a {{ic|java.lang.NoClassDefFoundError}} exception when attempting to run {{ic|sdkmanager}}, use OpenJDK 8's JRE temporarily, by installing the package {{Pkg|jre8-openjdk}} and [[Java#Switching_between_JVM|switching the Java environment]].}}<br />
<br />
==== Android Emulator ====<br />
<br />
The [https://developer.android.com/studio/run/emulator Android Emulator] is available as the {{ic|emulator}} SDK package, the {{AUR|android-emulator}} package, and there is also a dummy package for it: {{AUR|android-emulator-dummy}}.<br />
<br />
To run the Android Emulator you need an Intel or ARM System Image. You can install them through the AUR[https://aur.archlinux.org/packages/?K=android-+system+image], with the sdkmanager or using Android Studio's [https://developer.android.com/studio/run/managing-avds AVD Manager].<br />
<br />
==== Other SDK packages in the AUR ====<br />
<br />
The [https://developer.android.com/topic/libraries/support-library/ Android Support Library] is now available online from [https://developer.android.com/studio/build/dependencies#google-maven Google's Maven repository].<br />
You can also install it offline through the {{ic|extras;android;m2repository}} SDK package (also available as {{AUR|android-support-repository}}).<br />
<br />
==== Making /opt/android-sdk group-writeable ====<br />
<br />
{{Accuracy|Don't do this. Using another package manager (Android SDK manager) to manage pacman-installed files is a bad idea. This renders package manager useless.}}<br />
<br />
The AUR packages install the SDK in {{ic|/opt/android-sdk/}}. This directory has root [[permissions]], so keep in mind to run sdk manager as root. If you intend to use it as a regular user, create the android-sdk users group, add your user.<br />
<br />
# groupadd android-sdk<br />
# gpasswd -a <user> android-sdk<br />
<br />
Set an access control list to let members of the newly created group write into the android-sdk folder. As running sdkmanager can also create new files, set the ACL as default ACL. the X in the default group entry means "allow execution if executable by the owner (or anyone else)"<br />
<br />
# setfacl -R -m g:android-sdk:rwx /opt/android-sdk<br />
# setfacl -d -m g:android-sdk:rwX /opt/android-sdk <br />
<br />
Re-login or as <user> log your terminal in to the newly created group:<br />
<br />
$ newgrp android-sdk<br />
<br />
=== Other IDEs ===<br />
<br />
Android Studio is the official Android development environment based on IntelliJ IDEA. Alternatively, you can use [[Netbeans]] with the NBAndroid-V2. All are described below.<br />
<br />
==== Netbeans ====<br />
<br />
If you prefer using [[Netbeans]] as your IDE and want to develop Android applications, use [https://github.com/NBANDROIDTEAM/NBANDROID-V2 NBAndroid-V2] .<br />
<br />
Install {{AUR|android-sdk}} package and follow the instructions from the [https://github.com/NBANDROIDTEAM/NBANDROID-V2/blob/master/README.md NBANDROID README].<br />
<br />
==== Vim / Neovim ====<br />
It's possible to write {{ ic | flutter }} Applications for Android and iOS with (neo)vim under IDE - like experience. Install a vim plugin Manager und install [https://github.com/neoclide/coc.nvim coc],<br />
after that install the [https://github.com/iamcco/coc-flutter coc-flutter] extensions which provides the autocompletion as in Android Studio and to load the code into an Android emulator.<br />
<br />
=== Other Tools ===<br />
<br />
==== Marvin ====<br />
<br />
Marvin is a tool which helps beginners set up an Android development environment. Installing {{AUR|marvin_dsc}} helps you set up the following things: JDK, Android SDK, IDE(s), and AVD.<br />
<br />
== Building ==<br />
<br />
Please note that these instructions are based on the [https://source.android.com/source/building official AOSP build instructions]. Other Android-derived systems such as LineageOS will often require extra steps.<br />
<br />
=== Required packages ===<br />
<br />
{{Note|Before doing the following steps, the [[multilib]] repository must be enabled in {{ic|/etc/pacman.conf}}.}}<br />
<br />
As of 2020/April, to build either AOSP 10 or LineageOS 17.1 you need (possibly a subset of):<br />
<br />
* {{Grp|base-devel}} {{Grp|multilib-devel}} {{Pkg|gcc}} {{Pkg|repo}} {{Pkg|git}} {{Pkg|gnupg}} {{Pkg|gperf}} {{Pkg|sdl}} {{Pkg|wxgtk2}} {{Pkg|squashfs-tools}} {{Pkg|curl}} {{Pkg|ncurses}} {{Pkg|zlib}} {{Pkg|schedtool}} {{Pkg|perl-switch}} {{Pkg|zip}} {{Pkg|unzip}} {{Pkg|libxslt}} {{Pkg|bc}} {{Pkg|rsync}} {{Pkg|ccache}} {{Pkg|lib32-zlib}} {{Pkg|lib32-ncurses}} {{Pkg|lib32-readline}} {{Aur|ncurses5-compat-libs}} {{Aur|lib32-ncurses5-compat-libs}}<br />
<br />
and a TTF font installed, e.g. {{Pkg|ttf-dejavu}}.<br />
In particular, no Python2 or Java are required, as they are provided by AOSP/Lineage.<br />
What follows below is only relevant to obsolete Android versions.<br />
<br />
To build any version of Android, you need to install these packages:<br />
<br />
* {{Pkg|lib32-gcc-libs}} {{Pkg|git}} {{Pkg|gnupg}} {{Pkg|flex}} {{Pkg|bison}} {{Pkg|gperf}} {{Pkg|sdl}} {{Pkg|wxgtk2}} {{Pkg|squashfs-tools}} {{Pkg|curl}} {{Pkg|ncurses}} {{Pkg|zlib}} {{Pkg|schedtool}} {{Pkg|perl-switch}} {{Pkg|zip}} {{Pkg|unzip}} {{Pkg|libxslt}} {{Pkg|python2-virtualenv}} {{Pkg|bc}} {{Pkg|rsync}} {{Aur|ncurses5-compat-libs}} {{Pkg|lib32-zlib}} {{Pkg|lib32-ncurses}} {{Pkg|lib32-readline}} {{Aur|lib32-ncurses5-compat-libs}}<br />
<br />
The {{Aur|aosp-devel}} metapackage provides them all for simple installation.<br />
<br />
Additionally, LineageOS requires the following packages: {{AUR|xml2}}, {{Pkg|lzop}}, {{Pkg|pngcrush}}, {{Pkg|imagemagick}}<br />
<br />
They can be installed with the {{Aur|lineageos-devel}} metapackage.<br />
<br />
{{Accuracy|The note below is not clear and probably incomplete.}}<br />
{{Note|1=Installing both {{Pkg|maven}} and {{Pkg|gradle}} to build LineageOS may result in a build speed improvement as the build process will prefer the system's }}<br />
<br />
=== Java Development Kit ===<br />
<br />
The [https://source.android.com/source/requirements required JDK version] depends on the Android version you are building:<br />
<br />
* For Android 9 (Pie) and up, Java is included with the Android source and no separate installation is needed.<br />
* For Android 7 and 8 (Nougat and Oreo), OpenJDK 8 is required, which is available with the {{Pkg|jdk8-openjdk}} package. <br />
* For Android 5 and 6 (Lollipop and Marshmallow), OpenJDK 7 is required, which is available with the {{Pkg|jdk7-openjdk}} package.<br />
<br />
{{Note|1=<br />
For older Android versions, where Java is not included, Java is expected to be in {{ic|/usr/lib/jvm/java-''version''-openjdk-amd64}}.<br />
Set JAVA_HOME to avoid this requirement and match the Arch Linux installation path.<br />
Example:<br />
<br />
$ export JAVA_HOME=/usr/lib/jvm/java-''version''-openjdk<br />
<br />
This change will be valid only for the current terminal session.<br />
}}<br />
<br />
=== Setting up the build environment ===<br />
<br />
[[Install]] the {{Pkg|repo}} package. <br />
<br />
Create a directory to build.<br />
<br />
$ mkdir ~/android<br />
$ cd ~/android<br />
<br />
The Android build process expects {{ic|python}} to be python2. Prepend it to the {{ic|PATH}}:<br />
<br />
$ mkdir bin<br />
$ ln -s /bin/python2 bin/python<br />
$ export PATH=$PWD/bin:$PATH<br />
<br />
Alternatively, create a python2 virtual environment and activate it:<br />
<br />
$ virtualenv2 --system-site-packages venv<br />
$ source venv/bin/activate<br />
<br />
{{Note|<br />
* This activation is only active for the current terminal session. The virtual env will be kept in the {{ic|venv}} folder.<br />
* Passing "--system-site-packages" to virtualenv2 points your virtual environment to your installed python2.7 modules. This should give you all python modules you need for the build, assuming you have installed the required dependencies such as python2-mako.<br />
* If during the build you still receive errors pertaining to missing python modules a quick and dirty fix might be to symlink /usr/lib/python2.7/* to ~/android/venv/lib/python2.7/ (Change ~/android to reflect your build directory if different than above).<br />
Example:<br />
<br />
$ ln -s /usr/lib/python2.7/* ~/android/venv/lib/python2.7/<br />
<br />
or (assuming build directory Data/Android_Build):<br />
<br />
$ ln -s /usr/lib/python2.7/* /Data/Android_Build/venv/lib/python2.7/<br />
<br />
}}<br />
<br />
=== Downloading the source code ===<br />
<br />
This will clone the repositories. You '''only''' need to do this the first time you build Android, or if you want to switch branches.<br />
<br />
* The {{ic|repo}} has a {{ic|-j}} switch that operates similarly to the one used with {{ic|make}}. Since it controls the number of simultaneous downloads, you should adjust the value depending on downstream network bandwidth.<br />
<br />
* You will need to specify a '''branch''' ([https://android.googlesource.com/platform/manifest/+refs list of branches]) to check out with the {{ic|-b}} switch. If you leave the switch out, you will get the so-called '''master branch'''.<br />
<br />
{{bc|<br />
$ repo init -u https://android.googlesource.com/platform/manifest -b master<br />
$ repo sync -j4<br />
}}<br />
<br />
{{Note|To further decrease sync times, you can utilize the -c switch with the repo command as such:<br />
<br />
$ repo sync -j8 -c<br />
<br />
The {{ic|-c}} switch will only sync the branch which is specified in the manifest, which in turn is determined by the branch specified with the {{ic|-b}} switch, or the default branch set by the repository maintainer.<br />
}}<br />
<br />
Wait a long time. Just the uncompiled source code, along with the {{ic|.repo}} and {{ic|.git}} directories that are used to keep track of it, are very large. As of Android 10, at least 250 GB of free disk space is [https://source.android.com/setup/build/requirements required].<br />
<br />
{{Note|If you want to update your local copy of the Android source, at a later time, simply enter the build directory, load the Virtualenv, and re-sync:<br />
<br />
$ repo sync<br />
<br />
}}<br />
<br />
=== Building the code ===<br />
<br />
This should do what you need for AOSP:<br />
<br />
$ source build/envsetup.sh<br />
$ lunch full-eng<br />
$ make -j4<br />
<br />
If you run '''lunch''' without arguments, it will ask what build you want to create. Use -j with a number between one and two times number of cores/threads.<br />
<br />
The build takes a very long time.<br />
<br />
{{Note|<br />
* Make sure you have enough RAM. Android will use the {{ic|/tmp}} directory heavily. By default the size of the partition the {{ic|/tmp}} folder is mounted on is half the size of your RAM. If it fills up, the build will fail. 4GB of RAM or more is recommended. Alternatively, you can get rid of the tmpfs from [[fstab]] all together.<br />
* From the [https://source.android.com/source/building#build-the-code Android Building and Running guide]:<br />
"GNU make can handle parallel tasks with a {{ic|-jN}} argument, and it's common to use a number of tasks N that's between 1 and 2 times the number of hardware threads on the computer being used for the build. E.g. on a dual-E5520 machine (2 CPUs, 4 cores per CPU, 2 threads per core), the fastest builds are made with commands between {{ic|make -j16}} and {{ic|make -j32}}."<br />
}}<br />
<br />
=== Testing the build ===<br />
<br />
When finished, run/test the final image(s).<br />
<br />
$ emulator<br />
<br />
=== Creating a flashable Image ===<br />
<br />
To create an image that can be flashed it is necessary to:<br />
<br />
make -j8 updatepackage<br />
<br />
This will create a zip image under {{ic|'''out/target/product/hammerhead'''}} (hammerhead being the device name) that can be flashed.<br />
<br />
== Flashing ==<br />
<br />
In some cases, you want to return to the stock Android after flashing custom ROMs to your Android mobile device. For flashing instructions of your device, please use [http://forum.xda-developers.com/ XDA forums].<br />
<br />
=== Fastboot ===<br />
<br />
Fastboot (as well as [[ADB]]) is included in the {{Pkg|android-tools}} package.<br />
<br />
{{Note|Restoring firmwares using {{ic|fastboot}} can be quite tricky, but you might want to browse [http://www.xda-developers.com/ XDA developers forums] for a stock firmware, which is mostly a {{ic|*.zip}} file, but inside of it, comes with the firmware files and {{ic|flash-all.sh}} script. For example, [https://developers.google.com/android/nexus/images Google Nexus] firmwares include {{ic|flash-all.sh}} script or another example could be for OnePlus One - [http://forum.xda-developers.com/oneplus-one/general/guide-return-opo-to-100-stock-t2826541 XDA thread], where you can find firmwares with included {{ic|flash-all.sh}} script.}}<br />
<br />
{{Note|If you get a {{ic|no permissions}} error or execution just hangs with {{ic|< waiting for any device >}} then you need to run {{ic|fastboot}} as the root user. Alternatively you can install {{Pkg|android-udev}} or the AUR package {{aur|android-udev-git}} and reconnect your device.}}<br />
<br />
=== Samsung devices ===<br />
<br />
Samsung devices cannot be flashed using '''Fastboot''' tool. Alternatives are '''Heimdall''' and '''Odin''' (by using Windows and VirtualBox).<br />
<br />
==== samloader ====<br />
<br />
To download original Samsung firmware, a platform independent script, [https://github.com/nlscc/samloader samloader] can be used.<br />
<br />
==== Heimdall ====<br />
<br />
[http://glassechidna.com.au/heimdall/ Heimdall] is a cross-platform open-source tool suite used to flash firmware (also known as ROMs) onto Samsung mobile devices and is also known as an alternative to [http://odindownload.com/ Odin]. It can be installed as {{Pkg|heimdall}}.<br />
<br />
The flashing instructions can be found on Heimdall's [https://gitlab.com/BenjaminDobell/Heimdall/tree/master/Linux GitLab repository] or on [http://forum.xda-developers.com/showthread.php?t=1922461 XDA forums].<br />
<br />
==== Odin (Virtualbox) ====<br />
<br />
{{Note|1=This section only covers preparation and '''not''' flashing instructions. Search [http://www.xda-developers.com XDA developers forums] to find flashing instructions for a specific device. For example, [https://forum.xda-developers.com/showthread.php?t=2265477 Samsung Galaxy S4].}}<br />
<br />
It is also possible to restore [http://www.sammobile.com/firmwares/ firmware (Android)] on the Samsung devices using [http://odindownload.com/ Odin], but inside the [[VirtualBox]].<br />
<br />
Arch Linux (host) preparation:<br />
<br />
# Install [[VirtualBox]] together with its [[VirtualBox#Extension_pack|extension pack]] and [[VirtualBox#Guest_additions_disc|guest additions]].<br />
# Install your preferred, but compatible with Odin, Windows operating system (with VirtualBox guest additions) into a virtual hard drive using VirtualBox.<br />
# Open VirtualBox settings of your Windows operating system, navigate to ''USB'', then tick (or make sure it is ticked) '''Enable USB 2.0 (EHCI) Controller'''.<br />
# At VirtualBox running Windows operating system, click in the menu bar ''Devices > USB Devices'', then click on your Samsung mobile device from the list, which is connected to your computer via USB.<br />
<br />
Windows (guest) preparation:<br />
<br />
# Install [http://androidxda.com/download-samsung-usb-drivers Samsung drivers].<br />
# Install [http://odindownload.com/ Odin].<br />
# Download required [http://www.sammobile.com/firmwares/ Samsung firmware (Android)] for your smartphone model.<br />
<br />
Check if configuration is working:<br />
<br />
# Turn your device into Download mode and connect to your Linux machine.<br />
# In virtual machine toolbar, select ''Devices > USB > ...Samsung...'' device.<br />
# Open Odin. The white box (a big one at the bottom-left side) named '''Message''', should print a line similar to this:<br />
<br />
<ID:0/003> Added!!<br />
<br />
which means that your device is visible to Odin & Windows operating system and is ready to be flashed.<br />
<br />
== Use Android on GNU/Linux ==<br />
<br />
There are several projects and methods which support running Android on GNU/Linux:<br />
<br />
* [[Anbox]]: container-based software to run Android on Linux kernels<br />
* [https://www.android-x86.org/ Android-x86]: a direct port of Android for the x86 architecture<br />
<br />
== Troubleshooting ==<br />
<br />
=== Android Studio: Android Virtual Devices show 'failed to load'. ===<br />
<br />
Make sure you have exported the variable {{ic|ANDROID_HOME}} as explained in [[#Android Studio]].<br />
<br />
=== Android Studio: 'failed to create the SD card' ===<br />
<br />
If you try to run an AVD (Android Virtual Device) under x86_64 Arch and get the error above, install the {{Pkg|lib32-gcc-libs}} package from the [[multilib]] repository.<br />
<br />
=== Eclipse: During Debugging "Source not found" ===<br />
<br />
Most probably the debugger wants to step into the Java code. As the source code of Android does not come with the Android SDK, this leads to an error. The best solution is to use step filters to not jump into the Java source code. Step filters are not activated by default. To activate them: ''Window > Preferences > Java > Debug > Step Filtering''.<br />
Consider to select them all. If appropriate you can add the android.* package. See the forum post for more information: http://www.eclipsezone.com/eclipse/forums/t83338.rhtml<br />
<br />
=== ValueError: unsupported pickle protocol ===<br />
<br />
One fix is to issue:<br />
<br />
$ rm ~/.repopickle_.gitconfig<br />
<br />
If that does not work, then try this:<br />
<br />
$ find /path/to/android-root -name .repopickle_config -delete<br />
<br />
=== libGL error: failed to load driver: swrast OR AVD does not load and no error message displayed ===<br />
<br />
Sometimes, beginning to load an AVD will cause an error message similar to this to be displayed, or the loading process will appear to finish but no AVD will load and no error message will be displayed.<br />
<br />
The AVD loads an incorrect version of libstdc++, you can remove the folder libstdc++ from {{ic|~/.android-sdk/emulator/lib64}} (for 64-bit) or {{ic|~/.android-sdk/emulator/lib}} (for 32-bit) , e.g.:<br />
<br />
$ rm -r ~/.android-sdk/emulator/lib64/libstdc++<br />
<br />
Note that in versions before Android Studio 3.0, this directory was in a different location:<br />
<br />
$ rm -r ~/Android/Sdk/emulator/lib64/libstdc++<br />
<br />
Alternatively you can set and export ANDROID_EMULATOR_USE_SYSTEM_LIBS in ~/.profile as:<br />
<br />
export ANDROID_EMULATOR_USE_SYSTEM_LIBS=1<br />
<br />
Reference: [https://developer.android.com/studio/command-line/variables.html#studio_jdk Android Studio user guide]<br />
<br />
Fix for the .desktop file might be achieved by using env command, prefixing the Exec line [[Desktop entries#Modify environment variables]]<br />
<br />
env ANDROID_EMULATOR_USE_SYSTEM_LIBS=1<br />
<br />
=== sh: glxinfo: command not found===<br />
<br />
Here is the full error:<br />
<br />
Cannot launch AVD in emulator.<br />
Output:<br />
sh: glxinfo: command not found<br />
sh: glxinfo: command not found<br />
libGL error: unable to load driver: swrast_dri.so<br />
libGL error: failed to load driver: swrast<br />
X Error of failed request: BadValue (integer parameter out of range for operation)<br />
Major opcode of failed request: 154 (GLX)<br />
Minor opcode of failed request: 24 (X_GLXCreateNewContext)<br />
Value in failed request: 0x0<br />
Serial number of failed request: 32<br />
Current serial number in output stream: 33<br />
QObject::~QObject: Timers cannot be stopped from another thread<br />
<br />
You can try to install glxinfo ({{Pkg|mesa-demos}}) but if your computer has enough power you could simply use software to render graphics. To do so, go to ''Tools > Android > AVD Manager'', edit the ''AVD'' (click the pencil icon), then select ''Software - GLES 2.0'' for ''Emulated Performance > Graphics''.<br />
<br />
=== Android Emulator: no keyboard input in xfwm4 ===<br />
<br />
In xfwm4, the vertical toolbar buttons window that is on the right of the emulator takes focus from the emulator and consumes keyboard events.<br />
([https://issuetracker.google.com/issues/37094173 bug report])<br />
<br />
You can use the workaround described in [https://stackoverflow.com/a/42720450 this Stack Overflow answer]:<br />
<br />
# Open the xfwm4 settings.<br />
# Switch to the Focus tab.<br />
# Change the Focus Model to "Focus follow mouse".<br />
# Disable ''Automatically raise windows when they receive focus'' option below.<br />
<br />
=== Android Emulator: Segmentation fault (core dumped) ===<br />
<br />
When using [[Nouveau]] drivers try to disable gpu hardware acceleration.<br />
<br />
In some devices it can only be done by editing {{ic|$HOME/.avd/<device_name>.avd/config.ini}}. ([https://stackoverflow.com/a/58376934 details])<br />
<br />
# Set {{ic|1=hw.gpu.enabled=no}}<br />
# Set {{ic|1=hw.gpu.mode=off}}<br />
<br />
=== adb: sideload connection failed: insufficient permissions for device ===<br />
<br />
If you get the error:<br />
<br />
adb: sideload connection failed: insufficient permissions for device<br />
See [http://developer.android.com/tools/device.html] for more information<br />
adb: trying pre-KitKat sideload method...<br />
adb: pre-KitKat sideload connection failed: insufficient permissions for device<br />
See [http://developer.android.com/tools/device.html] for more information<br />
<br />
you might solve it by restarting the adb server:<br />
<br />
$ adb kill-server<br />
# adb start-server</div>
Cuihao
https://wiki.archlinux.org/index.php?title=BOINC&diff=293461
BOINC
2014-01-18T13:20:13Z
<p>Cuihao: add Chinese (Simp.) translation</p>
<hr />
<div>[[Category:Mathematics and science]]<br />
[[de:BOINC]]<br />
[[fr:BOINC]]<br />
[[it:BOINC]]<br />
[[ja:BOINC]]<br />
[[sk:BOINC]]<br />
[[zh-cn:BOINC]]<br />
[http://boinc.berkeley.edu/ BOINC website]: ''"Use the idle time on your computer (Windows, Mac, or Linux) to cure diseases, study global warming, discover pulsars, and do many other types of scientific research. It's safe, secure, and easy."''<br />
<br />
[[Wikipedia:BOINC|Wikipedia]]: ''"The Berkeley Open Infrastructure for Network Computing (BOINC) is a non-commercial middleware system for volunteer and grid computing. It was originally developed to support the SETI@home project before it became useful as a platform for other distributed applications in areas as diverse as mathematics, medicine, molecular biology, climatology, and astrophysics. The intent of BOINC is to make it possible for researchers to tap into the enormous processing power of personal computers around the world."''<br />
<br />
== Installation ==<br />
[[Pacman|Install]] either {{Pkg|boinc}} or {{Pkg|boinc-nox}} from the [[Official Repositories]]. The latter package omits [[Xorg]] dependencies, and is therefore suited for use on headless servers.<br />
<br />
Both packages install a unit file named {{ic|boinc.service}}. Therefore, the daemon can be managed using {{ic|systemctl}}. Read [[Systemd#Using_units]] for details.<br />
<br />
== Using BOINC ==<br />
=== BOINC via the GUI ===<br />
By default, a password is created in {{ic|/var/lib/boinc/gui_rpc_auth.cfg}} for connecting to the daemon. To simplify connection of the GUI to the daemon, cd to your home directory, create a link to the file, and change permissions to allow read access to boinc group members.<br />
<br />
$ cd ~/<br />
$ ln -s /var/lib/boinc/gui_rpc_auth.cfg gui_rpc_auth.cfg<br />
# chmod 640 gui_rpc_auth.cfg<br />
<br />
If you prefer a different password, or none at all, you can edit {{ic|/var/lib/boinc/gui_rpc_auth.cfg}}. Then restart BOINC daemon.<br />
<br />
If you do not like the idea of having this file in your home directory, there is an alternative approach. BOINC Manager will also look for a readable gui_rpc_auth.cfg file in the current working directory. If you make the file readable by the '''boinc''' group and ensure that the manager is run with {{ic|/var/lib/boinc}} as the working directory, you should find that the client connects to the daemon automatically, as desired. This can usually be achieved via the menu editor in your desktop environment of choice.<br />
<br />
To start the GUI, use the ''boincmgr'' command<br />
$ boincmgr<br />
<br />
BOINC should now take you through the process of attaching to a project. NB, some projects will let you create an account remotely via the GUI while some may require you to first create an account via their website. You can attach to multiple projects if you have the resources (disk space, time, CPU power). Do this via menu option ''Tools / Attach to project''.<br />
<br />
If BOINC did not ask you to connect to a project, then make sure you are connected to the daemon. Go to menu option ''Advanced / Select computer'', choose your machine's name and enter the password. (To avoid this, make sure the above steps regarding gui_rpc_auth.cfg have been done.)<br />
<br />
==== Projects using GPU ====<br />
If you want to use your GPU, you need the proprietary nvidia or amd drivers. For ATI/AMD Cards you also need [https://aur.archlinux.org/packages.php?ID=21933 Catalyst driver for stock kernel] which you can get from AUR. For Nvidia, you also need the package {{Pkg|opencl-nvidia}} located in extra.<br />
<br />
In addition, the boinc user should be in the video group:<br />
# gpasswd -a boinc video<br />
The boinc user also needs to have access to your X session. Use this command to accomplish this:<br />
xhost local:boinc<br />
You may want to add that to a startup script.<br />
<br />
=== BOINC via the CLI ===<br />
<br />
For {{pkg|boinc-nox}}, {{ic|boinccmd}} and {{ic|boinc}} have help information that can be referenced. {{ic|boinccmd}} is BOINC's recommended command line interface.<br />
<br />
== Log files ==<br />
NB, BOINC places log files in {{ic|/var/lib/boinc/}}<br />
/var/lib/boinc/stderrdae.txt<br />
/var/lib/boinc/stdoutdae.txt<br />
<br />
== Considerations when choosing a project ==<br />
Projects have different minimum hardware requirements (CPU, disk space), and different times to taken to run each work unit. If you do not finish a work unit before the deadline it will sent out to someone else, but it is better to look around to see what projects suit your machine and your uptime patterns to avoid this happening. <br />
<br />
Also, if it is important to you, check if the project makes the data and results publicly available.<br />
<br />
=== Running on Arch64 ===<br />
Some projects provide only 32bit applications which may require 32bit libraries to run work units or show graphics. You will find most of these libraries in the [multilib] repository.<br />
<br />
;To run WUs (e.g. Climateprediction) <br />
:{{Pkg|lib32-glibc}}, {{Pkg|lib32-glib2}}<br />
;To show graphics (e.g. Several projects of WCG, Climateprediction)<br />
:{{Pkg|lib32-pango}}, {{Pkg|lib32-libxdamage}}, {{Pkg|lib32-libxi}}, {{Pkg|lib32-libgl}}, {{AUR|lib32-libjpeg6}} (AUR), and {{Pkg|lib32-libxmu}}<br />
<br />
==Troubleshooting==<br />
=== GPU missing ===<br />
If you get this error :<br />
{{bc|GPU Missing}}<br />
and the Work Unit does not start, you should [[Daemons|restart]] the {{ic|boinc.service}} daemon.<br />
<br />
This will happen if the BOINC daemon starts before the an X session is fully initialized.<br />
<br />
===Laptop overheating and battery duration reduction===<br />
If you run BIONC on a laptop with the ondemand governor (the default), it will keep the CPUs at their maximum frequencies (over)heating them and decreasing battery duration. The best way to fix this is to tell the ondemand to not rise the CPU frequencies for BIONC:<br />
{{bc|# echo 1 >/sys/devices/system/cpu/cpufreq/ondemand/ignore_nice_load}}<br />
<br />
To do this on boot, create the following tmpfiles.d config:<br />
{{hc|/etc/tmpfiles.d/ondemand-ignore-nice.conf|<br />
w /sys/devices/system/cpu/cpufreq/ondemand/ignore_nice_load - - - - 1}}<br />
<br />
=== Unable to download with World Community Grid ===<br />
If you're unable to download new work units for the World Community Grid project, the problem comes from openssl, so we need to rebuild a package with a small fix in it to allow downloads. <br />
<br />
First, we're going to get the sources for openssl using git:<br />
$ git clone git://projects.archlinux.org/svntogit/packages.git --single-branch --branch packages/openssl<br />
<br />
This will give you four files in ./packages/trunk/, including a PKGBUILD. The PKGBUILD is not the right one, so we need to get the correct one which you can get [http://pastebin.com/pYcYf4dr here]. <br />
<br />
Now we just have to create the package:<br />
$ makepkg <br />
<br />
This sould give you a file ending in *.pkg.tar.xz, all that's left is to install it, and restart the boinc service:<br />
<br />
# pacman -U openssl-1.0.1.c-1-x86_64.pkg.tar.xz<br />
# systemctl restart boinc<br />
<br />
Your new work units should now be able to download. If you have any trouble, the original thread on the forum can be seen [https://bbs.archlinux.org/viewtopic.php?pid=1160393#p1160393 here].<br />
<br />
== See also ==<br />
* [http://boinc.berkeley.edu/ BOINC homepage]<br />
* [http://boinc.berkeley.edu/projects.php List of BOINC projects]<br />
* [[Wikipedia:BOINC|Wikipedia entry]]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=BOINC_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=293459
BOINC (简体中文)
2014-01-18T13:18:09Z
<p>Cuihao: /* 密码文件配置 */</p>
<hr />
<div>[[Category:Mathematics and science (简体中文)]]<br />
[[en:BOINC]]<br />
[[de:BOINC]]<br />
[[fr:BOINC]]<br />
[[it:BOINC]]<br />
[[ja:BOINC]]<br />
[[sk:BOINC]]<br />
[http://boinc.berkeley.edu/ BOINC 官方网站]:“利用您的计算机(Windows、Mac 以及 Linux)的空闲的时间,帮助寻找治疗疾病的方法、研究全球变暖、发现脉冲星…… 进行各种各样的科学研究。这一切简单且安全!”<br />
<br />
[[Wikipedia:BOINC|维基百科]]:“伯克利开放式网络计算平台(Berkeley Open Infrastructure for Network Computing,BOINC)是一个用于志愿计算和网格计算的非商业的中间件系统。开发 BOINC 的最初目的是支援 SETI@home 项目,而后来它发展成为一个支持多种项目的强大的分布式计算平台,这些项目的研究领域涵盖了数学、医学、分子生物学、气候学以及天体物理学等等。BOINC 力图让研究人员享用到全世界数以亿计的个人电脑的强大计算能力。”<br />
<br />
== 安装 ==<br />
[[Official Repositories (简体中文)|官方仓库]] 提供了 {{Pkg|boinc}} 和 {{Pkg|boinc-nox}} 软件包,使用 [[pacman|安装]] 其一即可。后者不依赖 [[Xorg (简体中文)|Xorg]],适用于没有物理控制台的服务器。<br />
<br />
两个软件包都提供了系统服务 {{ic|boinc.service}},可以使用 {{ic|systemctl}} 管理。详情参见 [[Systemd (简体中文)#使用单元]]。<br />
<br />
== 使用 ==<br />
{{注意|以下内容仅仅适用于:1、Arch 官方软件包提供的 BOINC;2、从 systemd 服务正确启动。如要通过 boinc 命令启动管理,请自行查阅相关文档。}}<br />
<br />
=== 密码文件配置 ===<br />
BOINC 服务会在首次启动后自动创建密码文件 {{ic|/var/lib/boinc/gui_rpc_auth.cfg}},任何管理器都需要使用该密码来链接 BOINC 服务。如果从菜单图标启动 BOINC Manager,它会自动读取该文件。但该文件默认只有用户 boinc 可读,为了连接 BOINC 服务,你需要先将当前用户加入 boinc 组,并修改密码文件的访问权限:<br />
$ gpasswd -a <用户名> boinc<br />
$ sudo chmod g+r /var/lib/boinc/gui_rpc_auth.cfg<br />
重新登录即可生效。<br />
<br />
如果你经常使用命令行操作,或需要从终端启动 BOINC Manager,则可将密码文件连接至用户目录,这样 BOINC Manager 或 boinccmd 就能自动连接 BOINC 服务:<br />
$ cd ~/<br />
$ ln -s /var/lib/boinc/gui_rpc_auth.cfg gui_rpc_auth.cfg<br />
<br />
若要手动设置该密码(甚至清空),只需修改 {{ic|/var/lib/boinc/gui_rpc_auth.cfg}} 文件,并重启 BOINC 服务即可。<br />
<br />
=== 图形界面 ===<br />
运行 {{ic|boincmgr}} 命令启动图形界面的 BOINC Manager:<br />
$ boincmgr<br />
<br />
首次启动时,BOINC Manager 会指引你加入项目。注意,有些项目可以通过 BOINC Manager 直接创建项目,而有些则需要先在其网页上注册。如果存储空间、计算能力和时间等条件允许,可以通过菜单项 ''工具 / 增加项目'' 加入多个项目。<br />
<br />
如果 BOINC Manager 没有像上面那样指引你加入项目,那么可能是连接 BOINC 服务失败了。点击菜单项 ''高级 / 选择计算机'',输入计算机名(本机使用 {{ic|localhost}} 即可),然后输入 {{ic|/var/lib/boinc/gui_rpc_auth.cfg}} 中的密码即可。(如果不想每次都这样做,请确保按照上面的步骤正确配置 gui_rpc_auth.cfg 。)<br />
<br />
==== 使用 GPU 的项目 ====<br />
要使用 GPU 计算,需要安装闭源的 [[NVIDIA (简体中文)|NVIDIA]] 和 [[ATI (简体中文)|AMD]] 驱动。对于 ATI/AMD 显卡,还需要安装 [[AUR (简体中文)|AUR]] 软件包 {{AUR|libopencl}}。NVIDIA 用户只需安装 [extra] 中的 {{Pkg|opencl-nvidia}} 即可。<br />
<br />
此外还需要将 boinc 用户加入 video 组:<br />
# gpasswd -a boinc video<br />
boinc 用户还需要有访问你的 X session 的权限:<br />
xhost local:boinc<br />
若要避免每次都输入,可将上述命令加入启动脚本。<br />
<br />
{{注意|对于使用双显卡笔记本电脑的用户,如果使用某些软件(如 bumblebee/bbswitch)禁用了高性能显卡的驱动,在使用 GPU 计算时,可能遇到一些麻烦(找不到显卡、计算错误等等)。如果要使用 GPU 计算,原则上你必须在 BOINC 运行时确保显卡开启,以及加载正确的驱动模块。当前似乎还没有什么好的解决方案。 —— 译者注}}<br />
<br />
=== 命令行界面 ===<br />
<br />
对于命令行操作,可参照 {{ic|boinccmd}} 和 {{ic|boinc}} 命令的帮助及 man 手册。{{ic|boinccmd}} 是 BOINC 自带的命令行界面工具。<br />
<br />
== 日志文件 ==<br />
BOINC 的日志文件存放在 {{ic|/var/lib/boinc/}} 中:<br />
/var/lib/boinc/stderrdae.txt<br />
/var/lib/boinc/stdoutdae.txt<br />
<br />
== 选择项目时需要考虑的因素 ==<br />
不同项目的硬件要求(CPU、磁盘空间等)有所差异,每个任务的计算时间也不尽相同。如果不能在上报期限前完成任务,服务器会判定任务过期,并把他交给其他计算机。请根据硬件条件选择合适的项目,避免这样的问题发生。<br />
<br />
此外,项目的研究方向和公开度也是需要考虑的因素。<br />
<br />
=== 64 位系统 ===<br />
某些项目只提供32位计算程序和图形显示程序,因此你需要从 [multilib] 安装一些基本的库。<br />
<br />
;运行计算程序需要通常需要(以 Climateprediction 项目为例):<br />
:{{Pkg|lib32-glibc}}, {{Pkg|lib32-glib2}}<br />
;显示图形需要(如 WCG 的几个子项目、Climateprediction):<br />
:{{Pkg|lib32-pango}}, {{Pkg|lib32-libxdamage}}, {{Pkg|lib32-libxi}}, {{Pkg|lib32-libgl}}, {{AUR|lib32-libjpeg6}} (AUR), {{Pkg|lib32-libxmu}}<br />
<br />
==疑难解答==<br />
=== GPU missing ===<br />
如果出现这样的错误:<br />
{{bc|GPU Missing}}<br />
且 GPU 计算任务无法启动,则应尝试 [[Daemons (简体中文)#重启服务|重启]] {{ic|boinc.service}} 服务。<br />
<br />
这样的错误通常是因为 BOINC 服务先于 X 启动。<br />
<br />
=== 笔记本过热,续航时间缩短 ===<br />
如果在笔记本上运行 BOINC,且 CPU 频率管理策略为 ondemand(默认即是),CPU 频率会始终保持在最大值,导致 CPU 过热、续航时间缩短。解决方法是要求内核调整 CPU 频率时忽略 BOINC 任务的影响:<br />
{{bc|# echo 1 >/sys/devices/system/cpu/cpufreq/ondemand/ignore_nice_load}}<br />
<br />
创建以下 tmpfiles.d 配置,可实现开机自动设置:<br />
{{hc|/etc/tmpfiles.d/ondemand-ignore-nice.conf|<br />
w /sys/devices/system/cpu/cpufreq/ondemand/ignore_nice_load - - - - 1}}<br />
<br />
=== 无法下载 World Community Grid 任务 ===<br />
由于 openssl 的问题,一些用户无法下载 World Community Grid 项目的任务。要解决这一问题,需要对该软件包进行一些修改,然后重新编译。<br />
<br />
首先,获取 openssl 的 PKGBUILD,以及打包其他所需文件:<br />
$ git clone git://projects.archlinux.org/svntogit/packages.git --single-branch --branch packages/openssl<br />
如果你安装了 yaourt,也可以使用如下命令:<br />
$ yaourt -G openssl<br />
<br />
执行完第一个命令后,你会看到一个 ./packages/trunk/ 目录,其中有包括 PKGBUILD 在内的若干文件。该 PKGBUILD 需要修改,正确的可以从[http://pastebin.com/pYcYf4dr 这里]下载。<br />
<br />
然后,创建软件软件包:<br />
$ makepkg <br />
<br />
完成后,会出现一个扩展名为 .pkg.tar.xz 的软件包,安装并重启 BOINC 服务即可:<br />
<br />
# pacman -U openssl-*.pkg.tar.xz<br />
# systemctl restart boinc<br />
<br />
大功告成。如果还有问题,请参见[https://bbs.archlinux.org/viewtopic.php?pid=1160393#p1160393 原讨论贴],发帖请求帮助。<br />
<br />
== 相关资源 ==<br />
* [http://www.equn.com/ 中国分布式计算总站]<br />
* [http://boinc.berkeley.edu/ BOINC 官方网站]<br />
* [http://boinc.berkeley.edu/projects.php BOINC 项目列表]<br />
* [[Wikipedia:BOINC|维基百科的介绍]]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=BOINC_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=293458
BOINC (简体中文)
2014-01-18T13:17:41Z
<p>Cuihao: /* 使用 */</p>
<hr />
<div>[[Category:Mathematics and science (简体中文)]]<br />
[[en:BOINC]]<br />
[[de:BOINC]]<br />
[[fr:BOINC]]<br />
[[it:BOINC]]<br />
[[ja:BOINC]]<br />
[[sk:BOINC]]<br />
[http://boinc.berkeley.edu/ BOINC 官方网站]:“利用您的计算机(Windows、Mac 以及 Linux)的空闲的时间,帮助寻找治疗疾病的方法、研究全球变暖、发现脉冲星…… 进行各种各样的科学研究。这一切简单且安全!”<br />
<br />
[[Wikipedia:BOINC|维基百科]]:“伯克利开放式网络计算平台(Berkeley Open Infrastructure for Network Computing,BOINC)是一个用于志愿计算和网格计算的非商业的中间件系统。开发 BOINC 的最初目的是支援 SETI@home 项目,而后来它发展成为一个支持多种项目的强大的分布式计算平台,这些项目的研究领域涵盖了数学、医学、分子生物学、气候学以及天体物理学等等。BOINC 力图让研究人员享用到全世界数以亿计的个人电脑的强大计算能力。”<br />
<br />
== 安装 ==<br />
[[Official Repositories (简体中文)|官方仓库]] 提供了 {{Pkg|boinc}} 和 {{Pkg|boinc-nox}} 软件包,使用 [[pacman|安装]] 其一即可。后者不依赖 [[Xorg (简体中文)|Xorg]],适用于没有物理控制台的服务器。<br />
<br />
两个软件包都提供了系统服务 {{ic|boinc.service}},可以使用 {{ic|systemctl}} 管理。详情参见 [[Systemd (简体中文)#使用单元]]。<br />
<br />
== 使用 ==<br />
{{注意|以下内容仅仅适用于:1、Arch 官方软件包提供的 BOINC;2、从 systemd 服务正确启动。如要通过 boinc 命令启动管理,请自行查阅相关文档。}}<br />
<br />
=== 密码文件配置 ===<br />
BOINC 服务会在首次启动后自动创建密码文件 {{ic|/var/lib/boinc/gui_rpc_auth.cfg}},任何管理器都需要使用该密码来链接 BOINC 服务。如果从菜单图标启动 BOINC Manager,它会自动读取该文件。但该文件默认只有用户 boinc 可读,为了连接 BOINC 服务,你需要先将当前用户加入 boinc 组,并修改密码文件的访问权限:<br />
$ gpasswd -a <用户名> boinc<br />
$ sudo chmod g+x /var/lib/boinc/gui_rpc_auth.cfg<br />
重新登录即可生效。<br />
<br />
如果你经常使用命令行操作,或需要从终端启动 BOINC Manager,则可将密码文件连接至用户目录,这样 BOINC Manager 或 boinccmd 就能自动连接 BOINC 服务:<br />
$ cd ~/<br />
$ ln -s /var/lib/boinc/gui_rpc_auth.cfg gui_rpc_auth.cfg<br />
<br />
若要手动设置该密码(甚至清空),只需修改 {{ic|/var/lib/boinc/gui_rpc_auth.cfg}} 文件,并重启 BOINC 服务即可。<br />
<br />
=== 图形界面 ===<br />
运行 {{ic|boincmgr}} 命令启动图形界面的 BOINC Manager:<br />
$ boincmgr<br />
<br />
首次启动时,BOINC Manager 会指引你加入项目。注意,有些项目可以通过 BOINC Manager 直接创建项目,而有些则需要先在其网页上注册。如果存储空间、计算能力和时间等条件允许,可以通过菜单项 ''工具 / 增加项目'' 加入多个项目。<br />
<br />
如果 BOINC Manager 没有像上面那样指引你加入项目,那么可能是连接 BOINC 服务失败了。点击菜单项 ''高级 / 选择计算机'',输入计算机名(本机使用 {{ic|localhost}} 即可),然后输入 {{ic|/var/lib/boinc/gui_rpc_auth.cfg}} 中的密码即可。(如果不想每次都这样做,请确保按照上面的步骤正确配置 gui_rpc_auth.cfg 。)<br />
<br />
==== 使用 GPU 的项目 ====<br />
要使用 GPU 计算,需要安装闭源的 [[NVIDIA (简体中文)|NVIDIA]] 和 [[ATI (简体中文)|AMD]] 驱动。对于 ATI/AMD 显卡,还需要安装 [[AUR (简体中文)|AUR]] 软件包 {{AUR|libopencl}}。NVIDIA 用户只需安装 [extra] 中的 {{Pkg|opencl-nvidia}} 即可。<br />
<br />
此外还需要将 boinc 用户加入 video 组:<br />
# gpasswd -a boinc video<br />
boinc 用户还需要有访问你的 X session 的权限:<br />
xhost local:boinc<br />
若要避免每次都输入,可将上述命令加入启动脚本。<br />
<br />
{{注意|对于使用双显卡笔记本电脑的用户,如果使用某些软件(如 bumblebee/bbswitch)禁用了高性能显卡的驱动,在使用 GPU 计算时,可能遇到一些麻烦(找不到显卡、计算错误等等)。如果要使用 GPU 计算,原则上你必须在 BOINC 运行时确保显卡开启,以及加载正确的驱动模块。当前似乎还没有什么好的解决方案。 —— 译者注}}<br />
<br />
=== 命令行界面 ===<br />
<br />
对于命令行操作,可参照 {{ic|boinccmd}} 和 {{ic|boinc}} 命令的帮助及 man 手册。{{ic|boinccmd}} 是 BOINC 自带的命令行界面工具。<br />
<br />
== 日志文件 ==<br />
BOINC 的日志文件存放在 {{ic|/var/lib/boinc/}} 中:<br />
/var/lib/boinc/stderrdae.txt<br />
/var/lib/boinc/stdoutdae.txt<br />
<br />
== 选择项目时需要考虑的因素 ==<br />
不同项目的硬件要求(CPU、磁盘空间等)有所差异,每个任务的计算时间也不尽相同。如果不能在上报期限前完成任务,服务器会判定任务过期,并把他交给其他计算机。请根据硬件条件选择合适的项目,避免这样的问题发生。<br />
<br />
此外,项目的研究方向和公开度也是需要考虑的因素。<br />
<br />
=== 64 位系统 ===<br />
某些项目只提供32位计算程序和图形显示程序,因此你需要从 [multilib] 安装一些基本的库。<br />
<br />
;运行计算程序需要通常需要(以 Climateprediction 项目为例):<br />
:{{Pkg|lib32-glibc}}, {{Pkg|lib32-glib2}}<br />
;显示图形需要(如 WCG 的几个子项目、Climateprediction):<br />
:{{Pkg|lib32-pango}}, {{Pkg|lib32-libxdamage}}, {{Pkg|lib32-libxi}}, {{Pkg|lib32-libgl}}, {{AUR|lib32-libjpeg6}} (AUR), {{Pkg|lib32-libxmu}}<br />
<br />
==疑难解答==<br />
=== GPU missing ===<br />
如果出现这样的错误:<br />
{{bc|GPU Missing}}<br />
且 GPU 计算任务无法启动,则应尝试 [[Daemons (简体中文)#重启服务|重启]] {{ic|boinc.service}} 服务。<br />
<br />
这样的错误通常是因为 BOINC 服务先于 X 启动。<br />
<br />
=== 笔记本过热,续航时间缩短 ===<br />
如果在笔记本上运行 BOINC,且 CPU 频率管理策略为 ondemand(默认即是),CPU 频率会始终保持在最大值,导致 CPU 过热、续航时间缩短。解决方法是要求内核调整 CPU 频率时忽略 BOINC 任务的影响:<br />
{{bc|# echo 1 >/sys/devices/system/cpu/cpufreq/ondemand/ignore_nice_load}}<br />
<br />
创建以下 tmpfiles.d 配置,可实现开机自动设置:<br />
{{hc|/etc/tmpfiles.d/ondemand-ignore-nice.conf|<br />
w /sys/devices/system/cpu/cpufreq/ondemand/ignore_nice_load - - - - 1}}<br />
<br />
=== 无法下载 World Community Grid 任务 ===<br />
由于 openssl 的问题,一些用户无法下载 World Community Grid 项目的任务。要解决这一问题,需要对该软件包进行一些修改,然后重新编译。<br />
<br />
首先,获取 openssl 的 PKGBUILD,以及打包其他所需文件:<br />
$ git clone git://projects.archlinux.org/svntogit/packages.git --single-branch --branch packages/openssl<br />
如果你安装了 yaourt,也可以使用如下命令:<br />
$ yaourt -G openssl<br />
<br />
执行完第一个命令后,你会看到一个 ./packages/trunk/ 目录,其中有包括 PKGBUILD 在内的若干文件。该 PKGBUILD 需要修改,正确的可以从[http://pastebin.com/pYcYf4dr 这里]下载。<br />
<br />
然后,创建软件软件包:<br />
$ makepkg <br />
<br />
完成后,会出现一个扩展名为 .pkg.tar.xz 的软件包,安装并重启 BOINC 服务即可:<br />
<br />
# pacman -U openssl-*.pkg.tar.xz<br />
# systemctl restart boinc<br />
<br />
大功告成。如果还有问题,请参见[https://bbs.archlinux.org/viewtopic.php?pid=1160393#p1160393 原讨论贴],发帖请求帮助。<br />
<br />
== 相关资源 ==<br />
* [http://www.equn.com/ 中国分布式计算总站]<br />
* [http://boinc.berkeley.edu/ BOINC 官方网站]<br />
* [http://boinc.berkeley.edu/projects.php BOINC 项目列表]<br />
* [[Wikipedia:BOINC|维基百科的介绍]]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=BOINC_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=293456
BOINC (简体中文)
2014-01-18T13:14:56Z
<p>Cuihao: Created page with "Category:Mathematics and science (简体中文) en:BOINC de:BOINC fr:BOINC it:BOINC ja:BOINC sk:BOINC [http://boinc.berkeley.edu/ BOINC 官方网..."</p>
<hr />
<div>[[Category:Mathematics and science (简体中文)]]<br />
[[en:BOINC]]<br />
[[de:BOINC]]<br />
[[fr:BOINC]]<br />
[[it:BOINC]]<br />
[[ja:BOINC]]<br />
[[sk:BOINC]]<br />
[http://boinc.berkeley.edu/ BOINC 官方网站]:“利用您的计算机(Windows、Mac 以及 Linux)的空闲的时间,帮助寻找治疗疾病的方法、研究全球变暖、发现脉冲星…… 进行各种各样的科学研究。这一切简单且安全!”<br />
<br />
[[Wikipedia:BOINC|维基百科]]:“伯克利开放式网络计算平台(Berkeley Open Infrastructure for Network Computing,BOINC)是一个用于志愿计算和网格计算的非商业的中间件系统。开发 BOINC 的最初目的是支援 SETI@home 项目,而后来它发展成为一个支持多种项目的强大的分布式计算平台,这些项目的研究领域涵盖了数学、医学、分子生物学、气候学以及天体物理学等等。BOINC 力图让研究人员享用到全世界数以亿计的个人电脑的强大计算能力。”<br />
<br />
== 安装 ==<br />
[[Official Repositories (简体中文)|官方仓库]] 提供了 {{Pkg|boinc}} 和 {{Pkg|boinc-nox}} 软件包,使用 [[pacman|安装]] 其一即可。后者不依赖 [[Xorg (简体中文)|Xorg]],适用于没有物理控制台的服务器。<br />
<br />
两个软件包都提供了系统服务 {{ic|boinc.service}},可以使用 {{ic|systemctl}} 管理。详情参见 [[Systemd (简体中文)#使用单元]]。<br />
<br />
== 使用 ==<br />
{{注意|以下内容仅仅适用于:1、Arch 官方软件包提供的 BOINC;2、从 systemd 服务正确启动。如要通过 boinc 命令启动管理,请自行查阅相关文档。}}<br />
<br />
=== 密码文件配置 ===<br />
通常情况下,BOINC 会自动创建密码文件 {{ic|/var/lib/boinc/gui_rpc_auth.cfg}},任何管理器都需要使用该密码来链接 BOINC 服务。如果从菜单图标启动 BOINC Manager,它会自动读取该文件。但该文件默认只有用户 boinc 可读,为了连接 BOINC 服务,你需要先将当前用户加入 boinc 组,并修改密码文件的访问权限:<br />
$ gpasswd -a <用户名> boinc<br />
$ sudo chmod g+x /var/lib/boinc/gui_rpc_auth.cfg<br />
重新登录即可生效。<br />
<br />
如果你经常使用命令行操作,或需要从终端启动 BOINC Manager,则可将密码文件连接至用户目录,这样 BOINC Manager 或 boinccmd 就能自动连接 BOINC 服务:<br />
$ cd ~/<br />
$ ln -s /var/lib/boinc/gui_rpc_auth.cfg gui_rpc_auth.cfg<br />
<br />
若要手动设置该密码(甚至清空),只需修改 {{ic|/var/lib/boinc/gui_rpc_auth.cfg}} 文件,并重启 BOINC 服务即可。<br />
<br />
=== 图形界面 ===<br />
运行 {{ic|boincmgr}} 命令启动图形界面的 BOINC Manager:<br />
$ boincmgr<br />
<br />
首次启动时,BOINC Manager 会指引你加入项目。注意,有些项目可以通过 BOINC Manager 直接创建项目,而有些则需要先在其网页上注册。如果存储空间、计算能力和时间等条件允许,可以通过菜单项 ''工具 / 增加项目'' 加入多个项目。<br />
<br />
如果 BOINC Manager 没有像上面那样指引你加入项目,那么可能是连接 BOINC 服务失败了。点击菜单项 ''高级 / 选择计算机'',输入计算机名(本机使用 {{ic|localhost}} 即可),然后输入 {{ic|/var/lib/boinc/gui_rpc_auth.cfg}} 中的密码即可。(如果不想每次都这样做,请确保按照上面的步骤正确配置 gui_rpc_auth.cfg 。)<br />
<br />
==== 使用 GPU 的项目 ====<br />
要使用 GPU 计算,需要安装闭源的 [[NVIDIA (简体中文)|NVIDIA]] 和 [[ATI (简体中文)|AMD]] 驱动。对于 ATI/AMD 显卡,还需要安装 [[AUR (简体中文)|AUR]] 软件包 {{AUR|libopencl}}。NVIDIA 用户只需安装 [extra] 中的 {{Pkg|opencl-nvidia}} 即可。<br />
<br />
此外还需要将 boinc 用户加入 video 组:<br />
# gpasswd -a boinc video<br />
boinc 用户还需要有访问你的 X session 的权限:<br />
xhost local:boinc<br />
若要避免每次都输入,可将上述命令加入启动脚本。<br />
<br />
{{注意|对于使用双显卡笔记本电脑的用户,如果使用某些软件(如 bumblebee/bbswitch)禁用了高性能显卡的驱动,在使用 GPU 计算时,可能遇到一些麻烦(找不到显卡、计算错误等等)。如果要使用 GPU 计算,原则上你必须在 BOINC 运行时确保显卡开启,以及加载正确的驱动模块。当前似乎还没有什么好的解决方案。 —— 译者注}}<br />
<br />
=== 命令行界面 ===<br />
<br />
对于命令行操作,可参照 {{ic|boinccmd}} 和 {{ic|boinc}} 命令的帮助及 man 手册。{{ic|boinccmd}} 是 BOINC 自带的命令行界面工具。<br />
<br />
== 日志文件 ==<br />
BOINC 的日志文件存放在 {{ic|/var/lib/boinc/}} 中:<br />
/var/lib/boinc/stderrdae.txt<br />
/var/lib/boinc/stdoutdae.txt<br />
<br />
== 选择项目时需要考虑的因素 ==<br />
不同项目的硬件要求(CPU、磁盘空间等)有所差异,每个任务的计算时间也不尽相同。如果不能在上报期限前完成任务,服务器会判定任务过期,并把他交给其他计算机。请根据硬件条件选择合适的项目,避免这样的问题发生。<br />
<br />
此外,项目的研究方向和公开度也是需要考虑的因素。<br />
<br />
=== 64 位系统 ===<br />
某些项目只提供32位计算程序和图形显示程序,因此你需要从 [multilib] 安装一些基本的库。<br />
<br />
;运行计算程序需要通常需要(以 Climateprediction 项目为例):<br />
:{{Pkg|lib32-glibc}}, {{Pkg|lib32-glib2}}<br />
;显示图形需要(如 WCG 的几个子项目、Climateprediction):<br />
:{{Pkg|lib32-pango}}, {{Pkg|lib32-libxdamage}}, {{Pkg|lib32-libxi}}, {{Pkg|lib32-libgl}}, {{AUR|lib32-libjpeg6}} (AUR), {{Pkg|lib32-libxmu}}<br />
<br />
==疑难解答==<br />
=== GPU missing ===<br />
如果出现这样的错误:<br />
{{bc|GPU Missing}}<br />
且 GPU 计算任务无法启动,则应尝试 [[Daemons (简体中文)#重启服务|重启]] {{ic|boinc.service}} 服务。<br />
<br />
这样的错误通常是因为 BOINC 服务先于 X 启动。<br />
<br />
=== 笔记本过热,续航时间缩短 ===<br />
如果在笔记本上运行 BOINC,且 CPU 频率管理策略为 ondemand(默认即是),CPU 频率会始终保持在最大值,导致 CPU 过热、续航时间缩短。解决方法是要求内核调整 CPU 频率时忽略 BOINC 任务的影响:<br />
{{bc|# echo 1 >/sys/devices/system/cpu/cpufreq/ondemand/ignore_nice_load}}<br />
<br />
创建以下 tmpfiles.d 配置,可实现开机自动设置:<br />
{{hc|/etc/tmpfiles.d/ondemand-ignore-nice.conf|<br />
w /sys/devices/system/cpu/cpufreq/ondemand/ignore_nice_load - - - - 1}}<br />
<br />
=== 无法下载 World Community Grid 任务 ===<br />
由于 openssl 的问题,一些用户无法下载 World Community Grid 项目的任务。要解决这一问题,需要对该软件包进行一些修改,然后重新编译。<br />
<br />
首先,获取 openssl 的 PKGBUILD,以及打包其他所需文件:<br />
$ git clone git://projects.archlinux.org/svntogit/packages.git --single-branch --branch packages/openssl<br />
如果你安装了 yaourt,也可以使用如下命令:<br />
$ yaourt -G openssl<br />
<br />
执行完第一个命令后,你会看到一个 ./packages/trunk/ 目录,其中有包括 PKGBUILD 在内的若干文件。该 PKGBUILD 需要修改,正确的可以从[http://pastebin.com/pYcYf4dr 这里]下载。<br />
<br />
然后,创建软件软件包:<br />
$ makepkg <br />
<br />
完成后,会出现一个扩展名为 .pkg.tar.xz 的软件包,安装并重启 BOINC 服务即可:<br />
<br />
# pacman -U openssl-*.pkg.tar.xz<br />
# systemctl restart boinc<br />
<br />
大功告成。如果还有问题,请参见[https://bbs.archlinux.org/viewtopic.php?pid=1160393#p1160393 原讨论贴],发帖请求帮助。<br />
<br />
== 相关资源 ==<br />
* [http://www.equn.com/ 中国分布式计算总站]<br />
* [http://boinc.berkeley.edu/ BOINC 官方网站]<br />
* [http://boinc.berkeley.edu/projects.php BOINC 项目列表]<br />
* [[Wikipedia:BOINC|维基百科的介绍]]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Help_talk:Style_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=275832
Help talk:Style (简体中文)
2013-09-17T08:17:24Z
<p>Cuihao: </p>
<hr />
<div>==初步统一翻译术语的提案==<br />
<br />
先前曾和 Fengchao 先生[https://wiki.archlinux.org/index.php/Help:Editing 提议过统一英语术语的翻译],他让在下能在该页面规范的就先规范。如今在下差不多译毕,但仍觉得有必要'''专门建立统一英语术语翻译的页面,并加以维护,好方便大家在翻译或编辑页面时,快速查阅,以大大加强中文维基的一致性'''。现正式提出初步统一英语术语翻译的规范表如下,欢迎诸位提意见。如可行,则页面一般由在下建立并维护,并同时开始规范所有中文页面:<br />
<br />
{|<br />
! 英语术语 !! 翻译 !! 说明<br />
|-<br />
| Notes || 注意 || <br />
|-<br />
| Warning || 警告 ||<br />
|-<br />
| Tips || 小贴士 || 由于 Notes、Warning、Tips 的翻译已经模板化,即 [[Template:注意]] [[Template:警告]] [[Template:小贴士]] 故只能直接采用这样的翻译了,不予考虑有无更好的翻译。<br />
|-<br />
| Troubleshooting || 故障排除 ||<br />
|-<br />
| Known issues || 已知问题 || 鉴于 Xgdgsc 先生的意见,“疑难杂症”规范作废。<s>由于在下词穷,只能继续沿用原来的规范“已知问题”了。</s>“已知毛病”这词如何?<br />
|-<br />
| See also || 参见 || 在下认为前两者的韵味远胜于直译“问题解决”,“已知问题”,也更好地符合 [[Help:Style (简体中文)#措辞]] 提到的第一条规范。<br />
|-<br />
| Tips and Tricks || 锦囊妙计 || 恐怕这地方最充满争议,请大家继续提意见。其实还有一个貌似也不赖的待选翻译:“提示与窍门”。补上Felixonmars 先生提供的“技巧提示”。<br />
|-<br />
| Section || 章节 || 其实该页面却翻译为”段落“,恐错得离谱。<br />
|-<br />
| Summary || 摘要 ||<br />
|-<br />
| Preface or introduction || 前言与介绍 ||'''至此,以上应该是该页面所能规范的所有英语术语了,没有涉及到的其它术语翻译则规范则如下''',日后有待完善,到时欢迎大伙补充。<br />
|-<br />
| you || 您 || 一般翻译最好能避免英文 you 的直译,能避开最好<br />
|-<br />
| repository || 仓库 ||<br />
|-<br />
| Arch Philosophy || Arch 之道 ||<br />
|-<br />
| Package || 软件包 ||<br />
|-<br />
| Package group || 多软件包 ||<br />
|}<br />
<br />
最后,有必要声明在下提出该提案的初衷:和 [[The Arch Way (简体中文)|Arch 之道]] 无异,统一翻译术语是为了让中文维基更为简单易解,其措辞让用户感到愉悦,即'''以用户为中心'''。其实不光要统一翻译术语,将来在下还会出台翻译指南以供大伙借鉴。 -- [[User:Acgtyrant|Acgtyrant]] ([[User talk:Acgtyrant|talk]]) 11:43, 28 August 2013 (UTC)<br />
<br />
: 意外发现原来还有现成的 [[Arch Terminology (简体中文)]],不出意外,统一的翻译术语规范就直接放到这页面了 -- [[User:Acgtyrant|Acgtyrant]] ([[User talk:Acgtyrant|talk]]) 11:56, 28 August 2013 (UTC)<br />
:: Note 实际应该分成两个模板,一个是注意,一个是附注,两个英文一样,但是中文意思差别很大。 还没有建新模板.-- [[User:Fengchao|Fengchao]] ([[User talk:Fengchao|talk]]) 12:10, 28 August 2013 (UTC)<br />
::: <s>噢?那鄙人再花时间研究一下这两套所谓的模板。</s> 在下并不太清楚这样的情况,或许阁下该先新建所谓的”附注“模板,并在其页面加以说明下与”注意“模板的详细区别?到时再研究彼此的翻译规范。 -- [[User:Acgtyrant|Acgtyrant]] ([[User talk:Acgtyrant|talk]]) 12:41, 28 August 2013 (UTC)<br />
::: “一般翻译最好能避免英文 you 的直译,能避开最好”的意思是说尽量避免在中文维基使用第二人称吗? -- [[User:Acgtyrant|Acgtyrant]] ([[User talk:Acgtyrant|talk]]) 13:15, 28 August 2013 (UTC)<br />
: 疑难杂症这个翻译也过了,疑难杂症侧重点在疑难,已知问题侧重点在已知,很多时候已知问题并不疑难。个人反对在翻译中使用这样的“韵味”词语。反对锦囊妙计,提示与技巧与故障排除还是在一个档次的,唯一不在档次的是疑难杂症。arch phychology 是不是应该是philosophy? [[User:Xgdgsc|Xgdgsc]] ([[User talk:Xgdgsc|talk]]) 12:26, 28 August 2013 (UTC)<br />
:: 所言甚是,见笑了,让鄙人再想一想有无更合适的翻译。 -- [[User:Acgtyrant|Acgtyrant]] ([[User talk:Acgtyrant|talk]]) 12:41, 28 August 2013 (UTC)<br />
::: Known issus 翻译为“已知毛病”如何?觉得比“已知问题”更本地化点... -- [[User:Acgtyrant|Acgtyrant]] ([[User talk:Acgtyrant|talk]]) 13:03, 28 August 2013 (UTC)<br />
:::: 毛病太口语话了, 不适合做书面语。 -- [[User:Fengchao|Fengchao]] ([[User talk:Fengchao|talk]]) 12:25, 30 August 2013 (UTC)<br />
:: 先别急着改,等等更多人的意见。[[User:Xgdgsc|Xgdgsc]] ([[User talk:Xgdgsc|talk]]) 15:11, 28 August 2013 (UTC)<br />
:: 我也来提供一个 Tips and Tricks 的候选翻译: 技巧提示 -- [[User:Felixonmars|Felixonmars]] ([[User talk:Felixonmars|talk]]) 14:42, 16 September 2013 (UTC)<br />
::: 这个的确比『提示与技巧』精炼多了,也更为通俗点。-- [[User:Acgtyrant|Acgtyrant]] ([[User talk:Acgtyrant|talk]]) 07:34, 17 September 2013 (UTC)<br />
<br />
为了避免诸位产生不必要的错觉,在下长时间没有再继续跟进讨论,并不是打算半途而废,是因为在校 Arch Linux 一直没法顺利上网,需要可观的时间继续破解锐捷,成功时还会回来继续的,不过到时也许就该作出决定了,总之慢慢来。-- [[User:Acgtyrant|Acgtyrant]] ([[User talk:Acgtyrant|talk]]) 07:34, 17 September 2013 (UTC)<br />
<br />
首先,对于“Package group”,我一般直译为“软件包组”了。感觉还是有一定群众基础的(可以Google一下),而且字面上也不难理解。“多软件包”一词我觉得比较奇怪,“多软件包 gnome”、“多软件包 gcc”什么的……<br />
<br />
Troubleshooting嘛,我以前给翻译成“疑难解答”了。大概也不是我一人在用,查了历史,skydriver君似乎也采用过这个翻译。另外Google了一下,Windows便是采用的这个翻译([http://windows.microsoft.com/zh-CN/windows7/products/features/windows-troubleshooting])。个人觉得还是比较好听的。 --[[User:Cuihao|Cuihao]] ([[User talk:Cuihao|talk]]) 08:17, 17 September 2013 (UTC)</div>
Cuihao
https://wiki.archlinux.org/index.php?title=ArchWiki:Translation_Team_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=271027
ArchWiki:Translation Team (简体中文)
2013-08-13T16:18:50Z
<p>Cuihao: alsa弃坑…… 上学没时间了 /* 页面维护列表 */</p>
<hr />
<div>[[Category:简体中文]]<br />
[[Category:ArchWiki (简体中文)]]<br />
[[en:ArchWiki Translation Team]]<br />
[[es:ArchWiki Translation Team]]<br />
[[hr:ArchWiki Translation Team]]<br />
[[it:ArchWiki Translation Team]]<br />
[[pl:ArchWiki Translation Team]]<br />
[[tr:ArchWiki_Çeviri_Ekibi]]<br />
Arch Wiki 上有许多中文页面,这些页面是无数中文志愿者劳动的结晶。随着时间推移,有些页面因为没有及时维护,内容严重过时。而目前的翻译工作缺少组织,效率偏低。所以参照西班牙和意大利翻译组的做法,添加这个页面。<br />
<br />
如果希望进行翻译和维护,只需要编辑下面的[[#页面维护列表]],将自己加为页面的维护者。如果列表中还没有要认领翻译的页面,请自行添加。如果因为时间原因无法再维护页面,请及时将自己从维护者列表中删除。<br />
<br />
== 创建翻译 ==<br />
{{注意|如果不准备翻译页面的大部分内容,请尽量不要新建简体中文页面。检查英文页面的更新需要花费不少精力,没有翻译的页面会增加维护负担。}}<br />
# 如果还不知道如何编辑 wiki,请阅读 [[Help:Editing (简体中文)|编辑帮助]]。<br />
# 阅读 [[Help:i18n (简体中文)|i18n帮助]],文章给出了 ArchWiki 国际化和本地化的指南。<br />
# [[Special:UserLogin |登录]] 以进行编辑。<br />
# 选择要翻译的页面,例如从 [[Special:Random|随机页面]] 或[[#页面维护列表 | 页面维护列表]] 中选择一个未翻译完成的页面。假设要翻译 [[Some Page]].<br />
# 进入选择的英文页面,点击页面顶部的 '''编辑'''。<br />
# 添加要翻译文件的语言间链接 (参见[[Help:i18n#Interlanguage links]])。<br />
# 复制所有页面代码。<br />
# 保存页面 (新加了语言链接)<br />
# 访问页面左边新添加的语言链接,应该会进到 [[Some Page (简体中文)]] : {{ic|<nowiki>https://wiki.archlinux.org/index.php/Some_Page_(</nowiki>''简体中文'')}}<br />
# 因为页面不存在,点击 '''创建'''。<br />
# 将显示一个编辑器 - 粘贴复制的英文页面。<br />
# 将文章分类修改为本地化版本,例如将 {{ic|<nowiki>[[Category:Internationalization]]</nowiki>}} 修改为 {{ic|<nowiki>[[Category:Internationalization (简体中文)]]</nowiki>}},参阅[[Help:Category (简体中文)]].<br />
# 修改语言间链接,指向英文页面(将 {{ic|zh-CN}} 修改为 {{ic|en}},并将英文页面移到文章顶部。<br />
# 翻译页面,进行保存。<br />
# (推荐)给翻译完成的页面加上[[Template:TranslationStatus (简体中文)|翻译状态]],后有详细介绍。<br />
# 更新所有其它语言页面,加入刚翻译文章的语言间链接。<br />
# (可选)创建一个简体中文名称的页面,指向新创建的页面:访问 {{ic|<nowiki>https://wiki.archlinux.org/index.php/</nowiki>''页面的中文名称''}}.<br />
# (可选)建立新页面,并加入:{{bc|<nowiki>#REDIRECT [[Some Page (简体中文)</nowiki>]]}}<br />
<br />
== 完善翻译 ==<br />
[https://wiki.archlinux.org/index.php?title=Special:WhatLinksHere/Template:Translateme_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&limit=100 这个页面] 包含了需要完善翻译的简体中文页面。完善翻译的基本步骤:<br />
# 选择自己比较熟悉的文章进行翻译<br />
# 先检查英文页面的对应段落,更新成最新的英文后再翻译,避免翻译过时的内容,减少信息遗漏。<br />
# 翻译完成后删除页面中的 <nowiki>{{translateme (简体中文)}}</nowiki> 标记<br />
# (推荐)给翻译完成的页面加上[[Template:TranslationStatus (简体中文)|翻译状态]],后有详细介绍。<br />
<br />
== 更新过期页面 == <br />
如果发现有 Wiki 页面过期或错误:<br />
* 小的改动,有时间可以立即进行修改同步,维护者并不控制页面的编辑权限,越多的人参与维护越好。如果改动较大,请先联系维护者,避免重复劳动。<br />
* 没有时间查看更改,请给页面加上 {{ic|<nowiki>{{out of date}}</nowiki>}} 模版,这样其他贡献者更容易发现需要更新的页面,而读者看到过期标记就可以直接查看英文页面,以免被错误内容误导,白白耽误时间。<br />
* 没有时间翻译,请将过期的中文部分删去,从英文页面中复制更改的部分到中文页面的相应部分,去掉{{ic|<nowiki>{{out of date}}</nowiki>}}模板(如果页面上有的话)并加上{{ic|<nowiki>{{translateme (简体中文)}}</nowiki>}}模板,这样其他贡献者就更容易发现需要翻译的页面,而读者也不会被过期的内容误导。<br />
如果发现有页面未翻译:<br />
* 有时间的话,请将页面中的英文部分翻译为中文,并去掉{{ic|<nowiki>{{translateme (简体中文)}}</nowiki>}}模板。<br />
* 没有时间翻译,请为页面添加{{ic|<nowiki>{{translateme (简体中文)}}</nowiki>}}模板,这样其他的贡献者就能更容易发现需要翻译的页面。<br />
{{注意|在修改页面上的模板时,请同时更新页面维护列表的翻译状态。}}<br />
<br />
== 维护翻译 ==<br />
完成页面的翻译只是初步完成任务,即时同步英文页面改动、更新翻译是一个持续性的工作,可能会耗费更多的时间。<br />
<br />
=== 页面认领 ===<br />
所有人都可以认领页面。认领后的责任包括进行翻译,关注英文页面的改动,及时同步翻译。<br />
<br />
为了更好的跟踪英文页面的修改,请务必在设置中启用监视列表邮件通知,并监视对应的英文页面(从设置中找到监视列表,加入英文页面。或者直接到英文页面点击页面顶端的监视标签。这样只要有改动,就会收到邮件通知)。<br />
<br />
{{小贴士|如果收到邮件通知后没有访问页面或者访问了页面却没有登录用户,下次页面改动时就不会再发邮件通知。可以点击监视列表中的'''标记所有页面为已读'''再次获取更新。}}<br />
<br />
如果页面有维护者但长期得不到更新,将会在维护列表中删除维护者。<br />
<br />
=== 翻译状态模板 ===<br />
Arch 作为滚动发行版,软件变化比较快,对应的文档变化也比较快。许多翻译的文章由于缺乏更新,会产生命令运行出错或不起作用等问题。而由于这些过期页面没有及时标记出来,所以用户无法及时获得更新。[[Template:TranslationStatus (简体中文)|翻译状态模板]]就是为了解决这个问题而创建。<br />
<br />
此模板可以起到如下作用:<br />
* 为用户提供翻译状况,包括翻译时间、英文页面的最后版本等<br />
* 用户可以点击查看翻译后,英文页面的改动,这样英文不是很好的用户可以只查看很小一部分英文内容,并判断出是否影响操作。<br />
* 翻译人员可以跟踪页面状况,通过[https://wiki.archlinux.org/index.php/Special:WhatLinksHere/Template:TranslationStatus_(简体中文) 模板的反向链接]可以查找到所有标记页面,查看需要更新翻译的部分。<br />
<br />
[[Template:TranslationStatus (简体中文)|模板页面]]有详细的使用方法。<br />
<br />
=== 页面维护列表 ===<br />
{{注意|请按照拉丁字母顺序添加页面。}}<br />
<br />
需要优先翻译的页面:<br />
* [[Systemd/User]]<br />
* [[netctl]]<br />
<br />
{| class="wikitable sortable collapsible" border="1"<br />
|-<br />
! 页面<br />
! 翻译状态<br />
! 维护者<br />
! class="unsortable" width="30%" | 备注<br />
|-<br />
| [[acpid (简体中文)]]<br />
| 过期<br />
| Cael<br />
| <br />
|-<br />
| [[ACPI modules (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[Activating Numlock on Bootup (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[Ad-hoc networking (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[Advanced Linux Sound Architecture (简体中文)]]<br />
| 未翻译<br />
| 无<br />
| 部分未翻译<br />
|-<br />
| [[AHCI (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[Allow Users to Shutdown (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[aMule (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[Android (简体中文)]]<br />
| 完成<br />
| Stlt1sean<br />
| 无<br />
|-<br />
| [[Apache, suEXEC and Virtual Hosts (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[Arch Based Distributions (Active) (简体中文)]]<br />
| 完成<br />
| acgtyrant<br />
| 无<br />
|-<br />
| [[Arch Build System (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[ArchWiki:About (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[ATI (简体中文)]]<br />
| 过期<br />
| 无<br />
| 请优先翻译此文<br />
|-<br />
| [[AUR Helpers (简体中文)]]<br />
| 未翻译<br />
| 无<br />
| 无<br />
|-<br />
| [[Avant Window Navigator (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[awesome (简体中文)]]<br />
| 进行中<br />
| Cael<br />
| 无<br />
|-<br />
| [[Bash (简体中文)]]<br />
| 完成<br />
| Jaurung<br />
| 无<br />
|-<br />
| [[Bumblebee (简体中文)]]<br />
| 完成<br />
| Peter<br />
| 无<br />
|-<br />
| [[AMD Catalyst (简体中文)]]<br />
| 过期<br />
| Shibao Zhao<br />
| 无<br />
|-<br />
| [[Common Applications (简体中文)]]<br />
| 翻译中<br />
| DavidChen<br />
| 翻译中<br />
|-<br />
| [[Common Applications/Science (简体中文)]]<br />
| 未翻译<br />
| 无<br />
| 无<br />
|-<br />
| [[Compiz (简体中文)]]<br />
| 过期<br />
| 无<br />
| 无<br />
|-<br />
| [[Configuring Network (简体中文)]]<br />
| 完成<br />
| Stlt1sean<br />
| 无<br />
|-<br />
| [[Core Utilities (简体中文)]]<br />
| 完成<br />
| acgtyrant<br />
| 无<br />
|-<br />
| [[CPU Frequency Scaling (简体中文)]]<br />
| 完成<br />
| Flockyrocky<br />
| <br />
|-<br />
| [[Creating Packages (简体中文)]]<br />
| 完成<br />
| Cael<br />
| 无<br />
|-<br />
| [[Disabling IPv6 (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[Downgrading Packages (简体中文)]]<br />
| 完成<br />
| Cael<br />
| 无<br />
|-<br />
| [[E17 (简体中文)]]<br />
| 完成<br />
| Aaron_chen<br />
| 同步翻译至2012年12月24日18:00英文页面<br />
|-<br />
| [[Emacs (简体中文)]]<br />
| 翻译中<br />
| Jaurung yuanhang<br />
| 未完成<br />
|-<br />
|-<br />
| [[Font Configuration (简体中文)]]<br />
| 翻译中<br />
| Jaurung<br />
| 完善中<br />
|-<br />
| [[Fonts (简体中文)]]<br />
| 未翻译<br />
| 无<br />
| 无<br />
|-<br />
| [[Fstab (简体中文)]]<br />
| 完成<br />
| Fengchao<br />
| 无<br />
|-<br />
| [[GRUB (简体中文)]]<br />
| 翻译中<br />
| 无<br />
| 无<br />
|-<br />
| [[Help:Style (简体中文)]]<br />
| 完成<br />
| Fengchao<br />
| 无<br />
|-<br />
| [[i3 (简体中文)]]<br />
| 完成<br />
| acgtyrant<br />
| 无<br />
|-<br />
| [[IBus (简体中文)]]<br />
| 未翻译<br />
| 无<br />
| 请优先翻译此文<br />
|-<br />
| [[Improve_Pacman_Performance_(简体中文)]]<br />
| 未翻译<br />
| 无<br />
| 无<br />
|-<br />
| [[Intel Graphics (简体中文)]]<br />
| 完成<br />
| Shibao Zhao<br />
| 无<br />
|-<br />
| [[KDE (简体中文)]]<br />
| 完成<br />
| Stlt1sean<br />
| 无<br />
|-<br />
| [[Kernel Compilation (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[Kernel Compilation/Arch Build System (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[LAMP (简体中文)]]<br />
| 完成<br />
| Liuzhengyi<br />
| 勘误中<br />
|-<br />
| [[Laptop Mode Tools (简体中文)]]<br />
| 未翻译<br />
| 无<br />
| 无<br />
|-<br />
| [[Lenovo ThinkPad T420 (简体中文)]]<br />
| 完成<br />
| Flockyrocky<br />
| 无<br />
|-<br />
| [[LibreOffice (简体中文)]]<br />
| 过期<br />
| 无<br />
| 无<br />
|-<br />
| [[Local Mirror (简体中文)]]<br />
| 完成<br />
| Jason Zhang<br />
| 完善中<br />
|-<br />
| [[Makepkg (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[mkinitcpio (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[NetworkManager (简体中文)]] || 部分翻译 || 无 || 请优先翻译<br />
|-<br />
| [[Network Time Protocol daemon (简体中文)]]<br />
| 未翻译<br />
| 无<br />
| 部分未翻译<br />
|-<br />
| [[Official Repositories (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[Openbox (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[OpenOffice (简体中文)]]<br />
| 过期<br />
| 无<br />
| 无<br />
|-<br />
| [[Pacman (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[Pacman GUI Frontends (简体中文)]]<br />
| 未翻译<br />
| 无<br />
| 无<br />
|-<br />
| [[pacman Tips (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[Pidgin (简体中文)]]<br />
| 进行中<br />
| Cael<br />
| 无 <br />
|- <br />
| [[Plasma (简体中文)]]<br />
| 未翻译<br />
| 无<br />
| 无<br />
|- <br />
| [[Polipo (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无 <br />
|-<br />
| [[Python (简体中文)]]<br />
| 完成<br />
| Fengchao<br />
| 无 <br />
|-<br />
| [[ranger (简体中文)]]<br />
| 完成<br />
| Jason Zhang<br />
| 完善中<br />
|-<br />
| [[Reporting_Bug_Guidelines_(简体中文)]]<br />
| 翻译中<br />
| Jason Zhang<br />
| <br />
|-<br />
| [[Smart Common Input Method platform (简体中文)]]<br />
| 过期<br />
| 无<br />
| 无<br />
|-<br />
| [[Secure Shell (简体中文)]]<br />
| 未翻译<br />
| 无<br />
| 部分未翻译<br />
|-<br />
| [[Systemd (简体中文)]]<br />
| 完成<br />
| cuihao<br />
| 无<br />
|-<br />
| [[TeXLive (简体中文)]]<br />
| 完成<br />
| reverland<br />
| 无<br />
|-<br />
| [[USB Installation Media (简体中文)]]<br />
| 完成<br />
| Stlt1sean <br />
| 无<br />
|-<br />
| [[Vim (简体中文)]]<br />
| 完成<br />
| yukirock<br />
| 无<br />
|-<br />
| [[VirtualBox (简体中文)]]<br />
| 翻译中<br />
| Carl X. Su<br />
| 请优先翻译此文<br />
|-<br />
| [[VMware (简体中文)]]<br />
| 翻译中<br />
| Jason Zhang<br />
| 无<br />
|-<br />
| [[Wine (简体中文)]]<br />
| 完成<br />
| cuihao<br />
| 无<br />
|- <br />
| [[Xfce (简体中文)]] || 翻译中 || ZaticWu || 请优先翻译<br />
|-<br />
| [[Xmonad (简体中文)]]<br />
| 未翻译<br />
| Rns<br />
| 翻译中<br />
|-<br />
| [[Xrandr (简体中文)]]<br />
| 完成<br />
| acgtyrant<br />
| 无<br />
|-<br />
| [[Xscreensaver (简体中文)]]<br />
| 完成<br />
| liuyix<br />
| 无<br />
|-<br />
| [[LXDE (简体中文)]]<br />
| 翻译中<br />
| Tuxzz<br />
| 无<br />
|}<br />
<br />
翻译状态说明:<br />
;过期:页面内容未与英文页面同步,对应{{ic|<nowiki>{{out of date}}</nowiki>}} 模版<br />
;未翻译:页面中含有英文内容,对应{{ic|<nowiki>{{translateme (简体中文)}}</nowiki>}}模板<br />
;完成:页面已与英文页面同步<br />
<br />
== 贡献列表 ==<br />
为翻译做出贡献的用户请加入列表,感谢所有人做出的贡献。<br />
* [[User:Fengchao|Fengchao]] &ndash; [[Special:Contributions/Fengchao|贡献]] &ndash; [[Special:EmailUser/Fengchao|Send Email]] &ndash; [[ArchWiki:Administrators|ArchWiki Administrators]]<br />
* [[User:Skydiver|Skydiver]] &ndash; [[Special:Contributions/Skydiver|贡献]] &ndash; [[Special:EmailUser/Skydiver|Send Email]] &ndash; [[ArchWiki:Maintainers|ArchWiki Maintainers]]<br />
* [[User:Alswl|Alswl]] &ndash; [[Special:Contributions/Alswl|贡献]] &ndash; [[Special:EmailUser/Alswl|Send Email]]<br />
* [[User:Reverland|Reverland]] &ndash; [[Special:Contributions/Reverland|贡献]] &ndash; [[Special:EmailUser/Reverland|Send Email]]<br />
* [[User:Cuihao|cuihao]] &ndash; [[Special:Contributions/Cuihao|贡献]] &ndash; [[Special:EmailUser/Cuihao|Send Email]]<br />
* [[User:Cael|Cael]] &ndash; [[Special:Contributions/Cael|贡献]] &ndash; [[Special:EmailUser/Cael|Send Email]]<br />
* [[User:Flockyrocky|Flockyrocky]] &ndash; [[Special:Contributions/Flockyrocky|贡献]] &ndash; [[Special:EmailUser/Flockyrocky|Send Email]]<br />
* [[User:Tuxzz|Tuxzz]] &ndash; [[Special:Contributions/Tuxzz|贡献]] &ndash; [[Special:EmailUser/Tuxzz|Send Email]]<br />
* [[User:Aaron_chen|Aaron_chen]] &ndash; [[Special:Contributions/Aaron_chen|贡献]] &ndash; [[Special:E17/Aaron_chen|Send Email]]<br />
* [[User:Shibao Zhao|Shibao Zhao]] &ndash; [[Special:Contributions/Shibao Zhao|贡献]] &ndash; [[Special:EmailUser/Shibao Zhao|Send Email]] &ndash;<br />
* [[User:Radflum|Yk]] &ndash; [[Special:Contributions/Radflum|贡献]] &ndash; [[Special:EmailUser/Radflum|Send Email]]<br />
* [[User:Hang yan|Hang yan]] &ndash; [[Special:Contributions/Hang yan|贡献]] &ndash; [[Special:EmailUser/Hang yan|Send Email]]<br />
* [[User:Acgtyrant|Acgtyrant]] &ndash; [[Special:Contributions/Acgtyrant|贡献]] &ndash; [[Special:EmailUser/Acgtyrant|Send Email]]<br />
* [[User:Xuchunyang|Xuchunyang]] &ndash; [[Special:Contributions/Acgtyrant|贡献]] &ndash; [[Special:EmailUser/Acgtyrant|Send Email]]<br />
* [[User:Stlt1sean|Stlt1sean]] &ndash; [[Special:Contributions/Stlt1sean|贡献]] &ndash; [[Special:EmailUser/Stlt1sean|Send Email]]<br />
* [[User:Carl_tw|Carl X. Su]] &ndash; [[Special:Contributions/Carl_tw|贡献]] &ndash; [[Special:EmailUser/Carl_tw|Send Email]]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Wine_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=263376
Wine (简体中文)
2013-06-19T03:00:16Z
<p>Cuihao: /* 安装 */</p>
<hr />
<div>[[Category:Wine (简体中文)]]<br />
[[cs:Wine]]<br />
[[de:Wine]]<br />
[[en:Wine]]<br />
[[es:Wine]]<br />
[[fr:Wine]]<br />
[[it:Wine]]<br />
[[ja:Wine]]<br />
[[ru:Wine]]<br />
[[zh-TW:Wine]]<br />
{{TranslationStatus (简体中文)|Wine|2013-06-19|263008}}<br />
{{Article summary start}}<br />
{{Article summary text|[[Wikipedia:Wine (software)|Wine]] 是类UNIX系统下运行微软Windows程序的"兼容层"。在Wine中运行的Windows程序,就如同运行原生Linux程序一样,不会有模拟器那样的性能问题。}}<br />
{{Article summary heading|Related}}<br />
{{Article summary wiki|Steam}}<br />
{{Article summary wiki|CrossOver}}<br />
{{Article summary end}}<br />
<br />
获取更详细的介绍请浏览[http://www.winehq.org/ 项目官方网站]和[http://wiki.winehq.org/ wiki]页面。<br />
<br />
== 安装 ==<br />
{{警告|如果您的账户能浏览某些文件或资源,Wine运行的程序也可以。Wine不是[[wikipedia:Sandbox (computer security)|沙箱]]。如果很重视安全,请考虑使用[[wikipedia:Virtualization|虚拟化]]。}}<br />
<br />
通过 [[pacman (简体中文)|pacman]] 从[[official repositories|官方仓库]]安装软件包 {{Pkg|wine}} 即可获取 Wine 模拟器。 对于64位系统,需要启用 [[Multilib]] 仓库。<br />
<br />
另外,您可能需要安装 {{pkg|wine_gecko}} 和 {{pkg|wine-mono}} 软件包。它们分别用于运行依赖于 Internet Explorer 和 .NET 的程序。不过,也可以随后通过 Wine 在需要时下载安装这些组件。但如果提前下载安装,您就可以离线使用它们,而且 Wine 不必为了每一个 WINEPREFIX 都单独下载。<br />
<br />
'''平台差异'''<br />
<br />
默认的Wine是32位的程序,也是i686的Arch软件包。所以它不能运行64位的Windows程序(反正是罕见的)。<br />
<br />
然而,x86_64的Wine软件包目前以{{ic| --enable-win64}}方式编译。这个参数激活了[[Wikipedia:WoW64|WoW64]]的Wine版本。<br />
<br />
*在Windows中,这个复杂的子系统允许用户同时使用32位和64位的Windows程序,甚至是在同一目录。<br />
<br />
*在Wine中,用户将必须建立单独分开的目录/前缀。这项Wine功能仍是试验阶段,并建议用户使用一个win32{{ic|WINEPREFIX}}。浏览[http://wiki.winehq.org/Wine64 Wine64]以获取有关这个的详细信息。<br />
<br />
总结一下,配置{{ic|1=WINEARCH=win32}}后,x86_64平台的Arch和i686平台的Arch完全相同。<br />
<br />
{{注意|如果在64位环境中执行{{ic|winetricks}}或其它程序出现问题,请试试创建一个新的32位{{ic|WINEPREFIX}}. 参见下面的[[#使用 WINEARCH]]}}<br />
<br />
== 配置 ==<br />
Wine默认将配置文件和安装的Windows程序保存在{{ic|~/.wine}}。这样的目录称为一个"Wine prefix"或"Wine bottle"(保留原文,下文称“系统目录”)。每次运行Windows程序(包括内置程序,如{{ic|winecfg}})时,系统目录会自动创建(如果缺失)或更新。系统目录中存放有相当于Windows下{{ic | C:\}}C盘(更确切的说应是系统盘)的文件夹。<br />
<br />
通过设置{{ic|WINEPREFIX}}环境变量,可以更改Wine系统目录的位置。如果希望让不同的Windows程序使用不同的系统环境或配置,这一变量会非常有用。<br />
<br />
例如,如果您使用 <br />
{{ic |1= $ env WINEPREFIX=~/.win-a wine-A程序.exe}}参数来运行一个程序。另一个使用<br />
{{ic |1= $ env WINEPREFIX=~/.win-b wine-B程序.exe}}参数,这两个程序将使用独立的C盘和注册表配置。<br />
<br />
以下命令会建立一个默认的系统目录,且不启动任何Windows程序:<br />
$ env WINEPREFIX=~/.customprefix wineboot -u<br />
<br />
配置Wine的方式通常有:<br />
* [http://wiki.winehq.org/winecfg winecfg]是Wine的图形界面配置程序。控制台下调用{{ic|$ winecfg}}(或指定系统目录:{{ic|1=$ WINEPREFIX=~/.系统目录 winecfg}})即可启动<br />
* [http://wiki.winehq.org/control control.exe]是Windows控制面板的Wine实现,通过{{ic|$ wine control}}命令启动<br />
* [http://wiki.winehq.org/regedit regedit]是Wine的注册表编辑器,比较前两者,该工具能配置更多东西。部分常用键值参见:[http://wiki.winehq.org/UsefulRegistryKeys WineHQ's article on Useful Registry Keys]<br />
<br />
=== 使用 WINEARCH ===<br />
对于64位用户,如果使用[multilib]仓库里的Wine,默认创建的系统目录是64位环境的。若想使用纯32位环境,修改{{ic|WINEARCH}} 变量win32为即可:<br />
{{ic |1=$ WINEARCH=win32 winecfg}}这样就会生成32位Wine环境。若不设置{{ic|WINEARCH}}得到的就是64位环境。<br />
<br />
通过{{ic|WINEPREFIX}}变量,在不同的系统目录分别创建32位和64位环境:<br />
$ WINEARCH=win32 WINEPREFIX=~/win32 winecfg <br />
$ WINEPREFIX=~/win64 winecfg<br />
<br />
{{注意|系统目录创建过程中,64位版本的wine将视全部目录如同64位系统目录,也将不会在已存在的目录中创建任何32位的.创建32位系统目录,您必须让Wine创建指定的{{ic|WINEPREFIX}}目录。}}<br />
<br />
winetricks也接受{{ic|WINEPREFIX}}变量,以安装Steam为例:<br />
env WINEARCH=win32 WINEPREFIX=~/.local/share/wineprefixes/steam winetricks steam<br />
{{小贴士|编辑 [[Bash (简体中文)#外壳和环境变量|~/.bashrc]],使得 WINEPREFIX 和 WINEARCH 永久生效。}}<br />
{{注意|不必手动在 {{ic|wineprefixes}} 文件夹下建立 steam 文件夹,Wine会自动创建不存在的系统目录。}}<br />
<br />
=== 显卡驱动 ===<br />
使用Wine运行Windows游戏时,可能需要高性能的显卡驱动。[[Nvidia (简体中文)|Nvidia]]、[[ATI (简体中文)|Amd/ATI]]用户最好使用闭源驱动。[[Intel (简体中文)|Intel]]显卡用户也可以选择开源驱动,它已经非常成熟。<br />
<br />
要是显卡驱动有问题或者相关配置有误,控制台用Wine运行某些程序时会输出:<br />
Direct rendering is disabled, most likely your OpenGL drivers have not been installed correctly<br />
<br />
x86-64用户需要从[[multilib]]或[[AUR (简体中文)|AUR]]安装额外的32位库:<br />
* '''NVIDIA''':{{Pkg|lib32-nvidia-libgl}} 至于老显卡,请到 AUR 搜索 '''lib32-nvidia-utils''' (如-173xx)。<br />
* '''NVIDIA (开源驱动)''':{{Pkg|lib32-nouveau-dri}}。<br />
* '''Intel''':{{Pkg|lib32-intel-dri}}。运行Wine时需要手动添加{{ic|1=LIBGL_DRIVERS_PATH=/usr/lib32/xorg/modules/dri}}<br />
* '''AMD/ATI''':{{Pkg|lib32-catalyst-utils}}。<br />
* '''AMD/ATI (开源驱动)''': {{Pkg|lib32-ati-dri}}。<br />
<br />
{{注意|安装上述软件包后,可能需要重启X才能生效!}}<br />
<br />
=== 声音 ===<br />
Wine程序有可能遇到某些声音问题。首先,确保{{ic|winecfg}}中只启用了一种声卡驱动。目前,Wine对[[Alsa (简体中文)|Alsa]]的支持最好。<br />
<br />
x86_64平台下使用[[Alsa (简体中文)|Alsa]]的话,需要安装{{Pkg|lib32-alsa-lib}}。如果还要使用PulseAudio,则需安装{{Pkg|lib32-libpulse}}。<br />
<br />
若使用[[OSS (简体中文)|OSS]],需要安装{{Pkg|lib32-alsa-oss}}。仅靠内核驱动是不行的。<br />
<br />
安装上述软件包后,若{{ic|winecfg}}'''仍'''无法识别声卡(Selected driver: (none)),请尝试[http://wiki.jswindle.com/index.php/Wine_Registry#Configuring_Sound registry 通过注册表配置]。<br />
<br />
运行使用某些高级声音系统的游戏,可能还需要安装{{Pkg|lib32-openal}}。<br />
<br />
=== 其他函数库 ===<br />
<br />
某些程序(如 Office 2003)需要解析HTML、XML(使用MSXML库),需要安装{{Pkg|lib32-libxml2}}。<br />
<br />
播放音频的程序可能依赖{{Pkg|lib32-mpg123}}。<br />
<br />
对于使用图像处理库的程序,可能依赖{{Pkg|lib32-giflib}}和{{Pkg|lib32-libpng}},<br />
<br />
x86_64的加密支持需要{{Pkg|lib32-gnutls}}软件包。<br />
<br />
=== 字体 ===<br />
<br />
如果没有安装微软Truetype字体,Wine程序的字体显示可能会一团糟,参见[[MS Fonts (简体中文)]]。如果还是不行,试试{{ic|winetricks allfonts}}。<br />
<br />
上述操作后,杀死wine相关进程再运行{{ic|winecfg}},字体应该变好看了。<br />
<br />
如果字体看起来很毛糙,试试用[http://wiki.winehq.org/regedit regedit]导入下列文本文件:<br />
[HKEY_CURRENT_USER\Software\Wine\X11 Driver]<br />
"ClientSideWithRender"="N"<br />
<br />
=== 启动器和菜单 ===<br />
<br />
Wine不会为内置程序(如{{ic|winecfg}}、{{ic|winebrowser}})创建桌面启动器和菜单项。但手动安装的Windows程序通常会自动创建启动器和菜单项。在Windows下,安装程序(如{{ic|setup.exe}})通常会在桌面和开始菜单建立快捷方式,而Wine下会创建遵循freedesktop.org规范的.desktop文件(即启动器,相当于快捷方式)。<br />
<br />
{{小贴士|如果启动器''没有''自动创建,或者这些文件丢失了,可以尝试使用[http://wiki.winehq.org/winemenubuilder winemenubuilder]修复。}}<br />
<br />
Ubuntu下,Wine项目以子菜单形式出现在系统菜单。以下步骤将实现这个效果:<br />
<br />
==== 创建菜单项 ====<br />
首先,用Wine安装一个Windows程序,以建立基本的菜单。完成后,向其中添加菜单项。桌面右键选择{{ic|"创建启动器..."}}(不同桌面环境操作有所差异),设置如下:<br />
'''类型(Type)''': 应用程序(Application)<br />
'''名称(Name)''': 配置<br />
'''命令(Command)''': winecfg<br />
'''备注(Comment)''': Wine配置工具<br />
<br />
'''类型''': 应用程序<br />
'''名称''': 卸载程序<br />
'''命令''': wine uninstaller<br />
'''备注''': 卸载Wine下的Windows程序<br />
<br />
'''类型''': 应用程序<br />
'''名称''': 浏览 C:\<br />
'''命令''': wine winebrowser c:\\<br />
'''备注''': 浏览Wine中虚拟的C盘<br />
现在,桌面上出现了三个启动器,下面将把它们移入菜单。不过首先,我们给这些启动器加上动态图标(由图标主题提供)。方法是,用文本编辑器打开启动器,编辑Icon项目:<br />
<br />
{{ic|配置}} 启动器:<br />
Icon=wine-winecfg<br />
{{ic|卸载程序}} 启动器:<br />
Icon=wine-uninstaller<br />
{{ic|浏览 C:\}} 启动器:<br />
Icon=wine-winefile<br />
{{小贴士|多数桌面环境在上述“创建启动器”步骤即可设置图标。以第一个启动器为例,在选择图标窗口中搜索wine-winecfg,选择即可,无需手动编辑。 ——译者注}}<br />
如果图标无法显示或者你觉得很丑陋,换成其他图标也可以。右键设置启动器,应该有更改图标的地方。很多图标主题,例如[http://www.gnome-look.org/content/show.php/GNOME-colors?content=82562 GNOME-colors],都提供这些图标。<br />
<br />
现在,将启动器移入菜单。把启动器复制到 {{ic|~/.local/share/applications/wine/}} 目录即可。<br />
<br />
诶?图标还没出现在菜单中!还剩下最后一步,创建下列文本文件:<br />
{{hc|~/.config/menus/applications-merged/wine-utilities.menu|<nowiki><br />
<!DOCTYPE Menu PUBLIC "-//freedesktop//DTD Menu 1.0//EN"<br />
"http://www.freedesktop.org/standards/menu-spec/menu-1.0.dtd"><br />
<Menu><br />
<Name>Applications</Name><br />
<Menu><br />
<Name>wine-wine</Name><br />
<Directory>wine-wine.directory</Directory><br />
<Include><br />
<Filename>wine-Configuration.desktop</Filename><br />
</Include><br />
<Include><br />
<Filename>wine-Browse C:\.desktop</Filename><br />
</Include><br />
<Include><br />
<Filename>wine-Uninstall Programs.desktop</Filename><br />
</Include><br />
</Menu><br />
</Menu><br />
</nowiki>}}<br />
再看看菜单,应该万事大吉了。<br />
<br />
==== Gnome3 中清理 Wine 菜单启动项 ====<br />
系统全局的菜单启动器安装在 {{ic|/usr/share/applications/}},清除相应程序的“.desktop”文件即可从整个系统删除该启动器。<br />
<br />
如果这样还是无法解决问题,那么很可能 Wine 的启动器存放在用户级别的 {{ic|~/.local/share/applications/wine/Programs/}} 目录中。删除相应的“.desktop”文件即可清理对应启动项。删除整个 Programs 文件夹将清理所有 Wine 程序的启动项。<br />
<br />
==== 修复 KDE 4 菜单问题 ====<br />
Wine菜单项有可能[https://bugs.launchpad.net/ubuntu/+source/wine/+bug/263041 错误地出现]在{{ic|"Lost & Found(其他)"}} ,而非Wine子菜单。原因是kde-applications.menu文件缺失{{ic|MergeDir}}配置。<br />
<br />
编辑{{ic|/etc/xdg/menus/kde-applications.menu}}。<br />
<br />
在文件末尾处,{{ic|<DefaultMergeDirs/>}}后添加{{ic|<MergeDir>applications-merged</MergeDir>}}。修改后内容大致如下:<br />
<Menu><br />
<Include><br />
<And><br />
<Category>KDE</Category><br />
<Category>Core</Category><br />
</And><br />
</Include><br />
<DefaultMergeDirs/><br />
'''<MergeDir>applications-merged</MergeDir>'''<br />
<MergeFile>applications-kmenuedit.menu</MergeFile><br />
</Menu><br />
<br />
另一个方法是:<br />
ln -s ~/.config/menus/applications-merged ~/.config/menus/kde-applications-merged<br />
<br />
这样的好处是,不会因为KDE升级而重置配置。但该方法只对一个用户有效。<br />
<br />
== 运行 Windows 程序 ==<br />
{{警告|千万不要以root身份运行Wine!详情参见[http://wiki.winehq.org/FAQ#run_as_root 本文]。}}<br />
运行Windows程序:<br />
$ wine <exe文件><br />
<br />
内置的msiexec程序可以运行MSI安装包:<br />
$ msiexec installername.msi<br />
== 技巧 ==<br />
{{小贴士|此外您可能会感兴趣以下文章的开始所提供的链接<br />
* [http://appdb.winehq.org/ Wine程序数据库 (Wine Application Database, AppDB)] —— 特定Windows程序的Wine兼容情况(运行时的已知问题、用户评分、指南等等)<br />
* [http://forum.winehq.org/ WineHQ论坛] —— 要是看完上述网页还有问题,可以到这里咨询<br />
}}<br />
<br />
这里介绍一些安装Windows组件的工具。由于这些工具可能严重破坏Wine配置,没有需要时最好不要使用。<br />
<br />
=== 安装 Microsoft Office ===<br />
<br />
更新(2013年4月9日):对于 Wine 1.5.27,下面所述的步骤已经不必要了。先安装 winbind(包含在 {{pkg|samba}} 中),然后执行: <br />
<br />
$ export WINEPREFIX="<用户家目录中的某一可写目录>" <br />
$ export WINEARCH="win32"<br />
$ wine /到/office安装盘/的路径/setup.exe<br />
<br />
可以把上述 export 语句加入 bashrc 文件。<br />
<br />
安装结束后,打开 Word 或 Excel,联网激活。完成后,关闭程序,执行 '''winecfg''',在“函数库”选项卡中把 riched20 设置为“Native (Windows)”。这样 PowerPoint 就可以正常工作。<br />
(使用 Office Home/Student 2010 和 wine 1.5.27 测试。在线激活有效)<br />
<br />
安装Office套装前,需要先安装某些Windows组件:<br />
<br />
$ WINEARCH=win32 WINEPREFIX=/path/to/wineprefix winecfg<br />
# pacman -S winetricks<br />
$ winetricks msxml3 # For MS Office 2007<br />
$ winetricks msxml3 msxml6 # For MS Office 2010<br />
$ wine /path/to/office_cd/setup.exe<br />
<br />
更多信息,参见[http://appdb.winehq.org/appview.php?iVersionId=4992 WineHQ上的文章]。<br />
<br />
{{注意|{{Pkg|playonlinux}} 提供了一个自定义安装脚本,简化了 Office 2003、2007 和 2010 的安装。您只需提供 setup.exe 或 ISO 文件,这个脚本就可以指导您完成安装,完全不需要自己设置 Wine。}}<br />
<br />
=== OpenGL 模式 ===<br />
很多游戏(比如魔兽争霸啦)都支持OpenGL模式,在Wine下''可能''比默认DirectX模式性能更好。一般添加{{Ic|-opengl}}启动程序即可,但''不同程序可能有所不同'':<br />
$ wine /path/to/3d_game.exe -opengl<br />
<br />
请参考[http://appdb.winehq.org AppDB],了解特定程序的相关信息。<br />
<br />
=== 将 Wine 作为 Win16/Win32 程序的解释器 ===<br />
可以通知内核把Wine作为Win16/Win32程序的解释器。<br />
<br />
设置过程在 [[systemd (简体中文)|systemd]] 和 [[initscripts (简体中文)|initscripts]] 下有所差异。<br />
<br />
==== systemd ====<br />
通知内核识别和执行 Win16/Win32 程序的方式:<br />
echo ':DOSWin:M::MZ::/usr/bin/wine:' > /proc/sys/fs/binfmt_misc/register<br />
To make the setting permanent, create a configuration file in {{ic|/etc/tmpfiles.d}} with the following contents:<br />
测试效果,若一切正常,可以使该设置永久生效。在 {{ic|/etc/tmpfiles.d}} 目录创建新的配置文件,内容为:<br />
{{hc|/etc/tmpfiles.d/enable-doswin-exe.conf|<br />
w /proc/sys/fs/binfmt_misc/register - - - - :DOSWin:M::MZ::/usr/bin/wine:}}<br />
<br />
说明一下,和 initscripts 不同,systemd 会自动挂载 {{ic|/proc/sys/fs/binfmt_misc}},所以只需要通过临时文件机制向内核写入配置即可。<br />
<br />
更多信息,参见 [[Systemd (简体中文)#临时文件]]。<br />
<br />
==== initscripts ====<br />
首先,挂载 {{ic|binfmt_misc}}文件系统:<br />
# mount -t binfmt_misc none /proc/sys/fs/binfmt_misc<br />
或者,如果你想一直这么做,将以下内容加入{{ic|/etc/fstab}}:<br />
none /proc/sys/fs/binfmt_misc binfmt_misc defaults 0 0<br />
然后,告知内核如何解释(运行)Win16/Win32程序:<br />
echo ':DOSWin:M::MZ::/usr/bin/wine:' > /proc/sys/fs/binfmt_misc/register<br />
如果你想一直这么做,添加上述内容到{{ic|/etc/rc.local}}即可。最后的重定向是为了避免切换启动级别时的错误信息:<br />
{ echo ':DOSWin:M::MZ::/usr/bin/wine:' > /proc/sys/fs/binfmt_misc/register; } 2>/dev/null<br />
<br />
==== 效果测试 ====<br />
现在,直接运行Windows程序试试:<br />
chmod 755 exefile.exe<br />
./exefile.exe<br />
<br />
=== Wine 控制台 ===<br />
<br />
有些时候,可能需要运行{{ic|.exe}}给游戏打补丁,比如给古董游戏添加宽屏支持。这时直接通过Wine运行可能没有用。那么,打开终端,运行一下命令:<br />
<br />
$ wineconsole cmd<br />
<br />
将进入一个和Windows下cmd一样的命令行环境。在该环境下试试也许就可以了。<br />
<br />
=== Winetricks ===<br />
使用[http://wiki.winehq.org/winetricks Winetricks]快速脚本,能够方便地安装许多Windows组件,包括DirectX、msxml(被Office 2007、IE浏览器依赖)visual运行库还有其他更多的。<br />
<br />
您可以使用[[pacman (简体中文)|pacman]]或者从[[AUR (简体中文)|AUR]]上获取{{AUR|winetricks-svn}}软件包来安装该工具:<br />
<br />
运行:<br />
<br />
$ winetricks<br />
<br />
== 第三方工具 ==<br />
这些程序有其自己的主页和支持论坛。<br />
<br />
=== CrossOver ===<br />
<br />
[http://www.codeweavers.com/about/ CrossOver] 有单独的[[CrossOver|wiki 页面]].<br />
<br />
=== PlayOnLinux ===<br />
[http://www.playonlinux.com/ PlayOnLinux]是一个图形界面的Windows/DOS程序管理器。它提供了一些帮助配置/运行程序的脚本,能够管理多个不同版本的Wine,甚至能对不同程序使用不同Wine版本。参考[http://appdb.winehq.org AppDB],看看哪个Wine版本对你要运行的程序兼容最好。从[[AUR (简体中文)|AUR]]安装{{AUR|playonlinux}}。<br />
<br />
=== PyWinery ===<br />
[http://code.google.com/p/pywinery/ PyWinery]是一个简单的、图形界面的Wine系统目录管理器,用它可以方便地管理不同系统目录,并从不同系统目录运行程序。同时可以开启winetricks在同一系统目录,打开系统目录所在文件夹, {{ic|winecfg}}, 软件卸载程序和wineDOS。[[AUR (简体中文)|AUR]]中提供了软件包{{AUR|pywinery}}。当你使用很多系统目录(一个打游戏用、一个编程用……)时,这个程序会非常有用。<br />
<br />
它在默认情况下使用winetricks打开{{ic|.exe}}文件,所以你可以选择你有的任何Wine的配置。<br />
<br />
=== Q4wine ===<br />
[http://q4wine.brezblock.org.ua/ Q4Wine] 是一个图形界面的系统目录(wine-prefix)管理器。它的特色是可以把 QT 主题导入 Wine 配置,使两者完美整合。{{Pkg|q4wine}} 软件包在 <nowiki>[</nowiki>[[multilib]]] 仓库中提供。<br />
<br />
== 相关链接 ==<br />
* [http://www.winehq.com/ Wine官方网站]<br />
* [http://appdb.winehq.org/ Wine程序数据库]<br />
* [http://linuxgamingtoday.wordpress.com/2008/02/16/quick-tips-to-speed-up-your-gaming-in-wine/ 加速Wine,显卡及OpenGL高级配置]<br />
* [http://wiki.gotux.net/code:perl:fileinfo FileInfo] —— Find Win32 PE/COFF headers in EXE/DLL/OCX files under linux/unix environment</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Wine_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=263375
Wine (简体中文)
2013-06-19T02:57:14Z
<p>Cuihao: </p>
<hr />
<div>[[Category:Wine (简体中文)]]<br />
[[cs:Wine]]<br />
[[de:Wine]]<br />
[[en:Wine]]<br />
[[es:Wine]]<br />
[[fr:Wine]]<br />
[[it:Wine]]<br />
[[ja:Wine]]<br />
[[ru:Wine]]<br />
[[zh-TW:Wine]]<br />
{{TranslationStatus (简体中文)|Wine|2013-06-19|263008}}<br />
{{Article summary start}}<br />
{{Article summary text|[[Wikipedia:Wine (software)|Wine]] 是类UNIX系统下运行微软Windows程序的"兼容层"。在Wine中运行的Windows程序,就如同运行原生Linux程序一样,不会有模拟器那样的性能问题。}}<br />
{{Article summary heading|Related}}<br />
{{Article summary wiki|Steam}}<br />
{{Article summary wiki|CrossOver}}<br />
{{Article summary end}}<br />
<br />
获取更详细的介绍请浏览[http://www.winehq.org/ 项目官方网站]和[http://wiki.winehq.org/ wiki]页面。<br />
<br />
== 安装 ==<br />
{{警告|如果您的账户能浏览某些文件或资源,Wine运行的程序也可以。Wine不是[[wikipedia:Sandbox (computer security)|沙箱]]。如果很重视安全,请考虑使用[[wikipedia:Virtualization|虚拟化]]。}}<br />
<br />
通过 [[pacman (简体中文)|pacman]] 从 [[official repositories (简体中文)|官方仓库]]安装软件包 {{Pkg|wine}} 即可获取 Wine 模拟器。 对于64位系统,需要启用 [[Multilib]] 仓库。<br />
<br />
另外,您可能需要安装 {{pkg|wine_gecko}} 和 {{pkg|wine-mono}} 软件包。它们分别用于运行依赖于 Internet Explorer 和 .NET 的程序。不过,也可以随后通过 Wine 在需要时下载安装这些组件。但如果提前下载安装,您就可以离线使用它们,而且 Wine 不必为了每一个 WINEPREFIX 都单独下载。<br />
<br />
'''平台差异'''<br />
<br />
默认的Wine是32位的程序,也是i686的Arch软件包。所以它不能运行64位的Windows程序(反正是罕见的)。<br />
<br />
然而,x86_64的Wine软件包目前以{{ic| --enable-win64}}方式编译。这个参数激活了[[Wikipedia:WoW64|WoW64]]的Wine版本。<br />
<br />
*在Windows中,这个复杂的子系统允许用户同时使用32位和64位的Windows程序,甚至是在同一目录。<br />
<br />
*在Wine中,用户将必须建立单独分开的目录/前缀。这项Wine功能仍是试验阶段,并建议用户使用一个win32{{ic|WINEPREFIX}}。浏览[http://wiki.winehq.org/Wine64 Wine64]以获取有关这个的详细信息。<br />
<br />
总结一下,配置{{ic|1=WINEARCH=win32}}后,x86_64平台的Arch和i686平台的Arch完全相同。<br />
<br />
{{注意|如果在64位环境中执行{{ic|winetricks}}或其它程序出现问题,请试试创建一个新的32位{{ic|WINEPREFIX}}. 参见下面的[[#使用 WINEARCH]]}}<br />
<br />
== 配置 ==<br />
Wine默认将配置文件和安装的Windows程序保存在{{ic|~/.wine}}。这样的目录称为一个"Wine prefix"或"Wine bottle"(保留原文,下文称“系统目录”)。每次运行Windows程序(包括内置程序,如{{ic|winecfg}})时,系统目录会自动创建(如果缺失)或更新。系统目录中存放有相当于Windows下{{ic | C:\}}C盘(更确切的说应是系统盘)的文件夹。<br />
<br />
通过设置{{ic|WINEPREFIX}}环境变量,可以更改Wine系统目录的位置。如果希望让不同的Windows程序使用不同的系统环境或配置,这一变量会非常有用。<br />
<br />
例如,如果您使用 <br />
{{ic |1= $ env WINEPREFIX=~/.win-a wine-A程序.exe}}参数来运行一个程序。另一个使用<br />
{{ic |1= $ env WINEPREFIX=~/.win-b wine-B程序.exe}}参数,这两个程序将使用独立的C盘和注册表配置。<br />
<br />
以下命令会建立一个默认的系统目录,且不启动任何Windows程序:<br />
$ env WINEPREFIX=~/.customprefix wineboot -u<br />
<br />
配置Wine的方式通常有:<br />
* [http://wiki.winehq.org/winecfg winecfg]是Wine的图形界面配置程序。控制台下调用{{ic|$ winecfg}}(或指定系统目录:{{ic|1=$ WINEPREFIX=~/.系统目录 winecfg}})即可启动<br />
* [http://wiki.winehq.org/control control.exe]是Windows控制面板的Wine实现,通过{{ic|$ wine control}}命令启动<br />
* [http://wiki.winehq.org/regedit regedit]是Wine的注册表编辑器,比较前两者,该工具能配置更多东西。部分常用键值参见:[http://wiki.winehq.org/UsefulRegistryKeys WineHQ's article on Useful Registry Keys]<br />
<br />
=== 使用 WINEARCH ===<br />
对于64位用户,如果使用[multilib]仓库里的Wine,默认创建的系统目录是64位环境的。若想使用纯32位环境,修改{{ic|WINEARCH}} 变量win32为即可:<br />
{{ic |1=$ WINEARCH=win32 winecfg}}这样就会生成32位Wine环境。若不设置{{ic|WINEARCH}}得到的就是64位环境。<br />
<br />
通过{{ic|WINEPREFIX}}变量,在不同的系统目录分别创建32位和64位环境:<br />
$ WINEARCH=win32 WINEPREFIX=~/win32 winecfg <br />
$ WINEPREFIX=~/win64 winecfg<br />
<br />
{{注意|系统目录创建过程中,64位版本的wine将视全部目录如同64位系统目录,也将不会在已存在的目录中创建任何32位的.创建32位系统目录,您必须让Wine创建指定的{{ic|WINEPREFIX}}目录。}}<br />
<br />
winetricks也接受{{ic|WINEPREFIX}}变量,以安装Steam为例:<br />
env WINEARCH=win32 WINEPREFIX=~/.local/share/wineprefixes/steam winetricks steam<br />
{{小贴士|编辑 [[Bash (简体中文)#外壳和环境变量|~/.bashrc]],使得 WINEPREFIX 和 WINEARCH 永久生效。}}<br />
{{注意|不必手动在 {{ic|wineprefixes}} 文件夹下建立 steam 文件夹,Wine会自动创建不存在的系统目录。}}<br />
<br />
=== 显卡驱动 ===<br />
使用Wine运行Windows游戏时,可能需要高性能的显卡驱动。[[Nvidia (简体中文)|Nvidia]]、[[ATI (简体中文)|Amd/ATI]]用户最好使用闭源驱动。[[Intel (简体中文)|Intel]]显卡用户也可以选择开源驱动,它已经非常成熟。<br />
<br />
要是显卡驱动有问题或者相关配置有误,控制台用Wine运行某些程序时会输出:<br />
Direct rendering is disabled, most likely your OpenGL drivers have not been installed correctly<br />
<br />
x86-64用户需要从[[multilib]]或[[AUR (简体中文)|AUR]]安装额外的32位库:<br />
* '''NVIDIA''':{{Pkg|lib32-nvidia-libgl}} 至于老显卡,请到 AUR 搜索 '''lib32-nvidia-utils''' (如-173xx)。<br />
* '''NVIDIA (开源驱动)''':{{Pkg|lib32-nouveau-dri}}。<br />
* '''Intel''':{{Pkg|lib32-intel-dri}}。运行Wine时需要手动添加{{ic|1=LIBGL_DRIVERS_PATH=/usr/lib32/xorg/modules/dri}}<br />
* '''AMD/ATI''':{{Pkg|lib32-catalyst-utils}}。<br />
* '''AMD/ATI (开源驱动)''': {{Pkg|lib32-ati-dri}}。<br />
<br />
{{注意|安装上述软件包后,可能需要重启X才能生效!}}<br />
<br />
=== 声音 ===<br />
Wine程序有可能遇到某些声音问题。首先,确保{{ic|winecfg}}中只启用了一种声卡驱动。目前,Wine对[[Alsa (简体中文)|Alsa]]的支持最好。<br />
<br />
x86_64平台下使用[[Alsa (简体中文)|Alsa]]的话,需要安装{{Pkg|lib32-alsa-lib}}。如果还要使用PulseAudio,则需安装{{Pkg|lib32-libpulse}}。<br />
<br />
若使用[[OSS (简体中文)|OSS]],需要安装{{Pkg|lib32-alsa-oss}}。仅靠内核驱动是不行的。<br />
<br />
安装上述软件包后,若{{ic|winecfg}}'''仍'''无法识别声卡(Selected driver: (none)),请尝试[http://wiki.jswindle.com/index.php/Wine_Registry#Configuring_Sound registry 通过注册表配置]。<br />
<br />
运行使用某些高级声音系统的游戏,可能还需要安装{{Pkg|lib32-openal}}。<br />
<br />
=== 其他函数库 ===<br />
<br />
某些程序(如 Office 2003)需要解析HTML、XML(使用MSXML库),需要安装{{Pkg|lib32-libxml2}}。<br />
<br />
播放音频的程序可能依赖{{Pkg|lib32-mpg123}}。<br />
<br />
对于使用图像处理库的程序,可能依赖{{Pkg|lib32-giflib}}和{{Pkg|lib32-libpng}},<br />
<br />
x86_64的加密支持需要{{Pkg|lib32-gnutls}}软件包。<br />
<br />
=== 字体 ===<br />
<br />
如果没有安装微软Truetype字体,Wine程序的字体显示可能会一团糟,参见[[MS Fonts (简体中文)]]。如果还是不行,试试{{ic|winetricks allfonts}}。<br />
<br />
上述操作后,杀死wine相关进程再运行{{ic|winecfg}},字体应该变好看了。<br />
<br />
如果字体看起来很毛糙,试试用[http://wiki.winehq.org/regedit regedit]导入下列文本文件:<br />
[HKEY_CURRENT_USER\Software\Wine\X11 Driver]<br />
"ClientSideWithRender"="N"<br />
<br />
=== 启动器和菜单 ===<br />
<br />
Wine不会为内置程序(如{{ic|winecfg}}、{{ic|winebrowser}})创建桌面启动器和菜单项。但手动安装的Windows程序通常会自动创建启动器和菜单项。在Windows下,安装程序(如{{ic|setup.exe}})通常会在桌面和开始菜单建立快捷方式,而Wine下会创建遵循freedesktop.org规范的.desktop文件(即启动器,相当于快捷方式)。<br />
<br />
{{小贴士|如果启动器''没有''自动创建,或者这些文件丢失了,可以尝试使用[http://wiki.winehq.org/winemenubuilder winemenubuilder]修复。}}<br />
<br />
Ubuntu下,Wine项目以子菜单形式出现在系统菜单。以下步骤将实现这个效果:<br />
<br />
==== 创建菜单项 ====<br />
首先,用Wine安装一个Windows程序,以建立基本的菜单。完成后,向其中添加菜单项。桌面右键选择{{ic|"创建启动器..."}}(不同桌面环境操作有所差异),设置如下:<br />
'''类型(Type)''': 应用程序(Application)<br />
'''名称(Name)''': 配置<br />
'''命令(Command)''': winecfg<br />
'''备注(Comment)''': Wine配置工具<br />
<br />
'''类型''': 应用程序<br />
'''名称''': 卸载程序<br />
'''命令''': wine uninstaller<br />
'''备注''': 卸载Wine下的Windows程序<br />
<br />
'''类型''': 应用程序<br />
'''名称''': 浏览 C:\<br />
'''命令''': wine winebrowser c:\\<br />
'''备注''': 浏览Wine中虚拟的C盘<br />
现在,桌面上出现了三个启动器,下面将把它们移入菜单。不过首先,我们给这些启动器加上动态图标(由图标主题提供)。方法是,用文本编辑器打开启动器,编辑Icon项目:<br />
<br />
{{ic|配置}} 启动器:<br />
Icon=wine-winecfg<br />
{{ic|卸载程序}} 启动器:<br />
Icon=wine-uninstaller<br />
{{ic|浏览 C:\}} 启动器:<br />
Icon=wine-winefile<br />
{{小贴士|多数桌面环境在上述“创建启动器”步骤即可设置图标。以第一个启动器为例,在选择图标窗口中搜索wine-winecfg,选择即可,无需手动编辑。 ——译者注}}<br />
如果图标无法显示或者你觉得很丑陋,换成其他图标也可以。右键设置启动器,应该有更改图标的地方。很多图标主题,例如[http://www.gnome-look.org/content/show.php/GNOME-colors?content=82562 GNOME-colors],都提供这些图标。<br />
<br />
现在,将启动器移入菜单。把启动器复制到 {{ic|~/.local/share/applications/wine/}} 目录即可。<br />
<br />
诶?图标还没出现在菜单中!还剩下最后一步,创建下列文本文件:<br />
{{hc|~/.config/menus/applications-merged/wine-utilities.menu|<nowiki><br />
<!DOCTYPE Menu PUBLIC "-//freedesktop//DTD Menu 1.0//EN"<br />
"http://www.freedesktop.org/standards/menu-spec/menu-1.0.dtd"><br />
<Menu><br />
<Name>Applications</Name><br />
<Menu><br />
<Name>wine-wine</Name><br />
<Directory>wine-wine.directory</Directory><br />
<Include><br />
<Filename>wine-Configuration.desktop</Filename><br />
</Include><br />
<Include><br />
<Filename>wine-Browse C:\.desktop</Filename><br />
</Include><br />
<Include><br />
<Filename>wine-Uninstall Programs.desktop</Filename><br />
</Include><br />
</Menu><br />
</Menu><br />
</nowiki>}}<br />
再看看菜单,应该万事大吉了。<br />
<br />
==== Gnome3 中清理 Wine 菜单启动项 ====<br />
系统全局的菜单启动器安装在 {{ic|/usr/share/applications/}},清除相应程序的“.desktop”文件即可从整个系统删除该启动器。<br />
<br />
如果这样还是无法解决问题,那么很可能 Wine 的启动器存放在用户级别的 {{ic|~/.local/share/applications/wine/Programs/}} 目录中。删除相应的“.desktop”文件即可清理对应启动项。删除整个 Programs 文件夹将清理所有 Wine 程序的启动项。<br />
<br />
==== 修复 KDE 4 菜单问题 ====<br />
Wine菜单项有可能[https://bugs.launchpad.net/ubuntu/+source/wine/+bug/263041 错误地出现]在{{ic|"Lost & Found(其他)"}} ,而非Wine子菜单。原因是kde-applications.menu文件缺失{{ic|MergeDir}}配置。<br />
<br />
编辑{{ic|/etc/xdg/menus/kde-applications.menu}}。<br />
<br />
在文件末尾处,{{ic|<DefaultMergeDirs/>}}后添加{{ic|<MergeDir>applications-merged</MergeDir>}}。修改后内容大致如下:<br />
<Menu><br />
<Include><br />
<And><br />
<Category>KDE</Category><br />
<Category>Core</Category><br />
</And><br />
</Include><br />
<DefaultMergeDirs/><br />
'''<MergeDir>applications-merged</MergeDir>'''<br />
<MergeFile>applications-kmenuedit.menu</MergeFile><br />
</Menu><br />
<br />
另一个方法是:<br />
ln -s ~/.config/menus/applications-merged ~/.config/menus/kde-applications-merged<br />
<br />
这样的好处是,不会因为KDE升级而重置配置。但该方法只对一个用户有效。<br />
<br />
== 运行 Windows 程序 ==<br />
{{警告|千万不要以root身份运行Wine!详情参见[http://wiki.winehq.org/FAQ#run_as_root 本文]。}}<br />
运行Windows程序:<br />
$ wine <exe文件><br />
<br />
内置的msiexec程序可以运行MSI安装包:<br />
$ msiexec installername.msi<br />
== 技巧 ==<br />
{{小贴士|此外您可能会感兴趣以下文章的开始所提供的链接<br />
* [http://appdb.winehq.org/ Wine程序数据库 (Wine Application Database, AppDB)] —— 特定Windows程序的Wine兼容情况(运行时的已知问题、用户评分、指南等等)<br />
* [http://forum.winehq.org/ WineHQ论坛] —— 要是看完上述网页还有问题,可以到这里咨询<br />
}}<br />
<br />
这里介绍一些安装Windows组件的工具。由于这些工具可能严重破坏Wine配置,没有需要时最好不要使用。<br />
<br />
=== 安装 Microsoft Office ===<br />
<br />
更新(2013年4月9日):对于 Wine 1.5.27,下面所述的步骤已经不必要了。先安装 winbind(包含在 {{pkg|samba}} 中),然后执行: <br />
<br />
$ export WINEPREFIX="<用户家目录中的某一可写目录>" <br />
$ export WINEARCH="win32"<br />
$ wine /到/office安装盘/的路径/setup.exe<br />
<br />
可以把上述 export 语句加入 bashrc 文件。<br />
<br />
安装结束后,打开 Word 或 Excel,联网激活。完成后,关闭程序,执行 '''winecfg''',在“函数库”选项卡中把 riched20 设置为“Native (Windows)”。这样 PowerPoint 就可以正常工作。<br />
(使用 Office Home/Student 2010 和 wine 1.5.27 测试。在线激活有效)<br />
<br />
安装Office套装前,需要先安装某些Windows组件:<br />
<br />
$ WINEARCH=win32 WINEPREFIX=/path/to/wineprefix winecfg<br />
# pacman -S winetricks<br />
$ winetricks msxml3 # For MS Office 2007<br />
$ winetricks msxml3 msxml6 # For MS Office 2010<br />
$ wine /path/to/office_cd/setup.exe<br />
<br />
更多信息,参见[http://appdb.winehq.org/appview.php?iVersionId=4992 WineHQ上的文章]。<br />
<br />
{{注意|{{Pkg|playonlinux}} 提供了一个自定义安装脚本,简化了 Office 2003、2007 和 2010 的安装。您只需提供 setup.exe 或 ISO 文件,这个脚本就可以指导您完成安装,完全不需要自己设置 Wine。}}<br />
<br />
=== OpenGL 模式 ===<br />
很多游戏(比如魔兽争霸啦)都支持OpenGL模式,在Wine下''可能''比默认DirectX模式性能更好。一般添加{{Ic|-opengl}}启动程序即可,但''不同程序可能有所不同'':<br />
$ wine /path/to/3d_game.exe -opengl<br />
<br />
请参考[http://appdb.winehq.org AppDB],了解特定程序的相关信息。<br />
<br />
=== 将 Wine 作为 Win16/Win32 程序的解释器 ===<br />
可以通知内核把Wine作为Win16/Win32程序的解释器。<br />
<br />
设置过程在 [[systemd (简体中文)|systemd]] 和 [[initscripts (简体中文)|initscripts]] 下有所差异。<br />
<br />
==== systemd ====<br />
通知内核识别和执行 Win16/Win32 程序的方式:<br />
echo ':DOSWin:M::MZ::/usr/bin/wine:' > /proc/sys/fs/binfmt_misc/register<br />
To make the setting permanent, create a configuration file in {{ic|/etc/tmpfiles.d}} with the following contents:<br />
测试效果,若一切正常,可以使该设置永久生效。在 {{ic|/etc/tmpfiles.d}} 目录创建新的配置文件,内容为:<br />
{{hc|/etc/tmpfiles.d/enable-doswin-exe.conf|<br />
w /proc/sys/fs/binfmt_misc/register - - - - :DOSWin:M::MZ::/usr/bin/wine:}}<br />
<br />
说明一下,和 initscripts 不同,systemd 会自动挂载 {{ic|/proc/sys/fs/binfmt_misc}},所以只需要通过临时文件机制向内核写入配置即可。<br />
<br />
更多信息,参见 [[Systemd (简体中文)#临时文件]]。<br />
<br />
==== initscripts ====<br />
首先,挂载 {{ic|binfmt_misc}}文件系统:<br />
# mount -t binfmt_misc none /proc/sys/fs/binfmt_misc<br />
或者,如果你想一直这么做,将以下内容加入{{ic|/etc/fstab}}:<br />
none /proc/sys/fs/binfmt_misc binfmt_misc defaults 0 0<br />
然后,告知内核如何解释(运行)Win16/Win32程序:<br />
echo ':DOSWin:M::MZ::/usr/bin/wine:' > /proc/sys/fs/binfmt_misc/register<br />
如果你想一直这么做,添加上述内容到{{ic|/etc/rc.local}}即可。最后的重定向是为了避免切换启动级别时的错误信息:<br />
{ echo ':DOSWin:M::MZ::/usr/bin/wine:' > /proc/sys/fs/binfmt_misc/register; } 2>/dev/null<br />
<br />
==== 效果测试 ====<br />
现在,直接运行Windows程序试试:<br />
chmod 755 exefile.exe<br />
./exefile.exe<br />
<br />
=== Wine 控制台 ===<br />
<br />
有些时候,可能需要运行{{ic|.exe}}给游戏打补丁,比如给古董游戏添加宽屏支持。这时直接通过Wine运行可能没有用。那么,打开终端,运行一下命令:<br />
<br />
$ wineconsole cmd<br />
<br />
将进入一个和Windows下cmd一样的命令行环境。在该环境下试试也许就可以了。<br />
<br />
=== Winetricks ===<br />
使用[http://wiki.winehq.org/winetricks Winetricks]快速脚本,能够方便地安装许多Windows组件,包括DirectX、msxml(被Office 2007、IE浏览器依赖)visual运行库还有其他更多的。<br />
<br />
您可以使用[[pacman (简体中文)|pacman]]或者从[[AUR (简体中文)|AUR]]上获取{{AUR|winetricks-svn}}软件包来安装该工具:<br />
<br />
运行:<br />
<br />
$ winetricks<br />
<br />
== 第三方工具 ==<br />
这些程序有其自己的主页和支持论坛。<br />
<br />
=== CrossOver ===<br />
<br />
[http://www.codeweavers.com/about/ CrossOver] 有单独的[[CrossOver|wiki 页面]].<br />
<br />
=== PlayOnLinux ===<br />
[http://www.playonlinux.com/ PlayOnLinux]是一个图形界面的Windows/DOS程序管理器。它提供了一些帮助配置/运行程序的脚本,能够管理多个不同版本的Wine,甚至能对不同程序使用不同Wine版本。参考[http://appdb.winehq.org AppDB],看看哪个Wine版本对你要运行的程序兼容最好。从[[AUR (简体中文)|AUR]]安装{{AUR|playonlinux}}。<br />
<br />
=== PyWinery ===<br />
[http://code.google.com/p/pywinery/ PyWinery]是一个简单的、图形界面的Wine系统目录管理器,用它可以方便地管理不同系统目录,并从不同系统目录运行程序。同时可以开启winetricks在同一系统目录,打开系统目录所在文件夹, {{ic|winecfg}}, 软件卸载程序和wineDOS。[[AUR (简体中文)|AUR]]中提供了软件包{{AUR|pywinery}}。当你使用很多系统目录(一个打游戏用、一个编程用……)时,这个程序会非常有用。<br />
<br />
它在默认情况下使用winetricks打开{{ic|.exe}}文件,所以你可以选择你有的任何Wine的配置。<br />
<br />
=== Q4wine ===<br />
[http://q4wine.brezblock.org.ua/ Q4Wine] 是一个图形界面的系统目录(wine-prefix)管理器。它的特色是可以把 QT 主题导入 Wine 配置,使两者完美整合。{{Pkg|q4wine}} 软件包在 <nowiki>[</nowiki>[[multilib]]] 仓库中提供。<br />
<br />
== 相关链接 ==<br />
* [http://www.winehq.com/ Wine官方网站]<br />
* [http://appdb.winehq.org/ Wine程序数据库]<br />
* [http://linuxgamingtoday.wordpress.com/2008/02/16/quick-tips-to-speed-up-your-gaming-in-wine/ 加速Wine,显卡及OpenGL高级配置]<br />
* [http://wiki.gotux.net/code:perl:fileinfo FileInfo] —— Find Win32 PE/COFF headers in EXE/DLL/OCX files under linux/unix environment</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Systemd_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=262854
Systemd (简体中文)
2013-06-15T11:30:18Z
<p>Cuihao: /* 启动方式 */</p>
<hr />
<div>{{Lowercase title}}<br />
[[Category:Daemons and system services (简体中文)]]<br />
[[Category:Boot process (简体中文)]]<br />
[[ar:Systemd]]<br />
[[en:Systemd]]<br />
[[es:Systemd]]<br />
[[fr:Systemd]]<br />
[[it:Systemd]]<br />
[[ja:Systemd]]<br />
[[ru:Systemd]]<br />
[[zh-TW:Systemd]]<br />
{{Article summary start|概览}}<br />
{{Article summary text|本文介绍 Systemd 的安装和配置。}}<br />
{{Article summary heading|相关页面}}<br />
{{Article summary wiki|Systemd/Services}}<br />
{{Article summary wiki|systemd/User}}<br />
{{Article summary wiki|Systemd FAQ}}<br />
{{Article summary wiki|init Rosetta}}<br />
{{Article summary wiki|Daemons List}}<br />
{{Article summary wiki|udev (简体中文)}}<br />
{{Article summary end}}<br />
{{TranslationStatus (简体中文)|Systemd|2013-06-15|262626}}<br />
摘自[http://freedesktop.org/wiki/Software/systemd 项目主页]:<br />
<br />
'''''systemd''' 是 Linux 下的一款系统和服务管理器,兼容 SysV 和 LSB 的启动脚本。systemd 的特性有:支持并行化任务;同时采用 socket 式与 [[D-Bus (简体中文)|D-Bus]] 总线式激活服务;按需启动守护进程(daemon);利用 Linux 的 [[cgroups|cgroups]] 监视进程;支持快照和系统恢复;维护挂载点和自动挂载点;各服务间基于依赖关系进行精密控制。systemd 完全可以替代 Arch 曾经使用的 [[SysVinit (简体中文)|sysvinit]] 启动系统。''<br />
<br />
{{注意|[https://bbs.archlinux.org/viewtopic.php?pid&#61;1149530#p1149530 这个帖子]详细解释了 Arch 向 systemd 迁移的原因。}}<br />
<br />
另见[[Wikipedia:zh:systemd|维基百科上的介绍]]。<br />
<br />
== 迁移前需考虑 ==<br />
<br />
* 阅读[http://freedesktop.org/wiki/Software/systemd/ 该站],了解 systemd。<br />
* systemd 自己有一套日志('''journal''')系统,用于代替 '''syslog'''。两者也可以共存,参见后面的[[#日志|日志部分]]。<br />
* 虽然 systemd 可以替换 '''cron'''、'''acpid'''、'''xinetd''' 等的部分功能。至少目前还可以继续使用这些服务,无需立即切换。<br />
* 交互式 initscripts 启动脚本在 systemd 中无法工作。例如 '''netcfg-menu''' 无法{{Bug|31377}})在启动时显示。<br />
<br />
== 安装 ==<br />
{{注意|从[https://www.archlinux.org/news/systemd-is-now-the-default-on-new-installations/ 2012-10-13版安装介质]开始,安装程序已经默认安装{{pkg|systemd}} 和 {{pkg|systemd-sysvcompat}}。}}<br />
<br />
{{注意|如果是在 VPS 中使用 Arch,请先阅读:[[Virtual_Private_Server#Moving_your_VPS_from_initscripts_to_systemd]]。}}<br />
<br />
本部分帮助还在使用{{AUR|sysvinit}} 和 initscripts 的用户迁移到 '''systemd'''.<br />
<br />
# 从[[Official_Repositories_(简体中文)|官方软件仓库]]安装 {{pkg|systemd}} 并添加[[Kernel parameters|内核参数]]{{Ic|1=init=/usr/lib/systemd/systemd}}<br />
# 使用 {{ic|systemctl enable <服务名>}} 启用需要的服务(大致相当于以前 {{ic|DAEMONS}} 数组的作用,新的服务名称参见 [[Daemons_List_(简体中文)]])。<br />
# 重启系统,执行命令 {{ic|cat /proc/1/comm}},如果返回{{ic|systemd}},表示 systemd 已经正常启动。<br />
# 确认主机名已经正确设置:{{ic|hostnamectl set-hostname myhostname}}。<br />
# 删除 initscripts 和 sysvinit,并安装{{pkg|systemd-sysvcompat}}.<br />
# (可选)删除{{ic|1=init=/usr/lib/systemd/systemd}}内核参数,现在已经不需要它了。{{pkg|systemd-sysvcompat}} 软件包提供了一个软链接,使 systemd 成为默认 init。<br />
<br />
=== 附加信息 ===<br />
* 如果内核参数中有 {{ic|quiet}},建议在一开始先去掉,以便调试。<br />
{{Accuracy|reason=据有的用户称,systemd 并未对用户组有特殊要求。尽管有很多不常用的用户组,但从 consolekit 切换到 logind 后这些用户组没有发生改变。''(据译者理解:如果你碰到特殊的权限问题,可能仍需要加入这些用户组才能解决。)''}}<br />
* 使用 systemd 的时候'''无需'''将用户加入特殊[[Users and Groups (简体中文)|用户组]](如{{ic|sys}}、{{ic|disk}}、{{ic|lp}}、{{ic|network}}、{{ic|video}}、{{ic|audio}}、{{ic|optical}}、{{ic|storage}}、{{ic|scanner}}、{{ic|power}}等等)。加入这些组反而会有问题,例如audio组会导致程序阻塞软件混声。每个 PAM 登录都拥有一个 logind 会话,它通过[[Wikipedia:Access control list|POSIX ACLs]],赋予本地会话以声音设备访问权限、通过[[udisks]]挂载和卸载移动设备的权限等。<br />
{{警告|如果您准备将用户移出UNIX通用的基础用户组,请三思而后行。某些应用程序,如 '''mplayer''' 和 '''fbida''',需要访问 {{ic|/dev/fb*}} 的权限,因此必须加入 '''video''' 用户组。还有 k3b,需要用户加入 '''optical''' 组以访问旧式光学设备。正确的方法是使用 [https://mailman.archlinux.org/pipermail/arch-general/2012-October/031808.html udev] 获取[https://bbs.archlinux.org/viewtopic.php?pid&#61;1183731#p1183731 访问权限]。}}<br />
* 阅读 [[Network Configuration (简体中文)]],了解如何配置网络。<br />
<br />
== 原生 systemd 配置文件 ==<br />
{{注意|可能需要手动创建某些文件。所有文件的权限都是644,属主 root,属组 root。}}<br />
<br />
=== 虚拟控制台 ===<br />
{{ic|/etc/vconsole.conf}} 文件用来配置虚拟控制台,包括键盘布局和控制台字体:<br />
{{hc|/etc/vconsole.conf|<nowiki><br />
KEYMAP=us<br />
FONT=lat9w-16<br />
FONT_MAP=8859-1_to_uni</nowiki>}}<br />
<br />
{{注意|{{ic|{{Pkg|systemd}}-194}} 默认使用内核字体和键盘布局,因此以后不必在上述文件中留下空的 {{ic|1=KEYMAP=}} 和 {{ic|1=FONT=}} 设置。}}<br />
<br />
还可以通过命令设置键盘布局:<br />
# localectl set-keymap de<br />
<br />
{{ic|localectl}} 也可以设置X11的键盘布局:<br />
<br />
# localectl set-x11-keymap de<br />
<br />
详情参见 {{ic|man 1 localectl}} 和 {{ic|man 5 vconsole.conf}}。<br />
<br />
* 另参阅 [[Fonts (简体中文)#控制台字体|控制台字体]] 和 [[KEYMAP (简体中文)#键盘布局|键盘布局]]。<br />
<br />
=== 硬件时钟 ===<br />
<br />
'''systemd''' 默认硬件时钟为协调世界时(UTC)。<br />
<br />
{{小贴士|推荐使用[[NTP (简体中文)|NTP服务]]来在线同步硬件时钟。}}<br />
<br />
==== 硬件时钟设定为地方时 ====<br />
<br />
将硬件时钟配置为地方时('''不建议'''):<br />
<br />
# timedatectl set-local-rtc true<br />
<br />
重新调整为 UTC:<br />
<br />
# timedatectl set-local-rtc false<br />
<br />
如果设置成本地时间,处理夏令时有些麻烦。如果夏令时调整发生在关机时,下次启动时时间会出现问题([http://www.cl.cam.ac.uk/~mgk25/mswish/ut-rtc.html 更多信息])。最新的内核直接从实时时钟芯片(RTC)读取时间,不使用 {{ic|hwclock}},内核把从 RTC 读取的时间当作 UTC 处理。所以如果硬件时间是地方时,系统启动一开始识别的时间是错误的,之后很快会进行矫正。这可能导致一些问题(尤其是时间倒退时)。<br />
<br />
如果同时安装了 Windows 操作系统([http://blogs.msdn.com/b/oldnewthing/archive/2004/09/02/224672.aspx 默认使用地方时]),那么一般 RTC 会被设置为地方时。Windows 其实也能处理 UTC,需要[[Time (简体中文)#Windows 系统使用 UTC|修改注册表]]。建议让 Windows 使用 UTC,而非让 Linux 使用地方时。Windows 使用 UTC 后,请记得禁用 Windows 的时间同步功能,以防 Windows 错误设置硬件时间。如上文所说,Linux 可以使用[[NTP (简体中文)|NTP服务]]来在线同步硬件时钟。<br />
<br />
* 详情参阅 [[Time (简体中文)]]。<br />
<br />
=== 内核模块 ===<br />
目前,所有必要模块的加载均由 [[udev]] 自动完成。所以,如果不需要使用任何额外的模块,就没有必要在任何配置文件中添加启动时加载的模块。但是,有些情况下可能需要在系统启动时加载某个额外的模块,或者将某个模块列入黑名单以便使系统正常运行。<br />
<br />
==== 开机加载 ====<br />
systemd 读取 {{ic|/etc/modules-load.d/}} 中的配置加载额外的内核模块。配置文件名称通常为 {{ic|/etc/modules-load.d/<program>.conf}}。格式很简单,一行一个要读取的模块名,而空行以及第一个非空格字符为{{ic|#}}或{{ic|;}}的行会被忽略,如:<br />
{{hc|/etc/modules-load.d/virtio-net.conf|<nowiki><br />
# Load virtio-net.ko at boot<br />
virtio-net</nowiki>}}<br />
<br />
另见{{ic|man 5 modules-load.d}}。<br />
<br />
==== 配置内核模块参数 ====<br />
<br />
在 {{ic|/etc/modprobe.d/modprobe.conf}} 中设置额外的模块参数。<br />
<br />
例如:<br />
<br />
* 我们已经在 {{ic|/etc/modules-load.d/loop.conf}} 中添加文本 {{ic|loop}},告诉系统开机自动加载该模块。<br />
<br />
* 在 {{ic|/etc/modprobe.d/modprobe.conf}} 设置额外的参数,如:{{ic|options loop max_loop&#61;64}}。<br />
<br />
最后,可以通过 {{ic|cat /sys/module/loop/parameters/max_loop}} 命令检查配置是否生效。<br />
<br />
==== 禁用内核模块 ====<br />
禁用内核模块的方法和 Arch 默认的 {{Pkg|initscripts}} 相同,因为该过程实际由 {{Pkg|kmod}} 处理。参见:[[Kernel modules (简体中文)#黑名单]]。<br />
<br />
=== 文件系统挂载 ===<br />
<br />
默认行为是:在启动一个需要挂载特定分区的服务之前,系统自动检查并挂载分区。{{ic|/etc/fstab}} 中设定的网络文件系统(如 [[NFS (简体中文)|NFS]]、[[Samba (简体中文)|Samba]])无需配置即可正常工作,systemd 将确保网络文件系统在网络链接就绪后挂载。<br />
<br />
详情参阅:{{ic|man 5 systemd.mount}}。<br />
<br />
==== 自动挂载 ====<br />
<br />
* 如果 {{ic|/home}} 分区较大,可以让不依赖 {{ic|/home}} 分区的服务先启动。把下面的参数添加到 {{ic|/etc/fstab}} 文件中 {{ic|/home}} 项目的参数部分即可:<br />
<br />
noauto,x-systemd.automount<br />
<br />
这样 {{ic|/home}} 分区只有需要访问时才会被挂载。内核会缓存所有的文件操作,直到 {{ic|/home}} 分区准备完成。<br />
<br />
{{注意|这样做会使 {{ic|/home}} 的文件系统类型被识别为 {{ic|autofs}},造成 [[mlocate]] 查询时忽略该目录。实际加速效果因配置而异,所以请自己权衡是否需要。}}<br />
<br />
* 挂载远程文件系统也是同理。如果你仅想在需要的时候才挂载,也可以添加 {{ic|noauto,x-systemd.automount}} 参数。另外,可以设置 {{ic|1=x-systemd.device-timeout=#}} 参数,设置超时时间,以防止网络资源不能访问的时候浪费时间。<br />
<br />
* 如果你的加密文件系统需要密钥,则需要添加 {{ic|noauto}} 参数到 {{ic|/etc/crypttab}} 文件中的对应位置。systemd 开机的时候就不会打开这个加密设备,会一直等待到设备被访问时再使用密钥文件挂载。比如在使用加密RAID设备的时候可以节省一定的时间,因为 systemd 不必等到设备可用后才能访问。例如:<br />
<br />
{{hc|/etc/crypttab|<br />
data /dev/md0 /root/key noauto}}<br />
<br />
==== LVM ====<br />
<br />
如果装有不通过 [[Mkinitcpio (简体中文)|initramfs]] 激活的[[LVM (简体中文)|LVM]]卷,则需启动 {{ic|lvm-monitoring}} 服务(由 {{pkg|lvm2}} 软件包提供):<br />
<br />
# systemctl enable lvm-monitoring<br />
<br />
=== ACPI 电源管理 ===<br />
'''systemd''' 能够处理某些电源相关的 [[Wikipedia:Advanced_Configuration_and_Power_Interface|ACPI]] 事件,通过 {{ic|/etc/systemd/logind.conf}} 的下列选项配置:<br />
* {{ic|HandlePowerKey}}:按下电源键后的动作<br />
* {{ic|HandleSleepKey}}:按下挂起键后的动作<br />
* {{ic|HandleHibernateKey}}: 按下休眠键后的动作<br />
* {{ic|HandleLidSwitch}}:合上笔记本盖后待机<br />
<br />
动作可以是 {{ic|ignore}}、{{ic|poweroff}}、{{ic|reboot}}、{{ic|halt}}、{{ic|suspend}}、{{ic|hibernate}}、{{ic|hybrid-sleep}}、{{ic|lock}} 或 {{ic|kexec}}。<br />
<br />
系统默认设置为:<br />
HandlePowerKey=poweroff<br />
HandleSuspendKey=suspend<br />
HandleHibernateKey=hibernate<br />
HandleLidSwitch=suspend<br />
<br />
不用图形界面、或者使用 [[i3 (简体中文)|i3]]、[[awesome (简体中文)|awesome]] 这样简单的桌面管理器时,systemd 可以替代 [[acpid (简体中文)|acpid]] 处理 ACPI 事件。<br />
<br />
{{注意|运行 {{ic|systemctl restart systemd-logind}},使上述更改立即生效。}}<br />
<br />
{{注意|systemd 无法处理交流电源和电池 ACPI 事件,所以还得使用 [[Laptop Mode Tools (简体中文)|Laptop Mode Tools]] 或 [[acpid (简体中文)|acpid]] 工具。}}<br />
<br />
在当前版本的 systemd 中,这些 {{ic|Handle}} 选项将会被应用到整个系统当中,除非它们被别的程序——例如某个桌面环境中的电源管理器——给“阻止”(inhibited)。如果其它的程序没有阻止这些 {{ic|Handle}} ,你可能会先被 systemd 挂起你的系统,然后当系统被唤醒之后,电源管理器又会再次将系统挂起。<br />
<br />
{{警告|目前只有[[GNOME]] 和 [[KDE]] 支持 "inhibited" 命令。在其它的桌面管理器同样实现该功能之前,如果你想使用[[Xfce]]、[[acpid]] 或者其它程序来管理 ACPI 事件,你需要把 {{ic|Handle}} 选项设置为 {{ic|ignore}}。}}<br />
<br />
{{注意|除了内核默认的待机支持后端(用于处理待机/休眠),systemd 也可以使用其他后端(比如 [[Uswsusp]] 或 [[TuxOnIce]])。}}<br />
<br />
要令 {{ic|systemctl hibernate}} 工作,需要按照[[Pm-utils#Hibernation_.28suspend2disk.29|休眠]]和 [[Pm-utils#Mkinitcpio_Resume_Hook|mkinitcpio 唤醒扩展]]的设置步骤进行操作。(不必安装 {{ic|pm-utils}})<br />
<br />
==== 休眠时执行的脚本 ====<br />
<br />
使用 {{ic|systemctl suspend}}、{{ic|systemctl hibernate}} 或 {{ic|systemctl hybrid-sleep}} 命令执行待机/休眠时,systemd 不会调用 [[pm-utils (简体中文)|pm-utils]]。[[pm-utils (简体中文)|pm-utils]] 的钩子扩展(hook)——包括 [[pm-utils (简体中文)#建立你自己的钩子|自定义钩子]]——会失效。不过,systemd 提供了两种类似的待机/休眠时执行脚本的机制。<br />
<br />
===== 使用服务文件 =====<br />
<br />
可以将服务文件附在 suspend.target、hibernate.target 或 sleep.target 中,这样就能在待机/休眠前后执行某些操作。用户级操作和root/系统级操作应该使用不同的服务文件。要启用用户级服务文件,使用 {{ic|# systemctl enable suspend@<用户名> && systemctl enable resume@<用户名>}}。例如:<br />
<br />
{{hc|/etc/systemd/system/suspend@.service|2=<nowiki><br />
[Unit]<br />
Description=User suspend actions<br />
Before=sleep.target<br />
<br />
[Service]<br />
User=%I<br />
Type=forking<br />
Environment=DISPLAY=:0<br />
ExecStartPre= -/usr/bin/pkill -u %u unison ; /usr/local/bin/music.sh stop ; /usr/bin/mysql -e 'slave stop'<br />
ExecStart=/usr/bin/sflock<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
{{hc|/etc/systemd/system/resume@.service|2=<nowiki><br />
[Unit]<br />
Description=User resume actions<br />
After=suspend.target<br />
<br />
[Service]<br />
User=%I<br />
Type=simple<br />
ExecStartPre=/usr/local/bin/ssh-connect.sh<br />
ExecStart=/usr/bin/mysql -e 'slave start'<br />
<br />
[Install]<br />
WantedBy=suspend.target</nowiki>}}<br />
<br />
至于root/系统级服务(使用 {{ic|# systemctl enable root-suspend}} 激活):<br />
<br />
{{hc|/etc/systemd/system/root-resume.service|2=<nowiki><br />
[Unit]<br />
Description=Local system resume actions<br />
After=suspend.target<br />
<br />
[Service]<br />
Type=simple<br />
ExecStart=/usr/bin/systemctl restart mnt-media.automount<br />
<br />
[Install]<br />
WantedBy=suspend.target</nowiki>}}<br />
<br />
{{hc|/etc/systemd/system/root-suspend.service|2=<nowiki><br />
[Unit]<br />
Description=Local system suspend actions<br />
Before=sleep.target<br />
<br />
[Service]<br />
Type=simple<br />
ExecStart=-/usr/bin/pkill sshfs<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
上述服务文件的一些解释(详见 {{ic|man systemd.service}}):<br />
* 如果设置 {{ic|1=<nowiki>Type=OneShot</nowiki>}},那么可以使用多个 {{ic|1=<nowiki>ExecStart=</nowiki>}} 参数。否则只能写一个,替代方案是在 {{ic|ExecStartPre}} 中添加命令,或使用分号分隔不同命令(见第一个例子,分号前后的空格都是'''必须'''的)。 <br />
* 若命令前加上一个“-”(半角减号),则命令返回非零值时会被忽略、当作正常执行处理。<br />
* 有关调试,最好的方法是用 {{ic|journalctl}} 查看日志。<br />
<br />
===== 合并待机和唤醒服务文件 =====<br />
<br />
利用下面这种自定义的一体化待机和唤醒服务,使用单一的钩子扩展即可对不同操作(待机/休眠/混合休眠)的不同阶段(进入/唤醒)进行控制。<br />
<br />
例子和解释:<br />
<br />
{{hc|/etc/systemd/system/wicd-sleep.service|2=<nowiki><br />
[Unit]<br />
Description=Wicd sleep hook<br />
Before=sleep.target<br />
StopWhenUnneeded=yes<br />
<br />
[Service]<br />
Type=oneshot<br />
RemainAfterExit=yes<br />
ExecStart=-/usr/share/wicd/daemon/suspend.py<br />
ExecStop=-/usr/share/wicd/daemon/autoconnect.py<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
* {{ic|1=<nowiki>RemainAfterExit=yes</nowiki>}}:服务启动后,除非显式地停止,否则就认为是活动的。<br />
* {{ic|1=<nowiki>StopWhenUnneeded=yes</nowiki>}}:服务活动时,如果无其他服务依赖该服务,就停止它。在本例中,该服务会在 sleep.target 停止后停止活动。<br />
* 由于 sleep.target 会被 suspend.target、hibernate.target、hybrid-sleep.target 调用,且 sleep.target 本身设置了 StopWhenUnneeded,该服务文件对以上各种操作都是有效的。<br />
<br />
===== 使用 /usr/lib/systemd/system-sleep =====<br />
<br />
systemd 在待机/休眠时执行 {{ic|/usr/lib/systemd/system-sleep/}} 里的所有脚本,传递下面两个参数:<br />
<br />
* 参数1:若是准备进入待机/休眠状态,则为 {{ic|pre}};唤醒时为 {{ic|post}}。<br />
* 参数2:事件名称,{{ic|suspend}},{{ic|hibernate}} 或 {{ic|hybrid-sleep}}。<br />
<br />
systemd 会同时执行所有脚本,而不是像 [[pm-utils (简体中文)|pm-utils]] 那样顺序执行。<br />
<br />
脚本输出会记录在相关服务({{ic|systemd-suspend.service}}、{{ic|systemd-hibernate.service}} 或 {{ic|systemd-hybrid-sleep.service}})中。通过[[#日志|日志]]查看:<br />
# journalctl -b -u systemd-suspend<br />
<br />
{{注意|除了使用自定义脚本,还可以利用 {{ic|sleep.target}}、{{ic|suspend.target}}、{{ic|hibernate.target}} 或 {{ic|hybrid-sleep.target}} 来为单元(unit)添加睡眠状态策略。}}<br />
<br />
脚本示范:<br />
{{hc|/usr/lib/systemd/system-sleep/example.sh|<br />
#!/bin/sh<br />
case $1/$2 in<br />
pre/*)<br />
echo "进入 $2 状态..."<br />
;;<br />
post/*)<br />
echo "从 $2 状态唤醒..."<br />
;;<br />
esac}}<br />
<br />
记得添加可执行权限: <br />
# chmod a+x /usr/lib/systemd/system-sleep/example.sh<br />
<br />
详情参见 {{ic|man 7 systemd.special}} 和 {{ic|man 8 systemd-sleep}}。<br />
<br />
=== 临时文件 ===<br />
{{ic|/usr/lib/tmpfiles.d/}} 和 {{ic|/etc/tmpfiles.d/}} 中的文件描述了 systemd-tmpfiles 如何创建、清理、删除临时文件和目录,这些文件和目录通常存放在 {{ic|/run}} 和 {{ic|/tmp}} 中。配置文件名称为 {{ic|/etc/tmpfiles.d/<program>.conf}}。此处的配置能覆盖 {{ic|/usr/lib/tmpfiles.d/}} 目录中的同名配置。<br />
<br />
临时文件通常和服务文件同时提供,以生成守护进程需要的文件和目录。例如 [[Samba (简体中文)|Samba]] 服务需要目录 {{ic|/run/samba}} 存在并设置正确的权限位,就象这样:<br />
{{hc|/usr/lib/tmpfiles.d/samba.conf|<br />
D /run/samba 0755 root root<br />
}}<br />
<br />
此外,临时文件还可以用来在开机时向特定文件写入某些内容。比如,要禁止系统从USB设备唤醒,利用旧的 {{ic|/etc/rc.local}} 可以用 {{ic|echo USBE > /proc/acpi/wakeup}},而现在可以这么做:<br />
{{hc|/etc/tmpfiles.d/disable-usb-wake.conf|<br />
w /proc/acpi/wakeup - - - - USBE<br />
}}<br />
<br />
详情参见 {{ic|man 5 tmpfiles.d}}。<br />
<br />
{{注意|该方法不能向 {{ic|/sys}} 中的配置文件添加参数,因为 {{ic|systemd-tmpfiles-setup}} 有可能在相关模块加载前运行。这种情况下,需要首先通过 {{ic|modinfo <模块名>}} 确认需要的参数,并在 [[Kernel modules (简体中文)#选项|{{ic|/etc/modprobe.d}} 下的一个文件]]中设置改参数。另外,还可以使用 [[udev (简体中文)#udev规则|udev 规则]],在设备就绪时设置相应属性。}}<br />
<br />
=== 单元(unit) ===<br />
一个单元配置文件可以描述如下内容之一:系统服务、socket、系统设备、挂载点、交换分区/文件、启动目标(target)、文件系统路径、由 systemd 管理的计时器。文件格式受 .desktop 文件启发,而最初起源是 Windows 下的 .ini 文件。<br />
<br />
详情参阅 {{ic|man 5 systemd.unit}}.<br />
<br />
== systemd 基本工具 ==<br />
<br />
检视和控制systemd的主要命令是{{ic|systemctl}}。该命令可用于查看系统状态和管理系统及服务。详见{{ic|man 1 systemctl}}。<br />
<br />
{{小贴士|在 {{ic|systemctl}} 参数中添加 {{ic|-H <用户名>@<主机名>}} 可以实现对其他机器的远程控制。该过程使用 [[SSH (简体中文)|SSH]] 链接。}}<br />
<br />
{{注意|{{ic|systemadm}} 是 systemd 的官方图形前端。由 [[AUR (简体中文)|AUR]] 中的软件包 {{AUR|systemd-ui-git}} 提供。}}<br />
<br />
=== 分析系统状态 ===<br />
<br />
输出激活的单元:<br />
<br />
$ systemctl<br />
<br />
以下命令等效:<br />
<br />
$ systemctl list-units<br />
<br />
输出运行失败的单元:<br />
<br />
$ systemctl --failed<br />
<br />
所有可用的单元文件存放在 {{ic|/usr/lib/systemd/system/}} 和 {{ic|/etc/systemd/system/}} 目录(后者优先级更高)。查看所有已安装服务:<br />
$ systemctl list-unit-files<br />
<br />
=== 使用单元 ===<br />
一个单元可以是系统服务({{ic|.service}})、挂载点({{ic|.mount}})、sockets({{ic|.sockets}})。<br />
<br />
使用 {{ic|systemctl}} 控制单元时,通常需要使用单元文件的全名,包括扩展名(例如 {{ic|sshd.service}})。但是有些单元可以在{{ic|systemctl}}中使用简写方式。<br />
* 如果无扩展名,systemctl 默认把扩展名当作 {{ic|.service}}。例如 {{ic|netcfg}} 和 {{ic|netcfg.service}} 是等价的。<br />
* 挂载点会自动转化为相应的 {{ic|.mount}} 单元。例如 {{ic|/home}} 等价于 {{ic|home.mount}}。<br />
* 设备会自动转化为相应的 {{ic|.device}} 单元,所以 {{ic|/dev/sda2}} 等价于 {{ic|dev-sda2.device}}。<br />
<br />
立即激活单元:<br />
<br />
# systemctl start <单元><br />
<br />
立即停止单元:<br />
# systemctl stop <单元><br />
<br />
重启单元:<br />
<br />
# systemctl restart <单元><br />
<br />
命令单元重新读取配置:<br />
<br />
# systemctl reload <单元><br />
<br />
输出单元运行状态:<br />
<br />
$ systemctl status <单元><br />
<br />
检查单元是否配置为自动启动:<br />
$ systemctl is-enabled <单元><br />
<br />
开机自动激活单元:<br />
<br />
# systemctl enable <单元><br />
<br />
{{注意|如果服务没有{{ic|Install}}段落,一般意味着应该通过其它服务自动调用它们。如果真的需要手动安装,可以直接连接服务,如下(将{{ic|foo}}替换为真实的服务名):<br />
{{bc|# ln -s /usr/lib/systemd/system/foo.service /etc/systemd/system/graphical.target.wants/}}<br />
}}<br />
<br />
取消开机自动激活单元:<br />
<br />
# systemctl disable <单元><br />
<br />
显示单元的手册页(必须由单元文件提供):<br />
<br />
# systemctl help <单元><br />
<br />
重新载入 systemd,扫描新的或有变动的单元:<br />
# systemctl daemon-reload<br />
<br />
=== 电源管理 ===<br />
<br />
安装 {{ic|polkit}} 后才可使用电源管理。<br />
<br />
如果你正登录在一个本地的{{ic|systemd-logind}}用户会话,且当前没有其它活动的会话,那么以下命令无需root权限即可执行。否则(例如,当前有另一个用户登录在某个tty),systemd 将会自动请求输入root密码。<br />
<br />
重启:<br />
<br />
$ systemctl reboot<br />
<br />
退出系统并停止电源:<br />
<br />
$ systemctl poweroff<br />
<br />
待机:<br />
<br />
$ systemctl suspend<br />
<br />
休眠:<br />
<br />
$ systemctl hibernate<br />
<br />
混合休眠模式(同时休眠到硬盘并待机):<br />
<br />
$ systemctl hybrid-sleep<br />
<br />
== 启动登录管理器 ==<br />
<br />
通过启动[[Display Manager (简体中文)|登录管理器]](或称显示管理器),即可进行图形界面登录。<br />
要启用图形界面登录,运行适当的[[Display Manager (简体中文)|登录管理器]](或称显示管理器)即可。目前,Arch 提供了 [[GDM (简体中文)|GDM]]、[[KDM (简体中文)|KDM]]、[[SLiM (简体中文)|SLiM]]、[[XDM (简体中文)|XDM]]、[[LXDM (简体中文)|LXDM]]、[[LightDM (简体中文)|LightDM]] 和 {{AUR|SDDM}} 的 systemd 服务文件。以 KDM 为例,配置开机启动:<br />
# systemctl enable kdm.service<br />
<br />
执行上述命令后,登录管理器应当能正常工作了。如果不是的话,很可能是因为你修改了{{ic|default.target}}。默认情况应当如下:<br />
<br />
{{hc|# ls -l /etc/systemd/system/default.target|/etc/systemd/system/default.target -> /usr/lib/systemd/system/graphical.target}}<br />
<br />
删除被修改的 {{ic|default.target}} 即可,systemd 会自动使用默认配置(即 {{ic|graphical.target}}):<br />
<br />
# rm /etc/systemd/system/default.target<br />
<br />
=== 使用 systemd-logind ===<br />
<br />
{{注意|自 2012-10-30 起, [[ConsoleKit]] 已被 [https://www.archlinux.org/news/consolekit-replaced-by-logind/ systemd-logind 取代] ,作为登录到桌面环境的默认方式。}}<br />
<br />
可使用 {{ic|loginctl}} 来查看用户会话的状态。所有 [[PolicyKit]] 操作,如挂起系统、挂载外部驱动器,都无需配置即可使用。<br />
<br />
$ loginctl show-session $XDG_SESSION_ID<br />
<br />
== 自己编写 .service 文件 ==<br />
''参见:[[Systemd/Services]]''<br />
<br />
=== 处理依赖关系 ===<br />
<br />
使用systemd时,可通过正确编写单元配置文件来解决其依赖关系。典型的情况是,单元{{ic|A}}要求单元{{ic|B}}在{{ic|A}}启动之前运行。在此情况下,向单元{{ic|A}}配置文件中的 {{ic|[Unit]}} 段添加 {{ic|1=Requires=B}} 和 {{ic|1=After=B}} 即可。若此依赖关系是可选的,可添加 {{ic|1=Wants=B}} 和 {{ic|1=After=B}}。请注意 {{ic|1=Wants=}} 和 {{ic|1=Requires=}} 并不意味着 {{ic|1=After=}},即如果 {{ic|1=After=}} 选项没有制定,这两个单元将被并行启动。<br />
<br />
依赖关系通常被用在服务(service)而不是[[#目标(target)|目标(target)]]上。例如, {{ic|network.target}} 一般会被某个配置网络接口的服务引入,所以,将自定义的单元排在该服务之后即可,因为 {{ic|network.target}} 已经启动。<br />
<br />
=== 启动方式 ===<br />
<br />
编写自定义的 service 文件时,可以选择几种不同的服务启动方式。启动方式可通过配置文件 {{ic|[Service]}} 段中的 {{ic|1=Type=}} 参数进行设置。具体的参数说明请参阅 {{ic|man systemd.service}} 。<br />
<br />
* {{ic|1=Type=simple}}(默认值):systemd认为该服务将立即启动。服务进程不会fork。如果该服务要启动其他服务,不要使用此类型启动,除非该服务是socket激活型。<br />
* {{ic|1=Type=forking}}:systemd认为当该服务进程fork,且父进程退出后服务启动成功。对于常规的守护进程(daemon),除非你确定此启动方式无法满足需求,使用此类型启动即可。使用此启动类型应同时指定 {{ic|1=PIDFile=}},以便systemd能够跟踪服务的主进程。<br />
* {{ic|1=Type=oneshot}}:这一选项适用于只执行一项任务、随后立即退出的服务。可能需要同时设置 {{ic|1=RemainAfterExit=yes}} 使得 systemd 在服务进程退出之后仍然认为服务处于激活状态。<br />
* {{ic|1=Type=notify}}:与 {{ic|1=Type=simple}} 相同,但约定服务会在就绪后向 systemd 发送一个信号。这一通知的实现由 {{ic|libsystemd-daemon.so}} 提供。<br />
* {{ic|1=Type=dbus}}:若以此方式启动,当指定的 {{ic|BusName}} 出现在DBus系统总线上时,systemd认为服务就绪。<br />
<br />
=== 修改现存单元文件 ===<br />
要更改由软件包提供的单元文件,先创建名为 {{ic|/etc/systemd/system/<单元名>.d/}} 的目录(如 {{ic|/etc/systemd/system/httpd.service.d/}}),然后放入 {{ic|*.conf}} 文件,其中可以添加或重置参数。这里设置的参数优先级高于原来的单元文件。例如,如果想添加一个额外的依赖,创建这么一个文件即可:<br />
<br />
{{hc|/etc/systemd/system/<unit>.d/customdependency.conf|2=<br />
[Unit]<br />
Requires=<新依赖><br />
After=<新依赖>}}<br />
<br />
然后运行以下命令使更改生效:<br />
<br />
# systemctl daemon-reload<br />
# systemctl restart <单元><br />
<br />
此外,把旧的单元文件从 {{ic|/usr/lib/systemd/system/}} 复制到 {{ic|/etc/systemd/system/}},然后进行修改,也可以达到同样效果。在 {{ic|/etc/systemd/system/}} 目录中的单元文件的优先级总是高于 {{ic|/usr/lib/systemd/system/}} 目录中的同名单元文件。注意,当 {{ic|/usr/lib/}} 中的单元文件因软件包升级变更时,{{ic|/etc/}} 中自定义的单元文件不会同步更新。此外,你还得执行 {{ic|systemctl reenable <unit>}},手动重新启用该单元。因此,建议使用前面一种利用 {{ic|*.conf}} 的方法。<br />
<br />
{{小贴士|可以用 {{ic|systemd-delta}} 命令来查看哪些单元文件被覆盖、哪些被修改。}}<br />
<br />
=== 单元配置文件的 vim 语法高亮支持 ===<br />
<br />
可从 [[Official Repositories|官方仓库]] 安装 {{Pkg|vim-systemd}} 软件包,使 unit 配置文件在 [[Vim]] 下支持语法高亮。<br />
<br />
== 目标(target) ==<br />
启动级别(runlevel)是一个旧的概念。现在,systemd 引入了一个和启动级别功能相似又不同的概念——目标(target)。不像数字表示的启动级别,每个''目标''都有名字和独特的功能,并且能同时启用多个。一些''目标''继承其他''目标''的服务,并启动新服务。systemd 提供了一些模仿 sysvinit 启动级别的''目标'',仍可以使用旧的 {{ic|telinit 启动级别}} 命令切换。<br />
<br />
=== 获取当前目标 ===<br />
不要使用 {{ic|runlevel}} 命令了:<br />
$ systemctl list-units --type=target<br />
<br />
=== 创建新目标 ===<br />
在 Fedora 中,启动级别 0、1、3、5、6 都被赋予特定用途,并且都对应一个 systemd 的''目标''。然而,没有什么很好的移植用户定义的启动级别(2、4)的方法。要实现类似功能,可以以原有的启动级别为基础,创建一个新的''目标'' {{ic|/etc/systemd/system/<新目标>}}(可以参考 {{ic|/usr/lib/systemd/system/graphical.target}}),创建 {{ic|/etc/systemd/system/<新目标>.wants}} 目录,向其中加入额外服务的链接(指向 {{ic|/usr/lib/systemd/system/}} 中的单元文件)。<br />
<br />
=== 目标表 ===<br />
{| border="1"<br />
!SysV 启动级别!!Systemd 目标!!注释<br />
|-<br />
| 0 || runlevel0.target, poweroff.target || 中断系统(halt)<br />
|-<br />
| 1, s, single || runlevel1.target, rescue.target || 单用户模式<br />
|-<br />
| 2, 4 || runlevel2.target, runlevel4.target, multi-user.target || 用户自定义启动级别,通常识别为级别3。<br />
|-<br />
| 3 || runlevel3.target, multi-user.target || 多用户,无图形界面。用户可以通过终端或网络登录。<br />
|-<br />
| 5 || runlevel5.target, graphical.target || 多用户,图形界面。继承级别3的服务,并启动图形界面服务。<br />
|-<br />
| 6 || runlevel6.target, reboot.target || 重启<br />
|-<br />
| emergency || emergency.target || 急救模式(Emergency shell)<br />
|-<br />
|}<br />
<br />
=== 切换启动级别/目标 ===<br />
systemd 中,启动级别通过“目标单元”访问。通过如下命令切换:<br />
# systemctl isolate graphical.target<br />
该命令对下次启动无影响。等价于{{ic|telinit 3}} 或 {{ic|telinit 5}}。<br />
<br />
=== 修改默认启动级别/目标 ===<br />
开机启动进的目标是 {{ic|default.target}},默认链接到 {{ic|graphical.target}} (大致相当于原来的启动级别5)。可以通过[[Kernel parameters (简体中文)|内核参数]]更改默认启动级别:<br />
<br />
{{小贴士|可以省略扩展名 {{ic|.target}}。}}<br />
<br />
* {{ic|1=systemd.unit=multi-user.target}} (大致相当于级别3)<br />
* {{ic|1=systemd.unit=rescue.target}} (大致相当于级别1)<br />
<br />
另一个方法是修改 {{ic|default.target}}。可以通过 {{ic|systemctl}} 修改它:<br />
<br />
# systemctl enable multi-user.target<br />
<br />
命令执行情况由 {{ic|systemctl}} 显示:链接 {{ic|/etc/systemd/system/default.target}} 被创建,指向新的默认启动级别。该方法当且仅当目标配置文件中有以下内容时有效:<br />
<br />
[Install]<br />
Alias=default.target<br />
<br />
目前,{{ic|multi-user.target}}、{{ic|graphical.target}} 都包含这段内容。<br />
<br />
== 日志 ==<br />
systemd 提供了自己日志系统(logging system),称为 journal. 使用 systemd 日志,无需额外安装日志服务(syslog)。读取日志的命令:<br />
# journalctl<br />
<br />
默认情况下(当 {{ic|1=Storage=}} 在文件 {{ic|/etc/systemd/journald.conf}} 中被设置为 {{ic|auto}}),日志记录将被写入 {{ic|/var/log/journal/}}。该目录是 {{pkg|systemd}} 软件包的一部分。若被删除,systemd '''不会'''自动创建它,直到下次升级软件包时重建该目录。如果该目录缺失,systemd 会将日志记录写入 {{ic|/run/systemd/journal}}。这意味着,系统重启后日志将丢失。<br />
<br />
=== 过滤输出 ===<br />
<br />
{{ic|journalctl}}可以根据特定字段过滤输出,例如:<br />
<br />
显示本次启动后的所有日志:<br />
<br />
# journalctl -b<br />
<br />
不过,一般大家更关心的不是本次启动后的日志,而是上次启动时的(例如,刚刚系统崩溃了)。目前还没有这项功能,正在 [http://comments.gmane.org/gmane.comp.sysutils.systemd.devel/6608 systemd-devel@lists.freedesktop.org] 讨论中。<br />
<br />
目前的折中方案是:<br />
<br />
# journalctl --since=today | tac | sed -n '/-- Reboot --/{n;:r;/-- Reboot --/q;p;n;b r}' | tac<br />
<br />
以上命令输出本日内的所有启动信息。但要注意,如果日志很多,该命令执行时间会比较漫长。<br />
<br />
动态跟踪最新信息:<br />
# journalctl -f<br />
<br />
显示特定程序的所有消息:<br />
{{bc|# journalctl /usr/lib/systemd/systemd}}<br />
<br />
显示特定进程的所有消息:<br />
{{bc|1=# journalctl _PID=1}}<br />
<br />
显示指定单元的所有消息:<br />
# journalctl -u netcfg<br />
<br />
详情参阅{{ic|man journalctl}}、{{ic|man systemd.journal-fields}},以及 Lennert 的这篇[http://0pointer.de/blog/projects/journalctl.html 博文]。<br />
<br />
=== 日志大小限制 ===<br />
<br />
如果按上面的操作保留日志的话,默认日志最大限制为所在文件系统容量的 10%,即:如果 {{ic|/var/log/journal}} 储存在 50GiB 的根分区中,那么日志最多存储 5GiB 数据。可以修改 {{ic|/etc/systemd/journald.conf}} 中的 {{ic|SystemMaxUse}} 来指定该最大限制。如限制日志最大 50MiB:<br />
<br />
SystemMaxUse=50M<br />
<br />
详情参见 {{ic|man journald.conf}}.<br />
<br />
=== 配合 syslog 使用 ===<br />
systemd 提供了 socket {{ic|/run/systemd/journal/syslog}},以兼容传统日志服务。所有系统信息都会被传入。要使传统日志服务工作,需要让服务链接该 socket,而非 {{ic|/dev/log}}([http://lwn.net/Articles/474968/ 官方说明])。Arch 软件仓库中的 {{pkg|syslog-ng}} 已经包含了需要的配置。<br />
<br />
设置开机启动 syslog-ng:<br />
# systemctl enable syslog-ng<br />
<br />
[http://0pointer.de/blog/projects/ 这里]有一份很不错的 {{ic|journalctl}} 指南。<br />
<br />
== 优化 ==<br />
<br />
参阅:[[Improve Boot Performance (简体中文)]]。<br />
<br />
=== 启动过程分析 ===<br />
systemd 提供了一个分析启动过程的工具 —— {{ic|systemd-analyze}}。可以用它看看哪些单元拖慢了开机过程,并据此进行优化。<br />
<br />
查看开机过程在内核/用户空间消耗的时间:<br />
$ systemd-analyze<br />
{{小贴士|<br />
* 如果在 {{ic|/etc/mkinitcpio.conf}} 的 {{ic|HOOKS}} 数组添加 {{ic|timestamp}},并重新生成启动内存镜像({{ic|mkinitcpio -p linux}}),{{ic|systemd-analyze}} 还可以显示处理该启动镜像花费的时间。<br />
* 如果使用 [[UEFI]] 引导,且启动引导器实现了 systemd 的 [http://www.freedesktop.org/wiki/Software/systemd/BootLoaderInterface Boot Loader Interface](目前只有 [[Gummiboot]] 实现了),'''systemd-analyze''' 还可以显示 EFI 固件和启动引导器自身花费的时间。}}<br />
<br />
按照耗费时间顺序,输出启动每个单元耗费的时间:<br />
$ systemd-analyze blame<br />
<br />
生成类似于 [[Bootchart (简体中文)|bootchart]] 的开机过程图表:<br />
$ systemd-analyze plot > plot.svg<br />
<br />
==== 使用 systemd-bootchart ====<br />
<br />
自2012年10月17日,bootchart 工具已经合并进 systemd 中,使用方法和原来的 bootchart 大同小异,添加下列内容到内核参数即可:<br />
<br />
initcall_debug printk.time=y init=/usr/lib/systemd/systemd-bootchart<br />
<br />
==== 使用 bootchart2 ====<br />
<br />
由于没有办法在内核参数设置两个 init,所以不能使用源里的 bootchart。不过,[[AUR (简体中文)|AUR]] 软件包 {{AUR|bootchart2-git}} 提供了一个的 systemd 服务,这样就可以和 systemd 一起使用。安装后启用即可:<br />
{{bc|# systemctl enable bootchart.service}}<br />
详情参阅 [https://github.com/mmeeks/bootchart bootchart 文档]。<br />
<br />
=== 预读 ===<br />
systemd 自己实现了一个 readahead,可以用来提高开机效率。不过,效果会因内核版本和硬件情况而不同(极少数情况下还会变慢)。开启 readahead:<br />
<br />
# systemctl enable systemd-readahead-collect.service systemd-readahead-replay.service<br />
<br />
要知道,readahead 的超级牛力只有在重启几次后才会显现。<br />
<br />
== 疑难解答 ==<br />
<br />
=== 关机/重启十分缓慢 ===<br />
<br />
如果关机特别慢(甚至跟死机了一样),很可能是某个拒不退出的服务在作怪。systemd 会等待一段时间,然后再尝试杀死它。请阅读[http://freedesktop.org/wiki/Software/systemd/Debugging#Shutdown_Completes_Eventually 这篇文章],确认你是否是该问题受害者。<br />
<br />
=== 短时进程无日志记录 ===<br />
<br />
若 {{ic|journalctl -u foounit.service}} 没有显示某个短时进程的任何输出,那么改用 PID 试试。例如,若 {{ic|systemd-modules-load.service}} 执行失败,那么先用 {{ic|systemctl status systemd-modules-load}} 查询其 PID(比如是123),然后检索该 PID 相关的日志 {{ic|journalctl -b _PID&#61;123}}。运行时进程的日志元数据(诸如 _SYSTEMD_UNIT 和 _COMM)被乱序收集在 {{ic|/proc}} 目录。要修复该问题,必须修改内核,使其通过套接字连接来提供上述数据,该过程类似于 SCM_CREDENTIALS。<br />
<br />
=== 诊断启动问题 ===<br />
<br />
使用如下内核参数引导:<br />
{{ic|<nowiki>systemd.log_level=debug systemd.log_target=kmsg log_buf_len=1M</nowiki>}}<br />
<br />
更多有关调试的信息,参见[http://freedesktop.org/wiki/Software/systemd/Debugging 该文]。<br />
<br />
=== 禁止在程序崩溃时转储内存 ===<br />
<br />
把 {{ic|/etc/sysctl.d/coredump.conf}} 链接到 {{ic|/dev/null}},然后应用 sysctl 即可([https://bbs.archlinux.org/viewtopic.php?id=154511 来源]):<br />
<br />
# ln -s /dev/null /etc/sysctl.d/coredump.conf<br />
# /usr/lib/systemd/systemd-sysctl<br />
# ulimit -c unlimited<br />
<br />
== 相关资源 ==<br />
<br />
*[http://www.freedesktop.org/wiki/Software/systemd 官方网站]<br />
*[http://0pointer.de/public/systemd-man/ man 手册页]<br />
*[http://freedesktop.org/wiki/Software/systemd/Optimizations systemd 优化]<br />
*[http://www.freedesktop.org/wiki/Software/systemd/FrequentlyAskedQuestions 常见问题 FAQ]<br />
*[http://www.freedesktop.org/wiki/Software/systemd/TipsAndTricks 小技巧]<br />
*[http://0pointer.de/public/systemd-ebook-psankar.pdf systemd for Administrators (PDF)]<br />
*[http://fedoraproject.org/wiki/Systemd Fedora 项目对 systemd 的介绍]<br />
*[http://fedoraproject.org/wiki/How_to_debug_Systemd_problems 如何调试 systemd 故障]<br />
*''The H Open'' 杂志上的[http://www.h-online.com/open/features/Control-Centre-The-systemd-Linux-init-system-1565543.html 两][http://www.h-online.com/open/features/Booting-up-Tools-and-tips-for-systemd-1570630.html 篇]科普文章<br />
*[http://0pointer.de/blog/projects/systemd.html Lennart 的博文]<br />
*[http://0pointer.de/blog/projects/systemd-update.html 更新报告]<br />
*[http://0pointer.de/blog/projects/systemd-update-2.html 更新报告2]<br />
*[http://0pointer.de/blog/projects/systemd-update-3.html 更新报告3]<br />
*[http://0pointer.de/blog/projects/why.html 最新动态]<br />
*[https://fedoraproject.org/wiki/SysVinit_to_Systemd_Cheatsheet/zh Fedora Wiki 上的 SysVinit 和 systemd 命令对比表]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Systemd_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=262821
Systemd (简体中文)
2013-06-15T05:03:48Z
<p>Cuihao: 同步完成</p>
<hr />
<div>{{Lowercase title}}<br />
[[Category:Daemons and system services (简体中文)]]<br />
[[Category:Boot process (简体中文)]]<br />
[[ar:Systemd]]<br />
[[en:Systemd]]<br />
[[es:Systemd]]<br />
[[fr:Systemd]]<br />
[[it:Systemd]]<br />
[[ja:Systemd]]<br />
[[ru:Systemd]]<br />
[[zh-TW:Systemd]]<br />
{{Article summary start|概览}}<br />
{{Article summary text|本文介绍 Systemd 的安装和配置。}}<br />
{{Article summary heading|相关页面}}<br />
{{Article summary wiki|Systemd/Services}}<br />
{{Article summary wiki|systemd/User}}<br />
{{Article summary wiki|Systemd FAQ}}<br />
{{Article summary wiki|init Rosetta}}<br />
{{Article summary wiki|Daemons List}}<br />
{{Article summary wiki|udev (简体中文)}}<br />
{{Article summary end}}<br />
{{TranslationStatus (简体中文)|Systemd|2013-06-15|262626}}<br />
摘自[http://freedesktop.org/wiki/Software/systemd 项目主页]:<br />
<br />
'''''systemd''' 是 Linux 下的一款系统和服务管理器,兼容 SysV 和 LSB 的启动脚本。systemd 的特性有:支持并行化任务;同时采用 socket 式与 [[D-Bus (简体中文)|D-Bus]] 总线式激活服务;按需启动守护进程(daemon);利用 Linux 的 [[cgroups|cgroups]] 监视进程;支持快照和系统恢复;维护挂载点和自动挂载点;各服务间基于依赖关系进行精密控制。systemd 完全可以替代 Arch 曾经使用的 [[SysVinit (简体中文)|sysvinit]] 启动系统。''<br />
<br />
{{注意|[https://bbs.archlinux.org/viewtopic.php?pid&#61;1149530#p1149530 这个帖子]详细解释了 Arch 向 systemd 迁移的原因。}}<br />
<br />
另见[[Wikipedia:zh:systemd|维基百科上的介绍]]。<br />
<br />
== 迁移前需考虑 ==<br />
<br />
* 阅读[http://freedesktop.org/wiki/Software/systemd/ 该站],了解 systemd。<br />
* systemd 自己有一套日志('''journal''')系统,用于代替 '''syslog'''。两者也可以共存,参见后面的[[#日志|日志部分]]。<br />
* 虽然 systemd 可以替换 '''cron'''、'''acpid'''、'''xinetd''' 等的部分功能。至少目前还可以继续使用这些服务,无需立即切换。<br />
* 交互式 initscripts 启动脚本在 systemd 中无法工作。例如 '''netcfg-menu''' 无法{{Bug|31377}})在启动时显示。<br />
<br />
== 安装 ==<br />
{{注意|从[https://www.archlinux.org/news/systemd-is-now-the-default-on-new-installations/ 2012-10-13版安装介质]开始,安装程序已经默认安装{{pkg|systemd}} 和 {{pkg|systemd-sysvcompat}}。}}<br />
<br />
{{注意|如果是在 VPS 中使用 Arch,请先阅读:[[Virtual_Private_Server#Moving_your_VPS_from_initscripts_to_systemd]]。}}<br />
<br />
本部分帮助还在使用{{AUR|sysvinit}} 和 initscripts 的用户迁移到 '''systemd'''.<br />
<br />
# 从[[Official_Repositories_(简体中文)|官方软件仓库]]安装 {{pkg|systemd}} 并添加[[Kernel parameters|内核参数]]{{Ic|1=init=/usr/lib/systemd/systemd}}<br />
# 使用 {{ic|systemctl enable <服务名>}} 启用需要的服务(大致相当于以前 {{ic|DAEMONS}} 数组的作用,新的服务名称参见 [[Daemons_List_(简体中文)]])。<br />
# 重启系统,执行命令 {{ic|cat /proc/1/comm}},如果返回{{ic|systemd}},表示 systemd 已经正常启动。<br />
# 确认主机名已经正确设置:{{ic|hostnamectl set-hostname myhostname}}。<br />
# 删除 initscripts 和 sysvinit,并安装{{pkg|systemd-sysvcompat}}.<br />
# (可选)删除{{ic|1=init=/usr/lib/systemd/systemd}}内核参数,现在已经不需要它了。{{pkg|systemd-sysvcompat}} 软件包提供了一个软链接,使 systemd 成为默认 init。<br />
<br />
=== 附加信息 ===<br />
* 如果内核参数中有 {{ic|quiet}},建议在一开始先去掉,以便调试。<br />
{{Accuracy|reason=据有的用户称,systemd 并未对用户组有特殊要求。尽管有很多不常用的用户组,但从 consolekit 切换到 logind 后这些用户组没有发生改变。''(据译者理解:如果你碰到特殊的权限问题,可能仍需要加入这些用户组才能解决。)''}}<br />
* 使用 systemd 的时候'''无需'''将用户加入特殊[[Users and Groups (简体中文)|用户组]](如{{ic|sys}}、{{ic|disk}}、{{ic|lp}}、{{ic|network}}、{{ic|video}}、{{ic|audio}}、{{ic|optical}}、{{ic|storage}}、{{ic|scanner}}、{{ic|power}}等等)。加入这些组反而会有问题,例如audio组会导致程序阻塞软件混声。每个 PAM 登录都拥有一个 logind 会话,它通过[[Wikipedia:Access control list|POSIX ACLs]],赋予本地会话以声音设备访问权限、通过[[udisks]]挂载和卸载移动设备的权限等。<br />
{{警告|如果您准备将用户移出UNIX通用的基础用户组,请三思而后行。某些应用程序,如 '''mplayer''' 和 '''fbida''',需要访问 {{ic|/dev/fb*}} 的权限,因此必须加入 '''video''' 用户组。还有 k3b,需要用户加入 '''optical''' 组以访问旧式光学设备。正确的方法是使用 [https://mailman.archlinux.org/pipermail/arch-general/2012-October/031808.html udev] 获取[https://bbs.archlinux.org/viewtopic.php?pid&#61;1183731#p1183731 访问权限]。}}<br />
* 阅读 [[Network Configuration (简体中文)]],了解如何配置网络。<br />
<br />
== 原生 systemd 配置文件 ==<br />
{{注意|可能需要手动创建某些文件。所有文件的权限都是644,属主 root,属组 root。}}<br />
<br />
=== 虚拟控制台 ===<br />
{{ic|/etc/vconsole.conf}} 文件用来配置虚拟控制台,包括键盘布局和控制台字体:<br />
{{hc|/etc/vconsole.conf|<nowiki><br />
KEYMAP=us<br />
FONT=lat9w-16<br />
FONT_MAP=8859-1_to_uni</nowiki>}}<br />
<br />
{{注意|{{ic|{{Pkg|systemd}}-194}} 默认使用内核字体和键盘布局,因此以后不必在上述文件中留下空的 {{ic|1=KEYMAP=}} 和 {{ic|1=FONT=}} 设置。}}<br />
<br />
还可以通过命令设置键盘布局:<br />
# localectl set-keymap de<br />
<br />
{{ic|localectl}} 也可以设置X11的键盘布局:<br />
<br />
# localectl set-x11-keymap de<br />
<br />
详情参见 {{ic|man 1 localectl}} 和 {{ic|man 5 vconsole.conf}}。<br />
<br />
* 另参阅 [[Fonts (简体中文)#控制台字体|控制台字体]] 和 [[KEYMAP (简体中文)#键盘布局|键盘布局]]。<br />
<br />
=== 硬件时钟 ===<br />
<br />
'''systemd''' 默认硬件时钟为协调世界时(UTC)。<br />
<br />
{{小贴士|推荐使用[[NTP (简体中文)|NTP服务]]来在线同步硬件时钟。}}<br />
<br />
==== 硬件时钟设定为地方时 ====<br />
<br />
将硬件时钟配置为地方时('''不建议'''):<br />
<br />
# timedatectl set-local-rtc true<br />
<br />
重新调整为 UTC:<br />
<br />
# timedatectl set-local-rtc false<br />
<br />
如果设置成本地时间,处理夏令时有些麻烦。如果夏令时调整发生在关机时,下次启动时时间会出现问题([http://www.cl.cam.ac.uk/~mgk25/mswish/ut-rtc.html 更多信息])。最新的内核直接从实时时钟芯片(RTC)读取时间,不使用 {{ic|hwclock}},内核把从 RTC 读取的时间当作 UTC 处理。所以如果硬件时间是地方时,系统启动一开始识别的时间是错误的,之后很快会进行矫正。这可能导致一些问题(尤其是时间倒退时)。<br />
<br />
如果同时安装了 Windows 操作系统([http://blogs.msdn.com/b/oldnewthing/archive/2004/09/02/224672.aspx 默认使用地方时]),那么一般 RTC 会被设置为地方时。Windows 其实也能处理 UTC,需要[[Time (简体中文)#Windows 系统使用 UTC|修改注册表]]。建议让 Windows 使用 UTC,而非让 Linux 使用地方时。Windows 使用 UTC 后,请记得禁用 Windows 的时间同步功能,以防 Windows 错误设置硬件时间。如上文所说,Linux 可以使用[[NTP (简体中文)|NTP服务]]来在线同步硬件时钟。<br />
<br />
* 详情参阅 [[Time (简体中文)]]。<br />
<br />
=== 内核模块 ===<br />
目前,所有必要模块的加载均由 [[udev]] 自动完成。所以,如果不需要使用任何额外的模块,就没有必要在任何配置文件中添加启动时加载的模块。但是,有些情况下可能需要在系统启动时加载某个额外的模块,或者将某个模块列入黑名单以便使系统正常运行。<br />
<br />
==== 开机加载 ====<br />
systemd 读取 {{ic|/etc/modules-load.d/}} 中的配置加载额外的内核模块。配置文件名称通常为 {{ic|/etc/modules-load.d/<program>.conf}}。格式很简单,一行一个要读取的模块名,而空行以及第一个非空格字符为{{ic|#}}或{{ic|;}}的行会被忽略,如:<br />
{{hc|/etc/modules-load.d/virtio-net.conf|<nowiki><br />
# Load virtio-net.ko at boot<br />
virtio-net</nowiki>}}<br />
<br />
另见{{ic|man 5 modules-load.d}}。<br />
<br />
==== 配置内核模块参数 ====<br />
<br />
在 {{ic|/etc/modprobe.d/modprobe.conf}} 中设置额外的模块参数。<br />
<br />
例如:<br />
<br />
* 我们已经在 {{ic|/etc/modules-load.d/loop.conf}} 中添加文本 {{ic|loop}},告诉系统开机自动加载该模块。<br />
<br />
* 在 {{ic|/etc/modprobe.d/modprobe.conf}} 设置额外的参数,如:{{ic|options loop max_loop&#61;64}}。<br />
<br />
最后,可以通过 {{ic|cat /sys/module/loop/parameters/max_loop}} 命令检查配置是否生效。<br />
<br />
==== 禁用内核模块 ====<br />
禁用内核模块的方法和 Arch 默认的 {{Pkg|initscripts}} 相同,因为该过程实际由 {{Pkg|kmod}} 处理。参见:[[Kernel modules (简体中文)#黑名单]]。<br />
<br />
=== 文件系统挂载 ===<br />
<br />
默认行为是:在启动一个需要挂载特定分区的服务之前,系统自动检查并挂载分区。{{ic|/etc/fstab}} 中设定的网络文件系统(如 [[NFS (简体中文)|NFS]]、[[Samba (简体中文)|Samba]])无需配置即可正常工作,systemd 将确保网络文件系统在网络链接就绪后挂载。<br />
<br />
详情参阅:{{ic|man 5 systemd.mount}}。<br />
<br />
==== 自动挂载 ====<br />
<br />
* 如果 {{ic|/home}} 分区较大,可以让不依赖 {{ic|/home}} 分区的服务先启动。把下面的参数添加到 {{ic|/etc/fstab}} 文件中 {{ic|/home}} 项目的参数部分即可:<br />
<br />
noauto,x-systemd.automount<br />
<br />
这样 {{ic|/home}} 分区只有需要访问时才会被挂载。内核会缓存所有的文件操作,直到 {{ic|/home}} 分区准备完成。<br />
<br />
{{注意|这样做会使 {{ic|/home}} 的文件系统类型被识别为 {{ic|autofs}},造成 [[mlocate]] 查询时忽略该目录。实际加速效果因配置而异,所以请自己权衡是否需要。}}<br />
<br />
* 挂载远程文件系统也是同理。如果你仅想在需要的时候才挂载,也可以添加 {{ic|noauto,x-systemd.automount}} 参数。另外,可以设置 {{ic|1=x-systemd.device-timeout=#}} 参数,设置超时时间,以防止网络资源不能访问的时候浪费时间。<br />
<br />
* 如果你的加密文件系统需要密钥,则需要添加 {{ic|noauto}} 参数到 {{ic|/etc/crypttab}} 文件中的对应位置。systemd 开机的时候就不会打开这个加密设备,会一直等待到设备被访问时再使用密钥文件挂载。比如在使用加密RAID设备的时候可以节省一定的时间,因为 systemd 不必等到设备可用后才能访问。例如:<br />
<br />
{{hc|/etc/crypttab|<br />
data /dev/md0 /root/key noauto}}<br />
<br />
==== LVM ====<br />
<br />
如果装有不通过 [[Mkinitcpio (简体中文)|initramfs]] 激活的[[LVM (简体中文)|LVM]]卷,则需启动 {{ic|lvm-monitoring}} 服务(由 {{pkg|lvm2}} 软件包提供):<br />
<br />
# systemctl enable lvm-monitoring<br />
<br />
=== ACPI 电源管理 ===<br />
'''systemd''' 能够处理某些电源相关的 [[Wikipedia:Advanced_Configuration_and_Power_Interface|ACPI]] 事件,通过 {{ic|/etc/systemd/logind.conf}} 的下列选项配置:<br />
* {{ic|HandlePowerKey}}:按下电源键后的动作<br />
* {{ic|HandleSleepKey}}:按下挂起键后的动作<br />
* {{ic|HandleHibernateKey}}: 按下休眠键后的动作<br />
* {{ic|HandleLidSwitch}}:合上笔记本盖后待机<br />
<br />
动作可以是 {{ic|ignore}}、{{ic|poweroff}}、{{ic|reboot}}、{{ic|halt}}、{{ic|suspend}}、{{ic|hibernate}}、{{ic|hybrid-sleep}}、{{ic|lock}} 或 {{ic|kexec}}。<br />
<br />
系统默认设置为:<br />
HandlePowerKey=poweroff<br />
HandleSuspendKey=suspend<br />
HandleHibernateKey=hibernate<br />
HandleLidSwitch=suspend<br />
<br />
不用图形界面、或者使用 [[i3 (简体中文)|i3]]、[[awesome (简体中文)|awesome]] 这样简单的桌面管理器时,systemd 可以替代 [[acpid (简体中文)|acpid]] 处理 ACPI 事件。<br />
<br />
{{注意|运行 {{ic|systemctl restart systemd-logind}},使上述更改立即生效。}}<br />
<br />
{{注意|systemd 无法处理交流电源和电池 ACPI 事件,所以还得使用 [[Laptop Mode Tools (简体中文)|Laptop Mode Tools]] 或 [[acpid (简体中文)|acpid]] 工具。}}<br />
<br />
在当前版本的 systemd 中,这些 {{ic|Handle}} 选项将会被应用到整个系统当中,除非它们被别的程序——例如某个桌面环境中的电源管理器——给“阻止”(inhibited)。如果其它的程序没有阻止这些 {{ic|Handle}} ,你可能会先被 systemd 挂起你的系统,然后当系统被唤醒之后,电源管理器又会再次将系统挂起。<br />
<br />
{{警告|目前只有[[GNOME]] 和 [[KDE]] 支持 "inhibited" 命令。在其它的桌面管理器同样实现该功能之前,如果你想使用[[Xfce]]、[[acpid]] 或者其它程序来管理 ACPI 事件,你需要把 {{ic|Handle}} 选项设置为 {{ic|ignore}}。}}<br />
<br />
{{注意|除了内核默认的待机支持后端(用于处理待机/休眠),systemd 也可以使用其他后端(比如 [[Uswsusp]] 或 [[TuxOnIce]])。}}<br />
<br />
要令 {{ic|systemctl hibernate}} 工作,需要按照[[Pm-utils#Hibernation_.28suspend2disk.29|休眠]]和 [[Pm-utils#Mkinitcpio_Resume_Hook|mkinitcpio 唤醒扩展]]的设置步骤进行操作。(不必安装 {{ic|pm-utils}})<br />
<br />
==== 休眠时执行的脚本 ====<br />
<br />
使用 {{ic|systemctl suspend}}、{{ic|systemctl hibernate}} 或 {{ic|systemctl hybrid-sleep}} 命令执行待机/休眠时,systemd 不会调用 [[pm-utils (简体中文)|pm-utils]]。[[pm-utils (简体中文)|pm-utils]] 的钩子扩展(hook)——包括 [[pm-utils (简体中文)#建立你自己的钩子|自定义钩子]]——会失效。不过,systemd 提供了两种类似的待机/休眠时执行脚本的机制。<br />
<br />
===== 使用服务文件 =====<br />
<br />
可以将服务文件附在 suspend.target、hibernate.target 或 sleep.target 中,这样就能在待机/休眠前后执行某些操作。用户级操作和root/系统级操作应该使用不同的服务文件。要启用用户级服务文件,使用 {{ic|# systemctl enable suspend@<用户名> && systemctl enable resume@<用户名>}}。例如:<br />
<br />
{{hc|/etc/systemd/system/suspend@.service|2=<nowiki><br />
[Unit]<br />
Description=User suspend actions<br />
Before=sleep.target<br />
<br />
[Service]<br />
User=%I<br />
Type=forking<br />
Environment=DISPLAY=:0<br />
ExecStartPre= -/usr/bin/pkill -u %u unison ; /usr/local/bin/music.sh stop ; /usr/bin/mysql -e 'slave stop'<br />
ExecStart=/usr/bin/sflock<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
{{hc|/etc/systemd/system/resume@.service|2=<nowiki><br />
[Unit]<br />
Description=User resume actions<br />
After=suspend.target<br />
<br />
[Service]<br />
User=%I<br />
Type=simple<br />
ExecStartPre=/usr/local/bin/ssh-connect.sh<br />
ExecStart=/usr/bin/mysql -e 'slave start'<br />
<br />
[Install]<br />
WantedBy=suspend.target</nowiki>}}<br />
<br />
至于root/系统级服务(使用 {{ic|# systemctl enable root-suspend}} 激活):<br />
<br />
{{hc|/etc/systemd/system/root-resume.service|2=<nowiki><br />
[Unit]<br />
Description=Local system resume actions<br />
After=suspend.target<br />
<br />
[Service]<br />
Type=simple<br />
ExecStart=/usr/bin/systemctl restart mnt-media.automount<br />
<br />
[Install]<br />
WantedBy=suspend.target</nowiki>}}<br />
<br />
{{hc|/etc/systemd/system/root-suspend.service|2=<nowiki><br />
[Unit]<br />
Description=Local system suspend actions<br />
Before=sleep.target<br />
<br />
[Service]<br />
Type=simple<br />
ExecStart=-/usr/bin/pkill sshfs<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
上述服务文件的一些解释(详见 {{ic|man systemd.service}}):<br />
* 如果设置 {{ic|1=<nowiki>Type=OneShot</nowiki>}},那么可以使用多个 {{ic|1=<nowiki>ExecStart=</nowiki>}} 参数。否则只能写一个,替代方案是在 {{ic|ExecStartPre}} 中添加命令,或使用分号分隔不同命令(见第一个例子,分号前后的空格都是'''必须'''的)。 <br />
* 若命令前加上一个“-”(半角减号),则命令返回非零值时会被忽略、当作正常执行处理。<br />
* 有关调试,最好的方法是用 {{ic|journalctl}} 查看日志。<br />
<br />
===== 合并待机和唤醒服务文件 =====<br />
<br />
利用下面这种自定义的一体化待机和唤醒服务,使用单一的钩子扩展即可对不同操作(待机/休眠/混合休眠)的不同阶段(进入/唤醒)进行控制。<br />
<br />
例子和解释:<br />
<br />
{{hc|/etc/systemd/system/wicd-sleep.service|2=<nowiki><br />
[Unit]<br />
Description=Wicd sleep hook<br />
Before=sleep.target<br />
StopWhenUnneeded=yes<br />
<br />
[Service]<br />
Type=oneshot<br />
RemainAfterExit=yes<br />
ExecStart=-/usr/share/wicd/daemon/suspend.py<br />
ExecStop=-/usr/share/wicd/daemon/autoconnect.py<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
* {{ic|1=<nowiki>RemainAfterExit=yes</nowiki>}}:服务启动后,除非显式地停止,否则就认为是活动的。<br />
* {{ic|1=<nowiki>StopWhenUnneeded=yes</nowiki>}}:服务活动时,如果无其他服务依赖该服务,就停止它。在本例中,该服务会在 sleep.target 停止后停止活动。<br />
* 由于 sleep.target 会被 suspend.target、hibernate.target、hybrid-sleep.target 调用,且 sleep.target 本身设置了 StopWhenUnneeded,该服务文件对以上各种操作都是有效的。<br />
<br />
===== 使用 /usr/lib/systemd/system-sleep =====<br />
<br />
systemd 在待机/休眠时执行 {{ic|/usr/lib/systemd/system-sleep/}} 里的所有脚本,传递下面两个参数:<br />
<br />
* 参数1:若是准备进入待机/休眠状态,则为 {{ic|pre}};唤醒时为 {{ic|post}}。<br />
* 参数2:事件名称,{{ic|suspend}},{{ic|hibernate}} 或 {{ic|hybrid-sleep}}。<br />
<br />
systemd 会同时执行所有脚本,而不是像 [[pm-utils (简体中文)|pm-utils]] 那样顺序执行。<br />
<br />
脚本输出会记录在相关服务({{ic|systemd-suspend.service}}、{{ic|systemd-hibernate.service}} 或 {{ic|systemd-hybrid-sleep.service}})中。通过[[#日志|日志]]查看:<br />
# journalctl -b -u systemd-suspend<br />
<br />
{{注意|除了使用自定义脚本,还可以利用 {{ic|sleep.target}}、{{ic|suspend.target}}、{{ic|hibernate.target}} 或 {{ic|hybrid-sleep.target}} 来为单元(unit)添加睡眠状态策略。}}<br />
<br />
脚本示范:<br />
{{hc|/usr/lib/systemd/system-sleep/example.sh|<br />
#!/bin/sh<br />
case $1/$2 in<br />
pre/*)<br />
echo "进入 $2 状态..."<br />
;;<br />
post/*)<br />
echo "从 $2 状态唤醒..."<br />
;;<br />
esac}}<br />
<br />
记得添加可执行权限: <br />
# chmod a+x /usr/lib/systemd/system-sleep/example.sh<br />
<br />
详情参见 {{ic|man 7 systemd.special}} 和 {{ic|man 8 systemd-sleep}}。<br />
<br />
=== 临时文件 ===<br />
{{ic|/usr/lib/tmpfiles.d/}} 和 {{ic|/etc/tmpfiles.d/}} 中的文件描述了 systemd-tmpfiles 如何创建、清理、删除临时文件和目录,这些文件和目录通常存放在 {{ic|/run}} 和 {{ic|/tmp}} 中。配置文件名称为 {{ic|/etc/tmpfiles.d/<program>.conf}}。此处的配置能覆盖 {{ic|/usr/lib/tmpfiles.d/}} 目录中的同名配置。<br />
<br />
临时文件通常和服务文件同时提供,以生成守护进程需要的文件和目录。例如 [[Samba (简体中文)|Samba]] 服务需要目录 {{ic|/run/samba}} 存在并设置正确的权限位,就象这样:<br />
{{hc|/usr/lib/tmpfiles.d/samba.conf|<br />
D /run/samba 0755 root root<br />
}}<br />
<br />
此外,临时文件还可以用来在开机时向特定文件写入某些内容。比如,要禁止系统从USB设备唤醒,利用旧的 {{ic|/etc/rc.local}} 可以用 {{ic|echo USBE > /proc/acpi/wakeup}},而现在可以这么做:<br />
{{hc|/etc/tmpfiles.d/disable-usb-wake.conf|<br />
w /proc/acpi/wakeup - - - - USBE<br />
}}<br />
<br />
详情参见 {{ic|man 5 tmpfiles.d}}。<br />
<br />
{{注意|该方法不能向 {{ic|/sys}} 中的配置文件添加参数,因为 {{ic|systemd-tmpfiles-setup}} 有可能在相关模块加载前运行。这种情况下,需要首先通过 {{ic|modinfo <模块名>}} 确认需要的参数,并在 [[Kernel modules (简体中文)#选项|{{ic|/etc/modprobe.d}} 下的一个文件]]中设置改参数。另外,还可以使用 [[udev (简体中文)#udev规则|udev 规则]],在设备就绪时设置相应属性。}}<br />
<br />
=== 单元(unit) ===<br />
一个单元配置文件可以描述如下内容之一:系统服务、socket、系统设备、挂载点、交换分区/文件、启动目标(target)、文件系统路径、由 systemd 管理的计时器。文件格式受 .desktop 文件启发,而最初起源是 Windows 下的 .ini 文件。<br />
<br />
详情参阅 {{ic|man 5 systemd.unit}}.<br />
<br />
== systemd 基本工具 ==<br />
<br />
检视和控制systemd的主要命令是{{ic|systemctl}}。该命令可用于查看系统状态和管理系统及服务。详见{{ic|man 1 systemctl}}。<br />
<br />
{{小贴士|在 {{ic|systemctl}} 参数中添加 {{ic|-H <用户名>@<主机名>}} 可以实现对其他机器的远程控制。该过程使用 [[SSH (简体中文)|SSH]] 链接。}}<br />
<br />
{{注意|{{ic|systemadm}} 是 systemd 的官方图形前端。由 [[AUR (简体中文)|AUR]] 中的软件包 {{AUR|systemd-ui-git}} 提供。}}<br />
<br />
=== 分析系统状态 ===<br />
<br />
输出激活的单元:<br />
<br />
$ systemctl<br />
<br />
以下命令等效:<br />
<br />
$ systemctl list-units<br />
<br />
输出运行失败的单元:<br />
<br />
$ systemctl --failed<br />
<br />
所有可用的单元文件存放在 {{ic|/usr/lib/systemd/system/}} 和 {{ic|/etc/systemd/system/}} 目录(后者优先级更高)。查看所有已安装服务:<br />
$ systemctl list-unit-files<br />
<br />
=== 使用单元 ===<br />
一个单元可以是系统服务({{ic|.service}})、挂载点({{ic|.mount}})、sockets({{ic|.sockets}})。<br />
<br />
使用 {{ic|systemctl}} 控制单元时,通常需要使用单元文件的全名,包括扩展名(例如 {{ic|sshd.service}})。但是有些单元可以在{{ic|systemctl}}中使用简写方式。<br />
* 如果无扩展名,systemctl 默认把扩展名当作 {{ic|.service}}。例如 {{ic|netcfg}} 和 {{ic|netcfg.service}} 是等价的。<br />
* 挂载点会自动转化为相应的 {{ic|.mount}} 单元。例如 {{ic|/home}} 等价于 {{ic|home.mount}}。<br />
* 设备会自动转化为相应的 {{ic|.device}} 单元,所以 {{ic|/dev/sda2}} 等价于 {{ic|dev-sda2.device}}。<br />
<br />
立即激活单元:<br />
<br />
# systemctl start <单元><br />
<br />
立即停止单元:<br />
# systemctl stop <单元><br />
<br />
重启单元:<br />
<br />
# systemctl restart <单元><br />
<br />
命令单元重新读取配置:<br />
<br />
# systemctl reload <单元><br />
<br />
输出单元运行状态:<br />
<br />
$ systemctl status <单元><br />
<br />
检查单元是否配置为自动启动:<br />
$ systemctl is-enabled <单元><br />
<br />
开机自动激活单元:<br />
<br />
# systemctl enable <单元><br />
<br />
{{注意|如果服务没有{{ic|Install}}段落,一般意味着应该通过其它服务自动调用它们。如果真的需要手动安装,可以直接连接服务,如下(将{{ic|foo}}替换为真实的服务名):<br />
{{bc|# ln -s /usr/lib/systemd/system/foo.service /etc/systemd/system/graphical.target.wants/}}<br />
}}<br />
<br />
取消开机自动激活单元:<br />
<br />
# systemctl disable <单元><br />
<br />
显示单元的手册页(必须由单元文件提供):<br />
<br />
# systemctl help <单元><br />
<br />
重新载入 systemd,扫描新的或有变动的单元:<br />
# systemctl daemon-reload<br />
<br />
=== 电源管理 ===<br />
<br />
安装 {{ic|polkit}} 后才可使用电源管理。<br />
<br />
如果你正登录在一个本地的{{ic|systemd-logind}}用户会话,且当前没有其它活动的会话,那么以下命令无需root权限即可执行。否则(例如,当前有另一个用户登录在某个tty),systemd 将会自动请求输入root密码。<br />
<br />
重启:<br />
<br />
$ systemctl reboot<br />
<br />
退出系统并停止电源:<br />
<br />
$ systemctl poweroff<br />
<br />
待机:<br />
<br />
$ systemctl suspend<br />
<br />
休眠:<br />
<br />
$ systemctl hibernate<br />
<br />
混合休眠模式(同时休眠到硬盘并待机):<br />
<br />
$ systemctl hybrid-sleep<br />
<br />
== 启动登录管理器 ==<br />
<br />
通过启动[[Display Manager (简体中文)|登录管理器]](或称显示管理器),即可进行图形界面登录。<br />
要启用图形界面登录,运行适当的[[Display Manager (简体中文)|登录管理器]](或称显示管理器)即可。目前,Arch 提供了 [[GDM (简体中文)|GDM]]、[[KDM (简体中文)|KDM]]、[[SLiM (简体中文)|SLiM]]、[[XDM (简体中文)|XDM]]、[[LXDM (简体中文)|LXDM]]、[[LightDM (简体中文)|LightDM]] 和 {{AUR|SDDM}} 的 systemd 服务文件。以 KDM 为例,配置开机启动:<br />
# systemctl enable kdm.service<br />
<br />
执行上述命令后,登录管理器应当能正常工作了。如果不是的话,很可能是因为你修改了{{ic|default.target}}。默认情况应当如下:<br />
<br />
{{hc|# ls -l /etc/systemd/system/default.target|/etc/systemd/system/default.target -> /usr/lib/systemd/system/graphical.target}}<br />
<br />
删除被修改的 {{ic|default.target}} 即可,systemd 会自动使用默认配置(即 {{ic|graphical.target}}):<br />
<br />
# rm /etc/systemd/system/default.target<br />
<br />
=== 使用 systemd-logind ===<br />
<br />
{{注意|自 2012-10-30 起, [[ConsoleKit]] 已被 [https://www.archlinux.org/news/consolekit-replaced-by-logind/ systemd-logind 取代] ,作为登录到桌面环境的默认方式。}}<br />
<br />
可使用 {{ic|loginctl}} 来查看用户会话的状态。所有 [[PolicyKit]] 操作,如挂起系统、挂载外部驱动器,都无需配置即可使用。<br />
<br />
$ loginctl show-session $XDG_SESSION_ID<br />
<br />
== 自己编写 .service 文件 ==<br />
''参见:[[Systemd/Services]]''<br />
<br />
=== 处理依赖关系 ===<br />
<br />
使用systemd时,可通过正确编写单元配置文件来解决其依赖关系。典型的情况是,单元{{ic|A}}要求单元{{ic|B}}在{{ic|A}}启动之前运行。在此情况下,向单元{{ic|A}}配置文件中的 {{ic|[Unit]}} 段添加 {{ic|1=Requires=B}} 和 {{ic|1=After=B}} 即可。若此依赖关系是可选的,可添加 {{ic|1=Wants=B}} 和 {{ic|1=After=B}}。请注意 {{ic|1=Wants=}} 和 {{ic|1=Requires=}} 并不意味着 {{ic|1=After=}},即如果 {{ic|1=After=}} 选项没有制定,这两个单元将被并行启动。<br />
<br />
依赖关系通常被用在服务(service)而不是[[#目标(target)|目标(target)]]上。例如, {{ic|network.target}} 一般会被某个配置网络接口的服务引入,所以,将自定义的单元排在该服务之后即可,因为 {{ic|network.target}} 已经启动。<br />
<br />
=== 启动方式 ===<br />
<br />
编写自定义的service文件时,可以选择几种不同的服务启动方式。启动方式可通过配置文件 {{ic|[Service]}} 段中的 {{ic|1=Type=}} 参数进行设置。具体的参数说明请参阅 {{ic|man systemd.service}} 。<br />
<br />
* {{ic|1=Type=simple}}(默认值):systemd认为该服务将立即启动。服务进程不会fork。如果该服务要启动其他服务,不要使用此类型启动,除非该服务是socket激活型。<br />
* {{ic|1=Type=forking}}:systemd认为当该服务进程fork,且父进程退出后服务启动成功。对于常规的守护进程(daemon),除非你确定此启动方式无法满足需求,使用此类型启动即可。使用此启动类型应同时指定 {{ic|1=PIDFile=}},以便systemd能够跟踪服务的主进程。<br />
* {{ic|1=Type=oneshot}}:这一选项适用于只执行一项任务随后退出的服务。你可能需要同时设置 {{ic|1=RemainAfterExit=yes}} 使得 systemd 在服务进程退出之后仍然认为服务处于激活状态。<br />
* {{ic|1=Type=notify}}:与 {{ic|1=Type=simple}} 相同,但约定服务会在就绪后向 systemd 发送一个信号。这一通知的实现由 {{ic|libsystemd-daemon.so}} 提供。<br />
* {{ic|1=Type=dbus}}:若以此方式启动,当指定的 {{ic|BusName}} 出现在DBus系统总线上时,systemd认为服务就绪。<br />
<br />
=== 修改现存单元文件 ===<br />
要更改由软件包提供的单元文件,先创建名为 {{ic|/etc/systemd/system/<单元名>.d/}} 的目录(如 {{ic|/etc/systemd/system/httpd.service.d/}}),然后放入 {{ic|*.conf}} 文件,其中可以添加或重置参数。这里设置的参数优先级高于原来的单元文件。例如,如果想添加一个额外的依赖,创建这么一个文件即可:<br />
<br />
{{hc|/etc/systemd/system/<unit>.d/customdependency.conf|2=<br />
[Unit]<br />
Requires=<新依赖><br />
After=<新依赖>}}<br />
<br />
然后运行以下命令使更改生效:<br />
<br />
# systemctl daemon-reload<br />
# systemctl restart <单元><br />
<br />
此外,把旧的单元文件从 {{ic|/usr/lib/systemd/system/}} 复制到 {{ic|/etc/systemd/system/}},然后进行修改,也可以达到同样效果。在 {{ic|/etc/systemd/system/}} 目录中的单元文件的优先级总是高于 {{ic|/usr/lib/systemd/system/}} 目录中的同名单元文件。注意,当 {{ic|/usr/lib/}} 中的单元文件因软件包升级变更时,{{ic|/etc/}} 中自定义的单元文件不会同步更新。此外,你还得执行 {{ic|systemctl reenable <unit>}},手动重新启用该单元。因此,建议使用前面一种利用 {{ic|*.conf}} 的方法。<br />
<br />
{{小贴士|可以用 {{ic|systemd-delta}} 命令来查看哪些单元文件被覆盖、哪些被修改。}}<br />
<br />
=== 单元配置文件的 vim 语法高亮支持 ===<br />
<br />
可从 [[Official Repositories|官方仓库]] 安装 {{Pkg|vim-systemd}} 软件包,使 unit 配置文件在 [[Vim]] 下支持语法高亮。<br />
<br />
== 目标(target) ==<br />
启动级别(runlevel)是一个旧的概念。现在,systemd 引入了一个和启动级别功能相似又不同的概念——目标(target)。不像数字表示的启动级别,每个''目标''都有名字和独特的功能,并且能同时启用多个。一些''目标''继承其他''目标''的服务,并启动新服务。systemd 提供了一些模仿 sysvinit 启动级别的''目标'',仍可以使用旧的 {{ic|telinit 启动级别}} 命令切换。<br />
<br />
=== 获取当前目标 ===<br />
不要使用 {{ic|runlevel}} 命令了:<br />
$ systemctl list-units --type=target<br />
<br />
=== 创建新目标 ===<br />
在 Fedora 中,启动级别 0、1、3、5、6 都被赋予特定用途,并且都对应一个 systemd 的''目标''。然而,没有什么很好的移植用户定义的启动级别(2、4)的方法。要实现类似功能,可以以原有的启动级别为基础,创建一个新的''目标'' {{ic|/etc/systemd/system/<新目标>}}(可以参考 {{ic|/usr/lib/systemd/system/graphical.target}}),创建 {{ic|/etc/systemd/system/<新目标>.wants}} 目录,向其中加入额外服务的链接(指向 {{ic|/usr/lib/systemd/system/}} 中的单元文件)。<br />
<br />
=== 目标表 ===<br />
{| border="1"<br />
!SysV 启动级别!!Systemd 目标!!注释<br />
|-<br />
| 0 || runlevel0.target, poweroff.target || 中断系统(halt)<br />
|-<br />
| 1, s, single || runlevel1.target, rescue.target || 单用户模式<br />
|-<br />
| 2, 4 || runlevel2.target, runlevel4.target, multi-user.target || 用户自定义启动级别,通常识别为级别3。<br />
|-<br />
| 3 || runlevel3.target, multi-user.target || 多用户,无图形界面。用户可以通过终端或网络登录。<br />
|-<br />
| 5 || runlevel5.target, graphical.target || 多用户,图形界面。继承级别3的服务,并启动图形界面服务。<br />
|-<br />
| 6 || runlevel6.target, reboot.target || 重启<br />
|-<br />
| emergency || emergency.target || 急救模式(Emergency shell)<br />
|-<br />
|}<br />
<br />
=== 切换启动级别/目标 ===<br />
systemd 中,启动级别通过“目标单元”访问。通过如下命令切换:<br />
# systemctl isolate graphical.target<br />
该命令对下次启动无影响。等价于{{ic|telinit 3}} 或 {{ic|telinit 5}}。<br />
<br />
=== 修改默认启动级别/目标 ===<br />
开机启动进的目标是 {{ic|default.target}},默认链接到 {{ic|graphical.target}} (大致相当于原来的启动级别5)。可以通过[[Kernel parameters (简体中文)|内核参数]]更改默认启动级别:<br />
<br />
{{小贴士|可以省略扩展名 {{ic|.target}}。}}<br />
<br />
* {{ic|1=systemd.unit=multi-user.target}} (大致相当于级别3)<br />
* {{ic|1=systemd.unit=rescue.target}} (大致相当于级别1)<br />
<br />
另一个方法是修改 {{ic|default.target}}。可以通过 {{ic|systemctl}} 修改它:<br />
<br />
# systemctl enable multi-user.target<br />
<br />
命令执行情况由 {{ic|systemctl}} 显示:链接 {{ic|/etc/systemd/system/default.target}} 被创建,指向新的默认启动级别。该方法当且仅当目标配置文件中有以下内容时有效:<br />
<br />
[Install]<br />
Alias=default.target<br />
<br />
目前,{{ic|multi-user.target}}、{{ic|graphical.target}} 都包含这段内容。<br />
<br />
== 日志 ==<br />
systemd 提供了自己日志系统(logging system),称为 journal. 使用 systemd 日志,无需额外安装日志服务(syslog)。读取日志的命令:<br />
# journalctl<br />
<br />
默认情况下(当 {{ic|1=Storage=}} 在文件 {{ic|/etc/systemd/journald.conf}} 中被设置为 {{ic|auto}}),日志记录将被写入 {{ic|/var/log/journal/}}。该目录是 {{pkg|systemd}} 软件包的一部分。若被删除,systemd '''不会'''自动创建它,直到下次升级软件包时重建该目录。如果该目录缺失,systemd 会将日志记录写入 {{ic|/run/systemd/journal}}。这意味着,系统重启后日志将丢失。<br />
<br />
=== 过滤输出 ===<br />
<br />
{{ic|journalctl}}可以根据特定字段过滤输出,例如:<br />
<br />
显示本次启动后的所有日志:<br />
<br />
# journalctl -b<br />
<br />
不过,一般大家更关心的不是本次启动后的日志,而是上次启动时的(例如,刚刚系统崩溃了)。目前还没有这项功能,正在 [http://comments.gmane.org/gmane.comp.sysutils.systemd.devel/6608 systemd-devel@lists.freedesktop.org] 讨论中。<br />
<br />
目前的折中方案是:<br />
<br />
# journalctl --since=today | tac | sed -n '/-- Reboot --/{n;:r;/-- Reboot --/q;p;n;b r}' | tac<br />
<br />
以上命令输出本日内的所有启动信息。但要注意,如果日志很多,该命令执行时间会比较漫长。<br />
<br />
动态跟踪最新信息:<br />
# journalctl -f<br />
<br />
显示特定程序的所有消息:<br />
{{bc|# journalctl /usr/lib/systemd/systemd}}<br />
<br />
显示特定进程的所有消息:<br />
{{bc|1=# journalctl _PID=1}}<br />
<br />
显示指定单元的所有消息:<br />
# journalctl -u netcfg<br />
<br />
详情参阅{{ic|man journalctl}}、{{ic|man systemd.journal-fields}},以及 Lennert 的这篇[http://0pointer.de/blog/projects/journalctl.html 博文]。<br />
<br />
=== 日志大小限制 ===<br />
<br />
如果按上面的操作保留日志的话,默认日志最大限制为所在文件系统容量的 10%,即:如果 {{ic|/var/log/journal}} 储存在 50GiB 的根分区中,那么日志最多存储 5GiB 数据。可以修改 {{ic|/etc/systemd/journald.conf}} 中的 {{ic|SystemMaxUse}} 来指定该最大限制。如限制日志最大 50MiB:<br />
<br />
SystemMaxUse=50M<br />
<br />
详情参见 {{ic|man journald.conf}}.<br />
<br />
=== 配合 syslog 使用 ===<br />
systemd 提供了 socket {{ic|/run/systemd/journal/syslog}},以兼容传统日志服务。所有系统信息都会被传入。要使传统日志服务工作,需要让服务链接该 socket,而非 {{ic|/dev/log}}([http://lwn.net/Articles/474968/ 官方说明])。Arch 软件仓库中的 {{pkg|syslog-ng}} 已经包含了需要的配置。<br />
<br />
设置开机启动 syslog-ng:<br />
# systemctl enable syslog-ng<br />
<br />
[http://0pointer.de/blog/projects/ 这里]有一份很不错的 {{ic|journalctl}} 指南。<br />
<br />
== 优化 ==<br />
<br />
参阅:[[Improve Boot Performance (简体中文)]]。<br />
<br />
=== 启动过程分析 ===<br />
systemd 提供了一个分析启动过程的工具 —— {{ic|systemd-analyze}}。可以用它看看哪些单元拖慢了开机过程,并据此进行优化。<br />
<br />
查看开机过程在内核/用户空间消耗的时间:<br />
$ systemd-analyze<br />
{{小贴士|<br />
* 如果在 {{ic|/etc/mkinitcpio.conf}} 的 {{ic|HOOKS}} 数组添加 {{ic|timestamp}},并重新生成启动内存镜像({{ic|mkinitcpio -p linux}}),{{ic|systemd-analyze}} 还可以显示处理该启动镜像花费的时间。<br />
* 如果使用 [[UEFI]] 引导,且启动引导器实现了 systemd 的 [http://www.freedesktop.org/wiki/Software/systemd/BootLoaderInterface Boot Loader Interface](目前只有 [[Gummiboot]] 实现了),'''systemd-analyze''' 还可以显示 EFI 固件和启动引导器自身花费的时间。}}<br />
<br />
按照耗费时间顺序,输出启动每个单元耗费的时间:<br />
$ systemd-analyze blame<br />
<br />
生成类似于 [[Bootchart (简体中文)|bootchart]] 的开机过程图表:<br />
$ systemd-analyze plot > plot.svg<br />
<br />
==== 使用 systemd-bootchart ====<br />
<br />
自2012年10月17日,bootchart 工具已经合并进 systemd 中,使用方法和原来的 bootchart 大同小异,添加下列内容到内核参数即可:<br />
<br />
initcall_debug printk.time=y init=/usr/lib/systemd/systemd-bootchart<br />
<br />
==== 使用 bootchart2 ====<br />
<br />
由于没有办法在内核参数设置两个 init,所以不能使用源里的 bootchart。不过,[[AUR (简体中文)|AUR]] 软件包 {{AUR|bootchart2-git}} 提供了一个的 systemd 服务,这样就可以和 systemd 一起使用。安装后启用即可:<br />
{{bc|# systemctl enable bootchart.service}}<br />
详情参阅 [https://github.com/mmeeks/bootchart bootchart 文档]。<br />
<br />
=== 预读 ===<br />
systemd 自己实现了一个 readahead,可以用来提高开机效率。不过,效果会因内核版本和硬件情况而不同(极少数情况下还会变慢)。开启 readahead:<br />
<br />
# systemctl enable systemd-readahead-collect.service systemd-readahead-replay.service<br />
<br />
要知道,readahead 的超级牛力只有在重启几次后才会显现。<br />
<br />
== 疑难解答 ==<br />
<br />
=== 关机/重启十分缓慢 ===<br />
<br />
如果关机特别慢(甚至跟死机了一样),很可能是某个拒不退出的服务在作怪。systemd 会等待一段时间,然后再尝试杀死它。请阅读[http://freedesktop.org/wiki/Software/systemd/Debugging#Shutdown_Completes_Eventually 这篇文章],确认你是否是该问题受害者。<br />
<br />
=== 短时进程无日志记录 ===<br />
<br />
若 {{ic|journalctl -u foounit.service}} 没有显示某个短时进程的任何输出,那么改用 PID 试试。例如,若 {{ic|systemd-modules-load.service}} 执行失败,那么先用 {{ic|systemctl status systemd-modules-load}} 查询其 PID(比如是123),然后检索该 PID 相关的日志 {{ic|journalctl -b _PID&#61;123}}。运行时进程的日志元数据(诸如 _SYSTEMD_UNIT 和 _COMM)被乱序收集在 {{ic|/proc}} 目录。要修复该问题,必须修改内核,使其通过套接字连接来提供上述数据,该过程类似于 SCM_CREDENTIALS。<br />
<br />
=== 诊断启动问题 ===<br />
<br />
使用如下内核参数引导:<br />
{{ic|<nowiki>systemd.log_level=debug systemd.log_target=kmsg log_buf_len=1M</nowiki>}}<br />
<br />
更多有关调试的信息,参见[http://freedesktop.org/wiki/Software/systemd/Debugging 该文]。<br />
<br />
=== 禁止在程序崩溃时转储内存 ===<br />
<br />
把 {{ic|/etc/sysctl.d/coredump.conf}} 链接到 {{ic|/dev/null}},然后应用 sysctl 即可([https://bbs.archlinux.org/viewtopic.php?id=154511 来源]):<br />
<br />
# ln -s /dev/null /etc/sysctl.d/coredump.conf<br />
# /usr/lib/systemd/systemd-sysctl<br />
# ulimit -c unlimited<br />
<br />
== 相关资源 ==<br />
<br />
*[http://www.freedesktop.org/wiki/Software/systemd 官方网站]<br />
*[http://0pointer.de/public/systemd-man/ man 手册页]<br />
*[http://freedesktop.org/wiki/Software/systemd/Optimizations systemd 优化]<br />
*[http://www.freedesktop.org/wiki/Software/systemd/FrequentlyAskedQuestions 常见问题 FAQ]<br />
*[http://www.freedesktop.org/wiki/Software/systemd/TipsAndTricks 小技巧]<br />
*[http://0pointer.de/public/systemd-ebook-psankar.pdf systemd for Administrators (PDF)]<br />
*[http://fedoraproject.org/wiki/Systemd Fedora 项目对 systemd 的介绍]<br />
*[http://fedoraproject.org/wiki/How_to_debug_Systemd_problems 如何调试 systemd 故障]<br />
*''The H Open'' 杂志上的[http://www.h-online.com/open/features/Control-Centre-The-systemd-Linux-init-system-1565543.html 两][http://www.h-online.com/open/features/Booting-up-Tools-and-tips-for-systemd-1570630.html 篇]科普文章<br />
*[http://0pointer.de/blog/projects/systemd.html Lennart 的博文]<br />
*[http://0pointer.de/blog/projects/systemd-update.html 更新报告]<br />
*[http://0pointer.de/blog/projects/systemd-update-2.html 更新报告2]<br />
*[http://0pointer.de/blog/projects/systemd-update-3.html 更新报告3]<br />
*[http://0pointer.de/blog/projects/why.html 最新动态]<br />
*[https://fedoraproject.org/wiki/SysVinit_to_Systemd_Cheatsheet/zh Fedora Wiki 上的 SysVinit 和 systemd 命令对比表]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Systemd_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=262820
Systemd (简体中文)
2013-06-15T05:01:33Z
<p>Cuihao: 好心痛 /* 从 initscripts 迁移到 systemd */</p>
<hr />
<div>{{Lowercase title}}<br />
[[Category:Daemons and system services (简体中文)]]<br />
[[Category:Boot process (简体中文)]]<br />
[[ar:Systemd]]<br />
[[en:Systemd]]<br />
[[es:Systemd]]<br />
[[fr:Systemd]]<br />
[[it:Systemd]]<br />
[[ja:Systemd]]<br />
[[ru:Systemd]]<br />
[[zh-TW:Systemd]]<br />
{{Article summary start|概览}}<br />
{{Article summary text|本文介绍 Systemd 的安装和配置。}}<br />
{{Article summary heading|相关页面}}<br />
{{Article summary wiki|Systemd/Services}}<br />
{{Article summary wiki|systemd/User}}<br />
{{Article summary wiki|Systemd FAQ}}<br />
{{Article summary wiki|init Rosetta}}<br />
{{Article summary wiki|Daemons List}}<br />
{{Article summary wiki|udev (简体中文)}}<br />
{{Article summary end}}<br />
{{TranslationStatus (简体中文)|Systemd|2013-03-14|250286}}<br />
摘自[http://freedesktop.org/wiki/Software/systemd 项目主页]:<br />
<br />
'''''systemd''' 是 Linux 下的一款系统和服务管理器,兼容 SysV 和 LSB 的启动脚本。systemd 的特性有:支持并行化任务;同时采用 socket 式与 [[D-Bus (简体中文)|D-Bus]] 总线式激活服务;按需启动守护进程(daemon);利用 Linux 的 [[cgroups|cgroups]] 监视进程;支持快照和系统恢复;维护挂载点和自动挂载点;各服务间基于依赖关系进行精密控制。systemd 完全可以替代 Arch 曾经使用的 [[SysVinit (简体中文)|sysvinit]] 启动系统。''<br />
<br />
{{注意|[https://bbs.archlinux.org/viewtopic.php?pid&#61;1149530#p1149530 这个帖子]详细解释了 Arch 向 systemd 迁移的原因。}}<br />
<br />
另见[[Wikipedia:zh:systemd|维基百科上的介绍]]。<br />
<br />
== 迁移前需考虑 ==<br />
<br />
* 阅读[http://freedesktop.org/wiki/Software/systemd/ 该站],了解 systemd。<br />
* systemd 自己有一套日志('''journal''')系统,用于代替 '''syslog'''。两者也可以共存,参见后面的[[#日志|日志部分]]。<br />
* 虽然 systemd 可以替换 '''cron'''、'''acpid'''、'''xinetd''' 等的部分功能。至少目前还可以继续使用这些服务,无需立即切换。<br />
* 交互式 initscripts 启动脚本在 systemd 中无法工作。例如 '''netcfg-menu''' 无法{{Bug|31377}})在启动时显示。<br />
<br />
== 安装 ==<br />
{{注意|从[https://www.archlinux.org/news/systemd-is-now-the-default-on-new-installations/ 2012-10-13版安装介质]开始,安装程序已经默认安装{{pkg|systemd}} 和 {{pkg|systemd-sysvcompat}}。}}<br />
<br />
{{注意|如果是在 VPS 中使用 Arch,请先阅读:[[Virtual_Private_Server#Moving_your_VPS_from_initscripts_to_systemd]]。}}<br />
<br />
本部分帮助还在使用{{AUR|sysvinit}} 和 initscripts 的用户迁移到 '''systemd'''.<br />
<br />
# 从[[Official_Repositories_(简体中文)|官方软件仓库]]安装 {{pkg|systemd}} 并添加[[Kernel parameters|内核参数]]{{Ic|1=init=/usr/lib/systemd/systemd}}<br />
# 使用 {{ic|systemctl enable <服务名>}} 启用需要的服务(大致相当于以前 {{ic|DAEMONS}} 数组的作用,新的服务名称参见 [[Daemons_List_(简体中文)]])。<br />
# 重启系统,执行命令 {{ic|cat /proc/1/comm}},如果返回{{ic|systemd}},表示 systemd 已经正常启动。<br />
# 确认主机名已经正确设置:{{ic|hostnamectl set-hostname myhostname}}。<br />
# 删除 initscripts 和 sysvinit,并安装{{pkg|systemd-sysvcompat}}.<br />
# (可选)删除{{ic|1=init=/usr/lib/systemd/systemd}}内核参数,现在已经不需要它了。{{pkg|systemd-sysvcompat}} 软件包提供了一个软链接,使 systemd 成为默认 init。<br />
<br />
=== 附加信息 ===<br />
* 如果内核参数中有 {{ic|quiet}},建议在一开始先去掉,以便调试。<br />
{{Accuracy|reason=据有的用户称,systemd 并未对用户组有特殊要求。尽管有很多不常用的用户组,但从 consolekit 切换到 logind 后这些用户组没有发生改变。''(据译者理解:如果你碰到特殊的权限问题,可能仍需要加入这些用户组才能解决。)''}}<br />
* 使用 systemd 的时候'''无需'''将用户加入特殊[[Users and Groups (简体中文)|用户组]](如{{ic|sys}}、{{ic|disk}}、{{ic|lp}}、{{ic|network}}、{{ic|video}}、{{ic|audio}}、{{ic|optical}}、{{ic|storage}}、{{ic|scanner}}、{{ic|power}}等等)。加入这些组反而会有问题,例如audio组会导致程序阻塞软件混声。每个 PAM 登录都拥有一个 logind 会话,它通过[[Wikipedia:Access control list|POSIX ACLs]],赋予本地会话以声音设备访问权限、通过[[udisks]]挂载和卸载移动设备的权限等。<br />
{{警告|如果您准备将用户移出UNIX通用的基础用户组,请三思而后行。某些应用程序,如 '''mplayer''' 和 '''fbida''',需要访问 {{ic|/dev/fb*}} 的权限,因此必须加入 '''video''' 用户组。还有 k3b,需要用户加入 '''optical''' 组以访问旧式光学设备。正确的方法是使用 [https://mailman.archlinux.org/pipermail/arch-general/2012-October/031808.html udev] 获取[https://bbs.archlinux.org/viewtopic.php?pid&#61;1183731#p1183731 访问权限]。}}<br />
* 阅读 [[Network Configuration (简体中文)]],了解如何配置网络。<br />
<br />
== 原生 systemd 配置文件 ==<br />
{{注意|可能需要手动创建某些文件。所有文件的权限都是644,属主 root,属组 root。}}<br />
<br />
=== 虚拟控制台 ===<br />
{{ic|/etc/vconsole.conf}} 文件用来配置虚拟控制台,包括键盘布局和控制台字体:<br />
{{hc|/etc/vconsole.conf|<nowiki><br />
KEYMAP=us<br />
FONT=lat9w-16<br />
FONT_MAP=8859-1_to_uni</nowiki>}}<br />
<br />
{{注意|{{ic|{{Pkg|systemd}}-194}} 默认使用内核字体和键盘布局,因此以后不必在上述文件中留下空的 {{ic|1=KEYMAP=}} 和 {{ic|1=FONT=}} 设置。}}<br />
<br />
还可以通过命令设置键盘布局:<br />
# localectl set-keymap de<br />
<br />
{{ic|localectl}} 也可以设置X11的键盘布局:<br />
<br />
# localectl set-x11-keymap de<br />
<br />
详情参见 {{ic|man 1 localectl}} 和 {{ic|man 5 vconsole.conf}}。<br />
<br />
* 另参阅 [[Fonts (简体中文)#控制台字体|控制台字体]] 和 [[KEYMAP (简体中文)#键盘布局|键盘布局]]。<br />
<br />
=== 硬件时钟 ===<br />
<br />
'''systemd''' 默认硬件时钟为协调世界时(UTC)。<br />
<br />
{{小贴士|推荐使用[[NTP (简体中文)|NTP服务]]来在线同步硬件时钟。}}<br />
<br />
==== 硬件时钟设定为地方时 ====<br />
<br />
将硬件时钟配置为地方时('''不建议'''):<br />
<br />
# timedatectl set-local-rtc true<br />
<br />
重新调整为 UTC:<br />
<br />
# timedatectl set-local-rtc false<br />
<br />
如果设置成本地时间,处理夏令时有些麻烦。如果夏令时调整发生在关机时,下次启动时时间会出现问题([http://www.cl.cam.ac.uk/~mgk25/mswish/ut-rtc.html 更多信息])。最新的内核直接从实时时钟芯片(RTC)读取时间,不使用 {{ic|hwclock}},内核把从 RTC 读取的时间当作 UTC 处理。所以如果硬件时间是地方时,系统启动一开始识别的时间是错误的,之后很快会进行矫正。这可能导致一些问题(尤其是时间倒退时)。<br />
<br />
如果同时安装了 Windows 操作系统([http://blogs.msdn.com/b/oldnewthing/archive/2004/09/02/224672.aspx 默认使用地方时]),那么一般 RTC 会被设置为地方时。Windows 其实也能处理 UTC,需要[[Time (简体中文)#Windows 系统使用 UTC|修改注册表]]。建议让 Windows 使用 UTC,而非让 Linux 使用地方时。Windows 使用 UTC 后,请记得禁用 Windows 的时间同步功能,以防 Windows 错误设置硬件时间。如上文所说,Linux 可以使用[[NTP (简体中文)|NTP服务]]来在线同步硬件时钟。<br />
<br />
* 详情参阅 [[Time (简体中文)]]。<br />
<br />
=== 内核模块 ===<br />
目前,所有必要模块的加载均由 [[udev]] 自动完成。所以,如果不需要使用任何额外的模块,就没有必要在任何配置文件中添加启动时加载的模块。但是,有些情况下可能需要在系统启动时加载某个额外的模块,或者将某个模块列入黑名单以便使系统正常运行。<br />
<br />
==== 开机加载 ====<br />
systemd 读取 {{ic|/etc/modules-load.d/}} 中的配置加载额外的内核模块。配置文件名称通常为 {{ic|/etc/modules-load.d/<program>.conf}}。格式很简单,一行一个要读取的模块名,而空行以及第一个非空格字符为{{ic|#}}或{{ic|;}}的行会被忽略,如:<br />
{{hc|/etc/modules-load.d/virtio-net.conf|<nowiki><br />
# Load virtio-net.ko at boot<br />
virtio-net</nowiki>}}<br />
<br />
另见{{ic|man 5 modules-load.d}}。<br />
<br />
==== 配置内核模块参数 ====<br />
<br />
在 {{ic|/etc/modprobe.d/modprobe.conf}} 中设置额外的模块参数。<br />
<br />
例如:<br />
<br />
* 我们已经在 {{ic|/etc/modules-load.d/loop.conf}} 中添加文本 {{ic|loop}},告诉系统开机自动加载该模块。<br />
<br />
* 在 {{ic|/etc/modprobe.d/modprobe.conf}} 设置额外的参数,如:{{ic|options loop max_loop&#61;64}}。<br />
<br />
最后,可以通过 {{ic|cat /sys/module/loop/parameters/max_loop}} 命令检查配置是否生效。<br />
<br />
==== 禁用内核模块 ====<br />
禁用内核模块的方法和 Arch 默认的 {{Pkg|initscripts}} 相同,因为该过程实际由 {{Pkg|kmod}} 处理。参见:[[Kernel modules (简体中文)#黑名单]]。<br />
<br />
=== 文件系统挂载 ===<br />
<br />
默认行为是:在启动一个需要挂载特定分区的服务之前,系统自动检查并挂载分区。{{ic|/etc/fstab}} 中设定的网络文件系统(如 [[NFS (简体中文)|NFS]]、[[Samba (简体中文)|Samba]])无需配置即可正常工作,systemd 将确保网络文件系统在网络链接就绪后挂载。<br />
<br />
详情参阅:{{ic|man 5 systemd.mount}}。<br />
<br />
==== 自动挂载 ====<br />
<br />
* 如果 {{ic|/home}} 分区较大,可以让不依赖 {{ic|/home}} 分区的服务先启动。把下面的参数添加到 {{ic|/etc/fstab}} 文件中 {{ic|/home}} 项目的参数部分即可:<br />
<br />
noauto,x-systemd.automount<br />
<br />
这样 {{ic|/home}} 分区只有需要访问时才会被挂载。内核会缓存所有的文件操作,直到 {{ic|/home}} 分区准备完成。<br />
<br />
{{注意|这样做会使 {{ic|/home}} 的文件系统类型被识别为 {{ic|autofs}},造成 [[mlocate]] 查询时忽略该目录。实际加速效果因配置而异,所以请自己权衡是否需要。}}<br />
<br />
* 挂载远程文件系统也是同理。如果你仅想在需要的时候才挂载,也可以添加 {{ic|noauto,x-systemd.automount}} 参数。另外,可以设置 {{ic|1=x-systemd.device-timeout=#}} 参数,设置超时时间,以防止网络资源不能访问的时候浪费时间。<br />
<br />
* 如果你的加密文件系统需要密钥,则需要添加 {{ic|noauto}} 参数到 {{ic|/etc/crypttab}} 文件中的对应位置。systemd 开机的时候就不会打开这个加密设备,会一直等待到设备被访问时再使用密钥文件挂载。比如在使用加密RAID设备的时候可以节省一定的时间,因为 systemd 不必等到设备可用后才能访问。例如:<br />
<br />
{{hc|/etc/crypttab|<br />
data /dev/md0 /root/key noauto}}<br />
<br />
==== LVM ====<br />
<br />
如果装有不通过 [[Mkinitcpio (简体中文)|initramfs]] 激活的[[LVM (简体中文)|LVM]]卷,则需启动 {{ic|lvm-monitoring}} 服务(由 {{pkg|lvm2}} 软件包提供):<br />
<br />
# systemctl enable lvm-monitoring<br />
<br />
=== ACPI 电源管理 ===<br />
'''systemd''' 能够处理某些电源相关的 [[Wikipedia:Advanced_Configuration_and_Power_Interface|ACPI]] 事件,通过 {{ic|/etc/systemd/logind.conf}} 的下列选项配置:<br />
* {{ic|HandlePowerKey}}:按下电源键后的动作<br />
* {{ic|HandleSleepKey}}:按下挂起键后的动作<br />
* {{ic|HandleHibernateKey}}: 按下休眠键后的动作<br />
* {{ic|HandleLidSwitch}}:合上笔记本盖后待机<br />
<br />
动作可以是 {{ic|ignore}}、{{ic|poweroff}}、{{ic|reboot}}、{{ic|halt}}、{{ic|suspend}}、{{ic|hibernate}}、{{ic|hybrid-sleep}}、{{ic|lock}} 或 {{ic|kexec}}。<br />
<br />
系统默认设置为:<br />
HandlePowerKey=poweroff<br />
HandleSuspendKey=suspend<br />
HandleHibernateKey=hibernate<br />
HandleLidSwitch=suspend<br />
<br />
不用图形界面、或者使用 [[i3 (简体中文)|i3]]、[[awesome (简体中文)|awesome]] 这样简单的桌面管理器时,systemd 可以替代 [[acpid (简体中文)|acpid]] 处理 ACPI 事件。<br />
<br />
{{注意|运行 {{ic|systemctl restart systemd-logind}},使上述更改立即生效。}}<br />
<br />
{{注意|systemd 无法处理交流电源和电池 ACPI 事件,所以还得使用 [[Laptop Mode Tools (简体中文)|Laptop Mode Tools]] 或 [[acpid (简体中文)|acpid]] 工具。}}<br />
<br />
在当前版本的 systemd 中,这些 {{ic|Handle}} 选项将会被应用到整个系统当中,除非它们被别的程序——例如某个桌面环境中的电源管理器——给“阻止”(inhibited)。如果其它的程序没有阻止这些 {{ic|Handle}} ,你可能会先被 systemd 挂起你的系统,然后当系统被唤醒之后,电源管理器又会再次将系统挂起。<br />
<br />
{{警告|目前只有[[GNOME]] 和 [[KDE]] 支持 "inhibited" 命令。在其它的桌面管理器同样实现该功能之前,如果你想使用[[Xfce]]、[[acpid]] 或者其它程序来管理 ACPI 事件,你需要把 {{ic|Handle}} 选项设置为 {{ic|ignore}}。}}<br />
<br />
{{注意|除了内核默认的待机支持后端(用于处理待机/休眠),systemd 也可以使用其他后端(比如 [[Uswsusp]] 或 [[TuxOnIce]])。}}<br />
<br />
要令 {{ic|systemctl hibernate}} 工作,需要按照[[Pm-utils#Hibernation_.28suspend2disk.29|休眠]]和 [[Pm-utils#Mkinitcpio_Resume_Hook|mkinitcpio 唤醒扩展]]的设置步骤进行操作。(不必安装 {{ic|pm-utils}})<br />
<br />
==== 休眠时执行的脚本 ====<br />
<br />
使用 {{ic|systemctl suspend}}、{{ic|systemctl hibernate}} 或 {{ic|systemctl hybrid-sleep}} 命令执行待机/休眠时,systemd 不会调用 [[pm-utils (简体中文)|pm-utils]]。[[pm-utils (简体中文)|pm-utils]] 的钩子扩展(hook)——包括 [[pm-utils (简体中文)#建立你自己的钩子|自定义钩子]]——会失效。不过,systemd 提供了两种类似的待机/休眠时执行脚本的机制。<br />
<br />
===== 使用服务文件 =====<br />
<br />
可以将服务文件附在 suspend.target、hibernate.target 或 sleep.target 中,这样就能在待机/休眠前后执行某些操作。用户级操作和root/系统级操作应该使用不同的服务文件。要启用用户级服务文件,使用 {{ic|# systemctl enable suspend@<用户名> && systemctl enable resume@<用户名>}}。例如:<br />
<br />
{{hc|/etc/systemd/system/suspend@.service|2=<nowiki><br />
[Unit]<br />
Description=User suspend actions<br />
Before=sleep.target<br />
<br />
[Service]<br />
User=%I<br />
Type=forking<br />
Environment=DISPLAY=:0<br />
ExecStartPre= -/usr/bin/pkill -u %u unison ; /usr/local/bin/music.sh stop ; /usr/bin/mysql -e 'slave stop'<br />
ExecStart=/usr/bin/sflock<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
{{hc|/etc/systemd/system/resume@.service|2=<nowiki><br />
[Unit]<br />
Description=User resume actions<br />
After=suspend.target<br />
<br />
[Service]<br />
User=%I<br />
Type=simple<br />
ExecStartPre=/usr/local/bin/ssh-connect.sh<br />
ExecStart=/usr/bin/mysql -e 'slave start'<br />
<br />
[Install]<br />
WantedBy=suspend.target</nowiki>}}<br />
<br />
至于root/系统级服务(使用 {{ic|# systemctl enable root-suspend}} 激活):<br />
<br />
{{hc|/etc/systemd/system/root-resume.service|2=<nowiki><br />
[Unit]<br />
Description=Local system resume actions<br />
After=suspend.target<br />
<br />
[Service]<br />
Type=simple<br />
ExecStart=/usr/bin/systemctl restart mnt-media.automount<br />
<br />
[Install]<br />
WantedBy=suspend.target</nowiki>}}<br />
<br />
{{hc|/etc/systemd/system/root-suspend.service|2=<nowiki><br />
[Unit]<br />
Description=Local system suspend actions<br />
Before=sleep.target<br />
<br />
[Service]<br />
Type=simple<br />
ExecStart=-/usr/bin/pkill sshfs<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
上述服务文件的一些解释(详见 {{ic|man systemd.service}}):<br />
* 如果设置 {{ic|1=<nowiki>Type=OneShot</nowiki>}},那么可以使用多个 {{ic|1=<nowiki>ExecStart=</nowiki>}} 参数。否则只能写一个,替代方案是在 {{ic|ExecStartPre}} 中添加命令,或使用分号分隔不同命令(见第一个例子,分号前后的空格都是'''必须'''的)。 <br />
* 若命令前加上一个“-”(半角减号),则命令返回非零值时会被忽略、当作正常执行处理。<br />
* 有关调试,最好的方法是用 {{ic|journalctl}} 查看日志。<br />
<br />
===== 合并待机和唤醒服务文件 =====<br />
<br />
利用下面这种自定义的一体化待机和唤醒服务,使用单一的钩子扩展即可对不同操作(待机/休眠/混合休眠)的不同阶段(进入/唤醒)进行控制。<br />
<br />
例子和解释:<br />
<br />
{{hc|/etc/systemd/system/wicd-sleep.service|2=<nowiki><br />
[Unit]<br />
Description=Wicd sleep hook<br />
Before=sleep.target<br />
StopWhenUnneeded=yes<br />
<br />
[Service]<br />
Type=oneshot<br />
RemainAfterExit=yes<br />
ExecStart=-/usr/share/wicd/daemon/suspend.py<br />
ExecStop=-/usr/share/wicd/daemon/autoconnect.py<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
* {{ic|1=<nowiki>RemainAfterExit=yes</nowiki>}}:服务启动后,除非显式地停止,否则就认为是活动的。<br />
* {{ic|1=<nowiki>StopWhenUnneeded=yes</nowiki>}}:服务活动时,如果无其他服务依赖该服务,就停止它。在本例中,该服务会在 sleep.target 停止后停止活动。<br />
* 由于 sleep.target 会被 suspend.target、hibernate.target、hybrid-sleep.target 调用,且 sleep.target 本身设置了 StopWhenUnneeded,该服务文件对以上各种操作都是有效的。<br />
<br />
===== 使用 /usr/lib/systemd/system-sleep =====<br />
<br />
systemd 在待机/休眠时执行 {{ic|/usr/lib/systemd/system-sleep/}} 里的所有脚本,传递下面两个参数:<br />
<br />
* 参数1:若是准备进入待机/休眠状态,则为 {{ic|pre}};唤醒时为 {{ic|post}}。<br />
* 参数2:事件名称,{{ic|suspend}},{{ic|hibernate}} 或 {{ic|hybrid-sleep}}。<br />
<br />
systemd 会同时执行所有脚本,而不是像 [[pm-utils (简体中文)|pm-utils]] 那样顺序执行。<br />
<br />
脚本输出会记录在相关服务({{ic|systemd-suspend.service}}、{{ic|systemd-hibernate.service}} 或 {{ic|systemd-hybrid-sleep.service}})中。通过[[#日志|日志]]查看:<br />
# journalctl -b -u systemd-suspend<br />
<br />
{{注意|除了使用自定义脚本,还可以利用 {{ic|sleep.target}}、{{ic|suspend.target}}、{{ic|hibernate.target}} 或 {{ic|hybrid-sleep.target}} 来为单元(unit)添加睡眠状态策略。}}<br />
<br />
脚本示范:<br />
{{hc|/usr/lib/systemd/system-sleep/example.sh|<br />
#!/bin/sh<br />
case $1/$2 in<br />
pre/*)<br />
echo "进入 $2 状态..."<br />
;;<br />
post/*)<br />
echo "从 $2 状态唤醒..."<br />
;;<br />
esac}}<br />
<br />
记得添加可执行权限: <br />
# chmod a+x /usr/lib/systemd/system-sleep/example.sh<br />
<br />
详情参见 {{ic|man 7 systemd.special}} 和 {{ic|man 8 systemd-sleep}}。<br />
<br />
=== 临时文件 ===<br />
{{ic|/usr/lib/tmpfiles.d/}} 和 {{ic|/etc/tmpfiles.d/}} 中的文件描述了 systemd-tmpfiles 如何创建、清理、删除临时文件和目录,这些文件和目录通常存放在 {{ic|/run}} 和 {{ic|/tmp}} 中。配置文件名称为 {{ic|/etc/tmpfiles.d/<program>.conf}}。此处的配置能覆盖 {{ic|/usr/lib/tmpfiles.d/}} 目录中的同名配置。<br />
<br />
临时文件通常和服务文件同时提供,以生成守护进程需要的文件和目录。例如 [[Samba (简体中文)|Samba]] 服务需要目录 {{ic|/run/samba}} 存在并设置正确的权限位,就象这样:<br />
{{hc|/usr/lib/tmpfiles.d/samba.conf|<br />
D /run/samba 0755 root root<br />
}}<br />
<br />
此外,临时文件还可以用来在开机时向特定文件写入某些内容。比如,要禁止系统从USB设备唤醒,利用旧的 {{ic|/etc/rc.local}} 可以用 {{ic|echo USBE > /proc/acpi/wakeup}},而现在可以这么做:<br />
{{hc|/etc/tmpfiles.d/disable-usb-wake.conf|<br />
w /proc/acpi/wakeup - - - - USBE<br />
}}<br />
<br />
详情参见 {{ic|man 5 tmpfiles.d}}。<br />
<br />
{{注意|该方法不能向 {{ic|/sys}} 中的配置文件添加参数,因为 {{ic|systemd-tmpfiles-setup}} 有可能在相关模块加载前运行。这种情况下,需要首先通过 {{ic|modinfo <模块名>}} 确认需要的参数,并在 [[Kernel modules (简体中文)#选项|{{ic|/etc/modprobe.d}} 下的一个文件]]中设置改参数。另外,还可以使用 [[udev (简体中文)#udev规则|udev 规则]],在设备就绪时设置相应属性。}}<br />
<br />
=== 单元(unit) ===<br />
一个单元配置文件可以描述如下内容之一:系统服务、socket、系统设备、挂载点、交换分区/文件、启动目标(target)、文件系统路径、由 systemd 管理的计时器。文件格式受 .desktop 文件启发,而最初起源是 Windows 下的 .ini 文件。<br />
<br />
详情参阅 {{ic|man 5 systemd.unit}}.<br />
<br />
== systemd 基本工具 ==<br />
<br />
检视和控制systemd的主要命令是{{ic|systemctl}}。该命令可用于查看系统状态和管理系统及服务。详见{{ic|man 1 systemctl}}。<br />
<br />
{{小贴士|在 {{ic|systemctl}} 参数中添加 {{ic|-H <用户名>@<主机名>}} 可以实现对其他机器的远程控制。该过程使用 [[SSH (简体中文)|SSH]] 链接。}}<br />
<br />
{{注意|{{ic|systemadm}} 是 systemd 的官方图形前端。由 [[AUR (简体中文)|AUR]] 中的软件包 {{AUR|systemd-ui-git}} 提供。}}<br />
<br />
=== 分析系统状态 ===<br />
<br />
输出激活的单元:<br />
<br />
$ systemctl<br />
<br />
以下命令等效:<br />
<br />
$ systemctl list-units<br />
<br />
输出运行失败的单元:<br />
<br />
$ systemctl --failed<br />
<br />
所有可用的单元文件存放在 {{ic|/usr/lib/systemd/system/}} 和 {{ic|/etc/systemd/system/}} 目录(后者优先级更高)。查看所有已安装服务:<br />
$ systemctl list-unit-files<br />
<br />
=== 使用单元 ===<br />
一个单元可以是系统服务({{ic|.service}})、挂载点({{ic|.mount}})、sockets({{ic|.sockets}})。<br />
<br />
使用 {{ic|systemctl}} 控制单元时,通常需要使用单元文件的全名,包括扩展名(例如 {{ic|sshd.service}})。但是有些单元可以在{{ic|systemctl}}中使用简写方式。<br />
* 如果无扩展名,systemctl 默认把扩展名当作 {{ic|.service}}。例如 {{ic|netcfg}} 和 {{ic|netcfg.service}} 是等价的。<br />
* 挂载点会自动转化为相应的 {{ic|.mount}} 单元。例如 {{ic|/home}} 等价于 {{ic|home.mount}}。<br />
* 设备会自动转化为相应的 {{ic|.device}} 单元,所以 {{ic|/dev/sda2}} 等价于 {{ic|dev-sda2.device}}。<br />
<br />
立即激活单元:<br />
<br />
# systemctl start <单元><br />
<br />
立即停止单元:<br />
# systemctl stop <单元><br />
<br />
重启单元:<br />
<br />
# systemctl restart <单元><br />
<br />
命令单元重新读取配置:<br />
<br />
# systemctl reload <单元><br />
<br />
输出单元运行状态:<br />
<br />
$ systemctl status <单元><br />
<br />
检查单元是否配置为自动启动:<br />
$ systemctl is-enabled <单元><br />
<br />
开机自动激活单元:<br />
<br />
# systemctl enable <单元><br />
<br />
{{注意|如果服务没有{{ic|Install}}段落,一般意味着应该通过其它服务自动调用它们。如果真的需要手动安装,可以直接连接服务,如下(将{{ic|foo}}替换为真实的服务名):<br />
{{bc|# ln -s /usr/lib/systemd/system/foo.service /etc/systemd/system/graphical.target.wants/}}<br />
}}<br />
<br />
取消开机自动激活单元:<br />
<br />
# systemctl disable <单元><br />
<br />
显示单元的手册页(必须由单元文件提供):<br />
<br />
# systemctl help <单元><br />
<br />
重新载入 systemd,扫描新的或有变动的单元:<br />
# systemctl daemon-reload<br />
<br />
=== 电源管理 ===<br />
<br />
安装 {{ic|polkit}} 后才可使用电源管理。<br />
<br />
如果你正登录在一个本地的{{ic|systemd-logind}}用户会话,且当前没有其它活动的会话,那么以下命令无需root权限即可执行。否则(例如,当前有另一个用户登录在某个tty),systemd 将会自动请求输入root密码。<br />
<br />
重启:<br />
<br />
$ systemctl reboot<br />
<br />
退出系统并停止电源:<br />
<br />
$ systemctl poweroff<br />
<br />
待机:<br />
<br />
$ systemctl suspend<br />
<br />
休眠:<br />
<br />
$ systemctl hibernate<br />
<br />
混合休眠模式(同时休眠到硬盘并待机):<br />
<br />
$ systemctl hybrid-sleep<br />
<br />
== 启动登录管理器 ==<br />
<br />
通过启动[[Display Manager (简体中文)|登录管理器]](或称显示管理器),即可进行图形界面登录。<br />
要启用图形界面登录,运行适当的[[Display Manager (简体中文)|登录管理器]](或称显示管理器)即可。目前,Arch 提供了 [[GDM (简体中文)|GDM]]、[[KDM (简体中文)|KDM]]、[[SLiM (简体中文)|SLiM]]、[[XDM (简体中文)|XDM]]、[[LXDM (简体中文)|LXDM]]、[[LightDM (简体中文)|LightDM]] 和 {{AUR|SDDM}} 的 systemd 服务文件。以 KDM 为例,配置开机启动:<br />
# systemctl enable kdm.service<br />
<br />
执行上述命令后,登录管理器应当能正常工作了。如果不是的话,很可能是因为你修改了{{ic|default.target}}。默认情况应当如下:<br />
<br />
{{hc|# ls -l /etc/systemd/system/default.target|/etc/systemd/system/default.target -> /usr/lib/systemd/system/graphical.target}}<br />
<br />
删除被修改的 {{ic|default.target}} 即可,systemd 会自动使用默认配置(即 {{ic|graphical.target}}):<br />
<br />
# rm /etc/systemd/system/default.target<br />
<br />
=== 使用 systemd-logind ===<br />
<br />
{{注意|自 2012-10-30 起, [[ConsoleKit]] 已被 [https://www.archlinux.org/news/consolekit-replaced-by-logind/ systemd-logind 取代] ,作为登录到桌面环境的默认方式。}}<br />
<br />
可使用 {{ic|loginctl}} 来查看用户会话的状态。所有 [[PolicyKit]] 操作,如挂起系统、挂载外部驱动器,都无需配置即可使用。<br />
<br />
$ loginctl show-session $XDG_SESSION_ID<br />
<br />
== 自己编写 .service 文件 ==<br />
''参见:[[Systemd/Services]]''<br />
<br />
=== 处理依赖关系 ===<br />
<br />
使用systemd时,可通过正确编写单元配置文件来解决其依赖关系。典型的情况是,单元{{ic|A}}要求单元{{ic|B}}在{{ic|A}}启动之前运行。在此情况下,向单元{{ic|A}}配置文件中的 {{ic|[Unit]}} 段添加 {{ic|1=Requires=B}} 和 {{ic|1=After=B}} 即可。若此依赖关系是可选的,可添加 {{ic|1=Wants=B}} 和 {{ic|1=After=B}}。请注意 {{ic|1=Wants=}} 和 {{ic|1=Requires=}} 并不意味着 {{ic|1=After=}},即如果 {{ic|1=After=}} 选项没有制定,这两个单元将被并行启动。<br />
<br />
依赖关系通常被用在服务(service)而不是[[#目标(target)|目标(target)]]上。例如, {{ic|network.target}} 一般会被某个配置网络接口的服务引入,所以,将自定义的单元排在该服务之后即可,因为 {{ic|network.target}} 已经启动。<br />
<br />
=== 启动方式 ===<br />
<br />
编写自定义的service文件时,可以选择几种不同的服务启动方式。启动方式可通过配置文件 {{ic|[Service]}} 段中的 {{ic|1=Type=}} 参数进行设置。具体的参数说明请参阅 {{ic|man systemd.service}} 。<br />
<br />
* {{ic|1=Type=simple}}(默认值):systemd认为该服务将立即启动。服务进程不会fork。如果该服务要启动其他服务,不要使用此类型启动,除非该服务是socket激活型。<br />
* {{ic|1=Type=forking}}:systemd认为当该服务进程fork,且父进程退出后服务启动成功。对于常规的守护进程(daemon),除非你确定此启动方式无法满足需求,使用此类型启动即可。使用此启动类型应同时指定 {{ic|1=PIDFile=}},以便systemd能够跟踪服务的主进程。<br />
* {{ic|1=Type=oneshot}}:这一选项适用于只执行一项任务随后退出的服务。你可能需要同时设置 {{ic|1=RemainAfterExit=yes}} 使得 systemd 在服务进程退出之后仍然认为服务处于激活状态。<br />
* {{ic|1=Type=notify}}:与 {{ic|1=Type=simple}} 相同,但约定服务会在就绪后向 systemd 发送一个信号。这一通知的实现由 {{ic|libsystemd-daemon.so}} 提供。<br />
* {{ic|1=Type=dbus}}:若以此方式启动,当指定的 {{ic|BusName}} 出现在DBus系统总线上时,systemd认为服务就绪。<br />
<br />
=== 修改现存单元文件 ===<br />
要更改由软件包提供的单元文件,先创建名为 {{ic|/etc/systemd/system/<单元名>.d/}} 的目录(如 {{ic|/etc/systemd/system/httpd.service.d/}}),然后放入 {{ic|*.conf}} 文件,其中可以添加或重置参数。这里设置的参数优先级高于原来的单元文件。例如,如果想添加一个额外的依赖,创建这么一个文件即可:<br />
<br />
{{hc|/etc/systemd/system/<unit>.d/customdependency.conf|2=<br />
[Unit]<br />
Requires=<新依赖><br />
After=<新依赖>}}<br />
<br />
然后运行以下命令使更改生效:<br />
<br />
# systemctl daemon-reload<br />
# systemctl restart <单元><br />
<br />
此外,把旧的单元文件从 {{ic|/usr/lib/systemd/system/}} 复制到 {{ic|/etc/systemd/system/}},然后进行修改,也可以达到同样效果。在 {{ic|/etc/systemd/system/}} 目录中的单元文件的优先级总是高于 {{ic|/usr/lib/systemd/system/}} 目录中的同名单元文件。注意,当 {{ic|/usr/lib/}} 中的单元文件因软件包升级变更时,{{ic|/etc/}} 中自定义的单元文件不会同步更新。此外,你还得执行 {{ic|systemctl reenable <unit>}},手动重新启用该单元。因此,建议使用前面一种利用 {{ic|*.conf}} 的方法。<br />
<br />
{{小贴士|可以用 {{ic|systemd-delta}} 命令来查看哪些单元文件被覆盖、哪些被修改。}}<br />
<br />
=== 单元配置文件的 vim 语法高亮支持 ===<br />
<br />
可从 [[Official Repositories|官方仓库]] 安装 {{Pkg|vim-systemd}} 软件包,使 unit 配置文件在 [[Vim]] 下支持语法高亮。<br />
<br />
== 目标(target) ==<br />
启动级别(runlevel)是一个旧的概念。现在,systemd 引入了一个和启动级别功能相似又不同的概念——目标(target)。不像数字表示的启动级别,每个''目标''都有名字和独特的功能,并且能同时启用多个。一些''目标''继承其他''目标''的服务,并启动新服务。systemd 提供了一些模仿 sysvinit 启动级别的''目标'',仍可以使用旧的 {{ic|telinit 启动级别}} 命令切换。<br />
<br />
=== 获取当前目标 ===<br />
不要使用 {{ic|runlevel}} 命令了:<br />
$ systemctl list-units --type=target<br />
<br />
=== 创建新目标 ===<br />
在 Fedora 中,启动级别 0、1、3、5、6 都被赋予特定用途,并且都对应一个 systemd 的''目标''。然而,没有什么很好的移植用户定义的启动级别(2、4)的方法。要实现类似功能,可以以原有的启动级别为基础,创建一个新的''目标'' {{ic|/etc/systemd/system/<新目标>}}(可以参考 {{ic|/usr/lib/systemd/system/graphical.target}}),创建 {{ic|/etc/systemd/system/<新目标>.wants}} 目录,向其中加入额外服务的链接(指向 {{ic|/usr/lib/systemd/system/}} 中的单元文件)。<br />
<br />
=== 目标表 ===<br />
{| border="1"<br />
!SysV 启动级别!!Systemd 目标!!注释<br />
|-<br />
| 0 || runlevel0.target, poweroff.target || 中断系统(halt)<br />
|-<br />
| 1, s, single || runlevel1.target, rescue.target || 单用户模式<br />
|-<br />
| 2, 4 || runlevel2.target, runlevel4.target, multi-user.target || 用户自定义启动级别,通常识别为级别3。<br />
|-<br />
| 3 || runlevel3.target, multi-user.target || 多用户,无图形界面。用户可以通过终端或网络登录。<br />
|-<br />
| 5 || runlevel5.target, graphical.target || 多用户,图形界面。继承级别3的服务,并启动图形界面服务。<br />
|-<br />
| 6 || runlevel6.target, reboot.target || 重启<br />
|-<br />
| emergency || emergency.target || 急救模式(Emergency shell)<br />
|-<br />
|}<br />
<br />
=== 切换启动级别/目标 ===<br />
systemd 中,启动级别通过“目标单元”访问。通过如下命令切换:<br />
# systemctl isolate graphical.target<br />
该命令对下次启动无影响。等价于{{ic|telinit 3}} 或 {{ic|telinit 5}}。<br />
<br />
=== 修改默认启动级别/目标 ===<br />
开机启动进的目标是 {{ic|default.target}},默认链接到 {{ic|graphical.target}} (大致相当于原来的启动级别5)。可以通过[[Kernel parameters (简体中文)|内核参数]]更改默认启动级别:<br />
<br />
{{小贴士|可以省略扩展名 {{ic|.target}}。}}<br />
<br />
* {{ic|1=systemd.unit=multi-user.target}} (大致相当于级别3)<br />
* {{ic|1=systemd.unit=rescue.target}} (大致相当于级别1)<br />
<br />
另一个方法是修改 {{ic|default.target}}。可以通过 {{ic|systemctl}} 修改它:<br />
<br />
# systemctl enable multi-user.target<br />
<br />
命令执行情况由 {{ic|systemctl}} 显示:链接 {{ic|/etc/systemd/system/default.target}} 被创建,指向新的默认启动级别。该方法当且仅当目标配置文件中有以下内容时有效:<br />
<br />
[Install]<br />
Alias=default.target<br />
<br />
目前,{{ic|multi-user.target}}、{{ic|graphical.target}} 都包含这段内容。<br />
<br />
== 日志 ==<br />
systemd 提供了自己日志系统(logging system),称为 journal. 使用 systemd 日志,无需额外安装日志服务(syslog)。读取日志的命令:<br />
# journalctl<br />
<br />
默认情况下(当 {{ic|1=Storage=}} 在文件 {{ic|/etc/systemd/journald.conf}} 中被设置为 {{ic|auto}}),日志记录将被写入 {{ic|/var/log/journal/}}。该目录是 {{pkg|systemd}} 软件包的一部分。若被删除,systemd '''不会'''自动创建它,直到下次升级软件包时重建该目录。如果该目录缺失,systemd 会将日志记录写入 {{ic|/run/systemd/journal}}。这意味着,系统重启后日志将丢失。<br />
<br />
=== 过滤输出 ===<br />
<br />
{{ic|journalctl}}可以根据特定字段过滤输出,例如:<br />
<br />
显示本次启动后的所有日志:<br />
<br />
# journalctl -b<br />
<br />
不过,一般大家更关心的不是本次启动后的日志,而是上次启动时的(例如,刚刚系统崩溃了)。目前还没有这项功能,正在 [http://comments.gmane.org/gmane.comp.sysutils.systemd.devel/6608 systemd-devel@lists.freedesktop.org] 讨论中。<br />
<br />
目前的折中方案是:<br />
<br />
# journalctl --since=today | tac | sed -n '/-- Reboot --/{n;:r;/-- Reboot --/q;p;n;b r}' | tac<br />
<br />
以上命令输出本日内的所有启动信息。但要注意,如果日志很多,该命令执行时间会比较漫长。<br />
<br />
动态跟踪最新信息:<br />
# journalctl -f<br />
<br />
显示特定程序的所有消息:<br />
{{bc|# journalctl /usr/lib/systemd/systemd}}<br />
<br />
显示特定进程的所有消息:<br />
{{bc|1=# journalctl _PID=1}}<br />
<br />
显示指定单元的所有消息:<br />
# journalctl -u netcfg<br />
<br />
详情参阅{{ic|man journalctl}}、{{ic|man systemd.journal-fields}},以及 Lennert 的这篇[http://0pointer.de/blog/projects/journalctl.html 博文]。<br />
<br />
=== 日志大小限制 ===<br />
<br />
如果按上面的操作保留日志的话,默认日志最大限制为所在文件系统容量的 10%,即:如果 {{ic|/var/log/journal}} 储存在 50GiB 的根分区中,那么日志最多存储 5GiB 数据。可以修改 {{ic|/etc/systemd/journald.conf}} 中的 {{ic|SystemMaxUse}} 来指定该最大限制。如限制日志最大 50MiB:<br />
<br />
SystemMaxUse=50M<br />
<br />
详情参见 {{ic|man journald.conf}}.<br />
<br />
=== 配合 syslog 使用 ===<br />
systemd 提供了 socket {{ic|/run/systemd/journal/syslog}},以兼容传统日志服务。所有系统信息都会被传入。要使传统日志服务工作,需要让服务链接该 socket,而非 {{ic|/dev/log}}([http://lwn.net/Articles/474968/ 官方说明])。Arch 软件仓库中的 {{pkg|syslog-ng}} 已经包含了需要的配置。<br />
<br />
设置开机启动 syslog-ng:<br />
# systemctl enable syslog-ng<br />
<br />
[http://0pointer.de/blog/projects/ 这里]有一份很不错的 {{ic|journalctl}} 指南。<br />
<br />
== 优化 ==<br />
<br />
参阅:[[Improve Boot Performance (简体中文)]]。<br />
<br />
=== 启动过程分析 ===<br />
systemd 提供了一个分析启动过程的工具 —— {{ic|systemd-analyze}}。可以用它看看哪些单元拖慢了开机过程,并据此进行优化。<br />
<br />
查看开机过程在内核/用户空间消耗的时间:<br />
$ systemd-analyze<br />
{{小贴士|<br />
* 如果在 {{ic|/etc/mkinitcpio.conf}} 的 {{ic|HOOKS}} 数组添加 {{ic|timestamp}},并重新生成启动内存镜像({{ic|mkinitcpio -p linux}}),{{ic|systemd-analyze}} 还可以显示处理该启动镜像花费的时间。<br />
* 如果使用 [[UEFI]] 引导,且启动引导器实现了 systemd 的 [http://www.freedesktop.org/wiki/Software/systemd/BootLoaderInterface Boot Loader Interface](目前只有 [[Gummiboot]] 实现了),'''systemd-analyze''' 还可以显示 EFI 固件和启动引导器自身花费的时间。}}<br />
<br />
按照耗费时间顺序,输出启动每个单元耗费的时间:<br />
$ systemd-analyze blame<br />
<br />
生成类似于 [[Bootchart (简体中文)|bootchart]] 的开机过程图表:<br />
$ systemd-analyze plot > plot.svg<br />
<br />
==== 使用 systemd-bootchart ====<br />
<br />
自2012年10月17日,bootchart 工具已经合并进 systemd 中,使用方法和原来的 bootchart 大同小异,添加下列内容到内核参数即可:<br />
<br />
initcall_debug printk.time=y init=/usr/lib/systemd/systemd-bootchart<br />
<br />
==== 使用 bootchart2 ====<br />
<br />
由于没有办法在内核参数设置两个 init,所以不能使用源里的 bootchart。不过,[[AUR (简体中文)|AUR]] 软件包 {{AUR|bootchart2-git}} 提供了一个的 systemd 服务,这样就可以和 systemd 一起使用。安装后启用即可:<br />
{{bc|# systemctl enable bootchart.service}}<br />
详情参阅 [https://github.com/mmeeks/bootchart bootchart 文档]。<br />
<br />
=== 预读 ===<br />
systemd 自己实现了一个 readahead,可以用来提高开机效率。不过,效果会因内核版本和硬件情况而不同(极少数情况下还会变慢)。开启 readahead:<br />
<br />
# systemctl enable systemd-readahead-collect.service systemd-readahead-replay.service<br />
<br />
要知道,readahead 的超级牛力只有在重启几次后才会显现。<br />
<br />
== 疑难解答 ==<br />
<br />
=== 关机/重启十分缓慢 ===<br />
<br />
如果关机特别慢(甚至跟死机了一样),很可能是某个拒不退出的服务在作怪。systemd 会等待一段时间,然后再尝试杀死它。请阅读[http://freedesktop.org/wiki/Software/systemd/Debugging#Shutdown_Completes_Eventually 这篇文章],确认你是否是该问题受害者。<br />
<br />
=== 短时进程无日志记录 ===<br />
<br />
若 {{ic|journalctl -u foounit.service}} 没有显示某个短时进程的任何输出,那么改用 PID 试试。例如,若 {{ic|systemd-modules-load.service}} 执行失败,那么先用 {{ic|systemctl status systemd-modules-load}} 查询其 PID(比如是123),然后检索该 PID 相关的日志 {{ic|journalctl -b _PID&#61;123}}。运行时进程的日志元数据(诸如 _SYSTEMD_UNIT 和 _COMM)被乱序收集在 {{ic|/proc}} 目录。要修复该问题,必须修改内核,使其通过套接字连接来提供上述数据,该过程类似于 SCM_CREDENTIALS。<br />
<br />
=== 诊断启动问题 ===<br />
<br />
使用如下内核参数引导:<br />
{{ic|<nowiki>systemd.log_level=debug systemd.log_target=kmsg log_buf_len=1M</nowiki>}}<br />
<br />
更多有关调试的信息,参见[http://freedesktop.org/wiki/Software/systemd/Debugging 该文]。<br />
<br />
=== 禁止在程序崩溃时转储内存 ===<br />
<br />
把 {{ic|/etc/sysctl.d/coredump.conf}} 链接到 {{ic|/dev/null}},然后应用 sysctl 即可([https://bbs.archlinux.org/viewtopic.php?id=154511 来源]):<br />
<br />
# ln -s /dev/null /etc/sysctl.d/coredump.conf<br />
# /usr/lib/systemd/systemd-sysctl<br />
# ulimit -c unlimited<br />
<br />
== 相关资源 ==<br />
<br />
*[http://www.freedesktop.org/wiki/Software/systemd 官方网站]<br />
*[http://0pointer.de/public/systemd-man/ man 手册页]<br />
*[http://freedesktop.org/wiki/Software/systemd/Optimizations systemd 优化]<br />
*[http://www.freedesktop.org/wiki/Software/systemd/FrequentlyAskedQuestions 常见问题 FAQ]<br />
*[http://www.freedesktop.org/wiki/Software/systemd/TipsAndTricks 小技巧]<br />
*[http://0pointer.de/public/systemd-ebook-psankar.pdf systemd for Administrators (PDF)]<br />
*[http://fedoraproject.org/wiki/Systemd Fedora 项目对 systemd 的介绍]<br />
*[http://fedoraproject.org/wiki/How_to_debug_Systemd_problems 如何调试 systemd 故障]<br />
*''The H Open'' 杂志上的[http://www.h-online.com/open/features/Control-Centre-The-systemd-Linux-init-system-1565543.html 两][http://www.h-online.com/open/features/Booting-up-Tools-and-tips-for-systemd-1570630.html 篇]科普文章<br />
*[http://0pointer.de/blog/projects/systemd.html Lennart 的博文]<br />
*[http://0pointer.de/blog/projects/systemd-update.html 更新报告]<br />
*[http://0pointer.de/blog/projects/systemd-update-2.html 更新报告2]<br />
*[http://0pointer.de/blog/projects/systemd-update-3.html 更新报告3]<br />
*[http://0pointer.de/blog/projects/why.html 最新动态]<br />
*[https://fedoraproject.org/wiki/SysVinit_to_Systemd_Cheatsheet/zh Fedora Wiki 上的 SysVinit 和 systemd 命令对比表]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Systemd_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=262819
Systemd (简体中文)
2013-06-15T04:58:38Z
<p>Cuihao: /* 修改现存单元文件 */</p>
<hr />
<div>{{Lowercase title}}<br />
[[Category:Daemons and system services (简体中文)]]<br />
[[Category:Boot process (简体中文)]]<br />
[[ar:Systemd]]<br />
[[en:Systemd]]<br />
[[es:Systemd]]<br />
[[fr:Systemd]]<br />
[[it:Systemd]]<br />
[[ja:Systemd]]<br />
[[ru:Systemd]]<br />
[[zh-TW:Systemd]]<br />
{{Article summary start|概览}}<br />
{{Article summary text|本文介绍 Systemd 的安装和配置。}}<br />
{{Article summary heading|相关页面}}<br />
{{Article summary wiki|Systemd/Services}}<br />
{{Article summary wiki|systemd/User}}<br />
{{Article summary wiki|Systemd FAQ}}<br />
{{Article summary wiki|init Rosetta}}<br />
{{Article summary wiki|Daemons List}}<br />
{{Article summary wiki|udev (简体中文)}}<br />
{{Article summary end}}<br />
{{TranslationStatus (简体中文)|Systemd|2013-03-14|250286}}<br />
摘自[http://freedesktop.org/wiki/Software/systemd 项目主页]:<br />
<br />
'''''systemd''' 是 Linux 下的一款系统和服务管理器,兼容 SysV 和 LSB 的启动脚本。systemd 的特性有:支持并行化任务;同时采用 socket 式与 [[D-Bus (简体中文)|D-Bus]] 总线式激活服务;按需启动守护进程(daemon);利用 Linux 的 [[cgroups|cgroups]] 监视进程;支持快照和系统恢复;维护挂载点和自动挂载点;各服务间基于依赖关系进行精密控制。systemd 完全可以替代 Arch 曾经使用的 [[SysVinit (简体中文)|sysvinit]] 启动系统。''<br />
<br />
{{注意|[https://bbs.archlinux.org/viewtopic.php?pid&#61;1149530#p1149530 这个帖子]详细解释了 Arch 向 systemd 迁移的原因。}}<br />
<br />
另见[[Wikipedia:zh:systemd|维基百科上的介绍]]。<br />
<br />
== 迁移前需考虑 ==<br />
<br />
* 阅读[http://freedesktop.org/wiki/Software/systemd/ 该站],了解 systemd。<br />
* systemd 自己有一套日志('''journal''')系统,用于代替 '''syslog'''。两者也可以共存,参见后面的[[#日志|日志部分]]。<br />
* 虽然 systemd 可以替换 '''cron'''、'''acpid'''、'''xinetd''' 等的部分功能。至少目前还可以继续使用这些服务,无需立即切换。<br />
* 交互式 initscripts 启动脚本在 systemd 中无法工作。例如 '''netcfg-menu''' 无法{{Bug|31377}})在启动时显示。<br />
<br />
== 安装 ==<br />
{{注意|从[https://www.archlinux.org/news/systemd-is-now-the-default-on-new-installations/ 2012-10-13版安装介质]开始,安装程序已经默认安装{{pkg|systemd}} 和 {{pkg|systemd-sysvcompat}}。}}<br />
<br />
{{注意|如果是在 VPS 中使用 Arch,请先阅读:[[Virtual_Private_Server#Moving_your_VPS_from_initscripts_to_systemd]]。}}<br />
<br />
本部分帮助还在使用{{AUR|sysvinit}} 和 initscripts 的用户迁移到 '''systemd'''.<br />
<br />
# 从[[Official_Repositories_(简体中文)|官方软件仓库]]安装 {{pkg|systemd}} 并添加[[Kernel parameters|内核参数]]{{Ic|1=init=/usr/lib/systemd/systemd}}<br />
# 使用 {{ic|systemctl enable <服务名>}} 启用需要的服务(大致相当于以前 {{ic|DAEMONS}} 数组的作用,新的服务名称参见 [[Daemons_List_(简体中文)]])。<br />
# 重启系统,执行命令 {{ic|cat /proc/1/comm}},如果返回{{ic|systemd}},表示 systemd 已经正常启动。<br />
# 确认主机名已经正确设置:{{ic|hostnamectl set-hostname myhostname}}。<br />
# 删除 initscripts 和 sysvinit,并安装{{pkg|systemd-sysvcompat}}.<br />
# (可选)删除{{ic|1=init=/usr/lib/systemd/systemd}}内核参数,现在已经不需要它了。{{pkg|systemd-sysvcompat}} 软件包提供了一个软链接,使 systemd 成为默认 init。<br />
<br />
=== 附加信息 ===<br />
* 如果内核参数中有 {{ic|quiet}},建议在一开始先去掉,以便调试。<br />
{{Accuracy|reason=据有的用户称,systemd 并未对用户组有特殊要求。尽管有很多不常用的用户组,但从 consolekit 切换到 logind 后这些用户组没有发生改变。''(据译者理解:如果你碰到特殊的权限问题,可能仍需要加入这些用户组才能解决。)''}}<br />
* 使用 systemd 的时候'''无需'''将用户加入特殊[[Users and Groups (简体中文)|用户组]](如{{ic|sys}}、{{ic|disk}}、{{ic|lp}}、{{ic|network}}、{{ic|video}}、{{ic|audio}}、{{ic|optical}}、{{ic|storage}}、{{ic|scanner}}、{{ic|power}}等等)。加入这些组反而会有问题,例如audio组会导致程序阻塞软件混声。每个 PAM 登录都拥有一个 logind 会话,它通过[[Wikipedia:Access control list|POSIX ACLs]],赋予本地会话以声音设备访问权限、通过[[udisks]]挂载和卸载移动设备的权限等。<br />
{{警告|如果您准备将用户移出UNIX通用的基础用户组,请三思而后行。某些应用程序,如 '''mplayer''' 和 '''fbida''',需要访问 {{ic|/dev/fb*}} 的权限,因此必须加入 '''video''' 用户组。还有 k3b,需要用户加入 '''optical''' 组以访问旧式光学设备。正确的方法是使用 [https://mailman.archlinux.org/pipermail/arch-general/2012-October/031808.html udev] 获取[https://bbs.archlinux.org/viewtopic.php?pid&#61;1183731#p1183731 访问权限]。}}<br />
* 阅读 [[Network Configuration (简体中文)]],了解如何配置网络。<br />
<br />
== 原生 systemd 配置文件 ==<br />
{{注意|可能需要手动创建某些文件。所有文件的权限都是644,属主 root,属组 root。}}<br />
<br />
=== 虚拟控制台 ===<br />
{{ic|/etc/vconsole.conf}} 文件用来配置虚拟控制台,包括键盘布局和控制台字体:<br />
{{hc|/etc/vconsole.conf|<nowiki><br />
KEYMAP=us<br />
FONT=lat9w-16<br />
FONT_MAP=8859-1_to_uni</nowiki>}}<br />
<br />
{{注意|{{ic|{{Pkg|systemd}}-194}} 默认使用内核字体和键盘布局,因此以后不必在上述文件中留下空的 {{ic|1=KEYMAP=}} 和 {{ic|1=FONT=}} 设置。}}<br />
<br />
还可以通过命令设置键盘布局:<br />
# localectl set-keymap de<br />
<br />
{{ic|localectl}} 也可以设置X11的键盘布局:<br />
<br />
# localectl set-x11-keymap de<br />
<br />
详情参见 {{ic|man 1 localectl}} 和 {{ic|man 5 vconsole.conf}}。<br />
<br />
* 另参阅 [[Fonts (简体中文)#控制台字体|控制台字体]] 和 [[KEYMAP (简体中文)#键盘布局|键盘布局]]。<br />
<br />
=== 硬件时钟 ===<br />
<br />
'''systemd''' 默认硬件时钟为协调世界时(UTC)。<br />
<br />
{{小贴士|推荐使用[[NTP (简体中文)|NTP服务]]来在线同步硬件时钟。}}<br />
<br />
==== 硬件时钟设定为地方时 ====<br />
<br />
将硬件时钟配置为地方时('''不建议'''):<br />
<br />
# timedatectl set-local-rtc true<br />
<br />
重新调整为 UTC:<br />
<br />
# timedatectl set-local-rtc false<br />
<br />
如果设置成本地时间,处理夏令时有些麻烦。如果夏令时调整发生在关机时,下次启动时时间会出现问题([http://www.cl.cam.ac.uk/~mgk25/mswish/ut-rtc.html 更多信息])。最新的内核直接从实时时钟芯片(RTC)读取时间,不使用 {{ic|hwclock}},内核把从 RTC 读取的时间当作 UTC 处理。所以如果硬件时间是地方时,系统启动一开始识别的时间是错误的,之后很快会进行矫正。这可能导致一些问题(尤其是时间倒退时)。<br />
<br />
如果同时安装了 Windows 操作系统([http://blogs.msdn.com/b/oldnewthing/archive/2004/09/02/224672.aspx 默认使用地方时]),那么一般 RTC 会被设置为地方时。Windows 其实也能处理 UTC,需要[[Time (简体中文)#Windows 系统使用 UTC|修改注册表]]。建议让 Windows 使用 UTC,而非让 Linux 使用地方时。Windows 使用 UTC 后,请记得禁用 Windows 的时间同步功能,以防 Windows 错误设置硬件时间。如上文所说,Linux 可以使用[[NTP (简体中文)|NTP服务]]来在线同步硬件时钟。<br />
<br />
* 详情参阅 [[Time (简体中文)]]。<br />
<br />
=== 内核模块 ===<br />
目前,所有必要模块的加载均由 [[udev]] 自动完成。所以,如果不需要使用任何额外的模块,就没有必要在任何配置文件中添加启动时加载的模块。但是,有些情况下可能需要在系统启动时加载某个额外的模块,或者将某个模块列入黑名单以便使系统正常运行。<br />
<br />
==== 开机加载 ====<br />
systemd 读取 {{ic|/etc/modules-load.d/}} 中的配置加载额外的内核模块。配置文件名称通常为 {{ic|/etc/modules-load.d/<program>.conf}}。格式很简单,一行一个要读取的模块名,而空行以及第一个非空格字符为{{ic|#}}或{{ic|;}}的行会被忽略,如:<br />
{{hc|/etc/modules-load.d/virtio-net.conf|<nowiki><br />
# Load virtio-net.ko at boot<br />
virtio-net</nowiki>}}<br />
<br />
另见{{ic|man 5 modules-load.d}}。<br />
<br />
==== 配置内核模块参数 ====<br />
<br />
在 {{ic|/etc/modprobe.d/modprobe.conf}} 中设置额外的模块参数。<br />
<br />
例如:<br />
<br />
* 我们已经在 {{ic|/etc/modules-load.d/loop.conf}} 中添加文本 {{ic|loop}},告诉系统开机自动加载该模块。<br />
<br />
* 在 {{ic|/etc/modprobe.d/modprobe.conf}} 设置额外的参数,如:{{ic|options loop max_loop&#61;64}}。<br />
<br />
最后,可以通过 {{ic|cat /sys/module/loop/parameters/max_loop}} 命令检查配置是否生效。<br />
<br />
==== 禁用内核模块 ====<br />
禁用内核模块的方法和 Arch 默认的 {{Pkg|initscripts}} 相同,因为该过程实际由 {{Pkg|kmod}} 处理。参见:[[Kernel modules (简体中文)#黑名单]]。<br />
<br />
=== 文件系统挂载 ===<br />
<br />
默认行为是:在启动一个需要挂载特定分区的服务之前,系统自动检查并挂载分区。{{ic|/etc/fstab}} 中设定的网络文件系统(如 [[NFS (简体中文)|NFS]]、[[Samba (简体中文)|Samba]])无需配置即可正常工作,systemd 将确保网络文件系统在网络链接就绪后挂载。<br />
<br />
详情参阅:{{ic|man 5 systemd.mount}}。<br />
<br />
==== 自动挂载 ====<br />
<br />
* 如果 {{ic|/home}} 分区较大,可以让不依赖 {{ic|/home}} 分区的服务先启动。把下面的参数添加到 {{ic|/etc/fstab}} 文件中 {{ic|/home}} 项目的参数部分即可:<br />
<br />
noauto,x-systemd.automount<br />
<br />
这样 {{ic|/home}} 分区只有需要访问时才会被挂载。内核会缓存所有的文件操作,直到 {{ic|/home}} 分区准备完成。<br />
<br />
{{注意|这样做会使 {{ic|/home}} 的文件系统类型被识别为 {{ic|autofs}},造成 [[mlocate]] 查询时忽略该目录。实际加速效果因配置而异,所以请自己权衡是否需要。}}<br />
<br />
* 挂载远程文件系统也是同理。如果你仅想在需要的时候才挂载,也可以添加 {{ic|noauto,x-systemd.automount}} 参数。另外,可以设置 {{ic|1=x-systemd.device-timeout=#}} 参数,设置超时时间,以防止网络资源不能访问的时候浪费时间。<br />
<br />
* 如果你的加密文件系统需要密钥,则需要添加 {{ic|noauto}} 参数到 {{ic|/etc/crypttab}} 文件中的对应位置。systemd 开机的时候就不会打开这个加密设备,会一直等待到设备被访问时再使用密钥文件挂载。比如在使用加密RAID设备的时候可以节省一定的时间,因为 systemd 不必等到设备可用后才能访问。例如:<br />
<br />
{{hc|/etc/crypttab|<br />
data /dev/md0 /root/key noauto}}<br />
<br />
==== LVM ====<br />
<br />
如果装有不通过 [[Mkinitcpio (简体中文)|initramfs]] 激活的[[LVM (简体中文)|LVM]]卷,则需启动 {{ic|lvm-monitoring}} 服务(由 {{pkg|lvm2}} 软件包提供):<br />
<br />
# systemctl enable lvm-monitoring<br />
<br />
=== ACPI 电源管理 ===<br />
'''systemd''' 能够处理某些电源相关的 [[Wikipedia:Advanced_Configuration_and_Power_Interface|ACPI]] 事件,通过 {{ic|/etc/systemd/logind.conf}} 的下列选项配置:<br />
* {{ic|HandlePowerKey}}:按下电源键后的动作<br />
* {{ic|HandleSleepKey}}:按下挂起键后的动作<br />
* {{ic|HandleHibernateKey}}: 按下休眠键后的动作<br />
* {{ic|HandleLidSwitch}}:合上笔记本盖后待机<br />
<br />
动作可以是 {{ic|ignore}}、{{ic|poweroff}}、{{ic|reboot}}、{{ic|halt}}、{{ic|suspend}}、{{ic|hibernate}}、{{ic|hybrid-sleep}}、{{ic|lock}} 或 {{ic|kexec}}。<br />
<br />
系统默认设置为:<br />
HandlePowerKey=poweroff<br />
HandleSuspendKey=suspend<br />
HandleHibernateKey=hibernate<br />
HandleLidSwitch=suspend<br />
<br />
不用图形界面、或者使用 [[i3 (简体中文)|i3]]、[[awesome (简体中文)|awesome]] 这样简单的桌面管理器时,systemd 可以替代 [[acpid (简体中文)|acpid]] 处理 ACPI 事件。<br />
<br />
{{注意|运行 {{ic|systemctl restart systemd-logind}},使上述更改立即生效。}}<br />
<br />
{{注意|systemd 无法处理交流电源和电池 ACPI 事件,所以还得使用 [[Laptop Mode Tools (简体中文)|Laptop Mode Tools]] 或 [[acpid (简体中文)|acpid]] 工具。}}<br />
<br />
在当前版本的 systemd 中,这些 {{ic|Handle}} 选项将会被应用到整个系统当中,除非它们被别的程序——例如某个桌面环境中的电源管理器——给“阻止”(inhibited)。如果其它的程序没有阻止这些 {{ic|Handle}} ,你可能会先被 systemd 挂起你的系统,然后当系统被唤醒之后,电源管理器又会再次将系统挂起。<br />
<br />
{{警告|目前只有[[GNOME]] 和 [[KDE]] 支持 "inhibited" 命令。在其它的桌面管理器同样实现该功能之前,如果你想使用[[Xfce]]、[[acpid]] 或者其它程序来管理 ACPI 事件,你需要把 {{ic|Handle}} 选项设置为 {{ic|ignore}}。}}<br />
<br />
{{注意|除了内核默认的待机支持后端(用于处理待机/休眠),systemd 也可以使用其他后端(比如 [[Uswsusp]] 或 [[TuxOnIce]])。}}<br />
<br />
要令 {{ic|systemctl hibernate}} 工作,需要按照[[Pm-utils#Hibernation_.28suspend2disk.29|休眠]]和 [[Pm-utils#Mkinitcpio_Resume_Hook|mkinitcpio 唤醒扩展]]的设置步骤进行操作。(不必安装 {{ic|pm-utils}})<br />
<br />
==== 休眠时执行的脚本 ====<br />
<br />
使用 {{ic|systemctl suspend}}、{{ic|systemctl hibernate}} 或 {{ic|systemctl hybrid-sleep}} 命令执行待机/休眠时,systemd 不会调用 [[pm-utils (简体中文)|pm-utils]]。[[pm-utils (简体中文)|pm-utils]] 的钩子扩展(hook)——包括 [[pm-utils (简体中文)#建立你自己的钩子|自定义钩子]]——会失效。不过,systemd 提供了两种类似的待机/休眠时执行脚本的机制。<br />
<br />
===== 使用服务文件 =====<br />
<br />
可以将服务文件附在 suspend.target、hibernate.target 或 sleep.target 中,这样就能在待机/休眠前后执行某些操作。用户级操作和root/系统级操作应该使用不同的服务文件。要启用用户级服务文件,使用 {{ic|# systemctl enable suspend@<用户名> && systemctl enable resume@<用户名>}}。例如:<br />
<br />
{{hc|/etc/systemd/system/suspend@.service|2=<nowiki><br />
[Unit]<br />
Description=User suspend actions<br />
Before=sleep.target<br />
<br />
[Service]<br />
User=%I<br />
Type=forking<br />
Environment=DISPLAY=:0<br />
ExecStartPre= -/usr/bin/pkill -u %u unison ; /usr/local/bin/music.sh stop ; /usr/bin/mysql -e 'slave stop'<br />
ExecStart=/usr/bin/sflock<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
{{hc|/etc/systemd/system/resume@.service|2=<nowiki><br />
[Unit]<br />
Description=User resume actions<br />
After=suspend.target<br />
<br />
[Service]<br />
User=%I<br />
Type=simple<br />
ExecStartPre=/usr/local/bin/ssh-connect.sh<br />
ExecStart=/usr/bin/mysql -e 'slave start'<br />
<br />
[Install]<br />
WantedBy=suspend.target</nowiki>}}<br />
<br />
至于root/系统级服务(使用 {{ic|# systemctl enable root-suspend}} 激活):<br />
<br />
{{hc|/etc/systemd/system/root-resume.service|2=<nowiki><br />
[Unit]<br />
Description=Local system resume actions<br />
After=suspend.target<br />
<br />
[Service]<br />
Type=simple<br />
ExecStart=/usr/bin/systemctl restart mnt-media.automount<br />
<br />
[Install]<br />
WantedBy=suspend.target</nowiki>}}<br />
<br />
{{hc|/etc/systemd/system/root-suspend.service|2=<nowiki><br />
[Unit]<br />
Description=Local system suspend actions<br />
Before=sleep.target<br />
<br />
[Service]<br />
Type=simple<br />
ExecStart=-/usr/bin/pkill sshfs<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
上述服务文件的一些解释(详见 {{ic|man systemd.service}}):<br />
* 如果设置 {{ic|1=<nowiki>Type=OneShot</nowiki>}},那么可以使用多个 {{ic|1=<nowiki>ExecStart=</nowiki>}} 参数。否则只能写一个,替代方案是在 {{ic|ExecStartPre}} 中添加命令,或使用分号分隔不同命令(见第一个例子,分号前后的空格都是'''必须'''的)。 <br />
* 若命令前加上一个“-”(半角减号),则命令返回非零值时会被忽略、当作正常执行处理。<br />
* 有关调试,最好的方法是用 {{ic|journalctl}} 查看日志。<br />
<br />
===== 合并待机和唤醒服务文件 =====<br />
<br />
利用下面这种自定义的一体化待机和唤醒服务,使用单一的钩子扩展即可对不同操作(待机/休眠/混合休眠)的不同阶段(进入/唤醒)进行控制。<br />
<br />
例子和解释:<br />
<br />
{{hc|/etc/systemd/system/wicd-sleep.service|2=<nowiki><br />
[Unit]<br />
Description=Wicd sleep hook<br />
Before=sleep.target<br />
StopWhenUnneeded=yes<br />
<br />
[Service]<br />
Type=oneshot<br />
RemainAfterExit=yes<br />
ExecStart=-/usr/share/wicd/daemon/suspend.py<br />
ExecStop=-/usr/share/wicd/daemon/autoconnect.py<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
* {{ic|1=<nowiki>RemainAfterExit=yes</nowiki>}}:服务启动后,除非显式地停止,否则就认为是活动的。<br />
* {{ic|1=<nowiki>StopWhenUnneeded=yes</nowiki>}}:服务活动时,如果无其他服务依赖该服务,就停止它。在本例中,该服务会在 sleep.target 停止后停止活动。<br />
* 由于 sleep.target 会被 suspend.target、hibernate.target、hybrid-sleep.target 调用,且 sleep.target 本身设置了 StopWhenUnneeded,该服务文件对以上各种操作都是有效的。<br />
<br />
===== 使用 /usr/lib/systemd/system-sleep =====<br />
<br />
systemd 在待机/休眠时执行 {{ic|/usr/lib/systemd/system-sleep/}} 里的所有脚本,传递下面两个参数:<br />
<br />
* 参数1:若是准备进入待机/休眠状态,则为 {{ic|pre}};唤醒时为 {{ic|post}}。<br />
* 参数2:事件名称,{{ic|suspend}},{{ic|hibernate}} 或 {{ic|hybrid-sleep}}。<br />
<br />
systemd 会同时执行所有脚本,而不是像 [[pm-utils (简体中文)|pm-utils]] 那样顺序执行。<br />
<br />
脚本输出会记录在相关服务({{ic|systemd-suspend.service}}、{{ic|systemd-hibernate.service}} 或 {{ic|systemd-hybrid-sleep.service}})中。通过[[#日志|日志]]查看:<br />
# journalctl -b -u systemd-suspend<br />
<br />
{{注意|除了使用自定义脚本,还可以利用 {{ic|sleep.target}}、{{ic|suspend.target}}、{{ic|hibernate.target}} 或 {{ic|hybrid-sleep.target}} 来为单元(unit)添加睡眠状态策略。}}<br />
<br />
脚本示范:<br />
{{hc|/usr/lib/systemd/system-sleep/example.sh|<br />
#!/bin/sh<br />
case $1/$2 in<br />
pre/*)<br />
echo "进入 $2 状态..."<br />
;;<br />
post/*)<br />
echo "从 $2 状态唤醒..."<br />
;;<br />
esac}}<br />
<br />
记得添加可执行权限: <br />
# chmod a+x /usr/lib/systemd/system-sleep/example.sh<br />
<br />
详情参见 {{ic|man 7 systemd.special}} 和 {{ic|man 8 systemd-sleep}}。<br />
<br />
=== 临时文件 ===<br />
{{ic|/usr/lib/tmpfiles.d/}} 和 {{ic|/etc/tmpfiles.d/}} 中的文件描述了 systemd-tmpfiles 如何创建、清理、删除临时文件和目录,这些文件和目录通常存放在 {{ic|/run}} 和 {{ic|/tmp}} 中。配置文件名称为 {{ic|/etc/tmpfiles.d/<program>.conf}}。此处的配置能覆盖 {{ic|/usr/lib/tmpfiles.d/}} 目录中的同名配置。<br />
<br />
临时文件通常和服务文件同时提供,以生成守护进程需要的文件和目录。例如 [[Samba (简体中文)|Samba]] 服务需要目录 {{ic|/run/samba}} 存在并设置正确的权限位,就象这样:<br />
{{hc|/usr/lib/tmpfiles.d/samba.conf|<br />
D /run/samba 0755 root root<br />
}}<br />
<br />
此外,临时文件还可以用来在开机时向特定文件写入某些内容。比如,要禁止系统从USB设备唤醒,利用旧的 {{ic|/etc/rc.local}} 可以用 {{ic|echo USBE > /proc/acpi/wakeup}},而现在可以这么做:<br />
{{hc|/etc/tmpfiles.d/disable-usb-wake.conf|<br />
w /proc/acpi/wakeup - - - - USBE<br />
}}<br />
<br />
详情参见 {{ic|man 5 tmpfiles.d}}。<br />
<br />
{{注意|该方法不能向 {{ic|/sys}} 中的配置文件添加参数,因为 {{ic|systemd-tmpfiles-setup}} 有可能在相关模块加载前运行。这种情况下,需要首先通过 {{ic|modinfo <模块名>}} 确认需要的参数,并在 [[Kernel modules (简体中文)#选项|{{ic|/etc/modprobe.d}} 下的一个文件]]中设置改参数。另外,还可以使用 [[udev (简体中文)#udev规则|udev 规则]],在设备就绪时设置相应属性。}}<br />
<br />
=== 单元(unit) ===<br />
一个单元配置文件可以描述如下内容之一:系统服务、socket、系统设备、挂载点、交换分区/文件、启动目标(target)、文件系统路径、由 systemd 管理的计时器。文件格式受 .desktop 文件启发,而最初起源是 Windows 下的 .ini 文件。<br />
<br />
详情参阅 {{ic|man 5 systemd.unit}}.<br />
<br />
== 从 initscripts 迁移到 systemd ==<br />
<br />
=== initscripts 模拟 ===<br />
<br />
{{Pkg|initscripts}} 软件包提供了 systemd 和 Arch 传统配置的整合。当同时安装了{{Pkg|initscripts}}和systemd时,若以systemd启动系统,systemd将执行以下操作:<br />
<br />
# 解析{{ic|/etc/rc.conf}}中的{{ic|DAEMONS}}数组,并在系统启动时,启动所有列出的守护进程<br />
# 在启动过程中执行{{ic|/etc/rc.local}}<br />
# 在系统关闭过程中执行{{ic|/etc/rc.local.shutdown}}<br />
<br />
Initscripts模拟仅仅是作为用户迁移到systemd时的过渡措施,这一方案最终将'''不再可行'''。原生的systemd并不依赖于{{ic|rc.conf}}文件的集中式配置,因此推荐使用[[#原生 systemd 配置文件|原生的systemd配置文件]]。原生systemd配置文件比{{ic|/etc/rc.conf}}具有更高优先级。<br />
<br />
{{注意|替换{{ic|/etc/rc.local}}的推荐方式是针对系统启动时要运行的程序编写自定义的service文件,详见相关[[#自己编写_.service_文件|章节]].}}<br />
<br />
{{注意|如果你曾经在{{ic|/etc/inittab}}中禁用了以{{keypress|Ctrl+Alt+Del}}重启系统,则需要以root执行{{ic|systemctl mask ctrl-alt-del.target}}以使其对systemd重新生效。}}<br />
<br />
{{警告|如果同时安装 systemd(197-4 及后续版本)和 initscripts,那么 {{ic|/etc/rc.local}} 脚本会导致启动过程无法结束(原因是 getty@tty1.service 自 197-4 版本起不再等待 rc-local.service 执行,getty 无法启动)。请删除或重命名 {{ic|/etc/rc.local}}。}}<br />
<br />
=== 迁移 DAEMONS 列表 ===<br />
<br />
若要用纯 systemd 环境,则应当删除 {{ic|/etc/rc.conf}} 文件,并完全通过 {{ic|systemctl}} 命令来启动服务。对于 {{ic|DAEMONS}} 列表里面的每个 {{ic|<服务名>}},现在可以用以下命令设置成自动启动:<br />
<br />
# systemctl enable <服务名>.service<br />
<br />
{{小贴士|[[Daemons List|这里]]有一份 initscripts 和 systemd 下常见服务的对应列表。}}<br />
<br />
如果提示 {{ic|<service_name>.service}} 不存在:<br />
<br />
* 很有可能是 systemd 用了不同的服务名。比如 {{ic|crond}} 服务替换成 {{ic|cronie.service}};{{ic|alsa}} 服务替换成了 {{ic|alsa-store.service}} 和 {{ic|alsa-restore.service}} 两个服务。另一个值得注意的是 {{ic|network}} 服务,它被替换为一整套服务文件(详情参见 [[Configuring_Network_(简体中文)]])。<br />
* 另外,有些程序尚未提供 .service 文件,还得继续用 {{ic|rc.conf}} 来配置开机自动启动。<br />
<br />
{{小贴士|用下面这个命令来查看已安装软件包包含的服务文件:<br />
$ pacman -Ql cronie<br />
[...]<br />
cronie /etc/rc.d/crond # DAEMONS 列表中使用的 initscript 服务 (原生 systemd 配置中不再使用)<br />
[...]<br />
cronie /usr/lib/systemd/system/cronie.service # 原生 systemd 服务文件<br />
[...]<br />
}}<br />
<br />
* 有些服务不需要用户明确配置自启动。比如安装 {{ic|dbus-core}} 软件包后 {{ic|dbus.service}} 就会自动被配置为自启动。而 {{ic|alsa-store.service}} 和 {{ic|alsa-restore.service}} 也会由 systemd 自动启动。可以用 {{ic|systemctl list-unit-files}} 命令来查看服务列表并检查运行状态。<br />
<br />
== systemd 基本工具 ==<br />
<br />
检视和控制systemd的主要命令是{{ic|systemctl}}。该命令可用于查看系统状态和管理系统及服务。详见{{ic|man 1 systemctl}}。<br />
<br />
{{小贴士|在 {{ic|systemctl}} 参数中添加 {{ic|-H <用户名>@<主机名>}} 可以实现对其他机器的远程控制。该过程使用 [[SSH (简体中文)|SSH]] 链接。}}<br />
<br />
{{注意|{{ic|systemadm}} 是 systemd 的官方图形前端。由 [[AUR (简体中文)|AUR]] 中的软件包 {{AUR|systemd-ui-git}} 提供。}}<br />
<br />
=== 分析系统状态 ===<br />
<br />
输出激活的单元:<br />
<br />
$ systemctl<br />
<br />
以下命令等效:<br />
<br />
$ systemctl list-units<br />
<br />
输出运行失败的单元:<br />
<br />
$ systemctl --failed<br />
<br />
所有可用的单元文件存放在 {{ic|/usr/lib/systemd/system/}} 和 {{ic|/etc/systemd/system/}} 目录(后者优先级更高)。查看所有已安装服务:<br />
$ systemctl list-unit-files<br />
<br />
=== 使用单元 ===<br />
一个单元可以是系统服务({{ic|.service}})、挂载点({{ic|.mount}})、sockets({{ic|.sockets}})。<br />
<br />
使用 {{ic|systemctl}} 控制单元时,通常需要使用单元文件的全名,包括扩展名(例如 {{ic|sshd.service}})。但是有些单元可以在{{ic|systemctl}}中使用简写方式。<br />
* 如果无扩展名,systemctl 默认把扩展名当作 {{ic|.service}}。例如 {{ic|netcfg}} 和 {{ic|netcfg.service}} 是等价的。<br />
* 挂载点会自动转化为相应的 {{ic|.mount}} 单元。例如 {{ic|/home}} 等价于 {{ic|home.mount}}。<br />
* 设备会自动转化为相应的 {{ic|.device}} 单元,所以 {{ic|/dev/sda2}} 等价于 {{ic|dev-sda2.device}}。<br />
<br />
立即激活单元:<br />
<br />
# systemctl start <单元><br />
<br />
立即停止单元:<br />
# systemctl stop <单元><br />
<br />
重启单元:<br />
<br />
# systemctl restart <单元><br />
<br />
命令单元重新读取配置:<br />
<br />
# systemctl reload <单元><br />
<br />
输出单元运行状态:<br />
<br />
$ systemctl status <单元><br />
<br />
检查单元是否配置为自动启动:<br />
$ systemctl is-enabled <单元><br />
<br />
开机自动激活单元:<br />
<br />
# systemctl enable <单元><br />
<br />
{{注意|如果服务没有{{ic|Install}}段落,一般意味着应该通过其它服务自动调用它们。如果真的需要手动安装,可以直接连接服务,如下(将{{ic|foo}}替换为真实的服务名):<br />
{{bc|# ln -s /usr/lib/systemd/system/foo.service /etc/systemd/system/graphical.target.wants/}}<br />
}}<br />
<br />
取消开机自动激活单元:<br />
<br />
# systemctl disable <单元><br />
<br />
显示单元的手册页(必须由单元文件提供):<br />
<br />
# systemctl help <单元><br />
<br />
重新载入 systemd,扫描新的或有变动的单元:<br />
# systemctl daemon-reload<br />
<br />
=== 电源管理 ===<br />
<br />
安装 {{ic|polkit}} 后才可使用电源管理。<br />
<br />
如果你正登录在一个本地的{{ic|systemd-logind}}用户会话,且当前没有其它活动的会话,那么以下命令无需root权限即可执行。否则(例如,当前有另一个用户登录在某个tty),systemd 将会自动请求输入root密码。<br />
<br />
重启:<br />
<br />
$ systemctl reboot<br />
<br />
退出系统并停止电源:<br />
<br />
$ systemctl poweroff<br />
<br />
待机:<br />
<br />
$ systemctl suspend<br />
<br />
休眠:<br />
<br />
$ systemctl hibernate<br />
<br />
混合休眠模式(同时休眠到硬盘并待机):<br />
<br />
$ systemctl hybrid-sleep<br />
<br />
== 启动登录管理器 ==<br />
<br />
通过启动[[Display Manager (简体中文)|登录管理器]](或称显示管理器),即可进行图形界面登录。<br />
要启用图形界面登录,运行适当的[[Display Manager (简体中文)|登录管理器]](或称显示管理器)即可。目前,Arch 提供了 [[GDM (简体中文)|GDM]]、[[KDM (简体中文)|KDM]]、[[SLiM (简体中文)|SLiM]]、[[XDM (简体中文)|XDM]]、[[LXDM (简体中文)|LXDM]]、[[LightDM (简体中文)|LightDM]] 和 {{AUR|SDDM}} 的 systemd 服务文件。以 KDM 为例,配置开机启动:<br />
# systemctl enable kdm.service<br />
<br />
执行上述命令后,登录管理器应当能正常工作了。如果不是的话,很可能是因为你修改了{{ic|default.target}}。默认情况应当如下:<br />
<br />
{{hc|# ls -l /etc/systemd/system/default.target|/etc/systemd/system/default.target -> /usr/lib/systemd/system/graphical.target}}<br />
<br />
删除被修改的 {{ic|default.target}} 即可,systemd 会自动使用默认配置(即 {{ic|graphical.target}}):<br />
<br />
# rm /etc/systemd/system/default.target<br />
<br />
=== 使用 systemd-logind ===<br />
<br />
{{注意|自 2012-10-30 起, [[ConsoleKit]] 已被 [https://www.archlinux.org/news/consolekit-replaced-by-logind/ systemd-logind 取代] ,作为登录到桌面环境的默认方式。}}<br />
<br />
可使用 {{ic|loginctl}} 来查看用户会话的状态。所有 [[PolicyKit]] 操作,如挂起系统、挂载外部驱动器,都无需配置即可使用。<br />
<br />
$ loginctl show-session $XDG_SESSION_ID<br />
<br />
== 自己编写 .service 文件 ==<br />
''参见:[[Systemd/Services]]''<br />
<br />
=== 处理依赖关系 ===<br />
<br />
使用systemd时,可通过正确编写单元配置文件来解决其依赖关系。典型的情况是,单元{{ic|A}}要求单元{{ic|B}}在{{ic|A}}启动之前运行。在此情况下,向单元{{ic|A}}配置文件中的 {{ic|[Unit]}} 段添加 {{ic|1=Requires=B}} 和 {{ic|1=After=B}} 即可。若此依赖关系是可选的,可添加 {{ic|1=Wants=B}} 和 {{ic|1=After=B}}。请注意 {{ic|1=Wants=}} 和 {{ic|1=Requires=}} 并不意味着 {{ic|1=After=}},即如果 {{ic|1=After=}} 选项没有制定,这两个单元将被并行启动。<br />
<br />
依赖关系通常被用在服务(service)而不是[[#目标(target)|目标(target)]]上。例如, {{ic|network.target}} 一般会被某个配置网络接口的服务引入,所以,将自定义的单元排在该服务之后即可,因为 {{ic|network.target}} 已经启动。<br />
<br />
=== 启动方式 ===<br />
<br />
编写自定义的service文件时,可以选择几种不同的服务启动方式。启动方式可通过配置文件 {{ic|[Service]}} 段中的 {{ic|1=Type=}} 参数进行设置。具体的参数说明请参阅 {{ic|man systemd.service}} 。<br />
<br />
* {{ic|1=Type=simple}}(默认值):systemd认为该服务将立即启动。服务进程不会fork。如果该服务要启动其他服务,不要使用此类型启动,除非该服务是socket激活型。<br />
* {{ic|1=Type=forking}}:systemd认为当该服务进程fork,且父进程退出后服务启动成功。对于常规的守护进程(daemon),除非你确定此启动方式无法满足需求,使用此类型启动即可。使用此启动类型应同时指定 {{ic|1=PIDFile=}},以便systemd能够跟踪服务的主进程。<br />
* {{ic|1=Type=oneshot}}:这一选项适用于只执行一项任务随后退出的服务。你可能需要同时设置 {{ic|1=RemainAfterExit=yes}} 使得 systemd 在服务进程退出之后仍然认为服务处于激活状态。<br />
* {{ic|1=Type=notify}}:与 {{ic|1=Type=simple}} 相同,但约定服务会在就绪后向 systemd 发送一个信号。这一通知的实现由 {{ic|libsystemd-daemon.so}} 提供。<br />
* {{ic|1=Type=dbus}}:若以此方式启动,当指定的 {{ic|BusName}} 出现在DBus系统总线上时,systemd认为服务就绪。<br />
<br />
=== 修改现存单元文件 ===<br />
要更改由软件包提供的单元文件,先创建名为 {{ic|/etc/systemd/system/<单元名>.d/}} 的目录(如 {{ic|/etc/systemd/system/httpd.service.d/}}),然后放入 {{ic|*.conf}} 文件,其中可以添加或重置参数。这里设置的参数优先级高于原来的单元文件。例如,如果想添加一个额外的依赖,创建这么一个文件即可:<br />
<br />
{{hc|/etc/systemd/system/<unit>.d/customdependency.conf|2=<br />
[Unit]<br />
Requires=<新依赖><br />
After=<新依赖>}}<br />
<br />
然后运行以下命令使更改生效:<br />
<br />
# systemctl daemon-reload<br />
# systemctl restart <单元><br />
<br />
此外,把旧的单元文件从 {{ic|/usr/lib/systemd/system/}} 复制到 {{ic|/etc/systemd/system/}},然后进行修改,也可以达到同样效果。在 {{ic|/etc/systemd/system/}} 目录中的单元文件的优先级总是高于 {{ic|/usr/lib/systemd/system/}} 目录中的同名单元文件。注意,当 {{ic|/usr/lib/}} 中的单元文件因软件包升级变更时,{{ic|/etc/}} 中自定义的单元文件不会同步更新。此外,你还得执行 {{ic|systemctl reenable <unit>}},手动重新启用该单元。因此,建议使用前面一种利用 {{ic|*.conf}} 的方法。<br />
<br />
{{小贴士|可以用 {{ic|systemd-delta}} 命令来查看哪些单元文件被覆盖、哪些被修改。}}<br />
<br />
=== 单元配置文件的 vim 语法高亮支持 ===<br />
<br />
可从 [[Official Repositories|官方仓库]] 安装 {{Pkg|vim-systemd}} 软件包,使 unit 配置文件在 [[Vim]] 下支持语法高亮。<br />
<br />
== 目标(target) ==<br />
启动级别(runlevel)是一个旧的概念。现在,systemd 引入了一个和启动级别功能相似又不同的概念——目标(target)。不像数字表示的启动级别,每个''目标''都有名字和独特的功能,并且能同时启用多个。一些''目标''继承其他''目标''的服务,并启动新服务。systemd 提供了一些模仿 sysvinit 启动级别的''目标'',仍可以使用旧的 {{ic|telinit 启动级别}} 命令切换。<br />
<br />
=== 获取当前目标 ===<br />
不要使用 {{ic|runlevel}} 命令了:<br />
$ systemctl list-units --type=target<br />
<br />
=== 创建新目标 ===<br />
在 Fedora 中,启动级别 0、1、3、5、6 都被赋予特定用途,并且都对应一个 systemd 的''目标''。然而,没有什么很好的移植用户定义的启动级别(2、4)的方法。要实现类似功能,可以以原有的启动级别为基础,创建一个新的''目标'' {{ic|/etc/systemd/system/<新目标>}}(可以参考 {{ic|/usr/lib/systemd/system/graphical.target}}),创建 {{ic|/etc/systemd/system/<新目标>.wants}} 目录,向其中加入额外服务的链接(指向 {{ic|/usr/lib/systemd/system/}} 中的单元文件)。<br />
<br />
=== 目标表 ===<br />
{| border="1"<br />
!SysV 启动级别!!Systemd 目标!!注释<br />
|-<br />
| 0 || runlevel0.target, poweroff.target || 中断系统(halt)<br />
|-<br />
| 1, s, single || runlevel1.target, rescue.target || 单用户模式<br />
|-<br />
| 2, 4 || runlevel2.target, runlevel4.target, multi-user.target || 用户自定义启动级别,通常识别为级别3。<br />
|-<br />
| 3 || runlevel3.target, multi-user.target || 多用户,无图形界面。用户可以通过终端或网络登录。<br />
|-<br />
| 5 || runlevel5.target, graphical.target || 多用户,图形界面。继承级别3的服务,并启动图形界面服务。<br />
|-<br />
| 6 || runlevel6.target, reboot.target || 重启<br />
|-<br />
| emergency || emergency.target || 急救模式(Emergency shell)<br />
|-<br />
|}<br />
<br />
=== 切换启动级别/目标 ===<br />
systemd 中,启动级别通过“目标单元”访问。通过如下命令切换:<br />
# systemctl isolate graphical.target<br />
该命令对下次启动无影响。等价于{{ic|telinit 3}} 或 {{ic|telinit 5}}。<br />
<br />
=== 修改默认启动级别/目标 ===<br />
开机启动进的目标是 {{ic|default.target}},默认链接到 {{ic|graphical.target}} (大致相当于原来的启动级别5)。可以通过[[Kernel parameters (简体中文)|内核参数]]更改默认启动级别:<br />
<br />
{{小贴士|可以省略扩展名 {{ic|.target}}。}}<br />
<br />
* {{ic|1=systemd.unit=multi-user.target}} (大致相当于级别3)<br />
* {{ic|1=systemd.unit=rescue.target}} (大致相当于级别1)<br />
<br />
另一个方法是修改 {{ic|default.target}}。可以通过 {{ic|systemctl}} 修改它:<br />
<br />
# systemctl enable multi-user.target<br />
<br />
命令执行情况由 {{ic|systemctl}} 显示:链接 {{ic|/etc/systemd/system/default.target}} 被创建,指向新的默认启动级别。该方法当且仅当目标配置文件中有以下内容时有效:<br />
<br />
[Install]<br />
Alias=default.target<br />
<br />
目前,{{ic|multi-user.target}}、{{ic|graphical.target}} 都包含这段内容。<br />
<br />
== 日志 ==<br />
systemd 提供了自己日志系统(logging system),称为 journal. 使用 systemd 日志,无需额外安装日志服务(syslog)。读取日志的命令:<br />
# journalctl<br />
<br />
默认情况下(当 {{ic|1=Storage=}} 在文件 {{ic|/etc/systemd/journald.conf}} 中被设置为 {{ic|auto}}),日志记录将被写入 {{ic|/var/log/journal/}}。该目录是 {{pkg|systemd}} 软件包的一部分。若被删除,systemd '''不会'''自动创建它,直到下次升级软件包时重建该目录。如果该目录缺失,systemd 会将日志记录写入 {{ic|/run/systemd/journal}}。这意味着,系统重启后日志将丢失。<br />
<br />
=== 过滤输出 ===<br />
<br />
{{ic|journalctl}}可以根据特定字段过滤输出,例如:<br />
<br />
显示本次启动后的所有日志:<br />
<br />
# journalctl -b<br />
<br />
不过,一般大家更关心的不是本次启动后的日志,而是上次启动时的(例如,刚刚系统崩溃了)。目前还没有这项功能,正在 [http://comments.gmane.org/gmane.comp.sysutils.systemd.devel/6608 systemd-devel@lists.freedesktop.org] 讨论中。<br />
<br />
目前的折中方案是:<br />
<br />
# journalctl --since=today | tac | sed -n '/-- Reboot --/{n;:r;/-- Reboot --/q;p;n;b r}' | tac<br />
<br />
以上命令输出本日内的所有启动信息。但要注意,如果日志很多,该命令执行时间会比较漫长。<br />
<br />
动态跟踪最新信息:<br />
# journalctl -f<br />
<br />
显示特定程序的所有消息:<br />
{{bc|# journalctl /usr/lib/systemd/systemd}}<br />
<br />
显示特定进程的所有消息:<br />
{{bc|1=# journalctl _PID=1}}<br />
<br />
显示指定单元的所有消息:<br />
# journalctl -u netcfg<br />
<br />
详情参阅{{ic|man journalctl}}、{{ic|man systemd.journal-fields}},以及 Lennert 的这篇[http://0pointer.de/blog/projects/journalctl.html 博文]。<br />
<br />
=== 日志大小限制 ===<br />
<br />
如果按上面的操作保留日志的话,默认日志最大限制为所在文件系统容量的 10%,即:如果 {{ic|/var/log/journal}} 储存在 50GiB 的根分区中,那么日志最多存储 5GiB 数据。可以修改 {{ic|/etc/systemd/journald.conf}} 中的 {{ic|SystemMaxUse}} 来指定该最大限制。如限制日志最大 50MiB:<br />
<br />
SystemMaxUse=50M<br />
<br />
详情参见 {{ic|man journald.conf}}.<br />
<br />
=== 配合 syslog 使用 ===<br />
systemd 提供了 socket {{ic|/run/systemd/journal/syslog}},以兼容传统日志服务。所有系统信息都会被传入。要使传统日志服务工作,需要让服务链接该 socket,而非 {{ic|/dev/log}}([http://lwn.net/Articles/474968/ 官方说明])。Arch 软件仓库中的 {{pkg|syslog-ng}} 已经包含了需要的配置。<br />
<br />
设置开机启动 syslog-ng:<br />
# systemctl enable syslog-ng<br />
<br />
[http://0pointer.de/blog/projects/ 这里]有一份很不错的 {{ic|journalctl}} 指南。<br />
<br />
== 优化 ==<br />
<br />
参阅:[[Improve Boot Performance (简体中文)]]。<br />
<br />
=== 启动过程分析 ===<br />
systemd 提供了一个分析启动过程的工具 —— {{ic|systemd-analyze}}。可以用它看看哪些单元拖慢了开机过程,并据此进行优化。<br />
<br />
查看开机过程在内核/用户空间消耗的时间:<br />
$ systemd-analyze<br />
{{小贴士|<br />
* 如果在 {{ic|/etc/mkinitcpio.conf}} 的 {{ic|HOOKS}} 数组添加 {{ic|timestamp}},并重新生成启动内存镜像({{ic|mkinitcpio -p linux}}),{{ic|systemd-analyze}} 还可以显示处理该启动镜像花费的时间。<br />
* 如果使用 [[UEFI]] 引导,且启动引导器实现了 systemd 的 [http://www.freedesktop.org/wiki/Software/systemd/BootLoaderInterface Boot Loader Interface](目前只有 [[Gummiboot]] 实现了),'''systemd-analyze''' 还可以显示 EFI 固件和启动引导器自身花费的时间。}}<br />
<br />
按照耗费时间顺序,输出启动每个单元耗费的时间:<br />
$ systemd-analyze blame<br />
<br />
生成类似于 [[Bootchart (简体中文)|bootchart]] 的开机过程图表:<br />
$ systemd-analyze plot > plot.svg<br />
<br />
==== 使用 systemd-bootchart ====<br />
<br />
自2012年10月17日,bootchart 工具已经合并进 systemd 中,使用方法和原来的 bootchart 大同小异,添加下列内容到内核参数即可:<br />
<br />
initcall_debug printk.time=y init=/usr/lib/systemd/systemd-bootchart<br />
<br />
==== 使用 bootchart2 ====<br />
<br />
由于没有办法在内核参数设置两个 init,所以不能使用源里的 bootchart。不过,[[AUR (简体中文)|AUR]] 软件包 {{AUR|bootchart2-git}} 提供了一个的 systemd 服务,这样就可以和 systemd 一起使用。安装后启用即可:<br />
{{bc|# systemctl enable bootchart.service}}<br />
详情参阅 [https://github.com/mmeeks/bootchart bootchart 文档]。<br />
<br />
=== 预读 ===<br />
systemd 自己实现了一个 readahead,可以用来提高开机效率。不过,效果会因内核版本和硬件情况而不同(极少数情况下还会变慢)。开启 readahead:<br />
<br />
# systemctl enable systemd-readahead-collect.service systemd-readahead-replay.service<br />
<br />
要知道,readahead 的超级牛力只有在重启几次后才会显现。<br />
<br />
== 疑难解答 ==<br />
<br />
=== 关机/重启十分缓慢 ===<br />
<br />
如果关机特别慢(甚至跟死机了一样),很可能是某个拒不退出的服务在作怪。systemd 会等待一段时间,然后再尝试杀死它。请阅读[http://freedesktop.org/wiki/Software/systemd/Debugging#Shutdown_Completes_Eventually 这篇文章],确认你是否是该问题受害者。<br />
<br />
=== 短时进程无日志记录 ===<br />
<br />
若 {{ic|journalctl -u foounit.service}} 没有显示某个短时进程的任何输出,那么改用 PID 试试。例如,若 {{ic|systemd-modules-load.service}} 执行失败,那么先用 {{ic|systemctl status systemd-modules-load}} 查询其 PID(比如是123),然后检索该 PID 相关的日志 {{ic|journalctl -b _PID&#61;123}}。运行时进程的日志元数据(诸如 _SYSTEMD_UNIT 和 _COMM)被乱序收集在 {{ic|/proc}} 目录。要修复该问题,必须修改内核,使其通过套接字连接来提供上述数据,该过程类似于 SCM_CREDENTIALS。<br />
<br />
=== 诊断启动问题 ===<br />
<br />
使用如下内核参数引导:<br />
{{ic|<nowiki>systemd.log_level=debug systemd.log_target=kmsg log_buf_len=1M</nowiki>}}<br />
<br />
更多有关调试的信息,参见[http://freedesktop.org/wiki/Software/systemd/Debugging 该文]。<br />
<br />
=== 禁止在程序崩溃时转储内存 ===<br />
<br />
把 {{ic|/etc/sysctl.d/coredump.conf}} 链接到 {{ic|/dev/null}},然后应用 sysctl 即可([https://bbs.archlinux.org/viewtopic.php?id=154511 来源]):<br />
<br />
# ln -s /dev/null /etc/sysctl.d/coredump.conf<br />
# /usr/lib/systemd/systemd-sysctl<br />
# ulimit -c unlimited<br />
<br />
== 相关资源 ==<br />
<br />
*[http://www.freedesktop.org/wiki/Software/systemd 官方网站]<br />
*[http://0pointer.de/public/systemd-man/ man 手册页]<br />
*[http://freedesktop.org/wiki/Software/systemd/Optimizations systemd 优化]<br />
*[http://www.freedesktop.org/wiki/Software/systemd/FrequentlyAskedQuestions 常见问题 FAQ]<br />
*[http://www.freedesktop.org/wiki/Software/systemd/TipsAndTricks 小技巧]<br />
*[http://0pointer.de/public/systemd-ebook-psankar.pdf systemd for Administrators (PDF)]<br />
*[http://fedoraproject.org/wiki/Systemd Fedora 项目对 systemd 的介绍]<br />
*[http://fedoraproject.org/wiki/How_to_debug_Systemd_problems 如何调试 systemd 故障]<br />
*''The H Open'' 杂志上的[http://www.h-online.com/open/features/Control-Centre-The-systemd-Linux-init-system-1565543.html 两][http://www.h-online.com/open/features/Booting-up-Tools-and-tips-for-systemd-1570630.html 篇]科普文章<br />
*[http://0pointer.de/blog/projects/systemd.html Lennart 的博文]<br />
*[http://0pointer.de/blog/projects/systemd-update.html 更新报告]<br />
*[http://0pointer.de/blog/projects/systemd-update-2.html 更新报告2]<br />
*[http://0pointer.de/blog/projects/systemd-update-3.html 更新报告3]<br />
*[http://0pointer.de/blog/projects/why.html 最新动态]<br />
*[https://fedoraproject.org/wiki/SysVinit_to_Systemd_Cheatsheet/zh Fedora Wiki 上的 SysVinit 和 systemd 命令对比表]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Systemd_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=262813
Systemd (简体中文)
2013-06-15T04:28:13Z
<p>Cuihao: /* 疑难解答 */</p>
<hr />
<div>{{Lowercase title}}<br />
[[Category:Daemons and system services (简体中文)]]<br />
[[Category:Boot process (简体中文)]]<br />
[[ar:Systemd]]<br />
[[en:Systemd]]<br />
[[es:Systemd]]<br />
[[fr:Systemd]]<br />
[[it:Systemd]]<br />
[[ja:Systemd]]<br />
[[ru:Systemd]]<br />
[[zh-TW:Systemd]]<br />
{{Article summary start|概览}}<br />
{{Article summary text|本文介绍 Systemd 的安装和配置。}}<br />
{{Article summary heading|相关页面}}<br />
{{Article summary wiki|Systemd/Services}}<br />
{{Article summary wiki|systemd/User}}<br />
{{Article summary wiki|Systemd FAQ}}<br />
{{Article summary wiki|init Rosetta}}<br />
{{Article summary wiki|Daemons List}}<br />
{{Article summary wiki|udev (简体中文)}}<br />
{{Article summary end}}<br />
{{TranslationStatus (简体中文)|Systemd|2013-03-14|250286}}<br />
摘自[http://freedesktop.org/wiki/Software/systemd 项目主页]:<br />
<br />
'''''systemd''' 是 Linux 下的一款系统和服务管理器,兼容 SysV 和 LSB 的启动脚本。systemd 的特性有:支持并行化任务;同时采用 socket 式与 [[D-Bus (简体中文)|D-Bus]] 总线式激活服务;按需启动守护进程(daemon);利用 Linux 的 [[cgroups|cgroups]] 监视进程;支持快照和系统恢复;维护挂载点和自动挂载点;各服务间基于依赖关系进行精密控制。systemd 完全可以替代 Arch 曾经使用的 [[SysVinit (简体中文)|sysvinit]] 启动系统。''<br />
<br />
{{注意|[https://bbs.archlinux.org/viewtopic.php?pid&#61;1149530#p1149530 这个帖子]详细解释了 Arch 向 systemd 迁移的原因。}}<br />
<br />
另见[[Wikipedia:zh:systemd|维基百科上的介绍]]。<br />
<br />
== 迁移前需考虑 ==<br />
<br />
* 阅读[http://freedesktop.org/wiki/Software/systemd/ 该站],了解 systemd。<br />
* systemd 自己有一套日志('''journal''')系统,用于代替 '''syslog'''。两者也可以共存,参见后面的[[#日志|日志部分]]。<br />
* 虽然 systemd 可以替换 '''cron'''、'''acpid'''、'''xinetd''' 等的部分功能。至少目前还可以继续使用这些服务,无需立即切换。<br />
* 交互式 initscripts 启动脚本在 systemd 中无法工作。例如 '''netcfg-menu''' 无法{{Bug|31377}})在启动时显示。<br />
<br />
== 安装 ==<br />
{{注意|从[https://www.archlinux.org/news/systemd-is-now-the-default-on-new-installations/ 2012-10-13版安装介质]开始,安装程序已经默认安装{{pkg|systemd}} 和 {{pkg|systemd-sysvcompat}}。}}<br />
<br />
{{注意|如果是在 VPS 中使用 Arch,请先阅读:[[Virtual_Private_Server#Moving_your_VPS_from_initscripts_to_systemd]]。}}<br />
<br />
本部分帮助还在使用{{AUR|sysvinit}} 和 initscripts 的用户迁移到 '''systemd'''.<br />
<br />
# 从[[Official_Repositories_(简体中文)|官方软件仓库]]安装 {{pkg|systemd}} 并添加[[Kernel parameters|内核参数]]{{Ic|1=init=/usr/lib/systemd/systemd}}<br />
# 使用 {{ic|systemctl enable <服务名>}} 启用需要的服务(大致相当于以前 {{ic|DAEMONS}} 数组的作用,新的服务名称参见 [[Daemons_List_(简体中文)]])。<br />
# 重启系统,执行命令 {{ic|cat /proc/1/comm}},如果返回{{ic|systemd}},表示 systemd 已经正常启动。<br />
# 确认主机名已经正确设置:{{ic|hostnamectl set-hostname myhostname}}。<br />
# 删除 initscripts 和 sysvinit,并安装{{pkg|systemd-sysvcompat}}.<br />
# (可选)删除{{ic|1=init=/usr/lib/systemd/systemd}}内核参数,现在已经不需要它了。{{pkg|systemd-sysvcompat}} 软件包提供了一个软链接,使 systemd 成为默认 init。<br />
<br />
=== 附加信息 ===<br />
* 如果内核参数中有 {{ic|quiet}},建议在一开始先去掉,以便调试。<br />
{{Accuracy|reason=据有的用户称,systemd 并未对用户组有特殊要求。尽管有很多不常用的用户组,但从 consolekit 切换到 logind 后这些用户组没有发生改变。''(据译者理解:如果你碰到特殊的权限问题,可能仍需要加入这些用户组才能解决。)''}}<br />
* 使用 systemd 的时候'''无需'''将用户加入特殊[[Users and Groups (简体中文)|用户组]](如{{ic|sys}}、{{ic|disk}}、{{ic|lp}}、{{ic|network}}、{{ic|video}}、{{ic|audio}}、{{ic|optical}}、{{ic|storage}}、{{ic|scanner}}、{{ic|power}}等等)。加入这些组反而会有问题,例如audio组会导致程序阻塞软件混声。每个 PAM 登录都拥有一个 logind 会话,它通过[[Wikipedia:Access control list|POSIX ACLs]],赋予本地会话以声音设备访问权限、通过[[udisks]]挂载和卸载移动设备的权限等。<br />
{{警告|如果您准备将用户移出UNIX通用的基础用户组,请三思而后行。某些应用程序,如 '''mplayer''' 和 '''fbida''',需要访问 {{ic|/dev/fb*}} 的权限,因此必须加入 '''video''' 用户组。还有 k3b,需要用户加入 '''optical''' 组以访问旧式光学设备。正确的方法是使用 [https://mailman.archlinux.org/pipermail/arch-general/2012-October/031808.html udev] 获取[https://bbs.archlinux.org/viewtopic.php?pid&#61;1183731#p1183731 访问权限]。}}<br />
* 阅读 [[Network Configuration (简体中文)]],了解如何配置网络。<br />
<br />
== 原生 systemd 配置文件 ==<br />
{{注意|可能需要手动创建某些文件。所有文件的权限都是644,属主 root,属组 root。}}<br />
<br />
=== 虚拟控制台 ===<br />
{{ic|/etc/vconsole.conf}} 文件用来配置虚拟控制台,包括键盘布局和控制台字体:<br />
{{hc|/etc/vconsole.conf|<nowiki><br />
KEYMAP=us<br />
FONT=lat9w-16<br />
FONT_MAP=8859-1_to_uni</nowiki>}}<br />
<br />
{{注意|{{ic|{{Pkg|systemd}}-194}} 默认使用内核字体和键盘布局,因此以后不必在上述文件中留下空的 {{ic|1=KEYMAP=}} 和 {{ic|1=FONT=}} 设置。}}<br />
<br />
还可以通过命令设置键盘布局:<br />
# localectl set-keymap de<br />
<br />
{{ic|localectl}} 也可以设置X11的键盘布局:<br />
<br />
# localectl set-x11-keymap de<br />
<br />
详情参见 {{ic|man 1 localectl}} 和 {{ic|man 5 vconsole.conf}}。<br />
<br />
* 另参阅 [[Fonts (简体中文)#控制台字体|控制台字体]] 和 [[KEYMAP (简体中文)#键盘布局|键盘布局]]。<br />
<br />
=== 硬件时钟 ===<br />
<br />
'''systemd''' 默认硬件时钟为协调世界时(UTC)。<br />
<br />
{{小贴士|推荐使用[[NTP (简体中文)|NTP服务]]来在线同步硬件时钟。}}<br />
<br />
==== 硬件时钟设定为地方时 ====<br />
<br />
将硬件时钟配置为地方时('''不建议'''):<br />
<br />
# timedatectl set-local-rtc true<br />
<br />
重新调整为 UTC:<br />
<br />
# timedatectl set-local-rtc false<br />
<br />
如果设置成本地时间,处理夏令时有些麻烦。如果夏令时调整发生在关机时,下次启动时时间会出现问题([http://www.cl.cam.ac.uk/~mgk25/mswish/ut-rtc.html 更多信息])。最新的内核直接从实时时钟芯片(RTC)读取时间,不使用 {{ic|hwclock}},内核把从 RTC 读取的时间当作 UTC 处理。所以如果硬件时间是地方时,系统启动一开始识别的时间是错误的,之后很快会进行矫正。这可能导致一些问题(尤其是时间倒退时)。<br />
<br />
如果同时安装了 Windows 操作系统([http://blogs.msdn.com/b/oldnewthing/archive/2004/09/02/224672.aspx 默认使用地方时]),那么一般 RTC 会被设置为地方时。Windows 其实也能处理 UTC,需要[[Time (简体中文)#Windows 系统使用 UTC|修改注册表]]。建议让 Windows 使用 UTC,而非让 Linux 使用地方时。Windows 使用 UTC 后,请记得禁用 Windows 的时间同步功能,以防 Windows 错误设置硬件时间。如上文所说,Linux 可以使用[[NTP (简体中文)|NTP服务]]来在线同步硬件时钟。<br />
<br />
* 详情参阅 [[Time (简体中文)]]。<br />
<br />
=== 内核模块 ===<br />
目前,所有必要模块的加载均由 [[udev]] 自动完成。所以,如果不需要使用任何额外的模块,就没有必要在任何配置文件中添加启动时加载的模块。但是,有些情况下可能需要在系统启动时加载某个额外的模块,或者将某个模块列入黑名单以便使系统正常运行。<br />
<br />
==== 开机加载 ====<br />
systemd 读取 {{ic|/etc/modules-load.d/}} 中的配置加载额外的内核模块。配置文件名称通常为 {{ic|/etc/modules-load.d/<program>.conf}}。格式很简单,一行一个要读取的模块名,而空行以及第一个非空格字符为{{ic|#}}或{{ic|;}}的行会被忽略,如:<br />
{{hc|/etc/modules-load.d/virtio-net.conf|<nowiki><br />
# Load virtio-net.ko at boot<br />
virtio-net</nowiki>}}<br />
<br />
另见{{ic|man 5 modules-load.d}}。<br />
<br />
==== 配置内核模块参数 ====<br />
<br />
在 {{ic|/etc/modprobe.d/modprobe.conf}} 中设置额外的模块参数。<br />
<br />
例如:<br />
<br />
* 我们已经在 {{ic|/etc/modules-load.d/loop.conf}} 中添加文本 {{ic|loop}},告诉系统开机自动加载该模块。<br />
<br />
* 在 {{ic|/etc/modprobe.d/modprobe.conf}} 设置额外的参数,如:{{ic|options loop max_loop&#61;64}}。<br />
<br />
最后,可以通过 {{ic|cat /sys/module/loop/parameters/max_loop}} 命令检查配置是否生效。<br />
<br />
==== 禁用内核模块 ====<br />
禁用内核模块的方法和 Arch 默认的 {{Pkg|initscripts}} 相同,因为该过程实际由 {{Pkg|kmod}} 处理。参见:[[Kernel modules (简体中文)#黑名单]]。<br />
<br />
=== 文件系统挂载 ===<br />
<br />
默认行为是:在启动一个需要挂载特定分区的服务之前,系统自动检查并挂载分区。{{ic|/etc/fstab}} 中设定的网络文件系统(如 [[NFS (简体中文)|NFS]]、[[Samba (简体中文)|Samba]])无需配置即可正常工作,systemd 将确保网络文件系统在网络链接就绪后挂载。<br />
<br />
详情参阅:{{ic|man 5 systemd.mount}}。<br />
<br />
==== 自动挂载 ====<br />
<br />
* 如果 {{ic|/home}} 分区较大,可以让不依赖 {{ic|/home}} 分区的服务先启动。把下面的参数添加到 {{ic|/etc/fstab}} 文件中 {{ic|/home}} 项目的参数部分即可:<br />
<br />
noauto,x-systemd.automount<br />
<br />
这样 {{ic|/home}} 分区只有需要访问时才会被挂载。内核会缓存所有的文件操作,直到 {{ic|/home}} 分区准备完成。<br />
<br />
{{注意|这样做会使 {{ic|/home}} 的文件系统类型被识别为 {{ic|autofs}},造成 [[mlocate]] 查询时忽略该目录。实际加速效果因配置而异,所以请自己权衡是否需要。}}<br />
<br />
* 挂载远程文件系统也是同理。如果你仅想在需要的时候才挂载,也可以添加 {{ic|noauto,x-systemd.automount}} 参数。另外,可以设置 {{ic|1=x-systemd.device-timeout=#}} 参数,设置超时时间,以防止网络资源不能访问的时候浪费时间。<br />
<br />
* 如果你的加密文件系统需要密钥,则需要添加 {{ic|noauto}} 参数到 {{ic|/etc/crypttab}} 文件中的对应位置。systemd 开机的时候就不会打开这个加密设备,会一直等待到设备被访问时再使用密钥文件挂载。比如在使用加密RAID设备的时候可以节省一定的时间,因为 systemd 不必等到设备可用后才能访问。例如:<br />
<br />
{{hc|/etc/crypttab|<br />
data /dev/md0 /root/key noauto}}<br />
<br />
==== LVM ====<br />
<br />
如果装有不通过 [[Mkinitcpio (简体中文)|initramfs]] 激活的[[LVM (简体中文)|LVM]]卷,则需启动 {{ic|lvm-monitoring}} 服务(由 {{pkg|lvm2}} 软件包提供):<br />
<br />
# systemctl enable lvm-monitoring<br />
<br />
=== ACPI 电源管理 ===<br />
'''systemd''' 能够处理某些电源相关的 [[Wikipedia:Advanced_Configuration_and_Power_Interface|ACPI]] 事件,通过 {{ic|/etc/systemd/logind.conf}} 的下列选项配置:<br />
* {{ic|HandlePowerKey}}:按下电源键后的动作<br />
* {{ic|HandleSleepKey}}:按下挂起键后的动作<br />
* {{ic|HandleHibernateKey}}: 按下休眠键后的动作<br />
* {{ic|HandleLidSwitch}}:合上笔记本盖后待机<br />
<br />
动作可以是 {{ic|ignore}}、{{ic|poweroff}}、{{ic|reboot}}、{{ic|halt}}、{{ic|suspend}}、{{ic|hibernate}}、{{ic|hybrid-sleep}}、{{ic|lock}} 或 {{ic|kexec}}。<br />
<br />
系统默认设置为:<br />
HandlePowerKey=poweroff<br />
HandleSuspendKey=suspend<br />
HandleHibernateKey=hibernate<br />
HandleLidSwitch=suspend<br />
<br />
不用图形界面、或者使用 [[i3 (简体中文)|i3]]、[[awesome (简体中文)|awesome]] 这样简单的桌面管理器时,systemd 可以替代 [[acpid (简体中文)|acpid]] 处理 ACPI 事件。<br />
<br />
{{注意|运行 {{ic|systemctl restart systemd-logind}},使上述更改立即生效。}}<br />
<br />
{{注意|systemd 无法处理交流电源和电池 ACPI 事件,所以还得使用 [[Laptop Mode Tools (简体中文)|Laptop Mode Tools]] 或 [[acpid (简体中文)|acpid]] 工具。}}<br />
<br />
在当前版本的 systemd 中,这些 {{ic|Handle}} 选项将会被应用到整个系统当中,除非它们被别的程序——例如某个桌面环境中的电源管理器——给“阻止”(inhibited)。如果其它的程序没有阻止这些 {{ic|Handle}} ,你可能会先被 systemd 挂起你的系统,然后当系统被唤醒之后,电源管理器又会再次将系统挂起。<br />
<br />
{{警告|目前只有[[GNOME]] 和 [[KDE]] 支持 "inhibited" 命令。在其它的桌面管理器同样实现该功能之前,如果你想使用[[Xfce]]、[[acpid]] 或者其它程序来管理 ACPI 事件,你需要把 {{ic|Handle}} 选项设置为 {{ic|ignore}}。}}<br />
<br />
{{注意|除了内核默认的待机支持后端(用于处理待机/休眠),systemd 也可以使用其他后端(比如 [[Uswsusp]] 或 [[TuxOnIce]])。}}<br />
<br />
要令 {{ic|systemctl hibernate}} 工作,需要按照[[Pm-utils#Hibernation_.28suspend2disk.29|休眠]]和 [[Pm-utils#Mkinitcpio_Resume_Hook|mkinitcpio 唤醒扩展]]的设置步骤进行操作。(不必安装 {{ic|pm-utils}})<br />
<br />
==== 休眠时执行的脚本 ====<br />
<br />
使用 {{ic|systemctl suspend}}、{{ic|systemctl hibernate}} 或 {{ic|systemctl hybrid-sleep}} 命令执行待机/休眠时,systemd 不会调用 [[pm-utils (简体中文)|pm-utils]]。[[pm-utils (简体中文)|pm-utils]] 的钩子扩展(hook)——包括 [[pm-utils (简体中文)#建立你自己的钩子|自定义钩子]]——会失效。不过,systemd 提供了两种类似的待机/休眠时执行脚本的机制。<br />
<br />
===== 使用服务文件 =====<br />
<br />
可以将服务文件附在 suspend.target、hibernate.target 或 sleep.target 中,这样就能在待机/休眠前后执行某些操作。用户级操作和root/系统级操作应该使用不同的服务文件。要启用用户级服务文件,使用 {{ic|# systemctl enable suspend@<用户名> && systemctl enable resume@<用户名>}}。例如:<br />
<br />
{{hc|/etc/systemd/system/suspend@.service|2=<nowiki><br />
[Unit]<br />
Description=User suspend actions<br />
Before=sleep.target<br />
<br />
[Service]<br />
User=%I<br />
Type=forking<br />
Environment=DISPLAY=:0<br />
ExecStartPre= -/usr/bin/pkill -u %u unison ; /usr/local/bin/music.sh stop ; /usr/bin/mysql -e 'slave stop'<br />
ExecStart=/usr/bin/sflock<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
{{hc|/etc/systemd/system/resume@.service|2=<nowiki><br />
[Unit]<br />
Description=User resume actions<br />
After=suspend.target<br />
<br />
[Service]<br />
User=%I<br />
Type=simple<br />
ExecStartPre=/usr/local/bin/ssh-connect.sh<br />
ExecStart=/usr/bin/mysql -e 'slave start'<br />
<br />
[Install]<br />
WantedBy=suspend.target</nowiki>}}<br />
<br />
至于root/系统级服务(使用 {{ic|# systemctl enable root-suspend}} 激活):<br />
<br />
{{hc|/etc/systemd/system/root-resume.service|2=<nowiki><br />
[Unit]<br />
Description=Local system resume actions<br />
After=suspend.target<br />
<br />
[Service]<br />
Type=simple<br />
ExecStart=/usr/bin/systemctl restart mnt-media.automount<br />
<br />
[Install]<br />
WantedBy=suspend.target</nowiki>}}<br />
<br />
{{hc|/etc/systemd/system/root-suspend.service|2=<nowiki><br />
[Unit]<br />
Description=Local system suspend actions<br />
Before=sleep.target<br />
<br />
[Service]<br />
Type=simple<br />
ExecStart=-/usr/bin/pkill sshfs<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
上述服务文件的一些解释(详见 {{ic|man systemd.service}}):<br />
* 如果设置 {{ic|1=<nowiki>Type=OneShot</nowiki>}},那么可以使用多个 {{ic|1=<nowiki>ExecStart=</nowiki>}} 参数。否则只能写一个,替代方案是在 {{ic|ExecStartPre}} 中添加命令,或使用分号分隔不同命令(见第一个例子,分号前后的空格都是'''必须'''的)。 <br />
* 若命令前加上一个“-”(半角减号),则命令返回非零值时会被忽略、当作正常执行处理。<br />
* 有关调试,最好的方法是用 {{ic|journalctl}} 查看日志。<br />
<br />
===== 合并待机和唤醒服务文件 =====<br />
<br />
利用下面这种自定义的一体化待机和唤醒服务,使用单一的钩子扩展即可对不同操作(待机/休眠/混合休眠)的不同阶段(进入/唤醒)进行控制。<br />
<br />
例子和解释:<br />
<br />
{{hc|/etc/systemd/system/wicd-sleep.service|2=<nowiki><br />
[Unit]<br />
Description=Wicd sleep hook<br />
Before=sleep.target<br />
StopWhenUnneeded=yes<br />
<br />
[Service]<br />
Type=oneshot<br />
RemainAfterExit=yes<br />
ExecStart=-/usr/share/wicd/daemon/suspend.py<br />
ExecStop=-/usr/share/wicd/daemon/autoconnect.py<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
* {{ic|1=<nowiki>RemainAfterExit=yes</nowiki>}}:服务启动后,除非显式地停止,否则就认为是活动的。<br />
* {{ic|1=<nowiki>StopWhenUnneeded=yes</nowiki>}}:服务活动时,如果无其他服务依赖该服务,就停止它。在本例中,该服务会在 sleep.target 停止后停止活动。<br />
* 由于 sleep.target 会被 suspend.target、hibernate.target、hybrid-sleep.target 调用,且 sleep.target 本身设置了 StopWhenUnneeded,该服务文件对以上各种操作都是有效的。<br />
<br />
===== 使用 /usr/lib/systemd/system-sleep =====<br />
<br />
systemd 在待机/休眠时执行 {{ic|/usr/lib/systemd/system-sleep/}} 里的所有脚本,传递下面两个参数:<br />
<br />
* 参数1:若是准备进入待机/休眠状态,则为 {{ic|pre}};唤醒时为 {{ic|post}}。<br />
* 参数2:事件名称,{{ic|suspend}},{{ic|hibernate}} 或 {{ic|hybrid-sleep}}。<br />
<br />
systemd 会同时执行所有脚本,而不是像 [[pm-utils (简体中文)|pm-utils]] 那样顺序执行。<br />
<br />
脚本输出会记录在相关服务({{ic|systemd-suspend.service}}、{{ic|systemd-hibernate.service}} 或 {{ic|systemd-hybrid-sleep.service}})中。通过[[#日志|日志]]查看:<br />
# journalctl -b -u systemd-suspend<br />
<br />
{{注意|除了使用自定义脚本,还可以利用 {{ic|sleep.target}}、{{ic|suspend.target}}、{{ic|hibernate.target}} 或 {{ic|hybrid-sleep.target}} 来为单元(unit)添加睡眠状态策略。}}<br />
<br />
脚本示范:<br />
{{hc|/usr/lib/systemd/system-sleep/example.sh|<br />
#!/bin/sh<br />
case $1/$2 in<br />
pre/*)<br />
echo "进入 $2 状态..."<br />
;;<br />
post/*)<br />
echo "从 $2 状态唤醒..."<br />
;;<br />
esac}}<br />
<br />
记得添加可执行权限: <br />
# chmod a+x /usr/lib/systemd/system-sleep/example.sh<br />
<br />
详情参见 {{ic|man 7 systemd.special}} 和 {{ic|man 8 systemd-sleep}}。<br />
<br />
=== 临时文件 ===<br />
{{ic|/usr/lib/tmpfiles.d/}} 和 {{ic|/etc/tmpfiles.d/}} 中的文件描述了 systemd-tmpfiles 如何创建、清理、删除临时文件和目录,这些文件和目录通常存放在 {{ic|/run}} 和 {{ic|/tmp}} 中。配置文件名称为 {{ic|/etc/tmpfiles.d/<program>.conf}}。此处的配置能覆盖 {{ic|/usr/lib/tmpfiles.d/}} 目录中的同名配置。<br />
<br />
临时文件通常和服务文件同时提供,以生成守护进程需要的文件和目录。例如 [[Samba (简体中文)|Samba]] 服务需要目录 {{ic|/run/samba}} 存在并设置正确的权限位,就象这样:<br />
{{hc|/usr/lib/tmpfiles.d/samba.conf|<br />
D /run/samba 0755 root root<br />
}}<br />
<br />
此外,临时文件还可以用来在开机时向特定文件写入某些内容。比如,要禁止系统从USB设备唤醒,利用旧的 {{ic|/etc/rc.local}} 可以用 {{ic|echo USBE > /proc/acpi/wakeup}},而现在可以这么做:<br />
{{hc|/etc/tmpfiles.d/disable-usb-wake.conf|<br />
w /proc/acpi/wakeup - - - - USBE<br />
}}<br />
<br />
详情参见 {{ic|man 5 tmpfiles.d}}。<br />
<br />
{{注意|该方法不能向 {{ic|/sys}} 中的配置文件添加参数,因为 {{ic|systemd-tmpfiles-setup}} 有可能在相关模块加载前运行。这种情况下,需要首先通过 {{ic|modinfo <模块名>}} 确认需要的参数,并在 [[Kernel modules (简体中文)#选项|{{ic|/etc/modprobe.d}} 下的一个文件]]中设置改参数。另外,还可以使用 [[udev (简体中文)#udev规则|udev 规则]],在设备就绪时设置相应属性。}}<br />
<br />
=== 单元(unit) ===<br />
一个单元配置文件可以描述如下内容之一:系统服务、socket、系统设备、挂载点、交换分区/文件、启动目标(target)、文件系统路径、由 systemd 管理的计时器。文件格式受 .desktop 文件启发,而最初起源是 Windows 下的 .ini 文件。<br />
<br />
详情参阅 {{ic|man 5 systemd.unit}}.<br />
<br />
== 从 initscripts 迁移到 systemd ==<br />
<br />
=== initscripts 模拟 ===<br />
<br />
{{Pkg|initscripts}} 软件包提供了 systemd 和 Arch 传统配置的整合。当同时安装了{{Pkg|initscripts}}和systemd时,若以systemd启动系统,systemd将执行以下操作:<br />
<br />
# 解析{{ic|/etc/rc.conf}}中的{{ic|DAEMONS}}数组,并在系统启动时,启动所有列出的守护进程<br />
# 在启动过程中执行{{ic|/etc/rc.local}}<br />
# 在系统关闭过程中执行{{ic|/etc/rc.local.shutdown}}<br />
<br />
Initscripts模拟仅仅是作为用户迁移到systemd时的过渡措施,这一方案最终将'''不再可行'''。原生的systemd并不依赖于{{ic|rc.conf}}文件的集中式配置,因此推荐使用[[#原生 systemd 配置文件|原生的systemd配置文件]]。原生systemd配置文件比{{ic|/etc/rc.conf}}具有更高优先级。<br />
<br />
{{注意|替换{{ic|/etc/rc.local}}的推荐方式是针对系统启动时要运行的程序编写自定义的service文件,详见相关[[#自己编写_.service_文件|章节]].}}<br />
<br />
{{注意|如果你曾经在{{ic|/etc/inittab}}中禁用了以{{keypress|Ctrl+Alt+Del}}重启系统,则需要以root执行{{ic|systemctl mask ctrl-alt-del.target}}以使其对systemd重新生效。}}<br />
<br />
{{警告|如果同时安装 systemd(197-4 及后续版本)和 initscripts,那么 {{ic|/etc/rc.local}} 脚本会导致启动过程无法结束(原因是 getty@tty1.service 自 197-4 版本起不再等待 rc-local.service 执行,getty 无法启动)。请删除或重命名 {{ic|/etc/rc.local}}。}}<br />
<br />
=== 迁移 DAEMONS 列表 ===<br />
<br />
若要用纯 systemd 环境,则应当删除 {{ic|/etc/rc.conf}} 文件,并完全通过 {{ic|systemctl}} 命令来启动服务。对于 {{ic|DAEMONS}} 列表里面的每个 {{ic|<服务名>}},现在可以用以下命令设置成自动启动:<br />
<br />
# systemctl enable <服务名>.service<br />
<br />
{{小贴士|[[Daemons List|这里]]有一份 initscripts 和 systemd 下常见服务的对应列表。}}<br />
<br />
如果提示 {{ic|<service_name>.service}} 不存在:<br />
<br />
* 很有可能是 systemd 用了不同的服务名。比如 {{ic|crond}} 服务替换成 {{ic|cronie.service}};{{ic|alsa}} 服务替换成了 {{ic|alsa-store.service}} 和 {{ic|alsa-restore.service}} 两个服务。另一个值得注意的是 {{ic|network}} 服务,它被替换为一整套服务文件(详情参见 [[Configuring_Network_(简体中文)]])。<br />
* 另外,有些程序尚未提供 .service 文件,还得继续用 {{ic|rc.conf}} 来配置开机自动启动。<br />
<br />
{{小贴士|用下面这个命令来查看已安装软件包包含的服务文件:<br />
$ pacman -Ql cronie<br />
[...]<br />
cronie /etc/rc.d/crond # DAEMONS 列表中使用的 initscript 服务 (原生 systemd 配置中不再使用)<br />
[...]<br />
cronie /usr/lib/systemd/system/cronie.service # 原生 systemd 服务文件<br />
[...]<br />
}}<br />
<br />
* 有些服务不需要用户明确配置自启动。比如安装 {{ic|dbus-core}} 软件包后 {{ic|dbus.service}} 就会自动被配置为自启动。而 {{ic|alsa-store.service}} 和 {{ic|alsa-restore.service}} 也会由 systemd 自动启动。可以用 {{ic|systemctl list-unit-files}} 命令来查看服务列表并检查运行状态。<br />
<br />
== systemd 基本工具 ==<br />
<br />
检视和控制systemd的主要命令是{{ic|systemctl}}。该命令可用于查看系统状态和管理系统及服务。详见{{ic|man 1 systemctl}}。<br />
<br />
{{小贴士|在 {{ic|systemctl}} 参数中添加 {{ic|-H <用户名>@<主机名>}} 可以实现对其他机器的远程控制。该过程使用 [[SSH (简体中文)|SSH]] 链接。}}<br />
<br />
{{注意|{{ic|systemadm}} 是 systemd 的官方图形前端。由 [[AUR (简体中文)|AUR]] 中的软件包 {{AUR|systemd-ui-git}} 提供。}}<br />
<br />
=== 分析系统状态 ===<br />
<br />
输出激活的单元:<br />
<br />
$ systemctl<br />
<br />
以下命令等效:<br />
<br />
$ systemctl list-units<br />
<br />
输出运行失败的单元:<br />
<br />
$ systemctl --failed<br />
<br />
所有可用的单元文件存放在 {{ic|/usr/lib/systemd/system/}} 和 {{ic|/etc/systemd/system/}} 目录(后者优先级更高)。查看所有已安装服务:<br />
$ systemctl list-unit-files<br />
<br />
=== 使用单元 ===<br />
一个单元可以是系统服务({{ic|.service}})、挂载点({{ic|.mount}})、sockets({{ic|.sockets}})。<br />
<br />
使用 {{ic|systemctl}} 控制单元时,通常需要使用单元文件的全名,包括扩展名(例如 {{ic|sshd.service}})。但是有些单元可以在{{ic|systemctl}}中使用简写方式。<br />
* 如果无扩展名,systemctl 默认把扩展名当作 {{ic|.service}}。例如 {{ic|netcfg}} 和 {{ic|netcfg.service}} 是等价的。<br />
* 挂载点会自动转化为相应的 {{ic|.mount}} 单元。例如 {{ic|/home}} 等价于 {{ic|home.mount}}。<br />
* 设备会自动转化为相应的 {{ic|.device}} 单元,所以 {{ic|/dev/sda2}} 等价于 {{ic|dev-sda2.device}}。<br />
<br />
立即激活单元:<br />
<br />
# systemctl start <单元><br />
<br />
立即停止单元:<br />
# systemctl stop <单元><br />
<br />
重启单元:<br />
<br />
# systemctl restart <单元><br />
<br />
命令单元重新读取配置:<br />
<br />
# systemctl reload <单元><br />
<br />
输出单元运行状态:<br />
<br />
$ systemctl status <单元><br />
<br />
检查单元是否配置为自动启动:<br />
$ systemctl is-enabled <单元><br />
<br />
开机自动激活单元:<br />
<br />
# systemctl enable <单元><br />
<br />
{{注意|如果服务没有{{ic|Install}}段落,一般意味着应该通过其它服务自动调用它们。如果真的需要手动安装,可以直接连接服务,如下(将{{ic|foo}}替换为真实的服务名):<br />
{{bc|# ln -s /usr/lib/systemd/system/foo.service /etc/systemd/system/graphical.target.wants/}}<br />
}}<br />
<br />
取消开机自动激活单元:<br />
<br />
# systemctl disable <单元><br />
<br />
显示单元的手册页(必须由单元文件提供):<br />
<br />
# systemctl help <单元><br />
<br />
重新载入 systemd,扫描新的或有变动的单元:<br />
# systemctl daemon-reload<br />
<br />
=== 电源管理 ===<br />
<br />
安装 {{ic|polkit}} 后才可使用电源管理。<br />
<br />
如果你正登录在一个本地的{{ic|systemd-logind}}用户会话,且当前没有其它活动的会话,那么以下命令无需root权限即可执行。否则(例如,当前有另一个用户登录在某个tty),systemd 将会自动请求输入root密码。<br />
<br />
重启:<br />
<br />
$ systemctl reboot<br />
<br />
退出系统并停止电源:<br />
<br />
$ systemctl poweroff<br />
<br />
待机:<br />
<br />
$ systemctl suspend<br />
<br />
休眠:<br />
<br />
$ systemctl hibernate<br />
<br />
混合休眠模式(同时休眠到硬盘并待机):<br />
<br />
$ systemctl hybrid-sleep<br />
<br />
== 启动登录管理器 ==<br />
<br />
通过启动[[Display Manager (简体中文)|登录管理器]](或称显示管理器),即可进行图形界面登录。<br />
要启用图形界面登录,运行适当的[[Display Manager (简体中文)|登录管理器]](或称显示管理器)即可。目前,Arch 提供了 [[GDM (简体中文)|GDM]]、[[KDM (简体中文)|KDM]]、[[SLiM (简体中文)|SLiM]]、[[XDM (简体中文)|XDM]]、[[LXDM (简体中文)|LXDM]]、[[LightDM (简体中文)|LightDM]] 和 {{AUR|SDDM}} 的 systemd 服务文件。以 KDM 为例,配置开机启动:<br />
# systemctl enable kdm.service<br />
<br />
执行上述命令后,登录管理器应当能正常工作了。如果不是的话,很可能是因为你修改了{{ic|default.target}}。默认情况应当如下:<br />
<br />
{{hc|# ls -l /etc/systemd/system/default.target|/etc/systemd/system/default.target -> /usr/lib/systemd/system/graphical.target}}<br />
<br />
删除被修改的 {{ic|default.target}} 即可,systemd 会自动使用默认配置(即 {{ic|graphical.target}}):<br />
<br />
# rm /etc/systemd/system/default.target<br />
<br />
=== 使用 systemd-logind ===<br />
<br />
{{注意|自 2012-10-30 起, [[ConsoleKit]] 已被 [https://www.archlinux.org/news/consolekit-replaced-by-logind/ systemd-logind 取代] ,作为登录到桌面环境的默认方式。}}<br />
<br />
可使用 {{ic|loginctl}} 来查看用户会话的状态。所有 [[PolicyKit]] 操作,如挂起系统、挂载外部驱动器,都无需配置即可使用。<br />
<br />
$ loginctl show-session $XDG_SESSION_ID<br />
<br />
== 自己编写 .service 文件 ==<br />
''参见:[[Systemd/Services]]''<br />
<br />
=== 处理依赖关系 ===<br />
<br />
使用systemd时,可通过正确编写单元配置文件来解决其依赖关系。典型的情况是,单元{{ic|A}}要求单元{{ic|B}}在{{ic|A}}启动之前运行。在此情况下,向单元{{ic|A}}配置文件中的 {{ic|[Unit]}} 段添加 {{ic|1=Requires=B}} 和 {{ic|1=After=B}} 即可。若此依赖关系是可选的,可添加 {{ic|1=Wants=B}} 和 {{ic|1=After=B}}。请注意 {{ic|1=Wants=}} 和 {{ic|1=Requires=}} 并不意味着 {{ic|1=After=}},即如果 {{ic|1=After=}} 选项没有制定,这两个单元将被并行启动。<br />
<br />
依赖关系通常被用在服务(service)而不是[[#目标(target)|目标(target)]]上。例如, {{ic|network.target}} 一般会被某个配置网络接口的服务引入,所以,将自定义的单元排在该服务之后即可,因为 {{ic|network.target}} 已经启动。<br />
<br />
=== 启动方式 ===<br />
<br />
编写自定义的service文件时,可以选择几种不同的服务启动方式。启动方式可通过配置文件 {{ic|[Service]}} 段中的 {{ic|1=Type=}} 参数进行设置。具体的参数说明请参阅 {{ic|man systemd.service}} 。<br />
<br />
* {{ic|1=Type=simple}}(默认值):systemd认为该服务将立即启动。服务进程不会fork。如果该服务要启动其他服务,不要使用此类型启动,除非该服务是socket激活型。<br />
* {{ic|1=Type=forking}}:systemd认为当该服务进程fork,且父进程退出后服务启动成功。对于常规的守护进程(daemon),除非你确定此启动方式无法满足需求,使用此类型启动即可。使用此启动类型应同时指定 {{ic|1=PIDFile=}},以便systemd能够跟踪服务的主进程。<br />
* {{ic|1=Type=oneshot}}:这一选项适用于只执行一项任务随后退出的服务。你可能需要同时设置 {{ic|1=RemainAfterExit=yes}} 使得 systemd 在服务进程退出之后仍然认为服务处于激活状态。<br />
* {{ic|1=Type=notify}}:与 {{ic|1=Type=simple}} 相同,但约定服务会在就绪后向 systemd 发送一个信号。这一通知的实现由 {{ic|libsystemd-daemon.so}} 提供。<br />
* {{ic|1=Type=dbus}}:若以此方式启动,当指定的 {{ic|BusName}} 出现在DBus系统总线上时,systemd认为服务就绪。<br />
<br />
=== 修改现存单元文件 ===<br />
<br />
To edit a unit file provided by a package, you can create a directory called {{ic|/etc/systemd/system/<unit>.d/}} for example {{ic|/etc/systemd/system/httpd.service.d/}} and place {{ic|*.conf}} files in there to override or add new options. '''systemd''' will parse these {{ic|*.conf}} files and apply them on top of the original unit. For example, if you simply want to add an additional dependency to a unit, you may create the following file:<br />
<br />
{{hc|/etc/systemd/system/<unit>.d/customdependency.conf|2=<br />
[Unit]<br />
Requires=<新依赖><br />
After=<新依赖>}}<br />
<br />
然后运行以下命令使更改生效:<br />
<br />
# systemctl daemon-reload<br />
# systemctl restart <单元><br />
<br />
Alternatively you can copy the old unit file from {{ic|/usr/lib/systemd/system/}} to {{ic|/etc/systemd/system/}} and make your changes there. A unit file in {{ic|/etc/systemd/system/}} always overrides the same unit in {{ic|/usr/lib/systemd/system/}}. Note that when the original unit in {{ic|/usr/lib/}} is changed due to a package upgrade, these changes will not automatically apply to your custom unit file in {{ic|/etc/}}. Additionally you will have to manually reenable the unit with {{ic|systemctl reenable <unit>}}. It is therefore recommended to use the {{ic|*.conf}} method described before instead.<br />
<br />
{{小贴士|可以用 {{ic|systemd-delta}} 命令来查看哪些单元文件被覆盖、哪些被修改。}}<br />
<br />
=== 单元配置文件的 vim 语法高亮支持 ===<br />
<br />
可从 [[Official Repositories|官方仓库]] 安装 {{Pkg|vim-systemd}} 软件包,使 unit 配置文件在 [[Vim]] 下支持语法高亮。<br />
<br />
== 目标(target) ==<br />
启动级别(runlevel)是一个旧的概念。现在,systemd 引入了一个和启动级别功能相似又不同的概念——目标(target)。不像数字表示的启动级别,每个''目标''都有名字和独特的功能,并且能同时启用多个。一些''目标''继承其他''目标''的服务,并启动新服务。systemd 提供了一些模仿 sysvinit 启动级别的''目标'',仍可以使用旧的 {{ic|telinit 启动级别}} 命令切换。<br />
<br />
=== 获取当前目标 ===<br />
不要使用 {{ic|runlevel}} 命令了:<br />
$ systemctl list-units --type=target<br />
<br />
=== 创建新目标 ===<br />
在 Fedora 中,启动级别 0、1、3、5、6 都被赋予特定用途,并且都对应一个 systemd 的''目标''。然而,没有什么很好的移植用户定义的启动级别(2、4)的方法。要实现类似功能,可以以原有的启动级别为基础,创建一个新的''目标'' {{ic|/etc/systemd/system/<新目标>}}(可以参考 {{ic|/usr/lib/systemd/system/graphical.target}}),创建 {{ic|/etc/systemd/system/<新目标>.wants}} 目录,向其中加入额外服务的链接(指向 {{ic|/usr/lib/systemd/system/}} 中的单元文件)。<br />
<br />
=== 目标表 ===<br />
{| border="1"<br />
!SysV 启动级别!!Systemd 目标!!注释<br />
|-<br />
| 0 || runlevel0.target, poweroff.target || 中断系统(halt)<br />
|-<br />
| 1, s, single || runlevel1.target, rescue.target || 单用户模式<br />
|-<br />
| 2, 4 || runlevel2.target, runlevel4.target, multi-user.target || 用户自定义启动级别,通常识别为级别3。<br />
|-<br />
| 3 || runlevel3.target, multi-user.target || 多用户,无图形界面。用户可以通过终端或网络登录。<br />
|-<br />
| 5 || runlevel5.target, graphical.target || 多用户,图形界面。继承级别3的服务,并启动图形界面服务。<br />
|-<br />
| 6 || runlevel6.target, reboot.target || 重启<br />
|-<br />
| emergency || emergency.target || 急救模式(Emergency shell)<br />
|-<br />
|}<br />
<br />
=== 切换启动级别/目标 ===<br />
systemd 中,启动级别通过“目标单元”访问。通过如下命令切换:<br />
# systemctl isolate graphical.target<br />
该命令对下次启动无影响。等价于{{ic|telinit 3}} 或 {{ic|telinit 5}}。<br />
<br />
=== 修改默认启动级别/目标 ===<br />
开机启动进的目标是 {{ic|default.target}},默认链接到 {{ic|graphical.target}} (大致相当于原来的启动级别5)。可以通过[[Kernel parameters (简体中文)|内核参数]]更改默认启动级别:<br />
<br />
{{小贴士|可以省略扩展名 {{ic|.target}}。}}<br />
<br />
* {{ic|1=systemd.unit=multi-user.target}} (大致相当于级别3)<br />
* {{ic|1=systemd.unit=rescue.target}} (大致相当于级别1)<br />
<br />
另一个方法是修改 {{ic|default.target}}。可以通过 {{ic|systemctl}} 修改它:<br />
<br />
# systemctl enable multi-user.target<br />
<br />
命令执行情况由 {{ic|systemctl}} 显示:链接 {{ic|/etc/systemd/system/default.target}} 被创建,指向新的默认启动级别。该方法当且仅当目标配置文件中有以下内容时有效:<br />
<br />
[Install]<br />
Alias=default.target<br />
<br />
目前,{{ic|multi-user.target}}、{{ic|graphical.target}} 都包含这段内容。<br />
<br />
== 日志 ==<br />
systemd 提供了自己日志系统(logging system),称为 journal. 使用 systemd 日志,无需额外安装日志服务(syslog)。读取日志的命令:<br />
# journalctl<br />
<br />
默认情况下(当 {{ic|1=Storage=}} 在文件 {{ic|/etc/systemd/journald.conf}} 中被设置为 {{ic|auto}}),日志记录将被写入 {{ic|/var/log/journal/}}。该目录是 {{pkg|systemd}} 软件包的一部分。若被删除,systemd '''不会'''自动创建它,直到下次升级软件包时重建该目录。如果该目录缺失,systemd 会将日志记录写入 {{ic|/run/systemd/journal}}。这意味着,系统重启后日志将丢失。<br />
<br />
=== 过滤输出 ===<br />
<br />
{{ic|journalctl}}可以根据特定字段过滤输出,例如:<br />
<br />
显示本次启动后的所有日志:<br />
<br />
# journalctl -b<br />
<br />
不过,一般大家更关心的不是本次启动后的日志,而是上次启动时的(例如,刚刚系统崩溃了)。目前还没有这项功能,正在 [http://comments.gmane.org/gmane.comp.sysutils.systemd.devel/6608 systemd-devel@lists.freedesktop.org] 讨论中。<br />
<br />
目前的折中方案是:<br />
<br />
# journalctl --since=today | tac | sed -n '/-- Reboot --/{n;:r;/-- Reboot --/q;p;n;b r}' | tac<br />
<br />
以上命令输出本日内的所有启动信息。但要注意,如果日志很多,该命令执行时间会比较漫长。<br />
<br />
动态跟踪最新信息:<br />
# journalctl -f<br />
<br />
显示特定程序的所有消息:<br />
{{bc|# journalctl /usr/lib/systemd/systemd}}<br />
<br />
显示特定进程的所有消息:<br />
{{bc|1=# journalctl _PID=1}}<br />
<br />
显示指定单元的所有消息:<br />
# journalctl -u netcfg<br />
<br />
详情参阅{{ic|man journalctl}}、{{ic|man systemd.journal-fields}},以及 Lennert 的这篇[http://0pointer.de/blog/projects/journalctl.html 博文]。<br />
<br />
=== 日志大小限制 ===<br />
<br />
如果按上面的操作保留日志的话,默认日志最大限制为所在文件系统容量的 10%,即:如果 {{ic|/var/log/journal}} 储存在 50GiB 的根分区中,那么日志最多存储 5GiB 数据。可以修改 {{ic|/etc/systemd/journald.conf}} 中的 {{ic|SystemMaxUse}} 来指定该最大限制。如限制日志最大 50MiB:<br />
<br />
SystemMaxUse=50M<br />
<br />
详情参见 {{ic|man journald.conf}}.<br />
<br />
=== 配合 syslog 使用 ===<br />
systemd 提供了 socket {{ic|/run/systemd/journal/syslog}},以兼容传统日志服务。所有系统信息都会被传入。要使传统日志服务工作,需要让服务链接该 socket,而非 {{ic|/dev/log}}([http://lwn.net/Articles/474968/ 官方说明])。Arch 软件仓库中的 {{pkg|syslog-ng}} 已经包含了需要的配置。<br />
<br />
设置开机启动 syslog-ng:<br />
# systemctl enable syslog-ng<br />
<br />
[http://0pointer.de/blog/projects/ 这里]有一份很不错的 {{ic|journalctl}} 指南。<br />
<br />
== 优化 ==<br />
<br />
参阅:[[Improve Boot Performance (简体中文)]]。<br />
<br />
=== 启动过程分析 ===<br />
systemd 提供了一个分析启动过程的工具 —— {{ic|systemd-analyze}}。可以用它看看哪些单元拖慢了开机过程,并据此进行优化。<br />
<br />
查看开机过程在内核/用户空间消耗的时间:<br />
$ systemd-analyze<br />
{{小贴士|<br />
* 如果在 {{ic|/etc/mkinitcpio.conf}} 的 {{ic|HOOKS}} 数组添加 {{ic|timestamp}},并重新生成启动内存镜像({{ic|mkinitcpio -p linux}}),{{ic|systemd-analyze}} 还可以显示处理该启动镜像花费的时间。<br />
* 如果使用 [[UEFI]] 引导,且启动引导器实现了 systemd 的 [http://www.freedesktop.org/wiki/Software/systemd/BootLoaderInterface Boot Loader Interface](目前只有 [[Gummiboot]] 实现了),'''systemd-analyze''' 还可以显示 EFI 固件和启动引导器自身花费的时间。}}<br />
<br />
按照耗费时间顺序,输出启动每个单元耗费的时间:<br />
$ systemd-analyze blame<br />
<br />
生成类似于 [[Bootchart (简体中文)|bootchart]] 的开机过程图表:<br />
$ systemd-analyze plot > plot.svg<br />
<br />
==== 使用 systemd-bootchart ====<br />
<br />
自2012年10月17日,bootchart 工具已经合并进 systemd 中,使用方法和原来的 bootchart 大同小异,添加下列内容到内核参数即可:<br />
<br />
initcall_debug printk.time=y init=/usr/lib/systemd/systemd-bootchart<br />
<br />
==== 使用 bootchart2 ====<br />
<br />
由于没有办法在内核参数设置两个 init,所以不能使用源里的 bootchart。不过,[[AUR (简体中文)|AUR]] 软件包 {{AUR|bootchart2-git}} 提供了一个的 systemd 服务,这样就可以和 systemd 一起使用。安装后启用即可:<br />
{{bc|# systemctl enable bootchart.service}}<br />
详情参阅 [https://github.com/mmeeks/bootchart bootchart 文档]。<br />
<br />
=== 预读 ===<br />
systemd 自己实现了一个 readahead,可以用来提高开机效率。不过,效果会因内核版本和硬件情况而不同(极少数情况下还会变慢)。开启 readahead:<br />
<br />
# systemctl enable systemd-readahead-collect.service systemd-readahead-replay.service<br />
<br />
要知道,readahead 的超级牛力只有在重启几次后才会显现。<br />
<br />
== 疑难解答 ==<br />
<br />
=== 关机/重启十分缓慢 ===<br />
<br />
如果关机特别慢(甚至跟死机了一样),很可能是某个拒不退出的服务在作怪。systemd 会等待一段时间,然后再尝试杀死它。请阅读[http://freedesktop.org/wiki/Software/systemd/Debugging#Shutdown_Completes_Eventually 这篇文章],确认你是否是该问题受害者。<br />
<br />
=== 短时进程无日志记录 ===<br />
<br />
若 {{ic|journalctl -u foounit.service}} 没有显示某个短时进程的任何输出,那么改用 PID 试试。例如,若 {{ic|systemd-modules-load.service}} 执行失败,那么先用 {{ic|systemctl status systemd-modules-load}} 查询其 PID(比如是123),然后检索该 PID 相关的日志 {{ic|journalctl -b _PID&#61;123}}。运行时进程的日志元数据(诸如 _SYSTEMD_UNIT 和 _COMM)被乱序收集在 {{ic|/proc}} 目录。要修复该问题,必须修改内核,使其通过套接字连接来提供上述数据,该过程类似于 SCM_CREDENTIALS。<br />
<br />
=== 诊断启动问题 ===<br />
<br />
使用如下内核参数引导:<br />
{{ic|<nowiki>systemd.log_level=debug systemd.log_target=kmsg log_buf_len=1M</nowiki>}}<br />
<br />
更多有关调试的信息,参见[http://freedesktop.org/wiki/Software/systemd/Debugging 该文]。<br />
<br />
=== 禁止在程序崩溃时转储内存 ===<br />
<br />
把 {{ic|/etc/sysctl.d/coredump.conf}} 链接到 {{ic|/dev/null}},然后应用 sysctl 即可([https://bbs.archlinux.org/viewtopic.php?id=154511 来源]):<br />
<br />
# ln -s /dev/null /etc/sysctl.d/coredump.conf<br />
# /usr/lib/systemd/systemd-sysctl<br />
# ulimit -c unlimited<br />
<br />
== 相关资源 ==<br />
<br />
*[http://www.freedesktop.org/wiki/Software/systemd 官方网站]<br />
*[http://0pointer.de/public/systemd-man/ man 手册页]<br />
*[http://freedesktop.org/wiki/Software/systemd/Optimizations systemd 优化]<br />
*[http://www.freedesktop.org/wiki/Software/systemd/FrequentlyAskedQuestions 常见问题 FAQ]<br />
*[http://www.freedesktop.org/wiki/Software/systemd/TipsAndTricks 小技巧]<br />
*[http://0pointer.de/public/systemd-ebook-psankar.pdf systemd for Administrators (PDF)]<br />
*[http://fedoraproject.org/wiki/Systemd Fedora 项目对 systemd 的介绍]<br />
*[http://fedoraproject.org/wiki/How_to_debug_Systemd_problems 如何调试 systemd 故障]<br />
*''The H Open'' 杂志上的[http://www.h-online.com/open/features/Control-Centre-The-systemd-Linux-init-system-1565543.html 两][http://www.h-online.com/open/features/Booting-up-Tools-and-tips-for-systemd-1570630.html 篇]科普文章<br />
*[http://0pointer.de/blog/projects/systemd.html Lennart 的博文]<br />
*[http://0pointer.de/blog/projects/systemd-update.html 更新报告]<br />
*[http://0pointer.de/blog/projects/systemd-update-2.html 更新报告2]<br />
*[http://0pointer.de/blog/projects/systemd-update-3.html 更新报告3]<br />
*[http://0pointer.de/blog/projects/why.html 最新动态]<br />
*[https://fedoraproject.org/wiki/SysVinit_to_Systemd_Cheatsheet/zh Fedora Wiki 上的 SysVinit 和 systemd 命令对比表]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Systemd_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=262812
Systemd (简体中文)
2013-06-15T04:24:49Z
<p>Cuihao: /* 启动过程分析 */</p>
<hr />
<div>{{Lowercase title}}<br />
[[Category:Daemons and system services (简体中文)]]<br />
[[Category:Boot process (简体中文)]]<br />
[[ar:Systemd]]<br />
[[en:Systemd]]<br />
[[es:Systemd]]<br />
[[fr:Systemd]]<br />
[[it:Systemd]]<br />
[[ja:Systemd]]<br />
[[ru:Systemd]]<br />
[[zh-TW:Systemd]]<br />
{{Article summary start|概览}}<br />
{{Article summary text|本文介绍 Systemd 的安装和配置。}}<br />
{{Article summary heading|相关页面}}<br />
{{Article summary wiki|Systemd/Services}}<br />
{{Article summary wiki|systemd/User}}<br />
{{Article summary wiki|Systemd FAQ}}<br />
{{Article summary wiki|init Rosetta}}<br />
{{Article summary wiki|Daemons List}}<br />
{{Article summary wiki|udev (简体中文)}}<br />
{{Article summary end}}<br />
{{TranslationStatus (简体中文)|Systemd|2013-03-14|250286}}<br />
摘自[http://freedesktop.org/wiki/Software/systemd 项目主页]:<br />
<br />
'''''systemd''' 是 Linux 下的一款系统和服务管理器,兼容 SysV 和 LSB 的启动脚本。systemd 的特性有:支持并行化任务;同时采用 socket 式与 [[D-Bus (简体中文)|D-Bus]] 总线式激活服务;按需启动守护进程(daemon);利用 Linux 的 [[cgroups|cgroups]] 监视进程;支持快照和系统恢复;维护挂载点和自动挂载点;各服务间基于依赖关系进行精密控制。systemd 完全可以替代 Arch 曾经使用的 [[SysVinit (简体中文)|sysvinit]] 启动系统。''<br />
<br />
{{注意|[https://bbs.archlinux.org/viewtopic.php?pid&#61;1149530#p1149530 这个帖子]详细解释了 Arch 向 systemd 迁移的原因。}}<br />
<br />
另见[[Wikipedia:zh:systemd|维基百科上的介绍]]。<br />
<br />
== 迁移前需考虑 ==<br />
<br />
* 阅读[http://freedesktop.org/wiki/Software/systemd/ 该站],了解 systemd。<br />
* systemd 自己有一套日志('''journal''')系统,用于代替 '''syslog'''。两者也可以共存,参见后面的[[#日志|日志部分]]。<br />
* 虽然 systemd 可以替换 '''cron'''、'''acpid'''、'''xinetd''' 等的部分功能。至少目前还可以继续使用这些服务,无需立即切换。<br />
* 交互式 initscripts 启动脚本在 systemd 中无法工作。例如 '''netcfg-menu''' 无法{{Bug|31377}})在启动时显示。<br />
<br />
== 安装 ==<br />
{{注意|从[https://www.archlinux.org/news/systemd-is-now-the-default-on-new-installations/ 2012-10-13版安装介质]开始,安装程序已经默认安装{{pkg|systemd}} 和 {{pkg|systemd-sysvcompat}}。}}<br />
<br />
{{注意|如果是在 VPS 中使用 Arch,请先阅读:[[Virtual_Private_Server#Moving_your_VPS_from_initscripts_to_systemd]]。}}<br />
<br />
本部分帮助还在使用{{AUR|sysvinit}} 和 initscripts 的用户迁移到 '''systemd'''.<br />
<br />
# 从[[Official_Repositories_(简体中文)|官方软件仓库]]安装 {{pkg|systemd}} 并添加[[Kernel parameters|内核参数]]{{Ic|1=init=/usr/lib/systemd/systemd}}<br />
# 使用 {{ic|systemctl enable <服务名>}} 启用需要的服务(大致相当于以前 {{ic|DAEMONS}} 数组的作用,新的服务名称参见 [[Daemons_List_(简体中文)]])。<br />
# 重启系统,执行命令 {{ic|cat /proc/1/comm}},如果返回{{ic|systemd}},表示 systemd 已经正常启动。<br />
# 确认主机名已经正确设置:{{ic|hostnamectl set-hostname myhostname}}。<br />
# 删除 initscripts 和 sysvinit,并安装{{pkg|systemd-sysvcompat}}.<br />
# (可选)删除{{ic|1=init=/usr/lib/systemd/systemd}}内核参数,现在已经不需要它了。{{pkg|systemd-sysvcompat}} 软件包提供了一个软链接,使 systemd 成为默认 init。<br />
<br />
=== 附加信息 ===<br />
* 如果内核参数中有 {{ic|quiet}},建议在一开始先去掉,以便调试。<br />
{{Accuracy|reason=据有的用户称,systemd 并未对用户组有特殊要求。尽管有很多不常用的用户组,但从 consolekit 切换到 logind 后这些用户组没有发生改变。''(据译者理解:如果你碰到特殊的权限问题,可能仍需要加入这些用户组才能解决。)''}}<br />
* 使用 systemd 的时候'''无需'''将用户加入特殊[[Users and Groups (简体中文)|用户组]](如{{ic|sys}}、{{ic|disk}}、{{ic|lp}}、{{ic|network}}、{{ic|video}}、{{ic|audio}}、{{ic|optical}}、{{ic|storage}}、{{ic|scanner}}、{{ic|power}}等等)。加入这些组反而会有问题,例如audio组会导致程序阻塞软件混声。每个 PAM 登录都拥有一个 logind 会话,它通过[[Wikipedia:Access control list|POSIX ACLs]],赋予本地会话以声音设备访问权限、通过[[udisks]]挂载和卸载移动设备的权限等。<br />
{{警告|如果您准备将用户移出UNIX通用的基础用户组,请三思而后行。某些应用程序,如 '''mplayer''' 和 '''fbida''',需要访问 {{ic|/dev/fb*}} 的权限,因此必须加入 '''video''' 用户组。还有 k3b,需要用户加入 '''optical''' 组以访问旧式光学设备。正确的方法是使用 [https://mailman.archlinux.org/pipermail/arch-general/2012-October/031808.html udev] 获取[https://bbs.archlinux.org/viewtopic.php?pid&#61;1183731#p1183731 访问权限]。}}<br />
* 阅读 [[Network Configuration (简体中文)]],了解如何配置网络。<br />
<br />
== 原生 systemd 配置文件 ==<br />
{{注意|可能需要手动创建某些文件。所有文件的权限都是644,属主 root,属组 root。}}<br />
<br />
=== 虚拟控制台 ===<br />
{{ic|/etc/vconsole.conf}} 文件用来配置虚拟控制台,包括键盘布局和控制台字体:<br />
{{hc|/etc/vconsole.conf|<nowiki><br />
KEYMAP=us<br />
FONT=lat9w-16<br />
FONT_MAP=8859-1_to_uni</nowiki>}}<br />
<br />
{{注意|{{ic|{{Pkg|systemd}}-194}} 默认使用内核字体和键盘布局,因此以后不必在上述文件中留下空的 {{ic|1=KEYMAP=}} 和 {{ic|1=FONT=}} 设置。}}<br />
<br />
还可以通过命令设置键盘布局:<br />
# localectl set-keymap de<br />
<br />
{{ic|localectl}} 也可以设置X11的键盘布局:<br />
<br />
# localectl set-x11-keymap de<br />
<br />
详情参见 {{ic|man 1 localectl}} 和 {{ic|man 5 vconsole.conf}}。<br />
<br />
* 另参阅 [[Fonts (简体中文)#控制台字体|控制台字体]] 和 [[KEYMAP (简体中文)#键盘布局|键盘布局]]。<br />
<br />
=== 硬件时钟 ===<br />
<br />
'''systemd''' 默认硬件时钟为协调世界时(UTC)。<br />
<br />
{{小贴士|推荐使用[[NTP (简体中文)|NTP服务]]来在线同步硬件时钟。}}<br />
<br />
==== 硬件时钟设定为地方时 ====<br />
<br />
将硬件时钟配置为地方时('''不建议'''):<br />
<br />
# timedatectl set-local-rtc true<br />
<br />
重新调整为 UTC:<br />
<br />
# timedatectl set-local-rtc false<br />
<br />
如果设置成本地时间,处理夏令时有些麻烦。如果夏令时调整发生在关机时,下次启动时时间会出现问题([http://www.cl.cam.ac.uk/~mgk25/mswish/ut-rtc.html 更多信息])。最新的内核直接从实时时钟芯片(RTC)读取时间,不使用 {{ic|hwclock}},内核把从 RTC 读取的时间当作 UTC 处理。所以如果硬件时间是地方时,系统启动一开始识别的时间是错误的,之后很快会进行矫正。这可能导致一些问题(尤其是时间倒退时)。<br />
<br />
如果同时安装了 Windows 操作系统([http://blogs.msdn.com/b/oldnewthing/archive/2004/09/02/224672.aspx 默认使用地方时]),那么一般 RTC 会被设置为地方时。Windows 其实也能处理 UTC,需要[[Time (简体中文)#Windows 系统使用 UTC|修改注册表]]。建议让 Windows 使用 UTC,而非让 Linux 使用地方时。Windows 使用 UTC 后,请记得禁用 Windows 的时间同步功能,以防 Windows 错误设置硬件时间。如上文所说,Linux 可以使用[[NTP (简体中文)|NTP服务]]来在线同步硬件时钟。<br />
<br />
* 详情参阅 [[Time (简体中文)]]。<br />
<br />
=== 内核模块 ===<br />
目前,所有必要模块的加载均由 [[udev]] 自动完成。所以,如果不需要使用任何额外的模块,就没有必要在任何配置文件中添加启动时加载的模块。但是,有些情况下可能需要在系统启动时加载某个额外的模块,或者将某个模块列入黑名单以便使系统正常运行。<br />
<br />
==== 开机加载 ====<br />
systemd 读取 {{ic|/etc/modules-load.d/}} 中的配置加载额外的内核模块。配置文件名称通常为 {{ic|/etc/modules-load.d/<program>.conf}}。格式很简单,一行一个要读取的模块名,而空行以及第一个非空格字符为{{ic|#}}或{{ic|;}}的行会被忽略,如:<br />
{{hc|/etc/modules-load.d/virtio-net.conf|<nowiki><br />
# Load virtio-net.ko at boot<br />
virtio-net</nowiki>}}<br />
<br />
另见{{ic|man 5 modules-load.d}}。<br />
<br />
==== 配置内核模块参数 ====<br />
<br />
在 {{ic|/etc/modprobe.d/modprobe.conf}} 中设置额外的模块参数。<br />
<br />
例如:<br />
<br />
* 我们已经在 {{ic|/etc/modules-load.d/loop.conf}} 中添加文本 {{ic|loop}},告诉系统开机自动加载该模块。<br />
<br />
* 在 {{ic|/etc/modprobe.d/modprobe.conf}} 设置额外的参数,如:{{ic|options loop max_loop&#61;64}}。<br />
<br />
最后,可以通过 {{ic|cat /sys/module/loop/parameters/max_loop}} 命令检查配置是否生效。<br />
<br />
==== 禁用内核模块 ====<br />
禁用内核模块的方法和 Arch 默认的 {{Pkg|initscripts}} 相同,因为该过程实际由 {{Pkg|kmod}} 处理。参见:[[Kernel modules (简体中文)#黑名单]]。<br />
<br />
=== 文件系统挂载 ===<br />
<br />
默认行为是:在启动一个需要挂载特定分区的服务之前,系统自动检查并挂载分区。{{ic|/etc/fstab}} 中设定的网络文件系统(如 [[NFS (简体中文)|NFS]]、[[Samba (简体中文)|Samba]])无需配置即可正常工作,systemd 将确保网络文件系统在网络链接就绪后挂载。<br />
<br />
详情参阅:{{ic|man 5 systemd.mount}}。<br />
<br />
==== 自动挂载 ====<br />
<br />
* 如果 {{ic|/home}} 分区较大,可以让不依赖 {{ic|/home}} 分区的服务先启动。把下面的参数添加到 {{ic|/etc/fstab}} 文件中 {{ic|/home}} 项目的参数部分即可:<br />
<br />
noauto,x-systemd.automount<br />
<br />
这样 {{ic|/home}} 分区只有需要访问时才会被挂载。内核会缓存所有的文件操作,直到 {{ic|/home}} 分区准备完成。<br />
<br />
{{注意|这样做会使 {{ic|/home}} 的文件系统类型被识别为 {{ic|autofs}},造成 [[mlocate]] 查询时忽略该目录。实际加速效果因配置而异,所以请自己权衡是否需要。}}<br />
<br />
* 挂载远程文件系统也是同理。如果你仅想在需要的时候才挂载,也可以添加 {{ic|noauto,x-systemd.automount}} 参数。另外,可以设置 {{ic|1=x-systemd.device-timeout=#}} 参数,设置超时时间,以防止网络资源不能访问的时候浪费时间。<br />
<br />
* 如果你的加密文件系统需要密钥,则需要添加 {{ic|noauto}} 参数到 {{ic|/etc/crypttab}} 文件中的对应位置。systemd 开机的时候就不会打开这个加密设备,会一直等待到设备被访问时再使用密钥文件挂载。比如在使用加密RAID设备的时候可以节省一定的时间,因为 systemd 不必等到设备可用后才能访问。例如:<br />
<br />
{{hc|/etc/crypttab|<br />
data /dev/md0 /root/key noauto}}<br />
<br />
==== LVM ====<br />
<br />
如果装有不通过 [[Mkinitcpio (简体中文)|initramfs]] 激活的[[LVM (简体中文)|LVM]]卷,则需启动 {{ic|lvm-monitoring}} 服务(由 {{pkg|lvm2}} 软件包提供):<br />
<br />
# systemctl enable lvm-monitoring<br />
<br />
=== ACPI 电源管理 ===<br />
'''systemd''' 能够处理某些电源相关的 [[Wikipedia:Advanced_Configuration_and_Power_Interface|ACPI]] 事件,通过 {{ic|/etc/systemd/logind.conf}} 的下列选项配置:<br />
* {{ic|HandlePowerKey}}:按下电源键后的动作<br />
* {{ic|HandleSleepKey}}:按下挂起键后的动作<br />
* {{ic|HandleHibernateKey}}: 按下休眠键后的动作<br />
* {{ic|HandleLidSwitch}}:合上笔记本盖后待机<br />
<br />
动作可以是 {{ic|ignore}}、{{ic|poweroff}}、{{ic|reboot}}、{{ic|halt}}、{{ic|suspend}}、{{ic|hibernate}}、{{ic|hybrid-sleep}}、{{ic|lock}} 或 {{ic|kexec}}。<br />
<br />
系统默认设置为:<br />
HandlePowerKey=poweroff<br />
HandleSuspendKey=suspend<br />
HandleHibernateKey=hibernate<br />
HandleLidSwitch=suspend<br />
<br />
不用图形界面、或者使用 [[i3 (简体中文)|i3]]、[[awesome (简体中文)|awesome]] 这样简单的桌面管理器时,systemd 可以替代 [[acpid (简体中文)|acpid]] 处理 ACPI 事件。<br />
<br />
{{注意|运行 {{ic|systemctl restart systemd-logind}},使上述更改立即生效。}}<br />
<br />
{{注意|systemd 无法处理交流电源和电池 ACPI 事件,所以还得使用 [[Laptop Mode Tools (简体中文)|Laptop Mode Tools]] 或 [[acpid (简体中文)|acpid]] 工具。}}<br />
<br />
在当前版本的 systemd 中,这些 {{ic|Handle}} 选项将会被应用到整个系统当中,除非它们被别的程序——例如某个桌面环境中的电源管理器——给“阻止”(inhibited)。如果其它的程序没有阻止这些 {{ic|Handle}} ,你可能会先被 systemd 挂起你的系统,然后当系统被唤醒之后,电源管理器又会再次将系统挂起。<br />
<br />
{{警告|目前只有[[GNOME]] 和 [[KDE]] 支持 "inhibited" 命令。在其它的桌面管理器同样实现该功能之前,如果你想使用[[Xfce]]、[[acpid]] 或者其它程序来管理 ACPI 事件,你需要把 {{ic|Handle}} 选项设置为 {{ic|ignore}}。}}<br />
<br />
{{注意|除了内核默认的待机支持后端(用于处理待机/休眠),systemd 也可以使用其他后端(比如 [[Uswsusp]] 或 [[TuxOnIce]])。}}<br />
<br />
要令 {{ic|systemctl hibernate}} 工作,需要按照[[Pm-utils#Hibernation_.28suspend2disk.29|休眠]]和 [[Pm-utils#Mkinitcpio_Resume_Hook|mkinitcpio 唤醒扩展]]的设置步骤进行操作。(不必安装 {{ic|pm-utils}})<br />
<br />
==== 休眠时执行的脚本 ====<br />
<br />
使用 {{ic|systemctl suspend}}、{{ic|systemctl hibernate}} 或 {{ic|systemctl hybrid-sleep}} 命令执行待机/休眠时,systemd 不会调用 [[pm-utils (简体中文)|pm-utils]]。[[pm-utils (简体中文)|pm-utils]] 的钩子扩展(hook)——包括 [[pm-utils (简体中文)#建立你自己的钩子|自定义钩子]]——会失效。不过,systemd 提供了两种类似的待机/休眠时执行脚本的机制。<br />
<br />
===== 使用服务文件 =====<br />
<br />
可以将服务文件附在 suspend.target、hibernate.target 或 sleep.target 中,这样就能在待机/休眠前后执行某些操作。用户级操作和root/系统级操作应该使用不同的服务文件。要启用用户级服务文件,使用 {{ic|# systemctl enable suspend@<用户名> && systemctl enable resume@<用户名>}}。例如:<br />
<br />
{{hc|/etc/systemd/system/suspend@.service|2=<nowiki><br />
[Unit]<br />
Description=User suspend actions<br />
Before=sleep.target<br />
<br />
[Service]<br />
User=%I<br />
Type=forking<br />
Environment=DISPLAY=:0<br />
ExecStartPre= -/usr/bin/pkill -u %u unison ; /usr/local/bin/music.sh stop ; /usr/bin/mysql -e 'slave stop'<br />
ExecStart=/usr/bin/sflock<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
{{hc|/etc/systemd/system/resume@.service|2=<nowiki><br />
[Unit]<br />
Description=User resume actions<br />
After=suspend.target<br />
<br />
[Service]<br />
User=%I<br />
Type=simple<br />
ExecStartPre=/usr/local/bin/ssh-connect.sh<br />
ExecStart=/usr/bin/mysql -e 'slave start'<br />
<br />
[Install]<br />
WantedBy=suspend.target</nowiki>}}<br />
<br />
至于root/系统级服务(使用 {{ic|# systemctl enable root-suspend}} 激活):<br />
<br />
{{hc|/etc/systemd/system/root-resume.service|2=<nowiki><br />
[Unit]<br />
Description=Local system resume actions<br />
After=suspend.target<br />
<br />
[Service]<br />
Type=simple<br />
ExecStart=/usr/bin/systemctl restart mnt-media.automount<br />
<br />
[Install]<br />
WantedBy=suspend.target</nowiki>}}<br />
<br />
{{hc|/etc/systemd/system/root-suspend.service|2=<nowiki><br />
[Unit]<br />
Description=Local system suspend actions<br />
Before=sleep.target<br />
<br />
[Service]<br />
Type=simple<br />
ExecStart=-/usr/bin/pkill sshfs<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
上述服务文件的一些解释(详见 {{ic|man systemd.service}}):<br />
* 如果设置 {{ic|1=<nowiki>Type=OneShot</nowiki>}},那么可以使用多个 {{ic|1=<nowiki>ExecStart=</nowiki>}} 参数。否则只能写一个,替代方案是在 {{ic|ExecStartPre}} 中添加命令,或使用分号分隔不同命令(见第一个例子,分号前后的空格都是'''必须'''的)。 <br />
* 若命令前加上一个“-”(半角减号),则命令返回非零值时会被忽略、当作正常执行处理。<br />
* 有关调试,最好的方法是用 {{ic|journalctl}} 查看日志。<br />
<br />
===== 合并待机和唤醒服务文件 =====<br />
<br />
利用下面这种自定义的一体化待机和唤醒服务,使用单一的钩子扩展即可对不同操作(待机/休眠/混合休眠)的不同阶段(进入/唤醒)进行控制。<br />
<br />
例子和解释:<br />
<br />
{{hc|/etc/systemd/system/wicd-sleep.service|2=<nowiki><br />
[Unit]<br />
Description=Wicd sleep hook<br />
Before=sleep.target<br />
StopWhenUnneeded=yes<br />
<br />
[Service]<br />
Type=oneshot<br />
RemainAfterExit=yes<br />
ExecStart=-/usr/share/wicd/daemon/suspend.py<br />
ExecStop=-/usr/share/wicd/daemon/autoconnect.py<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
* {{ic|1=<nowiki>RemainAfterExit=yes</nowiki>}}:服务启动后,除非显式地停止,否则就认为是活动的。<br />
* {{ic|1=<nowiki>StopWhenUnneeded=yes</nowiki>}}:服务活动时,如果无其他服务依赖该服务,就停止它。在本例中,该服务会在 sleep.target 停止后停止活动。<br />
* 由于 sleep.target 会被 suspend.target、hibernate.target、hybrid-sleep.target 调用,且 sleep.target 本身设置了 StopWhenUnneeded,该服务文件对以上各种操作都是有效的。<br />
<br />
===== 使用 /usr/lib/systemd/system-sleep =====<br />
<br />
systemd 在待机/休眠时执行 {{ic|/usr/lib/systemd/system-sleep/}} 里的所有脚本,传递下面两个参数:<br />
<br />
* 参数1:若是准备进入待机/休眠状态,则为 {{ic|pre}};唤醒时为 {{ic|post}}。<br />
* 参数2:事件名称,{{ic|suspend}},{{ic|hibernate}} 或 {{ic|hybrid-sleep}}。<br />
<br />
systemd 会同时执行所有脚本,而不是像 [[pm-utils (简体中文)|pm-utils]] 那样顺序执行。<br />
<br />
脚本输出会记录在相关服务({{ic|systemd-suspend.service}}、{{ic|systemd-hibernate.service}} 或 {{ic|systemd-hybrid-sleep.service}})中。通过[[#日志|日志]]查看:<br />
# journalctl -b -u systemd-suspend<br />
<br />
{{注意|除了使用自定义脚本,还可以利用 {{ic|sleep.target}}、{{ic|suspend.target}}、{{ic|hibernate.target}} 或 {{ic|hybrid-sleep.target}} 来为单元(unit)添加睡眠状态策略。}}<br />
<br />
脚本示范:<br />
{{hc|/usr/lib/systemd/system-sleep/example.sh|<br />
#!/bin/sh<br />
case $1/$2 in<br />
pre/*)<br />
echo "进入 $2 状态..."<br />
;;<br />
post/*)<br />
echo "从 $2 状态唤醒..."<br />
;;<br />
esac}}<br />
<br />
记得添加可执行权限: <br />
# chmod a+x /usr/lib/systemd/system-sleep/example.sh<br />
<br />
详情参见 {{ic|man 7 systemd.special}} 和 {{ic|man 8 systemd-sleep}}。<br />
<br />
=== 临时文件 ===<br />
{{ic|/usr/lib/tmpfiles.d/}} 和 {{ic|/etc/tmpfiles.d/}} 中的文件描述了 systemd-tmpfiles 如何创建、清理、删除临时文件和目录,这些文件和目录通常存放在 {{ic|/run}} 和 {{ic|/tmp}} 中。配置文件名称为 {{ic|/etc/tmpfiles.d/<program>.conf}}。此处的配置能覆盖 {{ic|/usr/lib/tmpfiles.d/}} 目录中的同名配置。<br />
<br />
临时文件通常和服务文件同时提供,以生成守护进程需要的文件和目录。例如 [[Samba (简体中文)|Samba]] 服务需要目录 {{ic|/run/samba}} 存在并设置正确的权限位,就象这样:<br />
{{hc|/usr/lib/tmpfiles.d/samba.conf|<br />
D /run/samba 0755 root root<br />
}}<br />
<br />
此外,临时文件还可以用来在开机时向特定文件写入某些内容。比如,要禁止系统从USB设备唤醒,利用旧的 {{ic|/etc/rc.local}} 可以用 {{ic|echo USBE > /proc/acpi/wakeup}},而现在可以这么做:<br />
{{hc|/etc/tmpfiles.d/disable-usb-wake.conf|<br />
w /proc/acpi/wakeup - - - - USBE<br />
}}<br />
<br />
详情参见 {{ic|man 5 tmpfiles.d}}。<br />
<br />
{{注意|该方法不能向 {{ic|/sys}} 中的配置文件添加参数,因为 {{ic|systemd-tmpfiles-setup}} 有可能在相关模块加载前运行。这种情况下,需要首先通过 {{ic|modinfo <模块名>}} 确认需要的参数,并在 [[Kernel modules (简体中文)#选项|{{ic|/etc/modprobe.d}} 下的一个文件]]中设置改参数。另外,还可以使用 [[udev (简体中文)#udev规则|udev 规则]],在设备就绪时设置相应属性。}}<br />
<br />
=== 单元(unit) ===<br />
一个单元配置文件可以描述如下内容之一:系统服务、socket、系统设备、挂载点、交换分区/文件、启动目标(target)、文件系统路径、由 systemd 管理的计时器。文件格式受 .desktop 文件启发,而最初起源是 Windows 下的 .ini 文件。<br />
<br />
详情参阅 {{ic|man 5 systemd.unit}}.<br />
<br />
== 从 initscripts 迁移到 systemd ==<br />
<br />
=== initscripts 模拟 ===<br />
<br />
{{Pkg|initscripts}} 软件包提供了 systemd 和 Arch 传统配置的整合。当同时安装了{{Pkg|initscripts}}和systemd时,若以systemd启动系统,systemd将执行以下操作:<br />
<br />
# 解析{{ic|/etc/rc.conf}}中的{{ic|DAEMONS}}数组,并在系统启动时,启动所有列出的守护进程<br />
# 在启动过程中执行{{ic|/etc/rc.local}}<br />
# 在系统关闭过程中执行{{ic|/etc/rc.local.shutdown}}<br />
<br />
Initscripts模拟仅仅是作为用户迁移到systemd时的过渡措施,这一方案最终将'''不再可行'''。原生的systemd并不依赖于{{ic|rc.conf}}文件的集中式配置,因此推荐使用[[#原生 systemd 配置文件|原生的systemd配置文件]]。原生systemd配置文件比{{ic|/etc/rc.conf}}具有更高优先级。<br />
<br />
{{注意|替换{{ic|/etc/rc.local}}的推荐方式是针对系统启动时要运行的程序编写自定义的service文件,详见相关[[#自己编写_.service_文件|章节]].}}<br />
<br />
{{注意|如果你曾经在{{ic|/etc/inittab}}中禁用了以{{keypress|Ctrl+Alt+Del}}重启系统,则需要以root执行{{ic|systemctl mask ctrl-alt-del.target}}以使其对systemd重新生效。}}<br />
<br />
{{警告|如果同时安装 systemd(197-4 及后续版本)和 initscripts,那么 {{ic|/etc/rc.local}} 脚本会导致启动过程无法结束(原因是 getty@tty1.service 自 197-4 版本起不再等待 rc-local.service 执行,getty 无法启动)。请删除或重命名 {{ic|/etc/rc.local}}。}}<br />
<br />
=== 迁移 DAEMONS 列表 ===<br />
<br />
若要用纯 systemd 环境,则应当删除 {{ic|/etc/rc.conf}} 文件,并完全通过 {{ic|systemctl}} 命令来启动服务。对于 {{ic|DAEMONS}} 列表里面的每个 {{ic|<服务名>}},现在可以用以下命令设置成自动启动:<br />
<br />
# systemctl enable <服务名>.service<br />
<br />
{{小贴士|[[Daemons List|这里]]有一份 initscripts 和 systemd 下常见服务的对应列表。}}<br />
<br />
如果提示 {{ic|<service_name>.service}} 不存在:<br />
<br />
* 很有可能是 systemd 用了不同的服务名。比如 {{ic|crond}} 服务替换成 {{ic|cronie.service}};{{ic|alsa}} 服务替换成了 {{ic|alsa-store.service}} 和 {{ic|alsa-restore.service}} 两个服务。另一个值得注意的是 {{ic|network}} 服务,它被替换为一整套服务文件(详情参见 [[Configuring_Network_(简体中文)]])。<br />
* 另外,有些程序尚未提供 .service 文件,还得继续用 {{ic|rc.conf}} 来配置开机自动启动。<br />
<br />
{{小贴士|用下面这个命令来查看已安装软件包包含的服务文件:<br />
$ pacman -Ql cronie<br />
[...]<br />
cronie /etc/rc.d/crond # DAEMONS 列表中使用的 initscript 服务 (原生 systemd 配置中不再使用)<br />
[...]<br />
cronie /usr/lib/systemd/system/cronie.service # 原生 systemd 服务文件<br />
[...]<br />
}}<br />
<br />
* 有些服务不需要用户明确配置自启动。比如安装 {{ic|dbus-core}} 软件包后 {{ic|dbus.service}} 就会自动被配置为自启动。而 {{ic|alsa-store.service}} 和 {{ic|alsa-restore.service}} 也会由 systemd 自动启动。可以用 {{ic|systemctl list-unit-files}} 命令来查看服务列表并检查运行状态。<br />
<br />
== systemd 基本工具 ==<br />
<br />
检视和控制systemd的主要命令是{{ic|systemctl}}。该命令可用于查看系统状态和管理系统及服务。详见{{ic|man 1 systemctl}}。<br />
<br />
{{小贴士|在 {{ic|systemctl}} 参数中添加 {{ic|-H <用户名>@<主机名>}} 可以实现对其他机器的远程控制。该过程使用 [[SSH (简体中文)|SSH]] 链接。}}<br />
<br />
{{注意|{{ic|systemadm}} 是 systemd 的官方图形前端。由 [[AUR (简体中文)|AUR]] 中的软件包 {{AUR|systemd-ui-git}} 提供。}}<br />
<br />
=== 分析系统状态 ===<br />
<br />
输出激活的单元:<br />
<br />
$ systemctl<br />
<br />
以下命令等效:<br />
<br />
$ systemctl list-units<br />
<br />
输出运行失败的单元:<br />
<br />
$ systemctl --failed<br />
<br />
所有可用的单元文件存放在 {{ic|/usr/lib/systemd/system/}} 和 {{ic|/etc/systemd/system/}} 目录(后者优先级更高)。查看所有已安装服务:<br />
$ systemctl list-unit-files<br />
<br />
=== 使用单元 ===<br />
一个单元可以是系统服务({{ic|.service}})、挂载点({{ic|.mount}})、sockets({{ic|.sockets}})。<br />
<br />
使用 {{ic|systemctl}} 控制单元时,通常需要使用单元文件的全名,包括扩展名(例如 {{ic|sshd.service}})。但是有些单元可以在{{ic|systemctl}}中使用简写方式。<br />
* 如果无扩展名,systemctl 默认把扩展名当作 {{ic|.service}}。例如 {{ic|netcfg}} 和 {{ic|netcfg.service}} 是等价的。<br />
* 挂载点会自动转化为相应的 {{ic|.mount}} 单元。例如 {{ic|/home}} 等价于 {{ic|home.mount}}。<br />
* 设备会自动转化为相应的 {{ic|.device}} 单元,所以 {{ic|/dev/sda2}} 等价于 {{ic|dev-sda2.device}}。<br />
<br />
立即激活单元:<br />
<br />
# systemctl start <单元><br />
<br />
立即停止单元:<br />
# systemctl stop <单元><br />
<br />
重启单元:<br />
<br />
# systemctl restart <单元><br />
<br />
命令单元重新读取配置:<br />
<br />
# systemctl reload <单元><br />
<br />
输出单元运行状态:<br />
<br />
$ systemctl status <单元><br />
<br />
检查单元是否配置为自动启动:<br />
$ systemctl is-enabled <单元><br />
<br />
开机自动激活单元:<br />
<br />
# systemctl enable <单元><br />
<br />
{{注意|如果服务没有{{ic|Install}}段落,一般意味着应该通过其它服务自动调用它们。如果真的需要手动安装,可以直接连接服务,如下(将{{ic|foo}}替换为真实的服务名):<br />
{{bc|# ln -s /usr/lib/systemd/system/foo.service /etc/systemd/system/graphical.target.wants/}}<br />
}}<br />
<br />
取消开机自动激活单元:<br />
<br />
# systemctl disable <单元><br />
<br />
显示单元的手册页(必须由单元文件提供):<br />
<br />
# systemctl help <单元><br />
<br />
重新载入 systemd,扫描新的或有变动的单元:<br />
# systemctl daemon-reload<br />
<br />
=== 电源管理 ===<br />
<br />
安装 {{ic|polkit}} 后才可使用电源管理。<br />
<br />
如果你正登录在一个本地的{{ic|systemd-logind}}用户会话,且当前没有其它活动的会话,那么以下命令无需root权限即可执行。否则(例如,当前有另一个用户登录在某个tty),systemd 将会自动请求输入root密码。<br />
<br />
重启:<br />
<br />
$ systemctl reboot<br />
<br />
退出系统并停止电源:<br />
<br />
$ systemctl poweroff<br />
<br />
待机:<br />
<br />
$ systemctl suspend<br />
<br />
休眠:<br />
<br />
$ systemctl hibernate<br />
<br />
混合休眠模式(同时休眠到硬盘并待机):<br />
<br />
$ systemctl hybrid-sleep<br />
<br />
== 启动登录管理器 ==<br />
<br />
通过启动[[Display Manager (简体中文)|登录管理器]](或称显示管理器),即可进行图形界面登录。<br />
要启用图形界面登录,运行适当的[[Display Manager (简体中文)|登录管理器]](或称显示管理器)即可。目前,Arch 提供了 [[GDM (简体中文)|GDM]]、[[KDM (简体中文)|KDM]]、[[SLiM (简体中文)|SLiM]]、[[XDM (简体中文)|XDM]]、[[LXDM (简体中文)|LXDM]]、[[LightDM (简体中文)|LightDM]] 和 {{AUR|SDDM}} 的 systemd 服务文件。以 KDM 为例,配置开机启动:<br />
# systemctl enable kdm.service<br />
<br />
执行上述命令后,登录管理器应当能正常工作了。如果不是的话,很可能是因为你修改了{{ic|default.target}}。默认情况应当如下:<br />
<br />
{{hc|# ls -l /etc/systemd/system/default.target|/etc/systemd/system/default.target -> /usr/lib/systemd/system/graphical.target}}<br />
<br />
删除被修改的 {{ic|default.target}} 即可,systemd 会自动使用默认配置(即 {{ic|graphical.target}}):<br />
<br />
# rm /etc/systemd/system/default.target<br />
<br />
=== 使用 systemd-logind ===<br />
<br />
{{注意|自 2012-10-30 起, [[ConsoleKit]] 已被 [https://www.archlinux.org/news/consolekit-replaced-by-logind/ systemd-logind 取代] ,作为登录到桌面环境的默认方式。}}<br />
<br />
可使用 {{ic|loginctl}} 来查看用户会话的状态。所有 [[PolicyKit]] 操作,如挂起系统、挂载外部驱动器,都无需配置即可使用。<br />
<br />
$ loginctl show-session $XDG_SESSION_ID<br />
<br />
== 自己编写 .service 文件 ==<br />
''参见:[[Systemd/Services]]''<br />
<br />
=== 处理依赖关系 ===<br />
<br />
使用systemd时,可通过正确编写单元配置文件来解决其依赖关系。典型的情况是,单元{{ic|A}}要求单元{{ic|B}}在{{ic|A}}启动之前运行。在此情况下,向单元{{ic|A}}配置文件中的 {{ic|[Unit]}} 段添加 {{ic|1=Requires=B}} 和 {{ic|1=After=B}} 即可。若此依赖关系是可选的,可添加 {{ic|1=Wants=B}} 和 {{ic|1=After=B}}。请注意 {{ic|1=Wants=}} 和 {{ic|1=Requires=}} 并不意味着 {{ic|1=After=}},即如果 {{ic|1=After=}} 选项没有制定,这两个单元将被并行启动。<br />
<br />
依赖关系通常被用在服务(service)而不是[[#目标(target)|目标(target)]]上。例如, {{ic|network.target}} 一般会被某个配置网络接口的服务引入,所以,将自定义的单元排在该服务之后即可,因为 {{ic|network.target}} 已经启动。<br />
<br />
=== 启动方式 ===<br />
<br />
编写自定义的service文件时,可以选择几种不同的服务启动方式。启动方式可通过配置文件 {{ic|[Service]}} 段中的 {{ic|1=Type=}} 参数进行设置。具体的参数说明请参阅 {{ic|man systemd.service}} 。<br />
<br />
* {{ic|1=Type=simple}}(默认值):systemd认为该服务将立即启动。服务进程不会fork。如果该服务要启动其他服务,不要使用此类型启动,除非该服务是socket激活型。<br />
* {{ic|1=Type=forking}}:systemd认为当该服务进程fork,且父进程退出后服务启动成功。对于常规的守护进程(daemon),除非你确定此启动方式无法满足需求,使用此类型启动即可。使用此启动类型应同时指定 {{ic|1=PIDFile=}},以便systemd能够跟踪服务的主进程。<br />
* {{ic|1=Type=oneshot}}:这一选项适用于只执行一项任务随后退出的服务。你可能需要同时设置 {{ic|1=RemainAfterExit=yes}} 使得 systemd 在服务进程退出之后仍然认为服务处于激活状态。<br />
* {{ic|1=Type=notify}}:与 {{ic|1=Type=simple}} 相同,但约定服务会在就绪后向 systemd 发送一个信号。这一通知的实现由 {{ic|libsystemd-daemon.so}} 提供。<br />
* {{ic|1=Type=dbus}}:若以此方式启动,当指定的 {{ic|BusName}} 出现在DBus系统总线上时,systemd认为服务就绪。<br />
<br />
=== 修改现存单元文件 ===<br />
<br />
To edit a unit file provided by a package, you can create a directory called {{ic|/etc/systemd/system/<unit>.d/}} for example {{ic|/etc/systemd/system/httpd.service.d/}} and place {{ic|*.conf}} files in there to override or add new options. '''systemd''' will parse these {{ic|*.conf}} files and apply them on top of the original unit. For example, if you simply want to add an additional dependency to a unit, you may create the following file:<br />
<br />
{{hc|/etc/systemd/system/<unit>.d/customdependency.conf|2=<br />
[Unit]<br />
Requires=<新依赖><br />
After=<新依赖>}}<br />
<br />
然后运行以下命令使更改生效:<br />
<br />
# systemctl daemon-reload<br />
# systemctl restart <单元><br />
<br />
Alternatively you can copy the old unit file from {{ic|/usr/lib/systemd/system/}} to {{ic|/etc/systemd/system/}} and make your changes there. A unit file in {{ic|/etc/systemd/system/}} always overrides the same unit in {{ic|/usr/lib/systemd/system/}}. Note that when the original unit in {{ic|/usr/lib/}} is changed due to a package upgrade, these changes will not automatically apply to your custom unit file in {{ic|/etc/}}. Additionally you will have to manually reenable the unit with {{ic|systemctl reenable <unit>}}. It is therefore recommended to use the {{ic|*.conf}} method described before instead.<br />
<br />
{{小贴士|可以用 {{ic|systemd-delta}} 命令来查看哪些单元文件被覆盖、哪些被修改。}}<br />
<br />
=== 单元配置文件的 vim 语法高亮支持 ===<br />
<br />
可从 [[Official Repositories|官方仓库]] 安装 {{Pkg|vim-systemd}} 软件包,使 unit 配置文件在 [[Vim]] 下支持语法高亮。<br />
<br />
== 目标(target) ==<br />
启动级别(runlevel)是一个旧的概念。现在,systemd 引入了一个和启动级别功能相似又不同的概念——目标(target)。不像数字表示的启动级别,每个''目标''都有名字和独特的功能,并且能同时启用多个。一些''目标''继承其他''目标''的服务,并启动新服务。systemd 提供了一些模仿 sysvinit 启动级别的''目标'',仍可以使用旧的 {{ic|telinit 启动级别}} 命令切换。<br />
<br />
=== 获取当前目标 ===<br />
不要使用 {{ic|runlevel}} 命令了:<br />
$ systemctl list-units --type=target<br />
<br />
=== 创建新目标 ===<br />
在 Fedora 中,启动级别 0、1、3、5、6 都被赋予特定用途,并且都对应一个 systemd 的''目标''。然而,没有什么很好的移植用户定义的启动级别(2、4)的方法。要实现类似功能,可以以原有的启动级别为基础,创建一个新的''目标'' {{ic|/etc/systemd/system/<新目标>}}(可以参考 {{ic|/usr/lib/systemd/system/graphical.target}}),创建 {{ic|/etc/systemd/system/<新目标>.wants}} 目录,向其中加入额外服务的链接(指向 {{ic|/usr/lib/systemd/system/}} 中的单元文件)。<br />
<br />
=== 目标表 ===<br />
{| border="1"<br />
!SysV 启动级别!!Systemd 目标!!注释<br />
|-<br />
| 0 || runlevel0.target, poweroff.target || 中断系统(halt)<br />
|-<br />
| 1, s, single || runlevel1.target, rescue.target || 单用户模式<br />
|-<br />
| 2, 4 || runlevel2.target, runlevel4.target, multi-user.target || 用户自定义启动级别,通常识别为级别3。<br />
|-<br />
| 3 || runlevel3.target, multi-user.target || 多用户,无图形界面。用户可以通过终端或网络登录。<br />
|-<br />
| 5 || runlevel5.target, graphical.target || 多用户,图形界面。继承级别3的服务,并启动图形界面服务。<br />
|-<br />
| 6 || runlevel6.target, reboot.target || 重启<br />
|-<br />
| emergency || emergency.target || 急救模式(Emergency shell)<br />
|-<br />
|}<br />
<br />
=== 切换启动级别/目标 ===<br />
systemd 中,启动级别通过“目标单元”访问。通过如下命令切换:<br />
# systemctl isolate graphical.target<br />
该命令对下次启动无影响。等价于{{ic|telinit 3}} 或 {{ic|telinit 5}}。<br />
<br />
=== 修改默认启动级别/目标 ===<br />
开机启动进的目标是 {{ic|default.target}},默认链接到 {{ic|graphical.target}} (大致相当于原来的启动级别5)。可以通过[[Kernel parameters (简体中文)|内核参数]]更改默认启动级别:<br />
<br />
{{小贴士|可以省略扩展名 {{ic|.target}}。}}<br />
<br />
* {{ic|1=systemd.unit=multi-user.target}} (大致相当于级别3)<br />
* {{ic|1=systemd.unit=rescue.target}} (大致相当于级别1)<br />
<br />
另一个方法是修改 {{ic|default.target}}。可以通过 {{ic|systemctl}} 修改它:<br />
<br />
# systemctl enable multi-user.target<br />
<br />
命令执行情况由 {{ic|systemctl}} 显示:链接 {{ic|/etc/systemd/system/default.target}} 被创建,指向新的默认启动级别。该方法当且仅当目标配置文件中有以下内容时有效:<br />
<br />
[Install]<br />
Alias=default.target<br />
<br />
目前,{{ic|multi-user.target}}、{{ic|graphical.target}} 都包含这段内容。<br />
<br />
== 日志 ==<br />
systemd 提供了自己日志系统(logging system),称为 journal. 使用 systemd 日志,无需额外安装日志服务(syslog)。读取日志的命令:<br />
# journalctl<br />
<br />
默认情况下(当 {{ic|1=Storage=}} 在文件 {{ic|/etc/systemd/journald.conf}} 中被设置为 {{ic|auto}}),日志记录将被写入 {{ic|/var/log/journal/}}。该目录是 {{pkg|systemd}} 软件包的一部分。若被删除,systemd '''不会'''自动创建它,直到下次升级软件包时重建该目录。如果该目录缺失,systemd 会将日志记录写入 {{ic|/run/systemd/journal}}。这意味着,系统重启后日志将丢失。<br />
<br />
=== 过滤输出 ===<br />
<br />
{{ic|journalctl}}可以根据特定字段过滤输出,例如:<br />
<br />
显示本次启动后的所有日志:<br />
<br />
# journalctl -b<br />
<br />
不过,一般大家更关心的不是本次启动后的日志,而是上次启动时的(例如,刚刚系统崩溃了)。目前还没有这项功能,正在 [http://comments.gmane.org/gmane.comp.sysutils.systemd.devel/6608 systemd-devel@lists.freedesktop.org] 讨论中。<br />
<br />
目前的折中方案是:<br />
<br />
# journalctl --since=today | tac | sed -n '/-- Reboot --/{n;:r;/-- Reboot --/q;p;n;b r}' | tac<br />
<br />
以上命令输出本日内的所有启动信息。但要注意,如果日志很多,该命令执行时间会比较漫长。<br />
<br />
动态跟踪最新信息:<br />
# journalctl -f<br />
<br />
显示特定程序的所有消息:<br />
{{bc|# journalctl /usr/lib/systemd/systemd}}<br />
<br />
显示特定进程的所有消息:<br />
{{bc|1=# journalctl _PID=1}}<br />
<br />
显示指定单元的所有消息:<br />
# journalctl -u netcfg<br />
<br />
详情参阅{{ic|man journalctl}}、{{ic|man systemd.journal-fields}},以及 Lennert 的这篇[http://0pointer.de/blog/projects/journalctl.html 博文]。<br />
<br />
=== 日志大小限制 ===<br />
<br />
如果按上面的操作保留日志的话,默认日志最大限制为所在文件系统容量的 10%,即:如果 {{ic|/var/log/journal}} 储存在 50GiB 的根分区中,那么日志最多存储 5GiB 数据。可以修改 {{ic|/etc/systemd/journald.conf}} 中的 {{ic|SystemMaxUse}} 来指定该最大限制。如限制日志最大 50MiB:<br />
<br />
SystemMaxUse=50M<br />
<br />
详情参见 {{ic|man journald.conf}}.<br />
<br />
=== 配合 syslog 使用 ===<br />
systemd 提供了 socket {{ic|/run/systemd/journal/syslog}},以兼容传统日志服务。所有系统信息都会被传入。要使传统日志服务工作,需要让服务链接该 socket,而非 {{ic|/dev/log}}([http://lwn.net/Articles/474968/ 官方说明])。Arch 软件仓库中的 {{pkg|syslog-ng}} 已经包含了需要的配置。<br />
<br />
设置开机启动 syslog-ng:<br />
# systemctl enable syslog-ng<br />
<br />
[http://0pointer.de/blog/projects/ 这里]有一份很不错的 {{ic|journalctl}} 指南。<br />
<br />
== 优化 ==<br />
<br />
参阅:[[Improve Boot Performance (简体中文)]]。<br />
<br />
=== 启动过程分析 ===<br />
systemd 提供了一个分析启动过程的工具 —— {{ic|systemd-analyze}}。可以用它看看哪些单元拖慢了开机过程,并据此进行优化。<br />
<br />
查看开机过程在内核/用户空间消耗的时间:<br />
$ systemd-analyze<br />
{{小贴士|<br />
* 如果在 {{ic|/etc/mkinitcpio.conf}} 的 {{ic|HOOKS}} 数组添加 {{ic|timestamp}},并重新生成启动内存镜像({{ic|mkinitcpio -p linux}}),{{ic|systemd-analyze}} 还可以显示处理该启动镜像花费的时间。<br />
* 如果使用 [[UEFI]] 引导,且启动引导器实现了 systemd 的 [http://www.freedesktop.org/wiki/Software/systemd/BootLoaderInterface Boot Loader Interface](目前只有 [[Gummiboot]] 实现了),'''systemd-analyze''' 还可以显示 EFI 固件和启动引导器自身花费的时间。}}<br />
<br />
按照耗费时间顺序,输出启动每个单元耗费的时间:<br />
$ systemd-analyze blame<br />
<br />
生成类似于 [[Bootchart (简体中文)|bootchart]] 的开机过程图表:<br />
$ systemd-analyze plot > plot.svg<br />
<br />
==== 使用 systemd-bootchart ====<br />
<br />
自2012年10月17日,bootchart 工具已经合并进 systemd 中,使用方法和原来的 bootchart 大同小异,添加下列内容到内核参数即可:<br />
<br />
initcall_debug printk.time=y init=/usr/lib/systemd/systemd-bootchart<br />
<br />
==== 使用 bootchart2 ====<br />
<br />
由于没有办法在内核参数设置两个 init,所以不能使用源里的 bootchart。不过,[[AUR (简体中文)|AUR]] 软件包 {{AUR|bootchart2-git}} 提供了一个的 systemd 服务,这样就可以和 systemd 一起使用。安装后启用即可:<br />
{{bc|# systemctl enable bootchart.service}}<br />
详情参阅 [https://github.com/mmeeks/bootchart bootchart 文档]。<br />
<br />
=== 预读 ===<br />
systemd 自己实现了一个 readahead,可以用来提高开机效率。不过,效果会因内核版本和硬件情况而不同(极少数情况下还会变慢)。开启 readahead:<br />
<br />
# systemctl enable systemd-readahead-collect.service systemd-readahead-replay.service<br />
<br />
要知道,readahead 的超级牛力只有在重启几次后才会显现。<br />
<br />
== 疑难解答 ==<br />
<br />
=== 关机/重启十分缓慢 ===<br />
<br />
如果关机特别慢(甚至跟死机了一样),很可能是某个拒不退出的服务在作怪。systemd 会等待一段时间,然后再尝试杀死它。请阅读[http://freedesktop.org/wiki/Software/systemd/Debugging#Shutdown_Completes_Eventually 这篇文章],确认你是否是该问题受害者。<br />
<br />
=== 短时进程无日志记录 ===<br />
<br />
若 {{ic|journalctl -u foounit.service}} 没有显示某个短时进程的任何输出,那么改用 PID 试试。例如,若 {{ic|systemd-modules-load.service}} 执行失败,那么先用 {{ic|systemctl status systemd-modules-load}} 查询其 PID(比如是123),然后检索该 PID 相关的日志 {{ic|journalctl -b _PID&#61;123}}。运行时进程的日志元数据(诸如 _SYSTEMD_UNIT 和 _COMM)被乱序收集在 {{ic|/proc}} 目录。要修复该问题,必须修改内核,使其通过套接字连接来提供上述数据,该过程类似于 SCM_CREDENTIALS。<br />
<br />
=== Diagnosing boot problems ===<br />
<br />
Boot with these parameters on the kernel command line:<br />
{{ic|<nowiki>systemd.log_level=debug systemd.log_target=kmsg log_buf_len=1M</nowiki>}}<br />
<br />
[http://freedesktop.org/wiki/Software/systemd/Debugging More Debugging Information]<br />
<br />
=== Disabling application crash dumps ===<br />
<br />
Just link {{ic|/etc/sysctl.d/coredump.conf}} to {{ic|/dev/null}} then apply with sysctl ([https://bbs.archlinux.org/viewtopic.php?id=154511 source]):<br />
<br />
# ln -s /dev/null /etc/sysctl.d/coredump.conf<br />
# /usr/lib/systemd/systemd-sysctl<br />
# ulimit -c unlimited<br />
<br />
== 相关资源 ==<br />
<br />
*[http://www.freedesktop.org/wiki/Software/systemd 官方网站]<br />
*[http://0pointer.de/public/systemd-man/ man 手册页]<br />
*[http://freedesktop.org/wiki/Software/systemd/Optimizations systemd 优化]<br />
*[http://www.freedesktop.org/wiki/Software/systemd/FrequentlyAskedQuestions 常见问题 FAQ]<br />
*[http://www.freedesktop.org/wiki/Software/systemd/TipsAndTricks 小技巧]<br />
*[http://0pointer.de/public/systemd-ebook-psankar.pdf systemd for Administrators (PDF)]<br />
*[http://fedoraproject.org/wiki/Systemd Fedora 项目对 systemd 的介绍]<br />
*[http://fedoraproject.org/wiki/How_to_debug_Systemd_problems 如何调试 systemd 故障]<br />
*''The H Open'' 杂志上的[http://www.h-online.com/open/features/Control-Centre-The-systemd-Linux-init-system-1565543.html 两][http://www.h-online.com/open/features/Booting-up-Tools-and-tips-for-systemd-1570630.html 篇]科普文章<br />
*[http://0pointer.de/blog/projects/systemd.html Lennart 的博文]<br />
*[http://0pointer.de/blog/projects/systemd-update.html 更新报告]<br />
*[http://0pointer.de/blog/projects/systemd-update-2.html 更新报告2]<br />
*[http://0pointer.de/blog/projects/systemd-update-3.html 更新报告3]<br />
*[http://0pointer.de/blog/projects/why.html 最新动态]<br />
*[https://fedoraproject.org/wiki/SysVinit_to_Systemd_Cheatsheet/zh Fedora Wiki 上的 SysVinit 和 systemd 命令对比表]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Systemd_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=262810
Systemd (简体中文)
2013-06-15T04:21:22Z
<p>Cuihao: 好多无聊的格式更改…… 懒得同步了</p>
<hr />
<div>{{Lowercase title}}<br />
[[Category:Daemons and system services (简体中文)]]<br />
[[Category:Boot process (简体中文)]]<br />
[[ar:Systemd]]<br />
[[en:Systemd]]<br />
[[es:Systemd]]<br />
[[fr:Systemd]]<br />
[[it:Systemd]]<br />
[[ja:Systemd]]<br />
[[ru:Systemd]]<br />
[[zh-TW:Systemd]]<br />
{{Article summary start|概览}}<br />
{{Article summary text|本文介绍 Systemd 的安装和配置。}}<br />
{{Article summary heading|相关页面}}<br />
{{Article summary wiki|Systemd/Services}}<br />
{{Article summary wiki|systemd/User}}<br />
{{Article summary wiki|Systemd FAQ}}<br />
{{Article summary wiki|init Rosetta}}<br />
{{Article summary wiki|Daemons List}}<br />
{{Article summary wiki|udev (简体中文)}}<br />
{{Article summary end}}<br />
{{TranslationStatus (简体中文)|Systemd|2013-03-14|250286}}<br />
摘自[http://freedesktop.org/wiki/Software/systemd 项目主页]:<br />
<br />
'''''systemd''' 是 Linux 下的一款系统和服务管理器,兼容 SysV 和 LSB 的启动脚本。systemd 的特性有:支持并行化任务;同时采用 socket 式与 [[D-Bus (简体中文)|D-Bus]] 总线式激活服务;按需启动守护进程(daemon);利用 Linux 的 [[cgroups|cgroups]] 监视进程;支持快照和系统恢复;维护挂载点和自动挂载点;各服务间基于依赖关系进行精密控制。systemd 完全可以替代 Arch 曾经使用的 [[SysVinit (简体中文)|sysvinit]] 启动系统。''<br />
<br />
{{注意|[https://bbs.archlinux.org/viewtopic.php?pid&#61;1149530#p1149530 这个帖子]详细解释了 Arch 向 systemd 迁移的原因。}}<br />
<br />
另见[[Wikipedia:zh:systemd|维基百科上的介绍]]。<br />
<br />
== 迁移前需考虑 ==<br />
<br />
* 阅读[http://freedesktop.org/wiki/Software/systemd/ 该站],了解 systemd。<br />
* systemd 自己有一套日志('''journal''')系统,用于代替 '''syslog'''。两者也可以共存,参见后面的[[#日志|日志部分]]。<br />
* 虽然 systemd 可以替换 '''cron'''、'''acpid'''、'''xinetd''' 等的部分功能。至少目前还可以继续使用这些服务,无需立即切换。<br />
* 交互式 initscripts 启动脚本在 systemd 中无法工作。例如 '''netcfg-menu''' 无法{{Bug|31377}})在启动时显示。<br />
<br />
== 安装 ==<br />
{{注意|从[https://www.archlinux.org/news/systemd-is-now-the-default-on-new-installations/ 2012-10-13版安装介质]开始,安装程序已经默认安装{{pkg|systemd}} 和 {{pkg|systemd-sysvcompat}}。}}<br />
<br />
{{注意|如果是在 VPS 中使用 Arch,请先阅读:[[Virtual_Private_Server#Moving_your_VPS_from_initscripts_to_systemd]]。}}<br />
<br />
本部分帮助还在使用{{AUR|sysvinit}} 和 initscripts 的用户迁移到 '''systemd'''.<br />
<br />
# 从[[Official_Repositories_(简体中文)|官方软件仓库]]安装 {{pkg|systemd}} 并添加[[Kernel parameters|内核参数]]{{Ic|1=init=/usr/lib/systemd/systemd}}<br />
# 使用 {{ic|systemctl enable <服务名>}} 启用需要的服务(大致相当于以前 {{ic|DAEMONS}} 数组的作用,新的服务名称参见 [[Daemons_List_(简体中文)]])。<br />
# 重启系统,执行命令 {{ic|cat /proc/1/comm}},如果返回{{ic|systemd}},表示 systemd 已经正常启动。<br />
# 确认主机名已经正确设置:{{ic|hostnamectl set-hostname myhostname}}。<br />
# 删除 initscripts 和 sysvinit,并安装{{pkg|systemd-sysvcompat}}.<br />
# (可选)删除{{ic|1=init=/usr/lib/systemd/systemd}}内核参数,现在已经不需要它了。{{pkg|systemd-sysvcompat}} 软件包提供了一个软链接,使 systemd 成为默认 init。<br />
<br />
=== 附加信息 ===<br />
* 如果内核参数中有 {{ic|quiet}},建议在一开始先去掉,以便调试。<br />
{{Accuracy|reason=据有的用户称,systemd 并未对用户组有特殊要求。尽管有很多不常用的用户组,但从 consolekit 切换到 logind 后这些用户组没有发生改变。''(据译者理解:如果你碰到特殊的权限问题,可能仍需要加入这些用户组才能解决。)''}}<br />
* 使用 systemd 的时候'''无需'''将用户加入特殊[[Users and Groups (简体中文)|用户组]](如{{ic|sys}}、{{ic|disk}}、{{ic|lp}}、{{ic|network}}、{{ic|video}}、{{ic|audio}}、{{ic|optical}}、{{ic|storage}}、{{ic|scanner}}、{{ic|power}}等等)。加入这些组反而会有问题,例如audio组会导致程序阻塞软件混声。每个 PAM 登录都拥有一个 logind 会话,它通过[[Wikipedia:Access control list|POSIX ACLs]],赋予本地会话以声音设备访问权限、通过[[udisks]]挂载和卸载移动设备的权限等。<br />
{{警告|如果您准备将用户移出UNIX通用的基础用户组,请三思而后行。某些应用程序,如 '''mplayer''' 和 '''fbida''',需要访问 {{ic|/dev/fb*}} 的权限,因此必须加入 '''video''' 用户组。还有 k3b,需要用户加入 '''optical''' 组以访问旧式光学设备。正确的方法是使用 [https://mailman.archlinux.org/pipermail/arch-general/2012-October/031808.html udev] 获取[https://bbs.archlinux.org/viewtopic.php?pid&#61;1183731#p1183731 访问权限]。}}<br />
* 阅读 [[Network Configuration (简体中文)]],了解如何配置网络。<br />
<br />
== 原生 systemd 配置文件 ==<br />
{{注意|可能需要手动创建某些文件。所有文件的权限都是644,属主 root,属组 root。}}<br />
<br />
=== 虚拟控制台 ===<br />
{{ic|/etc/vconsole.conf}} 文件用来配置虚拟控制台,包括键盘布局和控制台字体:<br />
{{hc|/etc/vconsole.conf|<nowiki><br />
KEYMAP=us<br />
FONT=lat9w-16<br />
FONT_MAP=8859-1_to_uni</nowiki>}}<br />
<br />
{{注意|{{ic|{{Pkg|systemd}}-194}} 默认使用内核字体和键盘布局,因此以后不必在上述文件中留下空的 {{ic|1=KEYMAP=}} 和 {{ic|1=FONT=}} 设置。}}<br />
<br />
还可以通过命令设置键盘布局:<br />
# localectl set-keymap de<br />
<br />
{{ic|localectl}} 也可以设置X11的键盘布局:<br />
<br />
# localectl set-x11-keymap de<br />
<br />
详情参见 {{ic|man 1 localectl}} 和 {{ic|man 5 vconsole.conf}}。<br />
<br />
* 另参阅 [[Fonts (简体中文)#控制台字体|控制台字体]] 和 [[KEYMAP (简体中文)#键盘布局|键盘布局]]。<br />
<br />
=== 硬件时钟 ===<br />
<br />
'''systemd''' 默认硬件时钟为协调世界时(UTC)。<br />
<br />
{{小贴士|推荐使用[[NTP (简体中文)|NTP服务]]来在线同步硬件时钟。}}<br />
<br />
==== 硬件时钟设定为地方时 ====<br />
<br />
将硬件时钟配置为地方时('''不建议'''):<br />
<br />
# timedatectl set-local-rtc true<br />
<br />
重新调整为 UTC:<br />
<br />
# timedatectl set-local-rtc false<br />
<br />
如果设置成本地时间,处理夏令时有些麻烦。如果夏令时调整发生在关机时,下次启动时时间会出现问题([http://www.cl.cam.ac.uk/~mgk25/mswish/ut-rtc.html 更多信息])。最新的内核直接从实时时钟芯片(RTC)读取时间,不使用 {{ic|hwclock}},内核把从 RTC 读取的时间当作 UTC 处理。所以如果硬件时间是地方时,系统启动一开始识别的时间是错误的,之后很快会进行矫正。这可能导致一些问题(尤其是时间倒退时)。<br />
<br />
如果同时安装了 Windows 操作系统([http://blogs.msdn.com/b/oldnewthing/archive/2004/09/02/224672.aspx 默认使用地方时]),那么一般 RTC 会被设置为地方时。Windows 其实也能处理 UTC,需要[[Time (简体中文)#Windows 系统使用 UTC|修改注册表]]。建议让 Windows 使用 UTC,而非让 Linux 使用地方时。Windows 使用 UTC 后,请记得禁用 Windows 的时间同步功能,以防 Windows 错误设置硬件时间。如上文所说,Linux 可以使用[[NTP (简体中文)|NTP服务]]来在线同步硬件时钟。<br />
<br />
* 详情参阅 [[Time (简体中文)]]。<br />
<br />
=== 内核模块 ===<br />
目前,所有必要模块的加载均由 [[udev]] 自动完成。所以,如果不需要使用任何额外的模块,就没有必要在任何配置文件中添加启动时加载的模块。但是,有些情况下可能需要在系统启动时加载某个额外的模块,或者将某个模块列入黑名单以便使系统正常运行。<br />
<br />
==== 开机加载 ====<br />
systemd 读取 {{ic|/etc/modules-load.d/}} 中的配置加载额外的内核模块。配置文件名称通常为 {{ic|/etc/modules-load.d/<program>.conf}}。格式很简单,一行一个要读取的模块名,而空行以及第一个非空格字符为{{ic|#}}或{{ic|;}}的行会被忽略,如:<br />
{{hc|/etc/modules-load.d/virtio-net.conf|<nowiki><br />
# Load virtio-net.ko at boot<br />
virtio-net</nowiki>}}<br />
<br />
另见{{ic|man 5 modules-load.d}}。<br />
<br />
==== 配置内核模块参数 ====<br />
<br />
在 {{ic|/etc/modprobe.d/modprobe.conf}} 中设置额外的模块参数。<br />
<br />
例如:<br />
<br />
* 我们已经在 {{ic|/etc/modules-load.d/loop.conf}} 中添加文本 {{ic|loop}},告诉系统开机自动加载该模块。<br />
<br />
* 在 {{ic|/etc/modprobe.d/modprobe.conf}} 设置额外的参数,如:{{ic|options loop max_loop&#61;64}}。<br />
<br />
最后,可以通过 {{ic|cat /sys/module/loop/parameters/max_loop}} 命令检查配置是否生效。<br />
<br />
==== 禁用内核模块 ====<br />
禁用内核模块的方法和 Arch 默认的 {{Pkg|initscripts}} 相同,因为该过程实际由 {{Pkg|kmod}} 处理。参见:[[Kernel modules (简体中文)#黑名单]]。<br />
<br />
=== 文件系统挂载 ===<br />
<br />
默认行为是:在启动一个需要挂载特定分区的服务之前,系统自动检查并挂载分区。{{ic|/etc/fstab}} 中设定的网络文件系统(如 [[NFS (简体中文)|NFS]]、[[Samba (简体中文)|Samba]])无需配置即可正常工作,systemd 将确保网络文件系统在网络链接就绪后挂载。<br />
<br />
详情参阅:{{ic|man 5 systemd.mount}}。<br />
<br />
==== 自动挂载 ====<br />
<br />
* 如果 {{ic|/home}} 分区较大,可以让不依赖 {{ic|/home}} 分区的服务先启动。把下面的参数添加到 {{ic|/etc/fstab}} 文件中 {{ic|/home}} 项目的参数部分即可:<br />
<br />
noauto,x-systemd.automount<br />
<br />
这样 {{ic|/home}} 分区只有需要访问时才会被挂载。内核会缓存所有的文件操作,直到 {{ic|/home}} 分区准备完成。<br />
<br />
{{注意|这样做会使 {{ic|/home}} 的文件系统类型被识别为 {{ic|autofs}},造成 [[mlocate]] 查询时忽略该目录。实际加速效果因配置而异,所以请自己权衡是否需要。}}<br />
<br />
* 挂载远程文件系统也是同理。如果你仅想在需要的时候才挂载,也可以添加 {{ic|noauto,x-systemd.automount}} 参数。另外,可以设置 {{ic|1=x-systemd.device-timeout=#}} 参数,设置超时时间,以防止网络资源不能访问的时候浪费时间。<br />
<br />
* 如果你的加密文件系统需要密钥,则需要添加 {{ic|noauto}} 参数到 {{ic|/etc/crypttab}} 文件中的对应位置。systemd 开机的时候就不会打开这个加密设备,会一直等待到设备被访问时再使用密钥文件挂载。比如在使用加密RAID设备的时候可以节省一定的时间,因为 systemd 不必等到设备可用后才能访问。例如:<br />
<br />
{{hc|/etc/crypttab|<br />
data /dev/md0 /root/key noauto}}<br />
<br />
==== LVM ====<br />
<br />
如果装有不通过 [[Mkinitcpio (简体中文)|initramfs]] 激活的[[LVM (简体中文)|LVM]]卷,则需启动 {{ic|lvm-monitoring}} 服务(由 {{pkg|lvm2}} 软件包提供):<br />
<br />
# systemctl enable lvm-monitoring<br />
<br />
=== ACPI 电源管理 ===<br />
'''systemd''' 能够处理某些电源相关的 [[Wikipedia:Advanced_Configuration_and_Power_Interface|ACPI]] 事件,通过 {{ic|/etc/systemd/logind.conf}} 的下列选项配置:<br />
* {{ic|HandlePowerKey}}:按下电源键后的动作<br />
* {{ic|HandleSleepKey}}:按下挂起键后的动作<br />
* {{ic|HandleHibernateKey}}: 按下休眠键后的动作<br />
* {{ic|HandleLidSwitch}}:合上笔记本盖后待机<br />
<br />
动作可以是 {{ic|ignore}}、{{ic|poweroff}}、{{ic|reboot}}、{{ic|halt}}、{{ic|suspend}}、{{ic|hibernate}}、{{ic|hybrid-sleep}}、{{ic|lock}} 或 {{ic|kexec}}。<br />
<br />
系统默认设置为:<br />
HandlePowerKey=poweroff<br />
HandleSuspendKey=suspend<br />
HandleHibernateKey=hibernate<br />
HandleLidSwitch=suspend<br />
<br />
不用图形界面、或者使用 [[i3 (简体中文)|i3]]、[[awesome (简体中文)|awesome]] 这样简单的桌面管理器时,systemd 可以替代 [[acpid (简体中文)|acpid]] 处理 ACPI 事件。<br />
<br />
{{注意|运行 {{ic|systemctl restart systemd-logind}},使上述更改立即生效。}}<br />
<br />
{{注意|systemd 无法处理交流电源和电池 ACPI 事件,所以还得使用 [[Laptop Mode Tools (简体中文)|Laptop Mode Tools]] 或 [[acpid (简体中文)|acpid]] 工具。}}<br />
<br />
在当前版本的 systemd 中,这些 {{ic|Handle}} 选项将会被应用到整个系统当中,除非它们被别的程序——例如某个桌面环境中的电源管理器——给“阻止”(inhibited)。如果其它的程序没有阻止这些 {{ic|Handle}} ,你可能会先被 systemd 挂起你的系统,然后当系统被唤醒之后,电源管理器又会再次将系统挂起。<br />
<br />
{{警告|目前只有[[GNOME]] 和 [[KDE]] 支持 "inhibited" 命令。在其它的桌面管理器同样实现该功能之前,如果你想使用[[Xfce]]、[[acpid]] 或者其它程序来管理 ACPI 事件,你需要把 {{ic|Handle}} 选项设置为 {{ic|ignore}}。}}<br />
<br />
{{注意|除了内核默认的待机支持后端(用于处理待机/休眠),systemd 也可以使用其他后端(比如 [[Uswsusp]] 或 [[TuxOnIce]])。}}<br />
<br />
要令 {{ic|systemctl hibernate}} 工作,需要按照[[Pm-utils#Hibernation_.28suspend2disk.29|休眠]]和 [[Pm-utils#Mkinitcpio_Resume_Hook|mkinitcpio 唤醒扩展]]的设置步骤进行操作。(不必安装 {{ic|pm-utils}})<br />
<br />
==== 休眠时执行的脚本 ====<br />
<br />
使用 {{ic|systemctl suspend}}、{{ic|systemctl hibernate}} 或 {{ic|systemctl hybrid-sleep}} 命令执行待机/休眠时,systemd 不会调用 [[pm-utils (简体中文)|pm-utils]]。[[pm-utils (简体中文)|pm-utils]] 的钩子扩展(hook)——包括 [[pm-utils (简体中文)#建立你自己的钩子|自定义钩子]]——会失效。不过,systemd 提供了两种类似的待机/休眠时执行脚本的机制。<br />
<br />
===== 使用服务文件 =====<br />
<br />
可以将服务文件附在 suspend.target、hibernate.target 或 sleep.target 中,这样就能在待机/休眠前后执行某些操作。用户级操作和root/系统级操作应该使用不同的服务文件。要启用用户级服务文件,使用 {{ic|# systemctl enable suspend@<用户名> && systemctl enable resume@<用户名>}}。例如:<br />
<br />
{{hc|/etc/systemd/system/suspend@.service|2=<nowiki><br />
[Unit]<br />
Description=User suspend actions<br />
Before=sleep.target<br />
<br />
[Service]<br />
User=%I<br />
Type=forking<br />
Environment=DISPLAY=:0<br />
ExecStartPre= -/usr/bin/pkill -u %u unison ; /usr/local/bin/music.sh stop ; /usr/bin/mysql -e 'slave stop'<br />
ExecStart=/usr/bin/sflock<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
{{hc|/etc/systemd/system/resume@.service|2=<nowiki><br />
[Unit]<br />
Description=User resume actions<br />
After=suspend.target<br />
<br />
[Service]<br />
User=%I<br />
Type=simple<br />
ExecStartPre=/usr/local/bin/ssh-connect.sh<br />
ExecStart=/usr/bin/mysql -e 'slave start'<br />
<br />
[Install]<br />
WantedBy=suspend.target</nowiki>}}<br />
<br />
至于root/系统级服务(使用 {{ic|# systemctl enable root-suspend}} 激活):<br />
<br />
{{hc|/etc/systemd/system/root-resume.service|2=<nowiki><br />
[Unit]<br />
Description=Local system resume actions<br />
After=suspend.target<br />
<br />
[Service]<br />
Type=simple<br />
ExecStart=/usr/bin/systemctl restart mnt-media.automount<br />
<br />
[Install]<br />
WantedBy=suspend.target</nowiki>}}<br />
<br />
{{hc|/etc/systemd/system/root-suspend.service|2=<nowiki><br />
[Unit]<br />
Description=Local system suspend actions<br />
Before=sleep.target<br />
<br />
[Service]<br />
Type=simple<br />
ExecStart=-/usr/bin/pkill sshfs<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
上述服务文件的一些解释(详见 {{ic|man systemd.service}}):<br />
* 如果设置 {{ic|1=<nowiki>Type=OneShot</nowiki>}},那么可以使用多个 {{ic|1=<nowiki>ExecStart=</nowiki>}} 参数。否则只能写一个,替代方案是在 {{ic|ExecStartPre}} 中添加命令,或使用分号分隔不同命令(见第一个例子,分号前后的空格都是'''必须'''的)。 <br />
* 若命令前加上一个“-”(半角减号),则命令返回非零值时会被忽略、当作正常执行处理。<br />
* 有关调试,最好的方法是用 {{ic|journalctl}} 查看日志。<br />
<br />
===== 合并待机和唤醒服务文件 =====<br />
<br />
利用下面这种自定义的一体化待机和唤醒服务,使用单一的钩子扩展即可对不同操作(待机/休眠/混合休眠)的不同阶段(进入/唤醒)进行控制。<br />
<br />
例子和解释:<br />
<br />
{{hc|/etc/systemd/system/wicd-sleep.service|2=<nowiki><br />
[Unit]<br />
Description=Wicd sleep hook<br />
Before=sleep.target<br />
StopWhenUnneeded=yes<br />
<br />
[Service]<br />
Type=oneshot<br />
RemainAfterExit=yes<br />
ExecStart=-/usr/share/wicd/daemon/suspend.py<br />
ExecStop=-/usr/share/wicd/daemon/autoconnect.py<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
* {{ic|1=<nowiki>RemainAfterExit=yes</nowiki>}}:服务启动后,除非显式地停止,否则就认为是活动的。<br />
* {{ic|1=<nowiki>StopWhenUnneeded=yes</nowiki>}}:服务活动时,如果无其他服务依赖该服务,就停止它。在本例中,该服务会在 sleep.target 停止后停止活动。<br />
* 由于 sleep.target 会被 suspend.target、hibernate.target、hybrid-sleep.target 调用,且 sleep.target 本身设置了 StopWhenUnneeded,该服务文件对以上各种操作都是有效的。<br />
<br />
===== 使用 /usr/lib/systemd/system-sleep =====<br />
<br />
systemd 在待机/休眠时执行 {{ic|/usr/lib/systemd/system-sleep/}} 里的所有脚本,传递下面两个参数:<br />
<br />
* 参数1:若是准备进入待机/休眠状态,则为 {{ic|pre}};唤醒时为 {{ic|post}}。<br />
* 参数2:事件名称,{{ic|suspend}},{{ic|hibernate}} 或 {{ic|hybrid-sleep}}。<br />
<br />
systemd 会同时执行所有脚本,而不是像 [[pm-utils (简体中文)|pm-utils]] 那样顺序执行。<br />
<br />
脚本输出会记录在相关服务({{ic|systemd-suspend.service}}、{{ic|systemd-hibernate.service}} 或 {{ic|systemd-hybrid-sleep.service}})中。通过[[#日志|日志]]查看:<br />
# journalctl -b -u systemd-suspend<br />
<br />
{{注意|除了使用自定义脚本,还可以利用 {{ic|sleep.target}}、{{ic|suspend.target}}、{{ic|hibernate.target}} 或 {{ic|hybrid-sleep.target}} 来为单元(unit)添加睡眠状态策略。}}<br />
<br />
脚本示范:<br />
{{hc|/usr/lib/systemd/system-sleep/example.sh|<br />
#!/bin/sh<br />
case $1/$2 in<br />
pre/*)<br />
echo "进入 $2 状态..."<br />
;;<br />
post/*)<br />
echo "从 $2 状态唤醒..."<br />
;;<br />
esac}}<br />
<br />
记得添加可执行权限: <br />
# chmod a+x /usr/lib/systemd/system-sleep/example.sh<br />
<br />
详情参见 {{ic|man 7 systemd.special}} 和 {{ic|man 8 systemd-sleep}}。<br />
<br />
=== 临时文件 ===<br />
{{ic|/usr/lib/tmpfiles.d/}} 和 {{ic|/etc/tmpfiles.d/}} 中的文件描述了 systemd-tmpfiles 如何创建、清理、删除临时文件和目录,这些文件和目录通常存放在 {{ic|/run}} 和 {{ic|/tmp}} 中。配置文件名称为 {{ic|/etc/tmpfiles.d/<program>.conf}}。此处的配置能覆盖 {{ic|/usr/lib/tmpfiles.d/}} 目录中的同名配置。<br />
<br />
临时文件通常和服务文件同时提供,以生成守护进程需要的文件和目录。例如 [[Samba (简体中文)|Samba]] 服务需要目录 {{ic|/run/samba}} 存在并设置正确的权限位,就象这样:<br />
{{hc|/usr/lib/tmpfiles.d/samba.conf|<br />
D /run/samba 0755 root root<br />
}}<br />
<br />
此外,临时文件还可以用来在开机时向特定文件写入某些内容。比如,要禁止系统从USB设备唤醒,利用旧的 {{ic|/etc/rc.local}} 可以用 {{ic|echo USBE > /proc/acpi/wakeup}},而现在可以这么做:<br />
{{hc|/etc/tmpfiles.d/disable-usb-wake.conf|<br />
w /proc/acpi/wakeup - - - - USBE<br />
}}<br />
<br />
详情参见 {{ic|man 5 tmpfiles.d}}。<br />
<br />
{{注意|该方法不能向 {{ic|/sys}} 中的配置文件添加参数,因为 {{ic|systemd-tmpfiles-setup}} 有可能在相关模块加载前运行。这种情况下,需要首先通过 {{ic|modinfo <模块名>}} 确认需要的参数,并在 [[Kernel modules (简体中文)#选项|{{ic|/etc/modprobe.d}} 下的一个文件]]中设置改参数。另外,还可以使用 [[udev (简体中文)#udev规则|udev 规则]],在设备就绪时设置相应属性。}}<br />
<br />
=== 单元(unit) ===<br />
一个单元配置文件可以描述如下内容之一:系统服务、socket、系统设备、挂载点、交换分区/文件、启动目标(target)、文件系统路径、由 systemd 管理的计时器。文件格式受 .desktop 文件启发,而最初起源是 Windows 下的 .ini 文件。<br />
<br />
详情参阅 {{ic|man 5 systemd.unit}}.<br />
<br />
== 从 initscripts 迁移到 systemd ==<br />
<br />
=== initscripts 模拟 ===<br />
<br />
{{Pkg|initscripts}} 软件包提供了 systemd 和 Arch 传统配置的整合。当同时安装了{{Pkg|initscripts}}和systemd时,若以systemd启动系统,systemd将执行以下操作:<br />
<br />
# 解析{{ic|/etc/rc.conf}}中的{{ic|DAEMONS}}数组,并在系统启动时,启动所有列出的守护进程<br />
# 在启动过程中执行{{ic|/etc/rc.local}}<br />
# 在系统关闭过程中执行{{ic|/etc/rc.local.shutdown}}<br />
<br />
Initscripts模拟仅仅是作为用户迁移到systemd时的过渡措施,这一方案最终将'''不再可行'''。原生的systemd并不依赖于{{ic|rc.conf}}文件的集中式配置,因此推荐使用[[#原生 systemd 配置文件|原生的systemd配置文件]]。原生systemd配置文件比{{ic|/etc/rc.conf}}具有更高优先级。<br />
<br />
{{注意|替换{{ic|/etc/rc.local}}的推荐方式是针对系统启动时要运行的程序编写自定义的service文件,详见相关[[#自己编写_.service_文件|章节]].}}<br />
<br />
{{注意|如果你曾经在{{ic|/etc/inittab}}中禁用了以{{keypress|Ctrl+Alt+Del}}重启系统,则需要以root执行{{ic|systemctl mask ctrl-alt-del.target}}以使其对systemd重新生效。}}<br />
<br />
{{警告|如果同时安装 systemd(197-4 及后续版本)和 initscripts,那么 {{ic|/etc/rc.local}} 脚本会导致启动过程无法结束(原因是 getty@tty1.service 自 197-4 版本起不再等待 rc-local.service 执行,getty 无法启动)。请删除或重命名 {{ic|/etc/rc.local}}。}}<br />
<br />
=== 迁移 DAEMONS 列表 ===<br />
<br />
若要用纯 systemd 环境,则应当删除 {{ic|/etc/rc.conf}} 文件,并完全通过 {{ic|systemctl}} 命令来启动服务。对于 {{ic|DAEMONS}} 列表里面的每个 {{ic|<服务名>}},现在可以用以下命令设置成自动启动:<br />
<br />
# systemctl enable <服务名>.service<br />
<br />
{{小贴士|[[Daemons List|这里]]有一份 initscripts 和 systemd 下常见服务的对应列表。}}<br />
<br />
如果提示 {{ic|<service_name>.service}} 不存在:<br />
<br />
* 很有可能是 systemd 用了不同的服务名。比如 {{ic|crond}} 服务替换成 {{ic|cronie.service}};{{ic|alsa}} 服务替换成了 {{ic|alsa-store.service}} 和 {{ic|alsa-restore.service}} 两个服务。另一个值得注意的是 {{ic|network}} 服务,它被替换为一整套服务文件(详情参见 [[Configuring_Network_(简体中文)]])。<br />
* 另外,有些程序尚未提供 .service 文件,还得继续用 {{ic|rc.conf}} 来配置开机自动启动。<br />
<br />
{{小贴士|用下面这个命令来查看已安装软件包包含的服务文件:<br />
$ pacman -Ql cronie<br />
[...]<br />
cronie /etc/rc.d/crond # DAEMONS 列表中使用的 initscript 服务 (原生 systemd 配置中不再使用)<br />
[...]<br />
cronie /usr/lib/systemd/system/cronie.service # 原生 systemd 服务文件<br />
[...]<br />
}}<br />
<br />
* 有些服务不需要用户明确配置自启动。比如安装 {{ic|dbus-core}} 软件包后 {{ic|dbus.service}} 就会自动被配置为自启动。而 {{ic|alsa-store.service}} 和 {{ic|alsa-restore.service}} 也会由 systemd 自动启动。可以用 {{ic|systemctl list-unit-files}} 命令来查看服务列表并检查运行状态。<br />
<br />
== systemd 基本工具 ==<br />
<br />
检视和控制systemd的主要命令是{{ic|systemctl}}。该命令可用于查看系统状态和管理系统及服务。详见{{ic|man 1 systemctl}}。<br />
<br />
{{小贴士|在 {{ic|systemctl}} 参数中添加 {{ic|-H <用户名>@<主机名>}} 可以实现对其他机器的远程控制。该过程使用 [[SSH (简体中文)|SSH]] 链接。}}<br />
<br />
{{注意|{{ic|systemadm}} 是 systemd 的官方图形前端。由 [[AUR (简体中文)|AUR]] 中的软件包 {{AUR|systemd-ui-git}} 提供。}}<br />
<br />
=== 分析系统状态 ===<br />
<br />
输出激活的单元:<br />
<br />
$ systemctl<br />
<br />
以下命令等效:<br />
<br />
$ systemctl list-units<br />
<br />
输出运行失败的单元:<br />
<br />
$ systemctl --failed<br />
<br />
所有可用的单元文件存放在 {{ic|/usr/lib/systemd/system/}} 和 {{ic|/etc/systemd/system/}} 目录(后者优先级更高)。查看所有已安装服务:<br />
$ systemctl list-unit-files<br />
<br />
=== 使用单元 ===<br />
一个单元可以是系统服务({{ic|.service}})、挂载点({{ic|.mount}})、sockets({{ic|.sockets}})。<br />
<br />
使用 {{ic|systemctl}} 控制单元时,通常需要使用单元文件的全名,包括扩展名(例如 {{ic|sshd.service}})。但是有些单元可以在{{ic|systemctl}}中使用简写方式。<br />
* 如果无扩展名,systemctl 默认把扩展名当作 {{ic|.service}}。例如 {{ic|netcfg}} 和 {{ic|netcfg.service}} 是等价的。<br />
* 挂载点会自动转化为相应的 {{ic|.mount}} 单元。例如 {{ic|/home}} 等价于 {{ic|home.mount}}。<br />
* 设备会自动转化为相应的 {{ic|.device}} 单元,所以 {{ic|/dev/sda2}} 等价于 {{ic|dev-sda2.device}}。<br />
<br />
立即激活单元:<br />
<br />
# systemctl start <单元><br />
<br />
立即停止单元:<br />
# systemctl stop <单元><br />
<br />
重启单元:<br />
<br />
# systemctl restart <单元><br />
<br />
命令单元重新读取配置:<br />
<br />
# systemctl reload <单元><br />
<br />
输出单元运行状态:<br />
<br />
$ systemctl status <单元><br />
<br />
检查单元是否配置为自动启动:<br />
$ systemctl is-enabled <单元><br />
<br />
开机自动激活单元:<br />
<br />
# systemctl enable <单元><br />
<br />
{{注意|如果服务没有{{ic|Install}}段落,一般意味着应该通过其它服务自动调用它们。如果真的需要手动安装,可以直接连接服务,如下(将{{ic|foo}}替换为真实的服务名):<br />
{{bc|# ln -s /usr/lib/systemd/system/foo.service /etc/systemd/system/graphical.target.wants/}}<br />
}}<br />
<br />
取消开机自动激活单元:<br />
<br />
# systemctl disable <单元><br />
<br />
显示单元的手册页(必须由单元文件提供):<br />
<br />
# systemctl help <单元><br />
<br />
重新载入 systemd,扫描新的或有变动的单元:<br />
# systemctl daemon-reload<br />
<br />
=== 电源管理 ===<br />
<br />
安装 {{ic|polkit}} 后才可使用电源管理。<br />
<br />
如果你正登录在一个本地的{{ic|systemd-logind}}用户会话,且当前没有其它活动的会话,那么以下命令无需root权限即可执行。否则(例如,当前有另一个用户登录在某个tty),systemd 将会自动请求输入root密码。<br />
<br />
重启:<br />
<br />
$ systemctl reboot<br />
<br />
退出系统并停止电源:<br />
<br />
$ systemctl poweroff<br />
<br />
待机:<br />
<br />
$ systemctl suspend<br />
<br />
休眠:<br />
<br />
$ systemctl hibernate<br />
<br />
混合休眠模式(同时休眠到硬盘并待机):<br />
<br />
$ systemctl hybrid-sleep<br />
<br />
== 启动登录管理器 ==<br />
<br />
通过启动[[Display Manager (简体中文)|登录管理器]](或称显示管理器),即可进行图形界面登录。<br />
要启用图形界面登录,运行适当的[[Display Manager (简体中文)|登录管理器]](或称显示管理器)即可。目前,Arch 提供了 [[GDM (简体中文)|GDM]]、[[KDM (简体中文)|KDM]]、[[SLiM (简体中文)|SLiM]]、[[XDM (简体中文)|XDM]]、[[LXDM (简体中文)|LXDM]]、[[LightDM (简体中文)|LightDM]] 和 {{AUR|SDDM}} 的 systemd 服务文件。以 KDM 为例,配置开机启动:<br />
# systemctl enable kdm.service<br />
<br />
执行上述命令后,登录管理器应当能正常工作了。如果不是的话,很可能是因为你修改了{{ic|default.target}}。默认情况应当如下:<br />
<br />
{{hc|# ls -l /etc/systemd/system/default.target|/etc/systemd/system/default.target -> /usr/lib/systemd/system/graphical.target}}<br />
<br />
删除被修改的 {{ic|default.target}} 即可,systemd 会自动使用默认配置(即 {{ic|graphical.target}}):<br />
<br />
# rm /etc/systemd/system/default.target<br />
<br />
=== 使用 systemd-logind ===<br />
<br />
{{注意|自 2012-10-30 起, [[ConsoleKit]] 已被 [https://www.archlinux.org/news/consolekit-replaced-by-logind/ systemd-logind 取代] ,作为登录到桌面环境的默认方式。}}<br />
<br />
可使用 {{ic|loginctl}} 来查看用户会话的状态。所有 [[PolicyKit]] 操作,如挂起系统、挂载外部驱动器,都无需配置即可使用。<br />
<br />
$ loginctl show-session $XDG_SESSION_ID<br />
<br />
== 自己编写 .service 文件 ==<br />
''参见:[[Systemd/Services]]''<br />
<br />
=== 处理依赖关系 ===<br />
<br />
使用systemd时,可通过正确编写单元配置文件来解决其依赖关系。典型的情况是,单元{{ic|A}}要求单元{{ic|B}}在{{ic|A}}启动之前运行。在此情况下,向单元{{ic|A}}配置文件中的 {{ic|[Unit]}} 段添加 {{ic|1=Requires=B}} 和 {{ic|1=After=B}} 即可。若此依赖关系是可选的,可添加 {{ic|1=Wants=B}} 和 {{ic|1=After=B}}。请注意 {{ic|1=Wants=}} 和 {{ic|1=Requires=}} 并不意味着 {{ic|1=After=}},即如果 {{ic|1=After=}} 选项没有制定,这两个单元将被并行启动。<br />
<br />
依赖关系通常被用在服务(service)而不是[[#目标(target)|目标(target)]]上。例如, {{ic|network.target}} 一般会被某个配置网络接口的服务引入,所以,将自定义的单元排在该服务之后即可,因为 {{ic|network.target}} 已经启动。<br />
<br />
=== 启动方式 ===<br />
<br />
编写自定义的service文件时,可以选择几种不同的服务启动方式。启动方式可通过配置文件 {{ic|[Service]}} 段中的 {{ic|1=Type=}} 参数进行设置。具体的参数说明请参阅 {{ic|man systemd.service}} 。<br />
<br />
* {{ic|1=Type=simple}}(默认值):systemd认为该服务将立即启动。服务进程不会fork。如果该服务要启动其他服务,不要使用此类型启动,除非该服务是socket激活型。<br />
* {{ic|1=Type=forking}}:systemd认为当该服务进程fork,且父进程退出后服务启动成功。对于常规的守护进程(daemon),除非你确定此启动方式无法满足需求,使用此类型启动即可。使用此启动类型应同时指定 {{ic|1=PIDFile=}},以便systemd能够跟踪服务的主进程。<br />
* {{ic|1=Type=oneshot}}:这一选项适用于只执行一项任务随后退出的服务。你可能需要同时设置 {{ic|1=RemainAfterExit=yes}} 使得 systemd 在服务进程退出之后仍然认为服务处于激活状态。<br />
* {{ic|1=Type=notify}}:与 {{ic|1=Type=simple}} 相同,但约定服务会在就绪后向 systemd 发送一个信号。这一通知的实现由 {{ic|libsystemd-daemon.so}} 提供。<br />
* {{ic|1=Type=dbus}}:若以此方式启动,当指定的 {{ic|BusName}} 出现在DBus系统总线上时,systemd认为服务就绪。<br />
<br />
=== 修改现存单元文件 ===<br />
<br />
To edit a unit file provided by a package, you can create a directory called {{ic|/etc/systemd/system/<unit>.d/}} for example {{ic|/etc/systemd/system/httpd.service.d/}} and place {{ic|*.conf}} files in there to override or add new options. '''systemd''' will parse these {{ic|*.conf}} files and apply them on top of the original unit. For example, if you simply want to add an additional dependency to a unit, you may create the following file:<br />
<br />
{{hc|/etc/systemd/system/<unit>.d/customdependency.conf|2=<br />
[Unit]<br />
Requires=<新依赖><br />
After=<新依赖>}}<br />
<br />
然后运行以下命令使更改生效:<br />
<br />
# systemctl daemon-reload<br />
# systemctl restart <单元><br />
<br />
Alternatively you can copy the old unit file from {{ic|/usr/lib/systemd/system/}} to {{ic|/etc/systemd/system/}} and make your changes there. A unit file in {{ic|/etc/systemd/system/}} always overrides the same unit in {{ic|/usr/lib/systemd/system/}}. Note that when the original unit in {{ic|/usr/lib/}} is changed due to a package upgrade, these changes will not automatically apply to your custom unit file in {{ic|/etc/}}. Additionally you will have to manually reenable the unit with {{ic|systemctl reenable <unit>}}. It is therefore recommended to use the {{ic|*.conf}} method described before instead.<br />
<br />
{{小贴士|可以用 {{ic|systemd-delta}} 命令来查看哪些单元文件被覆盖、哪些被修改。}}<br />
<br />
=== 单元配置文件的 vim 语法高亮支持 ===<br />
<br />
可从 [[Official Repositories|官方仓库]] 安装 {{Pkg|vim-systemd}} 软件包,使 unit 配置文件在 [[Vim]] 下支持语法高亮。<br />
<br />
== 目标(target) ==<br />
启动级别(runlevel)是一个旧的概念。现在,systemd 引入了一个和启动级别功能相似又不同的概念——目标(target)。不像数字表示的启动级别,每个''目标''都有名字和独特的功能,并且能同时启用多个。一些''目标''继承其他''目标''的服务,并启动新服务。systemd 提供了一些模仿 sysvinit 启动级别的''目标'',仍可以使用旧的 {{ic|telinit 启动级别}} 命令切换。<br />
<br />
=== 获取当前目标 ===<br />
不要使用 {{ic|runlevel}} 命令了:<br />
$ systemctl list-units --type=target<br />
<br />
=== 创建新目标 ===<br />
在 Fedora 中,启动级别 0、1、3、5、6 都被赋予特定用途,并且都对应一个 systemd 的''目标''。然而,没有什么很好的移植用户定义的启动级别(2、4)的方法。要实现类似功能,可以以原有的启动级别为基础,创建一个新的''目标'' {{ic|/etc/systemd/system/<新目标>}}(可以参考 {{ic|/usr/lib/systemd/system/graphical.target}}),创建 {{ic|/etc/systemd/system/<新目标>.wants}} 目录,向其中加入额外服务的链接(指向 {{ic|/usr/lib/systemd/system/}} 中的单元文件)。<br />
<br />
=== 目标表 ===<br />
{| border="1"<br />
!SysV 启动级别!!Systemd 目标!!注释<br />
|-<br />
| 0 || runlevel0.target, poweroff.target || 中断系统(halt)<br />
|-<br />
| 1, s, single || runlevel1.target, rescue.target || 单用户模式<br />
|-<br />
| 2, 4 || runlevel2.target, runlevel4.target, multi-user.target || 用户自定义启动级别,通常识别为级别3。<br />
|-<br />
| 3 || runlevel3.target, multi-user.target || 多用户,无图形界面。用户可以通过终端或网络登录。<br />
|-<br />
| 5 || runlevel5.target, graphical.target || 多用户,图形界面。继承级别3的服务,并启动图形界面服务。<br />
|-<br />
| 6 || runlevel6.target, reboot.target || 重启<br />
|-<br />
| emergency || emergency.target || 急救模式(Emergency shell)<br />
|-<br />
|}<br />
<br />
=== 切换启动级别/目标 ===<br />
systemd 中,启动级别通过“目标单元”访问。通过如下命令切换:<br />
# systemctl isolate graphical.target<br />
该命令对下次启动无影响。等价于{{ic|telinit 3}} 或 {{ic|telinit 5}}。<br />
<br />
=== 修改默认启动级别/目标 ===<br />
开机启动进的目标是 {{ic|default.target}},默认链接到 {{ic|graphical.target}} (大致相当于原来的启动级别5)。可以通过[[Kernel parameters (简体中文)|内核参数]]更改默认启动级别:<br />
<br />
{{小贴士|可以省略扩展名 {{ic|.target}}。}}<br />
<br />
* {{ic|1=systemd.unit=multi-user.target}} (大致相当于级别3)<br />
* {{ic|1=systemd.unit=rescue.target}} (大致相当于级别1)<br />
<br />
另一个方法是修改 {{ic|default.target}}。可以通过 {{ic|systemctl}} 修改它:<br />
<br />
# systemctl enable multi-user.target<br />
<br />
命令执行情况由 {{ic|systemctl}} 显示:链接 {{ic|/etc/systemd/system/default.target}} 被创建,指向新的默认启动级别。该方法当且仅当目标配置文件中有以下内容时有效:<br />
<br />
[Install]<br />
Alias=default.target<br />
<br />
目前,{{ic|multi-user.target}}、{{ic|graphical.target}} 都包含这段内容。<br />
<br />
== 日志 ==<br />
systemd 提供了自己日志系统(logging system),称为 journal. 使用 systemd 日志,无需额外安装日志服务(syslog)。读取日志的命令:<br />
# journalctl<br />
<br />
默认情况下(当 {{ic|1=Storage=}} 在文件 {{ic|/etc/systemd/journald.conf}} 中被设置为 {{ic|auto}}),日志记录将被写入 {{ic|/var/log/journal/}}。该目录是 {{pkg|systemd}} 软件包的一部分。若被删除,systemd '''不会'''自动创建它,直到下次升级软件包时重建该目录。如果该目录缺失,systemd 会将日志记录写入 {{ic|/run/systemd/journal}}。这意味着,系统重启后日志将丢失。<br />
<br />
=== 过滤输出 ===<br />
<br />
{{ic|journalctl}}可以根据特定字段过滤输出,例如:<br />
<br />
显示本次启动后的所有日志:<br />
<br />
# journalctl -b<br />
<br />
不过,一般大家更关心的不是本次启动后的日志,而是上次启动时的(例如,刚刚系统崩溃了)。目前还没有这项功能,正在 [http://comments.gmane.org/gmane.comp.sysutils.systemd.devel/6608 systemd-devel@lists.freedesktop.org] 讨论中。<br />
<br />
目前的折中方案是:<br />
<br />
# journalctl --since=today | tac | sed -n '/-- Reboot --/{n;:r;/-- Reboot --/q;p;n;b r}' | tac<br />
<br />
以上命令输出本日内的所有启动信息。但要注意,如果日志很多,该命令执行时间会比较漫长。<br />
<br />
动态跟踪最新信息:<br />
# journalctl -f<br />
<br />
显示特定程序的所有消息:<br />
{{bc|# journalctl /usr/lib/systemd/systemd}}<br />
<br />
显示特定进程的所有消息:<br />
{{bc|1=# journalctl _PID=1}}<br />
<br />
显示指定单元的所有消息:<br />
# journalctl -u netcfg<br />
<br />
详情参阅{{ic|man journalctl}}、{{ic|man systemd.journal-fields}},以及 Lennert 的这篇[http://0pointer.de/blog/projects/journalctl.html 博文]。<br />
<br />
=== 日志大小限制 ===<br />
<br />
如果按上面的操作保留日志的话,默认日志最大限制为所在文件系统容量的 10%,即:如果 {{ic|/var/log/journal}} 储存在 50GiB 的根分区中,那么日志最多存储 5GiB 数据。可以修改 {{ic|/etc/systemd/journald.conf}} 中的 {{ic|SystemMaxUse}} 来指定该最大限制。如限制日志最大 50MiB:<br />
<br />
SystemMaxUse=50M<br />
<br />
详情参见 {{ic|man journald.conf}}.<br />
<br />
=== 配合 syslog 使用 ===<br />
systemd 提供了 socket {{ic|/run/systemd/journal/syslog}},以兼容传统日志服务。所有系统信息都会被传入。要使传统日志服务工作,需要让服务链接该 socket,而非 {{ic|/dev/log}}([http://lwn.net/Articles/474968/ 官方说明])。Arch 软件仓库中的 {{pkg|syslog-ng}} 已经包含了需要的配置。<br />
<br />
设置开机启动 syslog-ng:<br />
# systemctl enable syslog-ng<br />
<br />
[http://0pointer.de/blog/projects/ 这里]有一份很不错的 {{ic|journalctl}} 指南。<br />
<br />
== 优化 ==<br />
<br />
参阅:[[Improve Boot Performance (简体中文)]]。<br />
<br />
=== 启动过程分析 ===<br />
systemd 提供了一个分析启动过程的工具 —— {{ic|systemd-analyze}}。可以用它看看哪些单元拖慢了开机过程,并据此进行优化。<br />
<br />
查看开机过程在内核/用户空间消耗的时间:<br />
$ systemd-analyze<br />
{{小贴士|* 如果在 {{ic|/etc/mkinitcpio.conf}} 的 {{ic|HOOKS}} 数组添加 {{ic|timestamp}},并重新生成启动内存镜像({{ic|mkinitcpio -p linux}}),{{ic|systemd-analyze}} 还可以显示处理该启动镜像花费的时间。<br />
* If you boot via [[UEFI]] and use a boot loader which implements systemds' [http://www.freedesktop.org/wiki/Software/systemd/BootLoaderInterface Boot Loader Interface] (which currently only [[Gummiboot]] does), '''systemd-analyze''' can additionally show you how much time was spent in the EFI firmware and the boot loader itself.}}<br />
<br />
按照耗费时间顺序,输出启动每个单元耗费的时间:<br />
$ systemd-analyze blame<br />
<br />
生成类似于 [[Bootchart (简体中文)|bootchart]] 的开机过程图表:<br />
$ systemd-analyze plot > plot.svg<br />
<br />
==== 使用 systemd-bootchart ====<br />
<br />
自2012年10月17日,bootchart 工具已经合并进 systemd 中,使用方法和原来的 bootchart 大同小异,添加下列内容到内核参数即可:<br />
<br />
initcall_debug printk.time=y init=/usr/lib/systemd/systemd-bootchart<br />
<br />
==== 使用 bootchart2 ====<br />
<br />
由于没有办法在内核参数设置两个 init,所以不能使用源里的 bootchart。不过,[[AUR (简体中文)|AUR]] 软件包 {{AUR|bootchart2-git}} 提供了一个的 systemd 服务,这样就可以和 systemd 一起使用。安装后启用即可:<br />
{{bc|# systemctl enable bootchart.service}}<br />
详情参阅 [https://github.com/mmeeks/bootchart bootchart 文档]。<br />
<br />
=== 预读 ===<br />
systemd 自己实现了一个 readahead,可以用来提高开机效率。不过,效果会因内核版本和硬件情况而不同(极少数情况下还会变慢)。开启 readahead:<br />
<br />
# systemctl enable systemd-readahead-collect.service systemd-readahead-replay.service<br />
<br />
要知道,readahead 的超级牛力只有在重启几次后才会显现。<br />
<br />
== 疑难解答 ==<br />
<br />
=== 关机/重启十分缓慢 ===<br />
<br />
如果关机特别慢(甚至跟死机了一样),很可能是某个拒不退出的服务在作怪。systemd 会等待一段时间,然后再尝试杀死它。请阅读[http://freedesktop.org/wiki/Software/systemd/Debugging#Shutdown_Completes_Eventually 这篇文章],确认你是否是该问题受害者。<br />
<br />
=== 短时进程无日志记录 ===<br />
<br />
若 {{ic|journalctl -u foounit.service}} 没有显示某个短时进程的任何输出,那么改用 PID 试试。例如,若 {{ic|systemd-modules-load.service}} 执行失败,那么先用 {{ic|systemctl status systemd-modules-load}} 查询其 PID(比如是123),然后检索该 PID 相关的日志 {{ic|journalctl -b _PID&#61;123}}。运行时进程的日志元数据(诸如 _SYSTEMD_UNIT 和 _COMM)被乱序收集在 {{ic|/proc}} 目录。要修复该问题,必须修改内核,使其通过套接字连接来提供上述数据,该过程类似于 SCM_CREDENTIALS。<br />
<br />
=== Diagnosing boot problems ===<br />
<br />
Boot with these parameters on the kernel command line:<br />
{{ic|<nowiki>systemd.log_level=debug systemd.log_target=kmsg log_buf_len=1M</nowiki>}}<br />
<br />
[http://freedesktop.org/wiki/Software/systemd/Debugging More Debugging Information]<br />
<br />
=== Disabling application crash dumps ===<br />
<br />
Just link {{ic|/etc/sysctl.d/coredump.conf}} to {{ic|/dev/null}} then apply with sysctl ([https://bbs.archlinux.org/viewtopic.php?id=154511 source]):<br />
<br />
# ln -s /dev/null /etc/sysctl.d/coredump.conf<br />
# /usr/lib/systemd/systemd-sysctl<br />
# ulimit -c unlimited<br />
<br />
== 相关资源 ==<br />
<br />
*[http://www.freedesktop.org/wiki/Software/systemd 官方网站]<br />
*[http://0pointer.de/public/systemd-man/ man 手册页]<br />
*[http://freedesktop.org/wiki/Software/systemd/Optimizations systemd 优化]<br />
*[http://www.freedesktop.org/wiki/Software/systemd/FrequentlyAskedQuestions 常见问题 FAQ]<br />
*[http://www.freedesktop.org/wiki/Software/systemd/TipsAndTricks 小技巧]<br />
*[http://0pointer.de/public/systemd-ebook-psankar.pdf systemd for Administrators (PDF)]<br />
*[http://fedoraproject.org/wiki/Systemd Fedora 项目对 systemd 的介绍]<br />
*[http://fedoraproject.org/wiki/How_to_debug_Systemd_problems 如何调试 systemd 故障]<br />
*''The H Open'' 杂志上的[http://www.h-online.com/open/features/Control-Centre-The-systemd-Linux-init-system-1565543.html 两][http://www.h-online.com/open/features/Booting-up-Tools-and-tips-for-systemd-1570630.html 篇]科普文章<br />
*[http://0pointer.de/blog/projects/systemd.html Lennart 的博文]<br />
*[http://0pointer.de/blog/projects/systemd-update.html 更新报告]<br />
*[http://0pointer.de/blog/projects/systemd-update-2.html 更新报告2]<br />
*[http://0pointer.de/blog/projects/systemd-update-3.html 更新报告3]<br />
*[http://0pointer.de/blog/projects/why.html 最新动态]<br />
*[https://fedoraproject.org/wiki/SysVinit_to_Systemd_Cheatsheet/zh Fedora Wiki 上的 SysVinit 和 systemd 命令对比表]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Systemd_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=257592
Systemd (简体中文)
2013-05-18T11:53:46Z
<p>Cuihao: 该同步 === Units === 了</p>
<hr />
<div>{{Lowercase title}}<br />
[[Category:Daemons and system services (简体中文)]]<br />
[[Category:Boot process (简体中文)]]<br />
[[en:Systemd]]<br />
[[es:Systemd]]<br />
[[fr:Systemd]]<br />
[[it:Systemd]]<br />
[[ja:Systemd]]<br />
[[ru:Systemd]]<br />
[[zh-TW:Systemd]]<br />
{{Article summary start|概览}}<br />
{{Article summary text|本文介绍 Systemd 的安装和配置。}}<br />
{{Article summary heading|相关页面}}<br />
{{Article summary wiki|Systemd/Services}}<br />
{{Article summary wiki|systemd/User}}<br />
{{Article summary wiki|Systemd FAQ}}<br />
{{Article summary wiki|init Rosetta}}<br />
{{Article summary wiki|udev (简体中文)}}<br />
{{Article summary end}}<br />
{{TranslationStatus (简体中文)|Systemd|2013-03-14|250286}}<br />
摘自[http://freedesktop.org/wiki/Software/systemd 项目主页]:<br />
<br />
'''''systemd''' 是 Linux 下的一款系统和服务管理器,兼容 SysV 和 LSB 的启动脚本。systemd 的特性有:支持并行化任务;同时采用 socket 式与 [[D-Bus (简体中文)|D-Bus]] 总线式激活服务;按需启动守护进程(daemon);利用 Linux 的 [[cgroups|cgroups]] 监视进程;支持快照和系统恢复;维护挂载点和自动挂载点;各服务间基于依赖关系进行精密控制。systemd 完全可以替代 Arch 曾经使用的 [[SysVinit (简体中文)|sysvinit]] 启动系统。''<br />
<br />
{{注意|[https://bbs.archlinux.org/viewtopic.php?pid&#61;1149530#p1149530 这个帖子]详细解释了 Arch 向 systemd 迁移的原因。}}<br />
<br />
另见[[Wikipedia:Systemd|维基百科上的介绍]]。<br />
<br />
== 迁移前需考虑 ==<br />
<br />
* 阅读[http://freedesktop.org/wiki/Software/systemd/ 该站],了解 systemd。<br />
* systemd 自己有一套日志('''journal''')系统,用于代替 '''syslog'''。两者也可以共存,参见后面的[[#日志|日志部分]]。<br />
* 虽然 systemd 可以替换 '''cron'''、'''acpid'''、'''xinetd''' 等的部分功能。至少目前还可以继续使用这些服务,无需立即切换。<br />
* 交互式 initscripts 启动脚本在 systemd 中无法工作。例如 '''netcfg-menu''' [https://bugs.archlinux.org/task/31377 无法]在启动时显示。<br />
<br />
== 安装 ==<br />
{{注意|从[https://www.archlinux.org/news/systemd-is-now-the-default-on-new-installations/ 2012-10-13版安装介质]开始,安装程序已经默认安装{{pkg|systemd}} 和 {{pkg|systemd-sysvcompat}}。}}<br />
{{注意|如果是在 VPS 中使用 Arch,请先阅读:[[Virtual_Private_Server#Moving_your_VPS_from_initscripts_to_systemd]]。}}<br />
<br />
本部分帮助还在使用{{pkg|sysvinit}} 和 {{pkg|initscripts}} 的用户迁移到{{pkg|systemd}}.<br />
<br />
# 从[[Official_Repositories_(简体中文)|官方软件仓库]]安装 {{pkg|systemd}} 并添加[[Kernel parameters|内核参数]]{{Ic|1=init=/usr/lib/systemd/systemd}}<br />
# 使用 {{ic|systemctl enable <服务名>}} 启用需要的服务(大致相当于以前 {{ic|DAEMONS}} 数组的作用,新的服务名称参见 [[Daemons_List_(简体中文)]])。<br />
# 重启系统,执行命令 {{ic|cat /proc/1/comm}},如果返回{{ic|systemd}},表示 systemd 已经正常启动。<br />
# 确认主机名已经正确设置:{{ic|hostnamectl set-hostname myhostname}}。<br />
# 删除 {{pkg|initscripts}} 和 {{pkg|sysvinit}},并安装{{pkg|systemd-sysvcompat}}.<br />
# (可选)删除{{ic|1=init=/usr/lib/systemd/systemd}}内核参数,现在已经不需要它了。{{pkg|systemd-sysvcompat}} 软件包提供了一个软链接,使 systemd 成为默认 init。<br />
<br />
=== 附加信息 ===<br />
* 如果内核参数中有 {{ic|quiet}},建议在一开始先去掉,以便调试。<br />
{{Accuracy|reason=据有的用户称,systemd 并未对用户组有特殊要求。尽管有很多不常用的用户组,但从 consolekit 切换到 logind 后这些用户组没有发生改变。''(据译者理解:如果你碰到特殊的权限问题,可能仍需要加入这些用户组才能解决。)''}}<br />
* 使用 systemd 的时候'''无需'''将用户加入特殊[[Users and Groups (简体中文)|用户组]](如{{ic|sys}}、{{ic|disk}}、{{ic|lp}}、{{ic|network}}、{{ic|video}}、{{ic|audio}}、{{ic|optical}}、{{ic|storage}}、{{ic|scanner}}、{{ic|power}}等等)。加入这些组反而会有问题,例如audio组会导致程序阻塞软件混声。每个 PAM 登录都拥有一个 logind 会话,它通过[[Wikipedia:Access control list|POSIX ACLs]],赋予本地会话以声音设备访问权限、通过[[udisks]]挂载和卸载移动设备的权限等。<br />
* 阅读 [[Network Configuration (简体中文)]],了解如何配置网络。<br />
<br />
== 原生 systemd 配置文件 ==<br />
{{注意|可能需要手动创建某些文件。所有文件的权限都是644,属主 root,属组 root。}}<br />
<br />
=== 虚拟控制台 ===<br />
{{ic|/etc/vconsole.conf}} 文件用来配置虚拟控制台,包括键盘布局和控制台字体:<br />
{{hc|/etc/vconsole.conf|<nowiki><br />
KEYMAP=us<br />
FONT=lat9w-16<br />
FONT_MAP=8859-1_to_uni</nowiki>}}<br />
<br />
{{注意|{{ic|{{Pkg|systemd}}-194}} 默认使用内核字体和键盘布局,因此以后不必在上述文件中留下空的 {{ic|1=KEYMAP=}} 和 {{ic|1=FONT=}} 设置。}}<br />
<br />
还可以通过命令设置键盘布局:<br />
# localectl set-keymap de<br />
<br />
{{ic|localectl}} 也可以设置X11的键盘布局:<br />
<br />
# localectl set-x11-keymap de<br />
<br />
详情参见 {{ic|man 1 localectl}} 和 {{ic|man 5 vconsole.conf}}。<br />
<br />
* 另参阅 [[Fonts (简体中文)#控制台字体|控制台字体]] 和 [[KEYMAP (简体中文)#键盘布局|键盘布局]]。<br />
<br />
=== 硬件时钟 ===<br />
<br />
systemd 默认识别硬件时钟为协调世界时(UTC)。<br />
<br />
{{小贴士|推荐使用[[NTP (简体中文)|NTP服务]]来在线同步硬件时钟。}}<br />
<br />
==== 硬件时钟设定为地方时 ====<br />
<br />
将硬件时钟配置为地方时('''不建议'''):<br />
<br />
# timedatectl set-local-rtc true<br />
<br />
重新调整为 UTC:<br />
<br />
# timedatectl set-local-rtc false<br />
<br />
如果设置成本地时间,处理夏令时有些麻烦。如果夏令时调整发生在关机时,下次启动时时间会出现问题([http://www.cl.cam.ac.uk/~mgk25/mswish/ut-rtc.html 更多信息])。最新的内核直接从实时时钟芯片(RTC)读取时间,不使用 {{ic|hwclock}},内核把从 RTC 读取的时间当作 UTC 处理。所以如果硬件时间是地方时,系统启动一开始识别的时间是错误的,之后很快会进行矫正。这可能导致一些问题(尤其是时间倒退时)。<br />
<br />
如果同时安装了 Windows 操作系统([http://blogs.msdn.com/b/oldnewthing/archive/2004/09/02/224672.aspx 默认使用地方时]),那么一般 RTC 会被设置为地方时。Windows 其实也能处理 UTC,需要[[Time (简体中文)#Windows 系统使用 UTC|修改注册表]]。建议让 Windows 使用 UTC,而非让 Linux 使用地方时。Windows 使用 UTC 后,请记得禁用 Windows 的时间同步功能,以防 Windows 错误设置硬件时间。如上文所说,Linux 可以使用[[NTP (简体中文)|NTP服务]]来在线同步硬件时钟。<br />
<br />
* 详情参阅 [[Time (简体中文)]]。<br />
<br />
=== 内核模块 ===<br />
目前,所有必要模块的加载均由 [[udev]] 自动完成。所以,如果不需要使用任何额外的模块,就没有必要在任何配置文件中添加启动时加载的模块。但是,有些情况下可能需要在系统启动时加载某个额外的模块,或者将某个模块列入黑名单以便使系统正常运行。<br />
<br />
==== 开机加载 ====<br />
systemd 读取 {{ic|/etc/modules-load.d/}} 中的配置加载额外的内核模块。配置文件名称通常为 {{ic|/etc/modules-load.d/<program>.conf}}。格式很简单,一行一个要读取的模块名,而空行以及第一个非空格字符为{{ic|#}}或{{ic|;}}的行会被忽略,如:<br />
{{hc|/etc/modules-load.d/virtio-net.conf|<nowiki><br />
# Load virtio-net.ko at boot<br />
virtio-net</nowiki>}}<br />
<br />
另见{{ic|man 5 modules-load.d}}。<br />
<br />
==== 配置内核模块参数 ====<br />
<br />
在 {{ic|/etc/modprobe.d/modprobe.conf}} 中设置额外的模块参数。<br />
<br />
例如:<br />
<br />
* 我们已经在 {{ic|/etc/modules-load.d/loop.conf}} 中添加文本 {{ic|loop}},告诉系统开机自动加载该模块。<br />
<br />
* 在 {{ic|/etc/modprobe.d/modprobe.conf}} 设置额外的参数,如:{{ic|options loop max_loop&#61;64}}。<br />
<br />
最后,可以通过 {{ic|cat /sys/module/loop/parameters/max_loop}} 命令检查配置是否生效。<br />
<br />
==== 禁用内核模块 ====<br />
禁用内核模块的方法和 Arch 默认的 {{Pkg|initscripts}} 相同,因为该过程实际由 {{Pkg|kmod}} 处理。参见:[[Kernel modules (简体中文)#黑名单]]。<br />
<br />
=== 文件系统挂载 ===<br />
<br />
默认行为是:在启动一个需要挂载特定分区的服务之前,系统自动检查并挂载分区。{{ic|/etc/fstab}} 中设定的网络文件系统(如 [[NFS (简体中文)|NFS]]、[[Samba (简体中文)|Samba]])无需配置即可正常工作,systemd 将确保网络文件系统在网络链接就绪后挂载。<br />
<br />
详情参阅:{{ic|man 5 systemd.mount}}。<br />
<br />
==== 自动挂载 ====<br />
<br />
* 如果 {{ic|/home}} 分区较大,可以让不依赖 {{ic|/home}} 分区的服务先启动。把下面的参数添加到 {{ic|/etc/fstab}} 文件中 {{ic|/home}} 项目的参数部分即可:<br />
<br />
noauto,x-systemd.automount<br />
<br />
这样 {{ic|/home}} 分区只有需要访问时才会被挂载。内核会缓存所有的文件操作,直到 {{ic|/home}} 分区准备完成。<br />
<br />
{{注意|这样做会使 {{ic|/home}} 的文件系统类型被识别为 {{ic|autofs}},造成 [[mlocate]] 查询时忽略该目录。实际加速效果因配置而异,所以请自己权衡是否需要。}}<br />
<br />
* 挂载远程文件系统也是同理。如果你仅想在需要的时候才挂载,也可以添加 {{ic|noauto,x-systemd.automount}} 参数。另外,可以设置 {{ic|1=x-systemd.device-timeout=#}} 参数,设置超时时间,以防止网络资源不能访问的时候浪费时间。<br />
<br />
* 如果你的加密文件系统需要密钥,则需要添加 {{ic|noauto}} 参数到 {{ic|/etc/crypttab}} 文件中的对应位置。systemd 开机的时候就不会打开这个加密设备,会一直等待到设备被访问时再使用密钥文件挂载。比如在使用加密RAID设备的时候可以节省一定的时间,因为 systemd 不必等到设备可用后才能访问。例如:<br />
<br />
{{hc|/etc/crypttab|<br />
data /dev/md0 /root/key noauto}}<br />
<br />
==== LVM ====<br />
<br />
如果装有不通过 [[Mkinitcpio (简体中文)|initramfs]] 激活的[[LVM (简体中文)|LVM]]卷,则需启动 {{ic|lvm-monitoring}} 服务(由 {{pkg|lvm2}} 软件包提供):<br />
<br />
# systemctl enable lvm-monitoring<br />
<br />
=== ACPI 电源管理 ===<br />
systemd 能够处理某些电源相关的 [[Wikipedia:Advanced_Configuration_and_Power_Interface|ACPI]] 事件,通过 {{ic|/etc/systemd/logind.conf}} 的下列选项配置:<br />
* {{ic|HandlePowerKey}}:按下电源键后的动作<br />
* {{ic|HandleSleepKey}}:按下挂起键后的动作<br />
* {{ic|HandleHibernateKey}}: 按下休眠键后的动作<br />
* {{ic|HandleLidSwitch}}:合上笔记本盖后待机<br />
<br />
动作可以是 {{ic|ignore}}、{{ic|poweroff}}、{{ic|reboot}}、{{ic|halt}}、{{ic|suspend}}、{{ic|hibernate}}、{{ic|hybrid-sleep}}、{{ic|lock}} 或 {{ic|kexec}}。<br />
<br />
系统默认设置为:<br />
HandlePowerKey=poweroff<br />
HandleSuspendKey=suspend<br />
HandleHibernateKey=hibernate<br />
HandleLidSwitch=suspend<br />
<br />
不用图形界面、或者使用 [[i3 (简体中文)|i3]]、[[awesome (简体中文)|awesome]] 这样简单的桌面管理器时,systemd 可以替代 [[acpid (简体中文)|acpid]] 处理 ACPI 事件。<br />
<br />
{{注意|运行 {{ic|systemctl restart systemd-logind}},使上述更改立即生效。}}<br />
<br />
{{注意|systemd 无法处理交流电源和电池 ACPI 事件,所以还得使用 [[Laptop Mode Tools (简体中文)|Laptop Mode Tools]] 或 [[acpid (简体中文)|acpid]] 工具。}}<br />
<br />
在当前版本的 systemd 中,这些 {{ic|Handle}} 选项将会被应用到整个系统当中,除非它们被别的程序——例如某个桌面环境中的电源管理器——给“阻止”(inhibited)。如果其它的程序没有阻止这些 {{ic|Handle}} ,你可能会先被 systemd 挂起你的系统,然后当系统被唤醒之后,电源管理器又会再次将系统挂起。<br />
<br />
{{警告|目前只有[[GNOME]] 和 [[KDE]] 支持 "inhibited" 命令。在其它的桌面管理器同样实现该功能之前,如果你想使用[[Xfce]]、[[acpid]] 或者其它程序来管理 ACPI 事件,你需要把 {{ic|Handle}} 选项设置为 {{ic|ignore}}。}}<br />
<br />
{{注意|除了内核默认的待机支持后端(用于处理待机/休眠),systemd 也可以使用其他后端(比如 [[Uswsusp]] 或 [[TuxOnIce]])。}}<br />
<br />
要令 {{ic|systemctl hibernate}} 工作,需要按照[[Pm-utils#Hibernation_.28suspend2disk.29|休眠]]和 [[Pm-utils#Mkinitcpio_Resume_Hook|mkinitcpio 唤醒扩展]]的设置步骤进行操作。(不必安装 {{ic|pm-utils}})<br />
<br />
==== 休眠时执行的脚本 ====<br />
<br />
使用 {{ic|systemctl suspend}}、{{ic|systemctl hibernate}} 或 {{ic|systemctl hybrid-sleep}} 命令执行待机/休眠时,systemd 不会调用 [[pm-utils (简体中文)|pm-utils]]。[[pm-utils (简体中文)|pm-utils]] 的钩子扩展(hook)——包括 [[pm-utils (简体中文)#建立你自己的钩子|自定义钩子]]——会失效。不过,systemd 提供了两种类似的待机/休眠时执行脚本的机制。<br />
<br />
===== 使用服务文件 =====<br />
<br />
可以将服务文件附在 suspend.target、hibernate.target 或 sleep.target 中,这样就能在待机/休眠前后执行某些操作。用户级操作和root/系统级操作应该使用不同的服务文件。要启用用户级服务文件,使用 {{ic|# systemctl enable suspend@<用户名> && systemctl enable resume@<用户名>}}。例如:<br />
<br />
{{hc|/etc/systemd/system/suspend@.service|2=<nowiki><br />
[Unit]<br />
Description=User suspend actions<br />
Before=sleep.target<br />
<br />
[Service]<br />
User=%I<br />
Type=forking<br />
Environment=DISPLAY=:0<br />
ExecStartPre= -/usr/bin/pkill -u %u unison ; /usr/local/bin/music.sh stop ; /usr/bin/mysql -e 'slave stop'<br />
ExecStart=/usr/bin/sflock<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
{{hc|/etc/systemd/system/resume@.service|2=<nowiki><br />
[Unit]<br />
Description=User resume actions<br />
After=suspend.target<br />
<br />
[Service]<br />
User=%I<br />
Type=simple<br />
ExecStartPre=/usr/local/bin/ssh-connect.sh<br />
ExecStart=/usr/bin/mysql -e 'slave start'<br />
<br />
[Install]<br />
WantedBy=suspend.target</nowiki>}}<br />
<br />
至于root/系统级服务(使用 {{ic|# systemctl enable root-suspend}} 激活):<br />
<br />
{{hc|/etc/systemd/system/root-resume.service|2=<nowiki><br />
[Unit]<br />
Description=Local system resume actions<br />
After=suspend.target<br />
<br />
[Service]<br />
Type=simple<br />
ExecStart=/usr/bin/systemctl restart mnt-media.automount<br />
<br />
[Install]<br />
WantedBy=suspend.target</nowiki>}}<br />
<br />
{{hc|/etc/systemd/system/root-suspend.service|2=<nowiki><br />
[Unit]<br />
Description=Local system suspend actions<br />
Before=sleep.target<br />
<br />
[Service]<br />
Type=simple<br />
ExecStart=-/usr/bin/pkill sshfs<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
上述服务文件的一些解释(详见 {{ic|man systemd.service}}):<br />
* 如果设置 {{ic|1=<nowiki>Type=OneShot</nowiki>}},那么可以使用多个 {{ic|1=<nowiki>ExecStart=</nowiki>}} 参数。否则只能写一个,替代方案是在 {{ic|ExecStartPre}} 中添加命令,或使用分号分隔不同命令(见第一个例子,分号前后的空格都是'''必须'''的)。 <br />
* 若命令前加上一个“-”(半角减号),则命令返回非零值时会被忽略、当作正常执行处理。<br />
* 有关调试,最好的方法是用 {{ic|journalctl}} 查看日志。<br />
<br />
===== 合并待机和唤醒服务文件 =====<br />
<br />
利用下面这种自定义的一体化待机和唤醒服务,使用单一的钩子扩展即可对不同操作(待机/休眠/混合休眠)的不同阶段(进入/唤醒)进行控制。<br />
<br />
例子和解释:<br />
<br />
{{hc|/etc/systemd/system/wicd-sleep.service|2=<nowiki><br />
[Unit]<br />
Description=Wicd sleep hook<br />
Before=sleep.target<br />
StopWhenUnneeded=yes<br />
<br />
[Service]<br />
Type=oneshot<br />
RemainAfterExit=yes<br />
ExecStart=-/usr/share/wicd/daemon/suspend.py<br />
ExecStop=-/usr/share/wicd/daemon/autoconnect.py<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
* {{ic|1=<nowiki>RemainAfterExit=yes</nowiki>}}:服务启动后,除非显式地停止,否则就认为是活动的。<br />
* {{ic|1=<nowiki>StopWhenUnneeded=yes</nowiki>}}:服务活动时,如果无其他服务依赖该服务,就停止它。在本例中,该服务会在 sleep.target 停止后停止活动。<br />
* 由于 sleep.target 会被 suspend.target、hibernate.target、hybrid-sleep.target 调用,且 sleep.target 本身设置了 StopWhenUnneeded,该服务文件对以上各种操作都是有效的。<br />
<br />
===== 使用 /usr/lib/systemd/system-sleep =====<br />
<br />
systemd 在待机/休眠时执行 {{ic|/usr/lib/systemd/system-sleep/}} 里的所有脚本,传递下面两个参数:<br />
<br />
* 参数1:若是准备进入待机/休眠状态,则为 {{ic|pre}};唤醒时为 {{ic|post}}。<br />
* 参数2:事件名称,{{ic|suspend}},{{ic|hibernate}} 或 {{ic|hybrid-sleep}}。<br />
<br />
systemd 会同时执行所有脚本,而不是像 [[pm-utils (简体中文)|pm-utils]] 那样顺序执行。<br />
<br />
脚本输出会记录在相关服务({{ic|systemd-suspend.service}}、{{ic|systemd-hibernate.service}} 或 {{ic|systemd-hybrid-sleep.service}})中。通过[[#日志|日志]]查看:<br />
# journalctl -b -u systemd-suspend<br />
<br />
脚本示范:<br />
<br />
{{hc|/usr/lib/systemd/system-sleep/example.sh|<br />
#!/bin/sh<br />
case $1/$2 in<br />
pre/*)<br />
echo "进入 $2 状态..."<br />
;;<br />
post/*)<br />
echo "从 $2 状态唤醒..."<br />
;;<br />
esac}}<br />
<br />
记得添加可执行权限: <br />
# chmod a+x /usr/lib/systemd/system-sleep/example.sh<br />
<br />
详情参见 {{ic|man 7 systemd.special}} 和 {{ic|man 8 systemd-sleep}}。<br />
<br />
=== 临时文件 ===<br />
{{ic|/usr/lib/tmpfiles.d/}} 和 {{ic|/etc/tmpfiles.d/}} 中的文件描述了 systemd-tmpfiles 如何创建、清理、删除临时文件和目录,这些文件和目录通常存放在 {{ic|/run}} 和 {{ic|/tmp}} 中。配置文件名称为 {{ic|/etc/tmpfiles.d/<program>.conf}}。此处的配置能覆盖 {{ic|/usr/lib/tmpfiles.d/}} 目录中的同名配置。<br />
<br />
临时文件通常和服务文件同时提供,以生成守护进程需要的文件和目录。例如 [[Samba (简体中文)|Samba]] 服务需要目录 {{ic|/run/samba}} 存在并设置正确的权限位,就象这样:<br />
{{hc|/usr/lib/tmpfiles.d/samba.conf|<br />
D /run/samba 0755 root root<br />
}}<br />
<br />
此外,临时文件还可以用来在开机时向特定文件写入某些内容。比如,要禁止系统从USB设备唤醒,利用旧的 {{ic|/etc/rc.local}} 可以用 {{ic|echo USBE > /proc/acpi/wakeup}},而现在可以这么做:<br />
{{hc|/etc/tmpfiles.d/disable-usb-wake.conf|<br />
w /proc/acpi/wakeup - - - - USBE<br />
}}<br />
<br />
详情参见 {{ic|man 5 tmpfiles.d}}。<br />
<br />
{{注意|该方法不能向 {{ic|/sys}} 中的配置文件添加参数,因为 {{ic|systemd-tmpfiles-setup}} 有可能在相关模块加载前运行。这种情况下,需要首先通过 {{ic|modinfo <模块名>}} 确认需要的参数,并在 [[Kernel modules (简体中文)#选项|{{ic|/etc/modprobe.d}} 下的一个文件]]中设置改参数。另外,还可以使用 [[udev (简体中文)#udev规则|udev 规则]],在设备就绪时设置相应属性。}}<br />
<br />
=== 单元(unit) ===<br />
一个单元配置文件可以描述如下内容之一:系统服务、socket、系统设备、挂载点、交换分区/文件、启动目标(target)、文件系统路径、由 systemd 管理的计时器。文件格式受 .desktop 文件启发,而最初起源是 Windows 下的 .ini 文件。<br />
<br />
详情参阅 {{ic|man 5 systemd.unit}}.<br />
<br />
== 从 initscripts 迁移到 systemd ==<br />
<br />
=== initscripts 模拟 ===<br />
<br />
{{Pkg|initscripts}} 软件包提供了 systemd 和 Arch 传统配置的整合。当同时安装了{{Pkg|initscripts}}和systemd时,若以systemd启动系统,systemd将执行以下操作:<br />
<br />
# 解析{{ic|/etc/rc.conf}}中的{{ic|DAEMONS}}数组,并在系统启动时,启动所有列出的守护进程<br />
# 在启动过程中执行{{ic|/etc/rc.local}}<br />
# 在系统关闭过程中执行{{ic|/etc/rc.local.shutdown}}<br />
<br />
Initscripts模拟仅仅是作为用户迁移到systemd时的过渡措施,这一方案最终将'''不再可行'''。原生的systemd并不依赖于{{ic|rc.conf}}文件的集中式配置,因此推荐使用[[#原生 systemd 配置文件|原生的systemd配置文件]]。原生systemd配置文件比{{ic|/etc/rc.conf}}具有更高优先级。<br />
<br />
{{注意|替换{{ic|/etc/rc.local}}的推荐方式是针对系统启动时要运行的程序编写自定义的service文件,详见相关[[#自己编写_.service_文件|章节]].}}<br />
<br />
{{注意|如果你曾经在{{ic|/etc/inittab}}中禁用了以{{keypress|Ctrl+Alt+Del}}重启系统,则需要以root执行{{ic|systemctl mask ctrl-alt-del.target}}以使其对systemd重新生效。}}<br />
<br />
{{警告|如果同时安装 systemd(197-4 及后续版本)和 initscripts,那么 {{ic|/etc/rc.local}} 脚本会导致启动过程无法结束(原因是 getty@tty1.service 自 197-4 版本起不再等待 rc-local.service 执行,getty 无法启动)。请删除或重命名 {{ic|/etc/rc.local}}。}}<br />
<br />
=== 迁移 DAEMONS 列表 ===<br />
<br />
若要用纯 systemd 环境,则应当删除 {{ic|/etc/rc.conf}} 文件,并完全通过 {{ic|systemctl}} 命令来启动服务。对于 {{ic|DAEMONS}} 列表里面的每个 {{ic|<服务名>}},现在可以用以下命令设置成自动启动:<br />
<br />
# systemctl enable <服务名>.service<br />
<br />
{{小贴士|[[Daemons List|这里]]有一份 initscripts 和 systemd 下常见服务的对应列表。}}<br />
<br />
如果提示 {{ic|<service_name>.service}} 不存在:<br />
<br />
* 很有可能是 systemd 用了不同的服务名。比如 {{ic|crond}} 服务替换成 {{ic|cronie.service}};{{ic|alsa}} 服务替换成了 {{ic|alsa-store.service}} 和 {{ic|alsa-restore.service}} 两个服务。另一个值得注意的是 {{ic|network}} 服务,它被替换为一整套服务文件(详情参见 [[Configuring_Network_(简体中文)]])。<br />
* 另外,有些程序尚未提供 .service 文件,还得继续用 {{ic|rc.conf}} 来配置开机自动启动。<br />
<br />
{{小贴士|用下面这个命令来查看已安装软件包包含的服务文件:<br />
$ pacman -Ql cronie<br />
[...]<br />
cronie /etc/rc.d/crond # DAEMONS 列表中使用的 initscript 服务 (原生 systemd 配置中不再使用)<br />
[...]<br />
cronie /usr/lib/systemd/system/cronie.service # 原生 systemd 服务文件<br />
[...]<br />
}}<br />
<br />
* 有些服务不需要用户明确配置自启动。比如安装 {{ic|dbus-core}} 软件包后 {{ic|dbus.service}} 就会自动被配置为自启动。而 {{ic|alsa-store.service}} 和 {{ic|alsa-restore.service}} 也会由 systemd 自动启动。可以用 {{ic|systemctl list-unit-files}} 命令来查看服务列表并检查运行状态。<br />
<br />
== systemd 基本工具 ==<br />
<br />
检视和控制systemd的主要命令是{{ic|systemctl}}。该命令可用于查看系统状态和管理系统及服务。详见{{ic|man 1 systemctl}}。<br />
<br />
{{小贴士|在 {{ic|systemctl}} 参数中添加 {{ic|-H <用户名>@<主机名>}} 可以实现对其他机器的远程控制。该过程使用 [[SSH (简体中文)|SSH]] 链接。}}<br />
<br />
{{注意|{{ic|systemadm}} 是 systemd 的官方图形前端。由 [[AUR (简体中文)|AUR]] 中的软件包 {{AUR|systemd-ui-git}} 提供。}}<br />
<br />
=== 分析系统状态 ===<br />
<br />
输出激活的单元:<br />
<br />
$ systemctl<br />
<br />
以下命令等效:<br />
<br />
$ systemctl list-units<br />
<br />
输出运行失败的单元:<br />
<br />
$ systemctl --failed<br />
<br />
所有可用的单元文件存放在 {{ic|/usr/lib/systemd/system/}} 和 {{ic|/etc/systemd/system/}} 目录(后者优先级更高)。查看所有已安装服务:<br />
$ systemctl list-unit-files<br />
<br />
=== 使用单元 ===<br />
一个单元可以是系统服务({{ic|.service}})、挂载点({{ic|.mount}})、sockets({{ic|.sockets}})。<br />
<br />
使用 {{ic|systemctl}} 控制单元时,通常需要使用单元文件的全名,包括扩展名(例如 {{ic|sshd.service}})。但是有些单元可以在{{ic|systemctl}}中使用简写方式。<br />
* 如果无扩展名,systemctl 默认把扩展名当作 {{ic|.service}}。例如 {{ic|netcfg}} 和 {{ic|netcfg.service}} 是等价的。<br />
* 挂载点会自动转化为相应的 {{ic|.mount}} 单元。例如 {{ic|/home}} 等价于 {{ic|home.mount}}。<br />
* 设备会自动转化为相应的 {{ic|.device}} 单元,所以 {{ic|/dev/sda2}} 等价于 {{ic|dev-sda2.device}}。<br />
<br />
立即激活单元:<br />
<br />
# systemctl start <单元><br />
<br />
立即停止单元:<br />
# systemctl stop <单元><br />
<br />
重启单元:<br />
<br />
# systemctl restart <单元><br />
<br />
命令单元重新读取配置:<br />
<br />
# systemctl reload <单元><br />
<br />
输出单元运行状态:<br />
<br />
$ systemctl status <单元><br />
<br />
检查单元是否配置为自动启动:<br />
$ systemctl is-enabled <单元><br />
<br />
开机自动激活单元:<br />
<br />
# systemctl enable <单元><br />
<br />
{{注意|如果服务没有{{ic|Install}}段落,一般意味着应该通过其它服务自动调用它们。如果真的需要手动安装,可以直接连接服务,如下(将{{ic|foo}}替换为真实的服务名):<br />
{{bc|# ln -s /usr/lib/systemd/system/foo.service /etc/systemd/system/graphical.target.wants/}}<br />
}}<br />
<br />
取消开机自动激活单元:<br />
<br />
# systemctl disable <单元><br />
<br />
显示单元的手册页(必须由单元文件提供):<br />
<br />
# systemctl help <单元><br />
<br />
重新载入 systemd,扫描新的或有变动的单元:<br />
# systemctl daemon-reload<br />
<br />
=== 电源管理 ===<br />
<br />
安装 {{ic|polkit}} 后才可使用电源管理。<br />
<br />
如果你正登录在一个本地的{{ic|systemd-logind}}用户会话,且当前没有其它活动的会话,那么以下命令无需root权限即可执行。否则(例如,当前有另一个用户登录在某个tty),systemd 将会自动请求输入root密码。<br />
<br />
重启:<br />
<br />
$ systemctl reboot<br />
<br />
退出系统并停止电源:<br />
<br />
$ systemctl poweroff<br />
<br />
待机:<br />
<br />
$ systemctl suspend<br />
<br />
休眠:<br />
<br />
$ systemctl hibernate<br />
<br />
混合休眠模式(同时休眠到硬盘并待机):<br />
<br />
$ systemctl hybrid-sleep<br />
<br />
== 启动桌面环境 ==<br />
<br />
=== 使用登录管理器 ===<br />
<br />
通过启动[[Display Manager (简体中文)|登录管理器]](或称显示管理器),即可进行图形界面登录。目前,Arch 提供了 [[GDM (简体中文)|GDM]]、[[KDM (简体中文)|KDM]]、[[SLiM (简体中文)|SLiM]]、[[XDM (简体中文)|XDM]]、[[LXDM (简体中文)|LXDM]] 和 [[LightDM (简体中文)|LightDM]] 的 systemd 服务文件。以 KDM 为例,配置开机启动:<br />
# systemctl enable kdm.service<br />
<br />
执行上述命令后,登录管理器应当能正常工作了。如果不是的话,很可能是因为你修改了{{ic|default.target}}。默认情况应当如下:<br />
<br />
{{hc|# ls -l /etc/systemd/system/default.target|/etc/systemd/system/default.target -> /usr/lib/systemd/system/graphical.target}}<br />
<br />
删除被修改的 {{ic|default.target}} 即可,systemd 会自动使用默认配置(即 {{ic|graphical.target}}):<br />
<br />
# rm /etc/systemd/system/default.target<br />
<br />
=== 使用 systemd-logind ===<br />
<br />
{{注意|自 2012-10-30 起, [[ConsoleKit]] 已被 [https://www.archlinux.org/news/consolekit-replaced-by-logind/ systemd-logind 取代] ,作为登录到桌面环境的默认方式。}}<br />
<br />
可使用 {{ic|loginctl}} 来查看用户会话的状态。所有 [[PolicyKit]] 操作,如挂起系统、挂载外部驱动器,都无需配置即可使用。<br />
<br />
$ loginctl show-session $XDG_SESSION_ID<br />
<br />
== 自己编写 .service 文件 ==<br />
''参见:[[Systemd/Services]]''<br />
<br />
=== 处理依赖关系 ===<br />
<br />
使用systemd时,可通过正确编写单元配置文件来解决其依赖关系。典型的情况是,单元{{ic|A}}要求单元{{ic|B}}在{{ic|A}}启动之前运行。在此情况下,向单元{{ic|A}}配置文件中的 {{ic|[Unit]}} 段添加 {{ic|1=Requires=B}} 和 {{ic|1=After=B}} 即可。若此依赖关系是可选的,可添加 {{ic|1=Wants=B}} 和 {{ic|1=After=B}}。请注意 {{ic|1=Wants=}} 和 {{ic|1=Requires=}} 并不意味着 {{ic|1=After=}},即如果 {{ic|1=After=}} 选项没有制定,这两个单元将被并行启动。<br />
<br />
依赖关系通常被用在服务(service)而不是[[#目标(target)|目标(target)]]上。例如, {{ic|network.target}} 一般会被某个配置网络接口的服务引入,所以,将自定义的单元排在该服务之后即可,因为 {{ic|network.target}} 已经启动。<br />
<br />
=== 启动方式 ===<br />
<br />
编写自定义的service文件时,可以选择几种不同的服务启动方式。启动方式可通过配置文件 {{ic|[Service]}} 段中的 {{ic|1=Type=}} 参数进行设置。具体的参数说明请参阅 {{ic|man systemd.service}} 。<br />
<br />
* {{ic|1=Type=simple}}:systemd认为该服务将立即启动。服务进程不会fork。如果该服务要启动其他服务,不要使用此类型启动,除非该服务是socket激活型。<br />
* {{ic|1=Type=forking}}:systemd认为当该服务进程fork,且父进程退出后服务启动成功。对于常规的守护进程(daemon),除非你确定此启动方式无法满足需求,使用此类型启动即可。使用此启动类型应同时指定 {{ic|1=PIDFile=}},以便systemd能够跟踪服务的主进程。<br />
* {{ic|1=Type=oneshot}}:这一选项适用于只执行一项任务随后退出的服务。你可能需要同时设置 {{ic|1=RemainAfterExit=yes}} 使得 systemd 在服务进程退出之后仍然认为服务处于激活状态。<br />
* {{ic|1=Type=notify}}:与 {{ic|1=Type=simple}} 相同,但约定服务会在就绪后向 systemd 发送一个信号。这一通知的实现由 {{ic|libsystemd-daemon.so}} 提供。<br />
* {{ic|1=Type=dbus}}:若以此方式启动,当指定的 {{ic|BusName}} 出现在DBus系统总线上时,systemd认为服务就绪。<br />
<br />
=== 替换现存单元文件 ===<br />
<br />
目录 {{ic|/etc/systemd/system/}} 中的单元配置文件比 {{ic|/usr/lib/systemd/system/}} 目录下的文件拥有更高优先级。<br />
若需要定制某个单元的启动(使配置不受软件包更新的影响),请将现有的单元配置文件从 {{ic|/usr/lib/}} 复制到 {{ic|/etc/}} 目录下,并在此目录中修改。或者,可以使用 {{ic|.include}} 来解析一个现有的配置文件,并覆盖原选项或添加新选项。例如,若想要某个服务的配置文件中添加额外依赖关系,可以编写:<br />
<br />
{{hc|/etc/systemd/system/<服务名>.service|2=<br />
.include /usr/lib/systemd/system/<服务名>.service<br />
<br />
[Unit]<br />
Requires=<新依赖><br />
After=<新依赖>}}<br />
<br />
然后运行以下命令使更改生效:<br />
<br />
# systemctl reenable <单元><br />
# systemctl restart <单元><br />
<br />
{{小贴士|可以用 {{ic|systemd-delta}} 命令来查看哪些单元文件被覆盖、哪些被修改。}}<br />
<br />
=== 单元配置文件的 vim 语法高亮支持 ===<br />
<br />
可从 [[Official Repositories|官方仓库]] 安装 {{Pkg|vim-systemd}} 软件包,使 unit 配置文件在 [[Vim]] 下支持语法高亮。<br />
<br />
== 目标(target) ==<br />
启动级别(runlevel)是一个旧的概念。现在,systemd 引入了一个和启动级别功能相似又不同的概念——目标(target)。不像数字表示的启动级别,每个''目标''都有名字和独特的功能,并且能同时启用多个。一些''目标''继承其他''目标''的服务,并启动新服务。systemd 提供了一些模仿 sysvinit 启动级别的''目标'',仍可以使用旧的 {{ic|telinit 启动级别}} 命令切换。<br />
<br />
=== 获取当前目标 ===<br />
不要使用 {{ic|runlevel}} 命令了:<br />
$ systemctl list-units --type=target<br />
<br />
=== 创建新目标 ===<br />
在 Fedora 中,启动级别 0、1、3、5、6 都被赋予特定用途,并且都对应一个 systemd 的''目标''。然而,没有什么很好的移植用户定义的启动级别(2、4)的方法。要实现类似功能,可以以原有的启动级别为基础,创建一个新的''目标'' {{ic|/etc/systemd/system/<新目标>}}(可以参考 {{ic|/usr/lib/systemd/system/graphical.target}}),创建 {{ic|/etc/systemd/system/<新目标>.wants}} 目录,向其中加入额外服务的链接(指向 {{ic|/usr/lib/systemd/system/}} 中的单元文件)。<br />
<br />
=== 目标表 ===<br />
{| border="1"<br />
!SysV 启动级别!!Systemd 目标!!注释<br />
|-<br />
| 0 || runlevel0.target, poweroff.target || 中断系统(halt)<br />
|-<br />
| 1, s, single || runlevel1.target, rescue.target || 单用户模式<br />
|-<br />
| 2, 4 || runlevel2.target, runlevel4.target, multi-user.target || 用户自定义启动级别,通常识别为级别3。<br />
|-<br />
| 3 || runlevel3.target, multi-user.target || 多用户,无图形界面。用户可以通过终端或网络登录。<br />
|-<br />
| 5 || runlevel5.target, graphical.target || 多用户,图形界面。继承级别3的服务,并启动图形界面服务。<br />
|-<br />
| 6 || runlevel6.target, reboot.target || 重启<br />
|-<br />
| emergency || emergency.target || 急救模式(Emergency shell)<br />
|-<br />
|}<br />
<br />
=== 切换启动级别/目标 ===<br />
systemd 中,启动级别通过“目标单元”访问。通过如下命令切换:<br />
# systemctl isolate graphical.target<br />
该命令对下次启动无影响。等价于{{ic|telinit 3}} 或 {{ic|telinit 5}}。<br />
<br />
=== 修改默认启动级别/目标 ===<br />
开机启动进的目标是 {{ic|default.target}},默认链接到 {{ic|graphical.target}} (大致相当于原来的启动级别5)。可以通过[[Kernel parameters (简体中文)|内核参数]]更改默认启动级别:<br />
<br />
{{小贴士|可以省略扩展名 {{ic|.target}}。}}<br />
<br />
* {{ic|1=systemd.unit=multi-user.target}} (大致相当于级别3)<br />
* {{ic|1=systemd.unit=rescue.target}} (大致相当于级别1)<br />
<br />
另一个方法是修改 {{ic|default.target}}。可以通过 {{ic|systemctl}} 修改它:<br />
<br />
# systemctl enable multi-user.target<br />
<br />
命令执行情况由 {{ic|systemctl}} 显示:链接 {{ic|/etc/systemd/system/default.target}} 被创建,指向新的默认启动级别。该方法当且仅当目标配置文件中有以下内容时有效:<br />
<br />
[Install]<br />
Alias=default.target<br />
<br />
目前,{{ic|multi-user.target}}、{{ic|graphical.target}} 都包含这段内容。<br />
<br />
== 日志 ==<br />
自版本 38 起,systemd 提供了自己日志系统(logging system),称为 journal. 使用 systemd 日志,无需额外安装日志服务(syslog)。读取日志的命令:<br />
# journalctl<br />
<br />
默认情况下(当 {{ic|Storage&#61;}} 在文件 {{ic|/etc/systemd/journald.conf}} 中被设置为 {{ic|auto}}),日志记录将被写入 {{ic|/var/log/journal/}}。该目录是 {{pkg|systemd}} 软件包的一部分。若被删除,systemd '''不会'''自动创建它,直到下次升级软件包时重建该目录。如果该目录缺失,systemd 会将日志记录写入 {{ic|/run/systemd/journal}}。这意味着,系统重启后日志将丢失。<br />
<br />
=== 过滤输出 ===<br />
<br />
{{ic|journalctl}}可以根据特定字段过滤输出,例如:<br />
<br />
显示本次启动后的所有日志:<br />
<br />
# journalctl -b<br />
<br />
不过,一般大家更关心的不是本次启动后的日志,而是上次启动时的(例如,刚刚系统崩溃了)。目前还没有这项功能,正在 [http://comments.gmane.org/gmane.comp.sysutils.systemd.devel/6608 systemd-devel@lists.freedesktop.org] 讨论中。<br />
<br />
目前的折中方案是:<br />
<br />
# journalctl --since=today | tac | sed -n '/-- Reboot --/{n;:r;/-- Reboot --/q;p;n;b r}' | tac<br />
<br />
以上命令输出本日内的所有启动信息。但要注意,如果日志很多,该命令执行时间会比较漫长。<br />
<br />
动态跟踪最新信息:<br />
# journalctl -f<br />
<br />
显示特定程序的所有消息:<br />
{{bc|# journalctl /usr/lib/systemd/systemd}}<br />
<br />
显示特定进程的所有消息:<br />
{{bc|1=# journalctl _PID=1}}<br />
<br />
显示指定单元的所有消息:<br />
# journalctl -u netcfg<br />
<br />
详情参阅{{ic|man journalctl}}、{{ic|man systemd.journal-fields}},以及 Lennert 的这篇[http://0pointer.de/blog/projects/journalctl.html 博文]。<br />
<br />
=== 日志大小限制 ===<br />
<br />
如果按上面的操作保留日志的话,默认日志最大限制为所在文件系统容量的 10%,即:如果 {{ic|/var/log/journal}} 储存在 50GiB 的根分区中,那么日志最多存储 5GiB 数据。可以修改 {{ic|/etc/systemd/journald.conf}} 中的 {{ic|SystemMaxUse}} 来指定该最大限制。如限制日志最大 50MiB:<br />
<br />
SystemMaxUse=50M<br />
<br />
详情参见 {{ic|man journald.conf}}.<br />
<br />
=== 配合 syslog 使用 ===<br />
systemd 提供了 socket {{ic|/run/systemd/journal/syslog}},以兼容传统日志服务。所有系统信息都会被传入。要使传统日志服务工作,需要让服务链接该 socket,而非 {{ic|/dev/log}}([http://lwn.net/Articles/474968/ 官方说明])。Arch 软件仓库中的 {{pkg|syslog-ng}} 已经包含了需要的配置。<br />
<br />
设置开机启动 syslog-ng:<br />
# systemctl enable syslog-ng<br />
<br />
[http://0pointer.de/blog/projects/ 这里]有一份很不错的 {{ic|journalctl}} 指南。<br />
<br />
== 优化 ==<br />
<br />
参阅:[[Improve Boot Performance (简体中文)]]。<br />
<br />
=== 启动过程分析 ===<br />
systemd 提供了一个分析启动过程的工具 —— {{ic|systemd-analyze}}。可以用它看看哪些单元拖慢了开机过程,并据此进行优化。安装 {{Pkg|python2-dbus}}、{{Pkg|python2-cairo}} 软件包后该工具才能工作。<br />
<br />
查看开机过程在内核/用户空间消耗的时间:<br />
$ systemd-analyze<br />
{{小贴士|如果在 {{ic|/etc/mkinitcpio.conf}} 的 {{ic|HOOKS}} 数组添加 {{ic|timestamp}},并重新生成启动内存镜像({{ic|mkinitcpio -p linux}}),{{ic|systemd-analyze}} 还可以显示处理该启动镜像花费的时间。}}<br />
<br />
按照耗费时间顺序,输出启动每个单元耗费的时间:<br />
$ systemd-analyze blame<br />
<br />
生成类似于 [[Bootchart (简体中文)|bootchart]] 的开机过程图表:<br />
$ systemd-analyze plot > plot.svg<br />
<br />
==== 使用 systemd-bootchart ====<br />
<br />
自2012年10月17日,bootchart 工具已经合并进 systemd 中,使用方法和原来的 bootchart 大同小异,添加下列内容到内核参数即可:<br />
<br />
initcall_debug printk.time=y init=/usr/lib/systemd/systemd-bootchart<br />
<br />
==== 使用 bootchart2 ====<br />
<br />
由于没有办法在内核参数设置两个 init,所以不能使用源里的 bootchart。不过,[[AUR (简体中文)|AUR]] 软件包 {{AUR|bootchart2}} 提供了一个的 systemd 服务,这样就可以和 systemd 一起使用。安装后启用即可:<br />
{{bc|# systemctl enable bootchart.service}}<br />
详情参阅 [https://github.com/mmeeks/bootchart bootchart 文档]。<br />
<br />
=== 预读 ===<br />
systemd 自己实现了一个 readahead,可以用来提高开机效率。不过,效果会因内核版本和硬件情况而不同(极少数情况下还会变慢)。开启 readahead:<br />
<br />
# systemctl enable systemd-readahead-collect.service systemd-readahead-replay.service<br />
<br />
要知道,readahead 的超级牛力只有在重启几次后才会显现。<br />
<br />
== 疑难解答 ==<br />
<br />
=== 关机/重启十分缓慢 ===<br />
<br />
如果关机特别慢(甚至跟死机了一样),很可能是某个拒不退出的服务在作怪。systemd 会等待一段时间,然后再尝试杀死它。请阅读[http://freedesktop.org/wiki/Software/systemd/Debugging#Shutdown_Completes_Eventually 这篇文章],确认你是否是该问题受害者。<br />
<br />
=== 短时进程无日志记录 ===<br />
<br />
若 {{ic|journalctl -u foounit.service}} 没有显示某个短时进程的任何输出,那么改用 PID 试试。例如,若 systemd-modules-load.service 执行失败,那么先用 {{ic|systemctl status systemd-modules-load}} 查询其 PID(比如是123),然后检索该 PID 相关的日志 {{ic|journalctl -b _PID&#61;123}}。运行时进程的日志元数据(诸如 _SYSTEMD_UNIT 和 _COMM)被乱序收集在 {{ic|/proc}} 目录。要修复该问题,必须修改内核,使其通过套接字连接来提供上述数据,该过程类似于 SCM_CREDENTIALS。<br />
<br />
== 相关资源 ==<br />
<br />
*[http://www.freedesktop.org/wiki/Software/systemd 官方网站]<br />
*[http://0pointer.de/public/systemd-man/ man 手册页]<br />
*[http://freedesktop.org/wiki/Software/systemd/Optimizations systemd 优化]<br />
*[http://www.freedesktop.org/wiki/Software/systemd/FrequentlyAskedQuestions 常见问题 FAQ]<br />
*[http://www.freedesktop.org/wiki/Software/systemd/TipsAndTricks 小技巧]<br />
*[http://0pointer.de/public/systemd-ebook-psankar.pdf systemd for Administrators (PDF)]<br />
*[http://fedoraproject.org/wiki/Systemd Fedora 项目对 systemd 的介绍]<br />
*[http://fedoraproject.org/wiki/How_to_debug_Systemd_problems 如何调试 systemd 故障]<br />
*''The H Open'' 杂志上的[http://www.h-online.com/open/features/Control-Centre-The-systemd-Linux-init-system-1565543.html 两][http://www.h-online.com/open/features/Booting-up-Tools-and-tips-for-systemd-1570630.html 篇]科普文章<br />
*[http://0pointer.de/blog/projects/systemd.html Lennart 的博文]<br />
*[http://0pointer.de/blog/projects/systemd-update.html 更新报告]<br />
*[http://0pointer.de/blog/projects/systemd-update-2.html 更新报告2]<br />
*[http://0pointer.de/blog/projects/systemd-update-3.html 更新报告3]<br />
*[http://0pointer.de/blog/projects/why.html 最新动态]<br />
*[https://fedoraproject.org/wiki/SysVinit_to_Systemd_Cheatsheet/zh Fedora Wiki 上的 SysVinit 和 systemd 命令对比表]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Advanced_Linux_Sound_Architecture_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=256550
Advanced Linux Sound Architecture (简体中文)
2013-05-10T12:04:54Z
<p>Cuihao: /* HDMI Output Does Not Work */</p>
<hr />
<div>[[Category:简体中文]]<br />
[[Category:Sound (简体中文)]]<br />
[[Category:Audio/Video (简体中文)]]<br />
[[cs:Advanced Linux Sound Architecture]]<br />
[[de:Alsa]]<br />
[[es:Advanced Linux Sound Architecture]]<br />
[[fa:ALSA]]<br />
[[fr:Alsa]]<br />
[[he:Advanced Linux Sound Architecture]]<br />
[[it:Advanced Linux Sound Architecture]]<br />
[[ja:Advanced Linux Sound Architecture]]<br />
[[nl:Advanced Linux Sound Architecture]]<br />
[[pt:Advanced Linux Sound Architecture]]<br />
[[ru:Advanced Linux Sound Architecture]]<br />
[[sk:Advanced Linux Sound Architecture]]<br />
[[th:Advanced Linux Sound Architecture]]<br />
[[tr:Gelişmiş_ses_mimarisi_(ALSA)]]<br />
[[en:Advanced Linux Sound Architecture]]<br />
<br />
{{Article summary start}}<br />
{{Article summary text|ALSA 的使用、配置及疑难解答。}}<br />
{{Article summary heading|相关页面}}<br />
{{Article summary wiki|Sound system (简体中文)}}<br />
{{Article summary wiki|Disable PC Speaker Beep (简体中文)}}<br />
{{Article summary end}}<br />
{{TranslationStatus (简体中文)|Advanced_Linux_Sound_Architecture|2013-04-25|254584}}<br />
<br />
[[Wikipedia:zh:ALSA|高级 Linux 声音体系]](Advanced Linux Sound Architecture,'''ALSA''')是Linux中提供声音设备驱动的内核组件,用来代替原来的开放声音系统(Open Sound System,OSSv3)。除了声音设备驱动,'''ALSA'''还包含一个用户空间的函数库,以方便开发者通过高级API使用驱动功能,而不必直接与内核驱动交互。<br />
<br />
{{注意|关于另一种声音体系,请阅读[[Open Sound System (简体中文)|开放声音系统(OSS)]]的页面。}}<br />
<br />
==安装==<br />
Arch 默认的内核已经通过一套模块提供了 ALSA,不必特别安装。<br />
<br />
[[udev (简体中文)|udev]]会在系统启动时自动检测硬件,并加载相应的声音设备驱动模块。这时,你的声卡已经可以工作了,只是所有声道默认都被设置成静音了。<br />
<br />
在本地登录(通过虚拟终端或登录管理器)的用户,都有权限播放音频并调整音量。要让远程登录的用户拥有这些权限,必须把用户[[Users and Groups (简体中文)#用户组管理|加入]] {{ic|audio}} 用户组。该组的成员可以直接访问声音设备,会导致某些程序独占音频输出(破坏软件混音),还可能影响用户快速切换和拖机(multiseat)。因此,除非真的有某些[https://wiki.ubuntu.com/Audio/TheAudioGroup 特殊需求],'''不建议'''把用户加入 {{ic|audio}} 用户组。<br />
<br />
===用户空间工具===<br />
从 [[Official Repositories (简体中文)|官方仓库]]:<br />
* [[pacman (简体中文)|安装]] {{Pkg|alsa-utils}} 软件包,其中的 {{ic|alsamixer}} 工具允许用户在控制台或终端中配置声音设备。<br />
* 安装 {{Pkg|alsa-oss}} 软件包,如果想让 OSS 应用程序与dmix(软件混音)协同工作。 <br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。}}<br />
*安装 {{Pkg|alsa-plugins}} 软件包,如果需要 [[#高质量重采样|高质量重采样]]、[[#Upmixing/Downmixing|软件模拟环绕立体声]]和其他高级特性。<br />
<br />
==解除各声道的静音==<br />
目前版本的 ALSA 安装后,所有声道'''默认是静音的''',必须手动解除。<br />
<br />
使用 {{ic|alsamixer}} 的 ncurses 界面,配置十分简单:<br />
<br />
$ alsamixer<br />
<br />
此外,还可以在命令行下使用 {{ic|amixer}}:<br />
<br />
$ amixer sset Master unmute<br />
<br />
在 {{ic|alsamixer}} 中,下方标有 {{ic|MM}} 的声道是静音的,而标有 {{ic|00}} 的通道已经启用。<br />
<br />
使用左右方向键,选中 {{ic|Master}} 和 {{ic|PCM}} 声道。按下 {{keypress|m}} 键解除静音。使用上方向键增加音量,直到增益值为{{ic|0}}。该值显示在左上方 {{ic|Item:}} 字段后。过高的增益值会导致声音失真。<br />
<br />
要想得到完整的 5.1 或 7.1 环绕立体声,还得解除 Front、Surround、Center、LFE (subwoofer) 和 Side 这些声道的静音(上述名称是 Intel HD Audio 声卡使用的声道名,可能因设备不同而有所差异)。注意,仅有这些设置,系统不会自动将立体声源(多数音乐)提升(upmix)成环绕立体声。如果需要这些功能,请阅读[[#Upmixing/Downmixing]]。<br />
<br />
要启用麦克风,切换至 Capture 选项卡,按下 {{keypress|F4}},按下 {{keypress|空格}} 启用其中一个声道即可。<br />
<br />
按下 {{Keypress|Esc}} 键退出 alsamixer。<br />
<br />
{{注意|<br />
* 有一些声卡,需要关闭数字输出或将其调成静音,才能输出模拟音频信号。对于 Soundblaster Audigy LS 声卡,需要把 IEC958 通道设成静音。 <br />
* 有些机器(如 Thinkpad T61),有一个 Speaker 通道,必须按照上述方法打开调整。<br />
* 有些机器(如 Dell E6400),可能还需要把 {{ic|Front}} 和 {{ic|Headphone}} 声道打开调整。}}<br />
<br />
接下来,测试声卡是否工作:<br />
<br />
$ speaker-test -c 2<br />
<br />
根据扬声器的实际情况,调整 -c 后的数值。对于 7.1 声道,这个数字是 8:<br />
<br />
$ speaker-test -c 8<br />
<br />
如果没有正常工作,请继续阅读 [[#配置]] 以及 [[#疑难解答]] 部分。<br />
<br />
{{pkg|alsa-utils}} 软件包提供了两个配置 alsa 的服务:{{ic|alsa-restore.service}} 和 {{ic|alsa-store.service}}。它们分别在开机和关机时自动运行。<br />
<br />
==配置==<br />
=== VirtualBox 虚拟机无声音 ===<br />
<br />
如果使用 VirtualBox 时碰上了麻烦,请尝试以下命令:<br />
<br />
{{hc|$ alsactl init|2=<nowiki><br />
<br />
Found hardware: "ICH" "SigmaTel STAC9700,83,84" "AC97a:83847600" "0x8086" "0x0000"<br />
Hardware is initialized using a generic method<br />
</nowiki><br />
}}<br />
<br />
如果还是不行,请检查所使用的音频软件是否激活了 ALSA 输出。<br />
<br />
===设置默认声卡===<br />
<br />
如果发现开机时声卡次序会发生变化,可以在通过 {{ic|/etc/modprobe.d}} 中的 {{ic|.conf}} 文件(比如 {{ic|/etc/modprobe.d/alsa-base.conf}})手动设置次序。<br />
比如,要让 mia 声卡成为 #0、Intel HDA 声卡成为 #1:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<br />
options snd slots=snd_mia,snd_hda_intel<br />
options snd_mia index=0<br />
options snd_hda_intel index=1<br />
}}<br />
<br />
使用 {{ic|<nowiki>lsmod | grep snd</nowiki>}} 获取设备列表。<br />
<br />
snd_mia 和 snd_hda_intel 分别是两个声卡的内核驱动模块。该配置仅适用于同时装有 mia 声卡和使用 snd_hda_intel 驱动的声卡(通常是板载声卡)的情况。<br />
<br />
如果使用 -2 的 index 值,ALSA 就不会将对应的设备作为主声卡使用。Linux Mint 和 Ubuntu 等发行版使用了以下配置,避免 USB 声卡和其他“非主流”设备变成 index 为 0 的主声卡:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<br />
options bt87x index=-2<br />
options cx88_alsa index=-2<br />
options saa7134-alsa index=-2<br />
options snd-atiixp-modem index=-2<br />
options snd-intel8x0m index=-2<br />
options snd-via82xx-modem index=-2<br />
options snd-usb-audio index=-2<br />
options snd-usb-caiaq index=-2<br />
options snd-usb-ua101 index=-2<br />
options snd-usb-us122l index=-2<br />
options snd-usb-usx2y index=-2<br />
# Keep snd-pcsp from being loaded as first soundcard<br />
options snd-pcsp index=-2<br />
# Keep snd-usb-audio from beeing loaded as first soundcard<br />
options snd-usb-audio index=-2<br />
}}<br />
<br />
以上配置会在系统重启后生效。<br />
<br />
{{Out of date|据英文 Wiki 用户反应,{{ic|/etc/asound.conf}} 这个配置文件默认是不存在的,下列操作可能有误。}}<br />
<br />
首先,运行 {{ic|aplay -l}},获取声卡的声卡ID和设备ID:<br />
<br />
{{hc|$ aplay -l|2=<nowiki><br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: Intel [HDA Intel], device 0: CONEXANT Analog [CONEXANT Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: Intel [HDA Intel], device 1: Conexant Digital [Conexant Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 1: JamLab [JamLab], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 2: Audio [Altec Lansing XT1 - USB Audio], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
</nowiki>}}<br />
<br />
拿输出中的最后一项来说,这个声卡的声卡ID为2,设备ID为0。若要把这块声卡作为默认声卡,可以把下列配置添加到系统级别的 {{ic|/etc/asound.conf}} 或用户级别的 {{ic|~/.asoundrc}} 文件。如果文件不存在,可以手动创建。其中的各个ID,请根据实际情况调整:<br />
{{bc|<br />
defaults.pcm.card 2<br />
defaults.pcm.device 0<br />
defaults.ctl.card 2<br />
}}<br />
“pcm”选项决定用来播放音频的设备,而“ctl”选项决定那个声卡能够由控制工具(如 alsamixer)使用。<br />
<br />
上述配置在重启音频程序(如 mplayer)后立即生效。<br />
<br />
===确认所有声音模块都已经加载===<br />
<br />
一般 udev 都会自动识别出声卡。使用以下命令确认:<br />
<br />
{{hc|<nowiki>$ lsmod | grep '^snd' | column -t</nowiki>|<nowiki><br />
snd_hda_codec_hdmi 22378 4<br />
snd_hda_codec_realtek 294191 1<br />
snd_hda_intel 21738 1<br />
snd_hda_codec 73739 3 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel<br />
snd_hwdep 6134 1 snd_hda_codec<br />
snd_pcm 71032 3 snd_hda_codec_hdmi,snd_hda_intel,snd_hda_codec<br />
snd_timer 18992 1 snd_pcm<br />
snd 55132 9 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel,snd_hda_codec,snd_hwdep,snd_pcm,snd_timer<br />
snd_page_alloc 7017 2 snd_hda_intel,snd_pcm<br />
</nowiki>}}<br />
<br />
如果你的输出和上面类似,那就说明声卡已经被正确识别。<br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。如有需要,请[[Kernel_modules_(简体中文)#装入|手动加载]]。}}<br />
<br />
还可以检查一下 {{ic|/dev/snd/}} 目录,看看是否有这些设备文件:<br />
<br />
{{hc|$ ls -l /dev/snd|2=<nowiki><br />
total 0<br />
crw-rw---- 1 root audio 116, 0 Apr 8 14:17 controlC0<br />
crw-rw---- 1 root audio 116, 32 Apr 8 14:17 controlC1<br />
crw-rw---- 1 root audio 116, 24 Apr 8 14:17 pcmC0D0c<br />
crw-rw---- 1 root audio 116, 16 Apr 8 14:17 pcmC0D0p<br />
crw-rw---- 1 root audio 116, 25 Apr 8 14:17 pcmC0D1c<br />
crw-rw---- 1 root audio 116, 56 Apr 8 14:17 pcmC1D0c<br />
crw-rw---- 1 root audio 116, 48 Apr 8 14:17 pcmC1D0p<br />
crw-rw---- 1 root audio 116, 1 Apr 8 14:17 seq<br />
crw-rw---- 1 root audio 116, 33 Apr 8 14:17 timer<br />
</nowiki>}}<br />
<br />
{{注意|在 IRC 或论坛寻求这方面帮助时,别忘了贴出上面几个命令的输出。}}<br />
<br />
如果你的输出跟上面类似,或至少有 '''controlC0''' 和 '''pcmC0D0p''',那么声卡就已经正常加载了。<br />
<br />
如果出现问题,声卡模块没有正确加载,那么请尝试手动加载模块:<br />
<br />
* 确定声卡对应的驱动模块:[http://www.alsa-project.org/main/index.php/Matrix:Main ALSA Soundcard Matrix]。这些模块都会有一个”snd-“前缀(例如:{{ic|snd-via82xx}})。<br />
* [[Kernel_modules_(简体中文)#装入|加载模块]]。<br />
* 检查 {{ic|/dev/snd}} 目录中的设备文件(参见上文);或者,检查 {{ic|alsamixer}} 或 {{ic|amixer}} 的输出是否正确。<br />
* 修改配置,使 {{ic|snd-<模块名>}} 和 {{ic|snd-pcm-oss}} 模块[[Kernel_modules_(简体中文)#装入|开机自动加载]]。<br />
<br />
===启用 SPDIF 输出===<br />
<br />
(由 Gentoo 论坛的 gralves 提供)<br />
* 在 GNOME 音量控制的选项(Options)标签中,把 IEC958 改成 PCM。该选项可以在首选项中启用。<br />
* 不使用 GNOME 音量控制工具的话:<br />
** 编辑 {{ic|/var/lib/alsa/asound.state}}。该文件是 alsasound 保存混音器设置的地方。<br />
** 找到这一行:“IEC958 Playback Switch”。该行上下应该有一行内容为 {{ic|value:false}},将其改为 {{ic|value:true}}。<br />
** 再找到这一行:”IEC958 Playback AC97-SPSA“。把对应的值改为 0。<br />
** 重启 ALSA。<br />
<br />
{{Out of date|rc.local 在 systemd 中已经弃用,以下方法可能不再适用。}}<br />
<br />
还有另外一个开机自动启用 SPDIF 输出的方法(在 SoundBlaster Audigy 声卡上测试过):<br />
<br />
* 在 {{ic|/etc/rc.local}} 中添加如下内容:<br />
# Use COAX-digital output<br />
amixer set 'IEC958 Optical' 100 unmute<br />
amixer set 'Audigy Analog/Digital Output Jack' on<br />
<br />
使用以下命令,获取你的声卡的数字输出的名称:<br />
$ amixer scontrols<br />
<br />
===系统级均衡器===<br />
====使用 AlsaEqual(包含界面)====<br />
从 [[AUR (简体中文)|AUR]] 安装 {{AUR|alsaequal}}。<br />
<br />
{{注意|如果使用的是64位系统,而又安装了32位的 Flash 插件,这里的设置会导致 Flash 无声。如果发生了这种情况,请禁用 alsaequal 或编译32位版本的 alsaequal。}}<br />
<br />
安装后,把下列内容添加到 ALSA 配置文件({{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}}):<br />
<br />
ctl.equal {<br />
type equal;<br />
}<br />
<br />
pcm.plugequal {<br />
type equal;<br />
# Modify the line below if you do not<br />
#slave.pcm "plughw:0,0";<br />
#by default we want to play from more sources at time:<br />
slave.pcm "plug:dmix";<br />
}<br />
#pcm.equal {<br />
# If you do not want the equalizer to be your<br />
# default soundcard comment the following<br />
# line and uncomment the above line. (You can<br />
# choose it as the output device by addressing<br />
# it with specific apps,eg mpg123 -a equal 06.Back_In_Black.mp3)<br />
pcm.!default {<br />
type plug;<br />
slave.pcm plugequal;<br />
}<br />
<br />
设置好后,切换均衡器:<br />
$ alsamixer -D equal<br />
<br />
需要注意,每个用户的 alsaequal 配置文件都是独立的,位于 {{ic|~/.alsaequal.bin}}。因此,如果想在 [[mpd]] 之类的以独立用户身份运行的程序中使用 AlsaEqual,就得像下面这样:<br />
# su mpd -c 'alsamixer -D equal'<br />
或者,在其他用户的主目录下创建符号链接,指向你的 {{ic|.alsaequal.bin}}……<br />
<br />
=====管理 AlsaEqual 配置=====<br />
从 [http://xyne.archlinux.ca/repos/ Xyne 的软件仓库] 或 [https://aur.archlinux.org/packages.php?ID=62420 AUR] 安装 [http://xyne.archlinux.ca/projects/alsaequal-mgr/ alsaequal-mgr]。<br />
<br />
同上,配置均衡器: <br />
$alsamixer -D equal<br />
<br />
如果对效果感到满意,将其命名(如“foo”)并保存该配置:<br />
$alsaequal-mgr save foo<br />
<br />
重新加载“foo”中的配置:<br />
$alsaequal-mgr load foo<br />
<br />
这样,就可以为游戏、电影、不同音乐流派、网络电话等等创建不同的均衡器配置,以便随时切换。<br />
<br />
参见[http://xyne.archlinux.ca/projects/alsaequal-mgr/ 项目主页]和程序的帮助信息,了解更多设置。<br />
<br />
====使用 mbeq====<br />
{{注意|该软件用到了一个 ladspa 插件,可能导致播放音频时 CPU 占用率偏高。In addition, this was made with stereophonic sound (e.g. headphones) in mind.}}<br />
<br />
安装 {{Pkg|alsa-plugins}}、{{Pkg|ladspa}} 和 {{Pkg|swh-plugins}} 软件包。<br />
<br />
* 在 {{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}} 中添加如下内容:<br />
<br />
{{hc|/etc/asound.conf|2=<nowiki>pcm.eq {<br />
type ladspa<br />
<br />
# The output from the EQ can either go direct to a hardware device<br />
# (if you have a hardware mixer, e.g. SBLive/Audigy) or it can go<br />
# to the software mixer shown here.<br />
#slave.pcm "plughw:0,0"<br />
slave.pcm "plug:dmix"<br />
<br />
# Sometimes you may need to specify the path to the plugins,<br />
# especially if you have just installed them. Once you have logged<br />
# out/restarted this should not be necessary, but if you get errors<br />
# about being unable to find plugins, try uncommenting this.<br />
#path "/usr/lib/ladspa"<br />
<br />
plugins [<br />
{<br />
label mbeq<br />
id 1197<br />
input {<br />
#this setting is here by example, edit to your own taste<br />
#bands: 50hz, 100hz, 156hz, 220hz, 311hz, 440hz, 622hz, 880hz, 1250hz, 1750hz, 25000hz,<br />
#50000hz, 10000hz, 20000hz<br />
controls [ -5 -5 -5 -5 -5 -10 -20 -15 -10 -10 -10 -10 -10 -3 -2 ]<br />
}<br />
}<br />
]<br />
}<br />
<br />
# Redirect the default device to go via the EQ - you may want to do<br />
# this last, once you are sure everything is working. Otherwise all<br />
# your audio programs will break/crash if something has gone wrong.<br />
<br />
pcm.!default {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
<br />
# Redirect the OSS emulation through the EQ too (when programs are running through "aoss")<br />
<br />
pcm.dsp0 {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
</nowiki>}}<br />
<br />
* 完成。若有问题,欢迎前往论坛咨询。<br />
<br />
==高质量重采样==<br />
启用软件混音时,ALSA 会强制把所有音频重采样到相同的频率(如果系统支持,默认是 48000)。dmix 的重采样算法很糟糕,会导致显著的质量损失。<br />
<br />
安装 {{pkg|alsa-plugins}} 和 {{pkg|libsamplerate}}。<br />
<br />
将默认的采样率转换器更改为 libsamplerate:<br />
<br />
{{hc|/etc/asound.conf|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
或:<br />
<br />
{{hc|~/.asoundrc|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
'''samplerate_best''' 输出的声音质量最佳,但需要耗费大量CPU资源进行实时重采样。还有其他的算法可供选择(如 '''samplerate''' 等),但比起默认的算法,这些算法的改进并不明显。<br />
<br />
{{警告|某些系统中,启用 samplerate_best 可能导致 Flash 播放器无声音。}}<br />
<br />
==上混和下混==<br />
{{注意|由于未找到 upmix 和 downmix 的常用翻译,译者暂时采用字面翻译(上混、下混)。所谓“上混”,即将声道较少的立体声(如常见的双声道立体声)信号,通过软件或硬件处理,模拟为声道较多的立体声(如5.1环绕立体声)信号。反之即为“下混”。}}<br />
<br />
===上混(upmixing)===<br />
播放双声道立体声音源(如音乐)时,利用上混,即可充分利用 5.1 或 7.1 环绕立体声的所有声道。以前,上混很麻烦,还经常出错。但如今就方便多了,只需要安装一些插件即可。这些插件由 {{Pkg|alsa-plugins}} 软件包提供。<br />
<br />
安装后,在配置文件({{ic|/etc/asound.conf}} 或 {{ic|~/.asoundrc}})中添加如下内容:<br />
pcm.upmix71 {<br />
type upmix<br />
slave.pcm "surround71"<br />
delay 15<br />
channels 8<br />
}<br />
<br />
该范例适用于7.1声道上混。类比该范例,同样能设置5.1或4.0声道上混。<br />
<br />
该设置添加了一个新的用于上混的 pcm。如果希望所有音频源都通过该 pcm 输出,在上述设置后添加如下配置即可:<br />
<br />
pcm.!default "plug:upmix71"<br />
<br />
插件默认配置就允许多个音频源同时通过它输出,一般无需额外配置。如果不行,就得向下面这样为上混配置 dmixer:<br />
pcm.dmix6 {<br />
type asym<br />
playback.pcm {<br />
type dmix<br />
ipc_key 567829<br />
slave {<br />
pcm "hw:0,0"<br />
channels 6<br />
}<br />
}<br />
}<br />
<br />
注意要用 dmix6 而非 surround71。<br />
<br />
如果遇到声音卡顿或混乱,可能需要增加 buffer_size(比如增加到 32768),或使用[[#高质量重采样|高质量重采样]]。<br />
<br />
===下混(downmixing)===<br />
有时也会用到下混,比如在只支持双声道立体声的电脑上观看5.1声道的电影时。这一功能由 ALSA 插件 vdownmix 实现(同样在 {{Pkg|alsa-plugins}} 软件包里)。<br />
<br />
在配置文件中添加如下内容:<br />
<br />
pcm.!surround51 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
pcm.!surround40 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
<br />
==混音==<br />
===软件混音(dmix)===<br />
<br />
{{注意|在 ALSA 1.0.9rc2 及以上版本中,如果声卡不支持硬件混音,模拟输出将默认使用 dmix 混音,无需额外设置。}}<br />
<br />
如果软件混音未正常启用,那么可以手动创建 {{ic|~/.asoundrc}},加入以下内容:<br />
<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
<br />
这样就启用了软件混音,多个程序能够同时使用声卡。<br />
<br />
至于 S/PDIF 之类的数字音频输出,目前的 ALSA 还不会默认启用 dmix。因此,必须像上面这样手动启用。<br />
<br />
常见问题的解答,参见[[#疑难解答]]部分。<br />
<br />
===硬件混音===<br />
====支持====<br />
如果声卡支持,一般无需任何配置即可启用硬件混音。绝大多数板载声卡都不支持硬件混音,必须得使用软件混音(见上文)。很多独立声卡都支持硬件混音,Linux 下支持较好的有:<br />
<br />
* Creative SoundBlaster Live! (5.1 型号)<br />
* Creative SoundBlaster Audigy (部分型号)<br />
* Creative SoundBlaster Audidy 2 (ZS 系列型号)<br />
* Creative SoundBlaster Audigy 4 (Pro 系列型号)<br />
<br />
{{注意|上述声卡的低端版本(Audigy SE、Audigy 2 NX、SoundBlaster Live! 24bit 以及 SoundBlaster Live! 7.1)使用了不同的芯片,'''不支持'''硬件混音。}}<br />
<br />
{{注意|板载 VIA8237 芯片支持4路硬件混音,但在某些主板上只能使用3路(第4路无声),甚至根本无法使用。即使能正常工作,质量也比不上其他混音方法。}}<br />
<br />
====问题修复====<br />
如果你的声卡是 Intel Corporation 82801I (ICH9 Family) HD Audio Controller (rev 02),且使用了64位系统,需要执行以下命令才能在 Enemy Territory 中听到声音:<br />
echo "et.x86 0 0 direct" > /proc/asound/card0/pcm0p/oss<br />
echo "et.x86 0 0 disable" > /proc/asound/card0/pcm0c/oss<br />
<br />
==疑难解答==<br />
===使用CPU频率动态调整时,音频发生跳跃===<br />
使用 {{ic|ondemand}} 或 {{ic|conservative}} 频率策略、启用CPU频率动态调整时,某些声卡和某些 ALSA 驱动会出现声音跳跃的情况。目前,唯一的解决方案是使用固定的频率,如 {{ic|performance}} 性能策略。<br />
<br />
详情参见 [[CPU Frequency Scaling (简体中文)]]。<br />
<br />
===同时只能有一个用户播放音频===<br />
有时会出现一个用户独占软件混音器的情况。多数情况下这没什么影响,但在使用 [[mpd]] 这样以独立用户身份运行的程序时,就会出现问题。当 mpd 运行时,用户自己就无法播放其他音频了。一种解决方法是,以正在使用的用户身份运行 mpd。另一种方法是,在 ALSA 配置文件的 {{ic|pcm.dmixer}} 部分添加 {{ic|ipc_key_add_uid 0}} 禁止独占。以下是 {{ic|asound.conf}} 示例的片段,省略的部分无需修改:<br />
<br />
...<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
slave {<br />
...<br />
<br />
===无法同时播放多个音频===<br />
如果安装了 [[PulseAudio (简体中文)|PulseAudio]](可能作为 [[GNOME (简体中文)|GNOME]] 的依赖被安装),可能出现无法同步播放的问题,因为 PulseAudio 的默认配置是“独占”声卡。ALSA 的用户可能对自己的配置已经很满意、不希望使用 [[PulseAudio (简体中文)|PulseAudio]]。解决方案是编辑 {{ic|/etc/asound.conf}} 并注释掉以下内容:<br />
# Use PulseAudio by default<br />
#pcm.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
# hint {<br />
# show on<br />
# description "Default ALSA Output (currently PulseAudio Sound Server)"<br />
# }<br />
#}<br />
<br />
可能还需要注释掉下面的内容:<br />
#ctl.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
#}<br />
<br />
比起完全卸载 [[PulseAudio (简体中文)|PulseAudio]],这大概是个更简单的方法。<br />
<br />
以下是 {{ic|/etc/asound.conf}} 配置文件的一个示例:<br />
{{bc|<nowiki>pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
}<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}</nowiki><br />
}}<br />
<br />
{{注意|本 {{ic|/etc/asound.conf}} 本来是配合 [[MPD]] 全局配置使用的。参见[[#同时只能有一个用户播放音频|该节]]关于多用户的内容。}}<br />
<br />
===有时候开机无声===<br />
如果会随机地出现开机无声的问题,原因可能是多声卡的顺序发生了变化。如果是这种情况,请参照[[#设置默认声卡]]进行设置。<br />
<br />
对于 mpd 用户,上述配置无效,请参考[http://mpd.wikia.com/wiki/Configuration#ALSA_MPD_software_volume_control 该文]设置。<br />
<br />
===特定程序的问题===<br />
对于不使用系统声音配置的程序,如 XMMS 和 Mplayer,必须特别设置以应用 ALSA 配置。<br />
<br />
对于 mplayer,将以下内容添加到 {{ic|~/.mplayer/config}} (或系统全局配置文件 {{ic|/etc/mplayer/mplayer.conf}}):<br />
ao=alsa<br />
<br />
对于 XMMS 和 Beep Media Player,打开选项窗口,要把声音驱动设置为 ALSA 而不是 OSS。<br />
<br />
XMMS 的设置方法:<br />
* 启动 XMMS<br />
** 选项 -> 首选项<br />
** 选择 Alsa 输出插件<br />
<br />
对于未提供 ALSA 输出的程序,可以使用 alsa-oss 软件包中的 aoss 工具启动。使用方法是:在要运行的程序的命令行前加上 {{ic|aoss}}。例如:<br />
aoss realplay<br />
<br />
pcm.!default{ ... } 貌似已经不管用了。但下面的设置有效:<br />
pcm.default pcm.dmixer<br />
<br />
===特定型号的配置===<br />
ALSA 会通过 BIOS 检测[http://www.kernel.org/doc/Documentation/sound/alsa/HD-Audio-Models.txt 声卡型号],少数情况下也可能无法正确识别。声卡芯片类型可以在 {{ic|alsamixer}} 界面上找到(如 ALC662),而型号则要设置在 {{ic|/etc/modprobe.d/modprobe.conf}} 或 {{ic|/etc/modprobe.d/sound.conf}} 文件中:<br />
<br />
options snd-hda-intel model=<型号><br />
<br />
具体设置因声卡型号而异。多数情况下不需要特地这样做。如果想知道具体声卡型号的设置,请查阅 [http://bugtrack.alsa-project.org/main/index.php/Matrix:Main Alsa Soundcard List],找到你的声卡型号,点击“Details”,然后查看“Setting up modprobe...”小节,将其中的内容插入 {{ic|/etc/modprobe.d/modprobe.conf}} 文件即可。例如,Intel AC97 的配置如下:<br />
<br />
{{bc|# ALSA portion<br />
alias char-major-116 snd<br />
alias snd-card-0 snd-intel8x0<br />
# module options should go here<br />
<br />
# OSS/Free portion<br />
alias char-major-14 soundcore<br />
alias sound-slot-0 snd-card-0<br />
<br />
# card #1<br />
alias sound-service-0-0 snd-mixer-oss<br />
alias sound-service-0-1 snd-seq-oss<br />
alias sound-service-0-3 snd-pcm-oss<br />
alias sound-service-0-8 snd-seq-oss<br />
alias sound-service-0-12 snd-pcm-oss}}<br />
<br />
===与蜂鸣器冲突===<br />
如果你确认所有声道都已解除静音,声卡驱动配置正确,且音量大小也合适,但还是听不到声音,那么请试试将下面的内容添加到 {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
(如果是 {{ic|via82xx}} 驱动)<br />
options snd-<模块名请自行补充> ac97_quirk=0<br />
<br />
(如果是 {{ic|snd_intel8x0}} 驱动)<br />
options snd-NAME-OF-MODULE ac97_quirk=1<br />
<br />
===麦克风输入无声音===<br />
In alsamixer, make sure that all the volume levels are up under recording, and that CAPTURE is toggled active on the microphone (e.g. Mic, Internal Mic) and/or on Capture (in alsamixer, select these items and press space). Try making positive Mic Boost and raising Capture and Digital levels higher; this make make static or distortion, but then you can adjust them back down once you are hearing ''something'' when you record<br />
<br />
As the pulseaudio wrapper is shown as "default" in alsamixer, you may have to press F6 to select your actual soundcard first. You may also need to enable and increase the volume of Line-in in the Playback section.<br />
<br />
To test the microphone, run these commands (see arecord's man page for further information):<br />
arecord -d 5 test-mic.wav<br />
aplay test-mic.wav<br />
<br />
If all fails, you may want to eliminate hardware failure by testing the microphone with a different device.<br />
<br />
For at least some computers, muting a microphone (MM) simply means its input does not go immediately to the speakers. It still receives input.<br />
<br />
Many Dell laptops need "-dmic" to be appended to the model name in {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-hda-intel model=dell-m6-dmic<br />
<br />
Some programs use try to use OSS as the main input software. Add the following lines to {{ic|/etc/modprobe.d/modprobe.conf}} to prevent OSS modules from being loaded:<br />
<br />
{{Note|The OSS modules are no longer autoloaded anyway.}}<br />
<br />
blacklist snd_pcm_oss<br />
blacklist snd_mixer_oss<br />
blacklist snd_seq_oss<br />
<br />
See also:<br />
* http://www.alsa-project.org/main/index.php/SoundcardTesting<br />
* http://alsa.opensrc.org/Record_from_mic<br />
<br />
===设置默认的麦克风/录音设备===<br />
某些程序(Pidgin、Adobe Flash)提供了设置录音设备的选项。如果录音设备不是内置的声卡,而是外接的(如USB摄像头、麦克风),就会有些问题。如果只想更改默认的录音设备、不更改回放设备,需要像下面这样设置 {{ic|~/.asoundrc}}:<br />
<br />
pcm.usb<br />
{<br />
type hw<br />
card U0x46d0x81d<br />
}<br />
<br />
pcm.!default<br />
{<br />
type asym<br />
playback.pcm<br />
{<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
capture.pcm<br />
{<br />
type plug<br />
slave.pcm "usb"<br />
}<br />
}<br />
<br />
把“U0x46d0x81d”替换为你的录音设备的名称。可以通过 {{ic|arecord -L}} 查看所有 ALSA 识别到的录音设备。<br />
<br />
===内置麦克风无效===<br />
首先,请检查 alsamixer,确保所有的录音声道的音量都达到可以录制的程度。如果还是不行,试试修改 /etc/sound.conf 并重载 snd-* 模块。这样就会多出来一个名叫 Capture 的音频控制,可以用它调节内置麦克风的音量。例如,对于 snd-hda-intel 模块,配置如下:<br />
<br />
options snd-hda-intel enable_msi=1<br />
<br />
然后,输入下面的命令重载模块,调整音量并测试。<br />
<br />
{{bc|# rmmod snd-hda-intel && modprobe snd-hda-intel}}<br />
<br />
===Intel 板载声卡无声===<br />
可能由两个相互冲突的驱动模块引起,分别是 {{ic|snd_intel8x0}} 和 {{ic|snd_intel8x0m}}。如果是的话,屏蔽 snd_intel8x0m 即可:<br />
<br />
{{hc|/etc/modprobe.d/modprobe.conf|blacklist snd_intel8x0m}}<br />
<br />
以下方法可能也有效:在 {{ic|alsamixer}} 或 {{ic|amixer}} 把 “External Amplifier” 设置为静音。参见:[http://alsa.opensrc.org/Intel8x0#Dell_Inspiron_8600_.28and_probably_others.29 ALSA wiki 相关条目]。<br />
<br />
也可以试试解除“Mix”的静音。<br />
<br />
===Intel 板载声卡耳机无声===<br />
对于笔记本电脑上的 '''Intel Corporation 82801 I (ICH9 Family) HD Audio Controller''' 声卡,可能需要添加如下内容到 modprobe 配置文件或 sound.conf,以解决耳机无声的问题:<br />
<br />
options snd-hda-intel model=$model<br />
<br />
$model 在下列型号中选择:<br />
<br />
* dell-vostro<br />
* olpc-xo-1_5<br />
* laptop<br />
* dell-m6<br />
* laptop-hpsense<br />
<br />
{{注意|可能需要在配置文件的 "alias" 语句之后添加上述内容。}}<br />
<br />
可以在内核文档中找到所有支持的型号,如[http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.35.y.git;a=blob;f=Documentation/sound/alsa/HD-Audio-Models.txt;h=dc25bb84b83b49665a7ed850e7bf5423d50cd3ba;hb=HEAD 该文]。这是 2.6.35 版本内核的文档,你需要自己查找适用于自己内核版本的文档。<br />
<br />
也可以从[http://www.mjmwired.net/kernel/Documentation/sound/alsa/HD-Audio-Models.txt 这里]获取型号列表。要查看你的声卡芯片名称,输入下列命令(其中的“*”请根据实际情况替换)。注意,有些芯片名称被更改过,因此实际名称与文件中的可能不匹配。<br />
<br />
cat /proc/asound/card*/codec* | grep Codec<br />
<br />
注意,这么做很可能导致所有输入设备(内置和外置麦克风)无法工作,即要么让麦克风正常、要么让耳机正常。如果遇到这一问题,请向 ALSA 开发者报告。<br />
<br />
另外,如果蜂鸣器(pcspkr)出现问题,可以试试使用以下配置修复:<br />
<br />
options snd-hda-intel model=$model enable=1 index=0<br />
<br />
===安装支持 S/PDIF 输出的显卡后无声 ===<br />
检测已加载的相关模块,并记录顺序:<br />
$ cat /proc/asound/modules<br />
0 snd_hda_intel<br />
1 snd_ca0106<br />
<br />
在 {{ic|/etc/modprobe.d/modprobe.conf}} 中禁用不希望使用的显卡的音频解码模块:<br />
# /etc/modprobe.d/modprobe.conf<br />
#<br />
install snd_hda_intel /bin/false<br />
<br />
如果不同设备使用同一模块,那么只能在 BIOS 中禁用其中之一。<br />
<br />
===音效差或卡顿===<br />
如果发现音效特别差,试试在 alsamixer 中调节一下 PCM 音量,到增益(gain)为0的水平。<br />
<br />
若使用 snd-usb-audio 驱动,可以试试在 {{ic|/etc/asound.conf}} 中启用 {{ic|softvol}}。下面是针对第一个声音设备的配置范例:<br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.dsnooper {<br />
type dsnoop<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
channels 2<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.softvol {<br />
type softvol<br />
slave { pcm "dmixer" }<br />
control {<br />
name "Master"<br />
card 0<br />
}<br />
}<br />
ctl.!default {<br />
type hw<br />
card 0<br />
}<br />
ctl.softvol {<br />
type hw<br />
card 0<br />
}<br />
ctl.dmixer {<br />
type hw<br />
card 0<br />
}<br />
<br />
===开始或停止音频播放时出现噪音===<br />
某些驱动模块(如 snd_ac97_codec 和 snd_hda_intel)会在声卡闲置时关闭它以节约用电。声卡启动或关闭时,就会发出刺耳的噪音。在调节音量或开关窗口(KDE4)时也可能发生这种情况。要消除这一问题,需要添加特定模块参数(通过 {{ic|modinfo snd_MY_MODULE}} 查询)关闭该功能。<br />
<br />
例如:对于 snd_hda_intel 模块,要禁用自动节电并解决噪音问题,需在 {{ic|/etc/modprobe.d/modprobe.conf}} 中添加如下内容:<br />
options snd_hda_intel power_save=0<br />
或:<br />
options snd_hda_intel power_save=0 power_save_controller=N<br />
<br />
可以先使用 {{ic|1=modprobe snd_hda_intel power_save=0}} 测试一下效果。<br />
<br />
对于 VIA VT1708S 板载声卡(使用 snd_hda_intel 模块)等一些声卡,即使把 power_save 参数设置为0可能仍会有噪音。还得在 alsamixer 中把“Line”声道激活才能生效,只要激活并调到一个非0的音量即可(1即可,不要太高)。<br />
<br />
内核模块文档:[https://www.kernel.org/doc/Documentation/sound/alsa/powersave.txt]<br />
<br />
对于笔记本,即使在 {{ic|/etc/modprobe.d}} 设置了 {{ic|power_save}} 参数,当切换电池时 pm-utils 仍会将该值重置为1。需要禁用相关的脚步才行(详情参见:[[Pm-utils#Disabling_a_hook]]):<br />
# touch /etc/pm/power.d/intel-audio-powersave<br />
<br />
===S/PDIF 输出无效===<br />
<br />
如果你已经在 alsamixer 中启用相关声道,但声卡的光学/同轴数字输出还是无效,试试以 root 身份运行:<br />
# iecset audio on<br />
<br />
每次开机都需要重新设置。如果嫌麻烦,可以自己写一个 [[Systemd (简体中文)#自己编写 .service 文件|systemd 服务]]。<br />
<br />
===HDMI 输出无效===<br />
若遇到声卡或主板的HDMI输出无法工作的问题,请首先确认 alsamixer 中的相关声道已经解除静音。如果还是不行,请进行下列操作:<br />
<br />
查询回放设备:<br />
<br />
$ aplay -l<br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: NVidia [HDA NVidia], device 0: ALC1200 Analog [ALC1200 Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 1: ALC1200 Digital [ALC1200 Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 3: NVIDIA HDMI [NVIDIA HDMI]<br />
Subdevices: 0/1<br />
Subdevice #0: subdevice #0<br />
<br />
我们需要的是 HDMI 设备的信息,下面来做一个测试(在该例中,0是声卡编号,3是设备编号):<br />
<br />
$ aplay -D plughw:0,3 /usr/share/sounds/alsa/Front_Center.wav<br />
<br />
如果 aplay 没有给出错误信息,但还是听不到声音,请尝试重启通过HDMI链接的播放设备(显示器或电视)。HDMI接口会在链接时执行一次握手,有可能因为当时没有检测到音频流而禁用了音频解码。特别是,如果使用了独立的窗口管理器(不太清楚 Gnome 和 KDE 的情况),可能就必须一边播放音乐、一边链接HDMI线。<br />
<br />
{{注意|如果你正在使用AMD显卡和3.0版Linux内核,就需要修改启动引导程序(Grub)的内核参数 {{ic|radeon.audio<nowiki>=</nowiki>1}},或在 modprobe 的配置文件中添加相关参数。进行上述操作前,请阅读 [http://www.x.org/wiki/RadeonFeature#fndef-72849d6d6eb3927d486f50ece74a739042965a6c-25 Radeon Feature Matrix],看看你的显卡是否已经被支持。[http://www.x.org/wiki/RadeonFeature#Decoder_ring_for_engineering_vs_marketing_names S. Islands] 中列举的一些显卡(从HD7750到HD7970)尚不支持 HDMI 音频输出。}}<br />
<br />
若测试成功,接下来编辑或创建 {{ic|~/.asoundrc}},把 HDMI 设置为默认音频设备。<br />
<br />
{{hc|~/.asoundrc|<br />
pcm.!default {<br />
type hw<br />
card 0<br />
device 3<br />
}<br />
}}<br />
<br />
如果上述配置无效,试试下面这个:<br />
{{hc|~/.asoundrc|<br />
defaults.pcm.card 0<br />
defaults.pcm.device 3<br />
defaults.ctl.card 0<br />
}}<br />
<br />
===HDMI Multi-channel PCM output does not work (Intel) ===<br />
As of Linux 3.1 multi-channel PCM output through HDMI with a Intel card (Intel Eaglelake, IbexPeak/Ironlake,SandyBridge/CougarPoint and IvyBridge/PantherPoint) is not yet supported. Support for it has been recently added and expected to be available in Linux 3.2. To make it work in Linux 3.1 you need to apply the following patches:<br />
<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=76adaa34db407f174dd06370cb60f6029c33b465 drm: support routines for HDMI/DP ELD]<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=e0dac65ed45e72fe34cc7ccc76de0ba220bd38bb drm/i915: pass ELD to HDMI/DP audio driver]<br />
<br />
===HP TX2500===<br />
Add these 2 lines into {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-cmipci mpu_port=0x330 fm_port=0x388<br />
options snd-hda-intel index=0 model=toshiba position_fix=1<br />
<br />
options snd-hda-intel model=hp (works for tx2000cto)<br />
<br />
===Skipping Sound When Playing MP3===<br />
If you have sound skipping when playing MP3 files and you have more then 2 speakers attached to your computer (i.e. > 2 speaker system), run alsamixer and disable the channels for the speakers that you '''DO NOT''' have (i.e. do not enable the sound for the center speaker if you do not have a center speaker.<br />
<br />
===Using a USB Headset and External USB Sound Cards===<br />
If you are using a USB headset with ALSA you can try using {{AUR|asoundconf}} (currently only available from the [[AUR]]) to set the headset as the primary sound output. Before running make sure you have usb audio module enabled ({{ic|modprobe snd-usb-audio}}).<br />
<br />
# asoundconf is-active<br />
# asoundconf list<br />
# asoundconf set-default-card <chosen soundcard><br />
<br />
==== Crackling sound with USB devices ====<br />
If you experience crackling sound on USB devices, you can try tuning the snd-usb-audio for minimal latency.<br />
<br />
Add this to your {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
options snd-usb-audio nrpacks=1<br />
<br />
source: http://alsa.opensrc.org/Usb-audio#Tuning_USB_devices_for_minimal_latencies<br />
<br />
==== Hot-plugging a USB Sound Card ====<br />
In order to automatically make a USB Sound Card the primary output device, when the card is plugged in, you can use the following udev rules (e.g. add the following two lines to {{ic|/etc/udev/rules.d/00-local.rules}} and reboot).<br />
<br />
{{bc|1=KERNEL=="pcmC[D0-9cp]*", ACTION=="add", PROGRAM="/bin/sh -c 'K=%k; K=$${K#pcmC}; K=$${K%%D*}; echo defaults.ctl.card $$K > /etc/asound.conf; echo defaults.pcm.card $$K >>/etc/asound.conf'"<br />
KERNEL=="pcmC[D0-9cp]*", ACTION=="remove", PROGRAM="/bin/sh -c 'echo defaults.ctl.card 0 > /etc/asound.conf; echo defaults.pcm.card 0 >>/etc/asound.conf'"}}<br />
<br />
===Error 'Unknown hardware' Appears After a Kernel Update===<br />
The following messages may be displayed during the start-up ALSA after the kernel update:<br />
Unknown hardware "foo" "bar" ...<br />
Hardware is initialized using a guess method<br />
/usr/sbin/alsactl: set_control:nnnn:failed to obtain info for control #mm (No such file or directory)<br />
<br />
or<br />
Found hardware: "HDA-Intel" "VIA VT1705" "HDA:11064397,18490397,00100000" "0x1849" "0x0397"<br />
Hardware is initialized using a generic method<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #1 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #2 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #25 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #26 (No such file or directory)<br />
<br />
Simply store ALSA mixer settings again (as root):<br />
# alsactl -f /var/lib/alsa/asound.state store<br />
<br />
It may be necessary configure ALSA again with alsamixer<br />
<br />
===HDA Analyzer===<br />
If the mappings to your audio pins(plugs) do not correspond but ALSA works fine, you could try HDA Analyzer -- a pyGTK2 GUI for HD-audio control can be found [http://www.alsa-project.org/main/index.php/HDA_Analyzer at the ALSA wiki].<br />
Try tweaking the Widget Control section of the PIN nodes, to make microphones IN and headphone jacks OUT. Referring to the Config Defaults heading is a good idea.<br />
<br />
NOTE: the script is done by such way that it is incompatible with python3 (which is now shipped with ArchLinux) but tries to use it.<br />
The workaround is: open "run.py", find all occurences of "python" (2 occurences - one on the first line, and the second on the last line) and replace them all by "python2".<br />
<br />
NOTE2: the script requires root acces, but running it via su/sudo is bogus. Run it via {{ic|kdesu}} or {{ic|gksu}}.<br />
<br />
===ALSA with SDL===<br />
If you get no sound via SDL and ALSA cannot be chosen from the application. Try setting the environmental variable SDL_AUDIODRIVER to alsa.<br />
# export SDL_AUDIODRIVER=alsa<br />
<br />
===Low Sound Workaround===<br />
<br />
If you are facing low sound even after maxing out your speakers/headphones, you can give the softvol plugin a try. Add the following to {{ic|/etc/asound.conf}}.<br />
{{bc|<nowiki><br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
<br />
pcm.softvol {<br />
type softvol<br />
slave {<br />
pcm "dmix"<br />
}<br />
control {<br />
name "Pre-Amp"<br />
card 0<br />
}<br />
min_dB -5.0<br />
max_dB 20.0<br />
resolution 6<br />
}<br />
</nowiki>}}<br />
{{note| You will probably have to restart the computer, as restarting the alsa daemon did not load the new configuration for me. Also, if the configuration does not work even after restarting, try changing {{ic|plug}} with {{ic|hw}} in the above configuration.}}<br />
<br />
After the changes are loaded successfully, you will see a {{ic|Pre-Amp}} section in alsamixer. You can adjust the levels there.<br />
{{note|Setting a high value for {{ic|Pre-Amp}} can cause sound distortion, so adjust it according to the level that suits you.}}<br />
<br />
===Popping sound after resuming from suspension===<br />
You might hear a popping sound after resuming the computer from suspension. This can be fixed by editing {{ic|/etc/pm/sleep.d/90alsa}} and removing the line that says {{ic|aplay -d 1 /dev/zero}}<br />
<br />
==配置文件范例==<br />
参见:[[Advanced Linux Sound Architecture/Example Configurations]]。<br />
<br />
==相关阅读==<br />
* [http://www.mjmwired.net/kernel/Documentation/sound/alsa/ALSA-Configuration.txt ALSA 模块高级配置]<br />
* [http://alsa.opensrc.org/Main_Page 非官方的 ALSA Wiki]<br />
* [https://bbs.archlinux.org/viewtopic.php?id=36815 如何从 svn 获取驱动代码并编译]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Advanced_Linux_Sound_Architecture_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=256353
Advanced Linux Sound Architecture (简体中文)
2013-05-08T07:04:39Z
<p>Cuihao: /* S/PDIF 输出无效 */</p>
<hr />
<div>[[Category:简体中文]]<br />
[[Category:Sound (简体中文)]]<br />
[[Category:Audio/Video (简体中文)]]<br />
[[cs:Advanced Linux Sound Architecture]]<br />
[[de:Alsa]]<br />
[[es:Advanced Linux Sound Architecture]]<br />
[[fa:ALSA]]<br />
[[fr:Alsa]]<br />
[[he:Advanced Linux Sound Architecture]]<br />
[[it:Advanced Linux Sound Architecture]]<br />
[[ja:Advanced Linux Sound Architecture]]<br />
[[nl:Advanced Linux Sound Architecture]]<br />
[[pt:Advanced Linux Sound Architecture]]<br />
[[ru:Advanced Linux Sound Architecture]]<br />
[[sk:Advanced Linux Sound Architecture]]<br />
[[th:Advanced Linux Sound Architecture]]<br />
[[tr:Gelişmiş_ses_mimarisi_(ALSA)]]<br />
[[en:Advanced Linux Sound Architecture]]<br />
<br />
{{Article summary start}}<br />
{{Article summary text|ALSA 的使用、配置及疑难解答。}}<br />
{{Article summary heading|相关页面}}<br />
{{Article summary wiki|Sound system (简体中文)}}<br />
{{Article summary wiki|Disable PC Speaker Beep (简体中文)}}<br />
{{Article summary end}}<br />
{{TranslationStatus (简体中文)|Advanced_Linux_Sound_Architecture|2013-04-25|254584}}<br />
<br />
[[Wikipedia:zh:ALSA|高级 Linux 声音体系]](Advanced Linux Sound Architecture,'''ALSA''')是Linux中提供声音设备驱动的内核组件,用来代替原来的开放声音系统(Open Sound System,OSSv3)。除了声音设备驱动,'''ALSA'''还包含一个用户空间的函数库,以方便开发者通过高级API使用驱动功能,而不必直接与内核驱动交互。<br />
<br />
{{注意|关于另一种声音体系,请阅读[[Open Sound System (简体中文)|开放声音系统(OSS)]]的页面。}}<br />
<br />
==安装==<br />
Arch 默认的内核已经通过一套模块提供了 ALSA,不必特别安装。<br />
<br />
[[udev (简体中文)|udev]]会在系统启动时自动检测硬件,并加载相应的声音设备驱动模块。这时,你的声卡已经可以工作了,只是所有声道默认都被设置成静音了。<br />
<br />
在本地登录(通过虚拟终端或登录管理器)的用户,都有权限播放音频并调整音量。要让远程登录的用户拥有这些权限,必须把用户[[Users and Groups (简体中文)#用户组管理|加入]] {{ic|audio}} 用户组。该组的成员可以直接访问声音设备,会导致某些程序独占音频输出(破坏软件混音),还可能影响用户快速切换和拖机(multiseat)。因此,除非真的有某些[https://wiki.ubuntu.com/Audio/TheAudioGroup 特殊需求],'''不建议'''把用户加入 {{ic|audio}} 用户组。<br />
<br />
===用户空间工具===<br />
从 [[Official Repositories (简体中文)|官方仓库]]:<br />
* [[pacman (简体中文)|安装]] {{Pkg|alsa-utils}} 软件包,其中的 {{ic|alsamixer}} 工具允许用户在控制台或终端中配置声音设备。<br />
* 安装 {{Pkg|alsa-oss}} 软件包,如果想让 OSS 应用程序与dmix(软件混音)协同工作。 <br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。}}<br />
*安装 {{Pkg|alsa-plugins}} 软件包,如果需要 [[#高质量重采样|高质量重采样]]、[[#Upmixing/Downmixing|软件模拟环绕立体声]]和其他高级特性。<br />
<br />
==解除各声道的静音==<br />
目前版本的 ALSA 安装后,所有声道'''默认是静音的''',必须手动解除。<br />
<br />
使用 {{ic|alsamixer}} 的 ncurses 界面,配置十分简单:<br />
<br />
$ alsamixer<br />
<br />
此外,还可以在命令行下使用 {{ic|amixer}}:<br />
<br />
$ amixer sset Master unmute<br />
<br />
在 {{ic|alsamixer}} 中,下方标有 {{ic|MM}} 的声道是静音的,而标有 {{ic|00}} 的通道已经启用。<br />
<br />
使用左右方向键,选中 {{ic|Master}} 和 {{ic|PCM}} 声道。按下 {{keypress|m}} 键解除静音。使用上方向键增加音量,直到增益值为{{ic|0}}。该值显示在左上方 {{ic|Item:}} 字段后。过高的增益值会导致声音失真。<br />
<br />
要想得到完整的 5.1 或 7.1 环绕立体声,还得解除 Front、Surround、Center、LFE (subwoofer) 和 Side 这些声道的静音(上述名称是 Intel HD Audio 声卡使用的声道名,可能因设备不同而有所差异)。注意,仅有这些设置,系统不会自动将立体声源(多数音乐)提升(upmix)成环绕立体声。如果需要这些功能,请阅读[[#Upmixing/Downmixing]]。<br />
<br />
要启用麦克风,切换至 Capture 选项卡,按下 {{keypress|F4}},按下 {{keypress|空格}} 启用其中一个声道即可。<br />
<br />
按下 {{Keypress|Esc}} 键退出 alsamixer。<br />
<br />
{{注意|<br />
* 有一些声卡,需要关闭数字输出或将其调成静音,才能输出模拟音频信号。对于 Soundblaster Audigy LS 声卡,需要把 IEC958 通道设成静音。 <br />
* 有些机器(如 Thinkpad T61),有一个 Speaker 通道,必须按照上述方法打开调整。<br />
* 有些机器(如 Dell E6400),可能还需要把 {{ic|Front}} 和 {{ic|Headphone}} 声道打开调整。}}<br />
<br />
接下来,测试声卡是否工作:<br />
<br />
$ speaker-test -c 2<br />
<br />
根据扬声器的实际情况,调整 -c 后的数值。对于 7.1 声道,这个数字是 8:<br />
<br />
$ speaker-test -c 8<br />
<br />
如果没有正常工作,请继续阅读 [[#配置]] 以及 [[#疑难解答]] 部分。<br />
<br />
{{pkg|alsa-utils}} 软件包提供了两个配置 alsa 的服务:{{ic|alsa-restore.service}} 和 {{ic|alsa-store.service}}。它们分别在开机和关机时自动运行。<br />
<br />
==配置==<br />
=== VirtualBox 虚拟机无声音 ===<br />
<br />
如果使用 VirtualBox 时碰上了麻烦,请尝试以下命令:<br />
<br />
{{hc|$ alsactl init|2=<nowiki><br />
<br />
Found hardware: "ICH" "SigmaTel STAC9700,83,84" "AC97a:83847600" "0x8086" "0x0000"<br />
Hardware is initialized using a generic method<br />
</nowiki><br />
}}<br />
<br />
如果还是不行,请检查所使用的音频软件是否激活了 ALSA 输出。<br />
<br />
===设置默认声卡===<br />
<br />
如果发现开机时声卡次序会发生变化,可以在通过 {{ic|/etc/modprobe.d}} 中的 {{ic|.conf}} 文件(比如 {{ic|/etc/modprobe.d/alsa-base.conf}})手动设置次序。<br />
比如,要让 mia 声卡成为 #0、Intel HDA 声卡成为 #1:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<br />
options snd slots=snd_mia,snd_hda_intel<br />
options snd_mia index=0<br />
options snd_hda_intel index=1<br />
}}<br />
<br />
使用 {{ic|<nowiki>lsmod | grep snd</nowiki>}} 获取设备列表。<br />
<br />
snd_mia 和 snd_hda_intel 分别是两个声卡的内核驱动模块。该配置仅适用于同时装有 mia 声卡和使用 snd_hda_intel 驱动的声卡(通常是板载声卡)的情况。<br />
<br />
如果使用 -2 的 index 值,ALSA 就不会将对应的设备作为主声卡使用。Linux Mint 和 Ubuntu 等发行版使用了以下配置,避免 USB 声卡和其他“非主流”设备变成 index 为 0 的主声卡:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<br />
options bt87x index=-2<br />
options cx88_alsa index=-2<br />
options saa7134-alsa index=-2<br />
options snd-atiixp-modem index=-2<br />
options snd-intel8x0m index=-2<br />
options snd-via82xx-modem index=-2<br />
options snd-usb-audio index=-2<br />
options snd-usb-caiaq index=-2<br />
options snd-usb-ua101 index=-2<br />
options snd-usb-us122l index=-2<br />
options snd-usb-usx2y index=-2<br />
# Keep snd-pcsp from being loaded as first soundcard<br />
options snd-pcsp index=-2<br />
# Keep snd-usb-audio from beeing loaded as first soundcard<br />
options snd-usb-audio index=-2<br />
}}<br />
<br />
以上配置会在系统重启后生效。<br />
<br />
{{Out of date|据英文 Wiki 用户反应,{{ic|/etc/asound.conf}} 这个配置文件默认是不存在的,下列操作可能有误。}}<br />
<br />
首先,运行 {{ic|aplay -l}},获取声卡的声卡ID和设备ID:<br />
<br />
{{hc|$ aplay -l|2=<nowiki><br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: Intel [HDA Intel], device 0: CONEXANT Analog [CONEXANT Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: Intel [HDA Intel], device 1: Conexant Digital [Conexant Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 1: JamLab [JamLab], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 2: Audio [Altec Lansing XT1 - USB Audio], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
</nowiki>}}<br />
<br />
拿输出中的最后一项来说,这个声卡的声卡ID为2,设备ID为0。若要把这块声卡作为默认声卡,可以把下列配置添加到系统级别的 {{ic|/etc/asound.conf}} 或用户级别的 {{ic|~/.asoundrc}} 文件。如果文件不存在,可以手动创建。其中的各个ID,请根据实际情况调整:<br />
{{bc|<br />
defaults.pcm.card 2<br />
defaults.pcm.device 0<br />
defaults.ctl.card 2<br />
}}<br />
“pcm”选项决定用来播放音频的设备,而“ctl”选项决定那个声卡能够由控制工具(如 alsamixer)使用。<br />
<br />
上述配置在重启音频程序(如 mplayer)后立即生效。<br />
<br />
===确认所有声音模块都已经加载===<br />
<br />
一般 udev 都会自动识别出声卡。使用以下命令确认:<br />
<br />
{{hc|<nowiki>$ lsmod | grep '^snd' | column -t</nowiki>|<nowiki><br />
snd_hda_codec_hdmi 22378 4<br />
snd_hda_codec_realtek 294191 1<br />
snd_hda_intel 21738 1<br />
snd_hda_codec 73739 3 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel<br />
snd_hwdep 6134 1 snd_hda_codec<br />
snd_pcm 71032 3 snd_hda_codec_hdmi,snd_hda_intel,snd_hda_codec<br />
snd_timer 18992 1 snd_pcm<br />
snd 55132 9 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel,snd_hda_codec,snd_hwdep,snd_pcm,snd_timer<br />
snd_page_alloc 7017 2 snd_hda_intel,snd_pcm<br />
</nowiki>}}<br />
<br />
如果你的输出和上面类似,那就说明声卡已经被正确识别。<br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。如有需要,请[[Kernel_modules_(简体中文)#装入|手动加载]]。}}<br />
<br />
还可以检查一下 {{ic|/dev/snd/}} 目录,看看是否有这些设备文件:<br />
<br />
{{hc|$ ls -l /dev/snd|2=<nowiki><br />
total 0<br />
crw-rw---- 1 root audio 116, 0 Apr 8 14:17 controlC0<br />
crw-rw---- 1 root audio 116, 32 Apr 8 14:17 controlC1<br />
crw-rw---- 1 root audio 116, 24 Apr 8 14:17 pcmC0D0c<br />
crw-rw---- 1 root audio 116, 16 Apr 8 14:17 pcmC0D0p<br />
crw-rw---- 1 root audio 116, 25 Apr 8 14:17 pcmC0D1c<br />
crw-rw---- 1 root audio 116, 56 Apr 8 14:17 pcmC1D0c<br />
crw-rw---- 1 root audio 116, 48 Apr 8 14:17 pcmC1D0p<br />
crw-rw---- 1 root audio 116, 1 Apr 8 14:17 seq<br />
crw-rw---- 1 root audio 116, 33 Apr 8 14:17 timer<br />
</nowiki>}}<br />
<br />
{{注意|在 IRC 或论坛寻求这方面帮助时,别忘了贴出上面几个命令的输出。}}<br />
<br />
如果你的输出跟上面类似,或至少有 '''controlC0''' 和 '''pcmC0D0p''',那么声卡就已经正常加载了。<br />
<br />
如果出现问题,声卡模块没有正确加载,那么请尝试手动加载模块:<br />
<br />
* 确定声卡对应的驱动模块:[http://www.alsa-project.org/main/index.php/Matrix:Main ALSA Soundcard Matrix]。这些模块都会有一个”snd-“前缀(例如:{{ic|snd-via82xx}})。<br />
* [[Kernel_modules_(简体中文)#装入|加载模块]]。<br />
* 检查 {{ic|/dev/snd}} 目录中的设备文件(参见上文);或者,检查 {{ic|alsamixer}} 或 {{ic|amixer}} 的输出是否正确。<br />
* 修改配置,使 {{ic|snd-<模块名>}} 和 {{ic|snd-pcm-oss}} 模块[[Kernel_modules_(简体中文)#装入|开机自动加载]]。<br />
<br />
===启用 SPDIF 输出===<br />
<br />
(由 Gentoo 论坛的 gralves 提供)<br />
* 在 GNOME 音量控制的选项(Options)标签中,把 IEC958 改成 PCM。该选项可以在首选项中启用。<br />
* 不使用 GNOME 音量控制工具的话:<br />
** 编辑 {{ic|/var/lib/alsa/asound.state}}。该文件是 alsasound 保存混音器设置的地方。<br />
** 找到这一行:“IEC958 Playback Switch”。该行上下应该有一行内容为 {{ic|value:false}},将其改为 {{ic|value:true}}。<br />
** 再找到这一行:”IEC958 Playback AC97-SPSA“。把对应的值改为 0。<br />
** 重启 ALSA。<br />
<br />
{{Out of date|rc.local 在 systemd 中已经弃用,以下方法可能不再适用。}}<br />
<br />
还有另外一个开机自动启用 SPDIF 输出的方法(在 SoundBlaster Audigy 声卡上测试过):<br />
<br />
* 在 {{ic|/etc/rc.local}} 中添加如下内容:<br />
# Use COAX-digital output<br />
amixer set 'IEC958 Optical' 100 unmute<br />
amixer set 'Audigy Analog/Digital Output Jack' on<br />
<br />
使用以下命令,获取你的声卡的数字输出的名称:<br />
$ amixer scontrols<br />
<br />
===系统级均衡器===<br />
====使用 AlsaEqual(包含界面)====<br />
从 [[AUR (简体中文)|AUR]] 安装 {{AUR|alsaequal}}。<br />
<br />
{{注意|如果使用的是64位系统,而又安装了32位的 Flash 插件,这里的设置会导致 Flash 无声。如果发生了这种情况,请禁用 alsaequal 或编译32位版本的 alsaequal。}}<br />
<br />
安装后,把下列内容添加到 ALSA 配置文件({{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}}):<br />
<br />
ctl.equal {<br />
type equal;<br />
}<br />
<br />
pcm.plugequal {<br />
type equal;<br />
# Modify the line below if you do not<br />
#slave.pcm "plughw:0,0";<br />
#by default we want to play from more sources at time:<br />
slave.pcm "plug:dmix";<br />
}<br />
#pcm.equal {<br />
# If you do not want the equalizer to be your<br />
# default soundcard comment the following<br />
# line and uncomment the above line. (You can<br />
# choose it as the output device by addressing<br />
# it with specific apps,eg mpg123 -a equal 06.Back_In_Black.mp3)<br />
pcm.!default {<br />
type plug;<br />
slave.pcm plugequal;<br />
}<br />
<br />
设置好后,切换均衡器:<br />
$ alsamixer -D equal<br />
<br />
需要注意,每个用户的 alsaequal 配置文件都是独立的,位于 {{ic|~/.alsaequal.bin}}。因此,如果想在 [[mpd]] 之类的以独立用户身份运行的程序中使用 AlsaEqual,就得像下面这样:<br />
# su mpd -c 'alsamixer -D equal'<br />
或者,在其他用户的主目录下创建符号链接,指向你的 {{ic|.alsaequal.bin}}……<br />
<br />
=====管理 AlsaEqual 配置=====<br />
从 [http://xyne.archlinux.ca/repos/ Xyne 的软件仓库] 或 [https://aur.archlinux.org/packages.php?ID=62420 AUR] 安装 [http://xyne.archlinux.ca/projects/alsaequal-mgr/ alsaequal-mgr]。<br />
<br />
同上,配置均衡器: <br />
$alsamixer -D equal<br />
<br />
如果对效果感到满意,将其命名(如“foo”)并保存该配置:<br />
$alsaequal-mgr save foo<br />
<br />
重新加载“foo”中的配置:<br />
$alsaequal-mgr load foo<br />
<br />
这样,就可以为游戏、电影、不同音乐流派、网络电话等等创建不同的均衡器配置,以便随时切换。<br />
<br />
参见[http://xyne.archlinux.ca/projects/alsaequal-mgr/ 项目主页]和程序的帮助信息,了解更多设置。<br />
<br />
====使用 mbeq====<br />
{{注意|该软件用到了一个 ladspa 插件,可能导致播放音频时 CPU 占用率偏高。In addition, this was made with stereophonic sound (e.g. headphones) in mind.}}<br />
<br />
安装 {{Pkg|alsa-plugins}}、{{Pkg|ladspa}} 和 {{Pkg|swh-plugins}} 软件包。<br />
<br />
* 在 {{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}} 中添加如下内容:<br />
<br />
{{hc|/etc/asound.conf|2=<nowiki>pcm.eq {<br />
type ladspa<br />
<br />
# The output from the EQ can either go direct to a hardware device<br />
# (if you have a hardware mixer, e.g. SBLive/Audigy) or it can go<br />
# to the software mixer shown here.<br />
#slave.pcm "plughw:0,0"<br />
slave.pcm "plug:dmix"<br />
<br />
# Sometimes you may need to specify the path to the plugins,<br />
# especially if you have just installed them. Once you have logged<br />
# out/restarted this should not be necessary, but if you get errors<br />
# about being unable to find plugins, try uncommenting this.<br />
#path "/usr/lib/ladspa"<br />
<br />
plugins [<br />
{<br />
label mbeq<br />
id 1197<br />
input {<br />
#this setting is here by example, edit to your own taste<br />
#bands: 50hz, 100hz, 156hz, 220hz, 311hz, 440hz, 622hz, 880hz, 1250hz, 1750hz, 25000hz,<br />
#50000hz, 10000hz, 20000hz<br />
controls [ -5 -5 -5 -5 -5 -10 -20 -15 -10 -10 -10 -10 -10 -3 -2 ]<br />
}<br />
}<br />
]<br />
}<br />
<br />
# Redirect the default device to go via the EQ - you may want to do<br />
# this last, once you are sure everything is working. Otherwise all<br />
# your audio programs will break/crash if something has gone wrong.<br />
<br />
pcm.!default {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
<br />
# Redirect the OSS emulation through the EQ too (when programs are running through "aoss")<br />
<br />
pcm.dsp0 {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
</nowiki>}}<br />
<br />
* 完成。若有问题,欢迎前往论坛咨询。<br />
<br />
==高质量重采样==<br />
启用软件混音时,ALSA 会强制把所有音频重采样到相同的频率(如果系统支持,默认是 48000)。dmix 的重采样算法很糟糕,会导致显著的质量损失。<br />
<br />
安装 {{pkg|alsa-plugins}} 和 {{pkg|libsamplerate}}。<br />
<br />
将默认的采样率转换器更改为 libsamplerate:<br />
<br />
{{hc|/etc/asound.conf|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
或:<br />
<br />
{{hc|~/.asoundrc|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
'''samplerate_best''' 输出的声音质量最佳,但需要耗费大量CPU资源进行实时重采样。还有其他的算法可供选择(如 '''samplerate''' 等),但比起默认的算法,这些算法的改进并不明显。<br />
<br />
{{警告|某些系统中,启用 samplerate_best 可能导致 Flash 播放器无声音。}}<br />
<br />
==上混和下混==<br />
{{注意|由于未找到 upmix 和 downmix 的常用翻译,译者暂时采用字面翻译(上混、下混)。所谓“上混”,即将声道较少的立体声(如常见的双声道立体声)信号,通过软件或硬件处理,模拟为声道较多的立体声(如5.1环绕立体声)信号。反之即为“下混”。}}<br />
<br />
===上混(upmixing)===<br />
播放双声道立体声音源(如音乐)时,利用上混,即可充分利用 5.1 或 7.1 环绕立体声的所有声道。以前,上混很麻烦,还经常出错。但如今就方便多了,只需要安装一些插件即可。这些插件由 {{Pkg|alsa-plugins}} 软件包提供。<br />
<br />
安装后,在配置文件({{ic|/etc/asound.conf}} 或 {{ic|~/.asoundrc}})中添加如下内容:<br />
pcm.upmix71 {<br />
type upmix<br />
slave.pcm "surround71"<br />
delay 15<br />
channels 8<br />
}<br />
<br />
该范例适用于7.1声道上混。类比该范例,同样能设置5.1或4.0声道上混。<br />
<br />
该设置添加了一个新的用于上混的 pcm。如果希望所有音频源都通过该 pcm 输出,在上述设置后添加如下配置即可:<br />
<br />
pcm.!default "plug:upmix71"<br />
<br />
插件默认配置就允许多个音频源同时通过它输出,一般无需额外配置。如果不行,就得向下面这样为上混配置 dmixer:<br />
pcm.dmix6 {<br />
type asym<br />
playback.pcm {<br />
type dmix<br />
ipc_key 567829<br />
slave {<br />
pcm "hw:0,0"<br />
channels 6<br />
}<br />
}<br />
}<br />
<br />
注意要用 dmix6 而非 surround71。<br />
<br />
如果遇到声音卡顿或混乱,可能需要增加 buffer_size(比如增加到 32768),或使用[[#高质量重采样|高质量重采样]]。<br />
<br />
===下混(downmixing)===<br />
有时也会用到下混,比如在只支持双声道立体声的电脑上观看5.1声道的电影时。这一功能由 ALSA 插件 vdownmix 实现(同样在 {{Pkg|alsa-plugins}} 软件包里)。<br />
<br />
在配置文件中添加如下内容:<br />
<br />
pcm.!surround51 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
pcm.!surround40 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
<br />
==混音==<br />
===软件混音(dmix)===<br />
<br />
{{注意|在 ALSA 1.0.9rc2 及以上版本中,如果声卡不支持硬件混音,模拟输出将默认使用 dmix 混音,无需额外设置。}}<br />
<br />
如果软件混音未正常启用,那么可以手动创建 {{ic|~/.asoundrc}},加入以下内容:<br />
<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
<br />
这样就启用了软件混音,多个程序能够同时使用声卡。<br />
<br />
至于 S/PDIF 之类的数字音频输出,目前的 ALSA 还不会默认启用 dmix。因此,必须像上面这样手动启用。<br />
<br />
常见问题的解答,参见[[#疑难解答]]部分。<br />
<br />
===硬件混音===<br />
====支持====<br />
如果声卡支持,一般无需任何配置即可启用硬件混音。绝大多数板载声卡都不支持硬件混音,必须得使用软件混音(见上文)。很多独立声卡都支持硬件混音,Linux 下支持较好的有:<br />
<br />
* Creative SoundBlaster Live! (5.1 型号)<br />
* Creative SoundBlaster Audigy (部分型号)<br />
* Creative SoundBlaster Audidy 2 (ZS 系列型号)<br />
* Creative SoundBlaster Audigy 4 (Pro 系列型号)<br />
<br />
{{注意|上述声卡的低端版本(Audigy SE、Audigy 2 NX、SoundBlaster Live! 24bit 以及 SoundBlaster Live! 7.1)使用了不同的芯片,'''不支持'''硬件混音。}}<br />
<br />
{{注意|板载 VIA8237 芯片支持4路硬件混音,但在某些主板上只能使用3路(第4路无声),甚至根本无法使用。即使能正常工作,质量也比不上其他混音方法。}}<br />
<br />
====问题修复====<br />
如果你的声卡是 Intel Corporation 82801I (ICH9 Family) HD Audio Controller (rev 02),且使用了64位系统,需要执行以下命令才能在 Enemy Territory 中听到声音:<br />
echo "et.x86 0 0 direct" > /proc/asound/card0/pcm0p/oss<br />
echo "et.x86 0 0 disable" > /proc/asound/card0/pcm0c/oss<br />
<br />
==疑难解答==<br />
===使用CPU频率动态调整时,音频发生跳跃===<br />
使用 {{ic|ondemand}} 或 {{ic|conservative}} 频率策略、启用CPU频率动态调整时,某些声卡和某些 ALSA 驱动会出现声音跳跃的情况。目前,唯一的解决方案是使用固定的频率,如 {{ic|performance}} 性能策略。<br />
<br />
详情参见 [[CPU Frequency Scaling (简体中文)]]。<br />
<br />
===同时只能有一个用户播放音频===<br />
有时会出现一个用户独占软件混音器的情况。多数情况下这没什么影响,但在使用 [[mpd]] 这样以独立用户身份运行的程序时,就会出现问题。当 mpd 运行时,用户自己就无法播放其他音频了。一种解决方法是,以正在使用的用户身份运行 mpd。另一种方法是,在 ALSA 配置文件的 {{ic|pcm.dmixer}} 部分添加 {{ic|ipc_key_add_uid 0}} 禁止独占。以下是 {{ic|asound.conf}} 示例的片段,省略的部分无需修改:<br />
<br />
...<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
slave {<br />
...<br />
<br />
===无法同时播放多个音频===<br />
如果安装了 [[PulseAudio (简体中文)|PulseAudio]](可能作为 [[GNOME (简体中文)|GNOME]] 的依赖被安装),可能出现无法同步播放的问题,因为 PulseAudio 的默认配置是“独占”声卡。ALSA 的用户可能对自己的配置已经很满意、不希望使用 [[PulseAudio (简体中文)|PulseAudio]]。解决方案是编辑 {{ic|/etc/asound.conf}} 并注释掉以下内容:<br />
# Use PulseAudio by default<br />
#pcm.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
# hint {<br />
# show on<br />
# description "Default ALSA Output (currently PulseAudio Sound Server)"<br />
# }<br />
#}<br />
<br />
可能还需要注释掉下面的内容:<br />
#ctl.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
#}<br />
<br />
比起完全卸载 [[PulseAudio (简体中文)|PulseAudio]],这大概是个更简单的方法。<br />
<br />
以下是 {{ic|/etc/asound.conf}} 配置文件的一个示例:<br />
{{bc|<nowiki>pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
}<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}</nowiki><br />
}}<br />
<br />
{{注意|本 {{ic|/etc/asound.conf}} 本来是配合 [[MPD]] 全局配置使用的。参见[[#同时只能有一个用户播放音频|该节]]关于多用户的内容。}}<br />
<br />
===有时候开机无声===<br />
如果会随机地出现开机无声的问题,原因可能是多声卡的顺序发生了变化。如果是这种情况,请参照[[#设置默认声卡]]进行设置。<br />
<br />
对于 mpd 用户,上述配置无效,请参考[http://mpd.wikia.com/wiki/Configuration#ALSA_MPD_software_volume_control 该文]设置。<br />
<br />
===特定程序的问题===<br />
对于不使用系统声音配置的程序,如 XMMS 和 Mplayer,必须特别设置以应用 ALSA 配置。<br />
<br />
对于 mplayer,将以下内容添加到 {{ic|~/.mplayer/config}} (或系统全局配置文件 {{ic|/etc/mplayer/mplayer.conf}}):<br />
ao=alsa<br />
<br />
对于 XMMS 和 Beep Media Player,打开选项窗口,要把声音驱动设置为 ALSA 而不是 OSS。<br />
<br />
XMMS 的设置方法:<br />
* 启动 XMMS<br />
** 选项 -> 首选项<br />
** 选择 Alsa 输出插件<br />
<br />
对于未提供 ALSA 输出的程序,可以使用 alsa-oss 软件包中的 aoss 工具启动。使用方法是:在要运行的程序的命令行前加上 {{ic|aoss}}。例如:<br />
aoss realplay<br />
<br />
pcm.!default{ ... } 貌似已经不管用了。但下面的设置有效:<br />
pcm.default pcm.dmixer<br />
<br />
===特定型号的配置===<br />
ALSA 会通过 BIOS 检测[http://www.kernel.org/doc/Documentation/sound/alsa/HD-Audio-Models.txt 声卡型号],少数情况下也可能无法正确识别。声卡芯片类型可以在 {{ic|alsamixer}} 界面上找到(如 ALC662),而型号则要设置在 {{ic|/etc/modprobe.d/modprobe.conf}} 或 {{ic|/etc/modprobe.d/sound.conf}} 文件中:<br />
<br />
options snd-hda-intel model=<型号><br />
<br />
具体设置因声卡型号而异。多数情况下不需要特地这样做。如果想知道具体声卡型号的设置,请查阅 [http://bugtrack.alsa-project.org/main/index.php/Matrix:Main Alsa Soundcard List],找到你的声卡型号,点击“Details”,然后查看“Setting up modprobe...”小节,将其中的内容插入 {{ic|/etc/modprobe.d/modprobe.conf}} 文件即可。例如,Intel AC97 的配置如下:<br />
<br />
{{bc|# ALSA portion<br />
alias char-major-116 snd<br />
alias snd-card-0 snd-intel8x0<br />
# module options should go here<br />
<br />
# OSS/Free portion<br />
alias char-major-14 soundcore<br />
alias sound-slot-0 snd-card-0<br />
<br />
# card #1<br />
alias sound-service-0-0 snd-mixer-oss<br />
alias sound-service-0-1 snd-seq-oss<br />
alias sound-service-0-3 snd-pcm-oss<br />
alias sound-service-0-8 snd-seq-oss<br />
alias sound-service-0-12 snd-pcm-oss}}<br />
<br />
===与蜂鸣器冲突===<br />
如果你确认所有声道都已解除静音,声卡驱动配置正确,且音量大小也合适,但还是听不到声音,那么请试试将下面的内容添加到 {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
(如果是 {{ic|via82xx}} 驱动)<br />
options snd-<模块名请自行补充> ac97_quirk=0<br />
<br />
(如果是 {{ic|snd_intel8x0}} 驱动)<br />
options snd-NAME-OF-MODULE ac97_quirk=1<br />
<br />
===麦克风输入无声音===<br />
In alsamixer, make sure that all the volume levels are up under recording, and that CAPTURE is toggled active on the microphone (e.g. Mic, Internal Mic) and/or on Capture (in alsamixer, select these items and press space). Try making positive Mic Boost and raising Capture and Digital levels higher; this make make static or distortion, but then you can adjust them back down once you are hearing ''something'' when you record<br />
<br />
As the pulseaudio wrapper is shown as "default" in alsamixer, you may have to press F6 to select your actual soundcard first. You may also need to enable and increase the volume of Line-in in the Playback section.<br />
<br />
To test the microphone, run these commands (see arecord's man page for further information):<br />
arecord -d 5 test-mic.wav<br />
aplay test-mic.wav<br />
<br />
If all fails, you may want to eliminate hardware failure by testing the microphone with a different device.<br />
<br />
For at least some computers, muting a microphone (MM) simply means its input does not go immediately to the speakers. It still receives input.<br />
<br />
Many Dell laptops need "-dmic" to be appended to the model name in {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-hda-intel model=dell-m6-dmic<br />
<br />
Some programs use try to use OSS as the main input software. Add the following lines to {{ic|/etc/modprobe.d/modprobe.conf}} to prevent OSS modules from being loaded:<br />
<br />
{{Note|The OSS modules are no longer autoloaded anyway.}}<br />
<br />
blacklist snd_pcm_oss<br />
blacklist snd_mixer_oss<br />
blacklist snd_seq_oss<br />
<br />
See also:<br />
* http://www.alsa-project.org/main/index.php/SoundcardTesting<br />
* http://alsa.opensrc.org/Record_from_mic<br />
<br />
===设置默认的麦克风/录音设备===<br />
某些程序(Pidgin、Adobe Flash)提供了设置录音设备的选项。如果录音设备不是内置的声卡,而是外接的(如USB摄像头、麦克风),就会有些问题。如果只想更改默认的录音设备、不更改回放设备,需要像下面这样设置 {{ic|~/.asoundrc}}:<br />
<br />
pcm.usb<br />
{<br />
type hw<br />
card U0x46d0x81d<br />
}<br />
<br />
pcm.!default<br />
{<br />
type asym<br />
playback.pcm<br />
{<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
capture.pcm<br />
{<br />
type plug<br />
slave.pcm "usb"<br />
}<br />
}<br />
<br />
把“U0x46d0x81d”替换为你的录音设备的名称。可以通过 {{ic|arecord -L}} 查看所有 ALSA 识别到的录音设备。<br />
<br />
===内置麦克风无效===<br />
首先,请检查 alsamixer,确保所有的录音声道的音量都达到可以录制的程度。如果还是不行,试试修改 /etc/sound.conf 并重载 snd-* 模块。这样就会多出来一个名叫 Capture 的音频控制,可以用它调节内置麦克风的音量。例如,对于 snd-hda-intel 模块,配置如下:<br />
<br />
options snd-hda-intel enable_msi=1<br />
<br />
然后,输入下面的命令重载模块,调整音量并测试。<br />
<br />
{{bc|# rmmod snd-hda-intel && modprobe snd-hda-intel}}<br />
<br />
===Intel 板载声卡无声===<br />
可能由两个相互冲突的驱动模块引起,分别是 {{ic|snd_intel8x0}} 和 {{ic|snd_intel8x0m}}。如果是的话,屏蔽 snd_intel8x0m 即可:<br />
<br />
{{hc|/etc/modprobe.d/modprobe.conf|blacklist snd_intel8x0m}}<br />
<br />
以下方法可能也有效:在 {{ic|alsamixer}} 或 {{ic|amixer}} 把 “External Amplifier” 设置为静音。参见:[http://alsa.opensrc.org/Intel8x0#Dell_Inspiron_8600_.28and_probably_others.29 ALSA wiki 相关条目]。<br />
<br />
也可以试试解除“Mix”的静音。<br />
<br />
===Intel 板载声卡耳机无声===<br />
对于笔记本电脑上的 '''Intel Corporation 82801 I (ICH9 Family) HD Audio Controller''' 声卡,可能需要添加如下内容到 modprobe 配置文件或 sound.conf,以解决耳机无声的问题:<br />
<br />
options snd-hda-intel model=$model<br />
<br />
$model 在下列型号中选择:<br />
<br />
* dell-vostro<br />
* olpc-xo-1_5<br />
* laptop<br />
* dell-m6<br />
* laptop-hpsense<br />
<br />
{{注意|可能需要在配置文件的 "alias" 语句之后添加上述内容。}}<br />
<br />
可以在内核文档中找到所有支持的型号,如[http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.35.y.git;a=blob;f=Documentation/sound/alsa/HD-Audio-Models.txt;h=dc25bb84b83b49665a7ed850e7bf5423d50cd3ba;hb=HEAD 该文]。这是 2.6.35 版本内核的文档,你需要自己查找适用于自己内核版本的文档。<br />
<br />
也可以从[http://www.mjmwired.net/kernel/Documentation/sound/alsa/HD-Audio-Models.txt 这里]获取型号列表。要查看你的声卡芯片名称,输入下列命令(其中的“*”请根据实际情况替换)。注意,有些芯片名称被更改过,因此实际名称与文件中的可能不匹配。<br />
<br />
cat /proc/asound/card*/codec* | grep Codec<br />
<br />
注意,这么做很可能导致所有输入设备(内置和外置麦克风)无法工作,即要么让麦克风正常、要么让耳机正常。如果遇到这一问题,请向 ALSA 开发者报告。<br />
<br />
另外,如果蜂鸣器(pcspkr)出现问题,可以试试使用以下配置修复:<br />
<br />
options snd-hda-intel model=$model enable=1 index=0<br />
<br />
===安装支持 S/PDIF 输出的显卡后无声 ===<br />
检测已加载的相关模块,并记录顺序:<br />
$ cat /proc/asound/modules<br />
0 snd_hda_intel<br />
1 snd_ca0106<br />
<br />
在 {{ic|/etc/modprobe.d/modprobe.conf}} 中禁用不希望使用的显卡的音频解码模块:<br />
# /etc/modprobe.d/modprobe.conf<br />
#<br />
install snd_hda_intel /bin/false<br />
<br />
如果不同设备使用同一模块,那么只能在 BIOS 中禁用其中之一。<br />
<br />
===音效差或卡顿===<br />
如果发现音效特别差,试试在 alsamixer 中调节一下 PCM 音量,到增益(gain)为0的水平。<br />
<br />
若使用 snd-usb-audio 驱动,可以试试在 {{ic|/etc/asound.conf}} 中启用 {{ic|softvol}}。下面是针对第一个声音设备的配置范例:<br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.dsnooper {<br />
type dsnoop<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
channels 2<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.softvol {<br />
type softvol<br />
slave { pcm "dmixer" }<br />
control {<br />
name "Master"<br />
card 0<br />
}<br />
}<br />
ctl.!default {<br />
type hw<br />
card 0<br />
}<br />
ctl.softvol {<br />
type hw<br />
card 0<br />
}<br />
ctl.dmixer {<br />
type hw<br />
card 0<br />
}<br />
<br />
===开始或停止音频播放时出现噪音===<br />
某些驱动模块(如 snd_ac97_codec 和 snd_hda_intel)会在声卡闲置时关闭它以节约用电。声卡启动或关闭时,就会发出刺耳的噪音。在调节音量或开关窗口(KDE4)时也可能发生这种情况。要消除这一问题,需要添加特定模块参数(通过 {{ic|modinfo snd_MY_MODULE}} 查询)关闭该功能。<br />
<br />
例如:对于 snd_hda_intel 模块,要禁用自动节电并解决噪音问题,需在 {{ic|/etc/modprobe.d/modprobe.conf}} 中添加如下内容:<br />
options snd_hda_intel power_save=0<br />
或:<br />
options snd_hda_intel power_save=0 power_save_controller=N<br />
<br />
可以先使用 {{ic|1=modprobe snd_hda_intel power_save=0}} 测试一下效果。<br />
<br />
对于 VIA VT1708S 板载声卡(使用 snd_hda_intel 模块)等一些声卡,即使把 power_save 参数设置为0可能仍会有噪音。还得在 alsamixer 中把“Line”声道激活才能生效,只要激活并调到一个非0的音量即可(1即可,不要太高)。<br />
<br />
内核模块文档:[https://www.kernel.org/doc/Documentation/sound/alsa/powersave.txt]<br />
<br />
对于笔记本,即使在 {{ic|/etc/modprobe.d}} 设置了 {{ic|power_save}} 参数,当切换电池时 pm-utils 仍会将该值重置为1。需要禁用相关的脚步才行(详情参见:[[Pm-utils#Disabling_a_hook]]):<br />
# touch /etc/pm/power.d/intel-audio-powersave<br />
<br />
===S/PDIF 输出无效===<br />
<br />
如果你已经在 alsamixer 中启用相关声道,但声卡的光学/同轴数字输出还是无效,试试以 root 身份运行:<br />
# iecset audio on<br />
<br />
每次开机都需要重新设置。如果嫌麻烦,可以自己写一个 [[Systemd (简体中文)#自己编写 .service 文件|systemd 服务]]。<br />
<br />
===HDMI Output Does Not Work===<br />
If the HDMI output of your motherboard/sound card is not working or stopped working, and have already enabled and unmuted it in alsamixer, try the following.<br />
<br />
Query for Playback Devices:<br />
<br />
$ aplay -l<br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: NVidia [HDA NVidia], device 0: ALC1200 Analog [ALC1200 Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 1: ALC1200 Digital [ALC1200 Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 3: NVIDIA HDMI [NVIDIA HDMI]<br />
Subdevices: 0/1<br />
Subdevice #0: subdevice #0<br />
<br />
Now that we have the info for the HDMI device, try a test, In the example below, 0 is the card number and 3 is the device number.<br />
<br />
$ aplay -D plughw:0,3 /usr/share/sounds/alsa/Front_Center.wav<br />
<br />
If aplay does not output any errors, but still no sound is heared, "reboot" the receiver, monitor or tv set. Since the HDMI interface executes a handshake on connection, it might have noticed before that there was no audio stream embedded, and disabled audio decoding. In particular, if you are using a standalone window manager (don’t know about Gnome or KDE), you may need to have some sound playing ''while'' plugging in the HDMI cable.<br />
<br />
{{Note|If you are using an ATI card and linux 3.0, check your kernel/grub settings, you most likely need to use the ''''radeon.audio'''<nowiki>=</nowiki>'''1'''' boot parameter, or alternatively set up the option in your modprobe configuration files. It's important to note that before trying, is to check the [http://www.x.org/wiki/RadeonFeature#fndef-72849d6d6eb3927d486f50ece74a739042965a6c-25 Radeon Feature Matrix] to see if it's actually supported. At the moment this is written (march 29th, 2013), some chipsets of the [http://www.x.org/wiki/RadeonFeature#Decoder_ring_for_engineering_vs_marketing_names S. Islands] (namely HD7750 through HD7970) doesn't (still) support HDMI Audio.}}<br />
<br />
If the test is successful, create or edit your ~/.asoundrc file to set HDMI as the default audio device.<br />
<br />
{{hc|~/.asoundrc|<br />
pcm.!default {<br />
type hw<br />
card 0<br />
device 3<br />
}<br />
}}<br />
<br />
Or you above config does not work try:<br />
{{hc|~/.asoundrc|<br />
defaults.pcm.card 0<br />
defaults.pcm.device 3<br />
defaults.ctl.card 0<br />
}}<br />
<br />
===HDMI Multi-channel PCM output does not work (Intel) ===<br />
As of Linux 3.1 multi-channel PCM output through HDMI with a Intel card (Intel Eaglelake, IbexPeak/Ironlake,SandyBridge/CougarPoint and IvyBridge/PantherPoint) is not yet supported. Support for it has been recently added and expected to be available in Linux 3.2. To make it work in Linux 3.1 you need to apply the following patches:<br />
<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=76adaa34db407f174dd06370cb60f6029c33b465 drm: support routines for HDMI/DP ELD]<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=e0dac65ed45e72fe34cc7ccc76de0ba220bd38bb drm/i915: pass ELD to HDMI/DP audio driver]<br />
<br />
===HP TX2500===<br />
Add these 2 lines into {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-cmipci mpu_port=0x330 fm_port=0x388<br />
options snd-hda-intel index=0 model=toshiba position_fix=1<br />
<br />
options snd-hda-intel model=hp (works for tx2000cto)<br />
<br />
===Skipping Sound When Playing MP3===<br />
If you have sound skipping when playing MP3 files and you have more then 2 speakers attached to your computer (i.e. > 2 speaker system), run alsamixer and disable the channels for the speakers that you '''DO NOT''' have (i.e. do not enable the sound for the center speaker if you do not have a center speaker.<br />
<br />
===Using a USB Headset and External USB Sound Cards===<br />
If you are using a USB headset with ALSA you can try using {{AUR|asoundconf}} (currently only available from the [[AUR]]) to set the headset as the primary sound output. Before running make sure you have usb audio module enabled ({{ic|modprobe snd-usb-audio}}).<br />
<br />
# asoundconf is-active<br />
# asoundconf list<br />
# asoundconf set-default-card <chosen soundcard><br />
<br />
==== Crackling sound with USB devices ====<br />
If you experience crackling sound on USB devices, you can try tuning the snd-usb-audio for minimal latency.<br />
<br />
Add this to your {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
options snd-usb-audio nrpacks=1<br />
<br />
source: http://alsa.opensrc.org/Usb-audio#Tuning_USB_devices_for_minimal_latencies<br />
<br />
==== Hot-plugging a USB Sound Card ====<br />
In order to automatically make a USB Sound Card the primary output device, when the card is plugged in, you can use the following udev rules (e.g. add the following two lines to {{ic|/etc/udev/rules.d/00-local.rules}} and reboot).<br />
<br />
{{bc|1=KERNEL=="pcmC[D0-9cp]*", ACTION=="add", PROGRAM="/bin/sh -c 'K=%k; K=$${K#pcmC}; K=$${K%%D*}; echo defaults.ctl.card $$K > /etc/asound.conf; echo defaults.pcm.card $$K >>/etc/asound.conf'"<br />
KERNEL=="pcmC[D0-9cp]*", ACTION=="remove", PROGRAM="/bin/sh -c 'echo defaults.ctl.card 0 > /etc/asound.conf; echo defaults.pcm.card 0 >>/etc/asound.conf'"}}<br />
<br />
===Error 'Unknown hardware' Appears After a Kernel Update===<br />
The following messages may be displayed during the start-up ALSA after the kernel update:<br />
Unknown hardware "foo" "bar" ...<br />
Hardware is initialized using a guess method<br />
/usr/sbin/alsactl: set_control:nnnn:failed to obtain info for control #mm (No such file or directory)<br />
<br />
or<br />
Found hardware: "HDA-Intel" "VIA VT1705" "HDA:11064397,18490397,00100000" "0x1849" "0x0397"<br />
Hardware is initialized using a generic method<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #1 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #2 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #25 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #26 (No such file or directory)<br />
<br />
Simply store ALSA mixer settings again (as root):<br />
# alsactl -f /var/lib/alsa/asound.state store<br />
<br />
It may be necessary configure ALSA again with alsamixer<br />
<br />
===HDA Analyzer===<br />
If the mappings to your audio pins(plugs) do not correspond but ALSA works fine, you could try HDA Analyzer -- a pyGTK2 GUI for HD-audio control can be found [http://www.alsa-project.org/main/index.php/HDA_Analyzer at the ALSA wiki].<br />
Try tweaking the Widget Control section of the PIN nodes, to make microphones IN and headphone jacks OUT. Referring to the Config Defaults heading is a good idea.<br />
<br />
NOTE: the script is done by such way that it is incompatible with python3 (which is now shipped with ArchLinux) but tries to use it.<br />
The workaround is: open "run.py", find all occurences of "python" (2 occurences - one on the first line, and the second on the last line) and replace them all by "python2".<br />
<br />
NOTE2: the script requires root acces, but running it via su/sudo is bogus. Run it via {{ic|kdesu}} or {{ic|gksu}}.<br />
<br />
===ALSA with SDL===<br />
If you get no sound via SDL and ALSA cannot be chosen from the application. Try setting the environmental variable SDL_AUDIODRIVER to alsa.<br />
# export SDL_AUDIODRIVER=alsa<br />
<br />
===Low Sound Workaround===<br />
<br />
If you are facing low sound even after maxing out your speakers/headphones, you can give the softvol plugin a try. Add the following to {{ic|/etc/asound.conf}}.<br />
{{bc|<nowiki><br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
<br />
pcm.softvol {<br />
type softvol<br />
slave {<br />
pcm "dmix"<br />
}<br />
control {<br />
name "Pre-Amp"<br />
card 0<br />
}<br />
min_dB -5.0<br />
max_dB 20.0<br />
resolution 6<br />
}<br />
</nowiki>}}<br />
{{note| You will probably have to restart the computer, as restarting the alsa daemon did not load the new configuration for me. Also, if the configuration does not work even after restarting, try changing {{ic|plug}} with {{ic|hw}} in the above configuration.}}<br />
<br />
After the changes are loaded successfully, you will see a {{ic|Pre-Amp}} section in alsamixer. You can adjust the levels there.<br />
{{note|Setting a high value for {{ic|Pre-Amp}} can cause sound distortion, so adjust it according to the level that suits you.}}<br />
<br />
===Popping sound after resuming from suspension===<br />
You might hear a popping sound after resuming the computer from suspension. This can be fixed by editing {{ic|/etc/pm/sleep.d/90alsa}} and removing the line that says {{ic|aplay -d 1 /dev/zero}}<br />
<br />
==配置文件范例==<br />
参见:[[Advanced Linux Sound Architecture/Example Configurations]]。<br />
<br />
==相关阅读==<br />
* [http://www.mjmwired.net/kernel/Documentation/sound/alsa/ALSA-Configuration.txt ALSA 模块高级配置]<br />
* [http://alsa.opensrc.org/Main_Page 非官方的 ALSA Wiki]<br />
* [https://bbs.archlinux.org/viewtopic.php?id=36815 如何从 svn 获取驱动代码并编译]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Advanced_Linux_Sound_Architecture_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=256352
Advanced Linux Sound Architecture (简体中文)
2013-05-08T06:55:52Z
<p>Cuihao: /* S/PDIF Output Does Not Work */</p>
<hr />
<div>[[Category:简体中文]]<br />
[[Category:Sound (简体中文)]]<br />
[[Category:Audio/Video (简体中文)]]<br />
[[cs:Advanced Linux Sound Architecture]]<br />
[[de:Alsa]]<br />
[[es:Advanced Linux Sound Architecture]]<br />
[[fa:ALSA]]<br />
[[fr:Alsa]]<br />
[[he:Advanced Linux Sound Architecture]]<br />
[[it:Advanced Linux Sound Architecture]]<br />
[[ja:Advanced Linux Sound Architecture]]<br />
[[nl:Advanced Linux Sound Architecture]]<br />
[[pt:Advanced Linux Sound Architecture]]<br />
[[ru:Advanced Linux Sound Architecture]]<br />
[[sk:Advanced Linux Sound Architecture]]<br />
[[th:Advanced Linux Sound Architecture]]<br />
[[tr:Gelişmiş_ses_mimarisi_(ALSA)]]<br />
[[en:Advanced Linux Sound Architecture]]<br />
<br />
{{Article summary start}}<br />
{{Article summary text|ALSA 的使用、配置及疑难解答。}}<br />
{{Article summary heading|相关页面}}<br />
{{Article summary wiki|Sound system (简体中文)}}<br />
{{Article summary wiki|Disable PC Speaker Beep (简体中文)}}<br />
{{Article summary end}}<br />
{{TranslationStatus (简体中文)|Advanced_Linux_Sound_Architecture|2013-04-25|254584}}<br />
<br />
[[Wikipedia:zh:ALSA|高级 Linux 声音体系]](Advanced Linux Sound Architecture,'''ALSA''')是Linux中提供声音设备驱动的内核组件,用来代替原来的开放声音系统(Open Sound System,OSSv3)。除了声音设备驱动,'''ALSA'''还包含一个用户空间的函数库,以方便开发者通过高级API使用驱动功能,而不必直接与内核驱动交互。<br />
<br />
{{注意|关于另一种声音体系,请阅读[[Open Sound System (简体中文)|开放声音系统(OSS)]]的页面。}}<br />
<br />
==安装==<br />
Arch 默认的内核已经通过一套模块提供了 ALSA,不必特别安装。<br />
<br />
[[udev (简体中文)|udev]]会在系统启动时自动检测硬件,并加载相应的声音设备驱动模块。这时,你的声卡已经可以工作了,只是所有声道默认都被设置成静音了。<br />
<br />
在本地登录(通过虚拟终端或登录管理器)的用户,都有权限播放音频并调整音量。要让远程登录的用户拥有这些权限,必须把用户[[Users and Groups (简体中文)#用户组管理|加入]] {{ic|audio}} 用户组。该组的成员可以直接访问声音设备,会导致某些程序独占音频输出(破坏软件混音),还可能影响用户快速切换和拖机(multiseat)。因此,除非真的有某些[https://wiki.ubuntu.com/Audio/TheAudioGroup 特殊需求],'''不建议'''把用户加入 {{ic|audio}} 用户组。<br />
<br />
===用户空间工具===<br />
从 [[Official Repositories (简体中文)|官方仓库]]:<br />
* [[pacman (简体中文)|安装]] {{Pkg|alsa-utils}} 软件包,其中的 {{ic|alsamixer}} 工具允许用户在控制台或终端中配置声音设备。<br />
* 安装 {{Pkg|alsa-oss}} 软件包,如果想让 OSS 应用程序与dmix(软件混音)协同工作。 <br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。}}<br />
*安装 {{Pkg|alsa-plugins}} 软件包,如果需要 [[#高质量重采样|高质量重采样]]、[[#Upmixing/Downmixing|软件模拟环绕立体声]]和其他高级特性。<br />
<br />
==解除各声道的静音==<br />
目前版本的 ALSA 安装后,所有声道'''默认是静音的''',必须手动解除。<br />
<br />
使用 {{ic|alsamixer}} 的 ncurses 界面,配置十分简单:<br />
<br />
$ alsamixer<br />
<br />
此外,还可以在命令行下使用 {{ic|amixer}}:<br />
<br />
$ amixer sset Master unmute<br />
<br />
在 {{ic|alsamixer}} 中,下方标有 {{ic|MM}} 的声道是静音的,而标有 {{ic|00}} 的通道已经启用。<br />
<br />
使用左右方向键,选中 {{ic|Master}} 和 {{ic|PCM}} 声道。按下 {{keypress|m}} 键解除静音。使用上方向键增加音量,直到增益值为{{ic|0}}。该值显示在左上方 {{ic|Item:}} 字段后。过高的增益值会导致声音失真。<br />
<br />
要想得到完整的 5.1 或 7.1 环绕立体声,还得解除 Front、Surround、Center、LFE (subwoofer) 和 Side 这些声道的静音(上述名称是 Intel HD Audio 声卡使用的声道名,可能因设备不同而有所差异)。注意,仅有这些设置,系统不会自动将立体声源(多数音乐)提升(upmix)成环绕立体声。如果需要这些功能,请阅读[[#Upmixing/Downmixing]]。<br />
<br />
要启用麦克风,切换至 Capture 选项卡,按下 {{keypress|F4}},按下 {{keypress|空格}} 启用其中一个声道即可。<br />
<br />
按下 {{Keypress|Esc}} 键退出 alsamixer。<br />
<br />
{{注意|<br />
* 有一些声卡,需要关闭数字输出或将其调成静音,才能输出模拟音频信号。对于 Soundblaster Audigy LS 声卡,需要把 IEC958 通道设成静音。 <br />
* 有些机器(如 Thinkpad T61),有一个 Speaker 通道,必须按照上述方法打开调整。<br />
* 有些机器(如 Dell E6400),可能还需要把 {{ic|Front}} 和 {{ic|Headphone}} 声道打开调整。}}<br />
<br />
接下来,测试声卡是否工作:<br />
<br />
$ speaker-test -c 2<br />
<br />
根据扬声器的实际情况,调整 -c 后的数值。对于 7.1 声道,这个数字是 8:<br />
<br />
$ speaker-test -c 8<br />
<br />
如果没有正常工作,请继续阅读 [[#配置]] 以及 [[#疑难解答]] 部分。<br />
<br />
{{pkg|alsa-utils}} 软件包提供了两个配置 alsa 的服务:{{ic|alsa-restore.service}} 和 {{ic|alsa-store.service}}。它们分别在开机和关机时自动运行。<br />
<br />
==配置==<br />
=== VirtualBox 虚拟机无声音 ===<br />
<br />
如果使用 VirtualBox 时碰上了麻烦,请尝试以下命令:<br />
<br />
{{hc|$ alsactl init|2=<nowiki><br />
<br />
Found hardware: "ICH" "SigmaTel STAC9700,83,84" "AC97a:83847600" "0x8086" "0x0000"<br />
Hardware is initialized using a generic method<br />
</nowiki><br />
}}<br />
<br />
如果还是不行,请检查所使用的音频软件是否激活了 ALSA 输出。<br />
<br />
===设置默认声卡===<br />
<br />
如果发现开机时声卡次序会发生变化,可以在通过 {{ic|/etc/modprobe.d}} 中的 {{ic|.conf}} 文件(比如 {{ic|/etc/modprobe.d/alsa-base.conf}})手动设置次序。<br />
比如,要让 mia 声卡成为 #0、Intel HDA 声卡成为 #1:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<br />
options snd slots=snd_mia,snd_hda_intel<br />
options snd_mia index=0<br />
options snd_hda_intel index=1<br />
}}<br />
<br />
使用 {{ic|<nowiki>lsmod | grep snd</nowiki>}} 获取设备列表。<br />
<br />
snd_mia 和 snd_hda_intel 分别是两个声卡的内核驱动模块。该配置仅适用于同时装有 mia 声卡和使用 snd_hda_intel 驱动的声卡(通常是板载声卡)的情况。<br />
<br />
如果使用 -2 的 index 值,ALSA 就不会将对应的设备作为主声卡使用。Linux Mint 和 Ubuntu 等发行版使用了以下配置,避免 USB 声卡和其他“非主流”设备变成 index 为 0 的主声卡:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<br />
options bt87x index=-2<br />
options cx88_alsa index=-2<br />
options saa7134-alsa index=-2<br />
options snd-atiixp-modem index=-2<br />
options snd-intel8x0m index=-2<br />
options snd-via82xx-modem index=-2<br />
options snd-usb-audio index=-2<br />
options snd-usb-caiaq index=-2<br />
options snd-usb-ua101 index=-2<br />
options snd-usb-us122l index=-2<br />
options snd-usb-usx2y index=-2<br />
# Keep snd-pcsp from being loaded as first soundcard<br />
options snd-pcsp index=-2<br />
# Keep snd-usb-audio from beeing loaded as first soundcard<br />
options snd-usb-audio index=-2<br />
}}<br />
<br />
以上配置会在系统重启后生效。<br />
<br />
{{Out of date|据英文 Wiki 用户反应,{{ic|/etc/asound.conf}} 这个配置文件默认是不存在的,下列操作可能有误。}}<br />
<br />
首先,运行 {{ic|aplay -l}},获取声卡的声卡ID和设备ID:<br />
<br />
{{hc|$ aplay -l|2=<nowiki><br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: Intel [HDA Intel], device 0: CONEXANT Analog [CONEXANT Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: Intel [HDA Intel], device 1: Conexant Digital [Conexant Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 1: JamLab [JamLab], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 2: Audio [Altec Lansing XT1 - USB Audio], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
</nowiki>}}<br />
<br />
拿输出中的最后一项来说,这个声卡的声卡ID为2,设备ID为0。若要把这块声卡作为默认声卡,可以把下列配置添加到系统级别的 {{ic|/etc/asound.conf}} 或用户级别的 {{ic|~/.asoundrc}} 文件。如果文件不存在,可以手动创建。其中的各个ID,请根据实际情况调整:<br />
{{bc|<br />
defaults.pcm.card 2<br />
defaults.pcm.device 0<br />
defaults.ctl.card 2<br />
}}<br />
“pcm”选项决定用来播放音频的设备,而“ctl”选项决定那个声卡能够由控制工具(如 alsamixer)使用。<br />
<br />
上述配置在重启音频程序(如 mplayer)后立即生效。<br />
<br />
===确认所有声音模块都已经加载===<br />
<br />
一般 udev 都会自动识别出声卡。使用以下命令确认:<br />
<br />
{{hc|<nowiki>$ lsmod | grep '^snd' | column -t</nowiki>|<nowiki><br />
snd_hda_codec_hdmi 22378 4<br />
snd_hda_codec_realtek 294191 1<br />
snd_hda_intel 21738 1<br />
snd_hda_codec 73739 3 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel<br />
snd_hwdep 6134 1 snd_hda_codec<br />
snd_pcm 71032 3 snd_hda_codec_hdmi,snd_hda_intel,snd_hda_codec<br />
snd_timer 18992 1 snd_pcm<br />
snd 55132 9 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel,snd_hda_codec,snd_hwdep,snd_pcm,snd_timer<br />
snd_page_alloc 7017 2 snd_hda_intel,snd_pcm<br />
</nowiki>}}<br />
<br />
如果你的输出和上面类似,那就说明声卡已经被正确识别。<br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。如有需要,请[[Kernel_modules_(简体中文)#装入|手动加载]]。}}<br />
<br />
还可以检查一下 {{ic|/dev/snd/}} 目录,看看是否有这些设备文件:<br />
<br />
{{hc|$ ls -l /dev/snd|2=<nowiki><br />
total 0<br />
crw-rw---- 1 root audio 116, 0 Apr 8 14:17 controlC0<br />
crw-rw---- 1 root audio 116, 32 Apr 8 14:17 controlC1<br />
crw-rw---- 1 root audio 116, 24 Apr 8 14:17 pcmC0D0c<br />
crw-rw---- 1 root audio 116, 16 Apr 8 14:17 pcmC0D0p<br />
crw-rw---- 1 root audio 116, 25 Apr 8 14:17 pcmC0D1c<br />
crw-rw---- 1 root audio 116, 56 Apr 8 14:17 pcmC1D0c<br />
crw-rw---- 1 root audio 116, 48 Apr 8 14:17 pcmC1D0p<br />
crw-rw---- 1 root audio 116, 1 Apr 8 14:17 seq<br />
crw-rw---- 1 root audio 116, 33 Apr 8 14:17 timer<br />
</nowiki>}}<br />
<br />
{{注意|在 IRC 或论坛寻求这方面帮助时,别忘了贴出上面几个命令的输出。}}<br />
<br />
如果你的输出跟上面类似,或至少有 '''controlC0''' 和 '''pcmC0D0p''',那么声卡就已经正常加载了。<br />
<br />
如果出现问题,声卡模块没有正确加载,那么请尝试手动加载模块:<br />
<br />
* 确定声卡对应的驱动模块:[http://www.alsa-project.org/main/index.php/Matrix:Main ALSA Soundcard Matrix]。这些模块都会有一个”snd-“前缀(例如:{{ic|snd-via82xx}})。<br />
* [[Kernel_modules_(简体中文)#装入|加载模块]]。<br />
* 检查 {{ic|/dev/snd}} 目录中的设备文件(参见上文);或者,检查 {{ic|alsamixer}} 或 {{ic|amixer}} 的输出是否正确。<br />
* 修改配置,使 {{ic|snd-<模块名>}} 和 {{ic|snd-pcm-oss}} 模块[[Kernel_modules_(简体中文)#装入|开机自动加载]]。<br />
<br />
===启用 SPDIF 输出===<br />
<br />
(由 Gentoo 论坛的 gralves 提供)<br />
* 在 GNOME 音量控制的选项(Options)标签中,把 IEC958 改成 PCM。该选项可以在首选项中启用。<br />
* 不使用 GNOME 音量控制工具的话:<br />
** 编辑 {{ic|/var/lib/alsa/asound.state}}。该文件是 alsasound 保存混音器设置的地方。<br />
** 找到这一行:“IEC958 Playback Switch”。该行上下应该有一行内容为 {{ic|value:false}},将其改为 {{ic|value:true}}。<br />
** 再找到这一行:”IEC958 Playback AC97-SPSA“。把对应的值改为 0。<br />
** 重启 ALSA。<br />
<br />
{{Out of date|rc.local 在 systemd 中已经弃用,以下方法可能不再适用。}}<br />
<br />
还有另外一个开机自动启用 SPDIF 输出的方法(在 SoundBlaster Audigy 声卡上测试过):<br />
<br />
* 在 {{ic|/etc/rc.local}} 中添加如下内容:<br />
# Use COAX-digital output<br />
amixer set 'IEC958 Optical' 100 unmute<br />
amixer set 'Audigy Analog/Digital Output Jack' on<br />
<br />
使用以下命令,获取你的声卡的数字输出的名称:<br />
$ amixer scontrols<br />
<br />
===系统级均衡器===<br />
====使用 AlsaEqual(包含界面)====<br />
从 [[AUR (简体中文)|AUR]] 安装 {{AUR|alsaequal}}。<br />
<br />
{{注意|如果使用的是64位系统,而又安装了32位的 Flash 插件,这里的设置会导致 Flash 无声。如果发生了这种情况,请禁用 alsaequal 或编译32位版本的 alsaequal。}}<br />
<br />
安装后,把下列内容添加到 ALSA 配置文件({{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}}):<br />
<br />
ctl.equal {<br />
type equal;<br />
}<br />
<br />
pcm.plugequal {<br />
type equal;<br />
# Modify the line below if you do not<br />
#slave.pcm "plughw:0,0";<br />
#by default we want to play from more sources at time:<br />
slave.pcm "plug:dmix";<br />
}<br />
#pcm.equal {<br />
# If you do not want the equalizer to be your<br />
# default soundcard comment the following<br />
# line and uncomment the above line. (You can<br />
# choose it as the output device by addressing<br />
# it with specific apps,eg mpg123 -a equal 06.Back_In_Black.mp3)<br />
pcm.!default {<br />
type plug;<br />
slave.pcm plugequal;<br />
}<br />
<br />
设置好后,切换均衡器:<br />
$ alsamixer -D equal<br />
<br />
需要注意,每个用户的 alsaequal 配置文件都是独立的,位于 {{ic|~/.alsaequal.bin}}。因此,如果想在 [[mpd]] 之类的以独立用户身份运行的程序中使用 AlsaEqual,就得像下面这样:<br />
# su mpd -c 'alsamixer -D equal'<br />
或者,在其他用户的主目录下创建符号链接,指向你的 {{ic|.alsaequal.bin}}……<br />
<br />
=====管理 AlsaEqual 配置=====<br />
从 [http://xyne.archlinux.ca/repos/ Xyne 的软件仓库] 或 [https://aur.archlinux.org/packages.php?ID=62420 AUR] 安装 [http://xyne.archlinux.ca/projects/alsaequal-mgr/ alsaequal-mgr]。<br />
<br />
同上,配置均衡器: <br />
$alsamixer -D equal<br />
<br />
如果对效果感到满意,将其命名(如“foo”)并保存该配置:<br />
$alsaequal-mgr save foo<br />
<br />
重新加载“foo”中的配置:<br />
$alsaequal-mgr load foo<br />
<br />
这样,就可以为游戏、电影、不同音乐流派、网络电话等等创建不同的均衡器配置,以便随时切换。<br />
<br />
参见[http://xyne.archlinux.ca/projects/alsaequal-mgr/ 项目主页]和程序的帮助信息,了解更多设置。<br />
<br />
====使用 mbeq====<br />
{{注意|该软件用到了一个 ladspa 插件,可能导致播放音频时 CPU 占用率偏高。In addition, this was made with stereophonic sound (e.g. headphones) in mind.}}<br />
<br />
安装 {{Pkg|alsa-plugins}}、{{Pkg|ladspa}} 和 {{Pkg|swh-plugins}} 软件包。<br />
<br />
* 在 {{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}} 中添加如下内容:<br />
<br />
{{hc|/etc/asound.conf|2=<nowiki>pcm.eq {<br />
type ladspa<br />
<br />
# The output from the EQ can either go direct to a hardware device<br />
# (if you have a hardware mixer, e.g. SBLive/Audigy) or it can go<br />
# to the software mixer shown here.<br />
#slave.pcm "plughw:0,0"<br />
slave.pcm "plug:dmix"<br />
<br />
# Sometimes you may need to specify the path to the plugins,<br />
# especially if you have just installed them. Once you have logged<br />
# out/restarted this should not be necessary, but if you get errors<br />
# about being unable to find plugins, try uncommenting this.<br />
#path "/usr/lib/ladspa"<br />
<br />
plugins [<br />
{<br />
label mbeq<br />
id 1197<br />
input {<br />
#this setting is here by example, edit to your own taste<br />
#bands: 50hz, 100hz, 156hz, 220hz, 311hz, 440hz, 622hz, 880hz, 1250hz, 1750hz, 25000hz,<br />
#50000hz, 10000hz, 20000hz<br />
controls [ -5 -5 -5 -5 -5 -10 -20 -15 -10 -10 -10 -10 -10 -3 -2 ]<br />
}<br />
}<br />
]<br />
}<br />
<br />
# Redirect the default device to go via the EQ - you may want to do<br />
# this last, once you are sure everything is working. Otherwise all<br />
# your audio programs will break/crash if something has gone wrong.<br />
<br />
pcm.!default {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
<br />
# Redirect the OSS emulation through the EQ too (when programs are running through "aoss")<br />
<br />
pcm.dsp0 {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
</nowiki>}}<br />
<br />
* 完成。若有问题,欢迎前往论坛咨询。<br />
<br />
==高质量重采样==<br />
启用软件混音时,ALSA 会强制把所有音频重采样到相同的频率(如果系统支持,默认是 48000)。dmix 的重采样算法很糟糕,会导致显著的质量损失。<br />
<br />
安装 {{pkg|alsa-plugins}} 和 {{pkg|libsamplerate}}。<br />
<br />
将默认的采样率转换器更改为 libsamplerate:<br />
<br />
{{hc|/etc/asound.conf|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
或:<br />
<br />
{{hc|~/.asoundrc|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
'''samplerate_best''' 输出的声音质量最佳,但需要耗费大量CPU资源进行实时重采样。还有其他的算法可供选择(如 '''samplerate''' 等),但比起默认的算法,这些算法的改进并不明显。<br />
<br />
{{警告|某些系统中,启用 samplerate_best 可能导致 Flash 播放器无声音。}}<br />
<br />
==上混和下混==<br />
{{注意|由于未找到 upmix 和 downmix 的常用翻译,译者暂时采用字面翻译(上混、下混)。所谓“上混”,即将声道较少的立体声(如常见的双声道立体声)信号,通过软件或硬件处理,模拟为声道较多的立体声(如5.1环绕立体声)信号。反之即为“下混”。}}<br />
<br />
===上混(upmixing)===<br />
播放双声道立体声音源(如音乐)时,利用上混,即可充分利用 5.1 或 7.1 环绕立体声的所有声道。以前,上混很麻烦,还经常出错。但如今就方便多了,只需要安装一些插件即可。这些插件由 {{Pkg|alsa-plugins}} 软件包提供。<br />
<br />
安装后,在配置文件({{ic|/etc/asound.conf}} 或 {{ic|~/.asoundrc}})中添加如下内容:<br />
pcm.upmix71 {<br />
type upmix<br />
slave.pcm "surround71"<br />
delay 15<br />
channels 8<br />
}<br />
<br />
该范例适用于7.1声道上混。类比该范例,同样能设置5.1或4.0声道上混。<br />
<br />
该设置添加了一个新的用于上混的 pcm。如果希望所有音频源都通过该 pcm 输出,在上述设置后添加如下配置即可:<br />
<br />
pcm.!default "plug:upmix71"<br />
<br />
插件默认配置就允许多个音频源同时通过它输出,一般无需额外配置。如果不行,就得向下面这样为上混配置 dmixer:<br />
pcm.dmix6 {<br />
type asym<br />
playback.pcm {<br />
type dmix<br />
ipc_key 567829<br />
slave {<br />
pcm "hw:0,0"<br />
channels 6<br />
}<br />
}<br />
}<br />
<br />
注意要用 dmix6 而非 surround71。<br />
<br />
如果遇到声音卡顿或混乱,可能需要增加 buffer_size(比如增加到 32768),或使用[[#高质量重采样|高质量重采样]]。<br />
<br />
===下混(downmixing)===<br />
有时也会用到下混,比如在只支持双声道立体声的电脑上观看5.1声道的电影时。这一功能由 ALSA 插件 vdownmix 实现(同样在 {{Pkg|alsa-plugins}} 软件包里)。<br />
<br />
在配置文件中添加如下内容:<br />
<br />
pcm.!surround51 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
pcm.!surround40 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
<br />
==混音==<br />
===软件混音(dmix)===<br />
<br />
{{注意|在 ALSA 1.0.9rc2 及以上版本中,如果声卡不支持硬件混音,模拟输出将默认使用 dmix 混音,无需额外设置。}}<br />
<br />
如果软件混音未正常启用,那么可以手动创建 {{ic|~/.asoundrc}},加入以下内容:<br />
<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
<br />
这样就启用了软件混音,多个程序能够同时使用声卡。<br />
<br />
至于 S/PDIF 之类的数字音频输出,目前的 ALSA 还不会默认启用 dmix。因此,必须像上面这样手动启用。<br />
<br />
常见问题的解答,参见[[#疑难解答]]部分。<br />
<br />
===硬件混音===<br />
====支持====<br />
如果声卡支持,一般无需任何配置即可启用硬件混音。绝大多数板载声卡都不支持硬件混音,必须得使用软件混音(见上文)。很多独立声卡都支持硬件混音,Linux 下支持较好的有:<br />
<br />
* Creative SoundBlaster Live! (5.1 型号)<br />
* Creative SoundBlaster Audigy (部分型号)<br />
* Creative SoundBlaster Audidy 2 (ZS 系列型号)<br />
* Creative SoundBlaster Audigy 4 (Pro 系列型号)<br />
<br />
{{注意|上述声卡的低端版本(Audigy SE、Audigy 2 NX、SoundBlaster Live! 24bit 以及 SoundBlaster Live! 7.1)使用了不同的芯片,'''不支持'''硬件混音。}}<br />
<br />
{{注意|板载 VIA8237 芯片支持4路硬件混音,但在某些主板上只能使用3路(第4路无声),甚至根本无法使用。即使能正常工作,质量也比不上其他混音方法。}}<br />
<br />
====问题修复====<br />
如果你的声卡是 Intel Corporation 82801I (ICH9 Family) HD Audio Controller (rev 02),且使用了64位系统,需要执行以下命令才能在 Enemy Territory 中听到声音:<br />
echo "et.x86 0 0 direct" > /proc/asound/card0/pcm0p/oss<br />
echo "et.x86 0 0 disable" > /proc/asound/card0/pcm0c/oss<br />
<br />
==疑难解答==<br />
===使用CPU频率动态调整时,音频发生跳跃===<br />
使用 {{ic|ondemand}} 或 {{ic|conservative}} 频率策略、启用CPU频率动态调整时,某些声卡和某些 ALSA 驱动会出现声音跳跃的情况。目前,唯一的解决方案是使用固定的频率,如 {{ic|performance}} 性能策略。<br />
<br />
详情参见 [[CPU Frequency Scaling (简体中文)]]。<br />
<br />
===同时只能有一个用户播放音频===<br />
有时会出现一个用户独占软件混音器的情况。多数情况下这没什么影响,但在使用 [[mpd]] 这样以独立用户身份运行的程序时,就会出现问题。当 mpd 运行时,用户自己就无法播放其他音频了。一种解决方法是,以正在使用的用户身份运行 mpd。另一种方法是,在 ALSA 配置文件的 {{ic|pcm.dmixer}} 部分添加 {{ic|ipc_key_add_uid 0}} 禁止独占。以下是 {{ic|asound.conf}} 示例的片段,省略的部分无需修改:<br />
<br />
...<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
slave {<br />
...<br />
<br />
===无法同时播放多个音频===<br />
如果安装了 [[PulseAudio (简体中文)|PulseAudio]](可能作为 [[GNOME (简体中文)|GNOME]] 的依赖被安装),可能出现无法同步播放的问题,因为 PulseAudio 的默认配置是“独占”声卡。ALSA 的用户可能对自己的配置已经很满意、不希望使用 [[PulseAudio (简体中文)|PulseAudio]]。解决方案是编辑 {{ic|/etc/asound.conf}} 并注释掉以下内容:<br />
# Use PulseAudio by default<br />
#pcm.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
# hint {<br />
# show on<br />
# description "Default ALSA Output (currently PulseAudio Sound Server)"<br />
# }<br />
#}<br />
<br />
可能还需要注释掉下面的内容:<br />
#ctl.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
#}<br />
<br />
比起完全卸载 [[PulseAudio (简体中文)|PulseAudio]],这大概是个更简单的方法。<br />
<br />
以下是 {{ic|/etc/asound.conf}} 配置文件的一个示例:<br />
{{bc|<nowiki>pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
}<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}</nowiki><br />
}}<br />
<br />
{{注意|本 {{ic|/etc/asound.conf}} 本来是配合 [[MPD]] 全局配置使用的。参见[[#同时只能有一个用户播放音频|该节]]关于多用户的内容。}}<br />
<br />
===有时候开机无声===<br />
如果会随机地出现开机无声的问题,原因可能是多声卡的顺序发生了变化。如果是这种情况,请参照[[#设置默认声卡]]进行设置。<br />
<br />
对于 mpd 用户,上述配置无效,请参考[http://mpd.wikia.com/wiki/Configuration#ALSA_MPD_software_volume_control 该文]设置。<br />
<br />
===特定程序的问题===<br />
对于不使用系统声音配置的程序,如 XMMS 和 Mplayer,必须特别设置以应用 ALSA 配置。<br />
<br />
对于 mplayer,将以下内容添加到 {{ic|~/.mplayer/config}} (或系统全局配置文件 {{ic|/etc/mplayer/mplayer.conf}}):<br />
ao=alsa<br />
<br />
对于 XMMS 和 Beep Media Player,打开选项窗口,要把声音驱动设置为 ALSA 而不是 OSS。<br />
<br />
XMMS 的设置方法:<br />
* 启动 XMMS<br />
** 选项 -> 首选项<br />
** 选择 Alsa 输出插件<br />
<br />
对于未提供 ALSA 输出的程序,可以使用 alsa-oss 软件包中的 aoss 工具启动。使用方法是:在要运行的程序的命令行前加上 {{ic|aoss}}。例如:<br />
aoss realplay<br />
<br />
pcm.!default{ ... } 貌似已经不管用了。但下面的设置有效:<br />
pcm.default pcm.dmixer<br />
<br />
===特定型号的配置===<br />
ALSA 会通过 BIOS 检测[http://www.kernel.org/doc/Documentation/sound/alsa/HD-Audio-Models.txt 声卡型号],少数情况下也可能无法正确识别。声卡芯片类型可以在 {{ic|alsamixer}} 界面上找到(如 ALC662),而型号则要设置在 {{ic|/etc/modprobe.d/modprobe.conf}} 或 {{ic|/etc/modprobe.d/sound.conf}} 文件中:<br />
<br />
options snd-hda-intel model=<型号><br />
<br />
具体设置因声卡型号而异。多数情况下不需要特地这样做。如果想知道具体声卡型号的设置,请查阅 [http://bugtrack.alsa-project.org/main/index.php/Matrix:Main Alsa Soundcard List],找到你的声卡型号,点击“Details”,然后查看“Setting up modprobe...”小节,将其中的内容插入 {{ic|/etc/modprobe.d/modprobe.conf}} 文件即可。例如,Intel AC97 的配置如下:<br />
<br />
{{bc|# ALSA portion<br />
alias char-major-116 snd<br />
alias snd-card-0 snd-intel8x0<br />
# module options should go here<br />
<br />
# OSS/Free portion<br />
alias char-major-14 soundcore<br />
alias sound-slot-0 snd-card-0<br />
<br />
# card #1<br />
alias sound-service-0-0 snd-mixer-oss<br />
alias sound-service-0-1 snd-seq-oss<br />
alias sound-service-0-3 snd-pcm-oss<br />
alias sound-service-0-8 snd-seq-oss<br />
alias sound-service-0-12 snd-pcm-oss}}<br />
<br />
===与蜂鸣器冲突===<br />
如果你确认所有声道都已解除静音,声卡驱动配置正确,且音量大小也合适,但还是听不到声音,那么请试试将下面的内容添加到 {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
(如果是 {{ic|via82xx}} 驱动)<br />
options snd-<模块名请自行补充> ac97_quirk=0<br />
<br />
(如果是 {{ic|snd_intel8x0}} 驱动)<br />
options snd-NAME-OF-MODULE ac97_quirk=1<br />
<br />
===麦克风输入无声音===<br />
In alsamixer, make sure that all the volume levels are up under recording, and that CAPTURE is toggled active on the microphone (e.g. Mic, Internal Mic) and/or on Capture (in alsamixer, select these items and press space). Try making positive Mic Boost and raising Capture and Digital levels higher; this make make static or distortion, but then you can adjust them back down once you are hearing ''something'' when you record<br />
<br />
As the pulseaudio wrapper is shown as "default" in alsamixer, you may have to press F6 to select your actual soundcard first. You may also need to enable and increase the volume of Line-in in the Playback section.<br />
<br />
To test the microphone, run these commands (see arecord's man page for further information):<br />
arecord -d 5 test-mic.wav<br />
aplay test-mic.wav<br />
<br />
If all fails, you may want to eliminate hardware failure by testing the microphone with a different device.<br />
<br />
For at least some computers, muting a microphone (MM) simply means its input does not go immediately to the speakers. It still receives input.<br />
<br />
Many Dell laptops need "-dmic" to be appended to the model name in {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-hda-intel model=dell-m6-dmic<br />
<br />
Some programs use try to use OSS as the main input software. Add the following lines to {{ic|/etc/modprobe.d/modprobe.conf}} to prevent OSS modules from being loaded:<br />
<br />
{{Note|The OSS modules are no longer autoloaded anyway.}}<br />
<br />
blacklist snd_pcm_oss<br />
blacklist snd_mixer_oss<br />
blacklist snd_seq_oss<br />
<br />
See also:<br />
* http://www.alsa-project.org/main/index.php/SoundcardTesting<br />
* http://alsa.opensrc.org/Record_from_mic<br />
<br />
===设置默认的麦克风/录音设备===<br />
某些程序(Pidgin、Adobe Flash)提供了设置录音设备的选项。如果录音设备不是内置的声卡,而是外接的(如USB摄像头、麦克风),就会有些问题。如果只想更改默认的录音设备、不更改回放设备,需要像下面这样设置 {{ic|~/.asoundrc}}:<br />
<br />
pcm.usb<br />
{<br />
type hw<br />
card U0x46d0x81d<br />
}<br />
<br />
pcm.!default<br />
{<br />
type asym<br />
playback.pcm<br />
{<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
capture.pcm<br />
{<br />
type plug<br />
slave.pcm "usb"<br />
}<br />
}<br />
<br />
把“U0x46d0x81d”替换为你的录音设备的名称。可以通过 {{ic|arecord -L}} 查看所有 ALSA 识别到的录音设备。<br />
<br />
===内置麦克风无效===<br />
首先,请检查 alsamixer,确保所有的录音声道的音量都达到可以录制的程度。如果还是不行,试试修改 /etc/sound.conf 并重载 snd-* 模块。这样就会多出来一个名叫 Capture 的音频控制,可以用它调节内置麦克风的音量。例如,对于 snd-hda-intel 模块,配置如下:<br />
<br />
options snd-hda-intel enable_msi=1<br />
<br />
然后,输入下面的命令重载模块,调整音量并测试。<br />
<br />
{{bc|# rmmod snd-hda-intel && modprobe snd-hda-intel}}<br />
<br />
===Intel 板载声卡无声===<br />
可能由两个相互冲突的驱动模块引起,分别是 {{ic|snd_intel8x0}} 和 {{ic|snd_intel8x0m}}。如果是的话,屏蔽 snd_intel8x0m 即可:<br />
<br />
{{hc|/etc/modprobe.d/modprobe.conf|blacklist snd_intel8x0m}}<br />
<br />
以下方法可能也有效:在 {{ic|alsamixer}} 或 {{ic|amixer}} 把 “External Amplifier” 设置为静音。参见:[http://alsa.opensrc.org/Intel8x0#Dell_Inspiron_8600_.28and_probably_others.29 ALSA wiki 相关条目]。<br />
<br />
也可以试试解除“Mix”的静音。<br />
<br />
===Intel 板载声卡耳机无声===<br />
对于笔记本电脑上的 '''Intel Corporation 82801 I (ICH9 Family) HD Audio Controller''' 声卡,可能需要添加如下内容到 modprobe 配置文件或 sound.conf,以解决耳机无声的问题:<br />
<br />
options snd-hda-intel model=$model<br />
<br />
$model 在下列型号中选择:<br />
<br />
* dell-vostro<br />
* olpc-xo-1_5<br />
* laptop<br />
* dell-m6<br />
* laptop-hpsense<br />
<br />
{{注意|可能需要在配置文件的 "alias" 语句之后添加上述内容。}}<br />
<br />
可以在内核文档中找到所有支持的型号,如[http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.35.y.git;a=blob;f=Documentation/sound/alsa/HD-Audio-Models.txt;h=dc25bb84b83b49665a7ed850e7bf5423d50cd3ba;hb=HEAD 该文]。这是 2.6.35 版本内核的文档,你需要自己查找适用于自己内核版本的文档。<br />
<br />
也可以从[http://www.mjmwired.net/kernel/Documentation/sound/alsa/HD-Audio-Models.txt 这里]获取型号列表。要查看你的声卡芯片名称,输入下列命令(其中的“*”请根据实际情况替换)。注意,有些芯片名称被更改过,因此实际名称与文件中的可能不匹配。<br />
<br />
cat /proc/asound/card*/codec* | grep Codec<br />
<br />
注意,这么做很可能导致所有输入设备(内置和外置麦克风)无法工作,即要么让麦克风正常、要么让耳机正常。如果遇到这一问题,请向 ALSA 开发者报告。<br />
<br />
另外,如果蜂鸣器(pcspkr)出现问题,可以试试使用以下配置修复:<br />
<br />
options snd-hda-intel model=$model enable=1 index=0<br />
<br />
===安装支持 S/PDIF 输出的显卡后无声 ===<br />
检测已加载的相关模块,并记录顺序:<br />
$ cat /proc/asound/modules<br />
0 snd_hda_intel<br />
1 snd_ca0106<br />
<br />
在 {{ic|/etc/modprobe.d/modprobe.conf}} 中禁用不希望使用的显卡的音频解码模块:<br />
# /etc/modprobe.d/modprobe.conf<br />
#<br />
install snd_hda_intel /bin/false<br />
<br />
如果不同设备使用同一模块,那么只能在 BIOS 中禁用其中之一。<br />
<br />
===音效差或卡顿===<br />
如果发现音效特别差,试试在 alsamixer 中调节一下 PCM 音量,到增益(gain)为0的水平。<br />
<br />
若使用 snd-usb-audio 驱动,可以试试在 {{ic|/etc/asound.conf}} 中启用 {{ic|softvol}}。下面是针对第一个声音设备的配置范例:<br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.dsnooper {<br />
type dsnoop<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
channels 2<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.softvol {<br />
type softvol<br />
slave { pcm "dmixer" }<br />
control {<br />
name "Master"<br />
card 0<br />
}<br />
}<br />
ctl.!default {<br />
type hw<br />
card 0<br />
}<br />
ctl.softvol {<br />
type hw<br />
card 0<br />
}<br />
ctl.dmixer {<br />
type hw<br />
card 0<br />
}<br />
<br />
===开始或停止音频播放时出现噪音===<br />
某些驱动模块(如 snd_ac97_codec 和 snd_hda_intel)会在声卡闲置时关闭它以节约用电。声卡启动或关闭时,就会发出刺耳的噪音。在调节音量或开关窗口(KDE4)时也可能发生这种情况。要消除这一问题,需要添加特定模块参数(通过 {{ic|modinfo snd_MY_MODULE}} 查询)关闭该功能。<br />
<br />
例如:对于 snd_hda_intel 模块,要禁用自动节电并解决噪音问题,需在 {{ic|/etc/modprobe.d/modprobe.conf}} 中添加如下内容:<br />
options snd_hda_intel power_save=0<br />
或:<br />
options snd_hda_intel power_save=0 power_save_controller=N<br />
<br />
可以先使用 {{ic|1=modprobe snd_hda_intel power_save=0}} 测试一下效果。<br />
<br />
对于 VIA VT1708S 板载声卡(使用 snd_hda_intel 模块)等一些声卡,即使把 power_save 参数设置为0可能仍会有噪音。还得在 alsamixer 中把“Line”声道激活才能生效,只要激活并调到一个非0的音量即可(1即可,不要太高)。<br />
<br />
内核模块文档:[https://www.kernel.org/doc/Documentation/sound/alsa/powersave.txt]<br />
<br />
对于笔记本,即使在 {{ic|/etc/modprobe.d}} 设置了 {{ic|power_save}} 参数,当切换电池时 pm-utils 仍会将该值重置为1。需要禁用相关的脚步才行(详情参见:[[Pm-utils#Disabling_a_hook]]):<br />
# touch /etc/pm/power.d/intel-audio-powersave<br />
<br />
===S/PDIF 输出无效===<br />
<br />
如果你已经在 alsamixer 中启用相关声道,但声卡的光学/同轴数字输出还是无效,试试以 root 身份运行:<br />
# iecset audio on<br />
<br />
每次开机都需要重新设置。如果嫌麻烦,可以自己写一个 [https://wiki.archlinux.org/index.php/Systemd_%28%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87%29#.E8.87.AA.E5.B7.B1.E7.BC.96.E5.86.99_.service_.E6.96.87.E4.BB.B6 systemd 服务]。<br />
<br />
===HDMI Output Does Not Work===<br />
If the HDMI output of your motherboard/sound card is not working or stopped working, and have already enabled and unmuted it in alsamixer, try the following.<br />
<br />
Query for Playback Devices:<br />
<br />
$ aplay -l<br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: NVidia [HDA NVidia], device 0: ALC1200 Analog [ALC1200 Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 1: ALC1200 Digital [ALC1200 Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 3: NVIDIA HDMI [NVIDIA HDMI]<br />
Subdevices: 0/1<br />
Subdevice #0: subdevice #0<br />
<br />
Now that we have the info for the HDMI device, try a test, In the example below, 0 is the card number and 3 is the device number.<br />
<br />
$ aplay -D plughw:0,3 /usr/share/sounds/alsa/Front_Center.wav<br />
<br />
If aplay does not output any errors, but still no sound is heared, "reboot" the receiver, monitor or tv set. Since the HDMI interface executes a handshake on connection, it might have noticed before that there was no audio stream embedded, and disabled audio decoding. In particular, if you are using a standalone window manager (don’t know about Gnome or KDE), you may need to have some sound playing ''while'' plugging in the HDMI cable.<br />
<br />
{{Note|If you are using an ATI card and linux 3.0, check your kernel/grub settings, you most likely need to use the ''''radeon.audio'''<nowiki>=</nowiki>'''1'''' boot parameter, or alternatively set up the option in your modprobe configuration files. It's important to note that before trying, is to check the [http://www.x.org/wiki/RadeonFeature#fndef-72849d6d6eb3927d486f50ece74a739042965a6c-25 Radeon Feature Matrix] to see if it's actually supported. At the moment this is written (march 29th, 2013), some chipsets of the [http://www.x.org/wiki/RadeonFeature#Decoder_ring_for_engineering_vs_marketing_names S. Islands] (namely HD7750 through HD7970) doesn't (still) support HDMI Audio.}}<br />
<br />
If the test is successful, create or edit your ~/.asoundrc file to set HDMI as the default audio device.<br />
<br />
{{hc|~/.asoundrc|<br />
pcm.!default {<br />
type hw<br />
card 0<br />
device 3<br />
}<br />
}}<br />
<br />
Or you above config does not work try:<br />
{{hc|~/.asoundrc|<br />
defaults.pcm.card 0<br />
defaults.pcm.device 3<br />
defaults.ctl.card 0<br />
}}<br />
<br />
===HDMI Multi-channel PCM output does not work (Intel) ===<br />
As of Linux 3.1 multi-channel PCM output through HDMI with a Intel card (Intel Eaglelake, IbexPeak/Ironlake,SandyBridge/CougarPoint and IvyBridge/PantherPoint) is not yet supported. Support for it has been recently added and expected to be available in Linux 3.2. To make it work in Linux 3.1 you need to apply the following patches:<br />
<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=76adaa34db407f174dd06370cb60f6029c33b465 drm: support routines for HDMI/DP ELD]<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=e0dac65ed45e72fe34cc7ccc76de0ba220bd38bb drm/i915: pass ELD to HDMI/DP audio driver]<br />
<br />
===HP TX2500===<br />
Add these 2 lines into {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-cmipci mpu_port=0x330 fm_port=0x388<br />
options snd-hda-intel index=0 model=toshiba position_fix=1<br />
<br />
options snd-hda-intel model=hp (works for tx2000cto)<br />
<br />
===Skipping Sound When Playing MP3===<br />
If you have sound skipping when playing MP3 files and you have more then 2 speakers attached to your computer (i.e. > 2 speaker system), run alsamixer and disable the channels for the speakers that you '''DO NOT''' have (i.e. do not enable the sound for the center speaker if you do not have a center speaker.<br />
<br />
===Using a USB Headset and External USB Sound Cards===<br />
If you are using a USB headset with ALSA you can try using {{AUR|asoundconf}} (currently only available from the [[AUR]]) to set the headset as the primary sound output. Before running make sure you have usb audio module enabled ({{ic|modprobe snd-usb-audio}}).<br />
<br />
# asoundconf is-active<br />
# asoundconf list<br />
# asoundconf set-default-card <chosen soundcard><br />
<br />
==== Crackling sound with USB devices ====<br />
If you experience crackling sound on USB devices, you can try tuning the snd-usb-audio for minimal latency.<br />
<br />
Add this to your {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
options snd-usb-audio nrpacks=1<br />
<br />
source: http://alsa.opensrc.org/Usb-audio#Tuning_USB_devices_for_minimal_latencies<br />
<br />
==== Hot-plugging a USB Sound Card ====<br />
In order to automatically make a USB Sound Card the primary output device, when the card is plugged in, you can use the following udev rules (e.g. add the following two lines to {{ic|/etc/udev/rules.d/00-local.rules}} and reboot).<br />
<br />
{{bc|1=KERNEL=="pcmC[D0-9cp]*", ACTION=="add", PROGRAM="/bin/sh -c 'K=%k; K=$${K#pcmC}; K=$${K%%D*}; echo defaults.ctl.card $$K > /etc/asound.conf; echo defaults.pcm.card $$K >>/etc/asound.conf'"<br />
KERNEL=="pcmC[D0-9cp]*", ACTION=="remove", PROGRAM="/bin/sh -c 'echo defaults.ctl.card 0 > /etc/asound.conf; echo defaults.pcm.card 0 >>/etc/asound.conf'"}}<br />
<br />
===Error 'Unknown hardware' Appears After a Kernel Update===<br />
The following messages may be displayed during the start-up ALSA after the kernel update:<br />
Unknown hardware "foo" "bar" ...<br />
Hardware is initialized using a guess method<br />
/usr/sbin/alsactl: set_control:nnnn:failed to obtain info for control #mm (No such file or directory)<br />
<br />
or<br />
Found hardware: "HDA-Intel" "VIA VT1705" "HDA:11064397,18490397,00100000" "0x1849" "0x0397"<br />
Hardware is initialized using a generic method<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #1 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #2 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #25 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #26 (No such file or directory)<br />
<br />
Simply store ALSA mixer settings again (as root):<br />
# alsactl -f /var/lib/alsa/asound.state store<br />
<br />
It may be necessary configure ALSA again with alsamixer<br />
<br />
===HDA Analyzer===<br />
If the mappings to your audio pins(plugs) do not correspond but ALSA works fine, you could try HDA Analyzer -- a pyGTK2 GUI for HD-audio control can be found [http://www.alsa-project.org/main/index.php/HDA_Analyzer at the ALSA wiki].<br />
Try tweaking the Widget Control section of the PIN nodes, to make microphones IN and headphone jacks OUT. Referring to the Config Defaults heading is a good idea.<br />
<br />
NOTE: the script is done by such way that it is incompatible with python3 (which is now shipped with ArchLinux) but tries to use it.<br />
The workaround is: open "run.py", find all occurences of "python" (2 occurences - one on the first line, and the second on the last line) and replace them all by "python2".<br />
<br />
NOTE2: the script requires root acces, but running it via su/sudo is bogus. Run it via {{ic|kdesu}} or {{ic|gksu}}.<br />
<br />
===ALSA with SDL===<br />
If you get no sound via SDL and ALSA cannot be chosen from the application. Try setting the environmental variable SDL_AUDIODRIVER to alsa.<br />
# export SDL_AUDIODRIVER=alsa<br />
<br />
===Low Sound Workaround===<br />
<br />
If you are facing low sound even after maxing out your speakers/headphones, you can give the softvol plugin a try. Add the following to {{ic|/etc/asound.conf}}.<br />
{{bc|<nowiki><br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
<br />
pcm.softvol {<br />
type softvol<br />
slave {<br />
pcm "dmix"<br />
}<br />
control {<br />
name "Pre-Amp"<br />
card 0<br />
}<br />
min_dB -5.0<br />
max_dB 20.0<br />
resolution 6<br />
}<br />
</nowiki>}}<br />
{{note| You will probably have to restart the computer, as restarting the alsa daemon did not load the new configuration for me. Also, if the configuration does not work even after restarting, try changing {{ic|plug}} with {{ic|hw}} in the above configuration.}}<br />
<br />
After the changes are loaded successfully, you will see a {{ic|Pre-Amp}} section in alsamixer. You can adjust the levels there.<br />
{{note|Setting a high value for {{ic|Pre-Amp}} can cause sound distortion, so adjust it according to the level that suits you.}}<br />
<br />
===Popping sound after resuming from suspension===<br />
You might hear a popping sound after resuming the computer from suspension. This can be fixed by editing {{ic|/etc/pm/sleep.d/90alsa}} and removing the line that says {{ic|aplay -d 1 /dev/zero}}<br />
<br />
==配置文件范例==<br />
参见:[[Advanced Linux Sound Architecture/Example Configurations]]。<br />
<br />
==相关阅读==<br />
* [http://www.mjmwired.net/kernel/Documentation/sound/alsa/ALSA-Configuration.txt ALSA 模块高级配置]<br />
* [http://alsa.opensrc.org/Main_Page 非官方的 ALSA Wiki]<br />
* [https://bbs.archlinux.org/viewtopic.php?id=36815 如何从 svn 获取驱动代码并编译]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Advanced_Linux_Sound_Architecture_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=256351
Advanced Linux Sound Architecture (简体中文)
2013-05-08T06:51:21Z
<p>Cuihao: /* 开始或停止音频播放时出现噪音 */</p>
<hr />
<div>[[Category:简体中文]]<br />
[[Category:Sound (简体中文)]]<br />
[[Category:Audio/Video (简体中文)]]<br />
[[cs:Advanced Linux Sound Architecture]]<br />
[[de:Alsa]]<br />
[[es:Advanced Linux Sound Architecture]]<br />
[[fa:ALSA]]<br />
[[fr:Alsa]]<br />
[[he:Advanced Linux Sound Architecture]]<br />
[[it:Advanced Linux Sound Architecture]]<br />
[[ja:Advanced Linux Sound Architecture]]<br />
[[nl:Advanced Linux Sound Architecture]]<br />
[[pt:Advanced Linux Sound Architecture]]<br />
[[ru:Advanced Linux Sound Architecture]]<br />
[[sk:Advanced Linux Sound Architecture]]<br />
[[th:Advanced Linux Sound Architecture]]<br />
[[tr:Gelişmiş_ses_mimarisi_(ALSA)]]<br />
[[en:Advanced Linux Sound Architecture]]<br />
<br />
{{Article summary start}}<br />
{{Article summary text|ALSA 的使用、配置及疑难解答。}}<br />
{{Article summary heading|相关页面}}<br />
{{Article summary wiki|Sound system (简体中文)}}<br />
{{Article summary wiki|Disable PC Speaker Beep (简体中文)}}<br />
{{Article summary end}}<br />
{{TranslationStatus (简体中文)|Advanced_Linux_Sound_Architecture|2013-04-25|254584}}<br />
<br />
[[Wikipedia:zh:ALSA|高级 Linux 声音体系]](Advanced Linux Sound Architecture,'''ALSA''')是Linux中提供声音设备驱动的内核组件,用来代替原来的开放声音系统(Open Sound System,OSSv3)。除了声音设备驱动,'''ALSA'''还包含一个用户空间的函数库,以方便开发者通过高级API使用驱动功能,而不必直接与内核驱动交互。<br />
<br />
{{注意|关于另一种声音体系,请阅读[[Open Sound System (简体中文)|开放声音系统(OSS)]]的页面。}}<br />
<br />
==安装==<br />
Arch 默认的内核已经通过一套模块提供了 ALSA,不必特别安装。<br />
<br />
[[udev (简体中文)|udev]]会在系统启动时自动检测硬件,并加载相应的声音设备驱动模块。这时,你的声卡已经可以工作了,只是所有声道默认都被设置成静音了。<br />
<br />
在本地登录(通过虚拟终端或登录管理器)的用户,都有权限播放音频并调整音量。要让远程登录的用户拥有这些权限,必须把用户[[Users and Groups (简体中文)#用户组管理|加入]] {{ic|audio}} 用户组。该组的成员可以直接访问声音设备,会导致某些程序独占音频输出(破坏软件混音),还可能影响用户快速切换和拖机(multiseat)。因此,除非真的有某些[https://wiki.ubuntu.com/Audio/TheAudioGroup 特殊需求],'''不建议'''把用户加入 {{ic|audio}} 用户组。<br />
<br />
===用户空间工具===<br />
从 [[Official Repositories (简体中文)|官方仓库]]:<br />
* [[pacman (简体中文)|安装]] {{Pkg|alsa-utils}} 软件包,其中的 {{ic|alsamixer}} 工具允许用户在控制台或终端中配置声音设备。<br />
* 安装 {{Pkg|alsa-oss}} 软件包,如果想让 OSS 应用程序与dmix(软件混音)协同工作。 <br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。}}<br />
*安装 {{Pkg|alsa-plugins}} 软件包,如果需要 [[#高质量重采样|高质量重采样]]、[[#Upmixing/Downmixing|软件模拟环绕立体声]]和其他高级特性。<br />
<br />
==解除各声道的静音==<br />
目前版本的 ALSA 安装后,所有声道'''默认是静音的''',必须手动解除。<br />
<br />
使用 {{ic|alsamixer}} 的 ncurses 界面,配置十分简单:<br />
<br />
$ alsamixer<br />
<br />
此外,还可以在命令行下使用 {{ic|amixer}}:<br />
<br />
$ amixer sset Master unmute<br />
<br />
在 {{ic|alsamixer}} 中,下方标有 {{ic|MM}} 的声道是静音的,而标有 {{ic|00}} 的通道已经启用。<br />
<br />
使用左右方向键,选中 {{ic|Master}} 和 {{ic|PCM}} 声道。按下 {{keypress|m}} 键解除静音。使用上方向键增加音量,直到增益值为{{ic|0}}。该值显示在左上方 {{ic|Item:}} 字段后。过高的增益值会导致声音失真。<br />
<br />
要想得到完整的 5.1 或 7.1 环绕立体声,还得解除 Front、Surround、Center、LFE (subwoofer) 和 Side 这些声道的静音(上述名称是 Intel HD Audio 声卡使用的声道名,可能因设备不同而有所差异)。注意,仅有这些设置,系统不会自动将立体声源(多数音乐)提升(upmix)成环绕立体声。如果需要这些功能,请阅读[[#Upmixing/Downmixing]]。<br />
<br />
要启用麦克风,切换至 Capture 选项卡,按下 {{keypress|F4}},按下 {{keypress|空格}} 启用其中一个声道即可。<br />
<br />
按下 {{Keypress|Esc}} 键退出 alsamixer。<br />
<br />
{{注意|<br />
* 有一些声卡,需要关闭数字输出或将其调成静音,才能输出模拟音频信号。对于 Soundblaster Audigy LS 声卡,需要把 IEC958 通道设成静音。 <br />
* 有些机器(如 Thinkpad T61),有一个 Speaker 通道,必须按照上述方法打开调整。<br />
* 有些机器(如 Dell E6400),可能还需要把 {{ic|Front}} 和 {{ic|Headphone}} 声道打开调整。}}<br />
<br />
接下来,测试声卡是否工作:<br />
<br />
$ speaker-test -c 2<br />
<br />
根据扬声器的实际情况,调整 -c 后的数值。对于 7.1 声道,这个数字是 8:<br />
<br />
$ speaker-test -c 8<br />
<br />
如果没有正常工作,请继续阅读 [[#配置]] 以及 [[#疑难解答]] 部分。<br />
<br />
{{pkg|alsa-utils}} 软件包提供了两个配置 alsa 的服务:{{ic|alsa-restore.service}} 和 {{ic|alsa-store.service}}。它们分别在开机和关机时自动运行。<br />
<br />
==配置==<br />
=== VirtualBox 虚拟机无声音 ===<br />
<br />
如果使用 VirtualBox 时碰上了麻烦,请尝试以下命令:<br />
<br />
{{hc|$ alsactl init|2=<nowiki><br />
<br />
Found hardware: "ICH" "SigmaTel STAC9700,83,84" "AC97a:83847600" "0x8086" "0x0000"<br />
Hardware is initialized using a generic method<br />
</nowiki><br />
}}<br />
<br />
如果还是不行,请检查所使用的音频软件是否激活了 ALSA 输出。<br />
<br />
===设置默认声卡===<br />
<br />
如果发现开机时声卡次序会发生变化,可以在通过 {{ic|/etc/modprobe.d}} 中的 {{ic|.conf}} 文件(比如 {{ic|/etc/modprobe.d/alsa-base.conf}})手动设置次序。<br />
比如,要让 mia 声卡成为 #0、Intel HDA 声卡成为 #1:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<br />
options snd slots=snd_mia,snd_hda_intel<br />
options snd_mia index=0<br />
options snd_hda_intel index=1<br />
}}<br />
<br />
使用 {{ic|<nowiki>lsmod | grep snd</nowiki>}} 获取设备列表。<br />
<br />
snd_mia 和 snd_hda_intel 分别是两个声卡的内核驱动模块。该配置仅适用于同时装有 mia 声卡和使用 snd_hda_intel 驱动的声卡(通常是板载声卡)的情况。<br />
<br />
如果使用 -2 的 index 值,ALSA 就不会将对应的设备作为主声卡使用。Linux Mint 和 Ubuntu 等发行版使用了以下配置,避免 USB 声卡和其他“非主流”设备变成 index 为 0 的主声卡:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<br />
options bt87x index=-2<br />
options cx88_alsa index=-2<br />
options saa7134-alsa index=-2<br />
options snd-atiixp-modem index=-2<br />
options snd-intel8x0m index=-2<br />
options snd-via82xx-modem index=-2<br />
options snd-usb-audio index=-2<br />
options snd-usb-caiaq index=-2<br />
options snd-usb-ua101 index=-2<br />
options snd-usb-us122l index=-2<br />
options snd-usb-usx2y index=-2<br />
# Keep snd-pcsp from being loaded as first soundcard<br />
options snd-pcsp index=-2<br />
# Keep snd-usb-audio from beeing loaded as first soundcard<br />
options snd-usb-audio index=-2<br />
}}<br />
<br />
以上配置会在系统重启后生效。<br />
<br />
{{Out of date|据英文 Wiki 用户反应,{{ic|/etc/asound.conf}} 这个配置文件默认是不存在的,下列操作可能有误。}}<br />
<br />
首先,运行 {{ic|aplay -l}},获取声卡的声卡ID和设备ID:<br />
<br />
{{hc|$ aplay -l|2=<nowiki><br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: Intel [HDA Intel], device 0: CONEXANT Analog [CONEXANT Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: Intel [HDA Intel], device 1: Conexant Digital [Conexant Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 1: JamLab [JamLab], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 2: Audio [Altec Lansing XT1 - USB Audio], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
</nowiki>}}<br />
<br />
拿输出中的最后一项来说,这个声卡的声卡ID为2,设备ID为0。若要把这块声卡作为默认声卡,可以把下列配置添加到系统级别的 {{ic|/etc/asound.conf}} 或用户级别的 {{ic|~/.asoundrc}} 文件。如果文件不存在,可以手动创建。其中的各个ID,请根据实际情况调整:<br />
{{bc|<br />
defaults.pcm.card 2<br />
defaults.pcm.device 0<br />
defaults.ctl.card 2<br />
}}<br />
“pcm”选项决定用来播放音频的设备,而“ctl”选项决定那个声卡能够由控制工具(如 alsamixer)使用。<br />
<br />
上述配置在重启音频程序(如 mplayer)后立即生效。<br />
<br />
===确认所有声音模块都已经加载===<br />
<br />
一般 udev 都会自动识别出声卡。使用以下命令确认:<br />
<br />
{{hc|<nowiki>$ lsmod | grep '^snd' | column -t</nowiki>|<nowiki><br />
snd_hda_codec_hdmi 22378 4<br />
snd_hda_codec_realtek 294191 1<br />
snd_hda_intel 21738 1<br />
snd_hda_codec 73739 3 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel<br />
snd_hwdep 6134 1 snd_hda_codec<br />
snd_pcm 71032 3 snd_hda_codec_hdmi,snd_hda_intel,snd_hda_codec<br />
snd_timer 18992 1 snd_pcm<br />
snd 55132 9 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel,snd_hda_codec,snd_hwdep,snd_pcm,snd_timer<br />
snd_page_alloc 7017 2 snd_hda_intel,snd_pcm<br />
</nowiki>}}<br />
<br />
如果你的输出和上面类似,那就说明声卡已经被正确识别。<br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。如有需要,请[[Kernel_modules_(简体中文)#装入|手动加载]]。}}<br />
<br />
还可以检查一下 {{ic|/dev/snd/}} 目录,看看是否有这些设备文件:<br />
<br />
{{hc|$ ls -l /dev/snd|2=<nowiki><br />
total 0<br />
crw-rw---- 1 root audio 116, 0 Apr 8 14:17 controlC0<br />
crw-rw---- 1 root audio 116, 32 Apr 8 14:17 controlC1<br />
crw-rw---- 1 root audio 116, 24 Apr 8 14:17 pcmC0D0c<br />
crw-rw---- 1 root audio 116, 16 Apr 8 14:17 pcmC0D0p<br />
crw-rw---- 1 root audio 116, 25 Apr 8 14:17 pcmC0D1c<br />
crw-rw---- 1 root audio 116, 56 Apr 8 14:17 pcmC1D0c<br />
crw-rw---- 1 root audio 116, 48 Apr 8 14:17 pcmC1D0p<br />
crw-rw---- 1 root audio 116, 1 Apr 8 14:17 seq<br />
crw-rw---- 1 root audio 116, 33 Apr 8 14:17 timer<br />
</nowiki>}}<br />
<br />
{{注意|在 IRC 或论坛寻求这方面帮助时,别忘了贴出上面几个命令的输出。}}<br />
<br />
如果你的输出跟上面类似,或至少有 '''controlC0''' 和 '''pcmC0D0p''',那么声卡就已经正常加载了。<br />
<br />
如果出现问题,声卡模块没有正确加载,那么请尝试手动加载模块:<br />
<br />
* 确定声卡对应的驱动模块:[http://www.alsa-project.org/main/index.php/Matrix:Main ALSA Soundcard Matrix]。这些模块都会有一个”snd-“前缀(例如:{{ic|snd-via82xx}})。<br />
* [[Kernel_modules_(简体中文)#装入|加载模块]]。<br />
* 检查 {{ic|/dev/snd}} 目录中的设备文件(参见上文);或者,检查 {{ic|alsamixer}} 或 {{ic|amixer}} 的输出是否正确。<br />
* 修改配置,使 {{ic|snd-<模块名>}} 和 {{ic|snd-pcm-oss}} 模块[[Kernel_modules_(简体中文)#装入|开机自动加载]]。<br />
<br />
===启用 SPDIF 输出===<br />
<br />
(由 Gentoo 论坛的 gralves 提供)<br />
* 在 GNOME 音量控制的选项(Options)标签中,把 IEC958 改成 PCM。该选项可以在首选项中启用。<br />
* 不使用 GNOME 音量控制工具的话:<br />
** 编辑 {{ic|/var/lib/alsa/asound.state}}。该文件是 alsasound 保存混音器设置的地方。<br />
** 找到这一行:“IEC958 Playback Switch”。该行上下应该有一行内容为 {{ic|value:false}},将其改为 {{ic|value:true}}。<br />
** 再找到这一行:”IEC958 Playback AC97-SPSA“。把对应的值改为 0。<br />
** 重启 ALSA。<br />
<br />
{{Out of date|rc.local 在 systemd 中已经弃用,以下方法可能不再适用。}}<br />
<br />
还有另外一个开机自动启用 SPDIF 输出的方法(在 SoundBlaster Audigy 声卡上测试过):<br />
<br />
* 在 {{ic|/etc/rc.local}} 中添加如下内容:<br />
# Use COAX-digital output<br />
amixer set 'IEC958 Optical' 100 unmute<br />
amixer set 'Audigy Analog/Digital Output Jack' on<br />
<br />
使用以下命令,获取你的声卡的数字输出的名称:<br />
$ amixer scontrols<br />
<br />
===系统级均衡器===<br />
====使用 AlsaEqual(包含界面)====<br />
从 [[AUR (简体中文)|AUR]] 安装 {{AUR|alsaequal}}。<br />
<br />
{{注意|如果使用的是64位系统,而又安装了32位的 Flash 插件,这里的设置会导致 Flash 无声。如果发生了这种情况,请禁用 alsaequal 或编译32位版本的 alsaequal。}}<br />
<br />
安装后,把下列内容添加到 ALSA 配置文件({{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}}):<br />
<br />
ctl.equal {<br />
type equal;<br />
}<br />
<br />
pcm.plugequal {<br />
type equal;<br />
# Modify the line below if you do not<br />
#slave.pcm "plughw:0,0";<br />
#by default we want to play from more sources at time:<br />
slave.pcm "plug:dmix";<br />
}<br />
#pcm.equal {<br />
# If you do not want the equalizer to be your<br />
# default soundcard comment the following<br />
# line and uncomment the above line. (You can<br />
# choose it as the output device by addressing<br />
# it with specific apps,eg mpg123 -a equal 06.Back_In_Black.mp3)<br />
pcm.!default {<br />
type plug;<br />
slave.pcm plugequal;<br />
}<br />
<br />
设置好后,切换均衡器:<br />
$ alsamixer -D equal<br />
<br />
需要注意,每个用户的 alsaequal 配置文件都是独立的,位于 {{ic|~/.alsaequal.bin}}。因此,如果想在 [[mpd]] 之类的以独立用户身份运行的程序中使用 AlsaEqual,就得像下面这样:<br />
# su mpd -c 'alsamixer -D equal'<br />
或者,在其他用户的主目录下创建符号链接,指向你的 {{ic|.alsaequal.bin}}……<br />
<br />
=====管理 AlsaEqual 配置=====<br />
从 [http://xyne.archlinux.ca/repos/ Xyne 的软件仓库] 或 [https://aur.archlinux.org/packages.php?ID=62420 AUR] 安装 [http://xyne.archlinux.ca/projects/alsaequal-mgr/ alsaequal-mgr]。<br />
<br />
同上,配置均衡器: <br />
$alsamixer -D equal<br />
<br />
如果对效果感到满意,将其命名(如“foo”)并保存该配置:<br />
$alsaequal-mgr save foo<br />
<br />
重新加载“foo”中的配置:<br />
$alsaequal-mgr load foo<br />
<br />
这样,就可以为游戏、电影、不同音乐流派、网络电话等等创建不同的均衡器配置,以便随时切换。<br />
<br />
参见[http://xyne.archlinux.ca/projects/alsaequal-mgr/ 项目主页]和程序的帮助信息,了解更多设置。<br />
<br />
====使用 mbeq====<br />
{{注意|该软件用到了一个 ladspa 插件,可能导致播放音频时 CPU 占用率偏高。In addition, this was made with stereophonic sound (e.g. headphones) in mind.}}<br />
<br />
安装 {{Pkg|alsa-plugins}}、{{Pkg|ladspa}} 和 {{Pkg|swh-plugins}} 软件包。<br />
<br />
* 在 {{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}} 中添加如下内容:<br />
<br />
{{hc|/etc/asound.conf|2=<nowiki>pcm.eq {<br />
type ladspa<br />
<br />
# The output from the EQ can either go direct to a hardware device<br />
# (if you have a hardware mixer, e.g. SBLive/Audigy) or it can go<br />
# to the software mixer shown here.<br />
#slave.pcm "plughw:0,0"<br />
slave.pcm "plug:dmix"<br />
<br />
# Sometimes you may need to specify the path to the plugins,<br />
# especially if you have just installed them. Once you have logged<br />
# out/restarted this should not be necessary, but if you get errors<br />
# about being unable to find plugins, try uncommenting this.<br />
#path "/usr/lib/ladspa"<br />
<br />
plugins [<br />
{<br />
label mbeq<br />
id 1197<br />
input {<br />
#this setting is here by example, edit to your own taste<br />
#bands: 50hz, 100hz, 156hz, 220hz, 311hz, 440hz, 622hz, 880hz, 1250hz, 1750hz, 25000hz,<br />
#50000hz, 10000hz, 20000hz<br />
controls [ -5 -5 -5 -5 -5 -10 -20 -15 -10 -10 -10 -10 -10 -3 -2 ]<br />
}<br />
}<br />
]<br />
}<br />
<br />
# Redirect the default device to go via the EQ - you may want to do<br />
# this last, once you are sure everything is working. Otherwise all<br />
# your audio programs will break/crash if something has gone wrong.<br />
<br />
pcm.!default {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
<br />
# Redirect the OSS emulation through the EQ too (when programs are running through "aoss")<br />
<br />
pcm.dsp0 {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
</nowiki>}}<br />
<br />
* 完成。若有问题,欢迎前往论坛咨询。<br />
<br />
==高质量重采样==<br />
启用软件混音时,ALSA 会强制把所有音频重采样到相同的频率(如果系统支持,默认是 48000)。dmix 的重采样算法很糟糕,会导致显著的质量损失。<br />
<br />
安装 {{pkg|alsa-plugins}} 和 {{pkg|libsamplerate}}。<br />
<br />
将默认的采样率转换器更改为 libsamplerate:<br />
<br />
{{hc|/etc/asound.conf|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
或:<br />
<br />
{{hc|~/.asoundrc|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
'''samplerate_best''' 输出的声音质量最佳,但需要耗费大量CPU资源进行实时重采样。还有其他的算法可供选择(如 '''samplerate''' 等),但比起默认的算法,这些算法的改进并不明显。<br />
<br />
{{警告|某些系统中,启用 samplerate_best 可能导致 Flash 播放器无声音。}}<br />
<br />
==上混和下混==<br />
{{注意|由于未找到 upmix 和 downmix 的常用翻译,译者暂时采用字面翻译(上混、下混)。所谓“上混”,即将声道较少的立体声(如常见的双声道立体声)信号,通过软件或硬件处理,模拟为声道较多的立体声(如5.1环绕立体声)信号。反之即为“下混”。}}<br />
<br />
===上混(upmixing)===<br />
播放双声道立体声音源(如音乐)时,利用上混,即可充分利用 5.1 或 7.1 环绕立体声的所有声道。以前,上混很麻烦,还经常出错。但如今就方便多了,只需要安装一些插件即可。这些插件由 {{Pkg|alsa-plugins}} 软件包提供。<br />
<br />
安装后,在配置文件({{ic|/etc/asound.conf}} 或 {{ic|~/.asoundrc}})中添加如下内容:<br />
pcm.upmix71 {<br />
type upmix<br />
slave.pcm "surround71"<br />
delay 15<br />
channels 8<br />
}<br />
<br />
该范例适用于7.1声道上混。类比该范例,同样能设置5.1或4.0声道上混。<br />
<br />
该设置添加了一个新的用于上混的 pcm。如果希望所有音频源都通过该 pcm 输出,在上述设置后添加如下配置即可:<br />
<br />
pcm.!default "plug:upmix71"<br />
<br />
插件默认配置就允许多个音频源同时通过它输出,一般无需额外配置。如果不行,就得向下面这样为上混配置 dmixer:<br />
pcm.dmix6 {<br />
type asym<br />
playback.pcm {<br />
type dmix<br />
ipc_key 567829<br />
slave {<br />
pcm "hw:0,0"<br />
channels 6<br />
}<br />
}<br />
}<br />
<br />
注意要用 dmix6 而非 surround71。<br />
<br />
如果遇到声音卡顿或混乱,可能需要增加 buffer_size(比如增加到 32768),或使用[[#高质量重采样|高质量重采样]]。<br />
<br />
===下混(downmixing)===<br />
有时也会用到下混,比如在只支持双声道立体声的电脑上观看5.1声道的电影时。这一功能由 ALSA 插件 vdownmix 实现(同样在 {{Pkg|alsa-plugins}} 软件包里)。<br />
<br />
在配置文件中添加如下内容:<br />
<br />
pcm.!surround51 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
pcm.!surround40 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
<br />
==混音==<br />
===软件混音(dmix)===<br />
<br />
{{注意|在 ALSA 1.0.9rc2 及以上版本中,如果声卡不支持硬件混音,模拟输出将默认使用 dmix 混音,无需额外设置。}}<br />
<br />
如果软件混音未正常启用,那么可以手动创建 {{ic|~/.asoundrc}},加入以下内容:<br />
<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
<br />
这样就启用了软件混音,多个程序能够同时使用声卡。<br />
<br />
至于 S/PDIF 之类的数字音频输出,目前的 ALSA 还不会默认启用 dmix。因此,必须像上面这样手动启用。<br />
<br />
常见问题的解答,参见[[#疑难解答]]部分。<br />
<br />
===硬件混音===<br />
====支持====<br />
如果声卡支持,一般无需任何配置即可启用硬件混音。绝大多数板载声卡都不支持硬件混音,必须得使用软件混音(见上文)。很多独立声卡都支持硬件混音,Linux 下支持较好的有:<br />
<br />
* Creative SoundBlaster Live! (5.1 型号)<br />
* Creative SoundBlaster Audigy (部分型号)<br />
* Creative SoundBlaster Audidy 2 (ZS 系列型号)<br />
* Creative SoundBlaster Audigy 4 (Pro 系列型号)<br />
<br />
{{注意|上述声卡的低端版本(Audigy SE、Audigy 2 NX、SoundBlaster Live! 24bit 以及 SoundBlaster Live! 7.1)使用了不同的芯片,'''不支持'''硬件混音。}}<br />
<br />
{{注意|板载 VIA8237 芯片支持4路硬件混音,但在某些主板上只能使用3路(第4路无声),甚至根本无法使用。即使能正常工作,质量也比不上其他混音方法。}}<br />
<br />
====问题修复====<br />
如果你的声卡是 Intel Corporation 82801I (ICH9 Family) HD Audio Controller (rev 02),且使用了64位系统,需要执行以下命令才能在 Enemy Territory 中听到声音:<br />
echo "et.x86 0 0 direct" > /proc/asound/card0/pcm0p/oss<br />
echo "et.x86 0 0 disable" > /proc/asound/card0/pcm0c/oss<br />
<br />
==疑难解答==<br />
===使用CPU频率动态调整时,音频发生跳跃===<br />
使用 {{ic|ondemand}} 或 {{ic|conservative}} 频率策略、启用CPU频率动态调整时,某些声卡和某些 ALSA 驱动会出现声音跳跃的情况。目前,唯一的解决方案是使用固定的频率,如 {{ic|performance}} 性能策略。<br />
<br />
详情参见 [[CPU Frequency Scaling (简体中文)]]。<br />
<br />
===同时只能有一个用户播放音频===<br />
有时会出现一个用户独占软件混音器的情况。多数情况下这没什么影响,但在使用 [[mpd]] 这样以独立用户身份运行的程序时,就会出现问题。当 mpd 运行时,用户自己就无法播放其他音频了。一种解决方法是,以正在使用的用户身份运行 mpd。另一种方法是,在 ALSA 配置文件的 {{ic|pcm.dmixer}} 部分添加 {{ic|ipc_key_add_uid 0}} 禁止独占。以下是 {{ic|asound.conf}} 示例的片段,省略的部分无需修改:<br />
<br />
...<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
slave {<br />
...<br />
<br />
===无法同时播放多个音频===<br />
如果安装了 [[PulseAudio (简体中文)|PulseAudio]](可能作为 [[GNOME (简体中文)|GNOME]] 的依赖被安装),可能出现无法同步播放的问题,因为 PulseAudio 的默认配置是“独占”声卡。ALSA 的用户可能对自己的配置已经很满意、不希望使用 [[PulseAudio (简体中文)|PulseAudio]]。解决方案是编辑 {{ic|/etc/asound.conf}} 并注释掉以下内容:<br />
# Use PulseAudio by default<br />
#pcm.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
# hint {<br />
# show on<br />
# description "Default ALSA Output (currently PulseAudio Sound Server)"<br />
# }<br />
#}<br />
<br />
可能还需要注释掉下面的内容:<br />
#ctl.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
#}<br />
<br />
比起完全卸载 [[PulseAudio (简体中文)|PulseAudio]],这大概是个更简单的方法。<br />
<br />
以下是 {{ic|/etc/asound.conf}} 配置文件的一个示例:<br />
{{bc|<nowiki>pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
}<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}</nowiki><br />
}}<br />
<br />
{{注意|本 {{ic|/etc/asound.conf}} 本来是配合 [[MPD]] 全局配置使用的。参见[[#同时只能有一个用户播放音频|该节]]关于多用户的内容。}}<br />
<br />
===有时候开机无声===<br />
如果会随机地出现开机无声的问题,原因可能是多声卡的顺序发生了变化。如果是这种情况,请参照[[#设置默认声卡]]进行设置。<br />
<br />
对于 mpd 用户,上述配置无效,请参考[http://mpd.wikia.com/wiki/Configuration#ALSA_MPD_software_volume_control 该文]设置。<br />
<br />
===特定程序的问题===<br />
对于不使用系统声音配置的程序,如 XMMS 和 Mplayer,必须特别设置以应用 ALSA 配置。<br />
<br />
对于 mplayer,将以下内容添加到 {{ic|~/.mplayer/config}} (或系统全局配置文件 {{ic|/etc/mplayer/mplayer.conf}}):<br />
ao=alsa<br />
<br />
对于 XMMS 和 Beep Media Player,打开选项窗口,要把声音驱动设置为 ALSA 而不是 OSS。<br />
<br />
XMMS 的设置方法:<br />
* 启动 XMMS<br />
** 选项 -> 首选项<br />
** 选择 Alsa 输出插件<br />
<br />
对于未提供 ALSA 输出的程序,可以使用 alsa-oss 软件包中的 aoss 工具启动。使用方法是:在要运行的程序的命令行前加上 {{ic|aoss}}。例如:<br />
aoss realplay<br />
<br />
pcm.!default{ ... } 貌似已经不管用了。但下面的设置有效:<br />
pcm.default pcm.dmixer<br />
<br />
===特定型号的配置===<br />
ALSA 会通过 BIOS 检测[http://www.kernel.org/doc/Documentation/sound/alsa/HD-Audio-Models.txt 声卡型号],少数情况下也可能无法正确识别。声卡芯片类型可以在 {{ic|alsamixer}} 界面上找到(如 ALC662),而型号则要设置在 {{ic|/etc/modprobe.d/modprobe.conf}} 或 {{ic|/etc/modprobe.d/sound.conf}} 文件中:<br />
<br />
options snd-hda-intel model=<型号><br />
<br />
具体设置因声卡型号而异。多数情况下不需要特地这样做。如果想知道具体声卡型号的设置,请查阅 [http://bugtrack.alsa-project.org/main/index.php/Matrix:Main Alsa Soundcard List],找到你的声卡型号,点击“Details”,然后查看“Setting up modprobe...”小节,将其中的内容插入 {{ic|/etc/modprobe.d/modprobe.conf}} 文件即可。例如,Intel AC97 的配置如下:<br />
<br />
{{bc|# ALSA portion<br />
alias char-major-116 snd<br />
alias snd-card-0 snd-intel8x0<br />
# module options should go here<br />
<br />
# OSS/Free portion<br />
alias char-major-14 soundcore<br />
alias sound-slot-0 snd-card-0<br />
<br />
# card #1<br />
alias sound-service-0-0 snd-mixer-oss<br />
alias sound-service-0-1 snd-seq-oss<br />
alias sound-service-0-3 snd-pcm-oss<br />
alias sound-service-0-8 snd-seq-oss<br />
alias sound-service-0-12 snd-pcm-oss}}<br />
<br />
===与蜂鸣器冲突===<br />
如果你确认所有声道都已解除静音,声卡驱动配置正确,且音量大小也合适,但还是听不到声音,那么请试试将下面的内容添加到 {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
(如果是 {{ic|via82xx}} 驱动)<br />
options snd-<模块名请自行补充> ac97_quirk=0<br />
<br />
(如果是 {{ic|snd_intel8x0}} 驱动)<br />
options snd-NAME-OF-MODULE ac97_quirk=1<br />
<br />
===麦克风输入无声音===<br />
In alsamixer, make sure that all the volume levels are up under recording, and that CAPTURE is toggled active on the microphone (e.g. Mic, Internal Mic) and/or on Capture (in alsamixer, select these items and press space). Try making positive Mic Boost and raising Capture and Digital levels higher; this make make static or distortion, but then you can adjust them back down once you are hearing ''something'' when you record<br />
<br />
As the pulseaudio wrapper is shown as "default" in alsamixer, you may have to press F6 to select your actual soundcard first. You may also need to enable and increase the volume of Line-in in the Playback section.<br />
<br />
To test the microphone, run these commands (see arecord's man page for further information):<br />
arecord -d 5 test-mic.wav<br />
aplay test-mic.wav<br />
<br />
If all fails, you may want to eliminate hardware failure by testing the microphone with a different device.<br />
<br />
For at least some computers, muting a microphone (MM) simply means its input does not go immediately to the speakers. It still receives input.<br />
<br />
Many Dell laptops need "-dmic" to be appended to the model name in {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-hda-intel model=dell-m6-dmic<br />
<br />
Some programs use try to use OSS as the main input software. Add the following lines to {{ic|/etc/modprobe.d/modprobe.conf}} to prevent OSS modules from being loaded:<br />
<br />
{{Note|The OSS modules are no longer autoloaded anyway.}}<br />
<br />
blacklist snd_pcm_oss<br />
blacklist snd_mixer_oss<br />
blacklist snd_seq_oss<br />
<br />
See also:<br />
* http://www.alsa-project.org/main/index.php/SoundcardTesting<br />
* http://alsa.opensrc.org/Record_from_mic<br />
<br />
===设置默认的麦克风/录音设备===<br />
某些程序(Pidgin、Adobe Flash)提供了设置录音设备的选项。如果录音设备不是内置的声卡,而是外接的(如USB摄像头、麦克风),就会有些问题。如果只想更改默认的录音设备、不更改回放设备,需要像下面这样设置 {{ic|~/.asoundrc}}:<br />
<br />
pcm.usb<br />
{<br />
type hw<br />
card U0x46d0x81d<br />
}<br />
<br />
pcm.!default<br />
{<br />
type asym<br />
playback.pcm<br />
{<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
capture.pcm<br />
{<br />
type plug<br />
slave.pcm "usb"<br />
}<br />
}<br />
<br />
把“U0x46d0x81d”替换为你的录音设备的名称。可以通过 {{ic|arecord -L}} 查看所有 ALSA 识别到的录音设备。<br />
<br />
===内置麦克风无效===<br />
首先,请检查 alsamixer,确保所有的录音声道的音量都达到可以录制的程度。如果还是不行,试试修改 /etc/sound.conf 并重载 snd-* 模块。这样就会多出来一个名叫 Capture 的音频控制,可以用它调节内置麦克风的音量。例如,对于 snd-hda-intel 模块,配置如下:<br />
<br />
options snd-hda-intel enable_msi=1<br />
<br />
然后,输入下面的命令重载模块,调整音量并测试。<br />
<br />
{{bc|# rmmod snd-hda-intel && modprobe snd-hda-intel}}<br />
<br />
===Intel 板载声卡无声===<br />
可能由两个相互冲突的驱动模块引起,分别是 {{ic|snd_intel8x0}} 和 {{ic|snd_intel8x0m}}。如果是的话,屏蔽 snd_intel8x0m 即可:<br />
<br />
{{hc|/etc/modprobe.d/modprobe.conf|blacklist snd_intel8x0m}}<br />
<br />
以下方法可能也有效:在 {{ic|alsamixer}} 或 {{ic|amixer}} 把 “External Amplifier” 设置为静音。参见:[http://alsa.opensrc.org/Intel8x0#Dell_Inspiron_8600_.28and_probably_others.29 ALSA wiki 相关条目]。<br />
<br />
也可以试试解除“Mix”的静音。<br />
<br />
===Intel 板载声卡耳机无声===<br />
对于笔记本电脑上的 '''Intel Corporation 82801 I (ICH9 Family) HD Audio Controller''' 声卡,可能需要添加如下内容到 modprobe 配置文件或 sound.conf,以解决耳机无声的问题:<br />
<br />
options snd-hda-intel model=$model<br />
<br />
$model 在下列型号中选择:<br />
<br />
* dell-vostro<br />
* olpc-xo-1_5<br />
* laptop<br />
* dell-m6<br />
* laptop-hpsense<br />
<br />
{{注意|可能需要在配置文件的 "alias" 语句之后添加上述内容。}}<br />
<br />
可以在内核文档中找到所有支持的型号,如[http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.35.y.git;a=blob;f=Documentation/sound/alsa/HD-Audio-Models.txt;h=dc25bb84b83b49665a7ed850e7bf5423d50cd3ba;hb=HEAD 该文]。这是 2.6.35 版本内核的文档,你需要自己查找适用于自己内核版本的文档。<br />
<br />
也可以从[http://www.mjmwired.net/kernel/Documentation/sound/alsa/HD-Audio-Models.txt 这里]获取型号列表。要查看你的声卡芯片名称,输入下列命令(其中的“*”请根据实际情况替换)。注意,有些芯片名称被更改过,因此实际名称与文件中的可能不匹配。<br />
<br />
cat /proc/asound/card*/codec* | grep Codec<br />
<br />
注意,这么做很可能导致所有输入设备(内置和外置麦克风)无法工作,即要么让麦克风正常、要么让耳机正常。如果遇到这一问题,请向 ALSA 开发者报告。<br />
<br />
另外,如果蜂鸣器(pcspkr)出现问题,可以试试使用以下配置修复:<br />
<br />
options snd-hda-intel model=$model enable=1 index=0<br />
<br />
===安装支持 S/PDIF 输出的显卡后无声 ===<br />
检测已加载的相关模块,并记录顺序:<br />
$ cat /proc/asound/modules<br />
0 snd_hda_intel<br />
1 snd_ca0106<br />
<br />
在 {{ic|/etc/modprobe.d/modprobe.conf}} 中禁用不希望使用的显卡的音频解码模块:<br />
# /etc/modprobe.d/modprobe.conf<br />
#<br />
install snd_hda_intel /bin/false<br />
<br />
如果不同设备使用同一模块,那么只能在 BIOS 中禁用其中之一。<br />
<br />
===音效差或卡顿===<br />
如果发现音效特别差,试试在 alsamixer 中调节一下 PCM 音量,到增益(gain)为0的水平。<br />
<br />
若使用 snd-usb-audio 驱动,可以试试在 {{ic|/etc/asound.conf}} 中启用 {{ic|softvol}}。下面是针对第一个声音设备的配置范例:<br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.dsnooper {<br />
type dsnoop<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
channels 2<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.softvol {<br />
type softvol<br />
slave { pcm "dmixer" }<br />
control {<br />
name "Master"<br />
card 0<br />
}<br />
}<br />
ctl.!default {<br />
type hw<br />
card 0<br />
}<br />
ctl.softvol {<br />
type hw<br />
card 0<br />
}<br />
ctl.dmixer {<br />
type hw<br />
card 0<br />
}<br />
<br />
===开始或停止音频播放时出现噪音===<br />
某些驱动模块(如 snd_ac97_codec 和 snd_hda_intel)会在声卡闲置时关闭它以节约用电。声卡启动或关闭时,就会发出刺耳的噪音。在调节音量或开关窗口(KDE4)时也可能发生这种情况。要消除这一问题,需要添加特定模块参数(通过 {{ic|modinfo snd_MY_MODULE}} 查询)关闭该功能。<br />
<br />
例如:对于 snd_hda_intel 模块,要禁用自动节电并解决噪音问题,需在 {{ic|/etc/modprobe.d/modprobe.conf}} 中添加如下内容:<br />
options snd_hda_intel power_save=0<br />
或:<br />
options snd_hda_intel power_save=0 power_save_controller=N<br />
<br />
可以先使用 {{ic|1=modprobe snd_hda_intel power_save=0}} 测试一下效果。<br />
<br />
对于 VIA VT1708S 板载声卡(使用 snd_hda_intel 模块)等一些声卡,即使把 power_save 参数设置为0可能仍会有噪音。还得在 alsamixer 中把“Line”声道激活才能生效,只要激活并调到一个非0的音量即可(1即可,不要太高)。<br />
<br />
内核模块文档:[https://www.kernel.org/doc/Documentation/sound/alsa/powersave.txt]<br />
<br />
对于笔记本,即使在 {{ic|/etc/modprobe.d}} 设置了 {{ic|power_save}} 参数,当切换电池时 pm-utils 仍会将该值重置为1。需要禁用相关的脚步才行(详情参见:[[Pm-utils#Disabling_a_hook]]):<br />
# touch /etc/pm/power.d/intel-audio-powersave<br />
<br />
===S/PDIF Output Does Not Work===<br />
<br />
If the optical/coaxial digital output of your motherboard/sound card is not working or stopped working, and have already enabled and unmuted it in alsamixer, try running<br />
# iecset audio on<br />
<br />
as root.<br />
<br />
You can also put this command in {{Ic|rc.local}} as it sometimes it may stop working after a reboot.<br />
<br />
===HDMI Output Does Not Work===<br />
If the HDMI output of your motherboard/sound card is not working or stopped working, and have already enabled and unmuted it in alsamixer, try the following.<br />
<br />
Query for Playback Devices:<br />
<br />
$ aplay -l<br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: NVidia [HDA NVidia], device 0: ALC1200 Analog [ALC1200 Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 1: ALC1200 Digital [ALC1200 Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 3: NVIDIA HDMI [NVIDIA HDMI]<br />
Subdevices: 0/1<br />
Subdevice #0: subdevice #0<br />
<br />
Now that we have the info for the HDMI device, try a test, In the example below, 0 is the card number and 3 is the device number.<br />
<br />
$ aplay -D plughw:0,3 /usr/share/sounds/alsa/Front_Center.wav<br />
<br />
If aplay does not output any errors, but still no sound is heared, "reboot" the receiver, monitor or tv set. Since the HDMI interface executes a handshake on connection, it might have noticed before that there was no audio stream embedded, and disabled audio decoding. In particular, if you are using a standalone window manager (don’t know about Gnome or KDE), you may need to have some sound playing ''while'' plugging in the HDMI cable.<br />
<br />
{{Note|If you are using an ATI card and linux 3.0, check your kernel/grub settings, you most likely need to use the ''''radeon.audio'''<nowiki>=</nowiki>'''1'''' boot parameter, or alternatively set up the option in your modprobe configuration files. It's important to note that before trying, is to check the [http://www.x.org/wiki/RadeonFeature#fndef-72849d6d6eb3927d486f50ece74a739042965a6c-25 Radeon Feature Matrix] to see if it's actually supported. At the moment this is written (march 29th, 2013), some chipsets of the [http://www.x.org/wiki/RadeonFeature#Decoder_ring_for_engineering_vs_marketing_names S. Islands] (namely HD7750 through HD7970) doesn't (still) support HDMI Audio.}}<br />
<br />
If the test is successful, create or edit your ~/.asoundrc file to set HDMI as the default audio device.<br />
<br />
{{hc|~/.asoundrc|<br />
pcm.!default {<br />
type hw<br />
card 0<br />
device 3<br />
}<br />
}}<br />
<br />
Or you above config does not work try:<br />
{{hc|~/.asoundrc|<br />
defaults.pcm.card 0<br />
defaults.pcm.device 3<br />
defaults.ctl.card 0<br />
}}<br />
<br />
===HDMI Multi-channel PCM output does not work (Intel) ===<br />
As of Linux 3.1 multi-channel PCM output through HDMI with a Intel card (Intel Eaglelake, IbexPeak/Ironlake,SandyBridge/CougarPoint and IvyBridge/PantherPoint) is not yet supported. Support for it has been recently added and expected to be available in Linux 3.2. To make it work in Linux 3.1 you need to apply the following patches:<br />
<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=76adaa34db407f174dd06370cb60f6029c33b465 drm: support routines for HDMI/DP ELD]<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=e0dac65ed45e72fe34cc7ccc76de0ba220bd38bb drm/i915: pass ELD to HDMI/DP audio driver]<br />
<br />
===HP TX2500===<br />
Add these 2 lines into {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-cmipci mpu_port=0x330 fm_port=0x388<br />
options snd-hda-intel index=0 model=toshiba position_fix=1<br />
<br />
options snd-hda-intel model=hp (works for tx2000cto)<br />
<br />
===Skipping Sound When Playing MP3===<br />
If you have sound skipping when playing MP3 files and you have more then 2 speakers attached to your computer (i.e. > 2 speaker system), run alsamixer and disable the channels for the speakers that you '''DO NOT''' have (i.e. do not enable the sound for the center speaker if you do not have a center speaker.<br />
<br />
===Using a USB Headset and External USB Sound Cards===<br />
If you are using a USB headset with ALSA you can try using {{AUR|asoundconf}} (currently only available from the [[AUR]]) to set the headset as the primary sound output. Before running make sure you have usb audio module enabled ({{ic|modprobe snd-usb-audio}}).<br />
<br />
# asoundconf is-active<br />
# asoundconf list<br />
# asoundconf set-default-card <chosen soundcard><br />
<br />
==== Crackling sound with USB devices ====<br />
If you experience crackling sound on USB devices, you can try tuning the snd-usb-audio for minimal latency.<br />
<br />
Add this to your {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
options snd-usb-audio nrpacks=1<br />
<br />
source: http://alsa.opensrc.org/Usb-audio#Tuning_USB_devices_for_minimal_latencies<br />
<br />
==== Hot-plugging a USB Sound Card ====<br />
In order to automatically make a USB Sound Card the primary output device, when the card is plugged in, you can use the following udev rules (e.g. add the following two lines to {{ic|/etc/udev/rules.d/00-local.rules}} and reboot).<br />
<br />
{{bc|1=KERNEL=="pcmC[D0-9cp]*", ACTION=="add", PROGRAM="/bin/sh -c 'K=%k; K=$${K#pcmC}; K=$${K%%D*}; echo defaults.ctl.card $$K > /etc/asound.conf; echo defaults.pcm.card $$K >>/etc/asound.conf'"<br />
KERNEL=="pcmC[D0-9cp]*", ACTION=="remove", PROGRAM="/bin/sh -c 'echo defaults.ctl.card 0 > /etc/asound.conf; echo defaults.pcm.card 0 >>/etc/asound.conf'"}}<br />
<br />
===Error 'Unknown hardware' Appears After a Kernel Update===<br />
The following messages may be displayed during the start-up ALSA after the kernel update:<br />
Unknown hardware "foo" "bar" ...<br />
Hardware is initialized using a guess method<br />
/usr/sbin/alsactl: set_control:nnnn:failed to obtain info for control #mm (No such file or directory)<br />
<br />
or<br />
Found hardware: "HDA-Intel" "VIA VT1705" "HDA:11064397,18490397,00100000" "0x1849" "0x0397"<br />
Hardware is initialized using a generic method<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #1 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #2 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #25 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #26 (No such file or directory)<br />
<br />
Simply store ALSA mixer settings again (as root):<br />
# alsactl -f /var/lib/alsa/asound.state store<br />
<br />
It may be necessary configure ALSA again with alsamixer<br />
<br />
===HDA Analyzer===<br />
If the mappings to your audio pins(plugs) do not correspond but ALSA works fine, you could try HDA Analyzer -- a pyGTK2 GUI for HD-audio control can be found [http://www.alsa-project.org/main/index.php/HDA_Analyzer at the ALSA wiki].<br />
Try tweaking the Widget Control section of the PIN nodes, to make microphones IN and headphone jacks OUT. Referring to the Config Defaults heading is a good idea.<br />
<br />
NOTE: the script is done by such way that it is incompatible with python3 (which is now shipped with ArchLinux) but tries to use it.<br />
The workaround is: open "run.py", find all occurences of "python" (2 occurences - one on the first line, and the second on the last line) and replace them all by "python2".<br />
<br />
NOTE2: the script requires root acces, but running it via su/sudo is bogus. Run it via {{ic|kdesu}} or {{ic|gksu}}.<br />
<br />
===ALSA with SDL===<br />
If you get no sound via SDL and ALSA cannot be chosen from the application. Try setting the environmental variable SDL_AUDIODRIVER to alsa.<br />
# export SDL_AUDIODRIVER=alsa<br />
<br />
===Low Sound Workaround===<br />
<br />
If you are facing low sound even after maxing out your speakers/headphones, you can give the softvol plugin a try. Add the following to {{ic|/etc/asound.conf}}.<br />
{{bc|<nowiki><br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
<br />
pcm.softvol {<br />
type softvol<br />
slave {<br />
pcm "dmix"<br />
}<br />
control {<br />
name "Pre-Amp"<br />
card 0<br />
}<br />
min_dB -5.0<br />
max_dB 20.0<br />
resolution 6<br />
}<br />
</nowiki>}}<br />
{{note| You will probably have to restart the computer, as restarting the alsa daemon did not load the new configuration for me. Also, if the configuration does not work even after restarting, try changing {{ic|plug}} with {{ic|hw}} in the above configuration.}}<br />
<br />
After the changes are loaded successfully, you will see a {{ic|Pre-Amp}} section in alsamixer. You can adjust the levels there.<br />
{{note|Setting a high value for {{ic|Pre-Amp}} can cause sound distortion, so adjust it according to the level that suits you.}}<br />
<br />
===Popping sound after resuming from suspension===<br />
You might hear a popping sound after resuming the computer from suspension. This can be fixed by editing {{ic|/etc/pm/sleep.d/90alsa}} and removing the line that says {{ic|aplay -d 1 /dev/zero}}<br />
<br />
==配置文件范例==<br />
参见:[[Advanced Linux Sound Architecture/Example Configurations]]。<br />
<br />
==相关阅读==<br />
* [http://www.mjmwired.net/kernel/Documentation/sound/alsa/ALSA-Configuration.txt ALSA 模块高级配置]<br />
* [http://alsa.opensrc.org/Main_Page 非官方的 ALSA Wiki]<br />
* [https://bbs.archlinux.org/viewtopic.php?id=36815 如何从 svn 获取驱动代码并编译]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Advanced_Linux_Sound_Architecture_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=256350
Advanced Linux Sound Architecture (简体中文)
2013-05-08T06:50:22Z
<p>Cuihao: /* Pops When Starting and Stopping Playback */</p>
<hr />
<div>[[Category:简体中文]]<br />
[[Category:Sound (简体中文)]]<br />
[[Category:Audio/Video (简体中文)]]<br />
[[cs:Advanced Linux Sound Architecture]]<br />
[[de:Alsa]]<br />
[[es:Advanced Linux Sound Architecture]]<br />
[[fa:ALSA]]<br />
[[fr:Alsa]]<br />
[[he:Advanced Linux Sound Architecture]]<br />
[[it:Advanced Linux Sound Architecture]]<br />
[[ja:Advanced Linux Sound Architecture]]<br />
[[nl:Advanced Linux Sound Architecture]]<br />
[[pt:Advanced Linux Sound Architecture]]<br />
[[ru:Advanced Linux Sound Architecture]]<br />
[[sk:Advanced Linux Sound Architecture]]<br />
[[th:Advanced Linux Sound Architecture]]<br />
[[tr:Gelişmiş_ses_mimarisi_(ALSA)]]<br />
[[en:Advanced Linux Sound Architecture]]<br />
<br />
{{Article summary start}}<br />
{{Article summary text|ALSA 的使用、配置及疑难解答。}}<br />
{{Article summary heading|相关页面}}<br />
{{Article summary wiki|Sound system (简体中文)}}<br />
{{Article summary wiki|Disable PC Speaker Beep (简体中文)}}<br />
{{Article summary end}}<br />
{{TranslationStatus (简体中文)|Advanced_Linux_Sound_Architecture|2013-04-25|254584}}<br />
<br />
[[Wikipedia:zh:ALSA|高级 Linux 声音体系]](Advanced Linux Sound Architecture,'''ALSA''')是Linux中提供声音设备驱动的内核组件,用来代替原来的开放声音系统(Open Sound System,OSSv3)。除了声音设备驱动,'''ALSA'''还包含一个用户空间的函数库,以方便开发者通过高级API使用驱动功能,而不必直接与内核驱动交互。<br />
<br />
{{注意|关于另一种声音体系,请阅读[[Open Sound System (简体中文)|开放声音系统(OSS)]]的页面。}}<br />
<br />
==安装==<br />
Arch 默认的内核已经通过一套模块提供了 ALSA,不必特别安装。<br />
<br />
[[udev (简体中文)|udev]]会在系统启动时自动检测硬件,并加载相应的声音设备驱动模块。这时,你的声卡已经可以工作了,只是所有声道默认都被设置成静音了。<br />
<br />
在本地登录(通过虚拟终端或登录管理器)的用户,都有权限播放音频并调整音量。要让远程登录的用户拥有这些权限,必须把用户[[Users and Groups (简体中文)#用户组管理|加入]] {{ic|audio}} 用户组。该组的成员可以直接访问声音设备,会导致某些程序独占音频输出(破坏软件混音),还可能影响用户快速切换和拖机(multiseat)。因此,除非真的有某些[https://wiki.ubuntu.com/Audio/TheAudioGroup 特殊需求],'''不建议'''把用户加入 {{ic|audio}} 用户组。<br />
<br />
===用户空间工具===<br />
从 [[Official Repositories (简体中文)|官方仓库]]:<br />
* [[pacman (简体中文)|安装]] {{Pkg|alsa-utils}} 软件包,其中的 {{ic|alsamixer}} 工具允许用户在控制台或终端中配置声音设备。<br />
* 安装 {{Pkg|alsa-oss}} 软件包,如果想让 OSS 应用程序与dmix(软件混音)协同工作。 <br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。}}<br />
*安装 {{Pkg|alsa-plugins}} 软件包,如果需要 [[#高质量重采样|高质量重采样]]、[[#Upmixing/Downmixing|软件模拟环绕立体声]]和其他高级特性。<br />
<br />
==解除各声道的静音==<br />
目前版本的 ALSA 安装后,所有声道'''默认是静音的''',必须手动解除。<br />
<br />
使用 {{ic|alsamixer}} 的 ncurses 界面,配置十分简单:<br />
<br />
$ alsamixer<br />
<br />
此外,还可以在命令行下使用 {{ic|amixer}}:<br />
<br />
$ amixer sset Master unmute<br />
<br />
在 {{ic|alsamixer}} 中,下方标有 {{ic|MM}} 的声道是静音的,而标有 {{ic|00}} 的通道已经启用。<br />
<br />
使用左右方向键,选中 {{ic|Master}} 和 {{ic|PCM}} 声道。按下 {{keypress|m}} 键解除静音。使用上方向键增加音量,直到增益值为{{ic|0}}。该值显示在左上方 {{ic|Item:}} 字段后。过高的增益值会导致声音失真。<br />
<br />
要想得到完整的 5.1 或 7.1 环绕立体声,还得解除 Front、Surround、Center、LFE (subwoofer) 和 Side 这些声道的静音(上述名称是 Intel HD Audio 声卡使用的声道名,可能因设备不同而有所差异)。注意,仅有这些设置,系统不会自动将立体声源(多数音乐)提升(upmix)成环绕立体声。如果需要这些功能,请阅读[[#Upmixing/Downmixing]]。<br />
<br />
要启用麦克风,切换至 Capture 选项卡,按下 {{keypress|F4}},按下 {{keypress|空格}} 启用其中一个声道即可。<br />
<br />
按下 {{Keypress|Esc}} 键退出 alsamixer。<br />
<br />
{{注意|<br />
* 有一些声卡,需要关闭数字输出或将其调成静音,才能输出模拟音频信号。对于 Soundblaster Audigy LS 声卡,需要把 IEC958 通道设成静音。 <br />
* 有些机器(如 Thinkpad T61),有一个 Speaker 通道,必须按照上述方法打开调整。<br />
* 有些机器(如 Dell E6400),可能还需要把 {{ic|Front}} 和 {{ic|Headphone}} 声道打开调整。}}<br />
<br />
接下来,测试声卡是否工作:<br />
<br />
$ speaker-test -c 2<br />
<br />
根据扬声器的实际情况,调整 -c 后的数值。对于 7.1 声道,这个数字是 8:<br />
<br />
$ speaker-test -c 8<br />
<br />
如果没有正常工作,请继续阅读 [[#配置]] 以及 [[#疑难解答]] 部分。<br />
<br />
{{pkg|alsa-utils}} 软件包提供了两个配置 alsa 的服务:{{ic|alsa-restore.service}} 和 {{ic|alsa-store.service}}。它们分别在开机和关机时自动运行。<br />
<br />
==配置==<br />
=== VirtualBox 虚拟机无声音 ===<br />
<br />
如果使用 VirtualBox 时碰上了麻烦,请尝试以下命令:<br />
<br />
{{hc|$ alsactl init|2=<nowiki><br />
<br />
Found hardware: "ICH" "SigmaTel STAC9700,83,84" "AC97a:83847600" "0x8086" "0x0000"<br />
Hardware is initialized using a generic method<br />
</nowiki><br />
}}<br />
<br />
如果还是不行,请检查所使用的音频软件是否激活了 ALSA 输出。<br />
<br />
===设置默认声卡===<br />
<br />
如果发现开机时声卡次序会发生变化,可以在通过 {{ic|/etc/modprobe.d}} 中的 {{ic|.conf}} 文件(比如 {{ic|/etc/modprobe.d/alsa-base.conf}})手动设置次序。<br />
比如,要让 mia 声卡成为 #0、Intel HDA 声卡成为 #1:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<br />
options snd slots=snd_mia,snd_hda_intel<br />
options snd_mia index=0<br />
options snd_hda_intel index=1<br />
}}<br />
<br />
使用 {{ic|<nowiki>lsmod | grep snd</nowiki>}} 获取设备列表。<br />
<br />
snd_mia 和 snd_hda_intel 分别是两个声卡的内核驱动模块。该配置仅适用于同时装有 mia 声卡和使用 snd_hda_intel 驱动的声卡(通常是板载声卡)的情况。<br />
<br />
如果使用 -2 的 index 值,ALSA 就不会将对应的设备作为主声卡使用。Linux Mint 和 Ubuntu 等发行版使用了以下配置,避免 USB 声卡和其他“非主流”设备变成 index 为 0 的主声卡:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<br />
options bt87x index=-2<br />
options cx88_alsa index=-2<br />
options saa7134-alsa index=-2<br />
options snd-atiixp-modem index=-2<br />
options snd-intel8x0m index=-2<br />
options snd-via82xx-modem index=-2<br />
options snd-usb-audio index=-2<br />
options snd-usb-caiaq index=-2<br />
options snd-usb-ua101 index=-2<br />
options snd-usb-us122l index=-2<br />
options snd-usb-usx2y index=-2<br />
# Keep snd-pcsp from being loaded as first soundcard<br />
options snd-pcsp index=-2<br />
# Keep snd-usb-audio from beeing loaded as first soundcard<br />
options snd-usb-audio index=-2<br />
}}<br />
<br />
以上配置会在系统重启后生效。<br />
<br />
{{Out of date|据英文 Wiki 用户反应,{{ic|/etc/asound.conf}} 这个配置文件默认是不存在的,下列操作可能有误。}}<br />
<br />
首先,运行 {{ic|aplay -l}},获取声卡的声卡ID和设备ID:<br />
<br />
{{hc|$ aplay -l|2=<nowiki><br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: Intel [HDA Intel], device 0: CONEXANT Analog [CONEXANT Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: Intel [HDA Intel], device 1: Conexant Digital [Conexant Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 1: JamLab [JamLab], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 2: Audio [Altec Lansing XT1 - USB Audio], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
</nowiki>}}<br />
<br />
拿输出中的最后一项来说,这个声卡的声卡ID为2,设备ID为0。若要把这块声卡作为默认声卡,可以把下列配置添加到系统级别的 {{ic|/etc/asound.conf}} 或用户级别的 {{ic|~/.asoundrc}} 文件。如果文件不存在,可以手动创建。其中的各个ID,请根据实际情况调整:<br />
{{bc|<br />
defaults.pcm.card 2<br />
defaults.pcm.device 0<br />
defaults.ctl.card 2<br />
}}<br />
“pcm”选项决定用来播放音频的设备,而“ctl”选项决定那个声卡能够由控制工具(如 alsamixer)使用。<br />
<br />
上述配置在重启音频程序(如 mplayer)后立即生效。<br />
<br />
===确认所有声音模块都已经加载===<br />
<br />
一般 udev 都会自动识别出声卡。使用以下命令确认:<br />
<br />
{{hc|<nowiki>$ lsmod | grep '^snd' | column -t</nowiki>|<nowiki><br />
snd_hda_codec_hdmi 22378 4<br />
snd_hda_codec_realtek 294191 1<br />
snd_hda_intel 21738 1<br />
snd_hda_codec 73739 3 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel<br />
snd_hwdep 6134 1 snd_hda_codec<br />
snd_pcm 71032 3 snd_hda_codec_hdmi,snd_hda_intel,snd_hda_codec<br />
snd_timer 18992 1 snd_pcm<br />
snd 55132 9 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel,snd_hda_codec,snd_hwdep,snd_pcm,snd_timer<br />
snd_page_alloc 7017 2 snd_hda_intel,snd_pcm<br />
</nowiki>}}<br />
<br />
如果你的输出和上面类似,那就说明声卡已经被正确识别。<br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。如有需要,请[[Kernel_modules_(简体中文)#装入|手动加载]]。}}<br />
<br />
还可以检查一下 {{ic|/dev/snd/}} 目录,看看是否有这些设备文件:<br />
<br />
{{hc|$ ls -l /dev/snd|2=<nowiki><br />
total 0<br />
crw-rw---- 1 root audio 116, 0 Apr 8 14:17 controlC0<br />
crw-rw---- 1 root audio 116, 32 Apr 8 14:17 controlC1<br />
crw-rw---- 1 root audio 116, 24 Apr 8 14:17 pcmC0D0c<br />
crw-rw---- 1 root audio 116, 16 Apr 8 14:17 pcmC0D0p<br />
crw-rw---- 1 root audio 116, 25 Apr 8 14:17 pcmC0D1c<br />
crw-rw---- 1 root audio 116, 56 Apr 8 14:17 pcmC1D0c<br />
crw-rw---- 1 root audio 116, 48 Apr 8 14:17 pcmC1D0p<br />
crw-rw---- 1 root audio 116, 1 Apr 8 14:17 seq<br />
crw-rw---- 1 root audio 116, 33 Apr 8 14:17 timer<br />
</nowiki>}}<br />
<br />
{{注意|在 IRC 或论坛寻求这方面帮助时,别忘了贴出上面几个命令的输出。}}<br />
<br />
如果你的输出跟上面类似,或至少有 '''controlC0''' 和 '''pcmC0D0p''',那么声卡就已经正常加载了。<br />
<br />
如果出现问题,声卡模块没有正确加载,那么请尝试手动加载模块:<br />
<br />
* 确定声卡对应的驱动模块:[http://www.alsa-project.org/main/index.php/Matrix:Main ALSA Soundcard Matrix]。这些模块都会有一个”snd-“前缀(例如:{{ic|snd-via82xx}})。<br />
* [[Kernel_modules_(简体中文)#装入|加载模块]]。<br />
* 检查 {{ic|/dev/snd}} 目录中的设备文件(参见上文);或者,检查 {{ic|alsamixer}} 或 {{ic|amixer}} 的输出是否正确。<br />
* 修改配置,使 {{ic|snd-<模块名>}} 和 {{ic|snd-pcm-oss}} 模块[[Kernel_modules_(简体中文)#装入|开机自动加载]]。<br />
<br />
===启用 SPDIF 输出===<br />
<br />
(由 Gentoo 论坛的 gralves 提供)<br />
* 在 GNOME 音量控制的选项(Options)标签中,把 IEC958 改成 PCM。该选项可以在首选项中启用。<br />
* 不使用 GNOME 音量控制工具的话:<br />
** 编辑 {{ic|/var/lib/alsa/asound.state}}。该文件是 alsasound 保存混音器设置的地方。<br />
** 找到这一行:“IEC958 Playback Switch”。该行上下应该有一行内容为 {{ic|value:false}},将其改为 {{ic|value:true}}。<br />
** 再找到这一行:”IEC958 Playback AC97-SPSA“。把对应的值改为 0。<br />
** 重启 ALSA。<br />
<br />
{{Out of date|rc.local 在 systemd 中已经弃用,以下方法可能不再适用。}}<br />
<br />
还有另外一个开机自动启用 SPDIF 输出的方法(在 SoundBlaster Audigy 声卡上测试过):<br />
<br />
* 在 {{ic|/etc/rc.local}} 中添加如下内容:<br />
# Use COAX-digital output<br />
amixer set 'IEC958 Optical' 100 unmute<br />
amixer set 'Audigy Analog/Digital Output Jack' on<br />
<br />
使用以下命令,获取你的声卡的数字输出的名称:<br />
$ amixer scontrols<br />
<br />
===系统级均衡器===<br />
====使用 AlsaEqual(包含界面)====<br />
从 [[AUR (简体中文)|AUR]] 安装 {{AUR|alsaequal}}。<br />
<br />
{{注意|如果使用的是64位系统,而又安装了32位的 Flash 插件,这里的设置会导致 Flash 无声。如果发生了这种情况,请禁用 alsaequal 或编译32位版本的 alsaequal。}}<br />
<br />
安装后,把下列内容添加到 ALSA 配置文件({{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}}):<br />
<br />
ctl.equal {<br />
type equal;<br />
}<br />
<br />
pcm.plugequal {<br />
type equal;<br />
# Modify the line below if you do not<br />
#slave.pcm "plughw:0,0";<br />
#by default we want to play from more sources at time:<br />
slave.pcm "plug:dmix";<br />
}<br />
#pcm.equal {<br />
# If you do not want the equalizer to be your<br />
# default soundcard comment the following<br />
# line and uncomment the above line. (You can<br />
# choose it as the output device by addressing<br />
# it with specific apps,eg mpg123 -a equal 06.Back_In_Black.mp3)<br />
pcm.!default {<br />
type plug;<br />
slave.pcm plugequal;<br />
}<br />
<br />
设置好后,切换均衡器:<br />
$ alsamixer -D equal<br />
<br />
需要注意,每个用户的 alsaequal 配置文件都是独立的,位于 {{ic|~/.alsaequal.bin}}。因此,如果想在 [[mpd]] 之类的以独立用户身份运行的程序中使用 AlsaEqual,就得像下面这样:<br />
# su mpd -c 'alsamixer -D equal'<br />
或者,在其他用户的主目录下创建符号链接,指向你的 {{ic|.alsaequal.bin}}……<br />
<br />
=====管理 AlsaEqual 配置=====<br />
从 [http://xyne.archlinux.ca/repos/ Xyne 的软件仓库] 或 [https://aur.archlinux.org/packages.php?ID=62420 AUR] 安装 [http://xyne.archlinux.ca/projects/alsaequal-mgr/ alsaequal-mgr]。<br />
<br />
同上,配置均衡器: <br />
$alsamixer -D equal<br />
<br />
如果对效果感到满意,将其命名(如“foo”)并保存该配置:<br />
$alsaequal-mgr save foo<br />
<br />
重新加载“foo”中的配置:<br />
$alsaequal-mgr load foo<br />
<br />
这样,就可以为游戏、电影、不同音乐流派、网络电话等等创建不同的均衡器配置,以便随时切换。<br />
<br />
参见[http://xyne.archlinux.ca/projects/alsaequal-mgr/ 项目主页]和程序的帮助信息,了解更多设置。<br />
<br />
====使用 mbeq====<br />
{{注意|该软件用到了一个 ladspa 插件,可能导致播放音频时 CPU 占用率偏高。In addition, this was made with stereophonic sound (e.g. headphones) in mind.}}<br />
<br />
安装 {{Pkg|alsa-plugins}}、{{Pkg|ladspa}} 和 {{Pkg|swh-plugins}} 软件包。<br />
<br />
* 在 {{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}} 中添加如下内容:<br />
<br />
{{hc|/etc/asound.conf|2=<nowiki>pcm.eq {<br />
type ladspa<br />
<br />
# The output from the EQ can either go direct to a hardware device<br />
# (if you have a hardware mixer, e.g. SBLive/Audigy) or it can go<br />
# to the software mixer shown here.<br />
#slave.pcm "plughw:0,0"<br />
slave.pcm "plug:dmix"<br />
<br />
# Sometimes you may need to specify the path to the plugins,<br />
# especially if you have just installed them. Once you have logged<br />
# out/restarted this should not be necessary, but if you get errors<br />
# about being unable to find plugins, try uncommenting this.<br />
#path "/usr/lib/ladspa"<br />
<br />
plugins [<br />
{<br />
label mbeq<br />
id 1197<br />
input {<br />
#this setting is here by example, edit to your own taste<br />
#bands: 50hz, 100hz, 156hz, 220hz, 311hz, 440hz, 622hz, 880hz, 1250hz, 1750hz, 25000hz,<br />
#50000hz, 10000hz, 20000hz<br />
controls [ -5 -5 -5 -5 -5 -10 -20 -15 -10 -10 -10 -10 -10 -3 -2 ]<br />
}<br />
}<br />
]<br />
}<br />
<br />
# Redirect the default device to go via the EQ - you may want to do<br />
# this last, once you are sure everything is working. Otherwise all<br />
# your audio programs will break/crash if something has gone wrong.<br />
<br />
pcm.!default {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
<br />
# Redirect the OSS emulation through the EQ too (when programs are running through "aoss")<br />
<br />
pcm.dsp0 {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
</nowiki>}}<br />
<br />
* 完成。若有问题,欢迎前往论坛咨询。<br />
<br />
==高质量重采样==<br />
启用软件混音时,ALSA 会强制把所有音频重采样到相同的频率(如果系统支持,默认是 48000)。dmix 的重采样算法很糟糕,会导致显著的质量损失。<br />
<br />
安装 {{pkg|alsa-plugins}} 和 {{pkg|libsamplerate}}。<br />
<br />
将默认的采样率转换器更改为 libsamplerate:<br />
<br />
{{hc|/etc/asound.conf|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
或:<br />
<br />
{{hc|~/.asoundrc|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
'''samplerate_best''' 输出的声音质量最佳,但需要耗费大量CPU资源进行实时重采样。还有其他的算法可供选择(如 '''samplerate''' 等),但比起默认的算法,这些算法的改进并不明显。<br />
<br />
{{警告|某些系统中,启用 samplerate_best 可能导致 Flash 播放器无声音。}}<br />
<br />
==上混和下混==<br />
{{注意|由于未找到 upmix 和 downmix 的常用翻译,译者暂时采用字面翻译(上混、下混)。所谓“上混”,即将声道较少的立体声(如常见的双声道立体声)信号,通过软件或硬件处理,模拟为声道较多的立体声(如5.1环绕立体声)信号。反之即为“下混”。}}<br />
<br />
===上混(upmixing)===<br />
播放双声道立体声音源(如音乐)时,利用上混,即可充分利用 5.1 或 7.1 环绕立体声的所有声道。以前,上混很麻烦,还经常出错。但如今就方便多了,只需要安装一些插件即可。这些插件由 {{Pkg|alsa-plugins}} 软件包提供。<br />
<br />
安装后,在配置文件({{ic|/etc/asound.conf}} 或 {{ic|~/.asoundrc}})中添加如下内容:<br />
pcm.upmix71 {<br />
type upmix<br />
slave.pcm "surround71"<br />
delay 15<br />
channels 8<br />
}<br />
<br />
该范例适用于7.1声道上混。类比该范例,同样能设置5.1或4.0声道上混。<br />
<br />
该设置添加了一个新的用于上混的 pcm。如果希望所有音频源都通过该 pcm 输出,在上述设置后添加如下配置即可:<br />
<br />
pcm.!default "plug:upmix71"<br />
<br />
插件默认配置就允许多个音频源同时通过它输出,一般无需额外配置。如果不行,就得向下面这样为上混配置 dmixer:<br />
pcm.dmix6 {<br />
type asym<br />
playback.pcm {<br />
type dmix<br />
ipc_key 567829<br />
slave {<br />
pcm "hw:0,0"<br />
channels 6<br />
}<br />
}<br />
}<br />
<br />
注意要用 dmix6 而非 surround71。<br />
<br />
如果遇到声音卡顿或混乱,可能需要增加 buffer_size(比如增加到 32768),或使用[[#高质量重采样|高质量重采样]]。<br />
<br />
===下混(downmixing)===<br />
有时也会用到下混,比如在只支持双声道立体声的电脑上观看5.1声道的电影时。这一功能由 ALSA 插件 vdownmix 实现(同样在 {{Pkg|alsa-plugins}} 软件包里)。<br />
<br />
在配置文件中添加如下内容:<br />
<br />
pcm.!surround51 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
pcm.!surround40 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
<br />
==混音==<br />
===软件混音(dmix)===<br />
<br />
{{注意|在 ALSA 1.0.9rc2 及以上版本中,如果声卡不支持硬件混音,模拟输出将默认使用 dmix 混音,无需额外设置。}}<br />
<br />
如果软件混音未正常启用,那么可以手动创建 {{ic|~/.asoundrc}},加入以下内容:<br />
<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
<br />
这样就启用了软件混音,多个程序能够同时使用声卡。<br />
<br />
至于 S/PDIF 之类的数字音频输出,目前的 ALSA 还不会默认启用 dmix。因此,必须像上面这样手动启用。<br />
<br />
常见问题的解答,参见[[#疑难解答]]部分。<br />
<br />
===硬件混音===<br />
====支持====<br />
如果声卡支持,一般无需任何配置即可启用硬件混音。绝大多数板载声卡都不支持硬件混音,必须得使用软件混音(见上文)。很多独立声卡都支持硬件混音,Linux 下支持较好的有:<br />
<br />
* Creative SoundBlaster Live! (5.1 型号)<br />
* Creative SoundBlaster Audigy (部分型号)<br />
* Creative SoundBlaster Audidy 2 (ZS 系列型号)<br />
* Creative SoundBlaster Audigy 4 (Pro 系列型号)<br />
<br />
{{注意|上述声卡的低端版本(Audigy SE、Audigy 2 NX、SoundBlaster Live! 24bit 以及 SoundBlaster Live! 7.1)使用了不同的芯片,'''不支持'''硬件混音。}}<br />
<br />
{{注意|板载 VIA8237 芯片支持4路硬件混音,但在某些主板上只能使用3路(第4路无声),甚至根本无法使用。即使能正常工作,质量也比不上其他混音方法。}}<br />
<br />
====问题修复====<br />
如果你的声卡是 Intel Corporation 82801I (ICH9 Family) HD Audio Controller (rev 02),且使用了64位系统,需要执行以下命令才能在 Enemy Territory 中听到声音:<br />
echo "et.x86 0 0 direct" > /proc/asound/card0/pcm0p/oss<br />
echo "et.x86 0 0 disable" > /proc/asound/card0/pcm0c/oss<br />
<br />
==疑难解答==<br />
===使用CPU频率动态调整时,音频发生跳跃===<br />
使用 {{ic|ondemand}} 或 {{ic|conservative}} 频率策略、启用CPU频率动态调整时,某些声卡和某些 ALSA 驱动会出现声音跳跃的情况。目前,唯一的解决方案是使用固定的频率,如 {{ic|performance}} 性能策略。<br />
<br />
详情参见 [[CPU Frequency Scaling (简体中文)]]。<br />
<br />
===同时只能有一个用户播放音频===<br />
有时会出现一个用户独占软件混音器的情况。多数情况下这没什么影响,但在使用 [[mpd]] 这样以独立用户身份运行的程序时,就会出现问题。当 mpd 运行时,用户自己就无法播放其他音频了。一种解决方法是,以正在使用的用户身份运行 mpd。另一种方法是,在 ALSA 配置文件的 {{ic|pcm.dmixer}} 部分添加 {{ic|ipc_key_add_uid 0}} 禁止独占。以下是 {{ic|asound.conf}} 示例的片段,省略的部分无需修改:<br />
<br />
...<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
slave {<br />
...<br />
<br />
===无法同时播放多个音频===<br />
如果安装了 [[PulseAudio (简体中文)|PulseAudio]](可能作为 [[GNOME (简体中文)|GNOME]] 的依赖被安装),可能出现无法同步播放的问题,因为 PulseAudio 的默认配置是“独占”声卡。ALSA 的用户可能对自己的配置已经很满意、不希望使用 [[PulseAudio (简体中文)|PulseAudio]]。解决方案是编辑 {{ic|/etc/asound.conf}} 并注释掉以下内容:<br />
# Use PulseAudio by default<br />
#pcm.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
# hint {<br />
# show on<br />
# description "Default ALSA Output (currently PulseAudio Sound Server)"<br />
# }<br />
#}<br />
<br />
可能还需要注释掉下面的内容:<br />
#ctl.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
#}<br />
<br />
比起完全卸载 [[PulseAudio (简体中文)|PulseAudio]],这大概是个更简单的方法。<br />
<br />
以下是 {{ic|/etc/asound.conf}} 配置文件的一个示例:<br />
{{bc|<nowiki>pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
}<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}</nowiki><br />
}}<br />
<br />
{{注意|本 {{ic|/etc/asound.conf}} 本来是配合 [[MPD]] 全局配置使用的。参见[[#同时只能有一个用户播放音频|该节]]关于多用户的内容。}}<br />
<br />
===有时候开机无声===<br />
如果会随机地出现开机无声的问题,原因可能是多声卡的顺序发生了变化。如果是这种情况,请参照[[#设置默认声卡]]进行设置。<br />
<br />
对于 mpd 用户,上述配置无效,请参考[http://mpd.wikia.com/wiki/Configuration#ALSA_MPD_software_volume_control 该文]设置。<br />
<br />
===特定程序的问题===<br />
对于不使用系统声音配置的程序,如 XMMS 和 Mplayer,必须特别设置以应用 ALSA 配置。<br />
<br />
对于 mplayer,将以下内容添加到 {{ic|~/.mplayer/config}} (或系统全局配置文件 {{ic|/etc/mplayer/mplayer.conf}}):<br />
ao=alsa<br />
<br />
对于 XMMS 和 Beep Media Player,打开选项窗口,要把声音驱动设置为 ALSA 而不是 OSS。<br />
<br />
XMMS 的设置方法:<br />
* 启动 XMMS<br />
** 选项 -> 首选项<br />
** 选择 Alsa 输出插件<br />
<br />
对于未提供 ALSA 输出的程序,可以使用 alsa-oss 软件包中的 aoss 工具启动。使用方法是:在要运行的程序的命令行前加上 {{ic|aoss}}。例如:<br />
aoss realplay<br />
<br />
pcm.!default{ ... } 貌似已经不管用了。但下面的设置有效:<br />
pcm.default pcm.dmixer<br />
<br />
===特定型号的配置===<br />
ALSA 会通过 BIOS 检测[http://www.kernel.org/doc/Documentation/sound/alsa/HD-Audio-Models.txt 声卡型号],少数情况下也可能无法正确识别。声卡芯片类型可以在 {{ic|alsamixer}} 界面上找到(如 ALC662),而型号则要设置在 {{ic|/etc/modprobe.d/modprobe.conf}} 或 {{ic|/etc/modprobe.d/sound.conf}} 文件中:<br />
<br />
options snd-hda-intel model=<型号><br />
<br />
具体设置因声卡型号而异。多数情况下不需要特地这样做。如果想知道具体声卡型号的设置,请查阅 [http://bugtrack.alsa-project.org/main/index.php/Matrix:Main Alsa Soundcard List],找到你的声卡型号,点击“Details”,然后查看“Setting up modprobe...”小节,将其中的内容插入 {{ic|/etc/modprobe.d/modprobe.conf}} 文件即可。例如,Intel AC97 的配置如下:<br />
<br />
{{bc|# ALSA portion<br />
alias char-major-116 snd<br />
alias snd-card-0 snd-intel8x0<br />
# module options should go here<br />
<br />
# OSS/Free portion<br />
alias char-major-14 soundcore<br />
alias sound-slot-0 snd-card-0<br />
<br />
# card #1<br />
alias sound-service-0-0 snd-mixer-oss<br />
alias sound-service-0-1 snd-seq-oss<br />
alias sound-service-0-3 snd-pcm-oss<br />
alias sound-service-0-8 snd-seq-oss<br />
alias sound-service-0-12 snd-pcm-oss}}<br />
<br />
===与蜂鸣器冲突===<br />
如果你确认所有声道都已解除静音,声卡驱动配置正确,且音量大小也合适,但还是听不到声音,那么请试试将下面的内容添加到 {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
(如果是 {{ic|via82xx}} 驱动)<br />
options snd-<模块名请自行补充> ac97_quirk=0<br />
<br />
(如果是 {{ic|snd_intel8x0}} 驱动)<br />
options snd-NAME-OF-MODULE ac97_quirk=1<br />
<br />
===麦克风输入无声音===<br />
In alsamixer, make sure that all the volume levels are up under recording, and that CAPTURE is toggled active on the microphone (e.g. Mic, Internal Mic) and/or on Capture (in alsamixer, select these items and press space). Try making positive Mic Boost and raising Capture and Digital levels higher; this make make static or distortion, but then you can adjust them back down once you are hearing ''something'' when you record<br />
<br />
As the pulseaudio wrapper is shown as "default" in alsamixer, you may have to press F6 to select your actual soundcard first. You may also need to enable and increase the volume of Line-in in the Playback section.<br />
<br />
To test the microphone, run these commands (see arecord's man page for further information):<br />
arecord -d 5 test-mic.wav<br />
aplay test-mic.wav<br />
<br />
If all fails, you may want to eliminate hardware failure by testing the microphone with a different device.<br />
<br />
For at least some computers, muting a microphone (MM) simply means its input does not go immediately to the speakers. It still receives input.<br />
<br />
Many Dell laptops need "-dmic" to be appended to the model name in {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-hda-intel model=dell-m6-dmic<br />
<br />
Some programs use try to use OSS as the main input software. Add the following lines to {{ic|/etc/modprobe.d/modprobe.conf}} to prevent OSS modules from being loaded:<br />
<br />
{{Note|The OSS modules are no longer autoloaded anyway.}}<br />
<br />
blacklist snd_pcm_oss<br />
blacklist snd_mixer_oss<br />
blacklist snd_seq_oss<br />
<br />
See also:<br />
* http://www.alsa-project.org/main/index.php/SoundcardTesting<br />
* http://alsa.opensrc.org/Record_from_mic<br />
<br />
===设置默认的麦克风/录音设备===<br />
某些程序(Pidgin、Adobe Flash)提供了设置录音设备的选项。如果录音设备不是内置的声卡,而是外接的(如USB摄像头、麦克风),就会有些问题。如果只想更改默认的录音设备、不更改回放设备,需要像下面这样设置 {{ic|~/.asoundrc}}:<br />
<br />
pcm.usb<br />
{<br />
type hw<br />
card U0x46d0x81d<br />
}<br />
<br />
pcm.!default<br />
{<br />
type asym<br />
playback.pcm<br />
{<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
capture.pcm<br />
{<br />
type plug<br />
slave.pcm "usb"<br />
}<br />
}<br />
<br />
把“U0x46d0x81d”替换为你的录音设备的名称。可以通过 {{ic|arecord -L}} 查看所有 ALSA 识别到的录音设备。<br />
<br />
===内置麦克风无效===<br />
首先,请检查 alsamixer,确保所有的录音声道的音量都达到可以录制的程度。如果还是不行,试试修改 /etc/sound.conf 并重载 snd-* 模块。这样就会多出来一个名叫 Capture 的音频控制,可以用它调节内置麦克风的音量。例如,对于 snd-hda-intel 模块,配置如下:<br />
<br />
options snd-hda-intel enable_msi=1<br />
<br />
然后,输入下面的命令重载模块,调整音量并测试。<br />
<br />
{{bc|# rmmod snd-hda-intel && modprobe snd-hda-intel}}<br />
<br />
===Intel 板载声卡无声===<br />
可能由两个相互冲突的驱动模块引起,分别是 {{ic|snd_intel8x0}} 和 {{ic|snd_intel8x0m}}。如果是的话,屏蔽 snd_intel8x0m 即可:<br />
<br />
{{hc|/etc/modprobe.d/modprobe.conf|blacklist snd_intel8x0m}}<br />
<br />
以下方法可能也有效:在 {{ic|alsamixer}} 或 {{ic|amixer}} 把 “External Amplifier” 设置为静音。参见:[http://alsa.opensrc.org/Intel8x0#Dell_Inspiron_8600_.28and_probably_others.29 ALSA wiki 相关条目]。<br />
<br />
也可以试试解除“Mix”的静音。<br />
<br />
===Intel 板载声卡耳机无声===<br />
对于笔记本电脑上的 '''Intel Corporation 82801 I (ICH9 Family) HD Audio Controller''' 声卡,可能需要添加如下内容到 modprobe 配置文件或 sound.conf,以解决耳机无声的问题:<br />
<br />
options snd-hda-intel model=$model<br />
<br />
$model 在下列型号中选择:<br />
<br />
* dell-vostro<br />
* olpc-xo-1_5<br />
* laptop<br />
* dell-m6<br />
* laptop-hpsense<br />
<br />
{{注意|可能需要在配置文件的 "alias" 语句之后添加上述内容。}}<br />
<br />
可以在内核文档中找到所有支持的型号,如[http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.35.y.git;a=blob;f=Documentation/sound/alsa/HD-Audio-Models.txt;h=dc25bb84b83b49665a7ed850e7bf5423d50cd3ba;hb=HEAD 该文]。这是 2.6.35 版本内核的文档,你需要自己查找适用于自己内核版本的文档。<br />
<br />
也可以从[http://www.mjmwired.net/kernel/Documentation/sound/alsa/HD-Audio-Models.txt 这里]获取型号列表。要查看你的声卡芯片名称,输入下列命令(其中的“*”请根据实际情况替换)。注意,有些芯片名称被更改过,因此实际名称与文件中的可能不匹配。<br />
<br />
cat /proc/asound/card*/codec* | grep Codec<br />
<br />
注意,这么做很可能导致所有输入设备(内置和外置麦克风)无法工作,即要么让麦克风正常、要么让耳机正常。如果遇到这一问题,请向 ALSA 开发者报告。<br />
<br />
另外,如果蜂鸣器(pcspkr)出现问题,可以试试使用以下配置修复:<br />
<br />
options snd-hda-intel model=$model enable=1 index=0<br />
<br />
===安装支持 S/PDIF 输出的显卡后无声 ===<br />
检测已加载的相关模块,并记录顺序:<br />
$ cat /proc/asound/modules<br />
0 snd_hda_intel<br />
1 snd_ca0106<br />
<br />
在 {{ic|/etc/modprobe.d/modprobe.conf}} 中禁用不希望使用的显卡的音频解码模块:<br />
# /etc/modprobe.d/modprobe.conf<br />
#<br />
install snd_hda_intel /bin/false<br />
<br />
如果不同设备使用同一模块,那么只能在 BIOS 中禁用其中之一。<br />
<br />
===音效差或卡顿===<br />
如果发现音效特别差,试试在 alsamixer 中调节一下 PCM 音量,到增益(gain)为0的水平。<br />
<br />
若使用 snd-usb-audio 驱动,可以试试在 {{ic|/etc/asound.conf}} 中启用 {{ic|softvol}}。下面是针对第一个声音设备的配置范例:<br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.dsnooper {<br />
type dsnoop<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
channels 2<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.softvol {<br />
type softvol<br />
slave { pcm "dmixer" }<br />
control {<br />
name "Master"<br />
card 0<br />
}<br />
}<br />
ctl.!default {<br />
type hw<br />
card 0<br />
}<br />
ctl.softvol {<br />
type hw<br />
card 0<br />
}<br />
ctl.dmixer {<br />
type hw<br />
card 0<br />
}<br />
<br />
===开始或停止音频播放时出现噪音===<br />
某些驱动模块(如 snd_ac97_codec 和 snd_hda_intel)会在声卡闲置时关闭它以节约用电。声卡启动或关闭时,就会发出刺耳的噪音。在调节音量或开关窗口(KDE4)时也可能发生这种情况。要消除这一问题,需要添加特定模块参数(通过 {{ic|modinfo snd_MY_MODULE}} 查询)关闭该功能。<br />
<br />
例如:对于 snd_hda_intel 模块,要禁用自动节电并解决噪音问题,需在 {{ic|/etc/modprobe.d/modprobe.conf}} 中添加如下内容:<br />
options snd_hda_intel power_save=0<br />
或:<br />
options snd_hda_intel power_save=0 power_save_controller=N<br />
<br />
可以先使用 {{ic|1=modprobe snd_hda_intel power_save=0}} 测试一下效果。<br />
<br />
对于 VIA VT1708S 板载声卡(使用 snd_hda_intel 模块)等一些声卡,即使把 power_save 参数设置为0可能仍会有噪音。还得在 alsamixer 中把“Line”声道激活才能生效,只要激活并调到一个非0的音量即可(1即可,不要太高)。<br />
<br />
内核模块文档:[https://www.kernel.org/doc/Documentation/sound/alsa/powersave.txt]<br />
<br />
对于笔记本,即使在 {{ic|/etc/modprobe.d}} 设置了 {{ic|power_save}} 参数,当切换电池时 pm-utils 仍会将该值重置为1。需要禁用相关的脚步才行(详情参见:[[Pm-utils#Disabling_a_hook Disabling a hook]]):<br />
# touch /etc/pm/power.d/intel-audio-powersave<br />
<br />
===S/PDIF Output Does Not Work===<br />
<br />
If the optical/coaxial digital output of your motherboard/sound card is not working or stopped working, and have already enabled and unmuted it in alsamixer, try running<br />
# iecset audio on<br />
<br />
as root.<br />
<br />
You can also put this command in {{Ic|rc.local}} as it sometimes it may stop working after a reboot.<br />
<br />
===HDMI Output Does Not Work===<br />
If the HDMI output of your motherboard/sound card is not working or stopped working, and have already enabled and unmuted it in alsamixer, try the following.<br />
<br />
Query for Playback Devices:<br />
<br />
$ aplay -l<br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: NVidia [HDA NVidia], device 0: ALC1200 Analog [ALC1200 Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 1: ALC1200 Digital [ALC1200 Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 3: NVIDIA HDMI [NVIDIA HDMI]<br />
Subdevices: 0/1<br />
Subdevice #0: subdevice #0<br />
<br />
Now that we have the info for the HDMI device, try a test, In the example below, 0 is the card number and 3 is the device number.<br />
<br />
$ aplay -D plughw:0,3 /usr/share/sounds/alsa/Front_Center.wav<br />
<br />
If aplay does not output any errors, but still no sound is heared, "reboot" the receiver, monitor or tv set. Since the HDMI interface executes a handshake on connection, it might have noticed before that there was no audio stream embedded, and disabled audio decoding. In particular, if you are using a standalone window manager (don’t know about Gnome or KDE), you may need to have some sound playing ''while'' plugging in the HDMI cable.<br />
<br />
{{Note|If you are using an ATI card and linux 3.0, check your kernel/grub settings, you most likely need to use the ''''radeon.audio'''<nowiki>=</nowiki>'''1'''' boot parameter, or alternatively set up the option in your modprobe configuration files. It's important to note that before trying, is to check the [http://www.x.org/wiki/RadeonFeature#fndef-72849d6d6eb3927d486f50ece74a739042965a6c-25 Radeon Feature Matrix] to see if it's actually supported. At the moment this is written (march 29th, 2013), some chipsets of the [http://www.x.org/wiki/RadeonFeature#Decoder_ring_for_engineering_vs_marketing_names S. Islands] (namely HD7750 through HD7970) doesn't (still) support HDMI Audio.}}<br />
<br />
If the test is successful, create or edit your ~/.asoundrc file to set HDMI as the default audio device.<br />
<br />
{{hc|~/.asoundrc|<br />
pcm.!default {<br />
type hw<br />
card 0<br />
device 3<br />
}<br />
}}<br />
<br />
Or you above config does not work try:<br />
{{hc|~/.asoundrc|<br />
defaults.pcm.card 0<br />
defaults.pcm.device 3<br />
defaults.ctl.card 0<br />
}}<br />
<br />
===HDMI Multi-channel PCM output does not work (Intel) ===<br />
As of Linux 3.1 multi-channel PCM output through HDMI with a Intel card (Intel Eaglelake, IbexPeak/Ironlake,SandyBridge/CougarPoint and IvyBridge/PantherPoint) is not yet supported. Support for it has been recently added and expected to be available in Linux 3.2. To make it work in Linux 3.1 you need to apply the following patches:<br />
<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=76adaa34db407f174dd06370cb60f6029c33b465 drm: support routines for HDMI/DP ELD]<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=e0dac65ed45e72fe34cc7ccc76de0ba220bd38bb drm/i915: pass ELD to HDMI/DP audio driver]<br />
<br />
===HP TX2500===<br />
Add these 2 lines into {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-cmipci mpu_port=0x330 fm_port=0x388<br />
options snd-hda-intel index=0 model=toshiba position_fix=1<br />
<br />
options snd-hda-intel model=hp (works for tx2000cto)<br />
<br />
===Skipping Sound When Playing MP3===<br />
If you have sound skipping when playing MP3 files and you have more then 2 speakers attached to your computer (i.e. > 2 speaker system), run alsamixer and disable the channels for the speakers that you '''DO NOT''' have (i.e. do not enable the sound for the center speaker if you do not have a center speaker.<br />
<br />
===Using a USB Headset and External USB Sound Cards===<br />
If you are using a USB headset with ALSA you can try using {{AUR|asoundconf}} (currently only available from the [[AUR]]) to set the headset as the primary sound output. Before running make sure you have usb audio module enabled ({{ic|modprobe snd-usb-audio}}).<br />
<br />
# asoundconf is-active<br />
# asoundconf list<br />
# asoundconf set-default-card <chosen soundcard><br />
<br />
==== Crackling sound with USB devices ====<br />
If you experience crackling sound on USB devices, you can try tuning the snd-usb-audio for minimal latency.<br />
<br />
Add this to your {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
options snd-usb-audio nrpacks=1<br />
<br />
source: http://alsa.opensrc.org/Usb-audio#Tuning_USB_devices_for_minimal_latencies<br />
<br />
==== Hot-plugging a USB Sound Card ====<br />
In order to automatically make a USB Sound Card the primary output device, when the card is plugged in, you can use the following udev rules (e.g. add the following two lines to {{ic|/etc/udev/rules.d/00-local.rules}} and reboot).<br />
<br />
{{bc|1=KERNEL=="pcmC[D0-9cp]*", ACTION=="add", PROGRAM="/bin/sh -c 'K=%k; K=$${K#pcmC}; K=$${K%%D*}; echo defaults.ctl.card $$K > /etc/asound.conf; echo defaults.pcm.card $$K >>/etc/asound.conf'"<br />
KERNEL=="pcmC[D0-9cp]*", ACTION=="remove", PROGRAM="/bin/sh -c 'echo defaults.ctl.card 0 > /etc/asound.conf; echo defaults.pcm.card 0 >>/etc/asound.conf'"}}<br />
<br />
===Error 'Unknown hardware' Appears After a Kernel Update===<br />
The following messages may be displayed during the start-up ALSA after the kernel update:<br />
Unknown hardware "foo" "bar" ...<br />
Hardware is initialized using a guess method<br />
/usr/sbin/alsactl: set_control:nnnn:failed to obtain info for control #mm (No such file or directory)<br />
<br />
or<br />
Found hardware: "HDA-Intel" "VIA VT1705" "HDA:11064397,18490397,00100000" "0x1849" "0x0397"<br />
Hardware is initialized using a generic method<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #1 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #2 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #25 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #26 (No such file or directory)<br />
<br />
Simply store ALSA mixer settings again (as root):<br />
# alsactl -f /var/lib/alsa/asound.state store<br />
<br />
It may be necessary configure ALSA again with alsamixer<br />
<br />
===HDA Analyzer===<br />
If the mappings to your audio pins(plugs) do not correspond but ALSA works fine, you could try HDA Analyzer -- a pyGTK2 GUI for HD-audio control can be found [http://www.alsa-project.org/main/index.php/HDA_Analyzer at the ALSA wiki].<br />
Try tweaking the Widget Control section of the PIN nodes, to make microphones IN and headphone jacks OUT. Referring to the Config Defaults heading is a good idea.<br />
<br />
NOTE: the script is done by such way that it is incompatible with python3 (which is now shipped with ArchLinux) but tries to use it.<br />
The workaround is: open "run.py", find all occurences of "python" (2 occurences - one on the first line, and the second on the last line) and replace them all by "python2".<br />
<br />
NOTE2: the script requires root acces, but running it via su/sudo is bogus. Run it via {{ic|kdesu}} or {{ic|gksu}}.<br />
<br />
===ALSA with SDL===<br />
If you get no sound via SDL and ALSA cannot be chosen from the application. Try setting the environmental variable SDL_AUDIODRIVER to alsa.<br />
# export SDL_AUDIODRIVER=alsa<br />
<br />
===Low Sound Workaround===<br />
<br />
If you are facing low sound even after maxing out your speakers/headphones, you can give the softvol plugin a try. Add the following to {{ic|/etc/asound.conf}}.<br />
{{bc|<nowiki><br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
<br />
pcm.softvol {<br />
type softvol<br />
slave {<br />
pcm "dmix"<br />
}<br />
control {<br />
name "Pre-Amp"<br />
card 0<br />
}<br />
min_dB -5.0<br />
max_dB 20.0<br />
resolution 6<br />
}<br />
</nowiki>}}<br />
{{note| You will probably have to restart the computer, as restarting the alsa daemon did not load the new configuration for me. Also, if the configuration does not work even after restarting, try changing {{ic|plug}} with {{ic|hw}} in the above configuration.}}<br />
<br />
After the changes are loaded successfully, you will see a {{ic|Pre-Amp}} section in alsamixer. You can adjust the levels there.<br />
{{note|Setting a high value for {{ic|Pre-Amp}} can cause sound distortion, so adjust it according to the level that suits you.}}<br />
<br />
===Popping sound after resuming from suspension===<br />
You might hear a popping sound after resuming the computer from suspension. This can be fixed by editing {{ic|/etc/pm/sleep.d/90alsa}} and removing the line that says {{ic|aplay -d 1 /dev/zero}}<br />
<br />
==配置文件范例==<br />
参见:[[Advanced Linux Sound Architecture/Example Configurations]]。<br />
<br />
==相关阅读==<br />
* [http://www.mjmwired.net/kernel/Documentation/sound/alsa/ALSA-Configuration.txt ALSA 模块高级配置]<br />
* [http://alsa.opensrc.org/Main_Page 非官方的 ALSA Wiki]<br />
* [https://bbs.archlinux.org/viewtopic.php?id=36815 如何从 svn 获取驱动代码并编译]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Advanced_Linux_Sound_Architecture_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=256311
Advanced Linux Sound Architecture (简体中文)
2013-05-07T12:43:16Z
<p>Cuihao: /* Poor sound quality or clipping */</p>
<hr />
<div>[[Category:简体中文]]<br />
[[Category:Sound (简体中文)]]<br />
[[Category:Audio/Video (简体中文)]]<br />
[[cs:Advanced Linux Sound Architecture]]<br />
[[de:Alsa]]<br />
[[es:Advanced Linux Sound Architecture]]<br />
[[fa:ALSA]]<br />
[[fr:Alsa]]<br />
[[he:Advanced Linux Sound Architecture]]<br />
[[it:Advanced Linux Sound Architecture]]<br />
[[ja:Advanced Linux Sound Architecture]]<br />
[[nl:Advanced Linux Sound Architecture]]<br />
[[pt:Advanced Linux Sound Architecture]]<br />
[[ru:Advanced Linux Sound Architecture]]<br />
[[sk:Advanced Linux Sound Architecture]]<br />
[[th:Advanced Linux Sound Architecture]]<br />
[[tr:Gelişmiş_ses_mimarisi_(ALSA)]]<br />
[[en:Advanced Linux Sound Architecture]]<br />
<br />
{{Article summary start}}<br />
{{Article summary text|ALSA 的使用、配置及疑难解答。}}<br />
{{Article summary heading|相关页面}}<br />
{{Article summary wiki|Sound system (简体中文)}}<br />
{{Article summary wiki|Disable PC Speaker Beep (简体中文)}}<br />
{{Article summary end}}<br />
{{TranslationStatus (简体中文)|Advanced_Linux_Sound_Architecture|2013-04-25|254584}}<br />
<br />
[[Wikipedia:zh:ALSA|高级 Linux 声音体系]](Advanced Linux Sound Architecture,'''ALSA''')是Linux中提供声音设备驱动的内核组件,用来代替原来的开放声音系统(Open Sound System,OSSv3)。除了声音设备驱动,'''ALSA'''还包含一个用户空间的函数库,以方便开发者通过高级API使用驱动功能,而不必直接与内核驱动交互。<br />
<br />
{{注意|关于另一种声音体系,请阅读[[Open Sound System (简体中文)|开放声音系统(OSS)]]的页面。}}<br />
<br />
==安装==<br />
Arch 默认的内核已经通过一套模块提供了 ALSA,不必特别安装。<br />
<br />
[[udev (简体中文)|udev]]会在系统启动时自动检测硬件,并加载相应的声音设备驱动模块。这时,你的声卡已经可以工作了,只是所有声道默认都被设置成静音了。<br />
<br />
在本地登录(通过虚拟终端或登录管理器)的用户,都有权限播放音频并调整音量。要让远程登录的用户拥有这些权限,必须把用户[[Users and Groups (简体中文)#用户组管理|加入]] {{ic|audio}} 用户组。该组的成员可以直接访问声音设备,会导致某些程序独占音频输出(破坏软件混音),还可能影响用户快速切换和拖机(multiseat)。因此,除非真的有某些[https://wiki.ubuntu.com/Audio/TheAudioGroup 特殊需求],'''不建议'''把用户加入 {{ic|audio}} 用户组。<br />
<br />
===用户空间工具===<br />
从 [[Official Repositories (简体中文)|官方仓库]]:<br />
* [[pacman (简体中文)|安装]] {{Pkg|alsa-utils}} 软件包,其中的 {{ic|alsamixer}} 工具允许用户在控制台或终端中配置声音设备。<br />
* 安装 {{Pkg|alsa-oss}} 软件包,如果想让 OSS 应用程序与dmix(软件混音)协同工作。 <br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。}}<br />
*安装 {{Pkg|alsa-plugins}} 软件包,如果需要 [[#高质量重采样|高质量重采样]]、[[#Upmixing/Downmixing|软件模拟环绕立体声]]和其他高级特性。<br />
<br />
==解除各声道的静音==<br />
目前版本的 ALSA 安装后,所有声道'''默认是静音的''',必须手动解除。<br />
<br />
使用 {{ic|alsamixer}} 的 ncurses 界面,配置十分简单:<br />
<br />
$ alsamixer<br />
<br />
此外,还可以在命令行下使用 {{ic|amixer}}:<br />
<br />
$ amixer sset Master unmute<br />
<br />
在 {{ic|alsamixer}} 中,下方标有 {{ic|MM}} 的声道是静音的,而标有 {{ic|00}} 的通道已经启用。<br />
<br />
使用左右方向键,选中 {{ic|Master}} 和 {{ic|PCM}} 声道。按下 {{keypress|m}} 键解除静音。使用上方向键增加音量,直到增益值为{{ic|0}}。该值显示在左上方 {{ic|Item:}} 字段后。过高的增益值会导致声音失真。<br />
<br />
要想得到完整的 5.1 或 7.1 环绕立体声,还得解除 Front、Surround、Center、LFE (subwoofer) 和 Side 这些声道的静音(上述名称是 Intel HD Audio 声卡使用的声道名,可能因设备不同而有所差异)。注意,仅有这些设置,系统不会自动将立体声源(多数音乐)提升(upmix)成环绕立体声。如果需要这些功能,请阅读[[#Upmixing/Downmixing]]。<br />
<br />
要启用麦克风,切换至 Capture 选项卡,按下 {{keypress|F4}},按下 {{keypress|空格}} 启用其中一个声道即可。<br />
<br />
按下 {{Keypress|Esc}} 键退出 alsamixer。<br />
<br />
{{注意|<br />
* 有一些声卡,需要关闭数字输出或将其调成静音,才能输出模拟音频信号。对于 Soundblaster Audigy LS 声卡,需要把 IEC958 通道设成静音。 <br />
* 有些机器(如 Thinkpad T61),有一个 Speaker 通道,必须按照上述方法打开调整。<br />
* 有些机器(如 Dell E6400),可能还需要把 {{ic|Front}} 和 {{ic|Headphone}} 声道打开调整。}}<br />
<br />
接下来,测试声卡是否工作:<br />
<br />
$ speaker-test -c 2<br />
<br />
根据扬声器的实际情况,调整 -c 后的数值。对于 7.1 声道,这个数字是 8:<br />
<br />
$ speaker-test -c 8<br />
<br />
如果没有正常工作,请继续阅读 [[#配置]] 以及 [[#疑难解答]] 部分。<br />
<br />
{{pkg|alsa-utils}} 软件包提供了两个配置 alsa 的服务:{{ic|alsa-restore.service}} 和 {{ic|alsa-store.service}}。它们分别在开机和关机时自动运行。<br />
<br />
==配置==<br />
=== VirtualBox 虚拟机无声音 ===<br />
<br />
如果使用 VirtualBox 时碰上了麻烦,请尝试以下命令:<br />
<br />
{{hc|$ alsactl init|2=<nowiki><br />
<br />
Found hardware: "ICH" "SigmaTel STAC9700,83,84" "AC97a:83847600" "0x8086" "0x0000"<br />
Hardware is initialized using a generic method<br />
</nowiki><br />
}}<br />
<br />
如果还是不行,请检查所使用的音频软件是否激活了 ALSA 输出。<br />
<br />
===设置默认声卡===<br />
<br />
如果发现开机时声卡次序会发生变化,可以在通过 {{ic|/etc/modprobe.d}} 中的 {{ic|.conf}} 文件(比如 {{ic|/etc/modprobe.d/alsa-base.conf}})手动设置次序。<br />
比如,要让 mia 声卡成为 #0、Intel HDA 声卡成为 #1:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<br />
options snd slots=snd_mia,snd_hda_intel<br />
options snd_mia index=0<br />
options snd_hda_intel index=1<br />
}}<br />
<br />
使用 {{ic|<nowiki>lsmod | grep snd</nowiki>}} 获取设备列表。<br />
<br />
snd_mia 和 snd_hda_intel 分别是两个声卡的内核驱动模块。该配置仅适用于同时装有 mia 声卡和使用 snd_hda_intel 驱动的声卡(通常是板载声卡)的情况。<br />
<br />
如果使用 -2 的 index 值,ALSA 就不会将对应的设备作为主声卡使用。Linux Mint 和 Ubuntu 等发行版使用了以下配置,避免 USB 声卡和其他“非主流”设备变成 index 为 0 的主声卡:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<br />
options bt87x index=-2<br />
options cx88_alsa index=-2<br />
options saa7134-alsa index=-2<br />
options snd-atiixp-modem index=-2<br />
options snd-intel8x0m index=-2<br />
options snd-via82xx-modem index=-2<br />
options snd-usb-audio index=-2<br />
options snd-usb-caiaq index=-2<br />
options snd-usb-ua101 index=-2<br />
options snd-usb-us122l index=-2<br />
options snd-usb-usx2y index=-2<br />
# Keep snd-pcsp from being loaded as first soundcard<br />
options snd-pcsp index=-2<br />
# Keep snd-usb-audio from beeing loaded as first soundcard<br />
options snd-usb-audio index=-2<br />
}}<br />
<br />
以上配置会在系统重启后生效。<br />
<br />
{{Out of date|据英文 Wiki 用户反应,{{ic|/etc/asound.conf}} 这个配置文件默认是不存在的,下列操作可能有误。}}<br />
<br />
首先,运行 {{ic|aplay -l}},获取声卡的声卡ID和设备ID:<br />
<br />
{{hc|$ aplay -l|2=<nowiki><br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: Intel [HDA Intel], device 0: CONEXANT Analog [CONEXANT Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: Intel [HDA Intel], device 1: Conexant Digital [Conexant Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 1: JamLab [JamLab], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 2: Audio [Altec Lansing XT1 - USB Audio], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
</nowiki>}}<br />
<br />
拿输出中的最后一项来说,这个声卡的声卡ID为2,设备ID为0。若要把这块声卡作为默认声卡,可以把下列配置添加到系统级别的 {{ic|/etc/asound.conf}} 或用户级别的 {{ic|~/.asoundrc}} 文件。如果文件不存在,可以手动创建。其中的各个ID,请根据实际情况调整:<br />
{{bc|<br />
defaults.pcm.card 2<br />
defaults.pcm.device 0<br />
defaults.ctl.card 2<br />
}}<br />
“pcm”选项决定用来播放音频的设备,而“ctl”选项决定那个声卡能够由控制工具(如 alsamixer)使用。<br />
<br />
上述配置在重启音频程序(如 mplayer)后立即生效。<br />
<br />
===确认所有声音模块都已经加载===<br />
<br />
一般 udev 都会自动识别出声卡。使用以下命令确认:<br />
<br />
{{hc|<nowiki>$ lsmod | grep '^snd' | column -t</nowiki>|<nowiki><br />
snd_hda_codec_hdmi 22378 4<br />
snd_hda_codec_realtek 294191 1<br />
snd_hda_intel 21738 1<br />
snd_hda_codec 73739 3 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel<br />
snd_hwdep 6134 1 snd_hda_codec<br />
snd_pcm 71032 3 snd_hda_codec_hdmi,snd_hda_intel,snd_hda_codec<br />
snd_timer 18992 1 snd_pcm<br />
snd 55132 9 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel,snd_hda_codec,snd_hwdep,snd_pcm,snd_timer<br />
snd_page_alloc 7017 2 snd_hda_intel,snd_pcm<br />
</nowiki>}}<br />
<br />
如果你的输出和上面类似,那就说明声卡已经被正确识别。<br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。如有需要,请[[Kernel_modules_(简体中文)#装入|手动加载]]。}}<br />
<br />
还可以检查一下 {{ic|/dev/snd/}} 目录,看看是否有这些设备文件:<br />
<br />
{{hc|$ ls -l /dev/snd|2=<nowiki><br />
total 0<br />
crw-rw---- 1 root audio 116, 0 Apr 8 14:17 controlC0<br />
crw-rw---- 1 root audio 116, 32 Apr 8 14:17 controlC1<br />
crw-rw---- 1 root audio 116, 24 Apr 8 14:17 pcmC0D0c<br />
crw-rw---- 1 root audio 116, 16 Apr 8 14:17 pcmC0D0p<br />
crw-rw---- 1 root audio 116, 25 Apr 8 14:17 pcmC0D1c<br />
crw-rw---- 1 root audio 116, 56 Apr 8 14:17 pcmC1D0c<br />
crw-rw---- 1 root audio 116, 48 Apr 8 14:17 pcmC1D0p<br />
crw-rw---- 1 root audio 116, 1 Apr 8 14:17 seq<br />
crw-rw---- 1 root audio 116, 33 Apr 8 14:17 timer<br />
</nowiki>}}<br />
<br />
{{注意|在 IRC 或论坛寻求这方面帮助时,别忘了贴出上面几个命令的输出。}}<br />
<br />
如果你的输出跟上面类似,或至少有 '''controlC0''' 和 '''pcmC0D0p''',那么声卡就已经正常加载了。<br />
<br />
如果出现问题,声卡模块没有正确加载,那么请尝试手动加载模块:<br />
<br />
* 确定声卡对应的驱动模块:[http://www.alsa-project.org/main/index.php/Matrix:Main ALSA Soundcard Matrix]。这些模块都会有一个”snd-“前缀(例如:{{ic|snd-via82xx}})。<br />
* [[Kernel_modules_(简体中文)#装入|加载模块]]。<br />
* 检查 {{ic|/dev/snd}} 目录中的设备文件(参见上文);或者,检查 {{ic|alsamixer}} 或 {{ic|amixer}} 的输出是否正确。<br />
* 修改配置,使 {{ic|snd-<模块名>}} 和 {{ic|snd-pcm-oss}} 模块[[Kernel_modules_(简体中文)#装入|开机自动加载]]。<br />
<br />
===启用 SPDIF 输出===<br />
<br />
(由 Gentoo 论坛的 gralves 提供)<br />
* 在 GNOME 音量控制的选项(Options)标签中,把 IEC958 改成 PCM。该选项可以在首选项中启用。<br />
* 不使用 GNOME 音量控制工具的话:<br />
** 编辑 {{ic|/var/lib/alsa/asound.state}}。该文件是 alsasound 保存混音器设置的地方。<br />
** 找到这一行:“IEC958 Playback Switch”。该行上下应该有一行内容为 {{ic|value:false}},将其改为 {{ic|value:true}}。<br />
** 再找到这一行:”IEC958 Playback AC97-SPSA“。把对应的值改为 0。<br />
** 重启 ALSA。<br />
<br />
{{Out of date|rc.local 在 systemd 中已经弃用,以下方法可能不再适用。}}<br />
<br />
还有另外一个开机自动启用 SPDIF 输出的方法(在 SoundBlaster Audigy 声卡上测试过):<br />
<br />
* 在 {{ic|/etc/rc.local}} 中添加如下内容:<br />
# Use COAX-digital output<br />
amixer set 'IEC958 Optical' 100 unmute<br />
amixer set 'Audigy Analog/Digital Output Jack' on<br />
<br />
使用以下命令,获取你的声卡的数字输出的名称:<br />
$ amixer scontrols<br />
<br />
===系统级均衡器===<br />
====使用 AlsaEqual(包含界面)====<br />
从 [[AUR (简体中文)|AUR]] 安装 {{AUR|alsaequal}}。<br />
<br />
{{注意|如果使用的是64位系统,而又安装了32位的 Flash 插件,这里的设置会导致 Flash 无声。如果发生了这种情况,请禁用 alsaequal 或编译32位版本的 alsaequal。}}<br />
<br />
安装后,把下列内容添加到 ALSA 配置文件({{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}}):<br />
<br />
ctl.equal {<br />
type equal;<br />
}<br />
<br />
pcm.plugequal {<br />
type equal;<br />
# Modify the line below if you do not<br />
#slave.pcm "plughw:0,0";<br />
#by default we want to play from more sources at time:<br />
slave.pcm "plug:dmix";<br />
}<br />
#pcm.equal {<br />
# If you do not want the equalizer to be your<br />
# default soundcard comment the following<br />
# line and uncomment the above line. (You can<br />
# choose it as the output device by addressing<br />
# it with specific apps,eg mpg123 -a equal 06.Back_In_Black.mp3)<br />
pcm.!default {<br />
type plug;<br />
slave.pcm plugequal;<br />
}<br />
<br />
设置好后,切换均衡器:<br />
$ alsamixer -D equal<br />
<br />
需要注意,每个用户的 alsaequal 配置文件都是独立的,位于 {{ic|~/.alsaequal.bin}}。因此,如果想在 [[mpd]] 之类的以独立用户身份运行的程序中使用 AlsaEqual,就得像下面这样:<br />
# su mpd -c 'alsamixer -D equal'<br />
或者,在其他用户的主目录下创建符号链接,指向你的 {{ic|.alsaequal.bin}}……<br />
<br />
=====管理 AlsaEqual 配置=====<br />
从 [http://xyne.archlinux.ca/repos/ Xyne 的软件仓库] 或 [https://aur.archlinux.org/packages.php?ID=62420 AUR] 安装 [http://xyne.archlinux.ca/projects/alsaequal-mgr/ alsaequal-mgr]。<br />
<br />
同上,配置均衡器: <br />
$alsamixer -D equal<br />
<br />
如果对效果感到满意,将其命名(如“foo”)并保存该配置:<br />
$alsaequal-mgr save foo<br />
<br />
重新加载“foo”中的配置:<br />
$alsaequal-mgr load foo<br />
<br />
这样,就可以为游戏、电影、不同音乐流派、网络电话等等创建不同的均衡器配置,以便随时切换。<br />
<br />
参见[http://xyne.archlinux.ca/projects/alsaequal-mgr/ 项目主页]和程序的帮助信息,了解更多设置。<br />
<br />
====使用 mbeq====<br />
{{注意|该软件用到了一个 ladspa 插件,可能导致播放音频时 CPU 占用率偏高。In addition, this was made with stereophonic sound (e.g. headphones) in mind.}}<br />
<br />
安装 {{Pkg|alsa-plugins}}、{{Pkg|ladspa}} 和 {{Pkg|swh-plugins}} 软件包。<br />
<br />
* 在 {{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}} 中添加如下内容:<br />
<br />
{{hc|/etc/asound.conf|2=<nowiki>pcm.eq {<br />
type ladspa<br />
<br />
# The output from the EQ can either go direct to a hardware device<br />
# (if you have a hardware mixer, e.g. SBLive/Audigy) or it can go<br />
# to the software mixer shown here.<br />
#slave.pcm "plughw:0,0"<br />
slave.pcm "plug:dmix"<br />
<br />
# Sometimes you may need to specify the path to the plugins,<br />
# especially if you have just installed them. Once you have logged<br />
# out/restarted this should not be necessary, but if you get errors<br />
# about being unable to find plugins, try uncommenting this.<br />
#path "/usr/lib/ladspa"<br />
<br />
plugins [<br />
{<br />
label mbeq<br />
id 1197<br />
input {<br />
#this setting is here by example, edit to your own taste<br />
#bands: 50hz, 100hz, 156hz, 220hz, 311hz, 440hz, 622hz, 880hz, 1250hz, 1750hz, 25000hz,<br />
#50000hz, 10000hz, 20000hz<br />
controls [ -5 -5 -5 -5 -5 -10 -20 -15 -10 -10 -10 -10 -10 -3 -2 ]<br />
}<br />
}<br />
]<br />
}<br />
<br />
# Redirect the default device to go via the EQ - you may want to do<br />
# this last, once you are sure everything is working. Otherwise all<br />
# your audio programs will break/crash if something has gone wrong.<br />
<br />
pcm.!default {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
<br />
# Redirect the OSS emulation through the EQ too (when programs are running through "aoss")<br />
<br />
pcm.dsp0 {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
</nowiki>}}<br />
<br />
* 完成。若有问题,欢迎前往论坛咨询。<br />
<br />
==高质量重采样==<br />
启用软件混音时,ALSA 会强制把所有音频重采样到相同的频率(如果系统支持,默认是 48000)。dmix 的重采样算法很糟糕,会导致显著的质量损失。<br />
<br />
安装 {{pkg|alsa-plugins}} 和 {{pkg|libsamplerate}}。<br />
<br />
将默认的采样率转换器更改为 libsamplerate:<br />
<br />
{{hc|/etc/asound.conf|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
或:<br />
<br />
{{hc|~/.asoundrc|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
'''samplerate_best''' 输出的声音质量最佳,但需要耗费大量CPU资源进行实时重采样。还有其他的算法可供选择(如 '''samplerate''' 等),但比起默认的算法,这些算法的改进并不明显。<br />
<br />
{{警告|某些系统中,启用 samplerate_best 可能导致 Flash 播放器无声音。}}<br />
<br />
==上混和下混==<br />
{{注意|由于未找到 upmix 和 downmix 的常用翻译,译者暂时采用字面翻译(上混、下混)。所谓“上混”,即将声道较少的立体声(如常见的双声道立体声)信号,通过软件或硬件处理,模拟为声道较多的立体声(如5.1环绕立体声)信号。反之即为“下混”。}}<br />
<br />
===上混(upmixing)===<br />
播放双声道立体声音源(如音乐)时,利用上混,即可充分利用 5.1 或 7.1 环绕立体声的所有声道。以前,上混很麻烦,还经常出错。但如今就方便多了,只需要安装一些插件即可。这些插件由 {{Pkg|alsa-plugins}} 软件包提供。<br />
<br />
安装后,在配置文件({{ic|/etc/asound.conf}} 或 {{ic|~/.asoundrc}})中添加如下内容:<br />
pcm.upmix71 {<br />
type upmix<br />
slave.pcm "surround71"<br />
delay 15<br />
channels 8<br />
}<br />
<br />
该范例适用于7.1声道上混。类比该范例,同样能设置5.1或4.0声道上混。<br />
<br />
该设置添加了一个新的用于上混的 pcm。如果希望所有音频源都通过该 pcm 输出,在上述设置后添加如下配置即可:<br />
<br />
pcm.!default "plug:upmix71"<br />
<br />
插件默认配置就允许多个音频源同时通过它输出,一般无需额外配置。如果不行,就得向下面这样为上混配置 dmixer:<br />
pcm.dmix6 {<br />
type asym<br />
playback.pcm {<br />
type dmix<br />
ipc_key 567829<br />
slave {<br />
pcm "hw:0,0"<br />
channels 6<br />
}<br />
}<br />
}<br />
<br />
注意要用 dmix6 而非 surround71。<br />
<br />
如果遇到声音卡顿或混乱,可能需要增加 buffer_size(比如增加到 32768),或使用[[#高质量重采样|高质量重采样]]。<br />
<br />
===下混(downmixing)===<br />
有时也会用到下混,比如在只支持双声道立体声的电脑上观看5.1声道的电影时。这一功能由 ALSA 插件 vdownmix 实现(同样在 {{Pkg|alsa-plugins}} 软件包里)。<br />
<br />
在配置文件中添加如下内容:<br />
<br />
pcm.!surround51 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
pcm.!surround40 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
<br />
==混音==<br />
===软件混音(dmix)===<br />
<br />
{{注意|在 ALSA 1.0.9rc2 及以上版本中,如果声卡不支持硬件混音,模拟输出将默认使用 dmix 混音,无需额外设置。}}<br />
<br />
如果软件混音未正常启用,那么可以手动创建 {{ic|~/.asoundrc}},加入以下内容:<br />
<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
<br />
这样就启用了软件混音,多个程序能够同时使用声卡。<br />
<br />
至于 S/PDIF 之类的数字音频输出,目前的 ALSA 还不会默认启用 dmix。因此,必须像上面这样手动启用。<br />
<br />
常见问题的解答,参见[[#疑难解答]]部分。<br />
<br />
===硬件混音===<br />
====支持====<br />
如果声卡支持,一般无需任何配置即可启用硬件混音。绝大多数板载声卡都不支持硬件混音,必须得使用软件混音(见上文)。很多独立声卡都支持硬件混音,Linux 下支持较好的有:<br />
<br />
* Creative SoundBlaster Live! (5.1 型号)<br />
* Creative SoundBlaster Audigy (部分型号)<br />
* Creative SoundBlaster Audidy 2 (ZS 系列型号)<br />
* Creative SoundBlaster Audigy 4 (Pro 系列型号)<br />
<br />
{{注意|上述声卡的低端版本(Audigy SE、Audigy 2 NX、SoundBlaster Live! 24bit 以及 SoundBlaster Live! 7.1)使用了不同的芯片,'''不支持'''硬件混音。}}<br />
<br />
{{注意|板载 VIA8237 芯片支持4路硬件混音,但在某些主板上只能使用3路(第4路无声),甚至根本无法使用。即使能正常工作,质量也比不上其他混音方法。}}<br />
<br />
====问题修复====<br />
如果你的声卡是 Intel Corporation 82801I (ICH9 Family) HD Audio Controller (rev 02),且使用了64位系统,需要执行以下命令才能在 Enemy Territory 中听到声音:<br />
echo "et.x86 0 0 direct" > /proc/asound/card0/pcm0p/oss<br />
echo "et.x86 0 0 disable" > /proc/asound/card0/pcm0c/oss<br />
<br />
==疑难解答==<br />
===使用CPU频率动态调整时,音频发生跳跃===<br />
使用 {{ic|ondemand}} 或 {{ic|conservative}} 频率策略、启用CPU频率动态调整时,某些声卡和某些 ALSA 驱动会出现声音跳跃的情况。目前,唯一的解决方案是使用固定的频率,如 {{ic|performance}} 性能策略。<br />
<br />
详情参见 [[CPU Frequency Scaling (简体中文)]]。<br />
<br />
===同时只能有一个用户播放音频===<br />
有时会出现一个用户独占软件混音器的情况。多数情况下这没什么影响,但在使用 [[mpd]] 这样以独立用户身份运行的程序时,就会出现问题。当 mpd 运行时,用户自己就无法播放其他音频了。一种解决方法是,以正在使用的用户身份运行 mpd。另一种方法是,在 ALSA 配置文件的 {{ic|pcm.dmixer}} 部分添加 {{ic|ipc_key_add_uid 0}} 禁止独占。以下是 {{ic|asound.conf}} 示例的片段,省略的部分无需修改:<br />
<br />
...<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
slave {<br />
...<br />
<br />
===无法同时播放多个音频===<br />
如果安装了 [[PulseAudio (简体中文)|PulseAudio]](可能作为 [[GNOME (简体中文)|GNOME]] 的依赖被安装),可能出现无法同步播放的问题,因为 PulseAudio 的默认配置是“独占”声卡。ALSA 的用户可能对自己的配置已经很满意、不希望使用 [[PulseAudio (简体中文)|PulseAudio]]。解决方案是编辑 {{ic|/etc/asound.conf}} 并注释掉以下内容:<br />
# Use PulseAudio by default<br />
#pcm.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
# hint {<br />
# show on<br />
# description "Default ALSA Output (currently PulseAudio Sound Server)"<br />
# }<br />
#}<br />
<br />
可能还需要注释掉下面的内容:<br />
#ctl.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
#}<br />
<br />
比起完全卸载 [[PulseAudio (简体中文)|PulseAudio]],这大概是个更简单的方法。<br />
<br />
以下是 {{ic|/etc/asound.conf}} 配置文件的一个示例:<br />
{{bc|<nowiki>pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
}<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}</nowiki><br />
}}<br />
<br />
{{注意|本 {{ic|/etc/asound.conf}} 本来是配合 [[MPD]] 全局配置使用的。参见[[#同时只能有一个用户播放音频|该节]]关于多用户的内容。}}<br />
<br />
===有时候开机无声===<br />
如果会随机地出现开机无声的问题,原因可能是多声卡的顺序发生了变化。如果是这种情况,请参照[[#设置默认声卡]]进行设置。<br />
<br />
对于 mpd 用户,上述配置无效,请参考[http://mpd.wikia.com/wiki/Configuration#ALSA_MPD_software_volume_control 该文]设置。<br />
<br />
===特定程序的问题===<br />
对于不使用系统声音配置的程序,如 XMMS 和 Mplayer,必须特别设置以应用 ALSA 配置。<br />
<br />
对于 mplayer,将以下内容添加到 {{ic|~/.mplayer/config}} (或系统全局配置文件 {{ic|/etc/mplayer/mplayer.conf}}):<br />
ao=alsa<br />
<br />
对于 XMMS 和 Beep Media Player,打开选项窗口,要把声音驱动设置为 ALSA 而不是 OSS。<br />
<br />
XMMS 的设置方法:<br />
* 启动 XMMS<br />
** 选项 -> 首选项<br />
** 选择 Alsa 输出插件<br />
<br />
对于未提供 ALSA 输出的程序,可以使用 alsa-oss 软件包中的 aoss 工具启动。使用方法是:在要运行的程序的命令行前加上 {{ic|aoss}}。例如:<br />
aoss realplay<br />
<br />
pcm.!default{ ... } 貌似已经不管用了。但下面的设置有效:<br />
pcm.default pcm.dmixer<br />
<br />
===特定型号的配置===<br />
ALSA 会通过 BIOS 检测[http://www.kernel.org/doc/Documentation/sound/alsa/HD-Audio-Models.txt 声卡型号],少数情况下也可能无法正确识别。声卡芯片类型可以在 {{ic|alsamixer}} 界面上找到(如 ALC662),而型号则要设置在 {{ic|/etc/modprobe.d/modprobe.conf}} 或 {{ic|/etc/modprobe.d/sound.conf}} 文件中:<br />
<br />
options snd-hda-intel model=<型号><br />
<br />
具体设置因声卡型号而异。多数情况下不需要特地这样做。如果想知道具体声卡型号的设置,请查阅 [http://bugtrack.alsa-project.org/main/index.php/Matrix:Main Alsa Soundcard List],找到你的声卡型号,点击“Details”,然后查看“Setting up modprobe...”小节,将其中的内容插入 {{ic|/etc/modprobe.d/modprobe.conf}} 文件即可。例如,Intel AC97 的配置如下:<br />
<br />
{{bc|# ALSA portion<br />
alias char-major-116 snd<br />
alias snd-card-0 snd-intel8x0<br />
# module options should go here<br />
<br />
# OSS/Free portion<br />
alias char-major-14 soundcore<br />
alias sound-slot-0 snd-card-0<br />
<br />
# card #1<br />
alias sound-service-0-0 snd-mixer-oss<br />
alias sound-service-0-1 snd-seq-oss<br />
alias sound-service-0-3 snd-pcm-oss<br />
alias sound-service-0-8 snd-seq-oss<br />
alias sound-service-0-12 snd-pcm-oss}}<br />
<br />
===与蜂鸣器冲突===<br />
如果你确认所有声道都已解除静音,声卡驱动配置正确,且音量大小也合适,但还是听不到声音,那么请试试将下面的内容添加到 {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
(如果是 {{ic|via82xx}} 驱动)<br />
options snd-<模块名请自行补充> ac97_quirk=0<br />
<br />
(如果是 {{ic|snd_intel8x0}} 驱动)<br />
options snd-NAME-OF-MODULE ac97_quirk=1<br />
<br />
===麦克风输入无声音===<br />
In alsamixer, make sure that all the volume levels are up under recording, and that CAPTURE is toggled active on the microphone (e.g. Mic, Internal Mic) and/or on Capture (in alsamixer, select these items and press space). Try making positive Mic Boost and raising Capture and Digital levels higher; this make make static or distortion, but then you can adjust them back down once you are hearing ''something'' when you record<br />
<br />
As the pulseaudio wrapper is shown as "default" in alsamixer, you may have to press F6 to select your actual soundcard first. You may also need to enable and increase the volume of Line-in in the Playback section.<br />
<br />
To test the microphone, run these commands (see arecord's man page for further information):<br />
arecord -d 5 test-mic.wav<br />
aplay test-mic.wav<br />
<br />
If all fails, you may want to eliminate hardware failure by testing the microphone with a different device.<br />
<br />
For at least some computers, muting a microphone (MM) simply means its input does not go immediately to the speakers. It still receives input.<br />
<br />
Many Dell laptops need "-dmic" to be appended to the model name in {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-hda-intel model=dell-m6-dmic<br />
<br />
Some programs use try to use OSS as the main input software. Add the following lines to {{ic|/etc/modprobe.d/modprobe.conf}} to prevent OSS modules from being loaded:<br />
<br />
{{Note|The OSS modules are no longer autoloaded anyway.}}<br />
<br />
blacklist snd_pcm_oss<br />
blacklist snd_mixer_oss<br />
blacklist snd_seq_oss<br />
<br />
See also:<br />
* http://www.alsa-project.org/main/index.php/SoundcardTesting<br />
* http://alsa.opensrc.org/Record_from_mic<br />
<br />
===设置默认的麦克风/录音设备===<br />
某些程序(Pidgin、Adobe Flash)提供了设置录音设备的选项。如果录音设备不是内置的声卡,而是外接的(如USB摄像头、麦克风),就会有些问题。如果只想更改默认的录音设备、不更改回放设备,需要像下面这样设置 {{ic|~/.asoundrc}}:<br />
<br />
pcm.usb<br />
{<br />
type hw<br />
card U0x46d0x81d<br />
}<br />
<br />
pcm.!default<br />
{<br />
type asym<br />
playback.pcm<br />
{<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
capture.pcm<br />
{<br />
type plug<br />
slave.pcm "usb"<br />
}<br />
}<br />
<br />
把“U0x46d0x81d”替换为你的录音设备的名称。可以通过 {{ic|arecord -L}} 查看所有 ALSA 识别到的录音设备。<br />
<br />
===内置麦克风无效===<br />
首先,请检查 alsamixer,确保所有的录音声道的音量都达到可以录制的程度。如果还是不行,试试修改 /etc/sound.conf 并重载 snd-* 模块。这样就会多出来一个名叫 Capture 的音频控制,可以用它调节内置麦克风的音量。例如,对于 snd-hda-intel 模块,配置如下:<br />
<br />
options snd-hda-intel enable_msi=1<br />
<br />
然后,输入下面的命令重载模块,调整音量并测试。<br />
<br />
{{bc|# rmmod snd-hda-intel && modprobe snd-hda-intel}}<br />
<br />
===Intel 板载声卡无声===<br />
可能由两个相互冲突的驱动模块引起,分别是 {{ic|snd_intel8x0}} 和 {{ic|snd_intel8x0m}}。如果是的话,屏蔽 snd_intel8x0m 即可:<br />
<br />
{{hc|/etc/modprobe.d/modprobe.conf|blacklist snd_intel8x0m}}<br />
<br />
以下方法可能也有效:在 {{ic|alsamixer}} 或 {{ic|amixer}} 把 “External Amplifier” 设置为静音。参见:[http://alsa.opensrc.org/Intel8x0#Dell_Inspiron_8600_.28and_probably_others.29 ALSA wiki 相关条目]。<br />
<br />
也可以试试解除“Mix”的静音。<br />
<br />
===Intel 板载声卡耳机无声===<br />
对于笔记本电脑上的 '''Intel Corporation 82801 I (ICH9 Family) HD Audio Controller''' 声卡,可能需要添加如下内容到 modprobe 配置文件或 sound.conf,以解决耳机无声的问题:<br />
<br />
options snd-hda-intel model=$model<br />
<br />
$model 在下列型号中选择:<br />
<br />
* dell-vostro<br />
* olpc-xo-1_5<br />
* laptop<br />
* dell-m6<br />
* laptop-hpsense<br />
<br />
{{注意|可能需要在配置文件的 "alias" 语句之后添加上述内容。}}<br />
<br />
可以在内核文档中找到所有支持的型号,如[http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.35.y.git;a=blob;f=Documentation/sound/alsa/HD-Audio-Models.txt;h=dc25bb84b83b49665a7ed850e7bf5423d50cd3ba;hb=HEAD 该文]。这是 2.6.35 版本内核的文档,你需要自己查找适用于自己内核版本的文档。<br />
<br />
也可以从[http://www.mjmwired.net/kernel/Documentation/sound/alsa/HD-Audio-Models.txt 这里]获取型号列表。要查看你的声卡芯片名称,输入下列命令(其中的“*”请根据实际情况替换)。注意,有些芯片名称被更改过,因此实际名称与文件中的可能不匹配。<br />
<br />
cat /proc/asound/card*/codec* | grep Codec<br />
<br />
注意,这么做很可能导致所有输入设备(内置和外置麦克风)无法工作,即要么让麦克风正常、要么让耳机正常。如果遇到这一问题,请向 ALSA 开发者报告。<br />
<br />
另外,如果蜂鸣器(pcspkr)出现问题,可以试试使用以下配置修复:<br />
<br />
options snd-hda-intel model=$model enable=1 index=0<br />
<br />
===安装支持 S/PDIF 输出的显卡后无声 ===<br />
检测已加载的相关模块,并记录顺序:<br />
$ cat /proc/asound/modules<br />
0 snd_hda_intel<br />
1 snd_ca0106<br />
<br />
在 {{ic|/etc/modprobe.d/modprobe.conf}} 中禁用不希望使用的显卡的音频解码模块:<br />
# /etc/modprobe.d/modprobe.conf<br />
#<br />
install snd_hda_intel /bin/false<br />
<br />
如果不同设备使用同一模块,那么只能在 BIOS 中禁用其中之一。<br />
<br />
===音效差或卡顿===<br />
如果发现音效特别差,试试在 alsamixer 中调节一下 PCM 音量,到增益(gain)为0的水平。<br />
<br />
若使用 snd-usb-audio 驱动,可以试试在 {{ic|/etc/asound.conf}} 中启用 {{ic|softvol}}。下面是针对第一个声音设备的配置范例:<br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.dsnooper {<br />
type dsnoop<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
channels 2<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.softvol {<br />
type softvol<br />
slave { pcm "dmixer" }<br />
control {<br />
name "Master"<br />
card 0<br />
}<br />
}<br />
ctl.!default {<br />
type hw<br />
card 0<br />
}<br />
ctl.softvol {<br />
type hw<br />
card 0<br />
}<br />
ctl.dmixer {<br />
type hw<br />
card 0<br />
}<br />
<br />
===Pops When Starting and Stopping Playback===<br />
Some modules (e.g. snd_ac97_codec and snd_hda_intel) can power off your sound card when not in use. This can make an audible noise (like a crack/pop/scratch) when turning on/off your sound card. Sometimes even when move the slider volume, or open and close windows (KDE4). If you find this annoying try {{ic|modinfo snd_MY_MODULE}}, and look for a module option that adjusts or disables this feature.<br />
<br />
Example: to disable the power saving mode and solve cracking sound trough speakers issue, using snd_hda_intel add in {{ic|/etc/modprobe.d/modprobe.conf}}<br />
options snd_hda_intel power_save=0<br />
or<br />
options snd_hda_intel power_save=0 power_save_controller=N<br />
<br />
You can also try it with {{ic|1=modprobe snd_hda_intel power_save=0}} before.<br />
<br />
You may also have to unmute the 'Line' ALSA channel for this to work. Any value will do (other than '0' or something too high).<br />
<br />
''Example:'' on an onboard VIA VT1708S (using the snd_hda_intel module) these cracks occured even though 'power_save' was set to 0. Unmuting the 'Line' channel and setting a value of '1' solved the issue.<br />
<br />
Source: https://www.kernel.org/doc/Documentation/sound/alsa/powersave.txt<br />
<br />
If you use a laptop, pm-utils will change {{ic|power_save}} back to 1 when you go onto battery power even if you disable power saving in {{ic|/etc/modprobe.d}}. Disable this for pm-utils by disabling the script that makes the change (see [https://wiki.archlinux.org/index.php/Pm-utils#Disabling_a_hook Disabling a hook] for more information):<br />
# touch /etc/pm/power.d/intel-audio-powersave<br />
<br />
===S/PDIF Output Does Not Work===<br />
<br />
If the optical/coaxial digital output of your motherboard/sound card is not working or stopped working, and have already enabled and unmuted it in alsamixer, try running<br />
# iecset audio on<br />
<br />
as root.<br />
<br />
You can also put this command in {{Ic|rc.local}} as it sometimes it may stop working after a reboot.<br />
<br />
===HDMI Output Does Not Work===<br />
If the HDMI output of your motherboard/sound card is not working or stopped working, and have already enabled and unmuted it in alsamixer, try the following.<br />
<br />
Query for Playback Devices:<br />
<br />
$ aplay -l<br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: NVidia [HDA NVidia], device 0: ALC1200 Analog [ALC1200 Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 1: ALC1200 Digital [ALC1200 Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 3: NVIDIA HDMI [NVIDIA HDMI]<br />
Subdevices: 0/1<br />
Subdevice #0: subdevice #0<br />
<br />
Now that we have the info for the HDMI device, try a test, In the example below, 0 is the card number and 3 is the device number.<br />
<br />
$ aplay -D plughw:0,3 /usr/share/sounds/alsa/Front_Center.wav<br />
<br />
If aplay does not output any errors, but still no sound is heared, "reboot" the receiver, monitor or tv set. Since the HDMI interface executes a handshake on connection, it might have noticed before that there was no audio stream embedded, and disabled audio decoding. In particular, if you are using a standalone window manager (don’t know about Gnome or KDE), you may need to have some sound playing ''while'' plugging in the HDMI cable.<br />
<br />
{{Note|If you are using an ATI card and linux 3.0, check your kernel/grub settings, you most likely need to use the ''''radeon.audio'''<nowiki>=</nowiki>'''1'''' boot parameter, or alternatively set up the option in your modprobe configuration files. It's important to note that before trying, is to check the [http://www.x.org/wiki/RadeonFeature#fndef-72849d6d6eb3927d486f50ece74a739042965a6c-25 Radeon Feature Matrix] to see if it's actually supported. At the moment this is written (march 29th, 2013), some chipsets of the [http://www.x.org/wiki/RadeonFeature#Decoder_ring_for_engineering_vs_marketing_names S. Islands] (namely HD7750 through HD7970) doesn't (still) support HDMI Audio.}}<br />
<br />
If the test is successful, create or edit your ~/.asoundrc file to set HDMI as the default audio device.<br />
<br />
{{hc|~/.asoundrc|<br />
pcm.!default {<br />
type hw<br />
card 0<br />
device 3<br />
}<br />
}}<br />
<br />
Or you above config does not work try:<br />
{{hc|~/.asoundrc|<br />
defaults.pcm.card 0<br />
defaults.pcm.device 3<br />
defaults.ctl.card 0<br />
}}<br />
<br />
===HDMI Multi-channel PCM output does not work (Intel) ===<br />
As of Linux 3.1 multi-channel PCM output through HDMI with a Intel card (Intel Eaglelake, IbexPeak/Ironlake,SandyBridge/CougarPoint and IvyBridge/PantherPoint) is not yet supported. Support for it has been recently added and expected to be available in Linux 3.2. To make it work in Linux 3.1 you need to apply the following patches:<br />
<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=76adaa34db407f174dd06370cb60f6029c33b465 drm: support routines for HDMI/DP ELD]<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=e0dac65ed45e72fe34cc7ccc76de0ba220bd38bb drm/i915: pass ELD to HDMI/DP audio driver]<br />
<br />
===HP TX2500===<br />
Add these 2 lines into {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-cmipci mpu_port=0x330 fm_port=0x388<br />
options snd-hda-intel index=0 model=toshiba position_fix=1<br />
<br />
options snd-hda-intel model=hp (works for tx2000cto)<br />
<br />
===Skipping Sound When Playing MP3===<br />
If you have sound skipping when playing MP3 files and you have more then 2 speakers attached to your computer (i.e. > 2 speaker system), run alsamixer and disable the channels for the speakers that you '''DO NOT''' have (i.e. do not enable the sound for the center speaker if you do not have a center speaker.<br />
<br />
===Using a USB Headset and External USB Sound Cards===<br />
If you are using a USB headset with ALSA you can try using {{AUR|asoundconf}} (currently only available from the [[AUR]]) to set the headset as the primary sound output. Before running make sure you have usb audio module enabled ({{ic|modprobe snd-usb-audio}}).<br />
<br />
# asoundconf is-active<br />
# asoundconf list<br />
# asoundconf set-default-card <chosen soundcard><br />
<br />
==== Crackling sound with USB devices ====<br />
If you experience crackling sound on USB devices, you can try tuning the snd-usb-audio for minimal latency.<br />
<br />
Add this to your {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
options snd-usb-audio nrpacks=1<br />
<br />
source: http://alsa.opensrc.org/Usb-audio#Tuning_USB_devices_for_minimal_latencies<br />
<br />
==== Hot-plugging a USB Sound Card ====<br />
In order to automatically make a USB Sound Card the primary output device, when the card is plugged in, you can use the following udev rules (e.g. add the following two lines to {{ic|/etc/udev/rules.d/00-local.rules}} and reboot).<br />
<br />
{{bc|1=KERNEL=="pcmC[D0-9cp]*", ACTION=="add", PROGRAM="/bin/sh -c 'K=%k; K=$${K#pcmC}; K=$${K%%D*}; echo defaults.ctl.card $$K > /etc/asound.conf; echo defaults.pcm.card $$K >>/etc/asound.conf'"<br />
KERNEL=="pcmC[D0-9cp]*", ACTION=="remove", PROGRAM="/bin/sh -c 'echo defaults.ctl.card 0 > /etc/asound.conf; echo defaults.pcm.card 0 >>/etc/asound.conf'"}}<br />
<br />
===Error 'Unknown hardware' Appears After a Kernel Update===<br />
The following messages may be displayed during the start-up ALSA after the kernel update:<br />
Unknown hardware "foo" "bar" ...<br />
Hardware is initialized using a guess method<br />
/usr/sbin/alsactl: set_control:nnnn:failed to obtain info for control #mm (No such file or directory)<br />
<br />
or<br />
Found hardware: "HDA-Intel" "VIA VT1705" "HDA:11064397,18490397,00100000" "0x1849" "0x0397"<br />
Hardware is initialized using a generic method<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #1 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #2 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #25 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #26 (No such file or directory)<br />
<br />
Simply store ALSA mixer settings again (as root):<br />
# alsactl -f /var/lib/alsa/asound.state store<br />
<br />
It may be necessary configure ALSA again with alsamixer<br />
<br />
===HDA Analyzer===<br />
If the mappings to your audio pins(plugs) do not correspond but ALSA works fine, you could try HDA Analyzer -- a pyGTK2 GUI for HD-audio control can be found [http://www.alsa-project.org/main/index.php/HDA_Analyzer at the ALSA wiki].<br />
Try tweaking the Widget Control section of the PIN nodes, to make microphones IN and headphone jacks OUT. Referring to the Config Defaults heading is a good idea.<br />
<br />
NOTE: the script is done by such way that it is incompatible with python3 (which is now shipped with ArchLinux) but tries to use it.<br />
The workaround is: open "run.py", find all occurences of "python" (2 occurences - one on the first line, and the second on the last line) and replace them all by "python2".<br />
<br />
NOTE2: the script requires root acces, but running it via su/sudo is bogus. Run it via {{ic|kdesu}} or {{ic|gksu}}.<br />
<br />
===ALSA with SDL===<br />
If you get no sound via SDL and ALSA cannot be chosen from the application. Try setting the environmental variable SDL_AUDIODRIVER to alsa.<br />
# export SDL_AUDIODRIVER=alsa<br />
<br />
===Low Sound Workaround===<br />
<br />
If you are facing low sound even after maxing out your speakers/headphones, you can give the softvol plugin a try. Add the following to {{ic|/etc/asound.conf}}.<br />
{{bc|<nowiki><br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
<br />
pcm.softvol {<br />
type softvol<br />
slave {<br />
pcm "dmix"<br />
}<br />
control {<br />
name "Pre-Amp"<br />
card 0<br />
}<br />
min_dB -5.0<br />
max_dB 20.0<br />
resolution 6<br />
}<br />
</nowiki>}}<br />
{{note| You will probably have to restart the computer, as restarting the alsa daemon did not load the new configuration for me. Also, if the configuration does not work even after restarting, try changing {{ic|plug}} with {{ic|hw}} in the above configuration.}}<br />
<br />
After the changes are loaded successfully, you will see a {{ic|Pre-Amp}} section in alsamixer. You can adjust the levels there.<br />
{{note|Setting a high value for {{ic|Pre-Amp}} can cause sound distortion, so adjust it according to the level that suits you.}}<br />
<br />
===Popping sound after resuming from suspension===<br />
You might hear a popping sound after resuming the computer from suspension. This can be fixed by editing {{ic|/etc/pm/sleep.d/90alsa}} and removing the line that says {{ic|aplay -d 1 /dev/zero}}<br />
<br />
==配置文件范例==<br />
参见:[[Advanced Linux Sound Architecture/Example Configurations]]。<br />
<br />
==相关阅读==<br />
* [http://www.mjmwired.net/kernel/Documentation/sound/alsa/ALSA-Configuration.txt ALSA 模块高级配置]<br />
* [http://alsa.opensrc.org/Main_Page 非官方的 ALSA Wiki]<br />
* [https://bbs.archlinux.org/viewtopic.php?id=36815 如何从 svn 获取驱动代码并编译]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Advanced_Linux_Sound_Architecture_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=256310
Advanced Linux Sound Architecture (简体中文)
2013-05-07T12:39:31Z
<p>Cuihao: /* 设置文件范例 */</p>
<hr />
<div>[[Category:简体中文]]<br />
[[Category:Sound (简体中文)]]<br />
[[Category:Audio/Video (简体中文)]]<br />
[[cs:Advanced Linux Sound Architecture]]<br />
[[de:Alsa]]<br />
[[es:Advanced Linux Sound Architecture]]<br />
[[fa:ALSA]]<br />
[[fr:Alsa]]<br />
[[he:Advanced Linux Sound Architecture]]<br />
[[it:Advanced Linux Sound Architecture]]<br />
[[ja:Advanced Linux Sound Architecture]]<br />
[[nl:Advanced Linux Sound Architecture]]<br />
[[pt:Advanced Linux Sound Architecture]]<br />
[[ru:Advanced Linux Sound Architecture]]<br />
[[sk:Advanced Linux Sound Architecture]]<br />
[[th:Advanced Linux Sound Architecture]]<br />
[[tr:Gelişmiş_ses_mimarisi_(ALSA)]]<br />
[[en:Advanced Linux Sound Architecture]]<br />
<br />
{{Article summary start}}<br />
{{Article summary text|ALSA 的使用、配置及疑难解答。}}<br />
{{Article summary heading|相关页面}}<br />
{{Article summary wiki|Sound system (简体中文)}}<br />
{{Article summary wiki|Disable PC Speaker Beep (简体中文)}}<br />
{{Article summary end}}<br />
{{TranslationStatus (简体中文)|Advanced_Linux_Sound_Architecture|2013-04-25|254584}}<br />
<br />
[[Wikipedia:zh:ALSA|高级 Linux 声音体系]](Advanced Linux Sound Architecture,'''ALSA''')是Linux中提供声音设备驱动的内核组件,用来代替原来的开放声音系统(Open Sound System,OSSv3)。除了声音设备驱动,'''ALSA'''还包含一个用户空间的函数库,以方便开发者通过高级API使用驱动功能,而不必直接与内核驱动交互。<br />
<br />
{{注意|关于另一种声音体系,请阅读[[Open Sound System (简体中文)|开放声音系统(OSS)]]的页面。}}<br />
<br />
==安装==<br />
Arch 默认的内核已经通过一套模块提供了 ALSA,不必特别安装。<br />
<br />
[[udev (简体中文)|udev]]会在系统启动时自动检测硬件,并加载相应的声音设备驱动模块。这时,你的声卡已经可以工作了,只是所有声道默认都被设置成静音了。<br />
<br />
在本地登录(通过虚拟终端或登录管理器)的用户,都有权限播放音频并调整音量。要让远程登录的用户拥有这些权限,必须把用户[[Users and Groups (简体中文)#用户组管理|加入]] {{ic|audio}} 用户组。该组的成员可以直接访问声音设备,会导致某些程序独占音频输出(破坏软件混音),还可能影响用户快速切换和拖机(multiseat)。因此,除非真的有某些[https://wiki.ubuntu.com/Audio/TheAudioGroup 特殊需求],'''不建议'''把用户加入 {{ic|audio}} 用户组。<br />
<br />
===用户空间工具===<br />
从 [[Official Repositories (简体中文)|官方仓库]]:<br />
* [[pacman (简体中文)|安装]] {{Pkg|alsa-utils}} 软件包,其中的 {{ic|alsamixer}} 工具允许用户在控制台或终端中配置声音设备。<br />
* 安装 {{Pkg|alsa-oss}} 软件包,如果想让 OSS 应用程序与dmix(软件混音)协同工作。 <br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。}}<br />
*安装 {{Pkg|alsa-plugins}} 软件包,如果需要 [[#高质量重采样|高质量重采样]]、[[#Upmixing/Downmixing|软件模拟环绕立体声]]和其他高级特性。<br />
<br />
==解除各声道的静音==<br />
目前版本的 ALSA 安装后,所有声道'''默认是静音的''',必须手动解除。<br />
<br />
使用 {{ic|alsamixer}} 的 ncurses 界面,配置十分简单:<br />
<br />
$ alsamixer<br />
<br />
此外,还可以在命令行下使用 {{ic|amixer}}:<br />
<br />
$ amixer sset Master unmute<br />
<br />
在 {{ic|alsamixer}} 中,下方标有 {{ic|MM}} 的声道是静音的,而标有 {{ic|00}} 的通道已经启用。<br />
<br />
使用左右方向键,选中 {{ic|Master}} 和 {{ic|PCM}} 声道。按下 {{keypress|m}} 键解除静音。使用上方向键增加音量,直到增益值为{{ic|0}}。该值显示在左上方 {{ic|Item:}} 字段后。过高的增益值会导致声音失真。<br />
<br />
要想得到完整的 5.1 或 7.1 环绕立体声,还得解除 Front、Surround、Center、LFE (subwoofer) 和 Side 这些声道的静音(上述名称是 Intel HD Audio 声卡使用的声道名,可能因设备不同而有所差异)。注意,仅有这些设置,系统不会自动将立体声源(多数音乐)提升(upmix)成环绕立体声。如果需要这些功能,请阅读[[#Upmixing/Downmixing]]。<br />
<br />
要启用麦克风,切换至 Capture 选项卡,按下 {{keypress|F4}},按下 {{keypress|空格}} 启用其中一个声道即可。<br />
<br />
按下 {{Keypress|Esc}} 键退出 alsamixer。<br />
<br />
{{注意|<br />
* 有一些声卡,需要关闭数字输出或将其调成静音,才能输出模拟音频信号。对于 Soundblaster Audigy LS 声卡,需要把 IEC958 通道设成静音。 <br />
* 有些机器(如 Thinkpad T61),有一个 Speaker 通道,必须按照上述方法打开调整。<br />
* 有些机器(如 Dell E6400),可能还需要把 {{ic|Front}} 和 {{ic|Headphone}} 声道打开调整。}}<br />
<br />
接下来,测试声卡是否工作:<br />
<br />
$ speaker-test -c 2<br />
<br />
根据扬声器的实际情况,调整 -c 后的数值。对于 7.1 声道,这个数字是 8:<br />
<br />
$ speaker-test -c 8<br />
<br />
如果没有正常工作,请继续阅读 [[#配置]] 以及 [[#疑难解答]] 部分。<br />
<br />
{{pkg|alsa-utils}} 软件包提供了两个配置 alsa 的服务:{{ic|alsa-restore.service}} 和 {{ic|alsa-store.service}}。它们分别在开机和关机时自动运行。<br />
<br />
==配置==<br />
=== VirtualBox 虚拟机无声音 ===<br />
<br />
如果使用 VirtualBox 时碰上了麻烦,请尝试以下命令:<br />
<br />
{{hc|$ alsactl init|2=<nowiki><br />
<br />
Found hardware: "ICH" "SigmaTel STAC9700,83,84" "AC97a:83847600" "0x8086" "0x0000"<br />
Hardware is initialized using a generic method<br />
</nowiki><br />
}}<br />
<br />
如果还是不行,请检查所使用的音频软件是否激活了 ALSA 输出。<br />
<br />
===设置默认声卡===<br />
<br />
如果发现开机时声卡次序会发生变化,可以在通过 {{ic|/etc/modprobe.d}} 中的 {{ic|.conf}} 文件(比如 {{ic|/etc/modprobe.d/alsa-base.conf}})手动设置次序。<br />
比如,要让 mia 声卡成为 #0、Intel HDA 声卡成为 #1:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<br />
options snd slots=snd_mia,snd_hda_intel<br />
options snd_mia index=0<br />
options snd_hda_intel index=1<br />
}}<br />
<br />
使用 {{ic|<nowiki>lsmod | grep snd</nowiki>}} 获取设备列表。<br />
<br />
snd_mia 和 snd_hda_intel 分别是两个声卡的内核驱动模块。该配置仅适用于同时装有 mia 声卡和使用 snd_hda_intel 驱动的声卡(通常是板载声卡)的情况。<br />
<br />
如果使用 -2 的 index 值,ALSA 就不会将对应的设备作为主声卡使用。Linux Mint 和 Ubuntu 等发行版使用了以下配置,避免 USB 声卡和其他“非主流”设备变成 index 为 0 的主声卡:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<br />
options bt87x index=-2<br />
options cx88_alsa index=-2<br />
options saa7134-alsa index=-2<br />
options snd-atiixp-modem index=-2<br />
options snd-intel8x0m index=-2<br />
options snd-via82xx-modem index=-2<br />
options snd-usb-audio index=-2<br />
options snd-usb-caiaq index=-2<br />
options snd-usb-ua101 index=-2<br />
options snd-usb-us122l index=-2<br />
options snd-usb-usx2y index=-2<br />
# Keep snd-pcsp from being loaded as first soundcard<br />
options snd-pcsp index=-2<br />
# Keep snd-usb-audio from beeing loaded as first soundcard<br />
options snd-usb-audio index=-2<br />
}}<br />
<br />
以上配置会在系统重启后生效。<br />
<br />
{{Out of date|据英文 Wiki 用户反应,{{ic|/etc/asound.conf}} 这个配置文件默认是不存在的,下列操作可能有误。}}<br />
<br />
首先,运行 {{ic|aplay -l}},获取声卡的声卡ID和设备ID:<br />
<br />
{{hc|$ aplay -l|2=<nowiki><br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: Intel [HDA Intel], device 0: CONEXANT Analog [CONEXANT Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: Intel [HDA Intel], device 1: Conexant Digital [Conexant Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 1: JamLab [JamLab], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 2: Audio [Altec Lansing XT1 - USB Audio], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
</nowiki>}}<br />
<br />
拿输出中的最后一项来说,这个声卡的声卡ID为2,设备ID为0。若要把这块声卡作为默认声卡,可以把下列配置添加到系统级别的 {{ic|/etc/asound.conf}} 或用户级别的 {{ic|~/.asoundrc}} 文件。如果文件不存在,可以手动创建。其中的各个ID,请根据实际情况调整:<br />
{{bc|<br />
defaults.pcm.card 2<br />
defaults.pcm.device 0<br />
defaults.ctl.card 2<br />
}}<br />
“pcm”选项决定用来播放音频的设备,而“ctl”选项决定那个声卡能够由控制工具(如 alsamixer)使用。<br />
<br />
上述配置在重启音频程序(如 mplayer)后立即生效。<br />
<br />
===确认所有声音模块都已经加载===<br />
<br />
一般 udev 都会自动识别出声卡。使用以下命令确认:<br />
<br />
{{hc|<nowiki>$ lsmod | grep '^snd' | column -t</nowiki>|<nowiki><br />
snd_hda_codec_hdmi 22378 4<br />
snd_hda_codec_realtek 294191 1<br />
snd_hda_intel 21738 1<br />
snd_hda_codec 73739 3 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel<br />
snd_hwdep 6134 1 snd_hda_codec<br />
snd_pcm 71032 3 snd_hda_codec_hdmi,snd_hda_intel,snd_hda_codec<br />
snd_timer 18992 1 snd_pcm<br />
snd 55132 9 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel,snd_hda_codec,snd_hwdep,snd_pcm,snd_timer<br />
snd_page_alloc 7017 2 snd_hda_intel,snd_pcm<br />
</nowiki>}}<br />
<br />
如果你的输出和上面类似,那就说明声卡已经被正确识别。<br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。如有需要,请[[Kernel_modules_(简体中文)#装入|手动加载]]。}}<br />
<br />
还可以检查一下 {{ic|/dev/snd/}} 目录,看看是否有这些设备文件:<br />
<br />
{{hc|$ ls -l /dev/snd|2=<nowiki><br />
total 0<br />
crw-rw---- 1 root audio 116, 0 Apr 8 14:17 controlC0<br />
crw-rw---- 1 root audio 116, 32 Apr 8 14:17 controlC1<br />
crw-rw---- 1 root audio 116, 24 Apr 8 14:17 pcmC0D0c<br />
crw-rw---- 1 root audio 116, 16 Apr 8 14:17 pcmC0D0p<br />
crw-rw---- 1 root audio 116, 25 Apr 8 14:17 pcmC0D1c<br />
crw-rw---- 1 root audio 116, 56 Apr 8 14:17 pcmC1D0c<br />
crw-rw---- 1 root audio 116, 48 Apr 8 14:17 pcmC1D0p<br />
crw-rw---- 1 root audio 116, 1 Apr 8 14:17 seq<br />
crw-rw---- 1 root audio 116, 33 Apr 8 14:17 timer<br />
</nowiki>}}<br />
<br />
{{注意|在 IRC 或论坛寻求这方面帮助时,别忘了贴出上面几个命令的输出。}}<br />
<br />
如果你的输出跟上面类似,或至少有 '''controlC0''' 和 '''pcmC0D0p''',那么声卡就已经正常加载了。<br />
<br />
如果出现问题,声卡模块没有正确加载,那么请尝试手动加载模块:<br />
<br />
* 确定声卡对应的驱动模块:[http://www.alsa-project.org/main/index.php/Matrix:Main ALSA Soundcard Matrix]。这些模块都会有一个”snd-“前缀(例如:{{ic|snd-via82xx}})。<br />
* [[Kernel_modules_(简体中文)#装入|加载模块]]。<br />
* 检查 {{ic|/dev/snd}} 目录中的设备文件(参见上文);或者,检查 {{ic|alsamixer}} 或 {{ic|amixer}} 的输出是否正确。<br />
* 修改配置,使 {{ic|snd-<模块名>}} 和 {{ic|snd-pcm-oss}} 模块[[Kernel_modules_(简体中文)#装入|开机自动加载]]。<br />
<br />
===启用 SPDIF 输出===<br />
<br />
(由 Gentoo 论坛的 gralves 提供)<br />
* 在 GNOME 音量控制的选项(Options)标签中,把 IEC958 改成 PCM。该选项可以在首选项中启用。<br />
* 不使用 GNOME 音量控制工具的话:<br />
** 编辑 {{ic|/var/lib/alsa/asound.state}}。该文件是 alsasound 保存混音器设置的地方。<br />
** 找到这一行:“IEC958 Playback Switch”。该行上下应该有一行内容为 {{ic|value:false}},将其改为 {{ic|value:true}}。<br />
** 再找到这一行:”IEC958 Playback AC97-SPSA“。把对应的值改为 0。<br />
** 重启 ALSA。<br />
<br />
{{Out of date|rc.local 在 systemd 中已经弃用,以下方法可能不再适用。}}<br />
<br />
还有另外一个开机自动启用 SPDIF 输出的方法(在 SoundBlaster Audigy 声卡上测试过):<br />
<br />
* 在 {{ic|/etc/rc.local}} 中添加如下内容:<br />
# Use COAX-digital output<br />
amixer set 'IEC958 Optical' 100 unmute<br />
amixer set 'Audigy Analog/Digital Output Jack' on<br />
<br />
使用以下命令,获取你的声卡的数字输出的名称:<br />
$ amixer scontrols<br />
<br />
===系统级均衡器===<br />
====使用 AlsaEqual(包含界面)====<br />
从 [[AUR (简体中文)|AUR]] 安装 {{AUR|alsaequal}}。<br />
<br />
{{注意|如果使用的是64位系统,而又安装了32位的 Flash 插件,这里的设置会导致 Flash 无声。如果发生了这种情况,请禁用 alsaequal 或编译32位版本的 alsaequal。}}<br />
<br />
安装后,把下列内容添加到 ALSA 配置文件({{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}}):<br />
<br />
ctl.equal {<br />
type equal;<br />
}<br />
<br />
pcm.plugequal {<br />
type equal;<br />
# Modify the line below if you do not<br />
#slave.pcm "plughw:0,0";<br />
#by default we want to play from more sources at time:<br />
slave.pcm "plug:dmix";<br />
}<br />
#pcm.equal {<br />
# If you do not want the equalizer to be your<br />
# default soundcard comment the following<br />
# line and uncomment the above line. (You can<br />
# choose it as the output device by addressing<br />
# it with specific apps,eg mpg123 -a equal 06.Back_In_Black.mp3)<br />
pcm.!default {<br />
type plug;<br />
slave.pcm plugequal;<br />
}<br />
<br />
设置好后,切换均衡器:<br />
$ alsamixer -D equal<br />
<br />
需要注意,每个用户的 alsaequal 配置文件都是独立的,位于 {{ic|~/.alsaequal.bin}}。因此,如果想在 [[mpd]] 之类的以独立用户身份运行的程序中使用 AlsaEqual,就得像下面这样:<br />
# su mpd -c 'alsamixer -D equal'<br />
或者,在其他用户的主目录下创建符号链接,指向你的 {{ic|.alsaequal.bin}}……<br />
<br />
=====管理 AlsaEqual 配置=====<br />
从 [http://xyne.archlinux.ca/repos/ Xyne 的软件仓库] 或 [https://aur.archlinux.org/packages.php?ID=62420 AUR] 安装 [http://xyne.archlinux.ca/projects/alsaequal-mgr/ alsaequal-mgr]。<br />
<br />
同上,配置均衡器: <br />
$alsamixer -D equal<br />
<br />
如果对效果感到满意,将其命名(如“foo”)并保存该配置:<br />
$alsaequal-mgr save foo<br />
<br />
重新加载“foo”中的配置:<br />
$alsaequal-mgr load foo<br />
<br />
这样,就可以为游戏、电影、不同音乐流派、网络电话等等创建不同的均衡器配置,以便随时切换。<br />
<br />
参见[http://xyne.archlinux.ca/projects/alsaequal-mgr/ 项目主页]和程序的帮助信息,了解更多设置。<br />
<br />
====使用 mbeq====<br />
{{注意|该软件用到了一个 ladspa 插件,可能导致播放音频时 CPU 占用率偏高。In addition, this was made with stereophonic sound (e.g. headphones) in mind.}}<br />
<br />
安装 {{Pkg|alsa-plugins}}、{{Pkg|ladspa}} 和 {{Pkg|swh-plugins}} 软件包。<br />
<br />
* 在 {{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}} 中添加如下内容:<br />
<br />
{{hc|/etc/asound.conf|2=<nowiki>pcm.eq {<br />
type ladspa<br />
<br />
# The output from the EQ can either go direct to a hardware device<br />
# (if you have a hardware mixer, e.g. SBLive/Audigy) or it can go<br />
# to the software mixer shown here.<br />
#slave.pcm "plughw:0,0"<br />
slave.pcm "plug:dmix"<br />
<br />
# Sometimes you may need to specify the path to the plugins,<br />
# especially if you have just installed them. Once you have logged<br />
# out/restarted this should not be necessary, but if you get errors<br />
# about being unable to find plugins, try uncommenting this.<br />
#path "/usr/lib/ladspa"<br />
<br />
plugins [<br />
{<br />
label mbeq<br />
id 1197<br />
input {<br />
#this setting is here by example, edit to your own taste<br />
#bands: 50hz, 100hz, 156hz, 220hz, 311hz, 440hz, 622hz, 880hz, 1250hz, 1750hz, 25000hz,<br />
#50000hz, 10000hz, 20000hz<br />
controls [ -5 -5 -5 -5 -5 -10 -20 -15 -10 -10 -10 -10 -10 -3 -2 ]<br />
}<br />
}<br />
]<br />
}<br />
<br />
# Redirect the default device to go via the EQ - you may want to do<br />
# this last, once you are sure everything is working. Otherwise all<br />
# your audio programs will break/crash if something has gone wrong.<br />
<br />
pcm.!default {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
<br />
# Redirect the OSS emulation through the EQ too (when programs are running through "aoss")<br />
<br />
pcm.dsp0 {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
</nowiki>}}<br />
<br />
* 完成。若有问题,欢迎前往论坛咨询。<br />
<br />
==高质量重采样==<br />
启用软件混音时,ALSA 会强制把所有音频重采样到相同的频率(如果系统支持,默认是 48000)。dmix 的重采样算法很糟糕,会导致显著的质量损失。<br />
<br />
安装 {{pkg|alsa-plugins}} 和 {{pkg|libsamplerate}}。<br />
<br />
将默认的采样率转换器更改为 libsamplerate:<br />
<br />
{{hc|/etc/asound.conf|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
或:<br />
<br />
{{hc|~/.asoundrc|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
'''samplerate_best''' 输出的声音质量最佳,但需要耗费大量CPU资源进行实时重采样。还有其他的算法可供选择(如 '''samplerate''' 等),但比起默认的算法,这些算法的改进并不明显。<br />
<br />
{{警告|某些系统中,启用 samplerate_best 可能导致 Flash 播放器无声音。}}<br />
<br />
==上混和下混==<br />
{{注意|由于未找到 upmix 和 downmix 的常用翻译,译者暂时采用字面翻译(上混、下混)。所谓“上混”,即将声道较少的立体声(如常见的双声道立体声)信号,通过软件或硬件处理,模拟为声道较多的立体声(如5.1环绕立体声)信号。反之即为“下混”。}}<br />
<br />
===上混(upmixing)===<br />
播放双声道立体声音源(如音乐)时,利用上混,即可充分利用 5.1 或 7.1 环绕立体声的所有声道。以前,上混很麻烦,还经常出错。但如今就方便多了,只需要安装一些插件即可。这些插件由 {{Pkg|alsa-plugins}} 软件包提供。<br />
<br />
安装后,在配置文件({{ic|/etc/asound.conf}} 或 {{ic|~/.asoundrc}})中添加如下内容:<br />
pcm.upmix71 {<br />
type upmix<br />
slave.pcm "surround71"<br />
delay 15<br />
channels 8<br />
}<br />
<br />
该范例适用于7.1声道上混。类比该范例,同样能设置5.1或4.0声道上混。<br />
<br />
该设置添加了一个新的用于上混的 pcm。如果希望所有音频源都通过该 pcm 输出,在上述设置后添加如下配置即可:<br />
<br />
pcm.!default "plug:upmix71"<br />
<br />
插件默认配置就允许多个音频源同时通过它输出,一般无需额外配置。如果不行,就得向下面这样为上混配置 dmixer:<br />
pcm.dmix6 {<br />
type asym<br />
playback.pcm {<br />
type dmix<br />
ipc_key 567829<br />
slave {<br />
pcm "hw:0,0"<br />
channels 6<br />
}<br />
}<br />
}<br />
<br />
注意要用 dmix6 而非 surround71。<br />
<br />
如果遇到声音卡顿或混乱,可能需要增加 buffer_size(比如增加到 32768),或使用[[#高质量重采样|高质量重采样]]。<br />
<br />
===下混(downmixing)===<br />
有时也会用到下混,比如在只支持双声道立体声的电脑上观看5.1声道的电影时。这一功能由 ALSA 插件 vdownmix 实现(同样在 {{Pkg|alsa-plugins}} 软件包里)。<br />
<br />
在配置文件中添加如下内容:<br />
<br />
pcm.!surround51 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
pcm.!surround40 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
<br />
==混音==<br />
===软件混音(dmix)===<br />
<br />
{{注意|在 ALSA 1.0.9rc2 及以上版本中,如果声卡不支持硬件混音,模拟输出将默认使用 dmix 混音,无需额外设置。}}<br />
<br />
如果软件混音未正常启用,那么可以手动创建 {{ic|~/.asoundrc}},加入以下内容:<br />
<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
<br />
这样就启用了软件混音,多个程序能够同时使用声卡。<br />
<br />
至于 S/PDIF 之类的数字音频输出,目前的 ALSA 还不会默认启用 dmix。因此,必须像上面这样手动启用。<br />
<br />
常见问题的解答,参见[[#疑难解答]]部分。<br />
<br />
===硬件混音===<br />
====支持====<br />
如果声卡支持,一般无需任何配置即可启用硬件混音。绝大多数板载声卡都不支持硬件混音,必须得使用软件混音(见上文)。很多独立声卡都支持硬件混音,Linux 下支持较好的有:<br />
<br />
* Creative SoundBlaster Live! (5.1 型号)<br />
* Creative SoundBlaster Audigy (部分型号)<br />
* Creative SoundBlaster Audidy 2 (ZS 系列型号)<br />
* Creative SoundBlaster Audigy 4 (Pro 系列型号)<br />
<br />
{{注意|上述声卡的低端版本(Audigy SE、Audigy 2 NX、SoundBlaster Live! 24bit 以及 SoundBlaster Live! 7.1)使用了不同的芯片,'''不支持'''硬件混音。}}<br />
<br />
{{注意|板载 VIA8237 芯片支持4路硬件混音,但在某些主板上只能使用3路(第4路无声),甚至根本无法使用。即使能正常工作,质量也比不上其他混音方法。}}<br />
<br />
====问题修复====<br />
如果你的声卡是 Intel Corporation 82801I (ICH9 Family) HD Audio Controller (rev 02),且使用了64位系统,需要执行以下命令才能在 Enemy Territory 中听到声音:<br />
echo "et.x86 0 0 direct" > /proc/asound/card0/pcm0p/oss<br />
echo "et.x86 0 0 disable" > /proc/asound/card0/pcm0c/oss<br />
<br />
==疑难解答==<br />
===使用CPU频率动态调整时,音频发生跳跃===<br />
使用 {{ic|ondemand}} 或 {{ic|conservative}} 频率策略、启用CPU频率动态调整时,某些声卡和某些 ALSA 驱动会出现声音跳跃的情况。目前,唯一的解决方案是使用固定的频率,如 {{ic|performance}} 性能策略。<br />
<br />
详情参见 [[CPU Frequency Scaling (简体中文)]]。<br />
<br />
===同时只能有一个用户播放音频===<br />
有时会出现一个用户独占软件混音器的情况。多数情况下这没什么影响,但在使用 [[mpd]] 这样以独立用户身份运行的程序时,就会出现问题。当 mpd 运行时,用户自己就无法播放其他音频了。一种解决方法是,以正在使用的用户身份运行 mpd。另一种方法是,在 ALSA 配置文件的 {{ic|pcm.dmixer}} 部分添加 {{ic|ipc_key_add_uid 0}} 禁止独占。以下是 {{ic|asound.conf}} 示例的片段,省略的部分无需修改:<br />
<br />
...<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
slave {<br />
...<br />
<br />
===无法同时播放多个音频===<br />
如果安装了 [[PulseAudio (简体中文)|PulseAudio]](可能作为 [[GNOME (简体中文)|GNOME]] 的依赖被安装),可能出现无法同步播放的问题,因为 PulseAudio 的默认配置是“独占”声卡。ALSA 的用户可能对自己的配置已经很满意、不希望使用 [[PulseAudio (简体中文)|PulseAudio]]。解决方案是编辑 {{ic|/etc/asound.conf}} 并注释掉以下内容:<br />
# Use PulseAudio by default<br />
#pcm.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
# hint {<br />
# show on<br />
# description "Default ALSA Output (currently PulseAudio Sound Server)"<br />
# }<br />
#}<br />
<br />
可能还需要注释掉下面的内容:<br />
#ctl.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
#}<br />
<br />
比起完全卸载 [[PulseAudio (简体中文)|PulseAudio]],这大概是个更简单的方法。<br />
<br />
以下是 {{ic|/etc/asound.conf}} 配置文件的一个示例:<br />
{{bc|<nowiki>pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
}<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}</nowiki><br />
}}<br />
<br />
{{注意|本 {{ic|/etc/asound.conf}} 本来是配合 [[MPD]] 全局配置使用的。参见[[#同时只能有一个用户播放音频|该节]]关于多用户的内容。}}<br />
<br />
===有时候开机无声===<br />
如果会随机地出现开机无声的问题,原因可能是多声卡的顺序发生了变化。如果是这种情况,请参照[[#设置默认声卡]]进行设置。<br />
<br />
对于 mpd 用户,上述配置无效,请参考[http://mpd.wikia.com/wiki/Configuration#ALSA_MPD_software_volume_control 该文]设置。<br />
<br />
===特定程序的问题===<br />
对于不使用系统声音配置的程序,如 XMMS 和 Mplayer,必须特别设置以应用 ALSA 配置。<br />
<br />
对于 mplayer,将以下内容添加到 {{ic|~/.mplayer/config}} (或系统全局配置文件 {{ic|/etc/mplayer/mplayer.conf}}):<br />
ao=alsa<br />
<br />
对于 XMMS 和 Beep Media Player,打开选项窗口,要把声音驱动设置为 ALSA 而不是 OSS。<br />
<br />
XMMS 的设置方法:<br />
* 启动 XMMS<br />
** 选项 -> 首选项<br />
** 选择 Alsa 输出插件<br />
<br />
对于未提供 ALSA 输出的程序,可以使用 alsa-oss 软件包中的 aoss 工具启动。使用方法是:在要运行的程序的命令行前加上 {{ic|aoss}}。例如:<br />
aoss realplay<br />
<br />
pcm.!default{ ... } 貌似已经不管用了。但下面的设置有效:<br />
pcm.default pcm.dmixer<br />
<br />
===特定型号的配置===<br />
ALSA 会通过 BIOS 检测[http://www.kernel.org/doc/Documentation/sound/alsa/HD-Audio-Models.txt 声卡型号],少数情况下也可能无法正确识别。声卡芯片类型可以在 {{ic|alsamixer}} 界面上找到(如 ALC662),而型号则要设置在 {{ic|/etc/modprobe.d/modprobe.conf}} 或 {{ic|/etc/modprobe.d/sound.conf}} 文件中:<br />
<br />
options snd-hda-intel model=<型号><br />
<br />
具体设置因声卡型号而异。多数情况下不需要特地这样做。如果想知道具体声卡型号的设置,请查阅 [http://bugtrack.alsa-project.org/main/index.php/Matrix:Main Alsa Soundcard List],找到你的声卡型号,点击“Details”,然后查看“Setting up modprobe...”小节,将其中的内容插入 {{ic|/etc/modprobe.d/modprobe.conf}} 文件即可。例如,Intel AC97 的配置如下:<br />
<br />
{{bc|# ALSA portion<br />
alias char-major-116 snd<br />
alias snd-card-0 snd-intel8x0<br />
# module options should go here<br />
<br />
# OSS/Free portion<br />
alias char-major-14 soundcore<br />
alias sound-slot-0 snd-card-0<br />
<br />
# card #1<br />
alias sound-service-0-0 snd-mixer-oss<br />
alias sound-service-0-1 snd-seq-oss<br />
alias sound-service-0-3 snd-pcm-oss<br />
alias sound-service-0-8 snd-seq-oss<br />
alias sound-service-0-12 snd-pcm-oss}}<br />
<br />
===与蜂鸣器冲突===<br />
如果你确认所有声道都已解除静音,声卡驱动配置正确,且音量大小也合适,但还是听不到声音,那么请试试将下面的内容添加到 {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
(如果是 {{ic|via82xx}} 驱动)<br />
options snd-<模块名请自行补充> ac97_quirk=0<br />
<br />
(如果是 {{ic|snd_intel8x0}} 驱动)<br />
options snd-NAME-OF-MODULE ac97_quirk=1<br />
<br />
===麦克风输入无声音===<br />
In alsamixer, make sure that all the volume levels are up under recording, and that CAPTURE is toggled active on the microphone (e.g. Mic, Internal Mic) and/or on Capture (in alsamixer, select these items and press space). Try making positive Mic Boost and raising Capture and Digital levels higher; this make make static or distortion, but then you can adjust them back down once you are hearing ''something'' when you record<br />
<br />
As the pulseaudio wrapper is shown as "default" in alsamixer, you may have to press F6 to select your actual soundcard first. You may also need to enable and increase the volume of Line-in in the Playback section.<br />
<br />
To test the microphone, run these commands (see arecord's man page for further information):<br />
arecord -d 5 test-mic.wav<br />
aplay test-mic.wav<br />
<br />
If all fails, you may want to eliminate hardware failure by testing the microphone with a different device.<br />
<br />
For at least some computers, muting a microphone (MM) simply means its input does not go immediately to the speakers. It still receives input.<br />
<br />
Many Dell laptops need "-dmic" to be appended to the model name in {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-hda-intel model=dell-m6-dmic<br />
<br />
Some programs use try to use OSS as the main input software. Add the following lines to {{ic|/etc/modprobe.d/modprobe.conf}} to prevent OSS modules from being loaded:<br />
<br />
{{Note|The OSS modules are no longer autoloaded anyway.}}<br />
<br />
blacklist snd_pcm_oss<br />
blacklist snd_mixer_oss<br />
blacklist snd_seq_oss<br />
<br />
See also:<br />
* http://www.alsa-project.org/main/index.php/SoundcardTesting<br />
* http://alsa.opensrc.org/Record_from_mic<br />
<br />
===设置默认的麦克风/录音设备===<br />
某些程序(Pidgin、Adobe Flash)提供了设置录音设备的选项。如果录音设备不是内置的声卡,而是外接的(如USB摄像头、麦克风),就会有些问题。如果只想更改默认的录音设备、不更改回放设备,需要像下面这样设置 {{ic|~/.asoundrc}}:<br />
<br />
pcm.usb<br />
{<br />
type hw<br />
card U0x46d0x81d<br />
}<br />
<br />
pcm.!default<br />
{<br />
type asym<br />
playback.pcm<br />
{<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
capture.pcm<br />
{<br />
type plug<br />
slave.pcm "usb"<br />
}<br />
}<br />
<br />
把“U0x46d0x81d”替换为你的录音设备的名称。可以通过 {{ic|arecord -L}} 查看所有 ALSA 识别到的录音设备。<br />
<br />
===内置麦克风无效===<br />
首先,请检查 alsamixer,确保所有的录音声道的音量都达到可以录制的程度。如果还是不行,试试修改 /etc/sound.conf 并重载 snd-* 模块。这样就会多出来一个名叫 Capture 的音频控制,可以用它调节内置麦克风的音量。例如,对于 snd-hda-intel 模块,配置如下:<br />
<br />
options snd-hda-intel enable_msi=1<br />
<br />
然后,输入下面的命令重载模块,调整音量并测试。<br />
<br />
{{bc|# rmmod snd-hda-intel && modprobe snd-hda-intel}}<br />
<br />
===Intel 板载声卡无声===<br />
可能由两个相互冲突的驱动模块引起,分别是 {{ic|snd_intel8x0}} 和 {{ic|snd_intel8x0m}}。如果是的话,屏蔽 snd_intel8x0m 即可:<br />
<br />
{{hc|/etc/modprobe.d/modprobe.conf|blacklist snd_intel8x0m}}<br />
<br />
以下方法可能也有效:在 {{ic|alsamixer}} 或 {{ic|amixer}} 把 “External Amplifier” 设置为静音。参见:[http://alsa.opensrc.org/Intel8x0#Dell_Inspiron_8600_.28and_probably_others.29 ALSA wiki 相关条目]。<br />
<br />
也可以试试解除“Mix”的静音。<br />
<br />
===Intel 板载声卡耳机无声===<br />
对于笔记本电脑上的 '''Intel Corporation 82801 I (ICH9 Family) HD Audio Controller''' 声卡,可能需要添加如下内容到 modprobe 配置文件或 sound.conf,以解决耳机无声的问题:<br />
<br />
options snd-hda-intel model=$model<br />
<br />
$model 在下列型号中选择:<br />
<br />
* dell-vostro<br />
* olpc-xo-1_5<br />
* laptop<br />
* dell-m6<br />
* laptop-hpsense<br />
<br />
{{注意|可能需要在配置文件的 "alias" 语句之后添加上述内容。}}<br />
<br />
可以在内核文档中找到所有支持的型号,如[http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.35.y.git;a=blob;f=Documentation/sound/alsa/HD-Audio-Models.txt;h=dc25bb84b83b49665a7ed850e7bf5423d50cd3ba;hb=HEAD 该文]。这是 2.6.35 版本内核的文档,你需要自己查找适用于自己内核版本的文档。<br />
<br />
也可以从[http://www.mjmwired.net/kernel/Documentation/sound/alsa/HD-Audio-Models.txt 这里]获取型号列表。要查看你的声卡芯片名称,输入下列命令(其中的“*”请根据实际情况替换)。注意,有些芯片名称被更改过,因此实际名称与文件中的可能不匹配。<br />
<br />
cat /proc/asound/card*/codec* | grep Codec<br />
<br />
注意,这么做很可能导致所有输入设备(内置和外置麦克风)无法工作,即要么让麦克风正常、要么让耳机正常。如果遇到这一问题,请向 ALSA 开发者报告。<br />
<br />
另外,如果蜂鸣器(pcspkr)出现问题,可以试试使用以下配置修复:<br />
<br />
options snd-hda-intel model=$model enable=1 index=0<br />
<br />
===安装支持 S/PDIF 输出的显卡后无声 ===<br />
检测已加载的相关模块,并记录顺序:<br />
$ cat /proc/asound/modules<br />
0 snd_hda_intel<br />
1 snd_ca0106<br />
<br />
在 {{ic|/etc/modprobe.d/modprobe.conf}} 中禁用不希望使用的显卡的音频解码模块:<br />
# /etc/modprobe.d/modprobe.conf<br />
#<br />
install snd_hda_intel /bin/false<br />
<br />
如果不同设备使用同一模块,那么只能在 BIOS 中禁用其中之一。<br />
<br />
===Poor sound quality or clipping===<br />
If you experience poor sound quality, try setting the PCM volume (in alsamixer) to a level such that gain is 0.<br />
<br />
If snd-usb-audio driver has been loaded, you could try to enable {{ic|softvol}} in '''/etc/asound.conf''' file. Example configuration for the first audio device:<br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.dsnooper {<br />
type dsnoop<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
channels 2<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.softvol {<br />
type softvol<br />
slave { pcm "dmixer" }<br />
control {<br />
name "Master"<br />
card 0<br />
}<br />
}<br />
ctl.!default {<br />
type hw<br />
card 0<br />
}<br />
ctl.softvol {<br />
type hw<br />
card 0<br />
}<br />
ctl.dmixer {<br />
type hw<br />
card 0<br />
}<br />
<br />
===Pops When Starting and Stopping Playback===<br />
Some modules (e.g. snd_ac97_codec and snd_hda_intel) can power off your sound card when not in use. This can make an audible noise (like a crack/pop/scratch) when turning on/off your sound card. Sometimes even when move the slider volume, or open and close windows (KDE4). If you find this annoying try {{ic|modinfo snd_MY_MODULE}}, and look for a module option that adjusts or disables this feature.<br />
<br />
Example: to disable the power saving mode and solve cracking sound trough speakers issue, using snd_hda_intel add in {{ic|/etc/modprobe.d/modprobe.conf}}<br />
options snd_hda_intel power_save=0<br />
or<br />
options snd_hda_intel power_save=0 power_save_controller=N<br />
<br />
You can also try it with {{ic|1=modprobe snd_hda_intel power_save=0}} before.<br />
<br />
You may also have to unmute the 'Line' ALSA channel for this to work. Any value will do (other than '0' or something too high).<br />
<br />
''Example:'' on an onboard VIA VT1708S (using the snd_hda_intel module) these cracks occured even though 'power_save' was set to 0. Unmuting the 'Line' channel and setting a value of '1' solved the issue.<br />
<br />
Source: https://www.kernel.org/doc/Documentation/sound/alsa/powersave.txt<br />
<br />
If you use a laptop, pm-utils will change {{ic|power_save}} back to 1 when you go onto battery power even if you disable power saving in {{ic|/etc/modprobe.d}}. Disable this for pm-utils by disabling the script that makes the change (see [https://wiki.archlinux.org/index.php/Pm-utils#Disabling_a_hook Disabling a hook] for more information):<br />
# touch /etc/pm/power.d/intel-audio-powersave<br />
<br />
===S/PDIF Output Does Not Work===<br />
<br />
If the optical/coaxial digital output of your motherboard/sound card is not working or stopped working, and have already enabled and unmuted it in alsamixer, try running<br />
# iecset audio on<br />
<br />
as root.<br />
<br />
You can also put this command in {{Ic|rc.local}} as it sometimes it may stop working after a reboot.<br />
<br />
===HDMI Output Does Not Work===<br />
If the HDMI output of your motherboard/sound card is not working or stopped working, and have already enabled and unmuted it in alsamixer, try the following.<br />
<br />
Query for Playback Devices:<br />
<br />
$ aplay -l<br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: NVidia [HDA NVidia], device 0: ALC1200 Analog [ALC1200 Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 1: ALC1200 Digital [ALC1200 Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 3: NVIDIA HDMI [NVIDIA HDMI]<br />
Subdevices: 0/1<br />
Subdevice #0: subdevice #0<br />
<br />
Now that we have the info for the HDMI device, try a test, In the example below, 0 is the card number and 3 is the device number.<br />
<br />
$ aplay -D plughw:0,3 /usr/share/sounds/alsa/Front_Center.wav<br />
<br />
If aplay does not output any errors, but still no sound is heared, "reboot" the receiver, monitor or tv set. Since the HDMI interface executes a handshake on connection, it might have noticed before that there was no audio stream embedded, and disabled audio decoding. In particular, if you are using a standalone window manager (don’t know about Gnome or KDE), you may need to have some sound playing ''while'' plugging in the HDMI cable.<br />
<br />
{{Note|If you are using an ATI card and linux 3.0, check your kernel/grub settings, you most likely need to use the ''''radeon.audio'''<nowiki>=</nowiki>'''1'''' boot parameter, or alternatively set up the option in your modprobe configuration files. It's important to note that before trying, is to check the [http://www.x.org/wiki/RadeonFeature#fndef-72849d6d6eb3927d486f50ece74a739042965a6c-25 Radeon Feature Matrix] to see if it's actually supported. At the moment this is written (march 29th, 2013), some chipsets of the [http://www.x.org/wiki/RadeonFeature#Decoder_ring_for_engineering_vs_marketing_names S. Islands] (namely HD7750 through HD7970) doesn't (still) support HDMI Audio.}}<br />
<br />
If the test is successful, create or edit your ~/.asoundrc file to set HDMI as the default audio device.<br />
<br />
{{hc|~/.asoundrc|<br />
pcm.!default {<br />
type hw<br />
card 0<br />
device 3<br />
}<br />
}}<br />
<br />
Or you above config does not work try:<br />
{{hc|~/.asoundrc|<br />
defaults.pcm.card 0<br />
defaults.pcm.device 3<br />
defaults.ctl.card 0<br />
}}<br />
<br />
===HDMI Multi-channel PCM output does not work (Intel) ===<br />
As of Linux 3.1 multi-channel PCM output through HDMI with a Intel card (Intel Eaglelake, IbexPeak/Ironlake,SandyBridge/CougarPoint and IvyBridge/PantherPoint) is not yet supported. Support for it has been recently added and expected to be available in Linux 3.2. To make it work in Linux 3.1 you need to apply the following patches:<br />
<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=76adaa34db407f174dd06370cb60f6029c33b465 drm: support routines for HDMI/DP ELD]<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=e0dac65ed45e72fe34cc7ccc76de0ba220bd38bb drm/i915: pass ELD to HDMI/DP audio driver]<br />
<br />
===HP TX2500===<br />
Add these 2 lines into {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-cmipci mpu_port=0x330 fm_port=0x388<br />
options snd-hda-intel index=0 model=toshiba position_fix=1<br />
<br />
options snd-hda-intel model=hp (works for tx2000cto)<br />
<br />
===Skipping Sound When Playing MP3===<br />
If you have sound skipping when playing MP3 files and you have more then 2 speakers attached to your computer (i.e. > 2 speaker system), run alsamixer and disable the channels for the speakers that you '''DO NOT''' have (i.e. do not enable the sound for the center speaker if you do not have a center speaker.<br />
<br />
===Using a USB Headset and External USB Sound Cards===<br />
If you are using a USB headset with ALSA you can try using {{AUR|asoundconf}} (currently only available from the [[AUR]]) to set the headset as the primary sound output. Before running make sure you have usb audio module enabled ({{ic|modprobe snd-usb-audio}}).<br />
<br />
# asoundconf is-active<br />
# asoundconf list<br />
# asoundconf set-default-card <chosen soundcard><br />
<br />
==== Crackling sound with USB devices ====<br />
If you experience crackling sound on USB devices, you can try tuning the snd-usb-audio for minimal latency.<br />
<br />
Add this to your {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
options snd-usb-audio nrpacks=1<br />
<br />
source: http://alsa.opensrc.org/Usb-audio#Tuning_USB_devices_for_minimal_latencies<br />
<br />
==== Hot-plugging a USB Sound Card ====<br />
In order to automatically make a USB Sound Card the primary output device, when the card is plugged in, you can use the following udev rules (e.g. add the following two lines to {{ic|/etc/udev/rules.d/00-local.rules}} and reboot).<br />
<br />
{{bc|1=KERNEL=="pcmC[D0-9cp]*", ACTION=="add", PROGRAM="/bin/sh -c 'K=%k; K=$${K#pcmC}; K=$${K%%D*}; echo defaults.ctl.card $$K > /etc/asound.conf; echo defaults.pcm.card $$K >>/etc/asound.conf'"<br />
KERNEL=="pcmC[D0-9cp]*", ACTION=="remove", PROGRAM="/bin/sh -c 'echo defaults.ctl.card 0 > /etc/asound.conf; echo defaults.pcm.card 0 >>/etc/asound.conf'"}}<br />
<br />
===Error 'Unknown hardware' Appears After a Kernel Update===<br />
The following messages may be displayed during the start-up ALSA after the kernel update:<br />
Unknown hardware "foo" "bar" ...<br />
Hardware is initialized using a guess method<br />
/usr/sbin/alsactl: set_control:nnnn:failed to obtain info for control #mm (No such file or directory)<br />
<br />
or<br />
Found hardware: "HDA-Intel" "VIA VT1705" "HDA:11064397,18490397,00100000" "0x1849" "0x0397"<br />
Hardware is initialized using a generic method<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #1 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #2 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #25 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #26 (No such file or directory)<br />
<br />
Simply store ALSA mixer settings again (as root):<br />
# alsactl -f /var/lib/alsa/asound.state store<br />
<br />
It may be necessary configure ALSA again with alsamixer<br />
<br />
===HDA Analyzer===<br />
If the mappings to your audio pins(plugs) do not correspond but ALSA works fine, you could try HDA Analyzer -- a pyGTK2 GUI for HD-audio control can be found [http://www.alsa-project.org/main/index.php/HDA_Analyzer at the ALSA wiki].<br />
Try tweaking the Widget Control section of the PIN nodes, to make microphones IN and headphone jacks OUT. Referring to the Config Defaults heading is a good idea.<br />
<br />
NOTE: the script is done by such way that it is incompatible with python3 (which is now shipped with ArchLinux) but tries to use it.<br />
The workaround is: open "run.py", find all occurences of "python" (2 occurences - one on the first line, and the second on the last line) and replace them all by "python2".<br />
<br />
NOTE2: the script requires root acces, but running it via su/sudo is bogus. Run it via {{ic|kdesu}} or {{ic|gksu}}.<br />
<br />
===ALSA with SDL===<br />
If you get no sound via SDL and ALSA cannot be chosen from the application. Try setting the environmental variable SDL_AUDIODRIVER to alsa.<br />
# export SDL_AUDIODRIVER=alsa<br />
<br />
===Low Sound Workaround===<br />
<br />
If you are facing low sound even after maxing out your speakers/headphones, you can give the softvol plugin a try. Add the following to {{ic|/etc/asound.conf}}.<br />
{{bc|<nowiki><br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
<br />
pcm.softvol {<br />
type softvol<br />
slave {<br />
pcm "dmix"<br />
}<br />
control {<br />
name "Pre-Amp"<br />
card 0<br />
}<br />
min_dB -5.0<br />
max_dB 20.0<br />
resolution 6<br />
}<br />
</nowiki>}}<br />
{{note| You will probably have to restart the computer, as restarting the alsa daemon did not load the new configuration for me. Also, if the configuration does not work even after restarting, try changing {{ic|plug}} with {{ic|hw}} in the above configuration.}}<br />
<br />
After the changes are loaded successfully, you will see a {{ic|Pre-Amp}} section in alsamixer. You can adjust the levels there.<br />
{{note|Setting a high value for {{ic|Pre-Amp}} can cause sound distortion, so adjust it according to the level that suits you.}}<br />
<br />
===Popping sound after resuming from suspension===<br />
You might hear a popping sound after resuming the computer from suspension. This can be fixed by editing {{ic|/etc/pm/sleep.d/90alsa}} and removing the line that says {{ic|aplay -d 1 /dev/zero}}<br />
<br />
==配置文件范例==<br />
参见:[[Advanced Linux Sound Architecture/Example Configurations]]。<br />
<br />
==相关阅读==<br />
* [http://www.mjmwired.net/kernel/Documentation/sound/alsa/ALSA-Configuration.txt ALSA 模块高级配置]<br />
* [http://alsa.opensrc.org/Main_Page 非官方的 ALSA Wiki]<br />
* [https://bbs.archlinux.org/viewtopic.php?id=36815 如何从 svn 获取驱动代码并编译]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Advanced_Linux_Sound_Architecture_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=256309
Advanced Linux Sound Architecture (简体中文)
2013-05-07T12:38:45Z
<p>Cuihao: /* Upmixing/Downmixing */</p>
<hr />
<div>[[Category:简体中文]]<br />
[[Category:Sound (简体中文)]]<br />
[[Category:Audio/Video (简体中文)]]<br />
[[cs:Advanced Linux Sound Architecture]]<br />
[[de:Alsa]]<br />
[[es:Advanced Linux Sound Architecture]]<br />
[[fa:ALSA]]<br />
[[fr:Alsa]]<br />
[[he:Advanced Linux Sound Architecture]]<br />
[[it:Advanced Linux Sound Architecture]]<br />
[[ja:Advanced Linux Sound Architecture]]<br />
[[nl:Advanced Linux Sound Architecture]]<br />
[[pt:Advanced Linux Sound Architecture]]<br />
[[ru:Advanced Linux Sound Architecture]]<br />
[[sk:Advanced Linux Sound Architecture]]<br />
[[th:Advanced Linux Sound Architecture]]<br />
[[tr:Gelişmiş_ses_mimarisi_(ALSA)]]<br />
[[en:Advanced Linux Sound Architecture]]<br />
<br />
{{Article summary start}}<br />
{{Article summary text|ALSA 的使用、配置及疑难解答。}}<br />
{{Article summary heading|相关页面}}<br />
{{Article summary wiki|Sound system (简体中文)}}<br />
{{Article summary wiki|Disable PC Speaker Beep (简体中文)}}<br />
{{Article summary end}}<br />
{{TranslationStatus (简体中文)|Advanced_Linux_Sound_Architecture|2013-04-25|254584}}<br />
<br />
[[Wikipedia:zh:ALSA|高级 Linux 声音体系]](Advanced Linux Sound Architecture,'''ALSA''')是Linux中提供声音设备驱动的内核组件,用来代替原来的开放声音系统(Open Sound System,OSSv3)。除了声音设备驱动,'''ALSA'''还包含一个用户空间的函数库,以方便开发者通过高级API使用驱动功能,而不必直接与内核驱动交互。<br />
<br />
{{注意|关于另一种声音体系,请阅读[[Open Sound System (简体中文)|开放声音系统(OSS)]]的页面。}}<br />
<br />
==安装==<br />
Arch 默认的内核已经通过一套模块提供了 ALSA,不必特别安装。<br />
<br />
[[udev (简体中文)|udev]]会在系统启动时自动检测硬件,并加载相应的声音设备驱动模块。这时,你的声卡已经可以工作了,只是所有声道默认都被设置成静音了。<br />
<br />
在本地登录(通过虚拟终端或登录管理器)的用户,都有权限播放音频并调整音量。要让远程登录的用户拥有这些权限,必须把用户[[Users and Groups (简体中文)#用户组管理|加入]] {{ic|audio}} 用户组。该组的成员可以直接访问声音设备,会导致某些程序独占音频输出(破坏软件混音),还可能影响用户快速切换和拖机(multiseat)。因此,除非真的有某些[https://wiki.ubuntu.com/Audio/TheAudioGroup 特殊需求],'''不建议'''把用户加入 {{ic|audio}} 用户组。<br />
<br />
===用户空间工具===<br />
从 [[Official Repositories (简体中文)|官方仓库]]:<br />
* [[pacman (简体中文)|安装]] {{Pkg|alsa-utils}} 软件包,其中的 {{ic|alsamixer}} 工具允许用户在控制台或终端中配置声音设备。<br />
* 安装 {{Pkg|alsa-oss}} 软件包,如果想让 OSS 应用程序与dmix(软件混音)协同工作。 <br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。}}<br />
*安装 {{Pkg|alsa-plugins}} 软件包,如果需要 [[#高质量重采样|高质量重采样]]、[[#Upmixing/Downmixing|软件模拟环绕立体声]]和其他高级特性。<br />
<br />
==解除各声道的静音==<br />
目前版本的 ALSA 安装后,所有声道'''默认是静音的''',必须手动解除。<br />
<br />
使用 {{ic|alsamixer}} 的 ncurses 界面,配置十分简单:<br />
<br />
$ alsamixer<br />
<br />
此外,还可以在命令行下使用 {{ic|amixer}}:<br />
<br />
$ amixer sset Master unmute<br />
<br />
在 {{ic|alsamixer}} 中,下方标有 {{ic|MM}} 的声道是静音的,而标有 {{ic|00}} 的通道已经启用。<br />
<br />
使用左右方向键,选中 {{ic|Master}} 和 {{ic|PCM}} 声道。按下 {{keypress|m}} 键解除静音。使用上方向键增加音量,直到增益值为{{ic|0}}。该值显示在左上方 {{ic|Item:}} 字段后。过高的增益值会导致声音失真。<br />
<br />
要想得到完整的 5.1 或 7.1 环绕立体声,还得解除 Front、Surround、Center、LFE (subwoofer) 和 Side 这些声道的静音(上述名称是 Intel HD Audio 声卡使用的声道名,可能因设备不同而有所差异)。注意,仅有这些设置,系统不会自动将立体声源(多数音乐)提升(upmix)成环绕立体声。如果需要这些功能,请阅读[[#Upmixing/Downmixing]]。<br />
<br />
要启用麦克风,切换至 Capture 选项卡,按下 {{keypress|F4}},按下 {{keypress|空格}} 启用其中一个声道即可。<br />
<br />
按下 {{Keypress|Esc}} 键退出 alsamixer。<br />
<br />
{{注意|<br />
* 有一些声卡,需要关闭数字输出或将其调成静音,才能输出模拟音频信号。对于 Soundblaster Audigy LS 声卡,需要把 IEC958 通道设成静音。 <br />
* 有些机器(如 Thinkpad T61),有一个 Speaker 通道,必须按照上述方法打开调整。<br />
* 有些机器(如 Dell E6400),可能还需要把 {{ic|Front}} 和 {{ic|Headphone}} 声道打开调整。}}<br />
<br />
接下来,测试声卡是否工作:<br />
<br />
$ speaker-test -c 2<br />
<br />
根据扬声器的实际情况,调整 -c 后的数值。对于 7.1 声道,这个数字是 8:<br />
<br />
$ speaker-test -c 8<br />
<br />
如果没有正常工作,请继续阅读 [[#配置]] 以及 [[#疑难解答]] 部分。<br />
<br />
{{pkg|alsa-utils}} 软件包提供了两个配置 alsa 的服务:{{ic|alsa-restore.service}} 和 {{ic|alsa-store.service}}。它们分别在开机和关机时自动运行。<br />
<br />
==配置==<br />
=== VirtualBox 虚拟机无声音 ===<br />
<br />
如果使用 VirtualBox 时碰上了麻烦,请尝试以下命令:<br />
<br />
{{hc|$ alsactl init|2=<nowiki><br />
<br />
Found hardware: "ICH" "SigmaTel STAC9700,83,84" "AC97a:83847600" "0x8086" "0x0000"<br />
Hardware is initialized using a generic method<br />
</nowiki><br />
}}<br />
<br />
如果还是不行,请检查所使用的音频软件是否激活了 ALSA 输出。<br />
<br />
===设置默认声卡===<br />
<br />
如果发现开机时声卡次序会发生变化,可以在通过 {{ic|/etc/modprobe.d}} 中的 {{ic|.conf}} 文件(比如 {{ic|/etc/modprobe.d/alsa-base.conf}})手动设置次序。<br />
比如,要让 mia 声卡成为 #0、Intel HDA 声卡成为 #1:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<br />
options snd slots=snd_mia,snd_hda_intel<br />
options snd_mia index=0<br />
options snd_hda_intel index=1<br />
}}<br />
<br />
使用 {{ic|<nowiki>lsmod | grep snd</nowiki>}} 获取设备列表。<br />
<br />
snd_mia 和 snd_hda_intel 分别是两个声卡的内核驱动模块。该配置仅适用于同时装有 mia 声卡和使用 snd_hda_intel 驱动的声卡(通常是板载声卡)的情况。<br />
<br />
如果使用 -2 的 index 值,ALSA 就不会将对应的设备作为主声卡使用。Linux Mint 和 Ubuntu 等发行版使用了以下配置,避免 USB 声卡和其他“非主流”设备变成 index 为 0 的主声卡:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<br />
options bt87x index=-2<br />
options cx88_alsa index=-2<br />
options saa7134-alsa index=-2<br />
options snd-atiixp-modem index=-2<br />
options snd-intel8x0m index=-2<br />
options snd-via82xx-modem index=-2<br />
options snd-usb-audio index=-2<br />
options snd-usb-caiaq index=-2<br />
options snd-usb-ua101 index=-2<br />
options snd-usb-us122l index=-2<br />
options snd-usb-usx2y index=-2<br />
# Keep snd-pcsp from being loaded as first soundcard<br />
options snd-pcsp index=-2<br />
# Keep snd-usb-audio from beeing loaded as first soundcard<br />
options snd-usb-audio index=-2<br />
}}<br />
<br />
以上配置会在系统重启后生效。<br />
<br />
{{Out of date|据英文 Wiki 用户反应,{{ic|/etc/asound.conf}} 这个配置文件默认是不存在的,下列操作可能有误。}}<br />
<br />
首先,运行 {{ic|aplay -l}},获取声卡的声卡ID和设备ID:<br />
<br />
{{hc|$ aplay -l|2=<nowiki><br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: Intel [HDA Intel], device 0: CONEXANT Analog [CONEXANT Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: Intel [HDA Intel], device 1: Conexant Digital [Conexant Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 1: JamLab [JamLab], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 2: Audio [Altec Lansing XT1 - USB Audio], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
</nowiki>}}<br />
<br />
拿输出中的最后一项来说,这个声卡的声卡ID为2,设备ID为0。若要把这块声卡作为默认声卡,可以把下列配置添加到系统级别的 {{ic|/etc/asound.conf}} 或用户级别的 {{ic|~/.asoundrc}} 文件。如果文件不存在,可以手动创建。其中的各个ID,请根据实际情况调整:<br />
{{bc|<br />
defaults.pcm.card 2<br />
defaults.pcm.device 0<br />
defaults.ctl.card 2<br />
}}<br />
“pcm”选项决定用来播放音频的设备,而“ctl”选项决定那个声卡能够由控制工具(如 alsamixer)使用。<br />
<br />
上述配置在重启音频程序(如 mplayer)后立即生效。<br />
<br />
===确认所有声音模块都已经加载===<br />
<br />
一般 udev 都会自动识别出声卡。使用以下命令确认:<br />
<br />
{{hc|<nowiki>$ lsmod | grep '^snd' | column -t</nowiki>|<nowiki><br />
snd_hda_codec_hdmi 22378 4<br />
snd_hda_codec_realtek 294191 1<br />
snd_hda_intel 21738 1<br />
snd_hda_codec 73739 3 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel<br />
snd_hwdep 6134 1 snd_hda_codec<br />
snd_pcm 71032 3 snd_hda_codec_hdmi,snd_hda_intel,snd_hda_codec<br />
snd_timer 18992 1 snd_pcm<br />
snd 55132 9 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel,snd_hda_codec,snd_hwdep,snd_pcm,snd_timer<br />
snd_page_alloc 7017 2 snd_hda_intel,snd_pcm<br />
</nowiki>}}<br />
<br />
如果你的输出和上面类似,那就说明声卡已经被正确识别。<br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。如有需要,请[[Kernel_modules_(简体中文)#装入|手动加载]]。}}<br />
<br />
还可以检查一下 {{ic|/dev/snd/}} 目录,看看是否有这些设备文件:<br />
<br />
{{hc|$ ls -l /dev/snd|2=<nowiki><br />
total 0<br />
crw-rw---- 1 root audio 116, 0 Apr 8 14:17 controlC0<br />
crw-rw---- 1 root audio 116, 32 Apr 8 14:17 controlC1<br />
crw-rw---- 1 root audio 116, 24 Apr 8 14:17 pcmC0D0c<br />
crw-rw---- 1 root audio 116, 16 Apr 8 14:17 pcmC0D0p<br />
crw-rw---- 1 root audio 116, 25 Apr 8 14:17 pcmC0D1c<br />
crw-rw---- 1 root audio 116, 56 Apr 8 14:17 pcmC1D0c<br />
crw-rw---- 1 root audio 116, 48 Apr 8 14:17 pcmC1D0p<br />
crw-rw---- 1 root audio 116, 1 Apr 8 14:17 seq<br />
crw-rw---- 1 root audio 116, 33 Apr 8 14:17 timer<br />
</nowiki>}}<br />
<br />
{{注意|在 IRC 或论坛寻求这方面帮助时,别忘了贴出上面几个命令的输出。}}<br />
<br />
如果你的输出跟上面类似,或至少有 '''controlC0''' 和 '''pcmC0D0p''',那么声卡就已经正常加载了。<br />
<br />
如果出现问题,声卡模块没有正确加载,那么请尝试手动加载模块:<br />
<br />
* 确定声卡对应的驱动模块:[http://www.alsa-project.org/main/index.php/Matrix:Main ALSA Soundcard Matrix]。这些模块都会有一个”snd-“前缀(例如:{{ic|snd-via82xx}})。<br />
* [[Kernel_modules_(简体中文)#装入|加载模块]]。<br />
* 检查 {{ic|/dev/snd}} 目录中的设备文件(参见上文);或者,检查 {{ic|alsamixer}} 或 {{ic|amixer}} 的输出是否正确。<br />
* 修改配置,使 {{ic|snd-<模块名>}} 和 {{ic|snd-pcm-oss}} 模块[[Kernel_modules_(简体中文)#装入|开机自动加载]]。<br />
<br />
===启用 SPDIF 输出===<br />
<br />
(由 Gentoo 论坛的 gralves 提供)<br />
* 在 GNOME 音量控制的选项(Options)标签中,把 IEC958 改成 PCM。该选项可以在首选项中启用。<br />
* 不使用 GNOME 音量控制工具的话:<br />
** 编辑 {{ic|/var/lib/alsa/asound.state}}。该文件是 alsasound 保存混音器设置的地方。<br />
** 找到这一行:“IEC958 Playback Switch”。该行上下应该有一行内容为 {{ic|value:false}},将其改为 {{ic|value:true}}。<br />
** 再找到这一行:”IEC958 Playback AC97-SPSA“。把对应的值改为 0。<br />
** 重启 ALSA。<br />
<br />
{{Out of date|rc.local 在 systemd 中已经弃用,以下方法可能不再适用。}}<br />
<br />
还有另外一个开机自动启用 SPDIF 输出的方法(在 SoundBlaster Audigy 声卡上测试过):<br />
<br />
* 在 {{ic|/etc/rc.local}} 中添加如下内容:<br />
# Use COAX-digital output<br />
amixer set 'IEC958 Optical' 100 unmute<br />
amixer set 'Audigy Analog/Digital Output Jack' on<br />
<br />
使用以下命令,获取你的声卡的数字输出的名称:<br />
$ amixer scontrols<br />
<br />
===系统级均衡器===<br />
====使用 AlsaEqual(包含界面)====<br />
从 [[AUR (简体中文)|AUR]] 安装 {{AUR|alsaequal}}。<br />
<br />
{{注意|如果使用的是64位系统,而又安装了32位的 Flash 插件,这里的设置会导致 Flash 无声。如果发生了这种情况,请禁用 alsaequal 或编译32位版本的 alsaequal。}}<br />
<br />
安装后,把下列内容添加到 ALSA 配置文件({{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}}):<br />
<br />
ctl.equal {<br />
type equal;<br />
}<br />
<br />
pcm.plugequal {<br />
type equal;<br />
# Modify the line below if you do not<br />
#slave.pcm "plughw:0,0";<br />
#by default we want to play from more sources at time:<br />
slave.pcm "plug:dmix";<br />
}<br />
#pcm.equal {<br />
# If you do not want the equalizer to be your<br />
# default soundcard comment the following<br />
# line and uncomment the above line. (You can<br />
# choose it as the output device by addressing<br />
# it with specific apps,eg mpg123 -a equal 06.Back_In_Black.mp3)<br />
pcm.!default {<br />
type plug;<br />
slave.pcm plugequal;<br />
}<br />
<br />
设置好后,切换均衡器:<br />
$ alsamixer -D equal<br />
<br />
需要注意,每个用户的 alsaequal 配置文件都是独立的,位于 {{ic|~/.alsaequal.bin}}。因此,如果想在 [[mpd]] 之类的以独立用户身份运行的程序中使用 AlsaEqual,就得像下面这样:<br />
# su mpd -c 'alsamixer -D equal'<br />
或者,在其他用户的主目录下创建符号链接,指向你的 {{ic|.alsaequal.bin}}……<br />
<br />
=====管理 AlsaEqual 配置=====<br />
从 [http://xyne.archlinux.ca/repos/ Xyne 的软件仓库] 或 [https://aur.archlinux.org/packages.php?ID=62420 AUR] 安装 [http://xyne.archlinux.ca/projects/alsaequal-mgr/ alsaequal-mgr]。<br />
<br />
同上,配置均衡器: <br />
$alsamixer -D equal<br />
<br />
如果对效果感到满意,将其命名(如“foo”)并保存该配置:<br />
$alsaequal-mgr save foo<br />
<br />
重新加载“foo”中的配置:<br />
$alsaequal-mgr load foo<br />
<br />
这样,就可以为游戏、电影、不同音乐流派、网络电话等等创建不同的均衡器配置,以便随时切换。<br />
<br />
参见[http://xyne.archlinux.ca/projects/alsaequal-mgr/ 项目主页]和程序的帮助信息,了解更多设置。<br />
<br />
====使用 mbeq====<br />
{{注意|该软件用到了一个 ladspa 插件,可能导致播放音频时 CPU 占用率偏高。In addition, this was made with stereophonic sound (e.g. headphones) in mind.}}<br />
<br />
安装 {{Pkg|alsa-plugins}}、{{Pkg|ladspa}} 和 {{Pkg|swh-plugins}} 软件包。<br />
<br />
* 在 {{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}} 中添加如下内容:<br />
<br />
{{hc|/etc/asound.conf|2=<nowiki>pcm.eq {<br />
type ladspa<br />
<br />
# The output from the EQ can either go direct to a hardware device<br />
# (if you have a hardware mixer, e.g. SBLive/Audigy) or it can go<br />
# to the software mixer shown here.<br />
#slave.pcm "plughw:0,0"<br />
slave.pcm "plug:dmix"<br />
<br />
# Sometimes you may need to specify the path to the plugins,<br />
# especially if you have just installed them. Once you have logged<br />
# out/restarted this should not be necessary, but if you get errors<br />
# about being unable to find plugins, try uncommenting this.<br />
#path "/usr/lib/ladspa"<br />
<br />
plugins [<br />
{<br />
label mbeq<br />
id 1197<br />
input {<br />
#this setting is here by example, edit to your own taste<br />
#bands: 50hz, 100hz, 156hz, 220hz, 311hz, 440hz, 622hz, 880hz, 1250hz, 1750hz, 25000hz,<br />
#50000hz, 10000hz, 20000hz<br />
controls [ -5 -5 -5 -5 -5 -10 -20 -15 -10 -10 -10 -10 -10 -3 -2 ]<br />
}<br />
}<br />
]<br />
}<br />
<br />
# Redirect the default device to go via the EQ - you may want to do<br />
# this last, once you are sure everything is working. Otherwise all<br />
# your audio programs will break/crash if something has gone wrong.<br />
<br />
pcm.!default {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
<br />
# Redirect the OSS emulation through the EQ too (when programs are running through "aoss")<br />
<br />
pcm.dsp0 {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
</nowiki>}}<br />
<br />
* 完成。若有问题,欢迎前往论坛咨询。<br />
<br />
==高质量重采样==<br />
启用软件混音时,ALSA 会强制把所有音频重采样到相同的频率(如果系统支持,默认是 48000)。dmix 的重采样算法很糟糕,会导致显著的质量损失。<br />
<br />
安装 {{pkg|alsa-plugins}} 和 {{pkg|libsamplerate}}。<br />
<br />
将默认的采样率转换器更改为 libsamplerate:<br />
<br />
{{hc|/etc/asound.conf|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
或:<br />
<br />
{{hc|~/.asoundrc|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
'''samplerate_best''' 输出的声音质量最佳,但需要耗费大量CPU资源进行实时重采样。还有其他的算法可供选择(如 '''samplerate''' 等),但比起默认的算法,这些算法的改进并不明显。<br />
<br />
{{警告|某些系统中,启用 samplerate_best 可能导致 Flash 播放器无声音。}}<br />
<br />
==上混和下混==<br />
{{注意|由于未找到 upmix 和 downmix 的常用翻译,译者暂时采用字面翻译(上混、下混)。所谓“上混”,即将声道较少的立体声(如常见的双声道立体声)信号,通过软件或硬件处理,模拟为声道较多的立体声(如5.1环绕立体声)信号。反之即为“下混”。}}<br />
<br />
===上混(upmixing)===<br />
播放双声道立体声音源(如音乐)时,利用上混,即可充分利用 5.1 或 7.1 环绕立体声的所有声道。以前,上混很麻烦,还经常出错。但如今就方便多了,只需要安装一些插件即可。这些插件由 {{Pkg|alsa-plugins}} 软件包提供。<br />
<br />
安装后,在配置文件({{ic|/etc/asound.conf}} 或 {{ic|~/.asoundrc}})中添加如下内容:<br />
pcm.upmix71 {<br />
type upmix<br />
slave.pcm "surround71"<br />
delay 15<br />
channels 8<br />
}<br />
<br />
该范例适用于7.1声道上混。类比该范例,同样能设置5.1或4.0声道上混。<br />
<br />
该设置添加了一个新的用于上混的 pcm。如果希望所有音频源都通过该 pcm 输出,在上述设置后添加如下配置即可:<br />
<br />
pcm.!default "plug:upmix71"<br />
<br />
插件默认配置就允许多个音频源同时通过它输出,一般无需额外配置。如果不行,就得向下面这样为上混配置 dmixer:<br />
pcm.dmix6 {<br />
type asym<br />
playback.pcm {<br />
type dmix<br />
ipc_key 567829<br />
slave {<br />
pcm "hw:0,0"<br />
channels 6<br />
}<br />
}<br />
}<br />
<br />
注意要用 dmix6 而非 surround71。<br />
<br />
如果遇到声音卡顿或混乱,可能需要增加 buffer_size(比如增加到 32768),或使用[[#高质量重采样|高质量重采样]]。<br />
<br />
===下混(downmixing)===<br />
有时也会用到下混,比如在只支持双声道立体声的电脑上观看5.1声道的电影时。这一功能由 ALSA 插件 vdownmix 实现(同样在 {{Pkg|alsa-plugins}} 软件包里)。<br />
<br />
在配置文件中添加如下内容:<br />
<br />
pcm.!surround51 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
pcm.!surround40 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
<br />
==混音==<br />
===软件混音(dmix)===<br />
<br />
{{注意|在 ALSA 1.0.9rc2 及以上版本中,如果声卡不支持硬件混音,模拟输出将默认使用 dmix 混音,无需额外设置。}}<br />
<br />
如果软件混音未正常启用,那么可以手动创建 {{ic|~/.asoundrc}},加入以下内容:<br />
<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
<br />
这样就启用了软件混音,多个程序能够同时使用声卡。<br />
<br />
至于 S/PDIF 之类的数字音频输出,目前的 ALSA 还不会默认启用 dmix。因此,必须像上面这样手动启用。<br />
<br />
常见问题的解答,参见[[#疑难解答]]部分。<br />
<br />
===硬件混音===<br />
====支持====<br />
如果声卡支持,一般无需任何配置即可启用硬件混音。绝大多数板载声卡都不支持硬件混音,必须得使用软件混音(见上文)。很多独立声卡都支持硬件混音,Linux 下支持较好的有:<br />
<br />
* Creative SoundBlaster Live! (5.1 型号)<br />
* Creative SoundBlaster Audigy (部分型号)<br />
* Creative SoundBlaster Audidy 2 (ZS 系列型号)<br />
* Creative SoundBlaster Audigy 4 (Pro 系列型号)<br />
<br />
{{注意|上述声卡的低端版本(Audigy SE、Audigy 2 NX、SoundBlaster Live! 24bit 以及 SoundBlaster Live! 7.1)使用了不同的芯片,'''不支持'''硬件混音。}}<br />
<br />
{{注意|板载 VIA8237 芯片支持4路硬件混音,但在某些主板上只能使用3路(第4路无声),甚至根本无法使用。即使能正常工作,质量也比不上其他混音方法。}}<br />
<br />
====问题修复====<br />
如果你的声卡是 Intel Corporation 82801I (ICH9 Family) HD Audio Controller (rev 02),且使用了64位系统,需要执行以下命令才能在 Enemy Territory 中听到声音:<br />
echo "et.x86 0 0 direct" > /proc/asound/card0/pcm0p/oss<br />
echo "et.x86 0 0 disable" > /proc/asound/card0/pcm0c/oss<br />
<br />
==疑难解答==<br />
===使用CPU频率动态调整时,音频发生跳跃===<br />
使用 {{ic|ondemand}} 或 {{ic|conservative}} 频率策略、启用CPU频率动态调整时,某些声卡和某些 ALSA 驱动会出现声音跳跃的情况。目前,唯一的解决方案是使用固定的频率,如 {{ic|performance}} 性能策略。<br />
<br />
详情参见 [[CPU Frequency Scaling (简体中文)]]。<br />
<br />
===同时只能有一个用户播放音频===<br />
有时会出现一个用户独占软件混音器的情况。多数情况下这没什么影响,但在使用 [[mpd]] 这样以独立用户身份运行的程序时,就会出现问题。当 mpd 运行时,用户自己就无法播放其他音频了。一种解决方法是,以正在使用的用户身份运行 mpd。另一种方法是,在 ALSA 配置文件的 {{ic|pcm.dmixer}} 部分添加 {{ic|ipc_key_add_uid 0}} 禁止独占。以下是 {{ic|asound.conf}} 示例的片段,省略的部分无需修改:<br />
<br />
...<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
slave {<br />
...<br />
<br />
===无法同时播放多个音频===<br />
如果安装了 [[PulseAudio (简体中文)|PulseAudio]](可能作为 [[GNOME (简体中文)|GNOME]] 的依赖被安装),可能出现无法同步播放的问题,因为 PulseAudio 的默认配置是“独占”声卡。ALSA 的用户可能对自己的配置已经很满意、不希望使用 [[PulseAudio (简体中文)|PulseAudio]]。解决方案是编辑 {{ic|/etc/asound.conf}} 并注释掉以下内容:<br />
# Use PulseAudio by default<br />
#pcm.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
# hint {<br />
# show on<br />
# description "Default ALSA Output (currently PulseAudio Sound Server)"<br />
# }<br />
#}<br />
<br />
可能还需要注释掉下面的内容:<br />
#ctl.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
#}<br />
<br />
比起完全卸载 [[PulseAudio (简体中文)|PulseAudio]],这大概是个更简单的方法。<br />
<br />
以下是 {{ic|/etc/asound.conf}} 配置文件的一个示例:<br />
{{bc|<nowiki>pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
}<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}</nowiki><br />
}}<br />
<br />
{{注意|本 {{ic|/etc/asound.conf}} 本来是配合 [[MPD]] 全局配置使用的。参见[[#同时只能有一个用户播放音频|该节]]关于多用户的内容。}}<br />
<br />
===有时候开机无声===<br />
如果会随机地出现开机无声的问题,原因可能是多声卡的顺序发生了变化。如果是这种情况,请参照[[#设置默认声卡]]进行设置。<br />
<br />
对于 mpd 用户,上述配置无效,请参考[http://mpd.wikia.com/wiki/Configuration#ALSA_MPD_software_volume_control 该文]设置。<br />
<br />
===特定程序的问题===<br />
对于不使用系统声音配置的程序,如 XMMS 和 Mplayer,必须特别设置以应用 ALSA 配置。<br />
<br />
对于 mplayer,将以下内容添加到 {{ic|~/.mplayer/config}} (或系统全局配置文件 {{ic|/etc/mplayer/mplayer.conf}}):<br />
ao=alsa<br />
<br />
对于 XMMS 和 Beep Media Player,打开选项窗口,要把声音驱动设置为 ALSA 而不是 OSS。<br />
<br />
XMMS 的设置方法:<br />
* 启动 XMMS<br />
** 选项 -> 首选项<br />
** 选择 Alsa 输出插件<br />
<br />
对于未提供 ALSA 输出的程序,可以使用 alsa-oss 软件包中的 aoss 工具启动。使用方法是:在要运行的程序的命令行前加上 {{ic|aoss}}。例如:<br />
aoss realplay<br />
<br />
pcm.!default{ ... } 貌似已经不管用了。但下面的设置有效:<br />
pcm.default pcm.dmixer<br />
<br />
===特定型号的配置===<br />
ALSA 会通过 BIOS 检测[http://www.kernel.org/doc/Documentation/sound/alsa/HD-Audio-Models.txt 声卡型号],少数情况下也可能无法正确识别。声卡芯片类型可以在 {{ic|alsamixer}} 界面上找到(如 ALC662),而型号则要设置在 {{ic|/etc/modprobe.d/modprobe.conf}} 或 {{ic|/etc/modprobe.d/sound.conf}} 文件中:<br />
<br />
options snd-hda-intel model=<型号><br />
<br />
具体设置因声卡型号而异。多数情况下不需要特地这样做。如果想知道具体声卡型号的设置,请查阅 [http://bugtrack.alsa-project.org/main/index.php/Matrix:Main Alsa Soundcard List],找到你的声卡型号,点击“Details”,然后查看“Setting up modprobe...”小节,将其中的内容插入 {{ic|/etc/modprobe.d/modprobe.conf}} 文件即可。例如,Intel AC97 的配置如下:<br />
<br />
{{bc|# ALSA portion<br />
alias char-major-116 snd<br />
alias snd-card-0 snd-intel8x0<br />
# module options should go here<br />
<br />
# OSS/Free portion<br />
alias char-major-14 soundcore<br />
alias sound-slot-0 snd-card-0<br />
<br />
# card #1<br />
alias sound-service-0-0 snd-mixer-oss<br />
alias sound-service-0-1 snd-seq-oss<br />
alias sound-service-0-3 snd-pcm-oss<br />
alias sound-service-0-8 snd-seq-oss<br />
alias sound-service-0-12 snd-pcm-oss}}<br />
<br />
===与蜂鸣器冲突===<br />
如果你确认所有声道都已解除静音,声卡驱动配置正确,且音量大小也合适,但还是听不到声音,那么请试试将下面的内容添加到 {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
(如果是 {{ic|via82xx}} 驱动)<br />
options snd-<模块名请自行补充> ac97_quirk=0<br />
<br />
(如果是 {{ic|snd_intel8x0}} 驱动)<br />
options snd-NAME-OF-MODULE ac97_quirk=1<br />
<br />
===麦克风输入无声音===<br />
In alsamixer, make sure that all the volume levels are up under recording, and that CAPTURE is toggled active on the microphone (e.g. Mic, Internal Mic) and/or on Capture (in alsamixer, select these items and press space). Try making positive Mic Boost and raising Capture and Digital levels higher; this make make static or distortion, but then you can adjust them back down once you are hearing ''something'' when you record<br />
<br />
As the pulseaudio wrapper is shown as "default" in alsamixer, you may have to press F6 to select your actual soundcard first. You may also need to enable and increase the volume of Line-in in the Playback section.<br />
<br />
To test the microphone, run these commands (see arecord's man page for further information):<br />
arecord -d 5 test-mic.wav<br />
aplay test-mic.wav<br />
<br />
If all fails, you may want to eliminate hardware failure by testing the microphone with a different device.<br />
<br />
For at least some computers, muting a microphone (MM) simply means its input does not go immediately to the speakers. It still receives input.<br />
<br />
Many Dell laptops need "-dmic" to be appended to the model name in {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-hda-intel model=dell-m6-dmic<br />
<br />
Some programs use try to use OSS as the main input software. Add the following lines to {{ic|/etc/modprobe.d/modprobe.conf}} to prevent OSS modules from being loaded:<br />
<br />
{{Note|The OSS modules are no longer autoloaded anyway.}}<br />
<br />
blacklist snd_pcm_oss<br />
blacklist snd_mixer_oss<br />
blacklist snd_seq_oss<br />
<br />
See also:<br />
* http://www.alsa-project.org/main/index.php/SoundcardTesting<br />
* http://alsa.opensrc.org/Record_from_mic<br />
<br />
===设置默认的麦克风/录音设备===<br />
某些程序(Pidgin、Adobe Flash)提供了设置录音设备的选项。如果录音设备不是内置的声卡,而是外接的(如USB摄像头、麦克风),就会有些问题。如果只想更改默认的录音设备、不更改回放设备,需要像下面这样设置 {{ic|~/.asoundrc}}:<br />
<br />
pcm.usb<br />
{<br />
type hw<br />
card U0x46d0x81d<br />
}<br />
<br />
pcm.!default<br />
{<br />
type asym<br />
playback.pcm<br />
{<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
capture.pcm<br />
{<br />
type plug<br />
slave.pcm "usb"<br />
}<br />
}<br />
<br />
把“U0x46d0x81d”替换为你的录音设备的名称。可以通过 {{ic|arecord -L}} 查看所有 ALSA 识别到的录音设备。<br />
<br />
===内置麦克风无效===<br />
首先,请检查 alsamixer,确保所有的录音声道的音量都达到可以录制的程度。如果还是不行,试试修改 /etc/sound.conf 并重载 snd-* 模块。这样就会多出来一个名叫 Capture 的音频控制,可以用它调节内置麦克风的音量。例如,对于 snd-hda-intel 模块,配置如下:<br />
<br />
options snd-hda-intel enable_msi=1<br />
<br />
然后,输入下面的命令重载模块,调整音量并测试。<br />
<br />
{{bc|# rmmod snd-hda-intel && modprobe snd-hda-intel}}<br />
<br />
===Intel 板载声卡无声===<br />
可能由两个相互冲突的驱动模块引起,分别是 {{ic|snd_intel8x0}} 和 {{ic|snd_intel8x0m}}。如果是的话,屏蔽 snd_intel8x0m 即可:<br />
<br />
{{hc|/etc/modprobe.d/modprobe.conf|blacklist snd_intel8x0m}}<br />
<br />
以下方法可能也有效:在 {{ic|alsamixer}} 或 {{ic|amixer}} 把 “External Amplifier” 设置为静音。参见:[http://alsa.opensrc.org/Intel8x0#Dell_Inspiron_8600_.28and_probably_others.29 ALSA wiki 相关条目]。<br />
<br />
也可以试试解除“Mix”的静音。<br />
<br />
===Intel 板载声卡耳机无声===<br />
对于笔记本电脑上的 '''Intel Corporation 82801 I (ICH9 Family) HD Audio Controller''' 声卡,可能需要添加如下内容到 modprobe 配置文件或 sound.conf,以解决耳机无声的问题:<br />
<br />
options snd-hda-intel model=$model<br />
<br />
$model 在下列型号中选择:<br />
<br />
* dell-vostro<br />
* olpc-xo-1_5<br />
* laptop<br />
* dell-m6<br />
* laptop-hpsense<br />
<br />
{{注意|可能需要在配置文件的 "alias" 语句之后添加上述内容。}}<br />
<br />
可以在内核文档中找到所有支持的型号,如[http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.35.y.git;a=blob;f=Documentation/sound/alsa/HD-Audio-Models.txt;h=dc25bb84b83b49665a7ed850e7bf5423d50cd3ba;hb=HEAD 该文]。这是 2.6.35 版本内核的文档,你需要自己查找适用于自己内核版本的文档。<br />
<br />
也可以从[http://www.mjmwired.net/kernel/Documentation/sound/alsa/HD-Audio-Models.txt 这里]获取型号列表。要查看你的声卡芯片名称,输入下列命令(其中的“*”请根据实际情况替换)。注意,有些芯片名称被更改过,因此实际名称与文件中的可能不匹配。<br />
<br />
cat /proc/asound/card*/codec* | grep Codec<br />
<br />
注意,这么做很可能导致所有输入设备(内置和外置麦克风)无法工作,即要么让麦克风正常、要么让耳机正常。如果遇到这一问题,请向 ALSA 开发者报告。<br />
<br />
另外,如果蜂鸣器(pcspkr)出现问题,可以试试使用以下配置修复:<br />
<br />
options snd-hda-intel model=$model enable=1 index=0<br />
<br />
===安装支持 S/PDIF 输出的显卡后无声 ===<br />
检测已加载的相关模块,并记录顺序:<br />
$ cat /proc/asound/modules<br />
0 snd_hda_intel<br />
1 snd_ca0106<br />
<br />
在 {{ic|/etc/modprobe.d/modprobe.conf}} 中禁用不希望使用的显卡的音频解码模块:<br />
# /etc/modprobe.d/modprobe.conf<br />
#<br />
install snd_hda_intel /bin/false<br />
<br />
如果不同设备使用同一模块,那么只能在 BIOS 中禁用其中之一。<br />
<br />
===Poor sound quality or clipping===<br />
If you experience poor sound quality, try setting the PCM volume (in alsamixer) to a level such that gain is 0.<br />
<br />
If snd-usb-audio driver has been loaded, you could try to enable {{ic|softvol}} in '''/etc/asound.conf''' file. Example configuration for the first audio device:<br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.dsnooper {<br />
type dsnoop<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
channels 2<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.softvol {<br />
type softvol<br />
slave { pcm "dmixer" }<br />
control {<br />
name "Master"<br />
card 0<br />
}<br />
}<br />
ctl.!default {<br />
type hw<br />
card 0<br />
}<br />
ctl.softvol {<br />
type hw<br />
card 0<br />
}<br />
ctl.dmixer {<br />
type hw<br />
card 0<br />
}<br />
<br />
===Pops When Starting and Stopping Playback===<br />
Some modules (e.g. snd_ac97_codec and snd_hda_intel) can power off your sound card when not in use. This can make an audible noise (like a crack/pop/scratch) when turning on/off your sound card. Sometimes even when move the slider volume, or open and close windows (KDE4). If you find this annoying try {{ic|modinfo snd_MY_MODULE}}, and look for a module option that adjusts or disables this feature.<br />
<br />
Example: to disable the power saving mode and solve cracking sound trough speakers issue, using snd_hda_intel add in {{ic|/etc/modprobe.d/modprobe.conf}}<br />
options snd_hda_intel power_save=0<br />
or<br />
options snd_hda_intel power_save=0 power_save_controller=N<br />
<br />
You can also try it with {{ic|1=modprobe snd_hda_intel power_save=0}} before.<br />
<br />
You may also have to unmute the 'Line' ALSA channel for this to work. Any value will do (other than '0' or something too high).<br />
<br />
''Example:'' on an onboard VIA VT1708S (using the snd_hda_intel module) these cracks occured even though 'power_save' was set to 0. Unmuting the 'Line' channel and setting a value of '1' solved the issue.<br />
<br />
Source: https://www.kernel.org/doc/Documentation/sound/alsa/powersave.txt<br />
<br />
If you use a laptop, pm-utils will change {{ic|power_save}} back to 1 when you go onto battery power even if you disable power saving in {{ic|/etc/modprobe.d}}. Disable this for pm-utils by disabling the script that makes the change (see [https://wiki.archlinux.org/index.php/Pm-utils#Disabling_a_hook Disabling a hook] for more information):<br />
# touch /etc/pm/power.d/intel-audio-powersave<br />
<br />
===S/PDIF Output Does Not Work===<br />
<br />
If the optical/coaxial digital output of your motherboard/sound card is not working or stopped working, and have already enabled and unmuted it in alsamixer, try running<br />
# iecset audio on<br />
<br />
as root.<br />
<br />
You can also put this command in {{Ic|rc.local}} as it sometimes it may stop working after a reboot.<br />
<br />
===HDMI Output Does Not Work===<br />
If the HDMI output of your motherboard/sound card is not working or stopped working, and have already enabled and unmuted it in alsamixer, try the following.<br />
<br />
Query for Playback Devices:<br />
<br />
$ aplay -l<br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: NVidia [HDA NVidia], device 0: ALC1200 Analog [ALC1200 Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 1: ALC1200 Digital [ALC1200 Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 3: NVIDIA HDMI [NVIDIA HDMI]<br />
Subdevices: 0/1<br />
Subdevice #0: subdevice #0<br />
<br />
Now that we have the info for the HDMI device, try a test, In the example below, 0 is the card number and 3 is the device number.<br />
<br />
$ aplay -D plughw:0,3 /usr/share/sounds/alsa/Front_Center.wav<br />
<br />
If aplay does not output any errors, but still no sound is heared, "reboot" the receiver, monitor or tv set. Since the HDMI interface executes a handshake on connection, it might have noticed before that there was no audio stream embedded, and disabled audio decoding. In particular, if you are using a standalone window manager (don’t know about Gnome or KDE), you may need to have some sound playing ''while'' plugging in the HDMI cable.<br />
<br />
{{Note|If you are using an ATI card and linux 3.0, check your kernel/grub settings, you most likely need to use the ''''radeon.audio'''<nowiki>=</nowiki>'''1'''' boot parameter, or alternatively set up the option in your modprobe configuration files. It's important to note that before trying, is to check the [http://www.x.org/wiki/RadeonFeature#fndef-72849d6d6eb3927d486f50ece74a739042965a6c-25 Radeon Feature Matrix] to see if it's actually supported. At the moment this is written (march 29th, 2013), some chipsets of the [http://www.x.org/wiki/RadeonFeature#Decoder_ring_for_engineering_vs_marketing_names S. Islands] (namely HD7750 through HD7970) doesn't (still) support HDMI Audio.}}<br />
<br />
If the test is successful, create or edit your ~/.asoundrc file to set HDMI as the default audio device.<br />
<br />
{{hc|~/.asoundrc|<br />
pcm.!default {<br />
type hw<br />
card 0<br />
device 3<br />
}<br />
}}<br />
<br />
Or you above config does not work try:<br />
{{hc|~/.asoundrc|<br />
defaults.pcm.card 0<br />
defaults.pcm.device 3<br />
defaults.ctl.card 0<br />
}}<br />
<br />
===HDMI Multi-channel PCM output does not work (Intel) ===<br />
As of Linux 3.1 multi-channel PCM output through HDMI with a Intel card (Intel Eaglelake, IbexPeak/Ironlake,SandyBridge/CougarPoint and IvyBridge/PantherPoint) is not yet supported. Support for it has been recently added and expected to be available in Linux 3.2. To make it work in Linux 3.1 you need to apply the following patches:<br />
<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=76adaa34db407f174dd06370cb60f6029c33b465 drm: support routines for HDMI/DP ELD]<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=e0dac65ed45e72fe34cc7ccc76de0ba220bd38bb drm/i915: pass ELD to HDMI/DP audio driver]<br />
<br />
===HP TX2500===<br />
Add these 2 lines into {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-cmipci mpu_port=0x330 fm_port=0x388<br />
options snd-hda-intel index=0 model=toshiba position_fix=1<br />
<br />
options snd-hda-intel model=hp (works for tx2000cto)<br />
<br />
===Skipping Sound When Playing MP3===<br />
If you have sound skipping when playing MP3 files and you have more then 2 speakers attached to your computer (i.e. > 2 speaker system), run alsamixer and disable the channels for the speakers that you '''DO NOT''' have (i.e. do not enable the sound for the center speaker if you do not have a center speaker.<br />
<br />
===Using a USB Headset and External USB Sound Cards===<br />
If you are using a USB headset with ALSA you can try using {{AUR|asoundconf}} (currently only available from the [[AUR]]) to set the headset as the primary sound output. Before running make sure you have usb audio module enabled ({{ic|modprobe snd-usb-audio}}).<br />
<br />
# asoundconf is-active<br />
# asoundconf list<br />
# asoundconf set-default-card <chosen soundcard><br />
<br />
==== Crackling sound with USB devices ====<br />
If you experience crackling sound on USB devices, you can try tuning the snd-usb-audio for minimal latency.<br />
<br />
Add this to your {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
options snd-usb-audio nrpacks=1<br />
<br />
source: http://alsa.opensrc.org/Usb-audio#Tuning_USB_devices_for_minimal_latencies<br />
<br />
==== Hot-plugging a USB Sound Card ====<br />
In order to automatically make a USB Sound Card the primary output device, when the card is plugged in, you can use the following udev rules (e.g. add the following two lines to {{ic|/etc/udev/rules.d/00-local.rules}} and reboot).<br />
<br />
{{bc|1=KERNEL=="pcmC[D0-9cp]*", ACTION=="add", PROGRAM="/bin/sh -c 'K=%k; K=$${K#pcmC}; K=$${K%%D*}; echo defaults.ctl.card $$K > /etc/asound.conf; echo defaults.pcm.card $$K >>/etc/asound.conf'"<br />
KERNEL=="pcmC[D0-9cp]*", ACTION=="remove", PROGRAM="/bin/sh -c 'echo defaults.ctl.card 0 > /etc/asound.conf; echo defaults.pcm.card 0 >>/etc/asound.conf'"}}<br />
<br />
===Error 'Unknown hardware' Appears After a Kernel Update===<br />
The following messages may be displayed during the start-up ALSA after the kernel update:<br />
Unknown hardware "foo" "bar" ...<br />
Hardware is initialized using a guess method<br />
/usr/sbin/alsactl: set_control:nnnn:failed to obtain info for control #mm (No such file or directory)<br />
<br />
or<br />
Found hardware: "HDA-Intel" "VIA VT1705" "HDA:11064397,18490397,00100000" "0x1849" "0x0397"<br />
Hardware is initialized using a generic method<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #1 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #2 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #25 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #26 (No such file or directory)<br />
<br />
Simply store ALSA mixer settings again (as root):<br />
# alsactl -f /var/lib/alsa/asound.state store<br />
<br />
It may be necessary configure ALSA again with alsamixer<br />
<br />
===HDA Analyzer===<br />
If the mappings to your audio pins(plugs) do not correspond but ALSA works fine, you could try HDA Analyzer -- a pyGTK2 GUI for HD-audio control can be found [http://www.alsa-project.org/main/index.php/HDA_Analyzer at the ALSA wiki].<br />
Try tweaking the Widget Control section of the PIN nodes, to make microphones IN and headphone jacks OUT. Referring to the Config Defaults heading is a good idea.<br />
<br />
NOTE: the script is done by such way that it is incompatible with python3 (which is now shipped with ArchLinux) but tries to use it.<br />
The workaround is: open "run.py", find all occurences of "python" (2 occurences - one on the first line, and the second on the last line) and replace them all by "python2".<br />
<br />
NOTE2: the script requires root acces, but running it via su/sudo is bogus. Run it via {{ic|kdesu}} or {{ic|gksu}}.<br />
<br />
===ALSA with SDL===<br />
If you get no sound via SDL and ALSA cannot be chosen from the application. Try setting the environmental variable SDL_AUDIODRIVER to alsa.<br />
# export SDL_AUDIODRIVER=alsa<br />
<br />
===Low Sound Workaround===<br />
<br />
If you are facing low sound even after maxing out your speakers/headphones, you can give the softvol plugin a try. Add the following to {{ic|/etc/asound.conf}}.<br />
{{bc|<nowiki><br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
<br />
pcm.softvol {<br />
type softvol<br />
slave {<br />
pcm "dmix"<br />
}<br />
control {<br />
name "Pre-Amp"<br />
card 0<br />
}<br />
min_dB -5.0<br />
max_dB 20.0<br />
resolution 6<br />
}<br />
</nowiki>}}<br />
{{note| You will probably have to restart the computer, as restarting the alsa daemon did not load the new configuration for me. Also, if the configuration does not work even after restarting, try changing {{ic|plug}} with {{ic|hw}} in the above configuration.}}<br />
<br />
After the changes are loaded successfully, you will see a {{ic|Pre-Amp}} section in alsamixer. You can adjust the levels there.<br />
{{note|Setting a high value for {{ic|Pre-Amp}} can cause sound distortion, so adjust it according to the level that suits you.}}<br />
<br />
===Popping sound after resuming from suspension===<br />
You might hear a popping sound after resuming the computer from suspension. This can be fixed by editing {{ic|/etc/pm/sleep.d/90alsa}} and removing the line that says {{ic|aplay -d 1 /dev/zero}}<br />
<br />
==设置文件范例==<br />
参见:[[Advanced Linux Sound Architecture/Example Configurations]]。<br />
<br />
==相关阅读==<br />
* [http://www.mjmwired.net/kernel/Documentation/sound/alsa/ALSA-Configuration.txt ALSA 模块高级配置]<br />
* [http://alsa.opensrc.org/Main_Page 非官方的 ALSA Wiki]<br />
* [https://bbs.archlinux.org/viewtopic.php?id=36815 如何从 svn 获取驱动代码并编译]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Advanced_Linux_Sound_Architecture_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=256308
Advanced Linux Sound Architecture (简体中文)
2013-05-07T12:07:53Z
<p>Cuihao: /* No sound when S/PDIF video card is installed */</p>
<hr />
<div>[[Category:简体中文]]<br />
[[Category:Sound (简体中文)]]<br />
[[Category:Audio/Video (简体中文)]]<br />
[[cs:Advanced Linux Sound Architecture]]<br />
[[de:Alsa]]<br />
[[es:Advanced Linux Sound Architecture]]<br />
[[fa:ALSA]]<br />
[[fr:Alsa]]<br />
[[he:Advanced Linux Sound Architecture]]<br />
[[it:Advanced Linux Sound Architecture]]<br />
[[ja:Advanced Linux Sound Architecture]]<br />
[[nl:Advanced Linux Sound Architecture]]<br />
[[pt:Advanced Linux Sound Architecture]]<br />
[[ru:Advanced Linux Sound Architecture]]<br />
[[sk:Advanced Linux Sound Architecture]]<br />
[[th:Advanced Linux Sound Architecture]]<br />
[[tr:Gelişmiş_ses_mimarisi_(ALSA)]]<br />
[[en:Advanced Linux Sound Architecture]]<br />
<br />
{{Article summary start}}<br />
{{Article summary text|ALSA 的使用、配置及疑难解答。}}<br />
{{Article summary heading|相关页面}}<br />
{{Article summary wiki|Sound system (简体中文)}}<br />
{{Article summary wiki|Disable PC Speaker Beep (简体中文)}}<br />
{{Article summary end}}<br />
{{TranslationStatus (简体中文)|Advanced_Linux_Sound_Architecture|2013-04-25|254584}}<br />
<br />
[[Wikipedia:zh:ALSA|高级 Linux 声音体系]](Advanced Linux Sound Architecture,'''ALSA''')是Linux中提供声音设备驱动的内核组件,用来代替原来的开放声音系统(Open Sound System,OSSv3)。除了声音设备驱动,'''ALSA'''还包含一个用户空间的函数库,以方便开发者通过高级API使用驱动功能,而不必直接与内核驱动交互。<br />
<br />
{{注意|关于另一种声音体系,请阅读[[Open Sound System (简体中文)|开放声音系统(OSS)]]的页面。}}<br />
<br />
==安装==<br />
Arch 默认的内核已经通过一套模块提供了 ALSA,不必特别安装。<br />
<br />
[[udev (简体中文)|udev]]会在系统启动时自动检测硬件,并加载相应的声音设备驱动模块。这时,你的声卡已经可以工作了,只是所有声道默认都被设置成静音了。<br />
<br />
在本地登录(通过虚拟终端或登录管理器)的用户,都有权限播放音频并调整音量。要让远程登录的用户拥有这些权限,必须把用户[[Users and Groups (简体中文)#用户组管理|加入]] {{ic|audio}} 用户组。该组的成员可以直接访问声音设备,会导致某些程序独占音频输出(破坏软件混音),还可能影响用户快速切换和拖机(multiseat)。因此,除非真的有某些[https://wiki.ubuntu.com/Audio/TheAudioGroup 特殊需求],'''不建议'''把用户加入 {{ic|audio}} 用户组。<br />
<br />
===用户空间工具===<br />
从 [[Official Repositories (简体中文)|官方仓库]]:<br />
* [[pacman (简体中文)|安装]] {{Pkg|alsa-utils}} 软件包,其中的 {{ic|alsamixer}} 工具允许用户在控制台或终端中配置声音设备。<br />
* 安装 {{Pkg|alsa-oss}} 软件包,如果想让 OSS 应用程序与dmix(软件混音)协同工作。 <br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。}}<br />
*安装 {{Pkg|alsa-plugins}} 软件包,如果需要 [[#高质量重采样|高质量重采样]]、[[#Upmixing/Downmixing|软件模拟环绕立体声]]和其他高级特性。<br />
<br />
==解除各声道的静音==<br />
目前版本的 ALSA 安装后,所有声道'''默认是静音的''',必须手动解除。<br />
<br />
使用 {{ic|alsamixer}} 的 ncurses 界面,配置十分简单:<br />
<br />
$ alsamixer<br />
<br />
此外,还可以在命令行下使用 {{ic|amixer}}:<br />
<br />
$ amixer sset Master unmute<br />
<br />
在 {{ic|alsamixer}} 中,下方标有 {{ic|MM}} 的声道是静音的,而标有 {{ic|00}} 的通道已经启用。<br />
<br />
使用左右方向键,选中 {{ic|Master}} 和 {{ic|PCM}} 声道。按下 {{keypress|m}} 键解除静音。使用上方向键增加音量,直到增益值为{{ic|0}}。该值显示在左上方 {{ic|Item:}} 字段后。过高的增益值会导致声音失真。<br />
<br />
要想得到完整的 5.1 或 7.1 环绕立体声,还得解除 Front、Surround、Center、LFE (subwoofer) 和 Side 这些声道的静音(上述名称是 Intel HD Audio 声卡使用的声道名,可能因设备不同而有所差异)。注意,仅有这些设置,系统不会自动将立体声源(多数音乐)提升(upmix)成环绕立体声。如果需要这些功能,请阅读[[#Upmixing/Downmixing]]。<br />
<br />
要启用麦克风,切换至 Capture 选项卡,按下 {{keypress|F4}},按下 {{keypress|空格}} 启用其中一个声道即可。<br />
<br />
按下 {{Keypress|Esc}} 键退出 alsamixer。<br />
<br />
{{注意|<br />
* 有一些声卡,需要关闭数字输出或将其调成静音,才能输出模拟音频信号。对于 Soundblaster Audigy LS 声卡,需要把 IEC958 通道设成静音。 <br />
* 有些机器(如 Thinkpad T61),有一个 Speaker 通道,必须按照上述方法打开调整。<br />
* 有些机器(如 Dell E6400),可能还需要把 {{ic|Front}} 和 {{ic|Headphone}} 声道打开调整。}}<br />
<br />
接下来,测试声卡是否工作:<br />
<br />
$ speaker-test -c 2<br />
<br />
根据扬声器的实际情况,调整 -c 后的数值。对于 7.1 声道,这个数字是 8:<br />
<br />
$ speaker-test -c 8<br />
<br />
如果没有正常工作,请继续阅读 [[#配置]] 以及 [[#疑难解答]] 部分。<br />
<br />
{{pkg|alsa-utils}} 软件包提供了两个配置 alsa 的服务:{{ic|alsa-restore.service}} 和 {{ic|alsa-store.service}}。它们分别在开机和关机时自动运行。<br />
<br />
==配置==<br />
=== VirtualBox 虚拟机无声音 ===<br />
<br />
如果使用 VirtualBox 时碰上了麻烦,请尝试以下命令:<br />
<br />
{{hc|$ alsactl init|2=<nowiki><br />
<br />
Found hardware: "ICH" "SigmaTel STAC9700,83,84" "AC97a:83847600" "0x8086" "0x0000"<br />
Hardware is initialized using a generic method<br />
</nowiki><br />
}}<br />
<br />
如果还是不行,请检查所使用的音频软件是否激活了 ALSA 输出。<br />
<br />
===设置默认声卡===<br />
<br />
如果发现开机时声卡次序会发生变化,可以在通过 {{ic|/etc/modprobe.d}} 中的 {{ic|.conf}} 文件(比如 {{ic|/etc/modprobe.d/alsa-base.conf}})手动设置次序。<br />
比如,要让 mia 声卡成为 #0、Intel HDA 声卡成为 #1:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<br />
options snd slots=snd_mia,snd_hda_intel<br />
options snd_mia index=0<br />
options snd_hda_intel index=1<br />
}}<br />
<br />
使用 {{ic|<nowiki>lsmod | grep snd</nowiki>}} 获取设备列表。<br />
<br />
snd_mia 和 snd_hda_intel 分别是两个声卡的内核驱动模块。该配置仅适用于同时装有 mia 声卡和使用 snd_hda_intel 驱动的声卡(通常是板载声卡)的情况。<br />
<br />
如果使用 -2 的 index 值,ALSA 就不会将对应的设备作为主声卡使用。Linux Mint 和 Ubuntu 等发行版使用了以下配置,避免 USB 声卡和其他“非主流”设备变成 index 为 0 的主声卡:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<br />
options bt87x index=-2<br />
options cx88_alsa index=-2<br />
options saa7134-alsa index=-2<br />
options snd-atiixp-modem index=-2<br />
options snd-intel8x0m index=-2<br />
options snd-via82xx-modem index=-2<br />
options snd-usb-audio index=-2<br />
options snd-usb-caiaq index=-2<br />
options snd-usb-ua101 index=-2<br />
options snd-usb-us122l index=-2<br />
options snd-usb-usx2y index=-2<br />
# Keep snd-pcsp from being loaded as first soundcard<br />
options snd-pcsp index=-2<br />
# Keep snd-usb-audio from beeing loaded as first soundcard<br />
options snd-usb-audio index=-2<br />
}}<br />
<br />
以上配置会在系统重启后生效。<br />
<br />
{{Out of date|据英文 Wiki 用户反应,{{ic|/etc/asound.conf}} 这个配置文件默认是不存在的,下列操作可能有误。}}<br />
<br />
首先,运行 {{ic|aplay -l}},获取声卡的声卡ID和设备ID:<br />
<br />
{{hc|$ aplay -l|2=<nowiki><br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: Intel [HDA Intel], device 0: CONEXANT Analog [CONEXANT Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: Intel [HDA Intel], device 1: Conexant Digital [Conexant Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 1: JamLab [JamLab], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 2: Audio [Altec Lansing XT1 - USB Audio], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
</nowiki>}}<br />
<br />
拿输出中的最后一项来说,这个声卡的声卡ID为2,设备ID为0。若要把这块声卡作为默认声卡,可以把下列配置添加到系统级别的 {{ic|/etc/asound.conf}} 或用户级别的 {{ic|~/.asoundrc}} 文件。如果文件不存在,可以手动创建。其中的各个ID,请根据实际情况调整:<br />
{{bc|<br />
defaults.pcm.card 2<br />
defaults.pcm.device 0<br />
defaults.ctl.card 2<br />
}}<br />
“pcm”选项决定用来播放音频的设备,而“ctl”选项决定那个声卡能够由控制工具(如 alsamixer)使用。<br />
<br />
上述配置在重启音频程序(如 mplayer)后立即生效。<br />
<br />
===确认所有声音模块都已经加载===<br />
<br />
一般 udev 都会自动识别出声卡。使用以下命令确认:<br />
<br />
{{hc|<nowiki>$ lsmod | grep '^snd' | column -t</nowiki>|<nowiki><br />
snd_hda_codec_hdmi 22378 4<br />
snd_hda_codec_realtek 294191 1<br />
snd_hda_intel 21738 1<br />
snd_hda_codec 73739 3 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel<br />
snd_hwdep 6134 1 snd_hda_codec<br />
snd_pcm 71032 3 snd_hda_codec_hdmi,snd_hda_intel,snd_hda_codec<br />
snd_timer 18992 1 snd_pcm<br />
snd 55132 9 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel,snd_hda_codec,snd_hwdep,snd_pcm,snd_timer<br />
snd_page_alloc 7017 2 snd_hda_intel,snd_pcm<br />
</nowiki>}}<br />
<br />
如果你的输出和上面类似,那就说明声卡已经被正确识别。<br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。如有需要,请[[Kernel_modules_(简体中文)#装入|手动加载]]。}}<br />
<br />
还可以检查一下 {{ic|/dev/snd/}} 目录,看看是否有这些设备文件:<br />
<br />
{{hc|$ ls -l /dev/snd|2=<nowiki><br />
total 0<br />
crw-rw---- 1 root audio 116, 0 Apr 8 14:17 controlC0<br />
crw-rw---- 1 root audio 116, 32 Apr 8 14:17 controlC1<br />
crw-rw---- 1 root audio 116, 24 Apr 8 14:17 pcmC0D0c<br />
crw-rw---- 1 root audio 116, 16 Apr 8 14:17 pcmC0D0p<br />
crw-rw---- 1 root audio 116, 25 Apr 8 14:17 pcmC0D1c<br />
crw-rw---- 1 root audio 116, 56 Apr 8 14:17 pcmC1D0c<br />
crw-rw---- 1 root audio 116, 48 Apr 8 14:17 pcmC1D0p<br />
crw-rw---- 1 root audio 116, 1 Apr 8 14:17 seq<br />
crw-rw---- 1 root audio 116, 33 Apr 8 14:17 timer<br />
</nowiki>}}<br />
<br />
{{注意|在 IRC 或论坛寻求这方面帮助时,别忘了贴出上面几个命令的输出。}}<br />
<br />
如果你的输出跟上面类似,或至少有 '''controlC0''' 和 '''pcmC0D0p''',那么声卡就已经正常加载了。<br />
<br />
如果出现问题,声卡模块没有正确加载,那么请尝试手动加载模块:<br />
<br />
* 确定声卡对应的驱动模块:[http://www.alsa-project.org/main/index.php/Matrix:Main ALSA Soundcard Matrix]。这些模块都会有一个”snd-“前缀(例如:{{ic|snd-via82xx}})。<br />
* [[Kernel_modules_(简体中文)#装入|加载模块]]。<br />
* 检查 {{ic|/dev/snd}} 目录中的设备文件(参见上文);或者,检查 {{ic|alsamixer}} 或 {{ic|amixer}} 的输出是否正确。<br />
* 修改配置,使 {{ic|snd-<模块名>}} 和 {{ic|snd-pcm-oss}} 模块[[Kernel_modules_(简体中文)#装入|开机自动加载]]。<br />
<br />
===启用 SPDIF 输出===<br />
<br />
(由 Gentoo 论坛的 gralves 提供)<br />
* 在 GNOME 音量控制的选项(Options)标签中,把 IEC958 改成 PCM。该选项可以在首选项中启用。<br />
* 不使用 GNOME 音量控制工具的话:<br />
** 编辑 {{ic|/var/lib/alsa/asound.state}}。该文件是 alsasound 保存混音器设置的地方。<br />
** 找到这一行:“IEC958 Playback Switch”。该行上下应该有一行内容为 {{ic|value:false}},将其改为 {{ic|value:true}}。<br />
** 再找到这一行:”IEC958 Playback AC97-SPSA“。把对应的值改为 0。<br />
** 重启 ALSA。<br />
<br />
{{Out of date|rc.local 在 systemd 中已经弃用,以下方法可能不再适用。}}<br />
<br />
还有另外一个开机自动启用 SPDIF 输出的方法(在 SoundBlaster Audigy 声卡上测试过):<br />
<br />
* 在 {{ic|/etc/rc.local}} 中添加如下内容:<br />
# Use COAX-digital output<br />
amixer set 'IEC958 Optical' 100 unmute<br />
amixer set 'Audigy Analog/Digital Output Jack' on<br />
<br />
使用以下命令,获取你的声卡的数字输出的名称:<br />
$ amixer scontrols<br />
<br />
===系统级均衡器===<br />
====使用 AlsaEqual(包含界面)====<br />
从 [[AUR (简体中文)|AUR]] 安装 {{AUR|alsaequal}}。<br />
<br />
{{注意|如果使用的是64位系统,而又安装了32位的 Flash 插件,这里的设置会导致 Flash 无声。如果发生了这种情况,请禁用 alsaequal 或编译32位版本的 alsaequal。}}<br />
<br />
安装后,把下列内容添加到 ALSA 配置文件({{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}}):<br />
<br />
ctl.equal {<br />
type equal;<br />
}<br />
<br />
pcm.plugequal {<br />
type equal;<br />
# Modify the line below if you do not<br />
#slave.pcm "plughw:0,0";<br />
#by default we want to play from more sources at time:<br />
slave.pcm "plug:dmix";<br />
}<br />
#pcm.equal {<br />
# If you do not want the equalizer to be your<br />
# default soundcard comment the following<br />
# line and uncomment the above line. (You can<br />
# choose it as the output device by addressing<br />
# it with specific apps,eg mpg123 -a equal 06.Back_In_Black.mp3)<br />
pcm.!default {<br />
type plug;<br />
slave.pcm plugequal;<br />
}<br />
<br />
设置好后,切换均衡器:<br />
$ alsamixer -D equal<br />
<br />
需要注意,每个用户的 alsaequal 配置文件都是独立的,位于 {{ic|~/.alsaequal.bin}}。因此,如果想在 [[mpd]] 之类的以独立用户身份运行的程序中使用 AlsaEqual,就得像下面这样:<br />
# su mpd -c 'alsamixer -D equal'<br />
或者,在其他用户的主目录下创建符号链接,指向你的 {{ic|.alsaequal.bin}}……<br />
<br />
=====管理 AlsaEqual 配置=====<br />
从 [http://xyne.archlinux.ca/repos/ Xyne 的软件仓库] 或 [https://aur.archlinux.org/packages.php?ID=62420 AUR] 安装 [http://xyne.archlinux.ca/projects/alsaequal-mgr/ alsaequal-mgr]。<br />
<br />
同上,配置均衡器: <br />
$alsamixer -D equal<br />
<br />
如果对效果感到满意,将其命名(如“foo”)并保存该配置:<br />
$alsaequal-mgr save foo<br />
<br />
重新加载“foo”中的配置:<br />
$alsaequal-mgr load foo<br />
<br />
这样,就可以为游戏、电影、不同音乐流派、网络电话等等创建不同的均衡器配置,以便随时切换。<br />
<br />
参见[http://xyne.archlinux.ca/projects/alsaequal-mgr/ 项目主页]和程序的帮助信息,了解更多设置。<br />
<br />
====使用 mbeq====<br />
{{注意|该软件用到了一个 ladspa 插件,可能导致播放音频时 CPU 占用率偏高。In addition, this was made with stereophonic sound (e.g. headphones) in mind.}}<br />
<br />
安装 {{Pkg|alsa-plugins}}、{{Pkg|ladspa}} 和 {{Pkg|swh-plugins}} 软件包。<br />
<br />
* 在 {{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}} 中添加如下内容:<br />
<br />
{{hc|/etc/asound.conf|2=<nowiki>pcm.eq {<br />
type ladspa<br />
<br />
# The output from the EQ can either go direct to a hardware device<br />
# (if you have a hardware mixer, e.g. SBLive/Audigy) or it can go<br />
# to the software mixer shown here.<br />
#slave.pcm "plughw:0,0"<br />
slave.pcm "plug:dmix"<br />
<br />
# Sometimes you may need to specify the path to the plugins,<br />
# especially if you have just installed them. Once you have logged<br />
# out/restarted this should not be necessary, but if you get errors<br />
# about being unable to find plugins, try uncommenting this.<br />
#path "/usr/lib/ladspa"<br />
<br />
plugins [<br />
{<br />
label mbeq<br />
id 1197<br />
input {<br />
#this setting is here by example, edit to your own taste<br />
#bands: 50hz, 100hz, 156hz, 220hz, 311hz, 440hz, 622hz, 880hz, 1250hz, 1750hz, 25000hz,<br />
#50000hz, 10000hz, 20000hz<br />
controls [ -5 -5 -5 -5 -5 -10 -20 -15 -10 -10 -10 -10 -10 -3 -2 ]<br />
}<br />
}<br />
]<br />
}<br />
<br />
# Redirect the default device to go via the EQ - you may want to do<br />
# this last, once you are sure everything is working. Otherwise all<br />
# your audio programs will break/crash if something has gone wrong.<br />
<br />
pcm.!default {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
<br />
# Redirect the OSS emulation through the EQ too (when programs are running through "aoss")<br />
<br />
pcm.dsp0 {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
</nowiki>}}<br />
<br />
* 完成。若有问题,欢迎前往论坛咨询。<br />
<br />
==高质量重采样==<br />
启用软件混音时,ALSA 会强制把所有音频重采样到相同的频率(如果系统支持,默认是 48000)。dmix 的重采样算法很糟糕,会导致显著的质量损失。<br />
<br />
安装 {{pkg|alsa-plugins}} 和 {{pkg|libsamplerate}}。<br />
<br />
将默认的采样率转换器更改为 libsamplerate:<br />
<br />
{{hc|/etc/asound.conf|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
或:<br />
<br />
{{hc|~/.asoundrc|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
'''samplerate_best''' 输出的声音质量最佳,但需要耗费大量CPU资源进行实时重采样。还有其他的算法可供选择(如 '''samplerate''' 等),但比起默认的算法,这些算法的改进并不明显。<br />
<br />
{{警告|某些系统中,启用 samplerate_best 可能导致 Flash 播放器无声音。}}<br />
<br />
==Upmixing/Downmixing==<br />
===Upmixing===<br />
In order for stereo sources like music to be able to saturate a 5.1 or 7.1 sound system, you need to use upmixing. In darker days this used to be tricky and error prone but nowadays plugins exist to easily take care of this task. Thus, install {{Pkg|alsa-plugins}}.<br />
<br />
Then add the following to your ALSA configuration file of choice (either {{ic|/etc/asound.conf}} or {{ic|~/.asoundrc}}):<br />
pcm.upmix71 {<br />
type upmix<br />
slave.pcm "surround71"<br />
delay 15<br />
channels 8<br />
}<br />
<br />
You can easily change this example for 7.1 upmixing to 5.1 or 4.0.<br />
<br />
This adds a new pcm that you can use for upmixing. If you want all sound sources to go through this pcm, add it as a default below the previous definition like so:<br />
<br />
pcm.!default "plug:upmix71"<br />
<br />
The plugin automatically allows multiple sources to play through it without problems so setting is as a default is actually a safe choice. <br />
If this is not working, you have to setup your own dmixer for the upmixing PCM like this: <br />
pcm.dmix6 {<br />
type asym<br />
playback.pcm {<br />
type dmix<br />
ipc_key 567829<br />
slave {<br />
pcm "hw:0,0"<br />
channels 6<br />
}<br />
}<br />
}<br />
<br />
and use "dmix6" instead of "surround71". <br />
If you experience skipping or distorted sound, consider increasing the buffer_size (to 32768, for example) or use a [[#高质量重采样|high quality resampler]].<br />
<br />
===Downmixing===<br />
If you want to downmix sources to stereo because you, for instance, want to watch a movie with 5.1 sound on a stereo system, you need to use the vdownmix plugin that alsa provides in the same package.<br />
<br />
Again, in your configuration file, add this:<br />
<br />
pcm.!surround51 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
pcm.!surround40 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
<br />
==混音==<br />
===软件混音(dmix)===<br />
<br />
{{注意|在 ALSA 1.0.9rc2 及以上版本中,如果声卡不支持硬件混音,模拟输出将默认使用 dmix 混音,无需额外设置。}}<br />
<br />
如果软件混音未正常启用,那么可以手动创建 {{ic|~/.asoundrc}},加入以下内容:<br />
<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
<br />
这样就启用了软件混音,多个程序能够同时使用声卡。<br />
<br />
至于 S/PDIF 之类的数字音频输出,目前的 ALSA 还不会默认启用 dmix。因此,必须像上面这样手动启用。<br />
<br />
常见问题的解答,参见[[#疑难解答]]部分。<br />
<br />
===硬件混音===<br />
====支持====<br />
如果声卡支持,一般无需任何配置即可启用硬件混音。绝大多数板载声卡都不支持硬件混音,必须得使用软件混音(见上文)。很多独立声卡都支持硬件混音,Linux 下支持较好的有:<br />
<br />
* Creative SoundBlaster Live! (5.1 型号)<br />
* Creative SoundBlaster Audigy (部分型号)<br />
* Creative SoundBlaster Audidy 2 (ZS 系列型号)<br />
* Creative SoundBlaster Audigy 4 (Pro 系列型号)<br />
<br />
{{注意|上述声卡的低端版本(Audigy SE、Audigy 2 NX、SoundBlaster Live! 24bit 以及 SoundBlaster Live! 7.1)使用了不同的芯片,'''不支持'''硬件混音。}}<br />
<br />
{{注意|板载 VIA8237 芯片支持4路硬件混音,但在某些主板上只能使用3路(第4路无声),甚至根本无法使用。即使能正常工作,质量也比不上其他混音方法。}}<br />
<br />
====问题修复====<br />
如果你的声卡是 Intel Corporation 82801I (ICH9 Family) HD Audio Controller (rev 02),且使用了64位系统,需要执行以下命令才能在 Enemy Territory 中听到声音:<br />
echo "et.x86 0 0 direct" > /proc/asound/card0/pcm0p/oss<br />
echo "et.x86 0 0 disable" > /proc/asound/card0/pcm0c/oss<br />
<br />
==疑难解答==<br />
===使用CPU频率动态调整时,音频发生跳跃===<br />
使用 {{ic|ondemand}} 或 {{ic|conservative}} 频率策略、启用CPU频率动态调整时,某些声卡和某些 ALSA 驱动会出现声音跳跃的情况。目前,唯一的解决方案是使用固定的频率,如 {{ic|performance}} 性能策略。<br />
<br />
详情参见 [[CPU Frequency Scaling (简体中文)]]。<br />
<br />
===同时只能有一个用户播放音频===<br />
有时会出现一个用户独占软件混音器的情况。多数情况下这没什么影响,但在使用 [[mpd]] 这样以独立用户身份运行的程序时,就会出现问题。当 mpd 运行时,用户自己就无法播放其他音频了。一种解决方法是,以正在使用的用户身份运行 mpd。另一种方法是,在 ALSA 配置文件的 {{ic|pcm.dmixer}} 部分添加 {{ic|ipc_key_add_uid 0}} 禁止独占。以下是 {{ic|asound.conf}} 示例的片段,省略的部分无需修改:<br />
<br />
...<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
slave {<br />
...<br />
<br />
===无法同时播放多个音频===<br />
如果安装了 [[PulseAudio (简体中文)|PulseAudio]](可能作为 [[GNOME (简体中文)|GNOME]] 的依赖被安装),可能出现无法同步播放的问题,因为 PulseAudio 的默认配置是“独占”声卡。ALSA 的用户可能对自己的配置已经很满意、不希望使用 [[PulseAudio (简体中文)|PulseAudio]]。解决方案是编辑 {{ic|/etc/asound.conf}} 并注释掉以下内容:<br />
# Use PulseAudio by default<br />
#pcm.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
# hint {<br />
# show on<br />
# description "Default ALSA Output (currently PulseAudio Sound Server)"<br />
# }<br />
#}<br />
<br />
可能还需要注释掉下面的内容:<br />
#ctl.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
#}<br />
<br />
比起完全卸载 [[PulseAudio (简体中文)|PulseAudio]],这大概是个更简单的方法。<br />
<br />
以下是 {{ic|/etc/asound.conf}} 配置文件的一个示例:<br />
{{bc|<nowiki>pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
}<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}</nowiki><br />
}}<br />
<br />
{{注意|本 {{ic|/etc/asound.conf}} 本来是配合 [[MPD]] 全局配置使用的。参见[[#同时只能有一个用户播放音频|该节]]关于多用户的内容。}}<br />
<br />
===有时候开机无声===<br />
如果会随机地出现开机无声的问题,原因可能是多声卡的顺序发生了变化。如果是这种情况,请参照[[#设置默认声卡]]进行设置。<br />
<br />
对于 mpd 用户,上述配置无效,请参考[http://mpd.wikia.com/wiki/Configuration#ALSA_MPD_software_volume_control 该文]设置。<br />
<br />
===特定程序的问题===<br />
对于不使用系统声音配置的程序,如 XMMS 和 Mplayer,必须特别设置以应用 ALSA 配置。<br />
<br />
对于 mplayer,将以下内容添加到 {{ic|~/.mplayer/config}} (或系统全局配置文件 {{ic|/etc/mplayer/mplayer.conf}}):<br />
ao=alsa<br />
<br />
对于 XMMS 和 Beep Media Player,打开选项窗口,要把声音驱动设置为 ALSA 而不是 OSS。<br />
<br />
XMMS 的设置方法:<br />
* 启动 XMMS<br />
** 选项 -> 首选项<br />
** 选择 Alsa 输出插件<br />
<br />
对于未提供 ALSA 输出的程序,可以使用 alsa-oss 软件包中的 aoss 工具启动。使用方法是:在要运行的程序的命令行前加上 {{ic|aoss}}。例如:<br />
aoss realplay<br />
<br />
pcm.!default{ ... } 貌似已经不管用了。但下面的设置有效:<br />
pcm.default pcm.dmixer<br />
<br />
===特定型号的配置===<br />
ALSA 会通过 BIOS 检测[http://www.kernel.org/doc/Documentation/sound/alsa/HD-Audio-Models.txt 声卡型号],少数情况下也可能无法正确识别。声卡芯片类型可以在 {{ic|alsamixer}} 界面上找到(如 ALC662),而型号则要设置在 {{ic|/etc/modprobe.d/modprobe.conf}} 或 {{ic|/etc/modprobe.d/sound.conf}} 文件中:<br />
<br />
options snd-hda-intel model=<型号><br />
<br />
具体设置因声卡型号而异。多数情况下不需要特地这样做。如果想知道具体声卡型号的设置,请查阅 [http://bugtrack.alsa-project.org/main/index.php/Matrix:Main Alsa Soundcard List],找到你的声卡型号,点击“Details”,然后查看“Setting up modprobe...”小节,将其中的内容插入 {{ic|/etc/modprobe.d/modprobe.conf}} 文件即可。例如,Intel AC97 的配置如下:<br />
<br />
{{bc|# ALSA portion<br />
alias char-major-116 snd<br />
alias snd-card-0 snd-intel8x0<br />
# module options should go here<br />
<br />
# OSS/Free portion<br />
alias char-major-14 soundcore<br />
alias sound-slot-0 snd-card-0<br />
<br />
# card #1<br />
alias sound-service-0-0 snd-mixer-oss<br />
alias sound-service-0-1 snd-seq-oss<br />
alias sound-service-0-3 snd-pcm-oss<br />
alias sound-service-0-8 snd-seq-oss<br />
alias sound-service-0-12 snd-pcm-oss}}<br />
<br />
===与蜂鸣器冲突===<br />
如果你确认所有声道都已解除静音,声卡驱动配置正确,且音量大小也合适,但还是听不到声音,那么请试试将下面的内容添加到 {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
(如果是 {{ic|via82xx}} 驱动)<br />
options snd-<模块名请自行补充> ac97_quirk=0<br />
<br />
(如果是 {{ic|snd_intel8x0}} 驱动)<br />
options snd-NAME-OF-MODULE ac97_quirk=1<br />
<br />
===麦克风输入无声音===<br />
In alsamixer, make sure that all the volume levels are up under recording, and that CAPTURE is toggled active on the microphone (e.g. Mic, Internal Mic) and/or on Capture (in alsamixer, select these items and press space). Try making positive Mic Boost and raising Capture and Digital levels higher; this make make static or distortion, but then you can adjust them back down once you are hearing ''something'' when you record<br />
<br />
As the pulseaudio wrapper is shown as "default" in alsamixer, you may have to press F6 to select your actual soundcard first. You may also need to enable and increase the volume of Line-in in the Playback section.<br />
<br />
To test the microphone, run these commands (see arecord's man page for further information):<br />
arecord -d 5 test-mic.wav<br />
aplay test-mic.wav<br />
<br />
If all fails, you may want to eliminate hardware failure by testing the microphone with a different device.<br />
<br />
For at least some computers, muting a microphone (MM) simply means its input does not go immediately to the speakers. It still receives input.<br />
<br />
Many Dell laptops need "-dmic" to be appended to the model name in {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-hda-intel model=dell-m6-dmic<br />
<br />
Some programs use try to use OSS as the main input software. Add the following lines to {{ic|/etc/modprobe.d/modprobe.conf}} to prevent OSS modules from being loaded:<br />
<br />
{{Note|The OSS modules are no longer autoloaded anyway.}}<br />
<br />
blacklist snd_pcm_oss<br />
blacklist snd_mixer_oss<br />
blacklist snd_seq_oss<br />
<br />
See also:<br />
* http://www.alsa-project.org/main/index.php/SoundcardTesting<br />
* http://alsa.opensrc.org/Record_from_mic<br />
<br />
===设置默认的麦克风/录音设备===<br />
某些程序(Pidgin、Adobe Flash)提供了设置录音设备的选项。如果录音设备不是内置的声卡,而是外接的(如USB摄像头、麦克风),就会有些问题。如果只想更改默认的录音设备、不更改回放设备,需要像下面这样设置 {{ic|~/.asoundrc}}:<br />
<br />
pcm.usb<br />
{<br />
type hw<br />
card U0x46d0x81d<br />
}<br />
<br />
pcm.!default<br />
{<br />
type asym<br />
playback.pcm<br />
{<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
capture.pcm<br />
{<br />
type plug<br />
slave.pcm "usb"<br />
}<br />
}<br />
<br />
把“U0x46d0x81d”替换为你的录音设备的名称。可以通过 {{ic|arecord -L}} 查看所有 ALSA 识别到的录音设备。<br />
<br />
===内置麦克风无效===<br />
首先,请检查 alsamixer,确保所有的录音声道的音量都达到可以录制的程度。如果还是不行,试试修改 /etc/sound.conf 并重载 snd-* 模块。这样就会多出来一个名叫 Capture 的音频控制,可以用它调节内置麦克风的音量。例如,对于 snd-hda-intel 模块,配置如下:<br />
<br />
options snd-hda-intel enable_msi=1<br />
<br />
然后,输入下面的命令重载模块,调整音量并测试。<br />
<br />
{{bc|# rmmod snd-hda-intel && modprobe snd-hda-intel}}<br />
<br />
===Intel 板载声卡无声===<br />
可能由两个相互冲突的驱动模块引起,分别是 {{ic|snd_intel8x0}} 和 {{ic|snd_intel8x0m}}。如果是的话,屏蔽 snd_intel8x0m 即可:<br />
<br />
{{hc|/etc/modprobe.d/modprobe.conf|blacklist snd_intel8x0m}}<br />
<br />
以下方法可能也有效:在 {{ic|alsamixer}} 或 {{ic|amixer}} 把 “External Amplifier” 设置为静音。参见:[http://alsa.opensrc.org/Intel8x0#Dell_Inspiron_8600_.28and_probably_others.29 ALSA wiki 相关条目]。<br />
<br />
也可以试试解除“Mix”的静音。<br />
<br />
===Intel 板载声卡耳机无声===<br />
对于笔记本电脑上的 '''Intel Corporation 82801 I (ICH9 Family) HD Audio Controller''' 声卡,可能需要添加如下内容到 modprobe 配置文件或 sound.conf,以解决耳机无声的问题:<br />
<br />
options snd-hda-intel model=$model<br />
<br />
$model 在下列型号中选择:<br />
<br />
* dell-vostro<br />
* olpc-xo-1_5<br />
* laptop<br />
* dell-m6<br />
* laptop-hpsense<br />
<br />
{{注意|可能需要在配置文件的 "alias" 语句之后添加上述内容。}}<br />
<br />
可以在内核文档中找到所有支持的型号,如[http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.35.y.git;a=blob;f=Documentation/sound/alsa/HD-Audio-Models.txt;h=dc25bb84b83b49665a7ed850e7bf5423d50cd3ba;hb=HEAD 该文]。这是 2.6.35 版本内核的文档,你需要自己查找适用于自己内核版本的文档。<br />
<br />
也可以从[http://www.mjmwired.net/kernel/Documentation/sound/alsa/HD-Audio-Models.txt 这里]获取型号列表。要查看你的声卡芯片名称,输入下列命令(其中的“*”请根据实际情况替换)。注意,有些芯片名称被更改过,因此实际名称与文件中的可能不匹配。<br />
<br />
cat /proc/asound/card*/codec* | grep Codec<br />
<br />
注意,这么做很可能导致所有输入设备(内置和外置麦克风)无法工作,即要么让麦克风正常、要么让耳机正常。如果遇到这一问题,请向 ALSA 开发者报告。<br />
<br />
另外,如果蜂鸣器(pcspkr)出现问题,可以试试使用以下配置修复:<br />
<br />
options snd-hda-intel model=$model enable=1 index=0<br />
<br />
===安装支持 S/PDIF 输出的显卡后无声 ===<br />
检测已加载的相关模块,并记录顺序:<br />
$ cat /proc/asound/modules<br />
0 snd_hda_intel<br />
1 snd_ca0106<br />
<br />
在 {{ic|/etc/modprobe.d/modprobe.conf}} 中禁用不希望使用的显卡的音频解码模块:<br />
# /etc/modprobe.d/modprobe.conf<br />
#<br />
install snd_hda_intel /bin/false<br />
<br />
如果不同设备使用同一模块,那么只能在 BIOS 中禁用其中之一。<br />
<br />
===Poor sound quality or clipping===<br />
If you experience poor sound quality, try setting the PCM volume (in alsamixer) to a level such that gain is 0.<br />
<br />
If snd-usb-audio driver has been loaded, you could try to enable {{ic|softvol}} in '''/etc/asound.conf''' file. Example configuration for the first audio device:<br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.dsnooper {<br />
type dsnoop<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
channels 2<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.softvol {<br />
type softvol<br />
slave { pcm "dmixer" }<br />
control {<br />
name "Master"<br />
card 0<br />
}<br />
}<br />
ctl.!default {<br />
type hw<br />
card 0<br />
}<br />
ctl.softvol {<br />
type hw<br />
card 0<br />
}<br />
ctl.dmixer {<br />
type hw<br />
card 0<br />
}<br />
<br />
===Pops When Starting and Stopping Playback===<br />
Some modules (e.g. snd_ac97_codec and snd_hda_intel) can power off your sound card when not in use. This can make an audible noise (like a crack/pop/scratch) when turning on/off your sound card. Sometimes even when move the slider volume, or open and close windows (KDE4). If you find this annoying try {{ic|modinfo snd_MY_MODULE}}, and look for a module option that adjusts or disables this feature.<br />
<br />
Example: to disable the power saving mode and solve cracking sound trough speakers issue, using snd_hda_intel add in {{ic|/etc/modprobe.d/modprobe.conf}}<br />
options snd_hda_intel power_save=0<br />
or<br />
options snd_hda_intel power_save=0 power_save_controller=N<br />
<br />
You can also try it with {{ic|1=modprobe snd_hda_intel power_save=0}} before.<br />
<br />
You may also have to unmute the 'Line' ALSA channel for this to work. Any value will do (other than '0' or something too high).<br />
<br />
''Example:'' on an onboard VIA VT1708S (using the snd_hda_intel module) these cracks occured even though 'power_save' was set to 0. Unmuting the 'Line' channel and setting a value of '1' solved the issue.<br />
<br />
Source: https://www.kernel.org/doc/Documentation/sound/alsa/powersave.txt<br />
<br />
If you use a laptop, pm-utils will change {{ic|power_save}} back to 1 when you go onto battery power even if you disable power saving in {{ic|/etc/modprobe.d}}. Disable this for pm-utils by disabling the script that makes the change (see [https://wiki.archlinux.org/index.php/Pm-utils#Disabling_a_hook Disabling a hook] for more information):<br />
# touch /etc/pm/power.d/intel-audio-powersave<br />
<br />
===S/PDIF Output Does Not Work===<br />
<br />
If the optical/coaxial digital output of your motherboard/sound card is not working or stopped working, and have already enabled and unmuted it in alsamixer, try running<br />
# iecset audio on<br />
<br />
as root.<br />
<br />
You can also put this command in {{Ic|rc.local}} as it sometimes it may stop working after a reboot.<br />
<br />
===HDMI Output Does Not Work===<br />
If the HDMI output of your motherboard/sound card is not working or stopped working, and have already enabled and unmuted it in alsamixer, try the following.<br />
<br />
Query for Playback Devices:<br />
<br />
$ aplay -l<br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: NVidia [HDA NVidia], device 0: ALC1200 Analog [ALC1200 Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 1: ALC1200 Digital [ALC1200 Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 3: NVIDIA HDMI [NVIDIA HDMI]<br />
Subdevices: 0/1<br />
Subdevice #0: subdevice #0<br />
<br />
Now that we have the info for the HDMI device, try a test, In the example below, 0 is the card number and 3 is the device number.<br />
<br />
$ aplay -D plughw:0,3 /usr/share/sounds/alsa/Front_Center.wav<br />
<br />
If aplay does not output any errors, but still no sound is heared, "reboot" the receiver, monitor or tv set. Since the HDMI interface executes a handshake on connection, it might have noticed before that there was no audio stream embedded, and disabled audio decoding. In particular, if you are using a standalone window manager (don’t know about Gnome or KDE), you may need to have some sound playing ''while'' plugging in the HDMI cable.<br />
<br />
{{Note|If you are using an ATI card and linux 3.0, check your kernel/grub settings, you most likely need to use the ''''radeon.audio'''<nowiki>=</nowiki>'''1'''' boot parameter, or alternatively set up the option in your modprobe configuration files. It's important to note that before trying, is to check the [http://www.x.org/wiki/RadeonFeature#fndef-72849d6d6eb3927d486f50ece74a739042965a6c-25 Radeon Feature Matrix] to see if it's actually supported. At the moment this is written (march 29th, 2013), some chipsets of the [http://www.x.org/wiki/RadeonFeature#Decoder_ring_for_engineering_vs_marketing_names S. Islands] (namely HD7750 through HD7970) doesn't (still) support HDMI Audio.}}<br />
<br />
If the test is successful, create or edit your ~/.asoundrc file to set HDMI as the default audio device.<br />
<br />
{{hc|~/.asoundrc|<br />
pcm.!default {<br />
type hw<br />
card 0<br />
device 3<br />
}<br />
}}<br />
<br />
Or you above config does not work try:<br />
{{hc|~/.asoundrc|<br />
defaults.pcm.card 0<br />
defaults.pcm.device 3<br />
defaults.ctl.card 0<br />
}}<br />
<br />
===HDMI Multi-channel PCM output does not work (Intel) ===<br />
As of Linux 3.1 multi-channel PCM output through HDMI with a Intel card (Intel Eaglelake, IbexPeak/Ironlake,SandyBridge/CougarPoint and IvyBridge/PantherPoint) is not yet supported. Support for it has been recently added and expected to be available in Linux 3.2. To make it work in Linux 3.1 you need to apply the following patches:<br />
<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=76adaa34db407f174dd06370cb60f6029c33b465 drm: support routines for HDMI/DP ELD]<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=e0dac65ed45e72fe34cc7ccc76de0ba220bd38bb drm/i915: pass ELD to HDMI/DP audio driver]<br />
<br />
===HP TX2500===<br />
Add these 2 lines into {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-cmipci mpu_port=0x330 fm_port=0x388<br />
options snd-hda-intel index=0 model=toshiba position_fix=1<br />
<br />
options snd-hda-intel model=hp (works for tx2000cto)<br />
<br />
===Skipping Sound When Playing MP3===<br />
If you have sound skipping when playing MP3 files and you have more then 2 speakers attached to your computer (i.e. > 2 speaker system), run alsamixer and disable the channels for the speakers that you '''DO NOT''' have (i.e. do not enable the sound for the center speaker if you do not have a center speaker.<br />
<br />
===Using a USB Headset and External USB Sound Cards===<br />
If you are using a USB headset with ALSA you can try using {{AUR|asoundconf}} (currently only available from the [[AUR]]) to set the headset as the primary sound output. Before running make sure you have usb audio module enabled ({{ic|modprobe snd-usb-audio}}).<br />
<br />
# asoundconf is-active<br />
# asoundconf list<br />
# asoundconf set-default-card <chosen soundcard><br />
<br />
==== Crackling sound with USB devices ====<br />
If you experience crackling sound on USB devices, you can try tuning the snd-usb-audio for minimal latency.<br />
<br />
Add this to your {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
options snd-usb-audio nrpacks=1<br />
<br />
source: http://alsa.opensrc.org/Usb-audio#Tuning_USB_devices_for_minimal_latencies<br />
<br />
==== Hot-plugging a USB Sound Card ====<br />
In order to automatically make a USB Sound Card the primary output device, when the card is plugged in, you can use the following udev rules (e.g. add the following two lines to {{ic|/etc/udev/rules.d/00-local.rules}} and reboot).<br />
<br />
{{bc|1=KERNEL=="pcmC[D0-9cp]*", ACTION=="add", PROGRAM="/bin/sh -c 'K=%k; K=$${K#pcmC}; K=$${K%%D*}; echo defaults.ctl.card $$K > /etc/asound.conf; echo defaults.pcm.card $$K >>/etc/asound.conf'"<br />
KERNEL=="pcmC[D0-9cp]*", ACTION=="remove", PROGRAM="/bin/sh -c 'echo defaults.ctl.card 0 > /etc/asound.conf; echo defaults.pcm.card 0 >>/etc/asound.conf'"}}<br />
<br />
===Error 'Unknown hardware' Appears After a Kernel Update===<br />
The following messages may be displayed during the start-up ALSA after the kernel update:<br />
Unknown hardware "foo" "bar" ...<br />
Hardware is initialized using a guess method<br />
/usr/sbin/alsactl: set_control:nnnn:failed to obtain info for control #mm (No such file or directory)<br />
<br />
or<br />
Found hardware: "HDA-Intel" "VIA VT1705" "HDA:11064397,18490397,00100000" "0x1849" "0x0397"<br />
Hardware is initialized using a generic method<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #1 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #2 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #25 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #26 (No such file or directory)<br />
<br />
Simply store ALSA mixer settings again (as root):<br />
# alsactl -f /var/lib/alsa/asound.state store<br />
<br />
It may be necessary configure ALSA again with alsamixer<br />
<br />
===HDA Analyzer===<br />
If the mappings to your audio pins(plugs) do not correspond but ALSA works fine, you could try HDA Analyzer -- a pyGTK2 GUI for HD-audio control can be found [http://www.alsa-project.org/main/index.php/HDA_Analyzer at the ALSA wiki].<br />
Try tweaking the Widget Control section of the PIN nodes, to make microphones IN and headphone jacks OUT. Referring to the Config Defaults heading is a good idea.<br />
<br />
NOTE: the script is done by such way that it is incompatible with python3 (which is now shipped with ArchLinux) but tries to use it.<br />
The workaround is: open "run.py", find all occurences of "python" (2 occurences - one on the first line, and the second on the last line) and replace them all by "python2".<br />
<br />
NOTE2: the script requires root acces, but running it via su/sudo is bogus. Run it via {{ic|kdesu}} or {{ic|gksu}}.<br />
<br />
===ALSA with SDL===<br />
If you get no sound via SDL and ALSA cannot be chosen from the application. Try setting the environmental variable SDL_AUDIODRIVER to alsa.<br />
# export SDL_AUDIODRIVER=alsa<br />
<br />
===Low Sound Workaround===<br />
<br />
If you are facing low sound even after maxing out your speakers/headphones, you can give the softvol plugin a try. Add the following to {{ic|/etc/asound.conf}}.<br />
{{bc|<nowiki><br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
<br />
pcm.softvol {<br />
type softvol<br />
slave {<br />
pcm "dmix"<br />
}<br />
control {<br />
name "Pre-Amp"<br />
card 0<br />
}<br />
min_dB -5.0<br />
max_dB 20.0<br />
resolution 6<br />
}<br />
</nowiki>}}<br />
{{note| You will probably have to restart the computer, as restarting the alsa daemon did not load the new configuration for me. Also, if the configuration does not work even after restarting, try changing {{ic|plug}} with {{ic|hw}} in the above configuration.}}<br />
<br />
After the changes are loaded successfully, you will see a {{ic|Pre-Amp}} section in alsamixer. You can adjust the levels there.<br />
{{note|Setting a high value for {{ic|Pre-Amp}} can cause sound distortion, so adjust it according to the level that suits you.}}<br />
<br />
===Popping sound after resuming from suspension===<br />
You might hear a popping sound after resuming the computer from suspension. This can be fixed by editing {{ic|/etc/pm/sleep.d/90alsa}} and removing the line that says {{ic|aplay -d 1 /dev/zero}}<br />
<br />
==设置文件范例==<br />
参见:[[Advanced Linux Sound Architecture/Example Configurations]]。<br />
<br />
==相关阅读==<br />
* [http://www.mjmwired.net/kernel/Documentation/sound/alsa/ALSA-Configuration.txt ALSA 模块高级配置]<br />
* [http://alsa.opensrc.org/Main_Page 非官方的 ALSA Wiki]<br />
* [https://bbs.archlinux.org/viewtopic.php?id=36815 如何从 svn 获取驱动代码并编译]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Advanced_Linux_Sound_Architecture_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=255088
Advanced Linux Sound Architecture (简体中文)
2013-04-25T03:21:14Z
<p>Cuihao: /* No Headphone Sound with Onboard Intel Sound Card */</p>
<hr />
<div>[[Category:简体中文]]<br />
[[Category:Sound (简体中文)]]<br />
[[Category:Audio/Video (简体中文)]]<br />
[[cs:Advanced Linux Sound Architecture]]<br />
[[de:Alsa]]<br />
[[es:Advanced Linux Sound Architecture]]<br />
[[fa:ALSA]]<br />
[[fr:Alsa]]<br />
[[he:Advanced Linux Sound Architecture]]<br />
[[it:Advanced Linux Sound Architecture]]<br />
[[ja:Advanced Linux Sound Architecture]]<br />
[[nl:Advanced Linux Sound Architecture]]<br />
[[pt:Advanced Linux Sound Architecture]]<br />
[[ru:Advanced Linux Sound Architecture]]<br />
[[sk:Advanced Linux Sound Architecture]]<br />
[[th:Advanced Linux Sound Architecture]]<br />
[[tr:Gelişmiş_ses_mimarisi_(ALSA)]]<br />
[[en:Advanced Linux Sound Architecture]]<br />
<br />
{{Article summary start}}<br />
{{Article summary text|ALSA 的使用、配置及疑难解答。}}<br />
{{Article summary heading|相关页面}}<br />
{{Article summary wiki|Sound system (简体中文)}}<br />
{{Article summary wiki|Disable PC Speaker Beep (简体中文)}}<br />
{{Article summary end}}<br />
{{TranslationStatus (简体中文)|Advanced_Linux_Sound_Architecture|2013-04-25|254584}}<br />
<br />
[[Wikipedia:zh:ALSA|高级 Linux 声音体系]](Advanced Linux Sound Architecture,'''ALSA''')是Linux中提供声音设备驱动的内核组件,用来代替原来的开放声音系统(Open Sound System,OSSv3)。除了声音设备驱动,'''ALSA'''还包含一个用户空间的函数库,以方便开发者通过高级API使用驱动功能,而不必直接与内核驱动交互。<br />
<br />
{{注意|关于另一种声音体系,请阅读[[Open Sound System (简体中文)|开放声音系统(OSS)]]的页面。}}<br />
<br />
==安装==<br />
Arch 默认的内核已经通过一套模块提供了 ALSA,不必特别安装。<br />
<br />
[[udev (简体中文)|udev]]会在系统启动时自动检测硬件,并加载相应的声音设备驱动模块。这时,你的声卡已经可以工作了,只是所有声道默认都被设置成静音了。<br />
<br />
在本地登录(通过虚拟终端或登录管理器)的用户,都有权限播放音频并调整音量。要让远程登录的用户拥有这些权限,必须把用户[[Users and Groups (简体中文)#用户组管理|加入]] {{ic|audio}} 用户组。该组的成员可以直接访问声音设备,会导致某些程序独占音频输出(破坏软件混音),还可能影响用户快速切换和拖机(multiseat)。因此,除非真的有某些[https://wiki.ubuntu.com/Audio/TheAudioGroup 特殊需求],'''不建议'''把用户加入 {{ic|audio}} 用户组。<br />
<br />
===用户空间工具===<br />
从 [[Official Repositories (简体中文)|官方仓库]]:<br />
* [[pacman (简体中文)|安装]] {{Pkg|alsa-utils}} 软件包,其中的 {{ic|alsamixer}} 工具允许用户在控制台或终端中配置声音设备。<br />
* 安装 {{Pkg|alsa-oss}} 软件包,如果想让 OSS 应用程序与dmix(软件混音)协同工作。 <br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。}}<br />
*安装 {{Pkg|alsa-plugins}} 软件包,如果需要 [[#高质量重采样|高质量重采样]]、[[#Upmixing/Downmixing|软件模拟环绕立体声]]和其他高级特性。<br />
<br />
==解除各声道的静音==<br />
目前版本的 ALSA 安装后,所有声道'''默认是静音的''',必须手动解除。<br />
<br />
使用 {{ic|alsamixer}} 的 ncurses 界面,配置十分简单:<br />
<br />
$ alsamixer<br />
<br />
此外,还可以在命令行下使用 {{ic|amixer}}:<br />
<br />
$ amixer sset Master unmute<br />
<br />
在 {{ic|alsamixer}} 中,下方标有 {{ic|MM}} 的声道是静音的,而标有 {{ic|00}} 的通道已经启用。<br />
<br />
使用左右方向键,选中 {{ic|Master}} 和 {{ic|PCM}} 声道。按下 {{keypress|m}} 键解除静音。使用上方向键增加音量,直到增益值为{{ic|0}}。该值显示在左上方 {{ic|Item:}} 字段后。过高的增益值会导致声音失真。<br />
<br />
要想得到完整的 5.1 或 7.1 环绕立体声,还得解除 Front、Surround、Center、LFE (subwoofer) 和 Side 这些声道的静音(上述名称是 Intel HD Audio 声卡使用的声道名,可能因设备不同而有所差异)。注意,仅有这些设置,系统不会自动将立体声源(多数音乐)提升(upmix)成环绕立体声。如果需要这些功能,请阅读[[#Upmixing/Downmixing]]。<br />
<br />
要启用麦克风,切换至 Capture 选项卡,按下 {{keypress|F4}},按下 {{keypress|空格}} 启用其中一个声道即可。<br />
<br />
按下 {{Keypress|Esc}} 键退出 alsamixer。<br />
<br />
{{注意|<br />
* 有一些声卡,需要关闭数字输出或将其调成静音,才能输出模拟音频信号。对于 Soundblaster Audigy LS 声卡,需要把 IEC958 通道设成静音。 <br />
* 有些机器(如 Thinkpad T61),有一个 Speaker 通道,必须按照上述方法打开调整。<br />
* 有些机器(如 Dell E6400),可能还需要把 {{ic|Front}} 和 {{ic|Headphone}} 声道打开调整。}}<br />
<br />
接下来,测试声卡是否工作:<br />
<br />
$ speaker-test -c 2<br />
<br />
根据扬声器的实际情况,调整 -c 后的数值。对于 7.1 声道,这个数字是 8:<br />
<br />
$ speaker-test -c 8<br />
<br />
如果没有正常工作,请继续阅读 [[#配置]] 以及 [[#疑难解答]] 部分。<br />
<br />
{{pkg|alsa-utils}} 软件包提供了两个配置 alsa 的服务:{{ic|alsa-restore.service}} 和 {{ic|alsa-store.service}}。它们分别在开机和关机时自动运行。<br />
<br />
==配置==<br />
=== VirtualBox 虚拟机无声音 ===<br />
<br />
如果使用 VirtualBox 时碰上了麻烦,请尝试以下命令:<br />
<br />
{{hc|$ alsactl init|2=<nowiki><br />
<br />
Found hardware: "ICH" "SigmaTel STAC9700,83,84" "AC97a:83847600" "0x8086" "0x0000"<br />
Hardware is initialized using a generic method<br />
</nowiki><br />
}}<br />
<br />
如果还是不行,请检查所使用的音频软件是否激活了 ALSA 输出。<br />
<br />
===设置默认声卡===<br />
<br />
如果发现开机时声卡次序会发生变化,可以在通过 {{ic|/etc/modprobe.d}} 中的 {{ic|.conf}} 文件(比如 {{ic|/etc/modprobe.d/alsa-base.conf}})手动设置次序。<br />
比如,要让 mia 声卡成为 #0、Intel HDA 声卡成为 #1:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<br />
options snd slots=snd_mia,snd_hda_intel<br />
options snd_mia index=0<br />
options snd_hda_intel index=1<br />
}}<br />
<br />
使用 {{ic|<nowiki>lsmod | grep snd</nowiki>}} 获取设备列表。<br />
<br />
snd_mia 和 snd_hda_intel 分别是两个声卡的内核驱动模块。该配置仅适用于同时装有 mia 声卡和使用 snd_hda_intel 驱动的声卡(通常是板载声卡)的情况。<br />
<br />
如果使用 -2 的 index 值,ALSA 就不会将对应的设备作为主声卡使用。Linux Mint 和 Ubuntu 等发行版使用了以下配置,避免 USB 声卡和其他“非主流”设备变成 index 为 0 的主声卡:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<br />
options bt87x index=-2<br />
options cx88_alsa index=-2<br />
options saa7134-alsa index=-2<br />
options snd-atiixp-modem index=-2<br />
options snd-intel8x0m index=-2<br />
options snd-via82xx-modem index=-2<br />
options snd-usb-audio index=-2<br />
options snd-usb-caiaq index=-2<br />
options snd-usb-ua101 index=-2<br />
options snd-usb-us122l index=-2<br />
options snd-usb-usx2y index=-2<br />
# Keep snd-pcsp from being loaded as first soundcard<br />
options snd-pcsp index=-2<br />
# Keep snd-usb-audio from beeing loaded as first soundcard<br />
options snd-usb-audio index=-2<br />
}}<br />
<br />
以上配置会在系统重启后生效。<br />
<br />
{{Out of date|据英文 Wiki 用户反应,{{ic|/etc/asound.conf}} 这个配置文件默认是不存在的,下列操作可能有误。}}<br />
<br />
首先,运行 {{ic|aplay -l}},获取声卡的声卡ID和设备ID:<br />
<br />
{{hc|$ aplay -l|2=<nowiki><br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: Intel [HDA Intel], device 0: CONEXANT Analog [CONEXANT Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: Intel [HDA Intel], device 1: Conexant Digital [Conexant Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 1: JamLab [JamLab], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 2: Audio [Altec Lansing XT1 - USB Audio], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
</nowiki>}}<br />
<br />
拿输出中的最后一项来说,这个声卡的声卡ID为2,设备ID为0。若要把这块声卡作为默认声卡,可以把下列配置添加到系统级别的 {{ic|/etc/asound.conf}} 或用户级别的 {{ic|~/.asoundrc}} 文件。如果文件不存在,可以手动创建。其中的各个ID,请根据实际情况调整:<br />
{{bc|<br />
defaults.pcm.card 2<br />
defaults.pcm.device 0<br />
defaults.ctl.card 2<br />
}}<br />
“pcm”选项决定用来播放音频的设备,而“ctl”选项决定那个声卡能够由控制工具(如 alsamixer)使用。<br />
<br />
上述配置在重启音频程序(如 mplayer)后立即生效。<br />
<br />
===确认所有声音模块都已经加载===<br />
<br />
一般 udev 都会自动识别出声卡。使用以下命令确认:<br />
<br />
{{hc|<nowiki>$ lsmod | grep '^snd' | column -t</nowiki>|<nowiki><br />
snd_hda_codec_hdmi 22378 4<br />
snd_hda_codec_realtek 294191 1<br />
snd_hda_intel 21738 1<br />
snd_hda_codec 73739 3 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel<br />
snd_hwdep 6134 1 snd_hda_codec<br />
snd_pcm 71032 3 snd_hda_codec_hdmi,snd_hda_intel,snd_hda_codec<br />
snd_timer 18992 1 snd_pcm<br />
snd 55132 9 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel,snd_hda_codec,snd_hwdep,snd_pcm,snd_timer<br />
snd_page_alloc 7017 2 snd_hda_intel,snd_pcm<br />
</nowiki>}}<br />
<br />
如果你的输出和上面类似,那就说明声卡已经被正确识别。<br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。如有需要,请[[Kernel_modules_(简体中文)#装入|手动加载]]。}}<br />
<br />
还可以检查一下 {{ic|/dev/snd/}} 目录,看看是否有这些设备文件:<br />
<br />
{{hc|$ ls -l /dev/snd|2=<nowiki><br />
total 0<br />
crw-rw---- 1 root audio 116, 0 Apr 8 14:17 controlC0<br />
crw-rw---- 1 root audio 116, 32 Apr 8 14:17 controlC1<br />
crw-rw---- 1 root audio 116, 24 Apr 8 14:17 pcmC0D0c<br />
crw-rw---- 1 root audio 116, 16 Apr 8 14:17 pcmC0D0p<br />
crw-rw---- 1 root audio 116, 25 Apr 8 14:17 pcmC0D1c<br />
crw-rw---- 1 root audio 116, 56 Apr 8 14:17 pcmC1D0c<br />
crw-rw---- 1 root audio 116, 48 Apr 8 14:17 pcmC1D0p<br />
crw-rw---- 1 root audio 116, 1 Apr 8 14:17 seq<br />
crw-rw---- 1 root audio 116, 33 Apr 8 14:17 timer<br />
</nowiki>}}<br />
<br />
{{注意|在 IRC 或论坛寻求这方面帮助时,别忘了贴出上面几个命令的输出。}}<br />
<br />
如果你的输出跟上面类似,或至少有 '''controlC0''' 和 '''pcmC0D0p''',那么声卡就已经正常加载了。<br />
<br />
如果出现问题,声卡模块没有正确加载,那么请尝试手动加载模块:<br />
<br />
* 确定声卡对应的驱动模块:[http://www.alsa-project.org/main/index.php/Matrix:Main ALSA Soundcard Matrix]。这些模块都会有一个”snd-“前缀(例如:{{ic|snd-via82xx}})。<br />
* [[Kernel_modules_(简体中文)#装入|加载模块]]。<br />
* 检查 {{ic|/dev/snd}} 目录中的设备文件(参见上文);或者,检查 {{ic|alsamixer}} 或 {{ic|amixer}} 的输出是否正确。<br />
* 修改配置,使 {{ic|snd-<模块名>}} 和 {{ic|snd-pcm-oss}} 模块[[Kernel_modules_(简体中文)#装入|开机自动加载]]。<br />
<br />
===启用 SPDIF 输出===<br />
<br />
(由 Gentoo 论坛的 gralves 提供)<br />
* 在 GNOME 音量控制的选项(Options)标签中,把 IEC958 改成 PCM。该选项可以在首选项中启用。<br />
* 不使用 GNOME 音量控制工具的话:<br />
** 编辑 {{ic|/var/lib/alsa/asound.state}}。该文件是 alsasound 保存混音器设置的地方。<br />
** 找到这一行:“IEC958 Playback Switch”。该行上下应该有一行内容为 {{ic|value:false}},将其改为 {{ic|value:true}}。<br />
** 再找到这一行:”IEC958 Playback AC97-SPSA“。把对应的值改为 0。<br />
** 重启 ALSA。<br />
<br />
{{Out of date|rc.local 在 systemd 中已经弃用,以下方法可能不再适用。}}<br />
<br />
还有另外一个开机自动启用 SPDIF 输出的方法(在 SoundBlaster Audigy 声卡上测试过):<br />
<br />
* 在 {{ic|/etc/rc.local}} 中添加如下内容:<br />
# Use COAX-digital output<br />
amixer set 'IEC958 Optical' 100 unmute<br />
amixer set 'Audigy Analog/Digital Output Jack' on<br />
<br />
使用以下命令,获取你的声卡的数字输出的名称:<br />
$ amixer scontrols<br />
<br />
===系统级均衡器===<br />
====使用 AlsaEqual(包含界面)====<br />
从 [[AUR (简体中文)|AUR]] 安装 {{AUR|alsaequal}}。<br />
<br />
{{注意|如果使用的是64位系统,而又安装了32位的 Flash 插件,这里的设置会导致 Flash 无声。如果发生了这种情况,请禁用 alsaequal 或编译32位版本的 alsaequal。}}<br />
<br />
安装后,把下列内容添加到 ALSA 配置文件({{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}}):<br />
<br />
ctl.equal {<br />
type equal;<br />
}<br />
<br />
pcm.plugequal {<br />
type equal;<br />
# Modify the line below if you do not<br />
#slave.pcm "plughw:0,0";<br />
#by default we want to play from more sources at time:<br />
slave.pcm "plug:dmix";<br />
}<br />
#pcm.equal {<br />
# If you do not want the equalizer to be your<br />
# default soundcard comment the following<br />
# line and uncomment the above line. (You can<br />
# choose it as the output device by addressing<br />
# it with specific apps,eg mpg123 -a equal 06.Back_In_Black.mp3)<br />
pcm.!default {<br />
type plug;<br />
slave.pcm plugequal;<br />
}<br />
<br />
设置好后,切换均衡器:<br />
$ alsamixer -D equal<br />
<br />
需要注意,每个用户的 alsaequal 配置文件都是独立的,位于 {{ic|~/.alsaequal.bin}}。因此,如果想在 [[mpd]] 之类的以独立用户身份运行的程序中使用 AlsaEqual,就得像下面这样:<br />
# su mpd -c 'alsamixer -D equal'<br />
或者,在其他用户的主目录下创建符号链接,指向你的 {{ic|.alsaequal.bin}}……<br />
<br />
=====管理 AlsaEqual 配置=====<br />
从 [http://xyne.archlinux.ca/repos/ Xyne 的软件仓库] 或 [https://aur.archlinux.org/packages.php?ID=62420 AUR] 安装 [http://xyne.archlinux.ca/projects/alsaequal-mgr/ alsaequal-mgr]。<br />
<br />
同上,配置均衡器: <br />
$alsamixer -D equal<br />
<br />
如果对效果感到满意,将其命名(如“foo”)并保存该配置:<br />
$alsaequal-mgr save foo<br />
<br />
重新加载“foo”中的配置:<br />
$alsaequal-mgr load foo<br />
<br />
这样,就可以为游戏、电影、不同音乐流派、网络电话等等创建不同的均衡器配置,以便随时切换。<br />
<br />
参见[http://xyne.archlinux.ca/projects/alsaequal-mgr/ 项目主页]和程序的帮助信息,了解更多设置。<br />
<br />
====使用 mbeq====<br />
{{注意|该软件用到了一个 ladspa 插件,可能导致播放音频时 CPU 占用率偏高。In addition, this was made with stereophonic sound (e.g. headphones) in mind.}}<br />
<br />
安装 {{Pkg|alsa-plugins}}、{{Pkg|ladspa}} 和 {{Pkg|swh-plugins}} 软件包。<br />
<br />
* 在 {{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}} 中添加如下内容:<br />
<br />
{{hc|/etc/asound.conf|2=<nowiki>pcm.eq {<br />
type ladspa<br />
<br />
# The output from the EQ can either go direct to a hardware device<br />
# (if you have a hardware mixer, e.g. SBLive/Audigy) or it can go<br />
# to the software mixer shown here.<br />
#slave.pcm "plughw:0,0"<br />
slave.pcm "plug:dmix"<br />
<br />
# Sometimes you may need to specify the path to the plugins,<br />
# especially if you have just installed them. Once you have logged<br />
# out/restarted this should not be necessary, but if you get errors<br />
# about being unable to find plugins, try uncommenting this.<br />
#path "/usr/lib/ladspa"<br />
<br />
plugins [<br />
{<br />
label mbeq<br />
id 1197<br />
input {<br />
#this setting is here by example, edit to your own taste<br />
#bands: 50hz, 100hz, 156hz, 220hz, 311hz, 440hz, 622hz, 880hz, 1250hz, 1750hz, 25000hz,<br />
#50000hz, 10000hz, 20000hz<br />
controls [ -5 -5 -5 -5 -5 -10 -20 -15 -10 -10 -10 -10 -10 -3 -2 ]<br />
}<br />
}<br />
]<br />
}<br />
<br />
# Redirect the default device to go via the EQ - you may want to do<br />
# this last, once you are sure everything is working. Otherwise all<br />
# your audio programs will break/crash if something has gone wrong.<br />
<br />
pcm.!default {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
<br />
# Redirect the OSS emulation through the EQ too (when programs are running through "aoss")<br />
<br />
pcm.dsp0 {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
</nowiki>}}<br />
<br />
* 完成。若有问题,欢迎前往论坛咨询。<br />
<br />
==高质量重采样==<br />
启用软件混音时,ALSA 会强制把所有音频重采样到相同的频率(如果系统支持,默认是 48000)。dmix 的重采样算法很糟糕,会导致显著的质量损失。<br />
<br />
安装 {{pkg|alsa-plugins}} 和 {{pkg|libsamplerate}}。<br />
<br />
将默认的采样率转换器更改为 libsamplerate:<br />
<br />
{{hc|/etc/asound.conf|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
或:<br />
<br />
{{hc|~/.asoundrc|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
'''samplerate_best''' 输出的声音质量最佳,但需要耗费大量CPU资源进行实时重采样。还有其他的算法可供选择(如 '''samplerate''' 等),但比起默认的算法,这些算法的改进并不明显。<br />
<br />
{{警告|某些系统中,启用 samplerate_best 可能导致 Flash 播放器无声音。}}<br />
<br />
==Upmixing/Downmixing==<br />
===Upmixing===<br />
In order for stereo sources like music to be able to saturate a 5.1 or 7.1 sound system, you need to use upmixing. In darker days this used to be tricky and error prone but nowadays plugins exist to easily take care of this task. Thus, install {{Pkg|alsa-plugins}}.<br />
<br />
Then add the following to your ALSA configuration file of choice (either {{ic|/etc/asound.conf}} or {{ic|~/.asoundrc}}):<br />
pcm.upmix71 {<br />
type upmix<br />
slave.pcm "surround71"<br />
delay 15<br />
channels 8<br />
}<br />
<br />
You can easily change this example for 7.1 upmixing to 5.1 or 4.0.<br />
<br />
This adds a new pcm that you can use for upmixing. If you want all sound sources to go through this pcm, add it as a default below the previous definition like so:<br />
<br />
pcm.!default "plug:upmix71"<br />
<br />
The plugin automatically allows multiple sources to play through it without problems so setting is as a default is actually a safe choice. <br />
If this is not working, you have to setup your own dmixer for the upmixing PCM like this: <br />
pcm.dmix6 {<br />
type asym<br />
playback.pcm {<br />
type dmix<br />
ipc_key 567829<br />
slave {<br />
pcm "hw:0,0"<br />
channels 6<br />
}<br />
}<br />
}<br />
<br />
and use "dmix6" instead of "surround71". <br />
If you experience skipping or distorted sound, consider increasing the buffer_size (to 32768, for example) or use a [[#高质量重采样|high quality resampler]].<br />
<br />
===Downmixing===<br />
If you want to downmix sources to stereo because you, for instance, want to watch a movie with 5.1 sound on a stereo system, you need to use the vdownmix plugin that alsa provides in the same package.<br />
<br />
Again, in your configuration file, add this:<br />
<br />
pcm.!surround51 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
pcm.!surround40 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
<br />
==混音==<br />
===软件混音(dmix)===<br />
<br />
{{注意|在 ALSA 1.0.9rc2 及以上版本中,如果声卡不支持硬件混音,模拟输出将默认使用 dmix 混音,无需额外设置。}}<br />
<br />
如果软件混音未正常启用,那么可以手动创建 {{ic|~/.asoundrc}},加入以下内容:<br />
<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
<br />
这样就启用了软件混音,多个程序能够同时使用声卡。<br />
<br />
至于 S/PDIF 之类的数字音频输出,目前的 ALSA 还不会默认启用 dmix。因此,必须像上面这样手动启用。<br />
<br />
常见问题的解答,参见[[#疑难解答]]部分。<br />
<br />
===硬件混音===<br />
====支持====<br />
如果声卡支持,一般无需任何配置即可启用硬件混音。绝大多数板载声卡都不支持硬件混音,必须得使用软件混音(见上文)。很多独立声卡都支持硬件混音,Linux 下支持较好的有:<br />
<br />
* Creative SoundBlaster Live! (5.1 型号)<br />
* Creative SoundBlaster Audigy (部分型号)<br />
* Creative SoundBlaster Audidy 2 (ZS 系列型号)<br />
* Creative SoundBlaster Audigy 4 (Pro 系列型号)<br />
<br />
{{注意|上述声卡的低端版本(Audigy SE、Audigy 2 NX、SoundBlaster Live! 24bit 以及 SoundBlaster Live! 7.1)使用了不同的芯片,'''不支持'''硬件混音。}}<br />
<br />
{{注意|板载 VIA8237 芯片支持4路硬件混音,但在某些主板上只能使用3路(第4路无声),甚至根本无法使用。即使能正常工作,质量也比不上其他混音方法。}}<br />
<br />
====问题修复====<br />
如果你的声卡是 Intel Corporation 82801I (ICH9 Family) HD Audio Controller (rev 02),且使用了64位系统,需要执行以下命令才能在 Enemy Territory 中听到声音:<br />
echo "et.x86 0 0 direct" > /proc/asound/card0/pcm0p/oss<br />
echo "et.x86 0 0 disable" > /proc/asound/card0/pcm0c/oss<br />
<br />
==疑难解答==<br />
===使用CPU频率动态调整时,音频发生跳跃===<br />
使用 {{ic|ondemand}} 或 {{ic|conservative}} 频率策略、启用CPU频率动态调整时,某些声卡和某些 ALSA 驱动会出现声音跳跃的情况。目前,唯一的解决方案是使用固定的频率,如 {{ic|performance}} 性能策略。<br />
<br />
详情参见 [[CPU Frequency Scaling (简体中文)]]。<br />
<br />
===同时只能有一个用户播放音频===<br />
有时会出现一个用户独占软件混音器的情况。多数情况下这没什么影响,但在使用 [[mpd]] 这样以独立用户身份运行的程序时,就会出现问题。当 mpd 运行时,用户自己就无法播放其他音频了。一种解决方法是,以正在使用的用户身份运行 mpd。另一种方法是,在 ALSA 配置文件的 {{ic|pcm.dmixer}} 部分添加 {{ic|ipc_key_add_uid 0}} 禁止独占。以下是 {{ic|asound.conf}} 示例的片段,省略的部分无需修改:<br />
<br />
...<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
slave {<br />
...<br />
<br />
===无法同时播放多个音频===<br />
如果安装了 [[PulseAudio (简体中文)|PulseAudio]](可能作为 [[GNOME (简体中文)|GNOME]] 的依赖被安装),可能出现无法同步播放的问题,因为 PulseAudio 的默认配置是“独占”声卡。ALSA 的用户可能对自己的配置已经很满意、不希望使用 [[PulseAudio (简体中文)|PulseAudio]]。解决方案是编辑 {{ic|/etc/asound.conf}} 并注释掉以下内容:<br />
# Use PulseAudio by default<br />
#pcm.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
# hint {<br />
# show on<br />
# description "Default ALSA Output (currently PulseAudio Sound Server)"<br />
# }<br />
#}<br />
<br />
可能还需要注释掉下面的内容:<br />
#ctl.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
#}<br />
<br />
比起完全卸载 [[PulseAudio (简体中文)|PulseAudio]],这大概是个更简单的方法。<br />
<br />
以下是 {{ic|/etc/asound.conf}} 配置文件的一个示例:<br />
{{bc|<nowiki>pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
}<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}</nowiki><br />
}}<br />
<br />
{{注意|本 {{ic|/etc/asound.conf}} 本来是配合 [[MPD]] 全局配置使用的。参见[[#同时只能有一个用户播放音频|该节]]关于多用户的内容。}}<br />
<br />
===有时候开机无声===<br />
如果会随机地出现开机无声的问题,原因可能是多声卡的顺序发生了变化。如果是这种情况,请参照[[#设置默认声卡]]进行设置。<br />
<br />
对于 mpd 用户,上述配置无效,请参考[http://mpd.wikia.com/wiki/Configuration#ALSA_MPD_software_volume_control 该文]设置。<br />
<br />
===特定程序的问题===<br />
对于不使用系统声音配置的程序,如 XMMS 和 Mplayer,必须特别设置以应用 ALSA 配置。<br />
<br />
对于 mplayer,将以下内容添加到 {{ic|~/.mplayer/config}} (或系统全局配置文件 {{ic|/etc/mplayer/mplayer.conf}}):<br />
ao=alsa<br />
<br />
对于 XMMS 和 Beep Media Player,打开选项窗口,要把声音驱动设置为 ALSA 而不是 OSS。<br />
<br />
XMMS 的设置方法:<br />
* 启动 XMMS<br />
** 选项 -> 首选项<br />
** 选择 Alsa 输出插件<br />
<br />
对于未提供 ALSA 输出的程序,可以使用 alsa-oss 软件包中的 aoss 工具启动。使用方法是:在要运行的程序的命令行前加上 {{ic|aoss}}。例如:<br />
aoss realplay<br />
<br />
pcm.!default{ ... } 貌似已经不管用了。但下面的设置有效:<br />
pcm.default pcm.dmixer<br />
<br />
===特定型号的配置===<br />
ALSA 会通过 BIOS 检测[http://www.kernel.org/doc/Documentation/sound/alsa/HD-Audio-Models.txt 声卡型号],少数情况下也可能无法正确识别。声卡芯片类型可以在 {{ic|alsamixer}} 界面上找到(如 ALC662),而型号则要设置在 {{ic|/etc/modprobe.d/modprobe.conf}} 或 {{ic|/etc/modprobe.d/sound.conf}} 文件中:<br />
<br />
options snd-hda-intel model=<型号><br />
<br />
具体设置因声卡型号而异。多数情况下不需要特地这样做。如果想知道具体声卡型号的设置,请查阅 [http://bugtrack.alsa-project.org/main/index.php/Matrix:Main Alsa Soundcard List],找到你的声卡型号,点击“Details”,然后查看“Setting up modprobe...”小节,将其中的内容插入 {{ic|/etc/modprobe.d/modprobe.conf}} 文件即可。例如,Intel AC97 的配置如下:<br />
<br />
{{bc|# ALSA portion<br />
alias char-major-116 snd<br />
alias snd-card-0 snd-intel8x0<br />
# module options should go here<br />
<br />
# OSS/Free portion<br />
alias char-major-14 soundcore<br />
alias sound-slot-0 snd-card-0<br />
<br />
# card #1<br />
alias sound-service-0-0 snd-mixer-oss<br />
alias sound-service-0-1 snd-seq-oss<br />
alias sound-service-0-3 snd-pcm-oss<br />
alias sound-service-0-8 snd-seq-oss<br />
alias sound-service-0-12 snd-pcm-oss}}<br />
<br />
===与蜂鸣器冲突===<br />
如果你确认所有声道都已解除静音,声卡驱动配置正确,且音量大小也合适,但还是听不到声音,那么请试试将下面的内容添加到 {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
(如果是 {{ic|via82xx}} 驱动)<br />
options snd-<模块名请自行补充> ac97_quirk=0<br />
<br />
(如果是 {{ic|snd_intel8x0}} 驱动)<br />
options snd-NAME-OF-MODULE ac97_quirk=1<br />
<br />
===麦克风输入无声音===<br />
In alsamixer, make sure that all the volume levels are up under recording, and that CAPTURE is toggled active on the microphone (e.g. Mic, Internal Mic) and/or on Capture (in alsamixer, select these items and press space). Try making positive Mic Boost and raising Capture and Digital levels higher; this make make static or distortion, but then you can adjust them back down once you are hearing ''something'' when you record<br />
<br />
As the pulseaudio wrapper is shown as "default" in alsamixer, you may have to press F6 to select your actual soundcard first. You may also need to enable and increase the volume of Line-in in the Playback section.<br />
<br />
To test the microphone, run these commands (see arecord's man page for further information):<br />
arecord -d 5 test-mic.wav<br />
aplay test-mic.wav<br />
<br />
If all fails, you may want to eliminate hardware failure by testing the microphone with a different device.<br />
<br />
For at least some computers, muting a microphone (MM) simply means its input does not go immediately to the speakers. It still receives input.<br />
<br />
Many Dell laptops need "-dmic" to be appended to the model name in {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-hda-intel model=dell-m6-dmic<br />
<br />
Some programs use try to use OSS as the main input software. Add the following lines to {{ic|/etc/modprobe.d/modprobe.conf}} to prevent OSS modules from being loaded:<br />
<br />
{{Note|The OSS modules are no longer autoloaded anyway.}}<br />
<br />
blacklist snd_pcm_oss<br />
blacklist snd_mixer_oss<br />
blacklist snd_seq_oss<br />
<br />
See also:<br />
* http://www.alsa-project.org/main/index.php/SoundcardTesting<br />
* http://alsa.opensrc.org/Record_from_mic<br />
<br />
===设置默认的麦克风/录音设备===<br />
某些程序(Pidgin、Adobe Flash)提供了设置录音设备的选项。如果录音设备不是内置的声卡,而是外接的(如USB摄像头、麦克风),就会有些问题。如果只想更改默认的录音设备、不更改回放设备,需要像下面这样设置 {{ic|~/.asoundrc}}:<br />
<br />
pcm.usb<br />
{<br />
type hw<br />
card U0x46d0x81d<br />
}<br />
<br />
pcm.!default<br />
{<br />
type asym<br />
playback.pcm<br />
{<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
capture.pcm<br />
{<br />
type plug<br />
slave.pcm "usb"<br />
}<br />
}<br />
<br />
把“U0x46d0x81d”替换为你的录音设备的名称。可以通过 {{ic|arecord -L}} 查看所有 ALSA 识别到的录音设备。<br />
<br />
===内置麦克风无效===<br />
首先,请检查 alsamixer,确保所有的录音声道的音量都达到可以录制的程度。如果还是不行,试试修改 /etc/sound.conf 并重载 snd-* 模块。这样就会多出来一个名叫 Capture 的音频控制,可以用它调节内置麦克风的音量。例如,对于 snd-hda-intel 模块,配置如下:<br />
<br />
options snd-hda-intel enable_msi=1<br />
<br />
然后,输入下面的命令重载模块,调整音量并测试。<br />
<br />
{{bc|# rmmod snd-hda-intel && modprobe snd-hda-intel}}<br />
<br />
===Intel 板载声卡无声===<br />
可能由两个相互冲突的驱动模块引起,分别是 {{ic|snd_intel8x0}} 和 {{ic|snd_intel8x0m}}。如果是的话,屏蔽 snd_intel8x0m 即可:<br />
<br />
{{hc|/etc/modprobe.d/modprobe.conf|blacklist snd_intel8x0m}}<br />
<br />
以下方法可能也有效:在 {{ic|alsamixer}} 或 {{ic|amixer}} 把 “External Amplifier” 设置为静音。参见:[http://alsa.opensrc.org/Intel8x0#Dell_Inspiron_8600_.28and_probably_others.29 ALSA wiki 相关条目]。<br />
<br />
也可以试试解除“Mix”的静音。<br />
<br />
===Intel 板载声卡耳机无声===<br />
对于笔记本电脑上的 '''Intel Corporation 82801 I (ICH9 Family) HD Audio Controller''' 声卡,可能需要添加如下内容到 modprobe 配置文件或 sound.conf,以解决耳机无声的问题:<br />
<br />
options snd-hda-intel model=$model<br />
<br />
$model 在下列型号中选择:<br />
<br />
* dell-vostro<br />
* olpc-xo-1_5<br />
* laptop<br />
* dell-m6<br />
* laptop-hpsense<br />
<br />
{{注意|可能需要在配置文件的 "alias" 语句之后添加上述内容。}}<br />
<br />
可以在内核文档中找到所有支持的型号,如[http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.35.y.git;a=blob;f=Documentation/sound/alsa/HD-Audio-Models.txt;h=dc25bb84b83b49665a7ed850e7bf5423d50cd3ba;hb=HEAD 该文]。这是 2.6.35 版本内核的文档,你需要自己查找适用于自己内核版本的文档。<br />
<br />
也可以从[http://www.mjmwired.net/kernel/Documentation/sound/alsa/HD-Audio-Models.txt 这里]获取型号列表。要查看你的声卡芯片名称,输入下列命令(其中的“*”请根据实际情况替换)。注意,有些芯片名称被更改过,因此实际名称与文件中的可能不匹配。<br />
<br />
cat /proc/asound/card*/codec* | grep Codec<br />
<br />
注意,这么做很可能导致所有输入设备(内置和外置麦克风)无法工作,即要么让麦克风正常、要么让耳机正常。如果遇到这一问题,请向 ALSA 开发者报告。<br />
<br />
另外,如果蜂鸣器(pcspkr)出现问题,可以试试使用以下配置修复:<br />
<br />
options snd-hda-intel model=$model enable=1 index=0<br />
<br />
===No sound when S/PDIF video card is installed===<br />
Discover available modules and their order:<br />
$ cat /proc/asound/modules<br />
0 snd_hda_intel<br />
1 snd_ca0106<br />
<br />
Disable the undesired video card audio codec in {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
# /etc/modprobe.d/modprobe.conf<br />
#<br />
install snd_hda_intel /bin/false<br />
<br />
If both devices use the same module, it might be possible to disable one of them in the BIOS.<br />
<br />
===Poor sound quality or clipping===<br />
If you experience poor sound quality, try setting the PCM volume (in alsamixer) to a level such that gain is 0.<br />
<br />
If snd-usb-audio driver has been loaded, you could try to enable {{ic|softvol}} in '''/etc/asound.conf''' file. Example configuration for the first audio device:<br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.dsnooper {<br />
type dsnoop<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
channels 2<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.softvol {<br />
type softvol<br />
slave { pcm "dmixer" }<br />
control {<br />
name "Master"<br />
card 0<br />
}<br />
}<br />
ctl.!default {<br />
type hw<br />
card 0<br />
}<br />
ctl.softvol {<br />
type hw<br />
card 0<br />
}<br />
ctl.dmixer {<br />
type hw<br />
card 0<br />
}<br />
<br />
===Pops When Starting and Stopping Playback===<br />
Some modules (e.g. snd_ac97_codec and snd_hda_intel) can power off your sound card when not in use. This can make an audible noise (like a crack/pop/scratch) when turning on/off your sound card. Sometimes even when move the slider volume, or open and close windows (KDE4). If you find this annoying try {{ic|modinfo snd_MY_MODULE}}, and look for a module option that adjusts or disables this feature.<br />
<br />
Example: to disable the power saving mode and solve cracking sound trough speakers issue, using snd_hda_intel add in {{ic|/etc/modprobe.d/modprobe.conf}}<br />
options snd_hda_intel power_save=0<br />
or<br />
options snd_hda_intel power_save=0 power_save_controller=N<br />
<br />
You can also try it with {{ic|1=modprobe snd_hda_intel power_save=0}} before.<br />
<br />
You may also have to unmute the 'Line' ALSA channel for this to work. Any value will do (other than '0' or something too high).<br />
<br />
''Example:'' on an onboard VIA VT1708S (using the snd_hda_intel module) these cracks occured even though 'power_save' was set to 0. Unmuting the 'Line' channel and setting a value of '1' solved the issue.<br />
<br />
Source: https://www.kernel.org/doc/Documentation/sound/alsa/powersave.txt<br />
<br />
If you use a laptop, pm-utils will change {{ic|power_save}} back to 1 when you go onto battery power even if you disable power saving in {{ic|/etc/modprobe.d}}. Disable this for pm-utils by disabling the script that makes the change (see [https://wiki.archlinux.org/index.php/Pm-utils#Disabling_a_hook Disabling a hook] for more information):<br />
# touch /etc/pm/power.d/intel-audio-powersave<br />
<br />
===S/PDIF Output Does Not Work===<br />
<br />
If the optical/coaxial digital output of your motherboard/sound card is not working or stopped working, and have already enabled and unmuted it in alsamixer, try running<br />
# iecset audio on<br />
<br />
as root.<br />
<br />
You can also put this command in {{Ic|rc.local}} as it sometimes it may stop working after a reboot.<br />
<br />
===HDMI Output Does Not Work===<br />
If the HDMI output of your motherboard/sound card is not working or stopped working, and have already enabled and unmuted it in alsamixer, try the following.<br />
<br />
Query for Playback Devices:<br />
<br />
$ aplay -l<br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: NVidia [HDA NVidia], device 0: ALC1200 Analog [ALC1200 Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 1: ALC1200 Digital [ALC1200 Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 3: NVIDIA HDMI [NVIDIA HDMI]<br />
Subdevices: 0/1<br />
Subdevice #0: subdevice #0<br />
<br />
Now that we have the info for the HDMI device, try a test, In the example below, 0 is the card number and 3 is the device number.<br />
<br />
$ aplay -D plughw:0,3 /usr/share/sounds/alsa/Front_Center.wav<br />
<br />
If aplay does not output any errors, but still no sound is heared, "reboot" the receiver, monitor or tv set. Since the HDMI interface executes a handshake on connection, it might have noticed before that there was no audio stream embedded, and disabled audio decoding. In particular, if you are using a standalone window manager (don’t know about Gnome or KDE), you may need to have some sound playing ''while'' plugging in the HDMI cable.<br />
<br />
{{Note|If you are using an ATI card and linux 3.0, check your kernel/grub settings, you most likely need to use the ''''radeon.audio'''<nowiki>=</nowiki>'''1'''' boot parameter, or alternatively set up the option in your modprobe configuration files. It's important to note that before trying, is to check the [http://www.x.org/wiki/RadeonFeature#fndef-72849d6d6eb3927d486f50ece74a739042965a6c-25 Radeon Feature Matrix] to see if it's actually supported. At the moment this is written (march 29th, 2013), some chipsets of the [http://www.x.org/wiki/RadeonFeature#Decoder_ring_for_engineering_vs_marketing_names S. Islands] (namely HD7750 through HD7970) doesn't (still) support HDMI Audio.}}<br />
<br />
If the test is successful, create or edit your ~/.asoundrc file to set HDMI as the default audio device.<br />
<br />
{{hc|~/.asoundrc|<br />
pcm.!default {<br />
type hw<br />
card 0<br />
device 3<br />
}<br />
}}<br />
<br />
Or you above config does not work try:<br />
{{hc|~/.asoundrc|<br />
defaults.pcm.card 0<br />
defaults.pcm.device 3<br />
defaults.ctl.card 0<br />
}}<br />
<br />
===HDMI Multi-channel PCM output does not work (Intel) ===<br />
As of Linux 3.1 multi-channel PCM output through HDMI with a Intel card (Intel Eaglelake, IbexPeak/Ironlake,SandyBridge/CougarPoint and IvyBridge/PantherPoint) is not yet supported. Support for it has been recently added and expected to be available in Linux 3.2. To make it work in Linux 3.1 you need to apply the following patches:<br />
<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=76adaa34db407f174dd06370cb60f6029c33b465 drm: support routines for HDMI/DP ELD]<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=e0dac65ed45e72fe34cc7ccc76de0ba220bd38bb drm/i915: pass ELD to HDMI/DP audio driver]<br />
<br />
===HP TX2500===<br />
Add these 2 lines into {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-cmipci mpu_port=0x330 fm_port=0x388<br />
options snd-hda-intel index=0 model=toshiba position_fix=1<br />
<br />
options snd-hda-intel model=hp (works for tx2000cto)<br />
<br />
===Skipping Sound When Playing MP3===<br />
If you have sound skipping when playing MP3 files and you have more then 2 speakers attached to your computer (i.e. > 2 speaker system), run alsamixer and disable the channels for the speakers that you '''DO NOT''' have (i.e. do not enable the sound for the center speaker if you do not have a center speaker.<br />
<br />
===Using a USB Headset and External USB Sound Cards===<br />
If you are using a USB headset with ALSA you can try using {{AUR|asoundconf}} (currently only available from the [[AUR]]) to set the headset as the primary sound output. Before running make sure you have usb audio module enabled ({{ic|modprobe snd-usb-audio}}).<br />
<br />
# asoundconf is-active<br />
# asoundconf list<br />
# asoundconf set-default-card <chosen soundcard><br />
<br />
==== Crackling sound with USB devices ====<br />
If you experience crackling sound on USB devices, you can try tuning the snd-usb-audio for minimal latency.<br />
<br />
Add this to your {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
options snd-usb-audio nrpacks=1<br />
<br />
source: http://alsa.opensrc.org/Usb-audio#Tuning_USB_devices_for_minimal_latencies<br />
<br />
==== Hot-plugging a USB Sound Card ====<br />
In order to automatically make a USB Sound Card the primary output device, when the card is plugged in, you can use the following udev rules (e.g. add the following two lines to {{ic|/etc/udev/rules.d/00-local.rules}} and reboot).<br />
<br />
{{bc|1=KERNEL=="pcmC[D0-9cp]*", ACTION=="add", PROGRAM="/bin/sh -c 'K=%k; K=$${K#pcmC}; K=$${K%%D*}; echo defaults.ctl.card $$K > /etc/asound.conf; echo defaults.pcm.card $$K >>/etc/asound.conf'"<br />
KERNEL=="pcmC[D0-9cp]*", ACTION=="remove", PROGRAM="/bin/sh -c 'echo defaults.ctl.card 0 > /etc/asound.conf; echo defaults.pcm.card 0 >>/etc/asound.conf'"}}<br />
<br />
===Error 'Unknown hardware' Appears After a Kernel Update===<br />
The following messages may be displayed during the start-up ALSA after the kernel update:<br />
Unknown hardware "foo" "bar" ...<br />
Hardware is initialized using a guess method<br />
/usr/sbin/alsactl: set_control:nnnn:failed to obtain info for control #mm (No such file or directory)<br />
<br />
or<br />
Found hardware: "HDA-Intel" "VIA VT1705" "HDA:11064397,18490397,00100000" "0x1849" "0x0397"<br />
Hardware is initialized using a generic method<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #1 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #2 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #25 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #26 (No such file or directory)<br />
<br />
Simply store ALSA mixer settings again (as root):<br />
# alsactl -f /var/lib/alsa/asound.state store<br />
<br />
It may be necessary configure ALSA again with alsamixer<br />
<br />
===HDA Analyzer===<br />
If the mappings to your audio pins(plugs) do not correspond but ALSA works fine, you could try HDA Analyzer -- a pyGTK2 GUI for HD-audio control can be found [http://www.alsa-project.org/main/index.php/HDA_Analyzer at the ALSA wiki].<br />
Try tweaking the Widget Control section of the PIN nodes, to make microphones IN and headphone jacks OUT. Referring to the Config Defaults heading is a good idea.<br />
<br />
NOTE: the script is done by such way that it is incompatible with python3 (which is now shipped with ArchLinux) but tries to use it.<br />
The workaround is: open "run.py", find all occurences of "python" (2 occurences - one on the first line, and the second on the last line) and replace them all by "python2".<br />
<br />
NOTE2: the script requires root acces, but running it via su/sudo is bogus. Run it via {{ic|kdesu}} or {{ic|gksu}}.<br />
<br />
===ALSA with SDL===<br />
If you get no sound via SDL and ALSA cannot be chosen from the application. Try setting the environmental variable SDL_AUDIODRIVER to alsa.<br />
# export SDL_AUDIODRIVER=alsa<br />
<br />
===Low Sound Workaround===<br />
<br />
If you are facing low sound even after maxing out your speakers/headphones, you can give the softvol plugin a try. Add the following to {{ic|/etc/asound.conf}}.<br />
{{bc|<nowiki><br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
<br />
pcm.softvol {<br />
type softvol<br />
slave {<br />
pcm "dmix"<br />
}<br />
control {<br />
name "Pre-Amp"<br />
card 0<br />
}<br />
min_dB -5.0<br />
max_dB 20.0<br />
resolution 6<br />
}<br />
</nowiki>}}<br />
{{note| You will probably have to restart the computer, as restarting the alsa daemon did not load the new configuration for me. Also, if the configuration does not work even after restarting, try changing {{ic|plug}} with {{ic|hw}} in the above configuration.}}<br />
<br />
After the changes are loaded successfully, you will see a {{ic|Pre-Amp}} section in alsamixer. You can adjust the levels there.<br />
{{note|Setting a high value for {{ic|Pre-Amp}} can cause sound distortion, so adjust it according to the level that suits you.}}<br />
<br />
===Popping sound after resuming from suspension===<br />
You might hear a popping sound after resuming the computer from suspension. This can be fixed by editing {{ic|/etc/pm/sleep.d/90alsa}} and removing the line that says {{ic|aplay -d 1 /dev/zero}}<br />
<br />
==设置文件范例==<br />
参见:[[Advanced Linux Sound Architecture/Example Configurations]]。<br />
<br />
==相关阅读==<br />
* [http://www.mjmwired.net/kernel/Documentation/sound/alsa/ALSA-Configuration.txt ALSA 模块高级配置]<br />
* [http://alsa.opensrc.org/Main_Page 非官方的 ALSA Wiki]<br />
* [https://bbs.archlinux.org/viewtopic.php?id=36815 如何从 svn 获取驱动代码并编译]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Advanced_Linux_Sound_Architecture_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=255078
Advanced Linux Sound Architecture (简体中文)
2013-04-25T02:45:23Z
<p>Cuihao: /* 内置麦克风无效 */</p>
<hr />
<div>[[Category:简体中文]]<br />
[[Category:Sound (简体中文)]]<br />
[[Category:Audio/Video (简体中文)]]<br />
[[cs:Advanced Linux Sound Architecture]]<br />
[[de:Alsa]]<br />
[[es:Advanced Linux Sound Architecture]]<br />
[[fa:ALSA]]<br />
[[fr:Alsa]]<br />
[[he:Advanced Linux Sound Architecture]]<br />
[[it:Advanced Linux Sound Architecture]]<br />
[[ja:Advanced Linux Sound Architecture]]<br />
[[nl:Advanced Linux Sound Architecture]]<br />
[[pt:Advanced Linux Sound Architecture]]<br />
[[ru:Advanced Linux Sound Architecture]]<br />
[[sk:Advanced Linux Sound Architecture]]<br />
[[th:Advanced Linux Sound Architecture]]<br />
[[tr:Gelişmiş_ses_mimarisi_(ALSA)]]<br />
[[en:Advanced Linux Sound Architecture]]<br />
<br />
{{Article summary start}}<br />
{{Article summary text|ALSA 的使用、配置及疑难解答。}}<br />
{{Article summary heading|相关页面}}<br />
{{Article summary wiki|Sound system (简体中文)}}<br />
{{Article summary wiki|Disable PC Speaker Beep (简体中文)}}<br />
{{Article summary end}}<br />
{{TranslationStatus (简体中文)|Advanced_Linux_Sound_Architecture|2013-04-25|254584}}<br />
<br />
[[Wikipedia:zh:ALSA|高级 Linux 声音体系]](Advanced Linux Sound Architecture,'''ALSA''')是Linux中提供声音设备驱动的内核组件,用来代替原来的开放声音系统(Open Sound System,OSSv3)。除了声音设备驱动,'''ALSA'''还包含一个用户空间的函数库,以方便开发者通过高级API使用驱动功能,而不必直接与内核驱动交互。<br />
<br />
{{注意|关于另一种声音体系,请阅读[[Open Sound System (简体中文)|开放声音系统(OSS)]]的页面。}}<br />
<br />
==安装==<br />
Arch 默认的内核已经通过一套模块提供了 ALSA,不必特别安装。<br />
<br />
[[udev (简体中文)|udev]]会在系统启动时自动检测硬件,并加载相应的声音设备驱动模块。这时,你的声卡已经可以工作了,只是所有声道默认都被设置成静音了。<br />
<br />
在本地登录(通过虚拟终端或登录管理器)的用户,都有权限播放音频并调整音量。要让远程登录的用户拥有这些权限,必须把用户[[Users and Groups (简体中文)#用户组管理|加入]] {{ic|audio}} 用户组。该组的成员可以直接访问声音设备,会导致某些程序独占音频输出(破坏软件混音),还可能影响用户快速切换和拖机(multiseat)。因此,除非真的有某些[https://wiki.ubuntu.com/Audio/TheAudioGroup 特殊需求],'''不建议'''把用户加入 {{ic|audio}} 用户组。<br />
<br />
===用户空间工具===<br />
从 [[Official Repositories (简体中文)|官方仓库]]:<br />
* [[pacman (简体中文)|安装]] {{Pkg|alsa-utils}} 软件包,其中的 {{ic|alsamixer}} 工具允许用户在控制台或终端中配置声音设备。<br />
* 安装 {{Pkg|alsa-oss}} 软件包,如果想让 OSS 应用程序与dmix(软件混音)协同工作。 <br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。}}<br />
*安装 {{Pkg|alsa-plugins}} 软件包,如果需要 [[#高质量重采样|高质量重采样]]、[[#Upmixing/Downmixing|软件模拟环绕立体声]]和其他高级特性。<br />
<br />
==解除各声道的静音==<br />
目前版本的 ALSA 安装后,所有声道'''默认是静音的''',必须手动解除。<br />
<br />
使用 {{ic|alsamixer}} 的 ncurses 界面,配置十分简单:<br />
<br />
$ alsamixer<br />
<br />
此外,还可以在命令行下使用 {{ic|amixer}}:<br />
<br />
$ amixer sset Master unmute<br />
<br />
在 {{ic|alsamixer}} 中,下方标有 {{ic|MM}} 的声道是静音的,而标有 {{ic|00}} 的通道已经启用。<br />
<br />
使用左右方向键,选中 {{ic|Master}} 和 {{ic|PCM}} 声道。按下 {{keypress|m}} 键解除静音。使用上方向键增加音量,直到增益值为{{ic|0}}。该值显示在左上方 {{ic|Item:}} 字段后。过高的增益值会导致声音失真。<br />
<br />
要想得到完整的 5.1 或 7.1 环绕立体声,还得解除 Front、Surround、Center、LFE (subwoofer) 和 Side 这些声道的静音(上述名称是 Intel HD Audio 声卡使用的声道名,可能因设备不同而有所差异)。注意,仅有这些设置,系统不会自动将立体声源(多数音乐)提升(upmix)成环绕立体声。如果需要这些功能,请阅读[[#Upmixing/Downmixing]]。<br />
<br />
要启用麦克风,切换至 Capture 选项卡,按下 {{keypress|F4}},按下 {{keypress|空格}} 启用其中一个声道即可。<br />
<br />
按下 {{Keypress|Esc}} 键退出 alsamixer。<br />
<br />
{{注意|<br />
* 有一些声卡,需要关闭数字输出或将其调成静音,才能输出模拟音频信号。对于 Soundblaster Audigy LS 声卡,需要把 IEC958 通道设成静音。 <br />
* 有些机器(如 Thinkpad T61),有一个 Speaker 通道,必须按照上述方法打开调整。<br />
* 有些机器(如 Dell E6400),可能还需要把 {{ic|Front}} 和 {{ic|Headphone}} 声道打开调整。}}<br />
<br />
接下来,测试声卡是否工作:<br />
<br />
$ speaker-test -c 2<br />
<br />
根据扬声器的实际情况,调整 -c 后的数值。对于 7.1 声道,这个数字是 8:<br />
<br />
$ speaker-test -c 8<br />
<br />
如果没有正常工作,请继续阅读 [[#配置]] 以及 [[#疑难解答]] 部分。<br />
<br />
{{pkg|alsa-utils}} 软件包提供了两个配置 alsa 的服务:{{ic|alsa-restore.service}} 和 {{ic|alsa-store.service}}。它们分别在开机和关机时自动运行。<br />
<br />
==配置==<br />
=== VirtualBox 虚拟机无声音 ===<br />
<br />
如果使用 VirtualBox 时碰上了麻烦,请尝试以下命令:<br />
<br />
{{hc|$ alsactl init|2=<nowiki><br />
<br />
Found hardware: "ICH" "SigmaTel STAC9700,83,84" "AC97a:83847600" "0x8086" "0x0000"<br />
Hardware is initialized using a generic method<br />
</nowiki><br />
}}<br />
<br />
如果还是不行,请检查所使用的音频软件是否激活了 ALSA 输出。<br />
<br />
===设置默认声卡===<br />
<br />
如果发现开机时声卡次序会发生变化,可以在通过 {{ic|/etc/modprobe.d}} 中的 {{ic|.conf}} 文件(比如 {{ic|/etc/modprobe.d/alsa-base.conf}})手动设置次序。<br />
比如,要让 mia 声卡成为 #0、Intel HDA 声卡成为 #1:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<br />
options snd slots=snd_mia,snd_hda_intel<br />
options snd_mia index=0<br />
options snd_hda_intel index=1<br />
}}<br />
<br />
使用 {{ic|<nowiki>lsmod | grep snd</nowiki>}} 获取设备列表。<br />
<br />
snd_mia 和 snd_hda_intel 分别是两个声卡的内核驱动模块。该配置仅适用于同时装有 mia 声卡和使用 snd_hda_intel 驱动的声卡(通常是板载声卡)的情况。<br />
<br />
如果使用 -2 的 index 值,ALSA 就不会将对应的设备作为主声卡使用。Linux Mint 和 Ubuntu 等发行版使用了以下配置,避免 USB 声卡和其他“非主流”设备变成 index 为 0 的主声卡:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<br />
options bt87x index=-2<br />
options cx88_alsa index=-2<br />
options saa7134-alsa index=-2<br />
options snd-atiixp-modem index=-2<br />
options snd-intel8x0m index=-2<br />
options snd-via82xx-modem index=-2<br />
options snd-usb-audio index=-2<br />
options snd-usb-caiaq index=-2<br />
options snd-usb-ua101 index=-2<br />
options snd-usb-us122l index=-2<br />
options snd-usb-usx2y index=-2<br />
# Keep snd-pcsp from being loaded as first soundcard<br />
options snd-pcsp index=-2<br />
# Keep snd-usb-audio from beeing loaded as first soundcard<br />
options snd-usb-audio index=-2<br />
}}<br />
<br />
以上配置会在系统重启后生效。<br />
<br />
{{Out of date|据英文 Wiki 用户反应,{{ic|/etc/asound.conf}} 这个配置文件默认是不存在的,下列操作可能有误。}}<br />
<br />
首先,运行 {{ic|aplay -l}},获取声卡的声卡ID和设备ID:<br />
<br />
{{hc|$ aplay -l|2=<nowiki><br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: Intel [HDA Intel], device 0: CONEXANT Analog [CONEXANT Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: Intel [HDA Intel], device 1: Conexant Digital [Conexant Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 1: JamLab [JamLab], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 2: Audio [Altec Lansing XT1 - USB Audio], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
</nowiki>}}<br />
<br />
拿输出中的最后一项来说,这个声卡的声卡ID为2,设备ID为0。若要把这块声卡作为默认声卡,可以把下列配置添加到系统级别的 {{ic|/etc/asound.conf}} 或用户级别的 {{ic|~/.asoundrc}} 文件。如果文件不存在,可以手动创建。其中的各个ID,请根据实际情况调整:<br />
{{bc|<br />
defaults.pcm.card 2<br />
defaults.pcm.device 0<br />
defaults.ctl.card 2<br />
}}<br />
“pcm”选项决定用来播放音频的设备,而“ctl”选项决定那个声卡能够由控制工具(如 alsamixer)使用。<br />
<br />
上述配置在重启音频程序(如 mplayer)后立即生效。<br />
<br />
===确认所有声音模块都已经加载===<br />
<br />
一般 udev 都会自动识别出声卡。使用以下命令确认:<br />
<br />
{{hc|<nowiki>$ lsmod | grep '^snd' | column -t</nowiki>|<nowiki><br />
snd_hda_codec_hdmi 22378 4<br />
snd_hda_codec_realtek 294191 1<br />
snd_hda_intel 21738 1<br />
snd_hda_codec 73739 3 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel<br />
snd_hwdep 6134 1 snd_hda_codec<br />
snd_pcm 71032 3 snd_hda_codec_hdmi,snd_hda_intel,snd_hda_codec<br />
snd_timer 18992 1 snd_pcm<br />
snd 55132 9 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel,snd_hda_codec,snd_hwdep,snd_pcm,snd_timer<br />
snd_page_alloc 7017 2 snd_hda_intel,snd_pcm<br />
</nowiki>}}<br />
<br />
如果你的输出和上面类似,那就说明声卡已经被正确识别。<br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。如有需要,请[[Kernel_modules_(简体中文)#装入|手动加载]]。}}<br />
<br />
还可以检查一下 {{ic|/dev/snd/}} 目录,看看是否有这些设备文件:<br />
<br />
{{hc|$ ls -l /dev/snd|2=<nowiki><br />
total 0<br />
crw-rw---- 1 root audio 116, 0 Apr 8 14:17 controlC0<br />
crw-rw---- 1 root audio 116, 32 Apr 8 14:17 controlC1<br />
crw-rw---- 1 root audio 116, 24 Apr 8 14:17 pcmC0D0c<br />
crw-rw---- 1 root audio 116, 16 Apr 8 14:17 pcmC0D0p<br />
crw-rw---- 1 root audio 116, 25 Apr 8 14:17 pcmC0D1c<br />
crw-rw---- 1 root audio 116, 56 Apr 8 14:17 pcmC1D0c<br />
crw-rw---- 1 root audio 116, 48 Apr 8 14:17 pcmC1D0p<br />
crw-rw---- 1 root audio 116, 1 Apr 8 14:17 seq<br />
crw-rw---- 1 root audio 116, 33 Apr 8 14:17 timer<br />
</nowiki>}}<br />
<br />
{{注意|在 IRC 或论坛寻求这方面帮助时,别忘了贴出上面几个命令的输出。}}<br />
<br />
如果你的输出跟上面类似,或至少有 '''controlC0''' 和 '''pcmC0D0p''',那么声卡就已经正常加载了。<br />
<br />
如果出现问题,声卡模块没有正确加载,那么请尝试手动加载模块:<br />
<br />
* 确定声卡对应的驱动模块:[http://www.alsa-project.org/main/index.php/Matrix:Main ALSA Soundcard Matrix]。这些模块都会有一个”snd-“前缀(例如:{{ic|snd-via82xx}})。<br />
* [[Kernel_modules_(简体中文)#装入|加载模块]]。<br />
* 检查 {{ic|/dev/snd}} 目录中的设备文件(参见上文);或者,检查 {{ic|alsamixer}} 或 {{ic|amixer}} 的输出是否正确。<br />
* 修改配置,使 {{ic|snd-<模块名>}} 和 {{ic|snd-pcm-oss}} 模块[[Kernel_modules_(简体中文)#装入|开机自动加载]]。<br />
<br />
===启用 SPDIF 输出===<br />
<br />
(由 Gentoo 论坛的 gralves 提供)<br />
* 在 GNOME 音量控制的选项(Options)标签中,把 IEC958 改成 PCM。该选项可以在首选项中启用。<br />
* 不使用 GNOME 音量控制工具的话:<br />
** 编辑 {{ic|/var/lib/alsa/asound.state}}。该文件是 alsasound 保存混音器设置的地方。<br />
** 找到这一行:“IEC958 Playback Switch”。该行上下应该有一行内容为 {{ic|value:false}},将其改为 {{ic|value:true}}。<br />
** 再找到这一行:”IEC958 Playback AC97-SPSA“。把对应的值改为 0。<br />
** 重启 ALSA。<br />
<br />
{{Out of date|rc.local 在 systemd 中已经弃用,以下方法可能不再适用。}}<br />
<br />
还有另外一个开机自动启用 SPDIF 输出的方法(在 SoundBlaster Audigy 声卡上测试过):<br />
<br />
* 在 {{ic|/etc/rc.local}} 中添加如下内容:<br />
# Use COAX-digital output<br />
amixer set 'IEC958 Optical' 100 unmute<br />
amixer set 'Audigy Analog/Digital Output Jack' on<br />
<br />
使用以下命令,获取你的声卡的数字输出的名称:<br />
$ amixer scontrols<br />
<br />
===系统级均衡器===<br />
====使用 AlsaEqual(包含界面)====<br />
从 [[AUR (简体中文)|AUR]] 安装 {{AUR|alsaequal}}。<br />
<br />
{{注意|如果使用的是64位系统,而又安装了32位的 Flash 插件,这里的设置会导致 Flash 无声。如果发生了这种情况,请禁用 alsaequal 或编译32位版本的 alsaequal。}}<br />
<br />
安装后,把下列内容添加到 ALSA 配置文件({{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}}):<br />
<br />
ctl.equal {<br />
type equal;<br />
}<br />
<br />
pcm.plugequal {<br />
type equal;<br />
# Modify the line below if you do not<br />
#slave.pcm "plughw:0,0";<br />
#by default we want to play from more sources at time:<br />
slave.pcm "plug:dmix";<br />
}<br />
#pcm.equal {<br />
# If you do not want the equalizer to be your<br />
# default soundcard comment the following<br />
# line and uncomment the above line. (You can<br />
# choose it as the output device by addressing<br />
# it with specific apps,eg mpg123 -a equal 06.Back_In_Black.mp3)<br />
pcm.!default {<br />
type plug;<br />
slave.pcm plugequal;<br />
}<br />
<br />
设置好后,切换均衡器:<br />
$ alsamixer -D equal<br />
<br />
需要注意,每个用户的 alsaequal 配置文件都是独立的,位于 {{ic|~/.alsaequal.bin}}。因此,如果想在 [[mpd]] 之类的以独立用户身份运行的程序中使用 AlsaEqual,就得像下面这样:<br />
# su mpd -c 'alsamixer -D equal'<br />
或者,在其他用户的主目录下创建符号链接,指向你的 {{ic|.alsaequal.bin}}……<br />
<br />
=====管理 AlsaEqual 配置=====<br />
从 [http://xyne.archlinux.ca/repos/ Xyne 的软件仓库] 或 [https://aur.archlinux.org/packages.php?ID=62420 AUR] 安装 [http://xyne.archlinux.ca/projects/alsaequal-mgr/ alsaequal-mgr]。<br />
<br />
同上,配置均衡器: <br />
$alsamixer -D equal<br />
<br />
如果对效果感到满意,将其命名(如“foo”)并保存该配置:<br />
$alsaequal-mgr save foo<br />
<br />
重新加载“foo”中的配置:<br />
$alsaequal-mgr load foo<br />
<br />
这样,就可以为游戏、电影、不同音乐流派、网络电话等等创建不同的均衡器配置,以便随时切换。<br />
<br />
参见[http://xyne.archlinux.ca/projects/alsaequal-mgr/ 项目主页]和程序的帮助信息,了解更多设置。<br />
<br />
====使用 mbeq====<br />
{{注意|该软件用到了一个 ladspa 插件,可能导致播放音频时 CPU 占用率偏高。In addition, this was made with stereophonic sound (e.g. headphones) in mind.}}<br />
<br />
安装 {{Pkg|alsa-plugins}}、{{Pkg|ladspa}} 和 {{Pkg|swh-plugins}} 软件包。<br />
<br />
* 在 {{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}} 中添加如下内容:<br />
<br />
{{hc|/etc/asound.conf|2=<nowiki>pcm.eq {<br />
type ladspa<br />
<br />
# The output from the EQ can either go direct to a hardware device<br />
# (if you have a hardware mixer, e.g. SBLive/Audigy) or it can go<br />
# to the software mixer shown here.<br />
#slave.pcm "plughw:0,0"<br />
slave.pcm "plug:dmix"<br />
<br />
# Sometimes you may need to specify the path to the plugins,<br />
# especially if you have just installed them. Once you have logged<br />
# out/restarted this should not be necessary, but if you get errors<br />
# about being unable to find plugins, try uncommenting this.<br />
#path "/usr/lib/ladspa"<br />
<br />
plugins [<br />
{<br />
label mbeq<br />
id 1197<br />
input {<br />
#this setting is here by example, edit to your own taste<br />
#bands: 50hz, 100hz, 156hz, 220hz, 311hz, 440hz, 622hz, 880hz, 1250hz, 1750hz, 25000hz,<br />
#50000hz, 10000hz, 20000hz<br />
controls [ -5 -5 -5 -5 -5 -10 -20 -15 -10 -10 -10 -10 -10 -3 -2 ]<br />
}<br />
}<br />
]<br />
}<br />
<br />
# Redirect the default device to go via the EQ - you may want to do<br />
# this last, once you are sure everything is working. Otherwise all<br />
# your audio programs will break/crash if something has gone wrong.<br />
<br />
pcm.!default {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
<br />
# Redirect the OSS emulation through the EQ too (when programs are running through "aoss")<br />
<br />
pcm.dsp0 {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
</nowiki>}}<br />
<br />
* 完成。若有问题,欢迎前往论坛咨询。<br />
<br />
==高质量重采样==<br />
启用软件混音时,ALSA 会强制把所有音频重采样到相同的频率(如果系统支持,默认是 48000)。dmix 的重采样算法很糟糕,会导致显著的质量损失。<br />
<br />
安装 {{pkg|alsa-plugins}} 和 {{pkg|libsamplerate}}。<br />
<br />
将默认的采样率转换器更改为 libsamplerate:<br />
<br />
{{hc|/etc/asound.conf|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
或:<br />
<br />
{{hc|~/.asoundrc|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
'''samplerate_best''' 输出的声音质量最佳,但需要耗费大量CPU资源进行实时重采样。还有其他的算法可供选择(如 '''samplerate''' 等),但比起默认的算法,这些算法的改进并不明显。<br />
<br />
{{警告|某些系统中,启用 samplerate_best 可能导致 Flash 播放器无声音。}}<br />
<br />
==Upmixing/Downmixing==<br />
===Upmixing===<br />
In order for stereo sources like music to be able to saturate a 5.1 or 7.1 sound system, you need to use upmixing. In darker days this used to be tricky and error prone but nowadays plugins exist to easily take care of this task. Thus, install {{Pkg|alsa-plugins}}.<br />
<br />
Then add the following to your ALSA configuration file of choice (either {{ic|/etc/asound.conf}} or {{ic|~/.asoundrc}}):<br />
pcm.upmix71 {<br />
type upmix<br />
slave.pcm "surround71"<br />
delay 15<br />
channels 8<br />
}<br />
<br />
You can easily change this example for 7.1 upmixing to 5.1 or 4.0.<br />
<br />
This adds a new pcm that you can use for upmixing. If you want all sound sources to go through this pcm, add it as a default below the previous definition like so:<br />
<br />
pcm.!default "plug:upmix71"<br />
<br />
The plugin automatically allows multiple sources to play through it without problems so setting is as a default is actually a safe choice. <br />
If this is not working, you have to setup your own dmixer for the upmixing PCM like this: <br />
pcm.dmix6 {<br />
type asym<br />
playback.pcm {<br />
type dmix<br />
ipc_key 567829<br />
slave {<br />
pcm "hw:0,0"<br />
channels 6<br />
}<br />
}<br />
}<br />
<br />
and use "dmix6" instead of "surround71". <br />
If you experience skipping or distorted sound, consider increasing the buffer_size (to 32768, for example) or use a [[#高质量重采样|high quality resampler]].<br />
<br />
===Downmixing===<br />
If you want to downmix sources to stereo because you, for instance, want to watch a movie with 5.1 sound on a stereo system, you need to use the vdownmix plugin that alsa provides in the same package.<br />
<br />
Again, in your configuration file, add this:<br />
<br />
pcm.!surround51 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
pcm.!surround40 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
<br />
==混音==<br />
===软件混音(dmix)===<br />
<br />
{{注意|在 ALSA 1.0.9rc2 及以上版本中,如果声卡不支持硬件混音,模拟输出将默认使用 dmix 混音,无需额外设置。}}<br />
<br />
如果软件混音未正常启用,那么可以手动创建 {{ic|~/.asoundrc}},加入以下内容:<br />
<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
<br />
这样就启用了软件混音,多个程序能够同时使用声卡。<br />
<br />
至于 S/PDIF 之类的数字音频输出,目前的 ALSA 还不会默认启用 dmix。因此,必须像上面这样手动启用。<br />
<br />
常见问题的解答,参见[[#疑难解答]]部分。<br />
<br />
===硬件混音===<br />
====支持====<br />
如果声卡支持,一般无需任何配置即可启用硬件混音。绝大多数板载声卡都不支持硬件混音,必须得使用软件混音(见上文)。很多独立声卡都支持硬件混音,Linux 下支持较好的有:<br />
<br />
* Creative SoundBlaster Live! (5.1 型号)<br />
* Creative SoundBlaster Audigy (部分型号)<br />
* Creative SoundBlaster Audidy 2 (ZS 系列型号)<br />
* Creative SoundBlaster Audigy 4 (Pro 系列型号)<br />
<br />
{{注意|上述声卡的低端版本(Audigy SE、Audigy 2 NX、SoundBlaster Live! 24bit 以及 SoundBlaster Live! 7.1)使用了不同的芯片,'''不支持'''硬件混音。}}<br />
<br />
{{注意|板载 VIA8237 芯片支持4路硬件混音,但在某些主板上只能使用3路(第4路无声),甚至根本无法使用。即使能正常工作,质量也比不上其他混音方法。}}<br />
<br />
====问题修复====<br />
如果你的声卡是 Intel Corporation 82801I (ICH9 Family) HD Audio Controller (rev 02),且使用了64位系统,需要执行以下命令才能在 Enemy Territory 中听到声音:<br />
echo "et.x86 0 0 direct" > /proc/asound/card0/pcm0p/oss<br />
echo "et.x86 0 0 disable" > /proc/asound/card0/pcm0c/oss<br />
<br />
==疑难解答==<br />
===使用CPU频率动态调整时,音频发生跳跃===<br />
使用 {{ic|ondemand}} 或 {{ic|conservative}} 频率策略、启用CPU频率动态调整时,某些声卡和某些 ALSA 驱动会出现声音跳跃的情况。目前,唯一的解决方案是使用固定的频率,如 {{ic|performance}} 性能策略。<br />
<br />
详情参见 [[CPU Frequency Scaling (简体中文)]]。<br />
<br />
===同时只能有一个用户播放音频===<br />
有时会出现一个用户独占软件混音器的情况。多数情况下这没什么影响,但在使用 [[mpd]] 这样以独立用户身份运行的程序时,就会出现问题。当 mpd 运行时,用户自己就无法播放其他音频了。一种解决方法是,以正在使用的用户身份运行 mpd。另一种方法是,在 ALSA 配置文件的 {{ic|pcm.dmixer}} 部分添加 {{ic|ipc_key_add_uid 0}} 禁止独占。以下是 {{ic|asound.conf}} 示例的片段,省略的部分无需修改:<br />
<br />
...<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
slave {<br />
...<br />
<br />
===无法同时播放多个音频===<br />
如果安装了 [[PulseAudio (简体中文)|PulseAudio]](可能作为 [[GNOME (简体中文)|GNOME]] 的依赖被安装),可能出现无法同步播放的问题,因为 PulseAudio 的默认配置是“独占”声卡。ALSA 的用户可能对自己的配置已经很满意、不希望使用 [[PulseAudio (简体中文)|PulseAudio]]。解决方案是编辑 {{ic|/etc/asound.conf}} 并注释掉以下内容:<br />
# Use PulseAudio by default<br />
#pcm.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
# hint {<br />
# show on<br />
# description "Default ALSA Output (currently PulseAudio Sound Server)"<br />
# }<br />
#}<br />
<br />
可能还需要注释掉下面的内容:<br />
#ctl.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
#}<br />
<br />
比起完全卸载 [[PulseAudio (简体中文)|PulseAudio]],这大概是个更简单的方法。<br />
<br />
以下是 {{ic|/etc/asound.conf}} 配置文件的一个示例:<br />
{{bc|<nowiki>pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
}<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}</nowiki><br />
}}<br />
<br />
{{注意|本 {{ic|/etc/asound.conf}} 本来是配合 [[MPD]] 全局配置使用的。参见[[#同时只能有一个用户播放音频|该节]]关于多用户的内容。}}<br />
<br />
===有时候开机无声===<br />
如果会随机地出现开机无声的问题,原因可能是多声卡的顺序发生了变化。如果是这种情况,请参照[[#设置默认声卡]]进行设置。<br />
<br />
对于 mpd 用户,上述配置无效,请参考[http://mpd.wikia.com/wiki/Configuration#ALSA_MPD_software_volume_control 该文]设置。<br />
<br />
===特定程序的问题===<br />
对于不使用系统声音配置的程序,如 XMMS 和 Mplayer,必须特别设置以应用 ALSA 配置。<br />
<br />
对于 mplayer,将以下内容添加到 {{ic|~/.mplayer/config}} (或系统全局配置文件 {{ic|/etc/mplayer/mplayer.conf}}):<br />
ao=alsa<br />
<br />
对于 XMMS 和 Beep Media Player,打开选项窗口,要把声音驱动设置为 ALSA 而不是 OSS。<br />
<br />
XMMS 的设置方法:<br />
* 启动 XMMS<br />
** 选项 -> 首选项<br />
** 选择 Alsa 输出插件<br />
<br />
对于未提供 ALSA 输出的程序,可以使用 alsa-oss 软件包中的 aoss 工具启动。使用方法是:在要运行的程序的命令行前加上 {{ic|aoss}}。例如:<br />
aoss realplay<br />
<br />
pcm.!default{ ... } 貌似已经不管用了。但下面的设置有效:<br />
pcm.default pcm.dmixer<br />
<br />
===特定型号的配置===<br />
ALSA 会通过 BIOS 检测[http://www.kernel.org/doc/Documentation/sound/alsa/HD-Audio-Models.txt 声卡型号],少数情况下也可能无法正确识别。声卡芯片类型可以在 {{ic|alsamixer}} 界面上找到(如 ALC662),而型号则要设置在 {{ic|/etc/modprobe.d/modprobe.conf}} 或 {{ic|/etc/modprobe.d/sound.conf}} 文件中:<br />
<br />
options snd-hda-intel model=<型号><br />
<br />
具体设置因声卡型号而异。多数情况下不需要特地这样做。如果想知道具体声卡型号的设置,请查阅 [http://bugtrack.alsa-project.org/main/index.php/Matrix:Main Alsa Soundcard List],找到你的声卡型号,点击“Details”,然后查看“Setting up modprobe...”小节,将其中的内容插入 {{ic|/etc/modprobe.d/modprobe.conf}} 文件即可。例如,Intel AC97 的配置如下:<br />
<br />
{{bc|# ALSA portion<br />
alias char-major-116 snd<br />
alias snd-card-0 snd-intel8x0<br />
# module options should go here<br />
<br />
# OSS/Free portion<br />
alias char-major-14 soundcore<br />
alias sound-slot-0 snd-card-0<br />
<br />
# card #1<br />
alias sound-service-0-0 snd-mixer-oss<br />
alias sound-service-0-1 snd-seq-oss<br />
alias sound-service-0-3 snd-pcm-oss<br />
alias sound-service-0-8 snd-seq-oss<br />
alias sound-service-0-12 snd-pcm-oss}}<br />
<br />
===与蜂鸣器冲突===<br />
如果你确认所有声道都已解除静音,声卡驱动配置正确,且音量大小也合适,但还是听不到声音,那么请试试将下面的内容添加到 {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
(如果是 {{ic|via82xx}} 驱动)<br />
options snd-<模块名请自行补充> ac97_quirk=0<br />
<br />
(如果是 {{ic|snd_intel8x0}} 驱动)<br />
options snd-NAME-OF-MODULE ac97_quirk=1<br />
<br />
===麦克风输入无声音===<br />
In alsamixer, make sure that all the volume levels are up under recording, and that CAPTURE is toggled active on the microphone (e.g. Mic, Internal Mic) and/or on Capture (in alsamixer, select these items and press space). Try making positive Mic Boost and raising Capture and Digital levels higher; this make make static or distortion, but then you can adjust them back down once you are hearing ''something'' when you record<br />
<br />
As the pulseaudio wrapper is shown as "default" in alsamixer, you may have to press F6 to select your actual soundcard first. You may also need to enable and increase the volume of Line-in in the Playback section.<br />
<br />
To test the microphone, run these commands (see arecord's man page for further information):<br />
arecord -d 5 test-mic.wav<br />
aplay test-mic.wav<br />
<br />
If all fails, you may want to eliminate hardware failure by testing the microphone with a different device.<br />
<br />
For at least some computers, muting a microphone (MM) simply means its input does not go immediately to the speakers. It still receives input.<br />
<br />
Many Dell laptops need "-dmic" to be appended to the model name in {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-hda-intel model=dell-m6-dmic<br />
<br />
Some programs use try to use OSS as the main input software. Add the following lines to {{ic|/etc/modprobe.d/modprobe.conf}} to prevent OSS modules from being loaded:<br />
<br />
{{Note|The OSS modules are no longer autoloaded anyway.}}<br />
<br />
blacklist snd_pcm_oss<br />
blacklist snd_mixer_oss<br />
blacklist snd_seq_oss<br />
<br />
See also:<br />
* http://www.alsa-project.org/main/index.php/SoundcardTesting<br />
* http://alsa.opensrc.org/Record_from_mic<br />
<br />
===设置默认的麦克风/录音设备===<br />
某些程序(Pidgin、Adobe Flash)提供了设置录音设备的选项。如果录音设备不是内置的声卡,而是外接的(如USB摄像头、麦克风),就会有些问题。如果只想更改默认的录音设备、不更改回放设备,需要像下面这样设置 {{ic|~/.asoundrc}}:<br />
<br />
pcm.usb<br />
{<br />
type hw<br />
card U0x46d0x81d<br />
}<br />
<br />
pcm.!default<br />
{<br />
type asym<br />
playback.pcm<br />
{<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
capture.pcm<br />
{<br />
type plug<br />
slave.pcm "usb"<br />
}<br />
}<br />
<br />
把“U0x46d0x81d”替换为你的录音设备的名称。可以通过 {{ic|arecord -L}} 查看所有 ALSA 识别到的录音设备。<br />
<br />
===内置麦克风无效===<br />
首先,请检查 alsamixer,确保所有的录音声道的音量都达到可以录制的程度。如果还是不行,试试修改 /etc/sound.conf 并重载 snd-* 模块。这样就会多出来一个名叫 Capture 的音频控制,可以用它调节内置麦克风的音量。例如,对于 snd-hda-intel 模块,配置如下:<br />
<br />
options snd-hda-intel enable_msi=1<br />
<br />
然后,输入下面的命令重载模块,调整音量并测试。<br />
<br />
{{bc|# rmmod snd-hda-intel && modprobe snd-hda-intel}}<br />
<br />
===Intel 板载声卡无声===<br />
可能由两个相互冲突的驱动模块引起,分别是 {{ic|snd_intel8x0}} 和 {{ic|snd_intel8x0m}}。如果是的话,屏蔽 snd_intel8x0m 即可:<br />
<br />
{{hc|/etc/modprobe.d/modprobe.conf|blacklist snd_intel8x0m}}<br />
<br />
以下方法可能也有效:在 {{ic|alsamixer}} 或 {{ic|amixer}} 把 “External Amplifier” 设置为静音。参见:[http://alsa.opensrc.org/Intel8x0#Dell_Inspiron_8600_.28and_probably_others.29 ALSA wiki 相关条目]。<br />
<br />
也可以试试解除“Mix”的静音。<br />
<br />
===No Headphone Sound with Onboard Intel Sound Card===<br />
With '''Intel Corporation 82801 I (ICH9 Family) HD Audio Controller''' on laptop, you may need to add this line to modprobe or sound.conf:<br />
<br />
options snd-hda-intel model=$model<br />
<br />
Where $model is any one of the following (in order of possibility to work, but not merit):<br />
<br />
* dell-vostro<br />
* olpc-xo-1_5<br />
* laptop<br />
* dell-m6<br />
* laptop-hpsense<br />
<br />
Note: It may be necessary to put this "options" line below (after) any "alias" lines about your card.<br />
<br />
You can see all the available models in the kernel documentation. For example [http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.35.y.git;a=blob;f=Documentation/sound/alsa/HD-Audio-Models.txt;h=dc25bb84b83b49665a7ed850e7bf5423d50cd3ba;hb=HEAD here], but check that it is the correct version of that document for your kernel version.<br />
<br />
A list of available models is also available [http://www.mjmwired.net/kernel/Documentation/sound/alsa/HD-Audio-Models.txt here]. To know your chip name type the following command (with * being corrected to match your files). Note that some chips could have been renamed and do not directly match the available ones in the file.<br />
<br />
cat /proc/asound/card*/codec* | grep Codec<br />
<br />
Note that there is a high chance none of the input devices (all internal and external mics) will work if you choose to do this, so it is either your headphones or your mic. Please report to ALSA if you are affected by this bug.<br />
<br />
And also, if you have problems getting beeps to work (pcspkr):<br />
<br />
options snd-hda-intel model=$model enable=1 index=0<br />
<br />
===No sound when S/PDIF video card is installed===<br />
Discover available modules and their order:<br />
$ cat /proc/asound/modules<br />
0 snd_hda_intel<br />
1 snd_ca0106<br />
<br />
Disable the undesired video card audio codec in {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
# /etc/modprobe.d/modprobe.conf<br />
#<br />
install snd_hda_intel /bin/false<br />
<br />
If both devices use the same module, it might be possible to disable one of them in the BIOS.<br />
<br />
===Poor sound quality or clipping===<br />
If you experience poor sound quality, try setting the PCM volume (in alsamixer) to a level such that gain is 0.<br />
<br />
If snd-usb-audio driver has been loaded, you could try to enable {{ic|softvol}} in '''/etc/asound.conf''' file. Example configuration for the first audio device:<br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.dsnooper {<br />
type dsnoop<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
channels 2<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.softvol {<br />
type softvol<br />
slave { pcm "dmixer" }<br />
control {<br />
name "Master"<br />
card 0<br />
}<br />
}<br />
ctl.!default {<br />
type hw<br />
card 0<br />
}<br />
ctl.softvol {<br />
type hw<br />
card 0<br />
}<br />
ctl.dmixer {<br />
type hw<br />
card 0<br />
}<br />
<br />
===Pops When Starting and Stopping Playback===<br />
Some modules (e.g. snd_ac97_codec and snd_hda_intel) can power off your sound card when not in use. This can make an audible noise (like a crack/pop/scratch) when turning on/off your sound card. Sometimes even when move the slider volume, or open and close windows (KDE4). If you find this annoying try {{ic|modinfo snd_MY_MODULE}}, and look for a module option that adjusts or disables this feature.<br />
<br />
Example: to disable the power saving mode and solve cracking sound trough speakers issue, using snd_hda_intel add in {{ic|/etc/modprobe.d/modprobe.conf}}<br />
options snd_hda_intel power_save=0<br />
or<br />
options snd_hda_intel power_save=0 power_save_controller=N<br />
<br />
You can also try it with {{ic|1=modprobe snd_hda_intel power_save=0}} before.<br />
<br />
You may also have to unmute the 'Line' ALSA channel for this to work. Any value will do (other than '0' or something too high).<br />
<br />
''Example:'' on an onboard VIA VT1708S (using the snd_hda_intel module) these cracks occured even though 'power_save' was set to 0. Unmuting the 'Line' channel and setting a value of '1' solved the issue.<br />
<br />
Source: https://www.kernel.org/doc/Documentation/sound/alsa/powersave.txt<br />
<br />
If you use a laptop, pm-utils will change {{ic|power_save}} back to 1 when you go onto battery power even if you disable power saving in {{ic|/etc/modprobe.d}}. Disable this for pm-utils by disabling the script that makes the change (see [https://wiki.archlinux.org/index.php/Pm-utils#Disabling_a_hook Disabling a hook] for more information):<br />
# touch /etc/pm/power.d/intel-audio-powersave<br />
<br />
===S/PDIF Output Does Not Work===<br />
<br />
If the optical/coaxial digital output of your motherboard/sound card is not working or stopped working, and have already enabled and unmuted it in alsamixer, try running<br />
# iecset audio on<br />
<br />
as root.<br />
<br />
You can also put this command in {{Ic|rc.local}} as it sometimes it may stop working after a reboot.<br />
<br />
===HDMI Output Does Not Work===<br />
If the HDMI output of your motherboard/sound card is not working or stopped working, and have already enabled and unmuted it in alsamixer, try the following.<br />
<br />
Query for Playback Devices:<br />
<br />
$ aplay -l<br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: NVidia [HDA NVidia], device 0: ALC1200 Analog [ALC1200 Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 1: ALC1200 Digital [ALC1200 Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 3: NVIDIA HDMI [NVIDIA HDMI]<br />
Subdevices: 0/1<br />
Subdevice #0: subdevice #0<br />
<br />
Now that we have the info for the HDMI device, try a test, In the example below, 0 is the card number and 3 is the device number.<br />
<br />
$ aplay -D plughw:0,3 /usr/share/sounds/alsa/Front_Center.wav<br />
<br />
If aplay does not output any errors, but still no sound is heared, "reboot" the receiver, monitor or tv set. Since the HDMI interface executes a handshake on connection, it might have noticed before that there was no audio stream embedded, and disabled audio decoding. In particular, if you are using a standalone window manager (don’t know about Gnome or KDE), you may need to have some sound playing ''while'' plugging in the HDMI cable.<br />
<br />
{{Note|If you are using an ATI card and linux 3.0, check your kernel/grub settings, you most likely need to use the ''''radeon.audio'''<nowiki>=</nowiki>'''1'''' boot parameter, or alternatively set up the option in your modprobe configuration files. It's important to note that before trying, is to check the [http://www.x.org/wiki/RadeonFeature#fndef-72849d6d6eb3927d486f50ece74a739042965a6c-25 Radeon Feature Matrix] to see if it's actually supported. At the moment this is written (march 29th, 2013), some chipsets of the [http://www.x.org/wiki/RadeonFeature#Decoder_ring_for_engineering_vs_marketing_names S. Islands] (namely HD7750 through HD7970) doesn't (still) support HDMI Audio.}}<br />
<br />
If the test is successful, create or edit your ~/.asoundrc file to set HDMI as the default audio device.<br />
<br />
{{hc|~/.asoundrc|<br />
pcm.!default {<br />
type hw<br />
card 0<br />
device 3<br />
}<br />
}}<br />
<br />
Or you above config does not work try:<br />
{{hc|~/.asoundrc|<br />
defaults.pcm.card 0<br />
defaults.pcm.device 3<br />
defaults.ctl.card 0<br />
}}<br />
<br />
===HDMI Multi-channel PCM output does not work (Intel) ===<br />
As of Linux 3.1 multi-channel PCM output through HDMI with a Intel card (Intel Eaglelake, IbexPeak/Ironlake,SandyBridge/CougarPoint and IvyBridge/PantherPoint) is not yet supported. Support for it has been recently added and expected to be available in Linux 3.2. To make it work in Linux 3.1 you need to apply the following patches:<br />
<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=76adaa34db407f174dd06370cb60f6029c33b465 drm: support routines for HDMI/DP ELD]<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=e0dac65ed45e72fe34cc7ccc76de0ba220bd38bb drm/i915: pass ELD to HDMI/DP audio driver]<br />
<br />
===HP TX2500===<br />
Add these 2 lines into {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-cmipci mpu_port=0x330 fm_port=0x388<br />
options snd-hda-intel index=0 model=toshiba position_fix=1<br />
<br />
options snd-hda-intel model=hp (works for tx2000cto)<br />
<br />
===Skipping Sound When Playing MP3===<br />
If you have sound skipping when playing MP3 files and you have more then 2 speakers attached to your computer (i.e. > 2 speaker system), run alsamixer and disable the channels for the speakers that you '''DO NOT''' have (i.e. do not enable the sound for the center speaker if you do not have a center speaker.<br />
<br />
===Using a USB Headset and External USB Sound Cards===<br />
If you are using a USB headset with ALSA you can try using {{AUR|asoundconf}} (currently only available from the [[AUR]]) to set the headset as the primary sound output. Before running make sure you have usb audio module enabled ({{ic|modprobe snd-usb-audio}}).<br />
<br />
# asoundconf is-active<br />
# asoundconf list<br />
# asoundconf set-default-card <chosen soundcard><br />
<br />
==== Crackling sound with USB devices ====<br />
If you experience crackling sound on USB devices, you can try tuning the snd-usb-audio for minimal latency.<br />
<br />
Add this to your {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
options snd-usb-audio nrpacks=1<br />
<br />
source: http://alsa.opensrc.org/Usb-audio#Tuning_USB_devices_for_minimal_latencies<br />
<br />
==== Hot-plugging a USB Sound Card ====<br />
In order to automatically make a USB Sound Card the primary output device, when the card is plugged in, you can use the following udev rules (e.g. add the following two lines to {{ic|/etc/udev/rules.d/00-local.rules}} and reboot).<br />
<br />
{{bc|1=KERNEL=="pcmC[D0-9cp]*", ACTION=="add", PROGRAM="/bin/sh -c 'K=%k; K=$${K#pcmC}; K=$${K%%D*}; echo defaults.ctl.card $$K > /etc/asound.conf; echo defaults.pcm.card $$K >>/etc/asound.conf'"<br />
KERNEL=="pcmC[D0-9cp]*", ACTION=="remove", PROGRAM="/bin/sh -c 'echo defaults.ctl.card 0 > /etc/asound.conf; echo defaults.pcm.card 0 >>/etc/asound.conf'"}}<br />
<br />
===Error 'Unknown hardware' Appears After a Kernel Update===<br />
The following messages may be displayed during the start-up ALSA after the kernel update:<br />
Unknown hardware "foo" "bar" ...<br />
Hardware is initialized using a guess method<br />
/usr/sbin/alsactl: set_control:nnnn:failed to obtain info for control #mm (No such file or directory)<br />
<br />
or<br />
Found hardware: "HDA-Intel" "VIA VT1705" "HDA:11064397,18490397,00100000" "0x1849" "0x0397"<br />
Hardware is initialized using a generic method<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #1 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #2 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #25 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #26 (No such file or directory)<br />
<br />
Simply store ALSA mixer settings again (as root):<br />
# alsactl -f /var/lib/alsa/asound.state store<br />
<br />
It may be necessary configure ALSA again with alsamixer<br />
<br />
===HDA Analyzer===<br />
If the mappings to your audio pins(plugs) do not correspond but ALSA works fine, you could try HDA Analyzer -- a pyGTK2 GUI for HD-audio control can be found [http://www.alsa-project.org/main/index.php/HDA_Analyzer at the ALSA wiki].<br />
Try tweaking the Widget Control section of the PIN nodes, to make microphones IN and headphone jacks OUT. Referring to the Config Defaults heading is a good idea.<br />
<br />
NOTE: the script is done by such way that it is incompatible with python3 (which is now shipped with ArchLinux) but tries to use it.<br />
The workaround is: open "run.py", find all occurences of "python" (2 occurences - one on the first line, and the second on the last line) and replace them all by "python2".<br />
<br />
NOTE2: the script requires root acces, but running it via su/sudo is bogus. Run it via {{ic|kdesu}} or {{ic|gksu}}.<br />
<br />
===ALSA with SDL===<br />
If you get no sound via SDL and ALSA cannot be chosen from the application. Try setting the environmental variable SDL_AUDIODRIVER to alsa.<br />
# export SDL_AUDIODRIVER=alsa<br />
<br />
===Low Sound Workaround===<br />
<br />
If you are facing low sound even after maxing out your speakers/headphones, you can give the softvol plugin a try. Add the following to {{ic|/etc/asound.conf}}.<br />
{{bc|<nowiki><br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
<br />
pcm.softvol {<br />
type softvol<br />
slave {<br />
pcm "dmix"<br />
}<br />
control {<br />
name "Pre-Amp"<br />
card 0<br />
}<br />
min_dB -5.0<br />
max_dB 20.0<br />
resolution 6<br />
}<br />
</nowiki>}}<br />
{{note| You will probably have to restart the computer, as restarting the alsa daemon did not load the new configuration for me. Also, if the configuration does not work even after restarting, try changing {{ic|plug}} with {{ic|hw}} in the above configuration.}}<br />
<br />
After the changes are loaded successfully, you will see a {{ic|Pre-Amp}} section in alsamixer. You can adjust the levels there.<br />
{{note|Setting a high value for {{ic|Pre-Amp}} can cause sound distortion, so adjust it according to the level that suits you.}}<br />
<br />
===Popping sound after resuming from suspension===<br />
You might hear a popping sound after resuming the computer from suspension. This can be fixed by editing {{ic|/etc/pm/sleep.d/90alsa}} and removing the line that says {{ic|aplay -d 1 /dev/zero}}<br />
<br />
==设置文件范例==<br />
参见:[[Advanced Linux Sound Architecture/Example Configurations]]。<br />
<br />
==相关阅读==<br />
* [http://www.mjmwired.net/kernel/Documentation/sound/alsa/ALSA-Configuration.txt ALSA 模块高级配置]<br />
* [http://alsa.opensrc.org/Main_Page 非官方的 ALSA Wiki]<br />
* [https://bbs.archlinux.org/viewtopic.php?id=36815 如何从 svn 获取驱动代码并编译]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Advanced_Linux_Sound_Architecture_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=255077
Advanced Linux Sound Architecture (简体中文)
2013-04-25T02:38:25Z
<p>Cuihao: </p>
<hr />
<div>[[Category:简体中文]]<br />
[[Category:Sound (简体中文)]]<br />
[[Category:Audio/Video (简体中文)]]<br />
[[cs:Advanced Linux Sound Architecture]]<br />
[[de:Alsa]]<br />
[[es:Advanced Linux Sound Architecture]]<br />
[[fa:ALSA]]<br />
[[fr:Alsa]]<br />
[[he:Advanced Linux Sound Architecture]]<br />
[[it:Advanced Linux Sound Architecture]]<br />
[[ja:Advanced Linux Sound Architecture]]<br />
[[nl:Advanced Linux Sound Architecture]]<br />
[[pt:Advanced Linux Sound Architecture]]<br />
[[ru:Advanced Linux Sound Architecture]]<br />
[[sk:Advanced Linux Sound Architecture]]<br />
[[th:Advanced Linux Sound Architecture]]<br />
[[tr:Gelişmiş_ses_mimarisi_(ALSA)]]<br />
[[en:Advanced Linux Sound Architecture]]<br />
<br />
{{Article summary start}}<br />
{{Article summary text|ALSA 的使用、配置及疑难解答。}}<br />
{{Article summary heading|相关页面}}<br />
{{Article summary wiki|Sound system (简体中文)}}<br />
{{Article summary wiki|Disable PC Speaker Beep (简体中文)}}<br />
{{Article summary end}}<br />
{{TranslationStatus (简体中文)|Advanced_Linux_Sound_Architecture|2013-04-25|254584}}<br />
<br />
[[Wikipedia:zh:ALSA|高级 Linux 声音体系]](Advanced Linux Sound Architecture,'''ALSA''')是Linux中提供声音设备驱动的内核组件,用来代替原来的开放声音系统(Open Sound System,OSSv3)。除了声音设备驱动,'''ALSA'''还包含一个用户空间的函数库,以方便开发者通过高级API使用驱动功能,而不必直接与内核驱动交互。<br />
<br />
{{注意|关于另一种声音体系,请阅读[[Open Sound System (简体中文)|开放声音系统(OSS)]]的页面。}}<br />
<br />
==安装==<br />
Arch 默认的内核已经通过一套模块提供了 ALSA,不必特别安装。<br />
<br />
[[udev (简体中文)|udev]]会在系统启动时自动检测硬件,并加载相应的声音设备驱动模块。这时,你的声卡已经可以工作了,只是所有声道默认都被设置成静音了。<br />
<br />
在本地登录(通过虚拟终端或登录管理器)的用户,都有权限播放音频并调整音量。要让远程登录的用户拥有这些权限,必须把用户[[Users and Groups (简体中文)#用户组管理|加入]] {{ic|audio}} 用户组。该组的成员可以直接访问声音设备,会导致某些程序独占音频输出(破坏软件混音),还可能影响用户快速切换和拖机(multiseat)。因此,除非真的有某些[https://wiki.ubuntu.com/Audio/TheAudioGroup 特殊需求],'''不建议'''把用户加入 {{ic|audio}} 用户组。<br />
<br />
===用户空间工具===<br />
从 [[Official Repositories (简体中文)|官方仓库]]:<br />
* [[pacman (简体中文)|安装]] {{Pkg|alsa-utils}} 软件包,其中的 {{ic|alsamixer}} 工具允许用户在控制台或终端中配置声音设备。<br />
* 安装 {{Pkg|alsa-oss}} 软件包,如果想让 OSS 应用程序与dmix(软件混音)协同工作。 <br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。}}<br />
*安装 {{Pkg|alsa-plugins}} 软件包,如果需要 [[#高质量重采样|高质量重采样]]、[[#Upmixing/Downmixing|软件模拟环绕立体声]]和其他高级特性。<br />
<br />
==解除各声道的静音==<br />
目前版本的 ALSA 安装后,所有声道'''默认是静音的''',必须手动解除。<br />
<br />
使用 {{ic|alsamixer}} 的 ncurses 界面,配置十分简单:<br />
<br />
$ alsamixer<br />
<br />
此外,还可以在命令行下使用 {{ic|amixer}}:<br />
<br />
$ amixer sset Master unmute<br />
<br />
在 {{ic|alsamixer}} 中,下方标有 {{ic|MM}} 的声道是静音的,而标有 {{ic|00}} 的通道已经启用。<br />
<br />
使用左右方向键,选中 {{ic|Master}} 和 {{ic|PCM}} 声道。按下 {{keypress|m}} 键解除静音。使用上方向键增加音量,直到增益值为{{ic|0}}。该值显示在左上方 {{ic|Item:}} 字段后。过高的增益值会导致声音失真。<br />
<br />
要想得到完整的 5.1 或 7.1 环绕立体声,还得解除 Front、Surround、Center、LFE (subwoofer) 和 Side 这些声道的静音(上述名称是 Intel HD Audio 声卡使用的声道名,可能因设备不同而有所差异)。注意,仅有这些设置,系统不会自动将立体声源(多数音乐)提升(upmix)成环绕立体声。如果需要这些功能,请阅读[[#Upmixing/Downmixing]]。<br />
<br />
要启用麦克风,切换至 Capture 选项卡,按下 {{keypress|F4}},按下 {{keypress|空格}} 启用其中一个声道即可。<br />
<br />
按下 {{Keypress|Esc}} 键退出 alsamixer。<br />
<br />
{{注意|<br />
* 有一些声卡,需要关闭数字输出或将其调成静音,才能输出模拟音频信号。对于 Soundblaster Audigy LS 声卡,需要把 IEC958 通道设成静音。 <br />
* 有些机器(如 Thinkpad T61),有一个 Speaker 通道,必须按照上述方法打开调整。<br />
* 有些机器(如 Dell E6400),可能还需要把 {{ic|Front}} 和 {{ic|Headphone}} 声道打开调整。}}<br />
<br />
接下来,测试声卡是否工作:<br />
<br />
$ speaker-test -c 2<br />
<br />
根据扬声器的实际情况,调整 -c 后的数值。对于 7.1 声道,这个数字是 8:<br />
<br />
$ speaker-test -c 8<br />
<br />
如果没有正常工作,请继续阅读 [[#配置]] 以及 [[#疑难解答]] 部分。<br />
<br />
{{pkg|alsa-utils}} 软件包提供了两个配置 alsa 的服务:{{ic|alsa-restore.service}} 和 {{ic|alsa-store.service}}。它们分别在开机和关机时自动运行。<br />
<br />
==配置==<br />
=== VirtualBox 虚拟机无声音 ===<br />
<br />
如果使用 VirtualBox 时碰上了麻烦,请尝试以下命令:<br />
<br />
{{hc|$ alsactl init|2=<nowiki><br />
<br />
Found hardware: "ICH" "SigmaTel STAC9700,83,84" "AC97a:83847600" "0x8086" "0x0000"<br />
Hardware is initialized using a generic method<br />
</nowiki><br />
}}<br />
<br />
如果还是不行,请检查所使用的音频软件是否激活了 ALSA 输出。<br />
<br />
===设置默认声卡===<br />
<br />
如果发现开机时声卡次序会发生变化,可以在通过 {{ic|/etc/modprobe.d}} 中的 {{ic|.conf}} 文件(比如 {{ic|/etc/modprobe.d/alsa-base.conf}})手动设置次序。<br />
比如,要让 mia 声卡成为 #0、Intel HDA 声卡成为 #1:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<br />
options snd slots=snd_mia,snd_hda_intel<br />
options snd_mia index=0<br />
options snd_hda_intel index=1<br />
}}<br />
<br />
使用 {{ic|<nowiki>lsmod | grep snd</nowiki>}} 获取设备列表。<br />
<br />
snd_mia 和 snd_hda_intel 分别是两个声卡的内核驱动模块。该配置仅适用于同时装有 mia 声卡和使用 snd_hda_intel 驱动的声卡(通常是板载声卡)的情况。<br />
<br />
如果使用 -2 的 index 值,ALSA 就不会将对应的设备作为主声卡使用。Linux Mint 和 Ubuntu 等发行版使用了以下配置,避免 USB 声卡和其他“非主流”设备变成 index 为 0 的主声卡:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<br />
options bt87x index=-2<br />
options cx88_alsa index=-2<br />
options saa7134-alsa index=-2<br />
options snd-atiixp-modem index=-2<br />
options snd-intel8x0m index=-2<br />
options snd-via82xx-modem index=-2<br />
options snd-usb-audio index=-2<br />
options snd-usb-caiaq index=-2<br />
options snd-usb-ua101 index=-2<br />
options snd-usb-us122l index=-2<br />
options snd-usb-usx2y index=-2<br />
# Keep snd-pcsp from being loaded as first soundcard<br />
options snd-pcsp index=-2<br />
# Keep snd-usb-audio from beeing loaded as first soundcard<br />
options snd-usb-audio index=-2<br />
}}<br />
<br />
以上配置会在系统重启后生效。<br />
<br />
{{Out of date|据英文 Wiki 用户反应,{{ic|/etc/asound.conf}} 这个配置文件默认是不存在的,下列操作可能有误。}}<br />
<br />
首先,运行 {{ic|aplay -l}},获取声卡的声卡ID和设备ID:<br />
<br />
{{hc|$ aplay -l|2=<nowiki><br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: Intel [HDA Intel], device 0: CONEXANT Analog [CONEXANT Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: Intel [HDA Intel], device 1: Conexant Digital [Conexant Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 1: JamLab [JamLab], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 2: Audio [Altec Lansing XT1 - USB Audio], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
</nowiki>}}<br />
<br />
拿输出中的最后一项来说,这个声卡的声卡ID为2,设备ID为0。若要把这块声卡作为默认声卡,可以把下列配置添加到系统级别的 {{ic|/etc/asound.conf}} 或用户级别的 {{ic|~/.asoundrc}} 文件。如果文件不存在,可以手动创建。其中的各个ID,请根据实际情况调整:<br />
{{bc|<br />
defaults.pcm.card 2<br />
defaults.pcm.device 0<br />
defaults.ctl.card 2<br />
}}<br />
“pcm”选项决定用来播放音频的设备,而“ctl”选项决定那个声卡能够由控制工具(如 alsamixer)使用。<br />
<br />
上述配置在重启音频程序(如 mplayer)后立即生效。<br />
<br />
===确认所有声音模块都已经加载===<br />
<br />
一般 udev 都会自动识别出声卡。使用以下命令确认:<br />
<br />
{{hc|<nowiki>$ lsmod | grep '^snd' | column -t</nowiki>|<nowiki><br />
snd_hda_codec_hdmi 22378 4<br />
snd_hda_codec_realtek 294191 1<br />
snd_hda_intel 21738 1<br />
snd_hda_codec 73739 3 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel<br />
snd_hwdep 6134 1 snd_hda_codec<br />
snd_pcm 71032 3 snd_hda_codec_hdmi,snd_hda_intel,snd_hda_codec<br />
snd_timer 18992 1 snd_pcm<br />
snd 55132 9 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel,snd_hda_codec,snd_hwdep,snd_pcm,snd_timer<br />
snd_page_alloc 7017 2 snd_hda_intel,snd_pcm<br />
</nowiki>}}<br />
<br />
如果你的输出和上面类似,那就说明声卡已经被正确识别。<br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。如有需要,请[[Kernel_modules_(简体中文)#装入|手动加载]]。}}<br />
<br />
还可以检查一下 {{ic|/dev/snd/}} 目录,看看是否有这些设备文件:<br />
<br />
{{hc|$ ls -l /dev/snd|2=<nowiki><br />
total 0<br />
crw-rw---- 1 root audio 116, 0 Apr 8 14:17 controlC0<br />
crw-rw---- 1 root audio 116, 32 Apr 8 14:17 controlC1<br />
crw-rw---- 1 root audio 116, 24 Apr 8 14:17 pcmC0D0c<br />
crw-rw---- 1 root audio 116, 16 Apr 8 14:17 pcmC0D0p<br />
crw-rw---- 1 root audio 116, 25 Apr 8 14:17 pcmC0D1c<br />
crw-rw---- 1 root audio 116, 56 Apr 8 14:17 pcmC1D0c<br />
crw-rw---- 1 root audio 116, 48 Apr 8 14:17 pcmC1D0p<br />
crw-rw---- 1 root audio 116, 1 Apr 8 14:17 seq<br />
crw-rw---- 1 root audio 116, 33 Apr 8 14:17 timer<br />
</nowiki>}}<br />
<br />
{{注意|在 IRC 或论坛寻求这方面帮助时,别忘了贴出上面几个命令的输出。}}<br />
<br />
如果你的输出跟上面类似,或至少有 '''controlC0''' 和 '''pcmC0D0p''',那么声卡就已经正常加载了。<br />
<br />
如果出现问题,声卡模块没有正确加载,那么请尝试手动加载模块:<br />
<br />
* 确定声卡对应的驱动模块:[http://www.alsa-project.org/main/index.php/Matrix:Main ALSA Soundcard Matrix]。这些模块都会有一个”snd-“前缀(例如:{{ic|snd-via82xx}})。<br />
* [[Kernel_modules_(简体中文)#装入|加载模块]]。<br />
* 检查 {{ic|/dev/snd}} 目录中的设备文件(参见上文);或者,检查 {{ic|alsamixer}} 或 {{ic|amixer}} 的输出是否正确。<br />
* 修改配置,使 {{ic|snd-<模块名>}} 和 {{ic|snd-pcm-oss}} 模块[[Kernel_modules_(简体中文)#装入|开机自动加载]]。<br />
<br />
===启用 SPDIF 输出===<br />
<br />
(由 Gentoo 论坛的 gralves 提供)<br />
* 在 GNOME 音量控制的选项(Options)标签中,把 IEC958 改成 PCM。该选项可以在首选项中启用。<br />
* 不使用 GNOME 音量控制工具的话:<br />
** 编辑 {{ic|/var/lib/alsa/asound.state}}。该文件是 alsasound 保存混音器设置的地方。<br />
** 找到这一行:“IEC958 Playback Switch”。该行上下应该有一行内容为 {{ic|value:false}},将其改为 {{ic|value:true}}。<br />
** 再找到这一行:”IEC958 Playback AC97-SPSA“。把对应的值改为 0。<br />
** 重启 ALSA。<br />
<br />
{{Out of date|rc.local 在 systemd 中已经弃用,以下方法可能不再适用。}}<br />
<br />
还有另外一个开机自动启用 SPDIF 输出的方法(在 SoundBlaster Audigy 声卡上测试过):<br />
<br />
* 在 {{ic|/etc/rc.local}} 中添加如下内容:<br />
# Use COAX-digital output<br />
amixer set 'IEC958 Optical' 100 unmute<br />
amixer set 'Audigy Analog/Digital Output Jack' on<br />
<br />
使用以下命令,获取你的声卡的数字输出的名称:<br />
$ amixer scontrols<br />
<br />
===系统级均衡器===<br />
====使用 AlsaEqual(包含界面)====<br />
从 [[AUR (简体中文)|AUR]] 安装 {{AUR|alsaequal}}。<br />
<br />
{{注意|如果使用的是64位系统,而又安装了32位的 Flash 插件,这里的设置会导致 Flash 无声。如果发生了这种情况,请禁用 alsaequal 或编译32位版本的 alsaequal。}}<br />
<br />
安装后,把下列内容添加到 ALSA 配置文件({{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}}):<br />
<br />
ctl.equal {<br />
type equal;<br />
}<br />
<br />
pcm.plugequal {<br />
type equal;<br />
# Modify the line below if you do not<br />
#slave.pcm "plughw:0,0";<br />
#by default we want to play from more sources at time:<br />
slave.pcm "plug:dmix";<br />
}<br />
#pcm.equal {<br />
# If you do not want the equalizer to be your<br />
# default soundcard comment the following<br />
# line and uncomment the above line. (You can<br />
# choose it as the output device by addressing<br />
# it with specific apps,eg mpg123 -a equal 06.Back_In_Black.mp3)<br />
pcm.!default {<br />
type plug;<br />
slave.pcm plugequal;<br />
}<br />
<br />
设置好后,切换均衡器:<br />
$ alsamixer -D equal<br />
<br />
需要注意,每个用户的 alsaequal 配置文件都是独立的,位于 {{ic|~/.alsaequal.bin}}。因此,如果想在 [[mpd]] 之类的以独立用户身份运行的程序中使用 AlsaEqual,就得像下面这样:<br />
# su mpd -c 'alsamixer -D equal'<br />
或者,在其他用户的主目录下创建符号链接,指向你的 {{ic|.alsaequal.bin}}……<br />
<br />
=====管理 AlsaEqual 配置=====<br />
从 [http://xyne.archlinux.ca/repos/ Xyne 的软件仓库] 或 [https://aur.archlinux.org/packages.php?ID=62420 AUR] 安装 [http://xyne.archlinux.ca/projects/alsaequal-mgr/ alsaequal-mgr]。<br />
<br />
同上,配置均衡器: <br />
$alsamixer -D equal<br />
<br />
如果对效果感到满意,将其命名(如“foo”)并保存该配置:<br />
$alsaequal-mgr save foo<br />
<br />
重新加载“foo”中的配置:<br />
$alsaequal-mgr load foo<br />
<br />
这样,就可以为游戏、电影、不同音乐流派、网络电话等等创建不同的均衡器配置,以便随时切换。<br />
<br />
参见[http://xyne.archlinux.ca/projects/alsaequal-mgr/ 项目主页]和程序的帮助信息,了解更多设置。<br />
<br />
====使用 mbeq====<br />
{{注意|该软件用到了一个 ladspa 插件,可能导致播放音频时 CPU 占用率偏高。In addition, this was made with stereophonic sound (e.g. headphones) in mind.}}<br />
<br />
安装 {{Pkg|alsa-plugins}}、{{Pkg|ladspa}} 和 {{Pkg|swh-plugins}} 软件包。<br />
<br />
* 在 {{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}} 中添加如下内容:<br />
<br />
{{hc|/etc/asound.conf|2=<nowiki>pcm.eq {<br />
type ladspa<br />
<br />
# The output from the EQ can either go direct to a hardware device<br />
# (if you have a hardware mixer, e.g. SBLive/Audigy) or it can go<br />
# to the software mixer shown here.<br />
#slave.pcm "plughw:0,0"<br />
slave.pcm "plug:dmix"<br />
<br />
# Sometimes you may need to specify the path to the plugins,<br />
# especially if you have just installed them. Once you have logged<br />
# out/restarted this should not be necessary, but if you get errors<br />
# about being unable to find plugins, try uncommenting this.<br />
#path "/usr/lib/ladspa"<br />
<br />
plugins [<br />
{<br />
label mbeq<br />
id 1197<br />
input {<br />
#this setting is here by example, edit to your own taste<br />
#bands: 50hz, 100hz, 156hz, 220hz, 311hz, 440hz, 622hz, 880hz, 1250hz, 1750hz, 25000hz,<br />
#50000hz, 10000hz, 20000hz<br />
controls [ -5 -5 -5 -5 -5 -10 -20 -15 -10 -10 -10 -10 -10 -3 -2 ]<br />
}<br />
}<br />
]<br />
}<br />
<br />
# Redirect the default device to go via the EQ - you may want to do<br />
# this last, once you are sure everything is working. Otherwise all<br />
# your audio programs will break/crash if something has gone wrong.<br />
<br />
pcm.!default {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
<br />
# Redirect the OSS emulation through the EQ too (when programs are running through "aoss")<br />
<br />
pcm.dsp0 {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
</nowiki>}}<br />
<br />
* 完成。若有问题,欢迎前往论坛咨询。<br />
<br />
==高质量重采样==<br />
启用软件混音时,ALSA 会强制把所有音频重采样到相同的频率(如果系统支持,默认是 48000)。dmix 的重采样算法很糟糕,会导致显著的质量损失。<br />
<br />
安装 {{pkg|alsa-plugins}} 和 {{pkg|libsamplerate}}。<br />
<br />
将默认的采样率转换器更改为 libsamplerate:<br />
<br />
{{hc|/etc/asound.conf|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
或:<br />
<br />
{{hc|~/.asoundrc|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
'''samplerate_best''' 输出的声音质量最佳,但需要耗费大量CPU资源进行实时重采样。还有其他的算法可供选择(如 '''samplerate''' 等),但比起默认的算法,这些算法的改进并不明显。<br />
<br />
{{警告|某些系统中,启用 samplerate_best 可能导致 Flash 播放器无声音。}}<br />
<br />
==Upmixing/Downmixing==<br />
===Upmixing===<br />
In order for stereo sources like music to be able to saturate a 5.1 or 7.1 sound system, you need to use upmixing. In darker days this used to be tricky and error prone but nowadays plugins exist to easily take care of this task. Thus, install {{Pkg|alsa-plugins}}.<br />
<br />
Then add the following to your ALSA configuration file of choice (either {{ic|/etc/asound.conf}} or {{ic|~/.asoundrc}}):<br />
pcm.upmix71 {<br />
type upmix<br />
slave.pcm "surround71"<br />
delay 15<br />
channels 8<br />
}<br />
<br />
You can easily change this example for 7.1 upmixing to 5.1 or 4.0.<br />
<br />
This adds a new pcm that you can use for upmixing. If you want all sound sources to go through this pcm, add it as a default below the previous definition like so:<br />
<br />
pcm.!default "plug:upmix71"<br />
<br />
The plugin automatically allows multiple sources to play through it without problems so setting is as a default is actually a safe choice. <br />
If this is not working, you have to setup your own dmixer for the upmixing PCM like this: <br />
pcm.dmix6 {<br />
type asym<br />
playback.pcm {<br />
type dmix<br />
ipc_key 567829<br />
slave {<br />
pcm "hw:0,0"<br />
channels 6<br />
}<br />
}<br />
}<br />
<br />
and use "dmix6" instead of "surround71". <br />
If you experience skipping or distorted sound, consider increasing the buffer_size (to 32768, for example) or use a [[#高质量重采样|high quality resampler]].<br />
<br />
===Downmixing===<br />
If you want to downmix sources to stereo because you, for instance, want to watch a movie with 5.1 sound on a stereo system, you need to use the vdownmix plugin that alsa provides in the same package.<br />
<br />
Again, in your configuration file, add this:<br />
<br />
pcm.!surround51 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
pcm.!surround40 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
<br />
==混音==<br />
===软件混音(dmix)===<br />
<br />
{{注意|在 ALSA 1.0.9rc2 及以上版本中,如果声卡不支持硬件混音,模拟输出将默认使用 dmix 混音,无需额外设置。}}<br />
<br />
如果软件混音未正常启用,那么可以手动创建 {{ic|~/.asoundrc}},加入以下内容:<br />
<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
<br />
这样就启用了软件混音,多个程序能够同时使用声卡。<br />
<br />
至于 S/PDIF 之类的数字音频输出,目前的 ALSA 还不会默认启用 dmix。因此,必须像上面这样手动启用。<br />
<br />
常见问题的解答,参见[[#疑难解答]]部分。<br />
<br />
===硬件混音===<br />
====支持====<br />
如果声卡支持,一般无需任何配置即可启用硬件混音。绝大多数板载声卡都不支持硬件混音,必须得使用软件混音(见上文)。很多独立声卡都支持硬件混音,Linux 下支持较好的有:<br />
<br />
* Creative SoundBlaster Live! (5.1 型号)<br />
* Creative SoundBlaster Audigy (部分型号)<br />
* Creative SoundBlaster Audidy 2 (ZS 系列型号)<br />
* Creative SoundBlaster Audigy 4 (Pro 系列型号)<br />
<br />
{{注意|上述声卡的低端版本(Audigy SE、Audigy 2 NX、SoundBlaster Live! 24bit 以及 SoundBlaster Live! 7.1)使用了不同的芯片,'''不支持'''硬件混音。}}<br />
<br />
{{注意|板载 VIA8237 芯片支持4路硬件混音,但在某些主板上只能使用3路(第4路无声),甚至根本无法使用。即使能正常工作,质量也比不上其他混音方法。}}<br />
<br />
====问题修复====<br />
如果你的声卡是 Intel Corporation 82801I (ICH9 Family) HD Audio Controller (rev 02),且使用了64位系统,需要执行以下命令才能在 Enemy Territory 中听到声音:<br />
echo "et.x86 0 0 direct" > /proc/asound/card0/pcm0p/oss<br />
echo "et.x86 0 0 disable" > /proc/asound/card0/pcm0c/oss<br />
<br />
==疑难解答==<br />
===使用CPU频率动态调整时,音频发生跳跃===<br />
使用 {{ic|ondemand}} 或 {{ic|conservative}} 频率策略、启用CPU频率动态调整时,某些声卡和某些 ALSA 驱动会出现声音跳跃的情况。目前,唯一的解决方案是使用固定的频率,如 {{ic|performance}} 性能策略。<br />
<br />
详情参见 [[CPU Frequency Scaling (简体中文)]]。<br />
<br />
===同时只能有一个用户播放音频===<br />
有时会出现一个用户独占软件混音器的情况。多数情况下这没什么影响,但在使用 [[mpd]] 这样以独立用户身份运行的程序时,就会出现问题。当 mpd 运行时,用户自己就无法播放其他音频了。一种解决方法是,以正在使用的用户身份运行 mpd。另一种方法是,在 ALSA 配置文件的 {{ic|pcm.dmixer}} 部分添加 {{ic|ipc_key_add_uid 0}} 禁止独占。以下是 {{ic|asound.conf}} 示例的片段,省略的部分无需修改:<br />
<br />
...<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
slave {<br />
...<br />
<br />
===无法同时播放多个音频===<br />
如果安装了 [[PulseAudio (简体中文)|PulseAudio]](可能作为 [[GNOME (简体中文)|GNOME]] 的依赖被安装),可能出现无法同步播放的问题,因为 PulseAudio 的默认配置是“独占”声卡。ALSA 的用户可能对自己的配置已经很满意、不希望使用 [[PulseAudio (简体中文)|PulseAudio]]。解决方案是编辑 {{ic|/etc/asound.conf}} 并注释掉以下内容:<br />
# Use PulseAudio by default<br />
#pcm.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
# hint {<br />
# show on<br />
# description "Default ALSA Output (currently PulseAudio Sound Server)"<br />
# }<br />
#}<br />
<br />
可能还需要注释掉下面的内容:<br />
#ctl.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
#}<br />
<br />
比起完全卸载 [[PulseAudio (简体中文)|PulseAudio]],这大概是个更简单的方法。<br />
<br />
以下是 {{ic|/etc/asound.conf}} 配置文件的一个示例:<br />
{{bc|<nowiki>pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
}<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}</nowiki><br />
}}<br />
<br />
{{注意|本 {{ic|/etc/asound.conf}} 本来是配合 [[MPD]] 全局配置使用的。参见[[#同时只能有一个用户播放音频|该节]]关于多用户的内容。}}<br />
<br />
===有时候开机无声===<br />
如果会随机地出现开机无声的问题,原因可能是多声卡的顺序发生了变化。如果是这种情况,请参照[[#设置默认声卡]]进行设置。<br />
<br />
对于 mpd 用户,上述配置无效,请参考[http://mpd.wikia.com/wiki/Configuration#ALSA_MPD_software_volume_control 该文]设置。<br />
<br />
===特定程序的问题===<br />
对于不使用系统声音配置的程序,如 XMMS 和 Mplayer,必须特别设置以应用 ALSA 配置。<br />
<br />
对于 mplayer,将以下内容添加到 {{ic|~/.mplayer/config}} (或系统全局配置文件 {{ic|/etc/mplayer/mplayer.conf}}):<br />
ao=alsa<br />
<br />
对于 XMMS 和 Beep Media Player,打开选项窗口,要把声音驱动设置为 ALSA 而不是 OSS。<br />
<br />
XMMS 的设置方法:<br />
* 启动 XMMS<br />
** 选项 -> 首选项<br />
** 选择 Alsa 输出插件<br />
<br />
对于未提供 ALSA 输出的程序,可以使用 alsa-oss 软件包中的 aoss 工具启动。使用方法是:在要运行的程序的命令行前加上 {{ic|aoss}}。例如:<br />
aoss realplay<br />
<br />
pcm.!default{ ... } 貌似已经不管用了。但下面的设置有效:<br />
pcm.default pcm.dmixer<br />
<br />
===特定型号的配置===<br />
ALSA 会通过 BIOS 检测[http://www.kernel.org/doc/Documentation/sound/alsa/HD-Audio-Models.txt 声卡型号],少数情况下也可能无法正确识别。声卡芯片类型可以在 {{ic|alsamixer}} 界面上找到(如 ALC662),而型号则要设置在 {{ic|/etc/modprobe.d/modprobe.conf}} 或 {{ic|/etc/modprobe.d/sound.conf}} 文件中:<br />
<br />
options snd-hda-intel model=<型号><br />
<br />
具体设置因声卡型号而异。多数情况下不需要特地这样做。如果想知道具体声卡型号的设置,请查阅 [http://bugtrack.alsa-project.org/main/index.php/Matrix:Main Alsa Soundcard List],找到你的声卡型号,点击“Details”,然后查看“Setting up modprobe...”小节,将其中的内容插入 {{ic|/etc/modprobe.d/modprobe.conf}} 文件即可。例如,Intel AC97 的配置如下:<br />
<br />
{{bc|# ALSA portion<br />
alias char-major-116 snd<br />
alias snd-card-0 snd-intel8x0<br />
# module options should go here<br />
<br />
# OSS/Free portion<br />
alias char-major-14 soundcore<br />
alias sound-slot-0 snd-card-0<br />
<br />
# card #1<br />
alias sound-service-0-0 snd-mixer-oss<br />
alias sound-service-0-1 snd-seq-oss<br />
alias sound-service-0-3 snd-pcm-oss<br />
alias sound-service-0-8 snd-seq-oss<br />
alias sound-service-0-12 snd-pcm-oss}}<br />
<br />
===与蜂鸣器冲突===<br />
如果你确认所有声道都已解除静音,声卡驱动配置正确,且音量大小也合适,但还是听不到声音,那么请试试将下面的内容添加到 {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
(如果是 {{ic|via82xx}} 驱动)<br />
options snd-<模块名请自行补充> ac97_quirk=0<br />
<br />
(如果是 {{ic|snd_intel8x0}} 驱动)<br />
options snd-NAME-OF-MODULE ac97_quirk=1<br />
<br />
===麦克风输入无声音===<br />
In alsamixer, make sure that all the volume levels are up under recording, and that CAPTURE is toggled active on the microphone (e.g. Mic, Internal Mic) and/or on Capture (in alsamixer, select these items and press space). Try making positive Mic Boost and raising Capture and Digital levels higher; this make make static or distortion, but then you can adjust them back down once you are hearing ''something'' when you record<br />
<br />
As the pulseaudio wrapper is shown as "default" in alsamixer, you may have to press F6 to select your actual soundcard first. You may also need to enable and increase the volume of Line-in in the Playback section.<br />
<br />
To test the microphone, run these commands (see arecord's man page for further information):<br />
arecord -d 5 test-mic.wav<br />
aplay test-mic.wav<br />
<br />
If all fails, you may want to eliminate hardware failure by testing the microphone with a different device.<br />
<br />
For at least some computers, muting a microphone (MM) simply means its input does not go immediately to the speakers. It still receives input.<br />
<br />
Many Dell laptops need "-dmic" to be appended to the model name in {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-hda-intel model=dell-m6-dmic<br />
<br />
Some programs use try to use OSS as the main input software. Add the following lines to {{ic|/etc/modprobe.d/modprobe.conf}} to prevent OSS modules from being loaded:<br />
<br />
{{Note|The OSS modules are no longer autoloaded anyway.}}<br />
<br />
blacklist snd_pcm_oss<br />
blacklist snd_mixer_oss<br />
blacklist snd_seq_oss<br />
<br />
See also:<br />
* http://www.alsa-project.org/main/index.php/SoundcardTesting<br />
* http://alsa.opensrc.org/Record_from_mic<br />
<br />
===设置默认的麦克风/录音设备===<br />
某些程序(Pidgin、Adobe Flash)提供了设置录音设备的选项。如果录音设备不是内置的声卡,而是外接的(如USB摄像头、麦克风),就会有些问题。如果只想更改默认的录音设备、不更改回放设备,需要像下面这样设置 {{ic|~/.asoundrc}}:<br />
<br />
pcm.usb<br />
{<br />
type hw<br />
card U0x46d0x81d<br />
}<br />
<br />
pcm.!default<br />
{<br />
type asym<br />
playback.pcm<br />
{<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
capture.pcm<br />
{<br />
type plug<br />
slave.pcm "usb"<br />
}<br />
}<br />
<br />
把“U0x46d0x81d”替换为你的录音设备的名称。可以通过 {{ic|arecord -L}} 查看所有 ALSA 识别到的录音设备。<br />
<br />
===内置麦克风无效===<br />
First make sure all the volume levels are up under recording in alsamixer. In my case adding the following option to /etc/sound.conf and reloading the snd-* module produced a new volume setting called Capture which was capturing for the internal mic. For eg, for snd-hda-intel add<br />
<br />
options snd-hda-intel enable_msi=1<br />
<br />
Then reload the module (as below), up the recording volume of Capture and then test.<br />
<br />
{{bc|# rmmod snd-hda-intel && modprobe snd-hda-intel}}<br />
<br />
===Intel 板载声卡无声===<br />
可能由两个相互冲突的驱动模块引起,分别是 {{ic|snd_intel8x0}} 和 {{ic|snd_intel8x0m}}。如果是的话,屏蔽 snd_intel8x0m 即可:<br />
<br />
{{hc|/etc/modprobe.d/modprobe.conf|blacklist snd_intel8x0m}}<br />
<br />
以下方法可能也有效:在 {{ic|alsamixer}} 或 {{ic|amixer}} 把 “External Amplifier” 设置为静音。参见:[http://alsa.opensrc.org/Intel8x0#Dell_Inspiron_8600_.28and_probably_others.29 ALSA wiki 相关条目]。<br />
<br />
也可以试试解除“Mix”的静音。<br />
<br />
===No Headphone Sound with Onboard Intel Sound Card===<br />
With '''Intel Corporation 82801 I (ICH9 Family) HD Audio Controller''' on laptop, you may need to add this line to modprobe or sound.conf:<br />
<br />
options snd-hda-intel model=$model<br />
<br />
Where $model is any one of the following (in order of possibility to work, but not merit):<br />
<br />
* dell-vostro<br />
* olpc-xo-1_5<br />
* laptop<br />
* dell-m6<br />
* laptop-hpsense<br />
<br />
Note: It may be necessary to put this "options" line below (after) any "alias" lines about your card.<br />
<br />
You can see all the available models in the kernel documentation. For example [http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.35.y.git;a=blob;f=Documentation/sound/alsa/HD-Audio-Models.txt;h=dc25bb84b83b49665a7ed850e7bf5423d50cd3ba;hb=HEAD here], but check that it is the correct version of that document for your kernel version.<br />
<br />
A list of available models is also available [http://www.mjmwired.net/kernel/Documentation/sound/alsa/HD-Audio-Models.txt here]. To know your chip name type the following command (with * being corrected to match your files). Note that some chips could have been renamed and do not directly match the available ones in the file.<br />
<br />
cat /proc/asound/card*/codec* | grep Codec<br />
<br />
Note that there is a high chance none of the input devices (all internal and external mics) will work if you choose to do this, so it is either your headphones or your mic. Please report to ALSA if you are affected by this bug.<br />
<br />
And also, if you have problems getting beeps to work (pcspkr):<br />
<br />
options snd-hda-intel model=$model enable=1 index=0<br />
<br />
===No sound when S/PDIF video card is installed===<br />
Discover available modules and their order:<br />
$ cat /proc/asound/modules<br />
0 snd_hda_intel<br />
1 snd_ca0106<br />
<br />
Disable the undesired video card audio codec in {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
# /etc/modprobe.d/modprobe.conf<br />
#<br />
install snd_hda_intel /bin/false<br />
<br />
If both devices use the same module, it might be possible to disable one of them in the BIOS.<br />
<br />
===Poor sound quality or clipping===<br />
If you experience poor sound quality, try setting the PCM volume (in alsamixer) to a level such that gain is 0.<br />
<br />
If snd-usb-audio driver has been loaded, you could try to enable {{ic|softvol}} in '''/etc/asound.conf''' file. Example configuration for the first audio device:<br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.dsnooper {<br />
type dsnoop<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
channels 2<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.softvol {<br />
type softvol<br />
slave { pcm "dmixer" }<br />
control {<br />
name "Master"<br />
card 0<br />
}<br />
}<br />
ctl.!default {<br />
type hw<br />
card 0<br />
}<br />
ctl.softvol {<br />
type hw<br />
card 0<br />
}<br />
ctl.dmixer {<br />
type hw<br />
card 0<br />
}<br />
<br />
===Pops When Starting and Stopping Playback===<br />
Some modules (e.g. snd_ac97_codec and snd_hda_intel) can power off your sound card when not in use. This can make an audible noise (like a crack/pop/scratch) when turning on/off your sound card. Sometimes even when move the slider volume, or open and close windows (KDE4). If you find this annoying try {{ic|modinfo snd_MY_MODULE}}, and look for a module option that adjusts or disables this feature.<br />
<br />
Example: to disable the power saving mode and solve cracking sound trough speakers issue, using snd_hda_intel add in {{ic|/etc/modprobe.d/modprobe.conf}}<br />
options snd_hda_intel power_save=0<br />
or<br />
options snd_hda_intel power_save=0 power_save_controller=N<br />
<br />
You can also try it with {{ic|1=modprobe snd_hda_intel power_save=0}} before.<br />
<br />
You may also have to unmute the 'Line' ALSA channel for this to work. Any value will do (other than '0' or something too high).<br />
<br />
''Example:'' on an onboard VIA VT1708S (using the snd_hda_intel module) these cracks occured even though 'power_save' was set to 0. Unmuting the 'Line' channel and setting a value of '1' solved the issue.<br />
<br />
Source: https://www.kernel.org/doc/Documentation/sound/alsa/powersave.txt<br />
<br />
If you use a laptop, pm-utils will change {{ic|power_save}} back to 1 when you go onto battery power even if you disable power saving in {{ic|/etc/modprobe.d}}. Disable this for pm-utils by disabling the script that makes the change (see [https://wiki.archlinux.org/index.php/Pm-utils#Disabling_a_hook Disabling a hook] for more information):<br />
# touch /etc/pm/power.d/intel-audio-powersave<br />
<br />
===S/PDIF Output Does Not Work===<br />
<br />
If the optical/coaxial digital output of your motherboard/sound card is not working or stopped working, and have already enabled and unmuted it in alsamixer, try running<br />
# iecset audio on<br />
<br />
as root.<br />
<br />
You can also put this command in {{Ic|rc.local}} as it sometimes it may stop working after a reboot.<br />
<br />
===HDMI Output Does Not Work===<br />
If the HDMI output of your motherboard/sound card is not working or stopped working, and have already enabled and unmuted it in alsamixer, try the following.<br />
<br />
Query for Playback Devices:<br />
<br />
$ aplay -l<br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: NVidia [HDA NVidia], device 0: ALC1200 Analog [ALC1200 Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 1: ALC1200 Digital [ALC1200 Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 3: NVIDIA HDMI [NVIDIA HDMI]<br />
Subdevices: 0/1<br />
Subdevice #0: subdevice #0<br />
<br />
Now that we have the info for the HDMI device, try a test, In the example below, 0 is the card number and 3 is the device number.<br />
<br />
$ aplay -D plughw:0,3 /usr/share/sounds/alsa/Front_Center.wav<br />
<br />
If aplay does not output any errors, but still no sound is heared, "reboot" the receiver, monitor or tv set. Since the HDMI interface executes a handshake on connection, it might have noticed before that there was no audio stream embedded, and disabled audio decoding. In particular, if you are using a standalone window manager (don’t know about Gnome or KDE), you may need to have some sound playing ''while'' plugging in the HDMI cable.<br />
<br />
{{Note|If you are using an ATI card and linux 3.0, check your kernel/grub settings, you most likely need to use the ''''radeon.audio'''<nowiki>=</nowiki>'''1'''' boot parameter, or alternatively set up the option in your modprobe configuration files. It's important to note that before trying, is to check the [http://www.x.org/wiki/RadeonFeature#fndef-72849d6d6eb3927d486f50ece74a739042965a6c-25 Radeon Feature Matrix] to see if it's actually supported. At the moment this is written (march 29th, 2013), some chipsets of the [http://www.x.org/wiki/RadeonFeature#Decoder_ring_for_engineering_vs_marketing_names S. Islands] (namely HD7750 through HD7970) doesn't (still) support HDMI Audio.}}<br />
<br />
If the test is successful, create or edit your ~/.asoundrc file to set HDMI as the default audio device.<br />
<br />
{{hc|~/.asoundrc|<br />
pcm.!default {<br />
type hw<br />
card 0<br />
device 3<br />
}<br />
}}<br />
<br />
Or you above config does not work try:<br />
{{hc|~/.asoundrc|<br />
defaults.pcm.card 0<br />
defaults.pcm.device 3<br />
defaults.ctl.card 0<br />
}}<br />
<br />
===HDMI Multi-channel PCM output does not work (Intel) ===<br />
As of Linux 3.1 multi-channel PCM output through HDMI with a Intel card (Intel Eaglelake, IbexPeak/Ironlake,SandyBridge/CougarPoint and IvyBridge/PantherPoint) is not yet supported. Support for it has been recently added and expected to be available in Linux 3.2. To make it work in Linux 3.1 you need to apply the following patches:<br />
<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=76adaa34db407f174dd06370cb60f6029c33b465 drm: support routines for HDMI/DP ELD]<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=e0dac65ed45e72fe34cc7ccc76de0ba220bd38bb drm/i915: pass ELD to HDMI/DP audio driver]<br />
<br />
===HP TX2500===<br />
Add these 2 lines into {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-cmipci mpu_port=0x330 fm_port=0x388<br />
options snd-hda-intel index=0 model=toshiba position_fix=1<br />
<br />
options snd-hda-intel model=hp (works for tx2000cto)<br />
<br />
===Skipping Sound When Playing MP3===<br />
If you have sound skipping when playing MP3 files and you have more then 2 speakers attached to your computer (i.e. > 2 speaker system), run alsamixer and disable the channels for the speakers that you '''DO NOT''' have (i.e. do not enable the sound for the center speaker if you do not have a center speaker.<br />
<br />
===Using a USB Headset and External USB Sound Cards===<br />
If you are using a USB headset with ALSA you can try using {{AUR|asoundconf}} (currently only available from the [[AUR]]) to set the headset as the primary sound output. Before running make sure you have usb audio module enabled ({{ic|modprobe snd-usb-audio}}).<br />
<br />
# asoundconf is-active<br />
# asoundconf list<br />
# asoundconf set-default-card <chosen soundcard><br />
<br />
==== Crackling sound with USB devices ====<br />
If you experience crackling sound on USB devices, you can try tuning the snd-usb-audio for minimal latency.<br />
<br />
Add this to your {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
options snd-usb-audio nrpacks=1<br />
<br />
source: http://alsa.opensrc.org/Usb-audio#Tuning_USB_devices_for_minimal_latencies<br />
<br />
==== Hot-plugging a USB Sound Card ====<br />
In order to automatically make a USB Sound Card the primary output device, when the card is plugged in, you can use the following udev rules (e.g. add the following two lines to {{ic|/etc/udev/rules.d/00-local.rules}} and reboot).<br />
<br />
{{bc|1=KERNEL=="pcmC[D0-9cp]*", ACTION=="add", PROGRAM="/bin/sh -c 'K=%k; K=$${K#pcmC}; K=$${K%%D*}; echo defaults.ctl.card $$K > /etc/asound.conf; echo defaults.pcm.card $$K >>/etc/asound.conf'"<br />
KERNEL=="pcmC[D0-9cp]*", ACTION=="remove", PROGRAM="/bin/sh -c 'echo defaults.ctl.card 0 > /etc/asound.conf; echo defaults.pcm.card 0 >>/etc/asound.conf'"}}<br />
<br />
===Error 'Unknown hardware' Appears After a Kernel Update===<br />
The following messages may be displayed during the start-up ALSA after the kernel update:<br />
Unknown hardware "foo" "bar" ...<br />
Hardware is initialized using a guess method<br />
/usr/sbin/alsactl: set_control:nnnn:failed to obtain info for control #mm (No such file or directory)<br />
<br />
or<br />
Found hardware: "HDA-Intel" "VIA VT1705" "HDA:11064397,18490397,00100000" "0x1849" "0x0397"<br />
Hardware is initialized using a generic method<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #1 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #2 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #25 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #26 (No such file or directory)<br />
<br />
Simply store ALSA mixer settings again (as root):<br />
# alsactl -f /var/lib/alsa/asound.state store<br />
<br />
It may be necessary configure ALSA again with alsamixer<br />
<br />
===HDA Analyzer===<br />
If the mappings to your audio pins(plugs) do not correspond but ALSA works fine, you could try HDA Analyzer -- a pyGTK2 GUI for HD-audio control can be found [http://www.alsa-project.org/main/index.php/HDA_Analyzer at the ALSA wiki].<br />
Try tweaking the Widget Control section of the PIN nodes, to make microphones IN and headphone jacks OUT. Referring to the Config Defaults heading is a good idea.<br />
<br />
NOTE: the script is done by such way that it is incompatible with python3 (which is now shipped with ArchLinux) but tries to use it.<br />
The workaround is: open "run.py", find all occurences of "python" (2 occurences - one on the first line, and the second on the last line) and replace them all by "python2".<br />
<br />
NOTE2: the script requires root acces, but running it via su/sudo is bogus. Run it via {{ic|kdesu}} or {{ic|gksu}}.<br />
<br />
===ALSA with SDL===<br />
If you get no sound via SDL and ALSA cannot be chosen from the application. Try setting the environmental variable SDL_AUDIODRIVER to alsa.<br />
# export SDL_AUDIODRIVER=alsa<br />
<br />
===Low Sound Workaround===<br />
<br />
If you are facing low sound even after maxing out your speakers/headphones, you can give the softvol plugin a try. Add the following to {{ic|/etc/asound.conf}}.<br />
{{bc|<nowiki><br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
<br />
pcm.softvol {<br />
type softvol<br />
slave {<br />
pcm "dmix"<br />
}<br />
control {<br />
name "Pre-Amp"<br />
card 0<br />
}<br />
min_dB -5.0<br />
max_dB 20.0<br />
resolution 6<br />
}<br />
</nowiki>}}<br />
{{note| You will probably have to restart the computer, as restarting the alsa daemon did not load the new configuration for me. Also, if the configuration does not work even after restarting, try changing {{ic|plug}} with {{ic|hw}} in the above configuration.}}<br />
<br />
After the changes are loaded successfully, you will see a {{ic|Pre-Amp}} section in alsamixer. You can adjust the levels there.<br />
{{note|Setting a high value for {{ic|Pre-Amp}} can cause sound distortion, so adjust it according to the level that suits you.}}<br />
<br />
===Popping sound after resuming from suspension===<br />
You might hear a popping sound after resuming the computer from suspension. This can be fixed by editing {{ic|/etc/pm/sleep.d/90alsa}} and removing the line that says {{ic|aplay -d 1 /dev/zero}}<br />
<br />
==设置文件范例==<br />
参见:[[Advanced Linux Sound Architecture/Example Configurations]]。<br />
<br />
==相关阅读==<br />
* [http://www.mjmwired.net/kernel/Documentation/sound/alsa/ALSA-Configuration.txt ALSA 模块高级配置]<br />
* [http://alsa.opensrc.org/Main_Page 非官方的 ALSA Wiki]<br />
* [https://bbs.archlinux.org/viewtopic.php?id=36815 如何从 svn 获取驱动代码并编译]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Advanced_Linux_Sound_Architecture_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=255076
Advanced Linux Sound Architecture (简体中文)
2013-04-25T02:37:10Z
<p>Cuihao: 囧,最近比较忙,烂尾了…… 继续干</p>
<hr />
<div>[[Category:简体中文]]<br />
[[Category:Sound (简体中文)]]<br />
[[Category:Audio/Video (简体中文)]]<br />
[[cs:Advanced Linux Sound Architecture]]<br />
[[de:Alsa]]<br />
[[es:Advanced Linux Sound Architecture]]<br />
[[fa:ALSA]]<br />
[[fr:Alsa]]<br />
[[he:Advanced Linux Sound Architecture]]<br />
[[it:Advanced Linux Sound Architecture]]<br />
[[ja:Advanced Linux Sound Architecture]]<br />
[[nl:Advanced Linux Sound Architecture]]<br />
[[pt:Advanced Linux Sound Architecture]]<br />
[[ru:Advanced Linux Sound Architecture]]<br />
[[sk:Advanced Linux Sound Architecture]]<br />
[[th:Advanced Linux Sound Architecture]]<br />
[[tr:Gelişmiş_ses_mimarisi_(ALSA)]]<br />
[[en:Advanced Linux Sound Architecture]]<br />
<br />
{{Article summary start}}<br />
{{Article summary text|ALSA 的使用、配置及疑难解答。}}<br />
{{Article summary heading|相关页面}}<br />
{{Article summary wiki|Sound system (简体中文)}}<br />
{{Article summary wiki|Disable PC Speaker Beep (简体中文)}}<br />
{{Article summary end}}<br />
{{TranslationStatus (简体中文)|Advanced Linux Sound Architecture|2013-03-14|254584}}<br />
<br />
[[Wikipedia:zh:ALSA|高级 Linux 声音体系]](Advanced Linux Sound Architecture,'''ALSA''')是Linux中提供声音设备驱动的内核组件,用来代替原来的开放声音系统(Open Sound System,OSSv3)。除了声音设备驱动,'''ALSA'''还包含一个用户空间的函数库,以方便开发者通过高级API使用驱动功能,而不必直接与内核驱动交互。<br />
<br />
{{注意|关于另一种声音体系,请阅读[[Open Sound System (简体中文)|开放声音系统(OSS)]]的页面。}}<br />
<br />
==安装==<br />
Arch 默认的内核已经通过一套模块提供了 ALSA,不必特别安装。<br />
<br />
[[udev (简体中文)|udev]]会在系统启动时自动检测硬件,并加载相应的声音设备驱动模块。这时,你的声卡已经可以工作了,只是所有声道默认都被设置成静音了。<br />
<br />
在本地登录(通过虚拟终端或登录管理器)的用户,都有权限播放音频并调整音量。要让远程登录的用户拥有这些权限,必须把用户[[Users and Groups (简体中文)#用户组管理|加入]] {{ic|audio}} 用户组。该组的成员可以直接访问声音设备,会导致某些程序独占音频输出(破坏软件混音),还可能影响用户快速切换和拖机(multiseat)。因此,除非真的有某些[https://wiki.ubuntu.com/Audio/TheAudioGroup 特殊需求],'''不建议'''把用户加入 {{ic|audio}} 用户组。<br />
<br />
===用户空间工具===<br />
从 [[Official Repositories (简体中文)|官方仓库]]:<br />
* [[pacman (简体中文)|安装]] {{Pkg|alsa-utils}} 软件包,其中的 {{ic|alsamixer}} 工具允许用户在控制台或终端中配置声音设备。<br />
* 安装 {{Pkg|alsa-oss}} 软件包,如果想让 OSS 应用程序与dmix(软件混音)协同工作。 <br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。}}<br />
*安装 {{Pkg|alsa-plugins}} 软件包,如果需要 [[#高质量重采样|高质量重采样]]、[[#Upmixing/Downmixing|软件模拟环绕立体声]]和其他高级特性。<br />
<br />
==解除各声道的静音==<br />
目前版本的 ALSA 安装后,所有声道'''默认是静音的''',必须手动解除。<br />
<br />
使用 {{ic|alsamixer}} 的 ncurses 界面,配置十分简单:<br />
<br />
$ alsamixer<br />
<br />
此外,还可以在命令行下使用 {{ic|amixer}}:<br />
<br />
$ amixer sset Master unmute<br />
<br />
在 {{ic|alsamixer}} 中,下方标有 {{ic|MM}} 的声道是静音的,而标有 {{ic|00}} 的通道已经启用。<br />
<br />
使用左右方向键,选中 {{ic|Master}} 和 {{ic|PCM}} 声道。按下 {{keypress|m}} 键解除静音。使用上方向键增加音量,直到增益值为{{ic|0}}。该值显示在左上方 {{ic|Item:}} 字段后。过高的增益值会导致声音失真。<br />
<br />
要想得到完整的 5.1 或 7.1 环绕立体声,还得解除 Front、Surround、Center、LFE (subwoofer) 和 Side 这些声道的静音(上述名称是 Intel HD Audio 声卡使用的声道名,可能因设备不同而有所差异)。注意,仅有这些设置,系统不会自动将立体声源(多数音乐)提升(upmix)成环绕立体声。如果需要这些功能,请阅读[[#Upmixing/Downmixing]]。<br />
<br />
要启用麦克风,切换至 Capture 选项卡,按下 {{keypress|F4}},按下 {{keypress|空格}} 启用其中一个声道即可。<br />
<br />
按下 {{Keypress|Esc}} 键退出 alsamixer。<br />
<br />
{{注意|<br />
* 有一些声卡,需要关闭数字输出或将其调成静音,才能输出模拟音频信号。对于 Soundblaster Audigy LS 声卡,需要把 IEC958 通道设成静音。 <br />
* 有些机器(如 Thinkpad T61),有一个 Speaker 通道,必须按照上述方法打开调整。<br />
* 有些机器(如 Dell E6400),可能还需要把 {{ic|Front}} 和 {{ic|Headphone}} 声道打开调整。}}<br />
<br />
接下来,测试声卡是否工作:<br />
<br />
$ speaker-test -c 2<br />
<br />
根据扬声器的实际情况,调整 -c 后的数值。对于 7.1 声道,这个数字是 8:<br />
<br />
$ speaker-test -c 8<br />
<br />
如果没有正常工作,请继续阅读 [[#配置]] 以及 [[#疑难解答]] 部分。<br />
<br />
{{pkg|alsa-utils}} 软件包提供了两个配置 alsa 的服务:{{ic|alsa-restore.service}} 和 {{ic|alsa-store.service}}。它们分别在开机和关机时自动运行。<br />
<br />
==配置==<br />
=== VirtualBox 虚拟机无声音 ===<br />
<br />
如果使用 VirtualBox 时碰上了麻烦,请尝试以下命令:<br />
<br />
{{hc|$ alsactl init|2=<nowiki><br />
<br />
Found hardware: "ICH" "SigmaTel STAC9700,83,84" "AC97a:83847600" "0x8086" "0x0000"<br />
Hardware is initialized using a generic method<br />
</nowiki><br />
}}<br />
<br />
如果还是不行,请检查所使用的音频软件是否激活了 ALSA 输出。<br />
<br />
===设置默认声卡===<br />
<br />
如果发现开机时声卡次序会发生变化,可以在通过 {{ic|/etc/modprobe.d}} 中的 {{ic|.conf}} 文件(比如 {{ic|/etc/modprobe.d/alsa-base.conf}})手动设置次序。<br />
比如,要让 mia 声卡成为 #0、Intel HDA 声卡成为 #1:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<br />
options snd slots=snd_mia,snd_hda_intel<br />
options snd_mia index=0<br />
options snd_hda_intel index=1<br />
}}<br />
<br />
使用 {{ic|<nowiki>lsmod | grep snd</nowiki>}} 获取设备列表。<br />
<br />
snd_mia 和 snd_hda_intel 分别是两个声卡的内核驱动模块。该配置仅适用于同时装有 mia 声卡和使用 snd_hda_intel 驱动的声卡(通常是板载声卡)的情况。<br />
<br />
如果使用 -2 的 index 值,ALSA 就不会将对应的设备作为主声卡使用。Linux Mint 和 Ubuntu 等发行版使用了以下配置,避免 USB 声卡和其他“非主流”设备变成 index 为 0 的主声卡:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<br />
options bt87x index=-2<br />
options cx88_alsa index=-2<br />
options saa7134-alsa index=-2<br />
options snd-atiixp-modem index=-2<br />
options snd-intel8x0m index=-2<br />
options snd-via82xx-modem index=-2<br />
options snd-usb-audio index=-2<br />
options snd-usb-caiaq index=-2<br />
options snd-usb-ua101 index=-2<br />
options snd-usb-us122l index=-2<br />
options snd-usb-usx2y index=-2<br />
# Keep snd-pcsp from being loaded as first soundcard<br />
options snd-pcsp index=-2<br />
# Keep snd-usb-audio from beeing loaded as first soundcard<br />
options snd-usb-audio index=-2<br />
}}<br />
<br />
以上配置会在系统重启后生效。<br />
<br />
{{Out of date|据英文 Wiki 用户反应,{{ic|/etc/asound.conf}} 这个配置文件默认是不存在的,下列操作可能有误。}}<br />
<br />
首先,运行 {{ic|aplay -l}},获取声卡的声卡ID和设备ID:<br />
<br />
{{hc|$ aplay -l|2=<nowiki><br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: Intel [HDA Intel], device 0: CONEXANT Analog [CONEXANT Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: Intel [HDA Intel], device 1: Conexant Digital [Conexant Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 1: JamLab [JamLab], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 2: Audio [Altec Lansing XT1 - USB Audio], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
</nowiki>}}<br />
<br />
拿输出中的最后一项来说,这个声卡的声卡ID为2,设备ID为0。若要把这块声卡作为默认声卡,可以把下列配置添加到系统级别的 {{ic|/etc/asound.conf}} 或用户级别的 {{ic|~/.asoundrc}} 文件。如果文件不存在,可以手动创建。其中的各个ID,请根据实际情况调整:<br />
{{bc|<br />
defaults.pcm.card 2<br />
defaults.pcm.device 0<br />
defaults.ctl.card 2<br />
}}<br />
“pcm”选项决定用来播放音频的设备,而“ctl”选项决定那个声卡能够由控制工具(如 alsamixer)使用。<br />
<br />
上述配置在重启音频程序(如 mplayer)后立即生效。<br />
<br />
===确认所有声音模块都已经加载===<br />
<br />
一般 udev 都会自动识别出声卡。使用以下命令确认:<br />
<br />
{{hc|<nowiki>$ lsmod | grep '^snd' | column -t</nowiki>|<nowiki><br />
snd_hda_codec_hdmi 22378 4<br />
snd_hda_codec_realtek 294191 1<br />
snd_hda_intel 21738 1<br />
snd_hda_codec 73739 3 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel<br />
snd_hwdep 6134 1 snd_hda_codec<br />
snd_pcm 71032 3 snd_hda_codec_hdmi,snd_hda_intel,snd_hda_codec<br />
snd_timer 18992 1 snd_pcm<br />
snd 55132 9 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel,snd_hda_codec,snd_hwdep,snd_pcm,snd_timer<br />
snd_page_alloc 7017 2 snd_hda_intel,snd_pcm<br />
</nowiki>}}<br />
<br />
如果你的输出和上面类似,那就说明声卡已经被正确识别。<br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。如有需要,请[[Kernel_modules_(简体中文)#装入|手动加载]]。}}<br />
<br />
还可以检查一下 {{ic|/dev/snd/}} 目录,看看是否有这些设备文件:<br />
<br />
{{hc|$ ls -l /dev/snd|2=<nowiki><br />
total 0<br />
crw-rw---- 1 root audio 116, 0 Apr 8 14:17 controlC0<br />
crw-rw---- 1 root audio 116, 32 Apr 8 14:17 controlC1<br />
crw-rw---- 1 root audio 116, 24 Apr 8 14:17 pcmC0D0c<br />
crw-rw---- 1 root audio 116, 16 Apr 8 14:17 pcmC0D0p<br />
crw-rw---- 1 root audio 116, 25 Apr 8 14:17 pcmC0D1c<br />
crw-rw---- 1 root audio 116, 56 Apr 8 14:17 pcmC1D0c<br />
crw-rw---- 1 root audio 116, 48 Apr 8 14:17 pcmC1D0p<br />
crw-rw---- 1 root audio 116, 1 Apr 8 14:17 seq<br />
crw-rw---- 1 root audio 116, 33 Apr 8 14:17 timer<br />
</nowiki>}}<br />
<br />
{{注意|在 IRC 或论坛寻求这方面帮助时,别忘了贴出上面几个命令的输出。}}<br />
<br />
如果你的输出跟上面类似,或至少有 '''controlC0''' 和 '''pcmC0D0p''',那么声卡就已经正常加载了。<br />
<br />
如果出现问题,声卡模块没有正确加载,那么请尝试手动加载模块:<br />
<br />
* 确定声卡对应的驱动模块:[http://www.alsa-project.org/main/index.php/Matrix:Main ALSA Soundcard Matrix]。这些模块都会有一个”snd-“前缀(例如:{{ic|snd-via82xx}})。<br />
* [[Kernel_modules_(简体中文)#装入|加载模块]]。<br />
* 检查 {{ic|/dev/snd}} 目录中的设备文件(参见上文);或者,检查 {{ic|alsamixer}} 或 {{ic|amixer}} 的输出是否正确。<br />
* 修改配置,使 {{ic|snd-<模块名>}} 和 {{ic|snd-pcm-oss}} 模块[[Kernel_modules_(简体中文)#装入|开机自动加载]]。<br />
<br />
===启用 SPDIF 输出===<br />
<br />
(由 Gentoo 论坛的 gralves 提供)<br />
* 在 GNOME 音量控制的选项(Options)标签中,把 IEC958 改成 PCM。该选项可以在首选项中启用。<br />
* 不使用 GNOME 音量控制工具的话:<br />
** 编辑 {{ic|/var/lib/alsa/asound.state}}。该文件是 alsasound 保存混音器设置的地方。<br />
** 找到这一行:“IEC958 Playback Switch”。该行上下应该有一行内容为 {{ic|value:false}},将其改为 {{ic|value:true}}。<br />
** 再找到这一行:”IEC958 Playback AC97-SPSA“。把对应的值改为 0。<br />
** 重启 ALSA。<br />
<br />
{{Out of date|rc.local 在 systemd 中已经弃用,以下方法可能不再适用。}}<br />
<br />
还有另外一个开机自动启用 SPDIF 输出的方法(在 SoundBlaster Audigy 声卡上测试过):<br />
<br />
* 在 {{ic|/etc/rc.local}} 中添加如下内容:<br />
# Use COAX-digital output<br />
amixer set 'IEC958 Optical' 100 unmute<br />
amixer set 'Audigy Analog/Digital Output Jack' on<br />
<br />
使用以下命令,获取你的声卡的数字输出的名称:<br />
$ amixer scontrols<br />
<br />
===系统级均衡器===<br />
====使用 AlsaEqual(包含界面)====<br />
从 [[AUR (简体中文)|AUR]] 安装 {{AUR|alsaequal}}。<br />
<br />
{{注意|如果使用的是64位系统,而又安装了32位的 Flash 插件,这里的设置会导致 Flash 无声。如果发生了这种情况,请禁用 alsaequal 或编译32位版本的 alsaequal。}}<br />
<br />
安装后,把下列内容添加到 ALSA 配置文件({{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}}):<br />
<br />
ctl.equal {<br />
type equal;<br />
}<br />
<br />
pcm.plugequal {<br />
type equal;<br />
# Modify the line below if you do not<br />
#slave.pcm "plughw:0,0";<br />
#by default we want to play from more sources at time:<br />
slave.pcm "plug:dmix";<br />
}<br />
#pcm.equal {<br />
# If you do not want the equalizer to be your<br />
# default soundcard comment the following<br />
# line and uncomment the above line. (You can<br />
# choose it as the output device by addressing<br />
# it with specific apps,eg mpg123 -a equal 06.Back_In_Black.mp3)<br />
pcm.!default {<br />
type plug;<br />
slave.pcm plugequal;<br />
}<br />
<br />
设置好后,切换均衡器:<br />
$ alsamixer -D equal<br />
<br />
需要注意,每个用户的 alsaequal 配置文件都是独立的,位于 {{ic|~/.alsaequal.bin}}。因此,如果想在 [[mpd]] 之类的以独立用户身份运行的程序中使用 AlsaEqual,就得像下面这样:<br />
# su mpd -c 'alsamixer -D equal'<br />
或者,在其他用户的主目录下创建符号链接,指向你的 {{ic|.alsaequal.bin}}……<br />
<br />
=====管理 AlsaEqual 配置=====<br />
从 [http://xyne.archlinux.ca/repos/ Xyne 的软件仓库] 或 [https://aur.archlinux.org/packages.php?ID=62420 AUR] 安装 [http://xyne.archlinux.ca/projects/alsaequal-mgr/ alsaequal-mgr]。<br />
<br />
同上,配置均衡器: <br />
$alsamixer -D equal<br />
<br />
如果对效果感到满意,将其命名(如“foo”)并保存该配置:<br />
$alsaequal-mgr save foo<br />
<br />
重新加载“foo”中的配置:<br />
$alsaequal-mgr load foo<br />
<br />
这样,就可以为游戏、电影、不同音乐流派、网络电话等等创建不同的均衡器配置,以便随时切换。<br />
<br />
参见[http://xyne.archlinux.ca/projects/alsaequal-mgr/ 项目主页]和程序的帮助信息,了解更多设置。<br />
<br />
====使用 mbeq====<br />
{{注意|该软件用到了一个 ladspa 插件,可能导致播放音频时 CPU 占用率偏高。In addition, this was made with stereophonic sound (e.g. headphones) in mind.}}<br />
<br />
安装 {{Pkg|alsa-plugins}}、{{Pkg|ladspa}} 和 {{Pkg|swh-plugins}} 软件包。<br />
<br />
* 在 {{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}} 中添加如下内容:<br />
<br />
{{hc|/etc/asound.conf|2=<nowiki>pcm.eq {<br />
type ladspa<br />
<br />
# The output from the EQ can either go direct to a hardware device<br />
# (if you have a hardware mixer, e.g. SBLive/Audigy) or it can go<br />
# to the software mixer shown here.<br />
#slave.pcm "plughw:0,0"<br />
slave.pcm "plug:dmix"<br />
<br />
# Sometimes you may need to specify the path to the plugins,<br />
# especially if you have just installed them. Once you have logged<br />
# out/restarted this should not be necessary, but if you get errors<br />
# about being unable to find plugins, try uncommenting this.<br />
#path "/usr/lib/ladspa"<br />
<br />
plugins [<br />
{<br />
label mbeq<br />
id 1197<br />
input {<br />
#this setting is here by example, edit to your own taste<br />
#bands: 50hz, 100hz, 156hz, 220hz, 311hz, 440hz, 622hz, 880hz, 1250hz, 1750hz, 25000hz,<br />
#50000hz, 10000hz, 20000hz<br />
controls [ -5 -5 -5 -5 -5 -10 -20 -15 -10 -10 -10 -10 -10 -3 -2 ]<br />
}<br />
}<br />
]<br />
}<br />
<br />
# Redirect the default device to go via the EQ - you may want to do<br />
# this last, once you are sure everything is working. Otherwise all<br />
# your audio programs will break/crash if something has gone wrong.<br />
<br />
pcm.!default {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
<br />
# Redirect the OSS emulation through the EQ too (when programs are running through "aoss")<br />
<br />
pcm.dsp0 {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
</nowiki>}}<br />
<br />
* 完成。若有问题,欢迎前往论坛咨询。<br />
<br />
==高质量重采样==<br />
启用软件混音时,ALSA 会强制把所有音频重采样到相同的频率(如果系统支持,默认是 48000)。dmix 的重采样算法很糟糕,会导致显著的质量损失。<br />
<br />
安装 {{pkg|alsa-plugins}} 和 {{pkg|libsamplerate}}。<br />
<br />
将默认的采样率转换器更改为 libsamplerate:<br />
<br />
{{hc|/etc/asound.conf|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
或:<br />
<br />
{{hc|~/.asoundrc|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
'''samplerate_best''' 输出的声音质量最佳,但需要耗费大量CPU资源进行实时重采样。还有其他的算法可供选择(如 '''samplerate''' 等),但比起默认的算法,这些算法的改进并不明显。<br />
<br />
{{警告|某些系统中,启用 samplerate_best 可能导致 Flash 播放器无声音。}}<br />
<br />
==Upmixing/Downmixing==<br />
===Upmixing===<br />
In order for stereo sources like music to be able to saturate a 5.1 or 7.1 sound system, you need to use upmixing. In darker days this used to be tricky and error prone but nowadays plugins exist to easily take care of this task. Thus, install {{Pkg|alsa-plugins}}.<br />
<br />
Then add the following to your ALSA configuration file of choice (either {{ic|/etc/asound.conf}} or {{ic|~/.asoundrc}}):<br />
pcm.upmix71 {<br />
type upmix<br />
slave.pcm "surround71"<br />
delay 15<br />
channels 8<br />
}<br />
<br />
You can easily change this example for 7.1 upmixing to 5.1 or 4.0.<br />
<br />
This adds a new pcm that you can use for upmixing. If you want all sound sources to go through this pcm, add it as a default below the previous definition like so:<br />
<br />
pcm.!default "plug:upmix71"<br />
<br />
The plugin automatically allows multiple sources to play through it without problems so setting is as a default is actually a safe choice. <br />
If this is not working, you have to setup your own dmixer for the upmixing PCM like this: <br />
pcm.dmix6 {<br />
type asym<br />
playback.pcm {<br />
type dmix<br />
ipc_key 567829<br />
slave {<br />
pcm "hw:0,0"<br />
channels 6<br />
}<br />
}<br />
}<br />
<br />
and use "dmix6" instead of "surround71". <br />
If you experience skipping or distorted sound, consider increasing the buffer_size (to 32768, for example) or use a [[#高质量重采样|high quality resampler]].<br />
<br />
===Downmixing===<br />
If you want to downmix sources to stereo because you, for instance, want to watch a movie with 5.1 sound on a stereo system, you need to use the vdownmix plugin that alsa provides in the same package.<br />
<br />
Again, in your configuration file, add this:<br />
<br />
pcm.!surround51 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
pcm.!surround40 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
<br />
==混音==<br />
===软件混音(dmix)===<br />
<br />
{{注意|在 ALSA 1.0.9rc2 及以上版本中,如果声卡不支持硬件混音,模拟输出将默认使用 dmix 混音,无需额外设置。}}<br />
<br />
如果软件混音未正常启用,那么可以手动创建 {{ic|~/.asoundrc}},加入以下内容:<br />
<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
<br />
这样就启用了软件混音,多个程序能够同时使用声卡。<br />
<br />
至于 S/PDIF 之类的数字音频输出,目前的 ALSA 还不会默认启用 dmix。因此,必须像上面这样手动启用。<br />
<br />
常见问题的解答,参见[[#疑难解答]]部分。<br />
<br />
===硬件混音===<br />
====支持====<br />
如果声卡支持,一般无需任何配置即可启用硬件混音。绝大多数板载声卡都不支持硬件混音,必须得使用软件混音(见上文)。很多独立声卡都支持硬件混音,Linux 下支持较好的有:<br />
<br />
* Creative SoundBlaster Live! (5.1 型号)<br />
* Creative SoundBlaster Audigy (部分型号)<br />
* Creative SoundBlaster Audidy 2 (ZS 系列型号)<br />
* Creative SoundBlaster Audigy 4 (Pro 系列型号)<br />
<br />
{{注意|上述声卡的低端版本(Audigy SE、Audigy 2 NX、SoundBlaster Live! 24bit 以及 SoundBlaster Live! 7.1)使用了不同的芯片,'''不支持'''硬件混音。}}<br />
<br />
{{注意|板载 VIA8237 芯片支持4路硬件混音,但在某些主板上只能使用3路(第4路无声),甚至根本无法使用。即使能正常工作,质量也比不上其他混音方法。}}<br />
<br />
====问题修复====<br />
如果你的声卡是 Intel Corporation 82801I (ICH9 Family) HD Audio Controller (rev 02),且使用了64位系统,需要执行以下命令才能在 Enemy Territory 中听到声音:<br />
echo "et.x86 0 0 direct" > /proc/asound/card0/pcm0p/oss<br />
echo "et.x86 0 0 disable" > /proc/asound/card0/pcm0c/oss<br />
<br />
==疑难解答==<br />
===使用CPU频率动态调整时,音频发生跳跃===<br />
使用 {{ic|ondemand}} 或 {{ic|conservative}} 频率策略、启用CPU频率动态调整时,某些声卡和某些 ALSA 驱动会出现声音跳跃的情况。目前,唯一的解决方案是使用固定的频率,如 {{ic|performance}} 性能策略。<br />
<br />
详情参见 [[CPU Frequency Scaling (简体中文)]]。<br />
<br />
===同时只能有一个用户播放音频===<br />
有时会出现一个用户独占软件混音器的情况。多数情况下这没什么影响,但在使用 [[mpd]] 这样以独立用户身份运行的程序时,就会出现问题。当 mpd 运行时,用户自己就无法播放其他音频了。一种解决方法是,以正在使用的用户身份运行 mpd。另一种方法是,在 ALSA 配置文件的 {{ic|pcm.dmixer}} 部分添加 {{ic|ipc_key_add_uid 0}} 禁止独占。以下是 {{ic|asound.conf}} 示例的片段,省略的部分无需修改:<br />
<br />
...<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
slave {<br />
...<br />
<br />
===无法同时播放多个音频===<br />
如果安装了 [[PulseAudio (简体中文)|PulseAudio]](可能作为 [[GNOME (简体中文)|GNOME]] 的依赖被安装),可能出现无法同步播放的问题,因为 PulseAudio 的默认配置是“独占”声卡。ALSA 的用户可能对自己的配置已经很满意、不希望使用 [[PulseAudio (简体中文)|PulseAudio]]。解决方案是编辑 {{ic|/etc/asound.conf}} 并注释掉以下内容:<br />
# Use PulseAudio by default<br />
#pcm.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
# hint {<br />
# show on<br />
# description "Default ALSA Output (currently PulseAudio Sound Server)"<br />
# }<br />
#}<br />
<br />
可能还需要注释掉下面的内容:<br />
#ctl.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
#}<br />
<br />
比起完全卸载 [[PulseAudio (简体中文)|PulseAudio]],这大概是个更简单的方法。<br />
<br />
以下是 {{ic|/etc/asound.conf}} 配置文件的一个示例:<br />
{{bc|<nowiki>pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
}<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}</nowiki><br />
}}<br />
<br />
{{注意|本 {{ic|/etc/asound.conf}} 本来是配合 [[MPD]] 全局配置使用的。参见[[#同时只能有一个用户播放音频|该节]]关于多用户的内容。}}<br />
<br />
===有时候开机无声===<br />
如果会随机地出现开机无声的问题,原因可能是多声卡的顺序发生了变化。如果是这种情况,请参照[[#设置默认声卡]]进行设置。<br />
<br />
对于 mpd 用户,上述配置无效,请参考[http://mpd.wikia.com/wiki/Configuration#ALSA_MPD_software_volume_control 该文]设置。<br />
<br />
===特定程序的问题===<br />
对于不使用系统声音配置的程序,如 XMMS 和 Mplayer,必须特别设置以应用 ALSA 配置。<br />
<br />
对于 mplayer,将以下内容添加到 {{ic|~/.mplayer/config}} (或系统全局配置文件 {{ic|/etc/mplayer/mplayer.conf}}):<br />
ao=alsa<br />
<br />
对于 XMMS 和 Beep Media Player,打开选项窗口,要把声音驱动设置为 ALSA 而不是 OSS。<br />
<br />
XMMS 的设置方法:<br />
* 启动 XMMS<br />
** 选项 -> 首选项<br />
** 选择 Alsa 输出插件<br />
<br />
对于未提供 ALSA 输出的程序,可以使用 alsa-oss 软件包中的 aoss 工具启动。使用方法是:在要运行的程序的命令行前加上 {{ic|aoss}}。例如:<br />
aoss realplay<br />
<br />
pcm.!default{ ... } 貌似已经不管用了。但下面的设置有效:<br />
pcm.default pcm.dmixer<br />
<br />
===特定型号的配置===<br />
ALSA 会通过 BIOS 检测[http://www.kernel.org/doc/Documentation/sound/alsa/HD-Audio-Models.txt 声卡型号],少数情况下也可能无法正确识别。声卡芯片类型可以在 {{ic|alsamixer}} 界面上找到(如 ALC662),而型号则要设置在 {{ic|/etc/modprobe.d/modprobe.conf}} 或 {{ic|/etc/modprobe.d/sound.conf}} 文件中:<br />
<br />
options snd-hda-intel model=<型号><br />
<br />
具体设置因声卡型号而异。多数情况下不需要特地这样做。如果想知道具体声卡型号的设置,请查阅 [http://bugtrack.alsa-project.org/main/index.php/Matrix:Main Alsa Soundcard List],找到你的声卡型号,点击“Details”,然后查看“Setting up modprobe...”小节,将其中的内容插入 {{ic|/etc/modprobe.d/modprobe.conf}} 文件即可。例如,Intel AC97 的配置如下:<br />
<br />
{{bc|# ALSA portion<br />
alias char-major-116 snd<br />
alias snd-card-0 snd-intel8x0<br />
# module options should go here<br />
<br />
# OSS/Free portion<br />
alias char-major-14 soundcore<br />
alias sound-slot-0 snd-card-0<br />
<br />
# card #1<br />
alias sound-service-0-0 snd-mixer-oss<br />
alias sound-service-0-1 snd-seq-oss<br />
alias sound-service-0-3 snd-pcm-oss<br />
alias sound-service-0-8 snd-seq-oss<br />
alias sound-service-0-12 snd-pcm-oss}}<br />
<br />
===与蜂鸣器冲突===<br />
如果你确认所有声道都已解除静音,声卡驱动配置正确,且音量大小也合适,但还是听不到声音,那么请试试将下面的内容添加到 {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
(如果是 {{ic|via82xx}} 驱动)<br />
options snd-<模块名请自行补充> ac97_quirk=0<br />
<br />
(如果是 {{ic|snd_intel8x0}} 驱动)<br />
options snd-NAME-OF-MODULE ac97_quirk=1<br />
<br />
===麦克风输入无声音===<br />
In alsamixer, make sure that all the volume levels are up under recording, and that CAPTURE is toggled active on the microphone (e.g. Mic, Internal Mic) and/or on Capture (in alsamixer, select these items and press space). Try making positive Mic Boost and raising Capture and Digital levels higher; this make make static or distortion, but then you can adjust them back down once you are hearing ''something'' when you record<br />
<br />
As the pulseaudio wrapper is shown as "default" in alsamixer, you may have to press F6 to select your actual soundcard first. You may also need to enable and increase the volume of Line-in in the Playback section.<br />
<br />
To test the microphone, run these commands (see arecord's man page for further information):<br />
arecord -d 5 test-mic.wav<br />
aplay test-mic.wav<br />
<br />
If all fails, you may want to eliminate hardware failure by testing the microphone with a different device.<br />
<br />
For at least some computers, muting a microphone (MM) simply means its input does not go immediately to the speakers. It still receives input.<br />
<br />
Many Dell laptops need "-dmic" to be appended to the model name in {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-hda-intel model=dell-m6-dmic<br />
<br />
Some programs use try to use OSS as the main input software. Add the following lines to {{ic|/etc/modprobe.d/modprobe.conf}} to prevent OSS modules from being loaded:<br />
<br />
{{Note|The OSS modules are no longer autoloaded anyway.}}<br />
<br />
blacklist snd_pcm_oss<br />
blacklist snd_mixer_oss<br />
blacklist snd_seq_oss<br />
<br />
See also:<br />
* http://www.alsa-project.org/main/index.php/SoundcardTesting<br />
* http://alsa.opensrc.org/Record_from_mic<br />
<br />
===设置默认的麦克风/录音设备===<br />
某些程序(Pidgin、Adobe Flash)提供了设置录音设备的选项。如果录音设备不是内置的声卡,而是外接的(如USB摄像头、麦克风),就会有些问题。如果只想更改默认的录音设备、不更改回放设备,需要像下面这样设置 {{ic|~/.asoundrc}}:<br />
<br />
pcm.usb<br />
{<br />
type hw<br />
card U0x46d0x81d<br />
}<br />
<br />
pcm.!default<br />
{<br />
type asym<br />
playback.pcm<br />
{<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
capture.pcm<br />
{<br />
type plug<br />
slave.pcm "usb"<br />
}<br />
}<br />
<br />
把“U0x46d0x81d”替换为你的录音设备的名称。可以通过 {{ic|arecord -L}} 查看所有 ALSA 识别到的录音设备。<br />
<br />
===内置麦克风无效===<br />
First make sure all the volume levels are up under recording in alsamixer. In my case adding the following option to /etc/sound.conf and reloading the snd-* module produced a new volume setting called Capture which was capturing for the internal mic. For eg, for snd-hda-intel add<br />
<br />
options snd-hda-intel enable_msi=1<br />
<br />
Then reload the module (as below), up the recording volume of Capture and then test.<br />
<br />
{{bc|# rmmod snd-hda-intel && modprobe snd-hda-intel}}<br />
<br />
===Intel 板载声卡无声===<br />
可能由两个相互冲突的驱动模块引起,分别是 {{ic|snd_intel8x0}} 和 {{ic|snd_intel8x0m}}。如果是的话,屏蔽 snd_intel8x0m 即可:<br />
<br />
{{hc|/etc/modprobe.d/modprobe.conf|blacklist snd_intel8x0m}}<br />
<br />
以下方法可能也有效:在 {{ic|alsamixer}} 或 {{ic|amixer}} 把 “External Amplifier” 设置为静音。参见:[http://alsa.opensrc.org/Intel8x0#Dell_Inspiron_8600_.28and_probably_others.29 ALSA wiki 相关条目]。<br />
<br />
也可以试试解除“Mix”的静音。<br />
<br />
===No Headphone Sound with Onboard Intel Sound Card===<br />
With '''Intel Corporation 82801 I (ICH9 Family) HD Audio Controller''' on laptop, you may need to add this line to modprobe or sound.conf:<br />
<br />
options snd-hda-intel model=$model<br />
<br />
Where $model is any one of the following (in order of possibility to work, but not merit):<br />
<br />
* dell-vostro<br />
* olpc-xo-1_5<br />
* laptop<br />
* dell-m6<br />
* laptop-hpsense<br />
<br />
Note: It may be necessary to put this "options" line below (after) any "alias" lines about your card.<br />
<br />
You can see all the available models in the kernel documentation. For example [http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.35.y.git;a=blob;f=Documentation/sound/alsa/HD-Audio-Models.txt;h=dc25bb84b83b49665a7ed850e7bf5423d50cd3ba;hb=HEAD here], but check that it is the correct version of that document for your kernel version.<br />
<br />
A list of available models is also available [http://www.mjmwired.net/kernel/Documentation/sound/alsa/HD-Audio-Models.txt here]. To know your chip name type the following command (with * being corrected to match your files). Note that some chips could have been renamed and do not directly match the available ones in the file.<br />
<br />
cat /proc/asound/card*/codec* | grep Codec<br />
<br />
Note that there is a high chance none of the input devices (all internal and external mics) will work if you choose to do this, so it is either your headphones or your mic. Please report to ALSA if you are affected by this bug.<br />
<br />
And also, if you have problems getting beeps to work (pcspkr):<br />
<br />
options snd-hda-intel model=$model enable=1 index=0<br />
<br />
===No sound when S/PDIF video card is installed===<br />
Discover available modules and their order:<br />
$ cat /proc/asound/modules<br />
0 snd_hda_intel<br />
1 snd_ca0106<br />
<br />
Disable the undesired video card audio codec in {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
# /etc/modprobe.d/modprobe.conf<br />
#<br />
install snd_hda_intel /bin/false<br />
<br />
If both devices use the same module, it might be possible to disable one of them in the BIOS.<br />
<br />
===Poor sound quality or clipping===<br />
If you experience poor sound quality, try setting the PCM volume (in alsamixer) to a level such that gain is 0.<br />
<br />
If snd-usb-audio driver has been loaded, you could try to enable {{ic|softvol}} in '''/etc/asound.conf''' file. Example configuration for the first audio device:<br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.dsnooper {<br />
type dsnoop<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
channels 2<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.softvol {<br />
type softvol<br />
slave { pcm "dmixer" }<br />
control {<br />
name "Master"<br />
card 0<br />
}<br />
}<br />
ctl.!default {<br />
type hw<br />
card 0<br />
}<br />
ctl.softvol {<br />
type hw<br />
card 0<br />
}<br />
ctl.dmixer {<br />
type hw<br />
card 0<br />
}<br />
<br />
===Pops When Starting and Stopping Playback===<br />
Some modules (e.g. snd_ac97_codec and snd_hda_intel) can power off your sound card when not in use. This can make an audible noise (like a crack/pop/scratch) when turning on/off your sound card. Sometimes even when move the slider volume, or open and close windows (KDE4). If you find this annoying try {{ic|modinfo snd_MY_MODULE}}, and look for a module option that adjusts or disables this feature.<br />
<br />
Example: to disable the power saving mode and solve cracking sound trough speakers issue, using snd_hda_intel add in {{ic|/etc/modprobe.d/modprobe.conf}}<br />
options snd_hda_intel power_save=0<br />
or<br />
options snd_hda_intel power_save=0 power_save_controller=N<br />
<br />
You can also try it with {{ic|1=modprobe snd_hda_intel power_save=0}} before.<br />
<br />
You may also have to unmute the 'Line' ALSA channel for this to work. Any value will do (other than '0' or something too high).<br />
<br />
''Example:'' on an onboard VIA VT1708S (using the snd_hda_intel module) these cracks occured even though 'power_save' was set to 0. Unmuting the 'Line' channel and setting a value of '1' solved the issue.<br />
<br />
Source: https://www.kernel.org/doc/Documentation/sound/alsa/powersave.txt<br />
<br />
If you use a laptop, pm-utils will change {{ic|power_save}} back to 1 when you go onto battery power even if you disable power saving in {{ic|/etc/modprobe.d}}. Disable this for pm-utils by disabling the script that makes the change (see [https://wiki.archlinux.org/index.php/Pm-utils#Disabling_a_hook Disabling a hook] for more information):<br />
# touch /etc/pm/power.d/intel-audio-powersave<br />
<br />
===S/PDIF Output Does Not Work===<br />
<br />
If the optical/coaxial digital output of your motherboard/sound card is not working or stopped working, and have already enabled and unmuted it in alsamixer, try running<br />
# iecset audio on<br />
<br />
as root.<br />
<br />
You can also put this command in {{Ic|rc.local}} as it sometimes it may stop working after a reboot.<br />
<br />
===HDMI Output Does Not Work===<br />
If the HDMI output of your motherboard/sound card is not working or stopped working, and have already enabled and unmuted it in alsamixer, try the following.<br />
<br />
Query for Playback Devices:<br />
<br />
$ aplay -l<br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: NVidia [HDA NVidia], device 0: ALC1200 Analog [ALC1200 Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 1: ALC1200 Digital [ALC1200 Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 3: NVIDIA HDMI [NVIDIA HDMI]<br />
Subdevices: 0/1<br />
Subdevice #0: subdevice #0<br />
<br />
Now that we have the info for the HDMI device, try a test, In the example below, 0 is the card number and 3 is the device number.<br />
<br />
$ aplay -D plughw:0,3 /usr/share/sounds/alsa/Front_Center.wav<br />
<br />
If aplay does not output any errors, but still no sound is heared, "reboot" the receiver, monitor or tv set. Since the HDMI interface executes a handshake on connection, it might have noticed before that there was no audio stream embedded, and disabled audio decoding. In particular, if you are using a standalone window manager (don’t know about Gnome or KDE), you may need to have some sound playing ''while'' plugging in the HDMI cable.<br />
<br />
{{Note|If you are using an ATI card and linux 3.0, check your kernel/grub settings, you most likely need to use the ''''radeon.audio'''<nowiki>=</nowiki>'''1'''' boot parameter, or alternatively set up the option in your modprobe configuration files. It's important to note that before trying, is to check the [http://www.x.org/wiki/RadeonFeature#fndef-72849d6d6eb3927d486f50ece74a739042965a6c-25 Radeon Feature Matrix] to see if it's actually supported. At the moment this is written (march 29th, 2013), some chipsets of the [http://www.x.org/wiki/RadeonFeature#Decoder_ring_for_engineering_vs_marketing_names S. Islands] (namely HD7750 through HD7970) doesn't (still) support HDMI Audio.}}<br />
<br />
If the test is successful, create or edit your ~/.asoundrc file to set HDMI as the default audio device.<br />
<br />
{{hc|~/.asoundrc|<br />
pcm.!default {<br />
type hw<br />
card 0<br />
device 3<br />
}<br />
}}<br />
<br />
Or you above config does not work try:<br />
{{hc|~/.asoundrc|<br />
defaults.pcm.card 0<br />
defaults.pcm.device 3<br />
defaults.ctl.card 0<br />
}}<br />
<br />
===HDMI Multi-channel PCM output does not work (Intel) ===<br />
As of Linux 3.1 multi-channel PCM output through HDMI with a Intel card (Intel Eaglelake, IbexPeak/Ironlake,SandyBridge/CougarPoint and IvyBridge/PantherPoint) is not yet supported. Support for it has been recently added and expected to be available in Linux 3.2. To make it work in Linux 3.1 you need to apply the following patches:<br />
<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=76adaa34db407f174dd06370cb60f6029c33b465 drm: support routines for HDMI/DP ELD]<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=e0dac65ed45e72fe34cc7ccc76de0ba220bd38bb drm/i915: pass ELD to HDMI/DP audio driver]<br />
<br />
===HP TX2500===<br />
Add these 2 lines into {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-cmipci mpu_port=0x330 fm_port=0x388<br />
options snd-hda-intel index=0 model=toshiba position_fix=1<br />
<br />
options snd-hda-intel model=hp (works for tx2000cto)<br />
<br />
===Skipping Sound When Playing MP3===<br />
If you have sound skipping when playing MP3 files and you have more then 2 speakers attached to your computer (i.e. > 2 speaker system), run alsamixer and disable the channels for the speakers that you '''DO NOT''' have (i.e. do not enable the sound for the center speaker if you do not have a center speaker.<br />
<br />
===Using a USB Headset and External USB Sound Cards===<br />
If you are using a USB headset with ALSA you can try using {{AUR|asoundconf}} (currently only available from the [[AUR]]) to set the headset as the primary sound output. Before running make sure you have usb audio module enabled ({{ic|modprobe snd-usb-audio}}).<br />
<br />
# asoundconf is-active<br />
# asoundconf list<br />
# asoundconf set-default-card <chosen soundcard><br />
<br />
==== Crackling sound with USB devices ====<br />
If you experience crackling sound on USB devices, you can try tuning the snd-usb-audio for minimal latency.<br />
<br />
Add this to your {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
options snd-usb-audio nrpacks=1<br />
<br />
source: http://alsa.opensrc.org/Usb-audio#Tuning_USB_devices_for_minimal_latencies<br />
<br />
==== Hot-plugging a USB Sound Card ====<br />
In order to automatically make a USB Sound Card the primary output device, when the card is plugged in, you can use the following udev rules (e.g. add the following two lines to {{ic|/etc/udev/rules.d/00-local.rules}} and reboot).<br />
<br />
{{bc|1=KERNEL=="pcmC[D0-9cp]*", ACTION=="add", PROGRAM="/bin/sh -c 'K=%k; K=$${K#pcmC}; K=$${K%%D*}; echo defaults.ctl.card $$K > /etc/asound.conf; echo defaults.pcm.card $$K >>/etc/asound.conf'"<br />
KERNEL=="pcmC[D0-9cp]*", ACTION=="remove", PROGRAM="/bin/sh -c 'echo defaults.ctl.card 0 > /etc/asound.conf; echo defaults.pcm.card 0 >>/etc/asound.conf'"}}<br />
<br />
===Error 'Unknown hardware' Appears After a Kernel Update===<br />
The following messages may be displayed during the start-up ALSA after the kernel update:<br />
Unknown hardware "foo" "bar" ...<br />
Hardware is initialized using a guess method<br />
/usr/sbin/alsactl: set_control:nnnn:failed to obtain info for control #mm (No such file or directory)<br />
<br />
or<br />
Found hardware: "HDA-Intel" "VIA VT1705" "HDA:11064397,18490397,00100000" "0x1849" "0x0397"<br />
Hardware is initialized using a generic method<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #1 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #2 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #25 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #26 (No such file or directory)<br />
<br />
Simply store ALSA mixer settings again (as root):<br />
# alsactl -f /var/lib/alsa/asound.state store<br />
<br />
It may be necessary configure ALSA again with alsamixer<br />
<br />
===HDA Analyzer===<br />
If the mappings to your audio pins(plugs) do not correspond but ALSA works fine, you could try HDA Analyzer -- a pyGTK2 GUI for HD-audio control can be found [http://www.alsa-project.org/main/index.php/HDA_Analyzer at the ALSA wiki].<br />
Try tweaking the Widget Control section of the PIN nodes, to make microphones IN and headphone jacks OUT. Referring to the Config Defaults heading is a good idea.<br />
<br />
NOTE: the script is done by such way that it is incompatible with python3 (which is now shipped with ArchLinux) but tries to use it.<br />
The workaround is: open "run.py", find all occurences of "python" (2 occurences - one on the first line, and the second on the last line) and replace them all by "python2".<br />
<br />
NOTE2: the script requires root acces, but running it via su/sudo is bogus. Run it via {{ic|kdesu}} or {{ic|gksu}}.<br />
<br />
===ALSA with SDL===<br />
If you get no sound via SDL and ALSA cannot be chosen from the application. Try setting the environmental variable SDL_AUDIODRIVER to alsa.<br />
# export SDL_AUDIODRIVER=alsa<br />
<br />
===Low Sound Workaround===<br />
<br />
If you are facing low sound even after maxing out your speakers/headphones, you can give the softvol plugin a try. Add the following to {{ic|/etc/asound.conf}}.<br />
{{bc|<nowiki><br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
<br />
pcm.softvol {<br />
type softvol<br />
slave {<br />
pcm "dmix"<br />
}<br />
control {<br />
name "Pre-Amp"<br />
card 0<br />
}<br />
min_dB -5.0<br />
max_dB 20.0<br />
resolution 6<br />
}<br />
</nowiki>}}<br />
{{note| You will probably have to restart the computer, as restarting the alsa daemon did not load the new configuration for me. Also, if the configuration does not work even after restarting, try changing {{ic|plug}} with {{ic|hw}} in the above configuration.}}<br />
<br />
After the changes are loaded successfully, you will see a {{ic|Pre-Amp}} section in alsamixer. You can adjust the levels there.<br />
{{note|Setting a high value for {{ic|Pre-Amp}} can cause sound distortion, so adjust it according to the level that suits you.}}<br />
<br />
===Popping sound after resuming from suspension===<br />
You might hear a popping sound after resuming the computer from suspension. This can be fixed by editing {{ic|/etc/pm/sleep.d/90alsa}} and removing the line that says {{ic|aplay -d 1 /dev/zero}}<br />
<br />
==设置文件范例==<br />
参见:[[Advanced Linux Sound Architecture/Example Configurations]]。<br />
<br />
==相关阅读==<br />
* [http://www.mjmwired.net/kernel/Documentation/sound/alsa/ALSA-Configuration.txt ALSA 模块高级配置]<br />
* [http://alsa.opensrc.org/Main_Page 非官方的 ALSA Wiki]<br />
* [https://bbs.archlinux.org/viewtopic.php?id=36815 如何从 svn 获取驱动代码并编译]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Advanced_Linux_Sound_Architecture_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=252080
Advanced Linux Sound Architecture (简体中文)
2013-03-27T12:30:03Z
<p>Cuihao: /* 疑难解答 */</p>
<hr />
<div>[[Category:简体中文]]<br />
[[Category:Sound (简体中文)]]<br />
[[Category:Audio/Video (简体中文)]]<br />
[[cs:Advanced Linux Sound Architecture]]<br />
[[de:Alsa]]<br />
[[es:Advanced Linux Sound Architecture]]<br />
[[fa:ALSA]]<br />
[[fr:Alsa]]<br />
[[he:Advanced Linux Sound Architecture]]<br />
[[it:Advanced Linux Sound Architecture]]<br />
[[ja:Advanced Linux Sound Architecture]]<br />
[[nl:Advanced Linux Sound Architecture]]<br />
[[pt:Advanced Linux Sound Architecture]]<br />
[[ru:Advanced Linux Sound Architecture]]<br />
[[sk:Advanced Linux Sound Architecture]]<br />
[[th:Advanced Linux Sound Architecture]]<br />
[[tr:Gelişmiş_ses_mimarisi_(ALSA)]]<br />
[[en:Advanced Linux Sound Architecture]]<br />
<br />
{{Article summary start}}<br />
{{Article summary text|ALSA 的使用、配置及疑难解答。}}<br />
{{Article summary heading|相关页面}}<br />
{{Article summary wiki|Sound (简体中文)}}<br />
{{Article summary wiki|Disable PC Speaker Beep (简体中文)}}<br />
{{Article summary end}}<br />
<br />
{{TranslationStatus (简体中文)|Systemd|2013-03-14|250172}}<br />
<br />
[[Wikipedia:zh:ALSA|高级 Linux 声音体系]](Advanced Linux Sound Architecture,'''ALSA''')是Linux中提供声音设备驱动的内核组件,用来代替原来的开放声音系统(Open Sound System,OSSv3)。除了声音设备驱动,'''ALSA'''还包含一个用户空间的函数库,以方便开发者通过高级API使用驱动功能,而不必直接与内核驱动交互。<br />
<br />
{{注意|关于另一种声音体系,请阅读[[Open Sound System (简体中文)|开放声音系统(OSS)]]的页面。}}<br />
<br />
==安装==<br />
Arch 默认的内核已经通过一套模块提供了 ALSA,不必特别安装。<br />
<br />
[[udev (简体中文)|udev]]会在系统启动时自动检测硬件,并加载相应的声音设备驱动模块。这时,你的声卡已经可以工作了,只是所有声道默认都被设置成静音了。<br />
<br />
在本地登录(通过虚拟终端或登录管理器)的用户,都有权限播放音频并调整音量。要让远程登录的用户拥有这些权限,必须把用户[[Users and Groups (简体中文)#用户组管理|加入]] {{ic|audio}} 用户组。该组的成员可以直接访问声音设备,会导致某些程序独占音频输出(破坏软件混音),还可能影响用户快速切换和拖机(multiseat)。因此,除非真的有某些[https://wiki.ubuntu.com/Audio/TheAudioGroup 特殊需求],'''不建议'''把用户加入 {{ic|audio}} 用户组。<br />
<br />
===用户空间工具===<br />
从 [[Official Repositories (简体中文)|官方仓库]]:<br />
* [[pacman (简体中文)|安装]] {{Pkg|alsa-utils}} 软件包,其中的 {{ic|alsamixer}} 工具允许用户在控制台或终端中配置声音设备。<br />
* 安装 {{Pkg|alsa-oss}} 软件包,如果想让 OSS 应用程序与dmix(软件混音)协同工作。 <br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。}}<br />
*安装 {{Pkg|alsa-plugins}} 软件包,如果需要 [[#高质量重采样|高质量重采样]]、[[#Upmixing/Downmixing|软件模拟环绕立体声]]和其他高级特性。<br />
<br />
==解除各声道的静音==<br />
目前版本的 ALSA 安装后,所有声道'''默认是静音的''',必须手动解除。<br />
<br />
使用 {{ic|alsamixer}} 的 ncurses 界面,配置十分简单:<br />
<br />
$ alsamixer<br />
<br />
此外,还可以在命令行下使用 {{ic|amixer}}:<br />
<br />
$ amixer sset Master unmute<br />
<br />
下方标有 {{ic|MM}} 的声道是静音的,而标有 {{ic|00}} 的通道已经启用。<br />
<br />
使用左右方向键,选中 {{ic|Master}} 和 {{ic|PCM}} 声道。按下 {{keypress|m}} 键解除静音。使用上方向键增加音量,直到增益值为{{ic|0}}。该值显示在左上方 {{ic|Item:}} 字段后。过高的增益值会导致声音失真。<br />
<br />
要想得到完整的 5.1 或 7.1 环绕立体声,还得解除 Front、Surround、Center、LFE (subwoofer) 和 Side 这些声道的静音(上述名称是 Intel HD Audio 声卡使用的声道名,可能因设备不同而有所差异)。注意,仅有这些设置,系统不会自动将立体声源(多数音乐)提升(upmix)成环绕立体声。如果需要这些功能,请阅读[[#Upmixing/Downmixing]]。<br />
<br />
按下 {{Keypress|Esc}} 键退出 alsamixer。<br />
<br />
{{注意|<br />
* 有一些声卡,需要关闭数字输出或将其调成静音,才能输出模拟音频信号。对于 Soundblaster Audigy LS 声卡,需要把 IEC958 通道设成静音。 <br />
* 有些机器(如 Thinkpad T61),有一个 Speaker 通道,必须按照上述方法打开调整。<br />
* 有些机器(如 Dell E6400),可能还需要把 {{ic|Front}} 和 {{ic|Headphone}} 声道打开调整。}}<br />
<br />
接下来,测试声卡是否工作:<br />
<br />
$ speaker-test -c 2<br />
<br />
根据扬声器的实际情况,调整 -c 后的数值。对于 7.1 声道,这个数字是 8:<br />
<br />
$ speaker-test -c 8<br />
<br />
如果没有正常工作,请继续阅读 [[#配置]] 以及 [[#疑难解答]] 部分。<br />
<br />
{{pkg|alsa-utils}} 软件包提供了两个配置 alsa 的服务:{{ic|alsa-restore.service}} 和 {{ic|alsa-store.service}}。它们分别在开机和关机时自动运行。<br />
<br />
==配置==<br />
=== VirtualBox 虚拟机无声音 ===<br />
<br />
如果使用 VirtualBox 时碰上了麻烦,请尝试以下命令:<br />
<br />
{{hc|$ alsactl init|2=<nowiki><br />
<br />
Found hardware: "ICH" "SigmaTel STAC9700,83,84" "AC97a:83847600" "0x8086" "0x0000"<br />
Hardware is initialized using a generic method<br />
</nowiki><br />
}}<br />
<br />
如果还是不行,请检查所使用的音频软件是否激活了 ALSA 输出。<br />
<br />
===设置默认声卡===<br />
<br />
如果发现开机时声卡次序会发生变化,可以在通过 {{ic|/etc/modprobe.d}} 中的 {{ic|.conf}} 文件(比如 {{ic|/etc/modprobe.d/alsa-base.conf}})手动设置次序。<br />
比如,要让 mia 声卡成为 #0、Intel HDA 声卡成为 #1:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<nowiki><br />
options snd slots=snd_mia,snd_hda_intel<br />
options snd_mia index=0<br />
options snd_hda_intel index=1<br />
</nowiki>}}<br />
<br />
snd_mia 和 snd_hda_intel 分别是两个声卡的内核驱动模块。该配置仅适用于同时装有 mia 声卡和使用 snd_hda_intel 驱动的声卡(通常是板载声卡)的情况。<br />
<br />
如果使用 -2 的 index 值,ALSA 就不会将对应的设备作为主声卡使用。Linux Mint 和 Ubuntu 等发行版使用了以下配置,避免 USB 声卡和其他“非主流”设备变成 index 为 0 的主声卡:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<nowiki><br />
options bt87x index=-2<br />
options cx88_alsa index=-2<br />
options saa7134-alsa index=-2<br />
options snd-atiixp-modem index=-2<br />
options snd-intel8x0m index=-2<br />
options snd-via82xx-modem index=-2<br />
options snd-usb-audio index=-2<br />
options snd-usb-caiaq index=-2<br />
options snd-usb-ua101 index=-2<br />
options snd-usb-us122l index=-2<br />
options snd-usb-usx2y index=-2<br />
# Keep snd-pcsp from being loaded as first soundcard<br />
options snd-pcsp index=-2<br />
# Keep snd-usb-audio from beeing loaded as first soundcard<br />
options snd-usb-audio index=-2<br />
</nowiki>}}<br />
<br />
以上配置会在系统重启后生效。<br />
<br />
{{Out of date|据英文 Wiki 用户反应,{{ic|/etc/asound.conf}} 这个配置文件默认是不存在的,下列操作可能有误。}}<br />
<br />
首先,运行 {{ic|aplay -l}},获取声卡的声卡ID和设备ID:<br />
<br />
{{hc|$ aplay -l|2=<nowiki><br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: Intel [HDA Intel], device 0: CONEXANT Analog [CONEXANT Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: Intel [HDA Intel], device 1: Conexant Digital [Conexant Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 1: JamLab [JamLab], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 2: Audio [Altec Lansing XT1 - USB Audio], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
</nowiki>}}<br />
<br />
拿输出中的最后一项来说,这个声卡的声卡ID为2,设备ID为0。若要把这块声卡作为默认声卡,可以把下列配置添加到系统级别的 {{ic|/etc/asound.conf}} 或用户级别的 {{ic|~/.asoundrc}} 文件。如果文件不存在,可以手动创建。其中的各个ID,请根据实际情况调整:<br />
{{hc|~/.asoundrc|2=<nowiki><br />
defaults.pcm.card 2<br />
defaults.pcm.device 0<br />
defaults.ctl.card 2<br />
</nowiki>}}<br />
“pcm”选项决定用来播放音频的设备,而“ctl”选项决定那个声卡能够由控制工具(如 alsamixer)使用。<br />
<br />
上述配置在重启音频程序(如 mplayer)后立即生效。<br />
<br />
===确认所有声音模块都已经加载===<br />
<br />
一般 udev 都会自动识别出声卡。使用以下命令确认:<br />
<br />
{{hc|<nowiki>$ lsmod | grep '^snd' | column -t</nowiki>|<nowiki><br />
snd_hda_codec_hdmi 22378 4<br />
snd_hda_codec_realtek 294191 1<br />
snd_hda_intel 21738 1<br />
snd_hda_codec 73739 3 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel<br />
snd_hwdep 6134 1 snd_hda_codec<br />
snd_pcm 71032 3 snd_hda_codec_hdmi,snd_hda_intel,snd_hda_codec<br />
snd_timer 18992 1 snd_pcm<br />
snd 55132 9 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel,snd_hda_codec,snd_hwdep,snd_pcm,snd_timer<br />
snd_page_alloc 7017 2 snd_hda_intel,snd_pcm<br />
</nowiki>}}<br />
<br />
如果你的输出和上面类似,那就说明声卡已经被正确识别。<br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。如有需要,请[[Kernel_modules_(简体中文)#装入|手动加载]]。}}<br />
<br />
还可以检查一下 {{ic|/dev/snd/}} 目录,看看是否有这些设备文件:<br />
<br />
{{hc|$ ls -l /dev/snd|2=<nowiki><br />
total 0<br />
crw-rw---- 1 root audio 116, 0 Apr 8 14:17 controlC0<br />
crw-rw---- 1 root audio 116, 32 Apr 8 14:17 controlC1<br />
crw-rw---- 1 root audio 116, 24 Apr 8 14:17 pcmC0D0c<br />
crw-rw---- 1 root audio 116, 16 Apr 8 14:17 pcmC0D0p<br />
crw-rw---- 1 root audio 116, 25 Apr 8 14:17 pcmC0D1c<br />
crw-rw---- 1 root audio 116, 56 Apr 8 14:17 pcmC1D0c<br />
crw-rw---- 1 root audio 116, 48 Apr 8 14:17 pcmC1D0p<br />
crw-rw---- 1 root audio 116, 1 Apr 8 14:17 seq<br />
crw-rw---- 1 root audio 116, 33 Apr 8 14:17 timer<br />
</nowiki>}}<br />
<br />
{{注意|在 IRC 或论坛寻求这方面帮助时,别忘了贴出上面几个命令的输出。}}<br />
<br />
如果你的输出跟上面类似,或至少有 '''controlC0''' 和 '''pcmC0D0p''',那么声卡就已经正常加载了。<br />
<br />
如果出现问题,声卡模块没有正确加载,那么请尝试手动加载模块:<br />
<br />
* 确定声卡对应的驱动模块:[http://www.alsa-project.org/main/index.php/Matrix:Main ALSA Soundcard Matrix]。这些模块都会有一个”snd-“前缀(例如:{{ic|snd-via82xx}})。<br />
* [[Kernel_modules_(简体中文)#装入|加载模块]]。<br />
* 检查 {{ic|/dev/snd}} 目录中的设备文件(参见上文);或者,检查 {{ic|alsamixer}} 或 {{ic|amixer}} 的输出是否正确。<br />
* 修改配置,使 {{ic|snd-<模块名>}} 和 {{ic|snd-pcm-oss}} 模块[[Kernel_modules_(简体中文)#装入|开机自动加载]]。<br />
<br />
===启用 SPDIF 输出===<br />
<br />
(由 Gentoo 论坛的 gralves 提供)<br />
* 在 GNOME 音量控制的选项(Options)标签中,把 IEC958 改成 PCM。该选项可以在首选项中启用。<br />
* 不使用 GNOME 音量控制工具的话:<br />
** 编辑 {{ic|/var/lib/alsa/asound.state}}。该文件是 alsasound 保存混音器设置的地方。<br />
** 找到这一行:“IEC958 Playback Switch”。该行上下应该有一行内容为 {{ic|value:false}},将其改为 {{ic|value:true}}。<br />
** 再找到这一行:”IEC958 Playback AC97-SPSA“。把对应的值改为 0。<br />
** 重启 ALSA。<br />
<br />
{{Out of date|rc.local 在 systemd 中已经弃用,以下方法可能不再适用。}}<br />
<br />
还有另外一个开机自动启用 SPDIF 输出的方法(在 SoundBlaster Audigy 声卡上测试过):<br />
<br />
* 在 {{ic|/etc/rc.local}} 中添加如下内容:<br />
# Use COAX-digital output<br />
amixer set 'IEC958 Optical' 100 unmute<br />
amixer set 'Audigy Analog/Digital Output Jack' on<br />
<br />
使用以下命令,获取你的声卡的数字输出的名称:<br />
$ amixer scontrols<br />
<br />
===系统级均衡器===<br />
====使用 AlsaEqual(包含界面)====<br />
从 [[AUR (简体中文)|AUR]] 安装 {{AUR|alsaequal}}。<br />
<br />
{{注意|如果使用的是64位系统,而又安装了32位的 Flash 插件,这里的设置会导致 Flash 无声。如果发生了这种情况,请禁用 alsaequal 或编译32位版本的 alsaequal。}}<br />
<br />
安装后,把下列内容添加到 ALSA 配置文件({{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}}):<br />
<br />
ctl.equal {<br />
type equal;<br />
}<br />
<br />
pcm.plugequal {<br />
type equal;<br />
# Modify the line below if you do not<br />
#slave.pcm "plughw:0,0";<br />
#by default we want to play from more sources at time:<br />
slave.pcm "plug:dmix";<br />
}<br />
#pcm.equal {<br />
# If you do not want the equalizer to be your<br />
# default soundcard comment the following<br />
# line and uncomment the above line. (You can<br />
# choose it as the output device by addressing<br />
# it with specific apps,eg mpg123 -a equal 06.Back_In_Black.mp3)<br />
pcm.!default {<br />
type plug;<br />
slave.pcm plugequal;<br />
}<br />
<br />
设置好后,切换均衡器:<br />
$ alsamixer -D equal<br />
<br />
需要注意,每个用户的 alsaequal 配置文件都是独立的,位于 {{ic|~/.alsaequal.bin}}。因此,如果想在 [[mpd]] 之类的以独立用户身份运行的程序中使用 AlsaEqual,就得像下面这样:<br />
# su mpd -c 'alsamixer -D equal'<br />
或者,在其他用户的主目录下创建符号链接,指向你的 {{ic|.alsaequal.bin}}……<br />
<br />
=====管理 AlsaEqual 配置=====<br />
从 [http://xyne.archlinux.ca/repos/ Xyne 的软件仓库] 或 [https://aur.archlinux.org/packages.php?ID=62420 AUR] 安装 [http://xyne.archlinux.ca/projects/alsaequal-mgr/ alsaequal-mgr]。<br />
<br />
同上,配置均衡器: <br />
$alsamixer -D equal<br />
<br />
如果对效果感到满意,将其命名(如“foo”)并保存该配置:<br />
$alsaequal-mgr save foo<br />
<br />
重新加载“foo”中的配置:<br />
$alsaequal-mgr load foo<br />
<br />
这样,就可以为游戏、电影、不同音乐流派、网络电话等等创建不同的均衡器配置,以便随时切换。<br />
<br />
参见[http://xyne.archlinux.ca/projects/alsaequal-mgr/ 项目主页]和程序的帮助信息,了解更多设置。<br />
<br />
====使用 mbeq====<br />
{{注意|该软件用到了一个 ladspa 插件,可能导致播放音频时 CPU 占用率偏高。In addition, this was made with stereophonic sound (e.g. headphones) in mind.}}<br />
<br />
安装 {{Pkg|alsa-plugins}}、{{Pkg|ladspa}} 和 {{Pkg|swh-plugins}} 软件包。<br />
<br />
* 在 {{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}} 中添加如下内容:<br />
<br />
{{hc|/etc/asound.conf|2=<nowiki>pcm.eq {<br />
type ladspa<br />
<br />
# The output from the EQ can either go direct to a hardware device<br />
# (if you have a hardware mixer, e.g. SBLive/Audigy) or it can go<br />
# to the software mixer shown here.<br />
#slave.pcm "plughw:0,0"<br />
slave.pcm "plug:dmix"<br />
<br />
# Sometimes you may need to specify the path to the plugins,<br />
# especially if you have just installed them. Once you have logged<br />
# out/restarted this should not be necessary, but if you get errors<br />
# about being unable to find plugins, try uncommenting this.<br />
#path "/usr/lib/ladspa"<br />
<br />
plugins [<br />
{<br />
label mbeq<br />
id 1197<br />
input {<br />
#this setting is here by example, edit to your own taste<br />
#bands: 50hz, 100hz, 156hz, 220hz, 311hz, 440hz, 622hz, 880hz, 1250hz, 1750hz, 25000hz,<br />
#50000hz, 10000hz, 20000hz<br />
controls [ -5 -5 -5 -5 -5 -10 -20 -15 -10 -10 -10 -10 -10 -3 -2 ]<br />
}<br />
}<br />
]<br />
}<br />
<br />
# Redirect the default device to go via the EQ - you may want to do<br />
# this last, once you are sure everything is working. Otherwise all<br />
# your audio programs will break/crash if something has gone wrong.<br />
<br />
pcm.!default {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
<br />
# Redirect the OSS emulation through the EQ too (when programs are running through "aoss")<br />
<br />
pcm.dsp0 {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
</nowiki>}}<br />
<br />
* 完成。若有问题,欢迎前往论坛咨询。<br />
<br />
==高质量重采样==<br />
启用软件混音时,ALSA 会强制把所有音频重采样到相同的频率(如果系统支持,默认是 48000)。dmix 的重采样算法很糟糕,会导致显著的质量损失。<br />
<br />
安装 {{pkg|alsa-plugins}} 和 {{pkg|libsamplerate}}。<br />
<br />
将默认的采样率转换器更改为 libsamplerate:<br />
<br />
{{hc|/etc/asound.conf|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
或:<br />
<br />
{{hc|~/.asoundrc|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
'''samplerate_best''' 输出的声音质量最佳,但需要耗费大量CPU资源进行实时重采样。还有其他的算法可供选择(如 '''samplerate''' 等),但比起默认的算法,这些算法的改进并不明显。<br />
<br />
{{警告|某些系统中,启用 samplerate_best 可能导致 Flash 播放器无声音。}}<br />
<br />
==Upmixing/Downmixing==<br />
===Upmixing===<br />
In order for stereo sources like music to be able to saturate a 5.1 or 7.1 sound system, you need to use upmixing. In darker days this used to be tricky and error prone but nowadays plugins exist to easily take care of this task. Thus, install {{Pkg|alsa-plugins}}.<br />
<br />
Then add the following to your ALSA configuration file of choice (either {{ic|/etc/asound.conf}} or {{ic|~/.asoundrc}}):<br />
pcm.upmix71 {<br />
type upmix<br />
slave.pcm "surround71"<br />
delay 15<br />
channels 8<br />
}<br />
<br />
You can easily change this example for 7.1 upmixing to 5.1 or 4.0.<br />
<br />
This adds a new pcm that you can use for upmixing. If you want all sound sources to go through this pcm, add it as a default below the previous definition like so:<br />
<br />
pcm.!default "plug:upmix71"<br />
<br />
The plugin automatically allows multiple sources to play through it without problems so setting is as a default is actually a safe choice. <br />
If this is not working, you have to setup your own dmixer for the upmixing PCM like this: <br />
pcm.dmix6 {<br />
type asym<br />
playback.pcm {<br />
type dmix<br />
ipc_key 567829<br />
slave {<br />
pcm "hw:0,0"<br />
channels 6<br />
}<br />
}<br />
}<br />
<br />
and use "dmix6" instead of "surround71". <br />
If you experience skipping or distorted sound, consider increasing the buffer_size (to 32768, for example) or use a [[#高质量重采样|high quality resampler]].<br />
<br />
===Downmixing===<br />
If you want to downmix sources to stereo because you, for instance, want to watch a movie with 5.1 sound on a stereo system, you need to use the vdownmix plugin that alsa provides in the same package.<br />
<br />
Again, in your configuration file, add this:<br />
<br />
pcm.!surround51 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
pcm.!surround40 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
<br />
==混音==<br />
===软件混音(dmix)===<br />
<br />
{{注意|在 ALSA 1.0.9rc2 及以上版本中,如果声卡不支持硬件混音,模拟输出将默认使用 dmix 混音,无需额外设置。}}<br />
<br />
如果软件混音未正常启用,那么可以手动创建 {{ic|~/.asoundrc}},加入以下内容:<br />
<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
<br />
这样就启用了软件混音,多个程序能够同时使用声卡。<br />
<br />
至于 S/PDIF 之类的数字音频输出,目前的 ALSA 还不会默认启用 dmix。因此,必须像上面这样手动启用。<br />
<br />
常见问题的解答,参见[[#疑难解答]]部分。<br />
<br />
===硬件混音===<br />
====支持====<br />
如果声卡支持,一般无需任何配置即可启用硬件混音。绝大多数板载声卡都不支持硬件混音,必须得使用软件混音(见上文)。很多独立声卡都支持硬件混音,Linux 下支持较好的有:<br />
<br />
* Creative SoundBlaster Live! (5.1 型号)<br />
* Creative SoundBlaster Audigy (部分型号)<br />
* Creative SoundBlaster Audidy 2 (ZS 系列型号)<br />
* Creative SoundBlaster Audigy 4 (Pro 系列型号)<br />
<br />
{{注意|上述声卡的低端版本(Audigy SE、Audigy 2 NX、SoundBlaster Live! 24bit 以及 SoundBlaster Live! 7.1)使用了不同的芯片,'''不支持'''硬件混音。}}<br />
<br />
{{注意|板载 VIA8237 芯片支持4路硬件混音,但在某些主板上只能使用3路(第4路无声),甚至根本无法使用。即使能正常工作,质量也比不上其他混音方法。}}<br />
<br />
====问题修复====<br />
如果你的声卡是 Intel Corporation 82801I (ICH9 Family) HD Audio Controller (rev 02),且使用了64位系统,需要执行以下命令才能在 Enemy Territory 中听到声音:<br />
echo "et.x86 0 0 direct" > /proc/asound/card0/pcm0p/oss<br />
echo "et.x86 0 0 disable" > /proc/asound/card0/pcm0c/oss<br />
<br />
==疑难解答==<br />
===使用CPU频率动态调整时,音频发生跳跃===<br />
使用 {{ic|ondemand}} 或 {{ic|conservative}} 频率策略、启用CPU频率动态调整时,某些声卡和某些 ALSA 驱动会出现声音跳跃的情况。目前,唯一的解决方案是使用固定的频率,如 {{ic|performance}} 性能策略。<br />
<br />
详情参见 [[CPU Frequency Scaling (简体中文)]]。<br />
<br />
===同时只能有一个用户播放音频===<br />
有时会出现一个用户独占软件混音器的情况。多数情况下这没什么影响,但在使用 [[mpd]] 这样以独立用户身份运行的程序时,就会出现问题。当 mpd 运行时,用户自己就无法播放其他音频了。一种解决方法是,以正在使用的用户身份运行 mpd。另一种方法是,在 ALSA 配置文件的 {{ic|pcm.dmixer}} 部分添加 {{ic|ipc_key_add_uid 0}} 禁止独占。以下是 {{ic|asound.conf}} 示例的片段,省略的部分无需修改:<br />
<br />
...<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
slave {<br />
...<br />
<br />
===无法同时播放多个音频===<br />
如果安装了 [[PulseAudio (简体中文)|PulseAudio]](可能作为 [[GNOME (简体中文)|GNOME]] 的依赖被安装),可能出现无法同步播放的问题,因为 PulseAudio 的默认配置是“独占”声卡。ALSA 的用户可能对自己的配置已经很满意、不希望使用 [[PulseAudio (简体中文)|PulseAudio]]。解决方案是编辑 {{ic|/etc/asound.conf}} 并注释掉以下内容:<br />
# Use PulseAudio by default<br />
#pcm.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
# hint {<br />
# show on<br />
# description "Default ALSA Output (currently PulseAudio Sound Server)"<br />
# }<br />
#}<br />
<br />
可能还需要注释掉下面的内容:<br />
#ctl.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
#}<br />
<br />
比起完全卸载 [[PulseAudio (简体中文)|PulseAudio]],这大概是个更简单的方法。<br />
<br />
以下是 {{ic|/etc/asound.conf}} 配置文件的一个示例:<br />
{{bc|<nowiki>pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
}<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}</nowiki><br />
}}<br />
<br />
{{注意|本 {{ic|/etc/asound.conf}} 本来是配合 [[MPD]] 全局配置使用的。参见[[#同时只能有一个用户播放音频|该节]]关于多用户的内容。}}<br />
<br />
===有时候开机无声===<br />
如果会随机地出现开机无声的问题,原因可能是多声卡的顺序发生了变化。如果是这种情况,请参照[[#设置默认声卡]]进行设置。<br />
<br />
对于 mpd 用户,上述配置无效,请参考[http://mpd.wikia.com/wiki/Configuration#ALSA_MPD_software_volume_control 该文]设置。<br />
<br />
===特定程序的问题===<br />
对于不使用系统声音配置的程序,如 XMMS 和 Mplayer,必须特别设置以应用 ALSA 配置。<br />
<br />
对于 mplayer,将以下内容添加到 {{ic|~/.mplayer/config}} (或系统全局配置文件 {{ic|/etc/mplayer/mplayer.conf}}):<br />
ao=alsa<br />
<br />
对于 XMMS 和 Beep Media Player,打开选项窗口,要把声音驱动设置为 ALSA 而不是 OSS。<br />
<br />
XMMS 的设置方法:<br />
* 启动 XMMS<br />
** 选项 -> 首选项<br />
** 选择 Alsa 输出插件<br />
<br />
对于未提供 ALSA 输出的程序,可以使用 alsa-oss 软件包中的 aoss 工具启动。使用方法是:在要运行的程序的命令行前加上 {{ic|aoss}}。例如:<br />
aoss realplay<br />
<br />
pcm.!default{ ... } 貌似已经不管用了。但下面的设置有效:<br />
pcm.default pcm.dmixer<br />
<br />
===特定型号的配置===<br />
ALSA 会通过 BIOS 检测[http://www.kernel.org/doc/Documentation/sound/alsa/HD-Audio-Models.txt 声卡型号],少数情况下也可能无法正确识别。声卡芯片类型可以在 {{ic|alsamixer}} 界面上找到(如 ALC662),而型号则要设置在 {{ic|/etc/modprobe.d/modprobe.conf}} 或 {{ic|/etc/modprobe.d/sound.conf}} 文件中:<br />
<br />
options snd-hda-intel model=<型号><br />
<br />
具体设置因声卡型号而异。多数情况下不需要特地这样做。如果想知道具体声卡型号的设置,请查阅 [http://bugtrack.alsa-project.org/main/index.php/Matrix:Main Alsa Soundcard List],找到你的声卡型号,点击“Details”,然后查看“Setting up modprobe...”小节,将其中的内容插入 {{ic|/etc/modprobe.d/modprobe.conf}} 文件即可。例如,Intel AC97 的配置如下:<br />
<br />
{{bc|# ALSA portion<br />
alias char-major-116 snd<br />
alias snd-card-0 snd-intel8x0<br />
# module options should go here<br />
<br />
# OSS/Free portion<br />
alias char-major-14 soundcore<br />
alias sound-slot-0 snd-card-0<br />
<br />
# card #1<br />
alias sound-service-0-0 snd-mixer-oss<br />
alias sound-service-0-1 snd-seq-oss<br />
alias sound-service-0-3 snd-pcm-oss<br />
alias sound-service-0-8 snd-seq-oss<br />
alias sound-service-0-12 snd-pcm-oss}}<br />
<br />
===与蜂鸣器冲突===<br />
如果你确认所有声道都已解除静音,声卡驱动配置正确,且音量大小也合适,但还是听不到声音,那么请试试将下面的内容添加到 {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
(如果是 {{ic|via82xx}} 驱动)<br />
options snd-<模块名请自行补充> ac97_quirk=0<br />
<br />
(如果是 {{ic|snd_intel8x0}} 驱动)<br />
options snd-NAME-OF-MODULE ac97_quirk=1<br />
<br />
===麦克风输入无声音===<br />
In alsamixer, make sure that all the volume levels are up under recording, and that CAPTURE is toggled active on the microphone (e.g. Mic, Internal Mic) and/or on Capture (in alsamixer, select these items and press space). Try making positive Mic Boost and raising Capture and Digital levels higher; this make make static or distortion, but then you can adjust them back down once you are hearing ''something'' when you record<br />
<br />
As the pulseaudio wrapper is shown as "default" in alsamixer, you may have to press F6 to select your actual soundcard first. You may also need to enable and increase the volume of Line-in in the Playback section.<br />
<br />
To test the microphone, run these commands (see arecord's man page for further information):<br />
arecord -d 5 test-mic.wav<br />
aplay test-mic.wav<br />
<br />
If all fails, you may want to eliminate hardware failure by testing the microphone with a different device.<br />
<br />
For at least some computers, muting a microphone (MM) simply means its input does not go immediately to the speakers. It still receives input.<br />
<br />
Many Dell laptops need "-dmic" to be appended to the model name in {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-hda-intel model=dell-m6-dmic<br />
<br />
Some programs use try to use OSS as the main input software. Add the following lines to {{ic|/etc/modprobe.d/modprobe.conf}} to prevent OSS modules from being loaded:<br />
<br />
{{Note|The OSS modules are no longer autoloaded anyway.}}<br />
<br />
blacklist snd_pcm_oss<br />
blacklist snd_mixer_oss<br />
blacklist snd_seq_oss<br />
<br />
See also:<br />
* http://www.alsa-project.org/main/index.php/SoundcardTesting<br />
* http://alsa.opensrc.org/Record_from_mic<br />
<br />
===设置默认的麦克风/录音设备===<br />
某些程序(Pidgin、Adobe Flash)提供了设置录音设备的选项。如果录音设备不是内置的声卡,而是外接的(如USB摄像头、麦克风),就会有些问题。如果只想更改默认的录音设备、不更改回放设备,需要像下面这样设置 {{ic|~/.asoundrc}}:<br />
<br />
pcm.usb<br />
{<br />
type hw<br />
card U0x46d0x81d<br />
}<br />
<br />
pcm.!default<br />
{<br />
type asym<br />
playback.pcm<br />
{<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
capture.pcm<br />
{<br />
type plug<br />
slave.pcm "usb"<br />
}<br />
}<br />
<br />
把“U0x46d0x81d”替换为你的录音设备的名称。可以通过 {{ic|arecord -L}} 查看所有 ALSA 识别到的录音设备。<br />
<br />
===内置麦克风无效===<br />
First make sure all the volume levels are up under recording in alsamixer. In my case adding the following option to /etc/sound.conf and reloading the snd-* module produced a new volume setting called Capture which was capturing for the internal mic. For eg, for snd-hda-intel add<br />
<br />
options snd-hda-intel enable_msi=1<br />
<br />
Then reload the module (as below), up the recording volume of Capture and then test.<br />
<br />
{{bc|# rmmod snd-hda-intel && modprobe snd-hda-intel}}<br />
<br />
===Intel 板载声卡无声===<br />
可能由两个相互冲突的驱动模块引起,分别是 {{ic|snd_intel8x0}} 和 {{ic|snd_intel8x0m}}。如果是的话,屏蔽 snd_intel8x0m 即可:<br />
<br />
{{hc|/etc/modprobe.d/modprobe.conf|blacklist snd_intel8x0m}}<br />
<br />
以下方法可能也有效:在 {{ic|alsamixer}} 或 {{ic|amixer}} 把 “External Amplifier” 设置为静音。参见:[http://alsa.opensrc.org/Intel8x0#Dell_Inspiron_8600_.28and_probably_others.29 ALSA wiki 相关条目]。<br />
<br />
也可以试试解除“Mix”的静音。<br />
<br />
===No Headphone Sound with Onboard Intel Sound Card===<br />
With '''Intel Corporation 82801 I (ICH9 Family) HD Audio Controller''' on laptop, you may need to add this line to modprobe or sound.conf:<br />
<br />
options snd-hda-intel model=$model<br />
<br />
Where $model is any one of the following (in order of possibility to work, but not merit):<br />
<br />
* dell-vostro<br />
* olpc-xo-1_5<br />
* laptop<br />
* dell-m6<br />
* laptop-hpsense<br />
<br />
Note: It may be necessary to put this "options" line below (after) any "alias" lines about your card.<br />
<br />
You can see all the available models in the kernel documentation. For example [http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.35.y.git;a=blob;f=Documentation/sound/alsa/HD-Audio-Models.txt;h=dc25bb84b83b49665a7ed850e7bf5423d50cd3ba;hb=HEAD here], but check that it is the correct version of that document for your kernel version.<br />
<br />
A list of available models is also available [http://www.mjmwired.net/kernel/Documentation/sound/alsa/HD-Audio-Models.txt here]. To know your chip name type the following command (with * being corrected to match your files). Note that some chips could have been renamed and do not directly match the available ones in the file.<br />
<br />
cat /proc/asound/card*/codec* | grep Codec<br />
<br />
Note that there is a high chance none of the input devices (all internal and external mics) will work if you choose to do this, so it is either your headphones or your mic. Please report to ALSA if you are affected by this bug.<br />
<br />
And also, if you have problems getting beeps to work (pcspkr):<br />
<br />
options snd-hda-intel model=$model enable=1 index=0<br />
<br />
===No sound when S/PDIF video card is installed===<br />
Discover available modules and their order:<br />
$ cat /proc/asound/modules<br />
0 snd_hda_intel<br />
1 snd_ca0106<br />
<br />
Disable the undesired video card audio codec in {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
# /etc/modprobe.d/modprobe.conf<br />
#<br />
install snd_hda_intel /bin/false<br />
<br />
If both devices use the same module, it might be possible to disable one of them in the BIOS.<br />
<br />
===Poor sound quality or clipping===<br />
If you experience poor sound quality, try setting the PCM volume (in alsamixer) to a level such that gain is 0.<br />
<br />
If snd-usb-audio driver has been loaded, you could try to enable {{ic|softvol}} in '''/etc/asound.conf''' file. Example configuration for the first audio device:<br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.dsnooper {<br />
type dsnoop<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
channels 2<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.softvol {<br />
type softvol<br />
slave { pcm "dmixer" }<br />
control {<br />
name "Master"<br />
card 0<br />
}<br />
}<br />
ctl.!default {<br />
type hw<br />
card 0<br />
}<br />
ctl.softvol {<br />
type hw<br />
card 0<br />
}<br />
ctl.dmixer {<br />
type hw<br />
card 0<br />
}<br />
<br />
===Pops When Starting and Stopping Playback===<br />
Some modules (e.g. snd_ac97_codec and snd_hda_intel) can power off your sound card when not in use. This can make an audible noise (like a crack/pop/scratch) when turning on/off your sound card. Sometimes even when move the slider volume, or open and close windows (KDE4). If you find this annoying try {{ic|modinfo snd_MY_MODULE}}, and look for a module option that adjusts or disables this feature.<br />
<br />
Example: to disable the power saving mode and solve cracking sound trough speakers issue, using snd_hda_intel add in {{ic|/etc/modprobe.d/modprobe.conf}}<br />
options snd_hda_intel power_save=0<br />
or<br />
options snd_hda_intel power_save=0 power_save_controller=N<br />
<br />
You can also try it with {{ic|1=modprobe snd_hda_intel power_save=0}} before.<br />
<br />
You may also have to unmute the 'Line' ALSA channel for this to work. Any value will do (other than '0' or something too high).<br />
<br />
''Example:'' on an onboard VIA VT1708S (using the snd_hda_intel module) these cracks occured even though 'power_save' was set to 0. Unmuting the 'Line' channel and setting a value of '1' solved the issue.<br />
<br />
Source: https://www.kernel.org/doc/Documentation/sound/alsa/powersave.txt<br />
<br />
If you use a laptop, pm-utils will change {{ic|power_save}} back to 1 when you go onto battery power even if you disable power saving in {{ic|/etc/modprobe.d}}. Disable this for pm-utils by disabling the script that makes the change (see [https://wiki.archlinux.org/index.php/Pm-utils#Disabling_a_hook Disabling a hook] for more information):<br />
# touch /etc/pm/power.d/intel-audio-powersave<br />
<br />
===S/PDIF Output Does Not Work===<br />
<br />
If the optical/coaxial digital output of your motherboard/sound card is not working or stopped working, and have already enabled and unmuted it in alsamixer, try running<br />
# iecset audio on<br />
<br />
as root.<br />
<br />
You can also put this command in {{Ic|rc.local}} as it sometimes it may stop working after a reboot.<br />
<br />
===HDMI Output Does Not Work===<br />
If the HDMI output of your motherboard/sound card is not working or stopped working, and have already enabled and unmuted it in alsamixer, try the following.<br />
<br />
Query for Playback Devices:<br />
<br />
$ aplay -l<br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: NVidia [HDA NVidia], device 0: ALC1200 Analog [ALC1200 Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 1: ALC1200 Digital [ALC1200 Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 3: NVIDIA HDMI [NVIDIA HDMI]<br />
Subdevices: 0/1<br />
Subdevice #0: subdevice #0<br />
<br />
Now that we have the info for the HDMI device, try a test, In the example below, 0 is the card number and 3 is the device number.<br />
<br />
$ aplay -D plughw:0,3 /usr/share/sounds/alsa/Front_Center.wav<br />
<br />
If aplay does not output any errors, but still no sound is heared, "reboot" the receiver, monitor or tv set. Since the HDMI interface executes a handshake on connection, it might have noticed before that there was no audio stream embedded, and disabled audio decoding. In particular, if you are using a standalone window manager (don’t know about Gnome or KDE), you may need to have some sound playing ''while'' plugging in the HDMI cable.<br />
<br />
{{Note|If you are using an ATI card and linux 3.0, check your kernel/grub settings, you most likely need to use the ''''radeon.audio'''<nowiki>=</nowiki>'''1'''' boot parameter, or alternatively set up the option in your modprobe configuration files.}}<br />
<br />
If the test is successful, create or edit your ~/.asoundrc file to set HDMI as the default audio device.<br />
<br />
{{hc|~/.asoundrc|<br />
pcm.!default {<br />
type hw<br />
card 0<br />
device 3<br />
}<br />
}}<br />
<br />
Or you above config does not work try:<br />
{{hc|~/.asoundrc|<br />
defaults.pcm.card 0<br />
defaults.pcm.device 3<br />
defaults.ctl.card 0<br />
}}<br />
<br />
===HDMI Multi-channel PCM output does not work (Intel) ===<br />
As of Linux 3.1 multi-channel PCM output through HDMI with a Intel card (Intel Eaglelake, IbexPeak/Ironlake,SandyBridge/CougarPoint and IvyBridge/PantherPoint) is not yet supported. Support for it has been recently added and expected to be available in Linux 3.2. To make it work in Linux 3.1 you need to apply the following patches:<br />
<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=76adaa34db407f174dd06370cb60f6029c33b465 drm: support routines for HDMI/DP ELD]<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=e0dac65ed45e72fe34cc7ccc76de0ba220bd38bb drm/i915: pass ELD to HDMI/DP audio driver]<br />
<br />
===HP TX2500===<br />
Add these 2 lines into {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-cmipci mpu_port=0x330 fm_port=0x388<br />
options snd-hda-intel index=0 model=toshiba position_fix=1<br />
<br />
options snd-hda-intel model=hp (works for tx2000cto)<br />
<br />
===Skipping Sound When Playing MP3===<br />
If you have sound skipping when playing MP3 files and you have more then 2 speakers attached to your computer (i.e. > 2 speaker system), run alsamixer and disable the channels for the speakers that you '''DO NOT''' have (i.e. do not enable the sound for the center speaker if you do not have a center speaker.<br />
<br />
===Using a USB Headset and External USB Sound Cards===<br />
If you are using a USB headset with ALSA you can try using {{AUR|asoundconf}} (currently only available from the [[AUR]]) to set the headset as the primary sound output. Before running make sure you have usb audio module enabled ({{ic|modprobe snd-usb-audio}}).<br />
<br />
# asoundconf is-active<br />
# asoundconf list<br />
# asoundconf set-default-card <chosen soundcard><br />
<br />
==== Crackling sound with USB devices ====<br />
If you experience crackling sound on USB devices, you can try tuning the snd-usb-audio for minimal latency.<br />
<br />
Add this to your {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
options snd-usb-audio nrpacks=1<br />
<br />
source: http://alsa.opensrc.org/Usb-audio#Tuning_USB_devices_for_minimal_latencies<br />
<br />
==== Hot-plugging a USB Sound Card ====<br />
In order to automatically make a USB Sound Card the primary output device, when the card is plugged in, you can use the following udev rules (e.g. add the following two lines to {{ic|/etc/udev/rules.d/00-local.rules}} and reboot).<br />
<br />
{{bc|1=KERNEL=="pcmC[D0-9cp]*", ACTION=="add", PROGRAM="/bin/sh -c 'K=%k; K=$${K#pcmC}; K=$${K%%D*}; echo defaults.ctl.card $$K > /etc/asound.conf; echo defaults.pcm.card $$K >>/etc/asound.conf'"<br />
KERNEL=="pcmC[D0-9cp]*", ACTION=="remove", PROGRAM="/bin/sh -c 'echo defaults.ctl.card 0 > /etc/asound.conf; echo defaults.pcm.card 0 >>/etc/asound.conf'"}}<br />
<br />
===Error 'Unknown hardware' Appears After a Kernel Update===<br />
The following messages may be displayed during the start-up ALSA after the kernel update:<br />
Unknown hardware "foo" "bar" ...<br />
Hardware is initialized using a guess method<br />
/usr/sbin/alsactl: set_control:nnnn:failed to obtain info for control #mm (No such file or directory)<br />
<br />
or<br />
Found hardware: "HDA-Intel" "VIA VT1705" "HDA:11064397,18490397,00100000" "0x1849" "0x0397"<br />
Hardware is initialized using a generic method<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #1 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #2 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #25 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #26 (No such file or directory)<br />
<br />
Simply store ALSA mixer settings again (as root):<br />
# alsactl -f /var/lib/alsa/asound.state store<br />
<br />
It may be necessary configure ALSA again with alsamixer<br />
<br />
===HDA Analyzer===<br />
If the mappings to your audio pins(plugs) do not correspond but ALSA works fine, you could try HDA Analyzer -- a pyGTK2 GUI for HD-audio control can be found [http://www.alsa-project.org/main/index.php/HDA_Analyzer at the ALSA wiki].<br />
Try tweaking the Widget Control section of the PIN nodes, to make microphones IN and headphone jacks OUT. Referring to the Config Defaults heading is a good idea.<br />
<br />
NOTE: the script is done by such way that it is incompatible with python3 (which is now shipped with ArchLinux) but tries to use it.<br />
The workaround is: open "run.py", find all occurences of "python" (2 occurences - one on the first line, and the second on the last line) and replace them all by "python2".<br />
<br />
NOTE2: the script requires root acces, but running it via su/sudo is bogus. Run it via {{ic|kdesu}} or {{ic|gksu}}.<br />
<br />
===ALSA with SDL===<br />
If you get no sound via SDL and ALSA cannot be chosen from the application. Try setting the environmental variable SDL_AUDIODRIVER to alsa.<br />
# export SDL_AUDIODRIVER=alsa<br />
<br />
===Low Sound Workaround===<br />
<br />
If you are facing low sound even after maxing out your speakers/headphones, you can give the softvol plugin a try. Add the following to {{ic|/etc/asound.conf}}.<br />
{{bc|<nowiki><br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
<br />
pcm.softvol {<br />
type softvol<br />
slave {<br />
pcm "dmix"<br />
}<br />
control {<br />
name "Pre-Amp"<br />
card 0<br />
}<br />
min_dB -5.0<br />
max_dB 20.0<br />
resolution 6<br />
}<br />
</nowiki>}}<br />
{{note| You will probably have to restart the computer, as restarting the alsa daemon did not load the new configuration for me. Also, if the configuration does not work even after restarting, try changing {{ic|plug}} with {{ic|hw}} in the above configuration.}}<br />
<br />
After the changes are loaded successfully, you will see a {{ic|Pre-Amp}} section in alsamixer. You can adjust the levels there.<br />
{{note|Setting a high value for {{ic|Pre-Amp}} can cause sound distortion, so adjust it according to the level that suits you.}}<br />
<br />
===Popping sound after resuming from suspension===<br />
You might hear a popping sound after resuming the computer from suspension. This can be fixed by editing {{ic|/etc/pm/sleep.d/90alsa}} and removing the line that says {{ic|aplay -d 1 /dev/zero}}<br />
<br />
==设置文件范例==<br />
参见:[[Advanced Linux Sound Architecture/Example Configurations]]。<br />
<br />
==相关阅读==<br />
* [http://www.mjmwired.net/kernel/Documentation/sound/alsa/ALSA-Configuration.txt ALSA 模块高级配置]<br />
* [http://alsa.opensrc.org/Main_Page 非官方的 ALSA Wiki]<br />
* [https://bbs.archlinux.org/viewtopic.php?id=36815 如何从 svn 获取驱动代码并编译]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Advanced_Linux_Sound_Architecture_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=251739
Advanced Linux Sound Architecture (简体中文)
2013-03-24T01:01:46Z
<p>Cuihao: /* Model Settings */</p>
<hr />
<div>[[Category:简体中文]]<br />
[[Category:Sound (简体中文)]]<br />
[[Category:Audio/Video (简体中文)]]<br />
[[cs:Advanced Linux Sound Architecture]]<br />
[[de:Alsa]]<br />
[[es:Advanced Linux Sound Architecture]]<br />
[[fa:ALSA]]<br />
[[fr:Alsa]]<br />
[[he:Advanced Linux Sound Architecture]]<br />
[[it:Advanced Linux Sound Architecture]]<br />
[[ja:Advanced Linux Sound Architecture]]<br />
[[nl:Advanced Linux Sound Architecture]]<br />
[[pt:Advanced Linux Sound Architecture]]<br />
[[ru:Advanced Linux Sound Architecture]]<br />
[[sk:Advanced Linux Sound Architecture]]<br />
[[th:Advanced Linux Sound Architecture]]<br />
[[tr:Gelişmiş_ses_mimarisi_(ALSA)]]<br />
[[en:Advanced Linux Sound Architecture]]<br />
<br />
{{Article summary start}}<br />
{{Article summary text|ALSA 的使用、配置及疑难解答。}}<br />
{{Article summary heading|相关页面}}<br />
{{Article summary wiki|Sound (简体中文)}}<br />
{{Article summary wiki|Disable PC Speaker Beep (简体中文)}}<br />
{{Article summary end}}<br />
<br />
{{TranslationStatus (简体中文)|Systemd|2013-03-14|250172}}<br />
<br />
[[Wikipedia:zh:ALSA|高级 Linux 声音体系]](Advanced Linux Sound Architecture,'''ALSA''')是Linux中提供声音设备驱动的内核组件,用来代替原来的开放声音系统(Open Sound System,OSSv3)。除了声音设备驱动,'''ALSA'''还包含一个用户空间的函数库,以方便开发者通过高级API使用驱动功能,而不必直接与内核驱动交互。<br />
<br />
{{注意|关于另一种声音体系,请阅读[[Open Sound System (简体中文)|开放声音系统(OSS)]]的页面。}}<br />
<br />
==安装==<br />
Arch 默认的内核已经通过一套模块提供了 ALSA,不必特别安装。<br />
<br />
[[udev (简体中文)|udev]]会在系统启动时自动检测硬件,并加载相应的声音设备驱动模块。这时,你的声卡已经可以工作了,只是所有声道默认都被设置成静音了。<br />
<br />
在本地登录(通过虚拟终端或登录管理器)的用户,都有权限播放音频并调整音量。要让远程登录的用户拥有这些权限,必须把用户[[Users and Groups (简体中文)#用户组管理|加入]] {{ic|audio}} 用户组。该组的成员可以直接访问声音设备,会导致某些程序独占音频输出(破坏软件混音),还可能影响用户快速切换和拖机(multiseat)。因此,除非真的有某些[https://wiki.ubuntu.com/Audio/TheAudioGroup 特殊需求],'''不建议'''把用户加入 {{ic|audio}} 用户组。<br />
<br />
===用户空间工具===<br />
从 [[Official Repositories (简体中文)|官方仓库]]:<br />
* [[pacman (简体中文)|安装]] {{Pkg|alsa-utils}} 软件包,其中的 {{ic|alsamixer}} 工具允许用户在控制台或终端中配置声音设备。<br />
* 安装 {{Pkg|alsa-oss}} 软件包,如果想让 OSS 应用程序与dmix(软件混音)协同工作。 <br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。}}<br />
*安装 {{Pkg|alsa-plugins}} 软件包,如果需要 [[#高质量重采样|高质量重采样]]、[[#Upmixing/Downmixing|软件模拟环绕立体声]]和其他高级特性。<br />
<br />
==解除各声道的静音==<br />
目前版本的 ALSA 安装后,所有声道'''默认是静音的''',必须手动解除。<br />
<br />
使用 {{ic|alsamixer}} 的 ncurses 界面,配置十分简单:<br />
<br />
$ alsamixer<br />
<br />
此外,还可以在命令行下使用 {{ic|amixer}}:<br />
<br />
$ amixer sset Master unmute<br />
<br />
下方标有 {{ic|MM}} 的声道是静音的,而标有 {{ic|00}} 的通道已经启用。<br />
<br />
使用左右方向键,选中 {{ic|Master}} 和 {{ic|PCM}} 声道。按下 {{keypress|m}} 键解除静音。使用上方向键增加音量,直到增益值为{{ic|0}}。该值显示在左上方 {{ic|Item:}} 字段后。过高的增益值会导致声音失真。<br />
<br />
要想得到完整的 5.1 或 7.1 环绕立体声,还得解除 Front、Surround、Center、LFE (subwoofer) 和 Side 这些声道的静音(上述名称是 Intel HD Audio 声卡使用的声道名,可能因设备不同而有所差异)。注意,仅有这些设置,系统不会自动将立体声源(多数音乐)提升(upmix)成环绕立体声。如果需要这些功能,请阅读[[#Upmixing/Downmixing]]。<br />
<br />
按下 {{Keypress|Esc}} 键退出 alsamixer。<br />
<br />
{{注意|<br />
* 有一些声卡,需要关闭数字输出或将其调成静音,才能输出模拟音频信号。对于 Soundblaster Audigy LS 声卡,需要把 IEC958 通道设成静音。 <br />
* 有些机器(如 Thinkpad T61),有一个 Speaker 通道,必须按照上述方法打开调整。<br />
* 有些机器(如 Dell E6400),可能还需要把 {{ic|Front}} 和 {{ic|Headphone}} 声道打开调整。}}<br />
<br />
接下来,测试声卡是否工作:<br />
<br />
$ speaker-test -c 2<br />
<br />
根据扬声器的实际情况,调整 -c 后的数值。对于 7.1 声道,这个数字是 8:<br />
<br />
$ speaker-test -c 8<br />
<br />
如果没有正常工作,请继续阅读 [[#配置]] 以及 [[#疑难解答]] 部分。<br />
<br />
{{pkg|alsa-utils}} 软件包提供了两个配置 alsa 的服务:{{ic|alsa-restore.service}} 和 {{ic|alsa-store.service}}。它们分别在开机和关机时自动运行。<br />
<br />
==配置==<br />
=== VirtualBox 虚拟机无声音 ===<br />
<br />
如果使用 VirtualBox 时碰上了麻烦,请尝试以下命令:<br />
<br />
{{hc|$ alsactl init|2=<nowiki><br />
<br />
Found hardware: "ICH" "SigmaTel STAC9700,83,84" "AC97a:83847600" "0x8086" "0x0000"<br />
Hardware is initialized using a generic method<br />
</nowiki><br />
}}<br />
<br />
如果还是不行,请检查所使用的音频软件是否激活了 ALSA 输出。<br />
<br />
===设置默认声卡===<br />
<br />
如果发现开机时声卡次序会发生变化,可以在通过 {{ic|/etc/modprobe.d}} 中的 {{ic|.conf}} 文件(比如 {{ic|/etc/modprobe.d/alsa-base.conf}})手动设置次序。<br />
比如,要让 mia 声卡成为 #0、Intel HDA 声卡成为 #1:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<nowiki><br />
options snd slots=snd_mia,snd_hda_intel<br />
options snd_mia index=0<br />
options snd_hda_intel index=1<br />
</nowiki>}}<br />
<br />
snd_mia 和 snd_hda_intel 分别是两个声卡的内核驱动模块。该配置仅适用于同时装有 mia 声卡和使用 snd_hda_intel 驱动的声卡(通常是板载声卡)的情况。<br />
<br />
如果使用 -2 的 index 值,ALSA 就不会将对应的设备作为主声卡使用。Linux Mint 和 Ubuntu 等发行版使用了以下配置,避免 USB 声卡和其他“非主流”设备变成 index 为 0 的主声卡:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<nowiki><br />
options bt87x index=-2<br />
options cx88_alsa index=-2<br />
options saa7134-alsa index=-2<br />
options snd-atiixp-modem index=-2<br />
options snd-intel8x0m index=-2<br />
options snd-via82xx-modem index=-2<br />
options snd-usb-audio index=-2<br />
options snd-usb-caiaq index=-2<br />
options snd-usb-ua101 index=-2<br />
options snd-usb-us122l index=-2<br />
options snd-usb-usx2y index=-2<br />
# Keep snd-pcsp from being loaded as first soundcard<br />
options snd-pcsp index=-2<br />
# Keep snd-usb-audio from beeing loaded as first soundcard<br />
options snd-usb-audio index=-2<br />
</nowiki>}}<br />
<br />
以上配置会在系统重启后生效。<br />
<br />
{{Out of date|据英文 Wiki 用户反应,{{ic|/etc/asound.conf}} 这个配置文件默认是不存在的,下列操作可能有误。}}<br />
<br />
首先,运行 {{ic|aplay -l}},获取声卡的声卡ID和设备ID:<br />
<br />
{{hc|$ aplay -l|2=<nowiki><br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: Intel [HDA Intel], device 0: CONEXANT Analog [CONEXANT Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: Intel [HDA Intel], device 1: Conexant Digital [Conexant Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 1: JamLab [JamLab], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 2: Audio [Altec Lansing XT1 - USB Audio], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
</nowiki>}}<br />
<br />
拿输出中的最后一项来说,这个声卡的声卡ID为2,设备ID为0。若要把这块声卡作为默认声卡,可以把下列配置添加到系统级别的 {{ic|/etc/asound.conf}} 或用户级别的 {{ic|~/.asoundrc}} 文件。如果文件不存在,可以手动创建。其中的各个ID,请根据实际情况调整:<br />
{{hc|~/.asoundrc|2=<nowiki><br />
defaults.pcm.card 2<br />
defaults.pcm.device 0<br />
defaults.ctl.card 2<br />
</nowiki>}}<br />
“pcm”选项决定用来播放音频的设备,而“ctl”选项决定那个声卡能够由控制工具(如 alsamixer)使用。<br />
<br />
上述配置在重启音频程序(如 mplayer)后立即生效。<br />
<br />
===确认所有声音模块都已经加载===<br />
<br />
一般 udev 都会自动识别出声卡。使用以下命令确认:<br />
<br />
{{hc|<nowiki>$ lsmod | grep '^snd' | column -t</nowiki>|<nowiki><br />
snd_hda_codec_hdmi 22378 4<br />
snd_hda_codec_realtek 294191 1<br />
snd_hda_intel 21738 1<br />
snd_hda_codec 73739 3 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel<br />
snd_hwdep 6134 1 snd_hda_codec<br />
snd_pcm 71032 3 snd_hda_codec_hdmi,snd_hda_intel,snd_hda_codec<br />
snd_timer 18992 1 snd_pcm<br />
snd 55132 9 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel,snd_hda_codec,snd_hwdep,snd_pcm,snd_timer<br />
snd_page_alloc 7017 2 snd_hda_intel,snd_pcm<br />
</nowiki>}}<br />
<br />
如果你的输出和上面类似,那就说明声卡已经被正确识别。<br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。如有需要,请[[Kernel_modules_(简体中文)#装入|手动加载]]。}}<br />
<br />
还可以检查一下 {{ic|/dev/snd/}} 目录,看看是否有这些设备文件:<br />
<br />
{{hc|$ ls -l /dev/snd|2=<nowiki><br />
total 0<br />
crw-rw---- 1 root audio 116, 0 Apr 8 14:17 controlC0<br />
crw-rw---- 1 root audio 116, 32 Apr 8 14:17 controlC1<br />
crw-rw---- 1 root audio 116, 24 Apr 8 14:17 pcmC0D0c<br />
crw-rw---- 1 root audio 116, 16 Apr 8 14:17 pcmC0D0p<br />
crw-rw---- 1 root audio 116, 25 Apr 8 14:17 pcmC0D1c<br />
crw-rw---- 1 root audio 116, 56 Apr 8 14:17 pcmC1D0c<br />
crw-rw---- 1 root audio 116, 48 Apr 8 14:17 pcmC1D0p<br />
crw-rw---- 1 root audio 116, 1 Apr 8 14:17 seq<br />
crw-rw---- 1 root audio 116, 33 Apr 8 14:17 timer<br />
</nowiki>}}<br />
<br />
{{注意|在 IRC 或论坛寻求这方面帮助时,别忘了贴出上面几个命令的输出。}}<br />
<br />
如果你的输出跟上面类似,或至少有 '''controlC0''' 和 '''pcmC0D0p''',那么声卡就已经正常加载了。<br />
<br />
如果出现问题,声卡模块没有正确加载,那么请尝试手动加载模块:<br />
<br />
* 确定声卡对应的驱动模块:[http://www.alsa-project.org/main/index.php/Matrix:Main ALSA Soundcard Matrix]。这些模块都会有一个”snd-“前缀(例如:{{ic|snd-via82xx}})。<br />
* [[Kernel_modules_(简体中文)#装入|加载模块]]。<br />
* 检查 {{ic|/dev/snd}} 目录中的设备文件(参见上文);或者,检查 {{ic|alsamixer}} 或 {{ic|amixer}} 的输出是否正确。<br />
* 修改配置,使 {{ic|snd-<模块名>}} 和 {{ic|snd-pcm-oss}} 模块[[Kernel_modules_(简体中文)#装入|开机自动加载]]。<br />
<br />
===启用 SPDIF 输出===<br />
<br />
(由 Gentoo 论坛的 gralves 提供)<br />
* 在 GNOME 音量控制的选项(Options)标签中,把 IEC958 改成 PCM。该选项可以在首选项中启用。<br />
* 不使用 GNOME 音量控制工具的话:<br />
** 编辑 {{ic|/var/lib/alsa/asound.state}}。该文件是 alsasound 保存混音器设置的地方。<br />
** 找到这一行:“IEC958 Playback Switch”。该行上下应该有一行内容为 {{ic|value:false}},将其改为 {{ic|value:true}}。<br />
** 再找到这一行:”IEC958 Playback AC97-SPSA“。把对应的值改为 0。<br />
** 重启 ALSA。<br />
<br />
{{Out of date|rc.local 在 systemd 中已经弃用,以下方法可能不再适用。}}<br />
<br />
还有另外一个开机自动启用 SPDIF 输出的方法(在 SoundBlaster Audigy 声卡上测试过):<br />
<br />
* 在 {{ic|/etc/rc.local}} 中添加如下内容:<br />
# Use COAX-digital output<br />
amixer set 'IEC958 Optical' 100 unmute<br />
amixer set 'Audigy Analog/Digital Output Jack' on<br />
<br />
使用以下命令,获取你的声卡的数字输出的名称:<br />
$ amixer scontrols<br />
<br />
===系统级均衡器===<br />
====使用 AlsaEqual(包含界面)====<br />
从 [[AUR (简体中文)|AUR]] 安装 {{AUR|alsaequal}}。<br />
<br />
{{注意|如果使用的是64位系统,而又安装了32位的 Flash 插件,这里的设置会导致 Flash 无声。如果发生了这种情况,请禁用 alsaequal 或编译32位版本的 alsaequal。}}<br />
<br />
安装后,把下列内容添加到 ALSA 配置文件({{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}}):<br />
<br />
ctl.equal {<br />
type equal;<br />
}<br />
<br />
pcm.plugequal {<br />
type equal;<br />
# Modify the line below if you do not<br />
#slave.pcm "plughw:0,0";<br />
#by default we want to play from more sources at time:<br />
slave.pcm "plug:dmix";<br />
}<br />
#pcm.equal {<br />
# If you do not want the equalizer to be your<br />
# default soundcard comment the following<br />
# line and uncomment the above line. (You can<br />
# choose it as the output device by addressing<br />
# it with specific apps,eg mpg123 -a equal 06.Back_In_Black.mp3)<br />
pcm.!default {<br />
type plug;<br />
slave.pcm plugequal;<br />
}<br />
<br />
设置好后,切换均衡器:<br />
$ alsamixer -D equal<br />
<br />
需要注意,每个用户的 alsaequal 配置文件都是独立的,位于 {{ic|~/.alsaequal.bin}}。因此,如果想在 [[mpd]] 之类的以独立用户身份运行的程序中使用 AlsaEqual,就得像下面这样:<br />
# su mpd -c 'alsamixer -D equal'<br />
或者,在其他用户的主目录下创建符号链接,指向你的 {{ic|.alsaequal.bin}}……<br />
<br />
=====管理 AlsaEqual 配置=====<br />
从 [http://xyne.archlinux.ca/repos/ Xyne 的软件仓库] 或 [https://aur.archlinux.org/packages.php?ID=62420 AUR] 安装 [http://xyne.archlinux.ca/projects/alsaequal-mgr/ alsaequal-mgr]。<br />
<br />
同上,配置均衡器: <br />
$alsamixer -D equal<br />
<br />
如果对效果感到满意,将其命名(如“foo”)并保存该配置:<br />
$alsaequal-mgr save foo<br />
<br />
重新加载“foo”中的配置:<br />
$alsaequal-mgr load foo<br />
<br />
这样,就可以为游戏、电影、不同音乐流派、网络电话等等创建不同的均衡器配置,以便随时切换。<br />
<br />
参见[http://xyne.archlinux.ca/projects/alsaequal-mgr/ 项目主页]和程序的帮助信息,了解更多设置。<br />
<br />
====使用 mbeq====<br />
{{注意|该软件用到了一个 ladspa 插件,可能导致播放音频时 CPU 占用率偏高。In addition, this was made with stereophonic sound (e.g. headphones) in mind.}}<br />
<br />
安装 {{Pkg|alsa-plugins}}、{{Pkg|ladspa}} 和 {{Pkg|swh-plugins}} 软件包。<br />
<br />
* 在 {{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}} 中添加如下内容:<br />
<br />
{{hc|/etc/asound.conf|2=<nowiki>pcm.eq {<br />
type ladspa<br />
<br />
# The output from the EQ can either go direct to a hardware device<br />
# (if you have a hardware mixer, e.g. SBLive/Audigy) or it can go<br />
# to the software mixer shown here.<br />
#slave.pcm "plughw:0,0"<br />
slave.pcm "plug:dmix"<br />
<br />
# Sometimes you may need to specify the path to the plugins,<br />
# especially if you have just installed them. Once you have logged<br />
# out/restarted this should not be necessary, but if you get errors<br />
# about being unable to find plugins, try uncommenting this.<br />
#path "/usr/lib/ladspa"<br />
<br />
plugins [<br />
{<br />
label mbeq<br />
id 1197<br />
input {<br />
#this setting is here by example, edit to your own taste<br />
#bands: 50hz, 100hz, 156hz, 220hz, 311hz, 440hz, 622hz, 880hz, 1250hz, 1750hz, 25000hz,<br />
#50000hz, 10000hz, 20000hz<br />
controls [ -5 -5 -5 -5 -5 -10 -20 -15 -10 -10 -10 -10 -10 -3 -2 ]<br />
}<br />
}<br />
]<br />
}<br />
<br />
# Redirect the default device to go via the EQ - you may want to do<br />
# this last, once you are sure everything is working. Otherwise all<br />
# your audio programs will break/crash if something has gone wrong.<br />
<br />
pcm.!default {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
<br />
# Redirect the OSS emulation through the EQ too (when programs are running through "aoss")<br />
<br />
pcm.dsp0 {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
</nowiki>}}<br />
<br />
* 完成。若有问题,欢迎前往论坛咨询。<br />
<br />
==高质量重采样==<br />
启用软件混音时,ALSA 会强制把所有音频重采样到相同的频率(如果系统支持,默认是 48000)。dmix 的重采样算法很糟糕,会导致显著的质量损失。<br />
<br />
安装 {{pkg|alsa-plugins}} 和 {{pkg|libsamplerate}}。<br />
<br />
将默认的采样率转换器更改为 libsamplerate:<br />
<br />
{{hc|/etc/asound.conf|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
或:<br />
<br />
{{hc|~/.asoundrc|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
'''samplerate_best''' 输出的声音质量最佳,但需要耗费大量CPU资源进行实时重采样。还有其他的算法可供选择(如 '''samplerate''' 等),但比起默认的算法,这些算法的改进并不明显。<br />
<br />
{{警告|某些系统中,启用 samplerate_best 可能导致 Flash 播放器无声音。}}<br />
<br />
==Upmixing/Downmixing==<br />
===Upmixing===<br />
In order for stereo sources like music to be able to saturate a 5.1 or 7.1 sound system, you need to use upmixing. In darker days this used to be tricky and error prone but nowadays plugins exist to easily take care of this task. Thus, install {{Pkg|alsa-plugins}}.<br />
<br />
Then add the following to your ALSA configuration file of choice (either {{ic|/etc/asound.conf}} or {{ic|~/.asoundrc}}):<br />
pcm.upmix71 {<br />
type upmix<br />
slave.pcm "surround71"<br />
delay 15<br />
channels 8<br />
}<br />
<br />
You can easily change this example for 7.1 upmixing to 5.1 or 4.0.<br />
<br />
This adds a new pcm that you can use for upmixing. If you want all sound sources to go through this pcm, add it as a default below the previous definition like so:<br />
<br />
pcm.!default "plug:upmix71"<br />
<br />
The plugin automatically allows multiple sources to play through it without problems so setting is as a default is actually a safe choice. <br />
If this is not working, you have to setup your own dmixer for the upmixing PCM like this: <br />
pcm.dmix6 {<br />
type asym<br />
playback.pcm {<br />
type dmix<br />
ipc_key 567829<br />
slave {<br />
pcm "hw:0,0"<br />
channels 6<br />
}<br />
}<br />
}<br />
<br />
and use "dmix6" instead of "surround71". <br />
If you experience skipping or distorted sound, consider increasing the buffer_size (to 32768, for example) or use a [[#高质量重采样|high quality resampler]].<br />
<br />
===Downmixing===<br />
If you want to downmix sources to stereo because you, for instance, want to watch a movie with 5.1 sound on a stereo system, you need to use the vdownmix plugin that alsa provides in the same package.<br />
<br />
Again, in your configuration file, add this:<br />
<br />
pcm.!surround51 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
pcm.!surround40 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
<br />
==混音==<br />
===软件混音(dmix)===<br />
<br />
{{注意|在 ALSA 1.0.9rc2 及以上版本中,如果声卡不支持硬件混音,模拟输出将默认使用 dmix 混音,无需额外设置。}}<br />
<br />
如果软件混音未正常启用,那么可以手动创建 {{ic|~/.asoundrc}},加入以下内容:<br />
<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
<br />
这样就启用了软件混音,多个程序能够同时使用声卡。<br />
<br />
至于 S/PDIF 之类的数字音频输出,目前的 ALSA 还不会默认启用 dmix。因此,必须像上面这样手动启用。<br />
<br />
常见问题的解答,参见[[#疑难解答]]部分。<br />
<br />
===硬件混音===<br />
====支持====<br />
如果声卡支持,一般无需任何配置即可启用硬件混音。绝大多数板载声卡都不支持硬件混音,必须得使用软件混音(见上文)。很多独立声卡都支持硬件混音,Linux 下支持较好的有:<br />
<br />
* Creative SoundBlaster Live! (5.1 型号)<br />
* Creative SoundBlaster Audigy (部分型号)<br />
* Creative SoundBlaster Audidy 2 (ZS 系列型号)<br />
* Creative SoundBlaster Audigy 4 (Pro 系列型号)<br />
<br />
{{注意|上述声卡的低端版本(Audigy SE、Audigy 2 NX、SoundBlaster Live! 24bit 以及 SoundBlaster Live! 7.1)使用了不同的芯片,'''不支持'''硬件混音。}}<br />
<br />
{{注意|板载 VIA8237 芯片支持4路硬件混音,但在某些主板上只能使用3路(第4路无声),甚至根本无法使用。即使能正常工作,质量也比不上其他混音方法。}}<br />
<br />
====问题修复====<br />
如果你的声卡是 Intel Corporation 82801I (ICH9 Family) HD Audio Controller (rev 02),且使用了64位系统,需要执行以下命令才能在 Enemy Territory 中听到声音:<br />
echo "et.x86 0 0 direct" > /proc/asound/card0/pcm0p/oss<br />
echo "et.x86 0 0 disable" > /proc/asound/card0/pcm0c/oss<br />
<br />
==疑难解答==<br />
===使用CPU频率动态调整时,音频发生跳跃===<br />
使用 {{ic|ondemand}} 或 {{ic|conservative}} 频率策略、启用CPU频率动态调整时,某些声卡和某些 ALSA 驱动会出现声音跳跃的情况。目前,唯一的解决方案是使用固定的频率,如 {{ic|performance}} 性能策略。<br />
<br />
详情参见 [[CPU Frequency Scaling (简体中文)]]。<br />
<br />
===同时只能有一个用户播放音频===<br />
有时会出现一个用户独占软件混音器的情况。多数情况下这没什么影响,但在使用 [[mpd]] 这样以独立用户身份运行的程序时,就会出现问题。当 mpd 运行时,用户自己就无法播放其他音频了。一种解决方法是,以正在使用的用户身份运行 mpd。另一种方法是,在 ALSA 配置文件的 {{ic|pcm.dmixer}} 部分添加 {{ic|ipc_key_add_uid 0}} 禁止独占。以下是 {{ic|asound.conf}} 示例的片段,省略的部分无需修改:<br />
<br />
...<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
slave {<br />
...<br />
<br />
===无法同时播放多个音频===<br />
如果安装了 [[PulseAudio (简体中文)|PulseAudio]](可能作为 [[GNOME (简体中文)|GNOME]] 的依赖被安装),可能出现无法同步播放的问题,因为 PulseAudio 的默认配置是“独占”声卡。ALSA 的用户可能对自己的配置已经很满意、不希望使用 [[PulseAudio (简体中文)|PulseAudio]]。解决方案是编辑 {{ic|/etc/asound.conf}} 并注释掉以下内容:<br />
# Use PulseAudio by default<br />
#pcm.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
# hint {<br />
# show on<br />
# description "Default ALSA Output (currently PulseAudio Sound Server)"<br />
# }<br />
#}<br />
<br />
可能还需要注释掉下面的内容:<br />
#ctl.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
#}<br />
<br />
比起完全卸载 [[PulseAudio (简体中文)|PulseAudio]],这大概是个更简单的方法。<br />
<br />
以下是 {{ic|/etc/asound.conf}} 配置文件的一个示例:<br />
{{bc|<nowiki>pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
}<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}</nowiki><br />
}}<br />
<br />
{{注意|本 {{ic|/etc/asound.conf}} 本来是配合 [[MPD]] 全局配置使用的。参见[[#同时只能有一个用户播放音频|该节]]关于多用户的内容。}}<br />
<br />
===有时候开机无声===<br />
如果会随机地出现开机无声的问题,原因可能是多声卡的顺序发生了变化。如果是这种情况,请参照[[#设置默认声卡]]进行设置。<br />
<br />
对于 mpd 用户,上述配置无效,请参考[http://mpd.wikia.com/wiki/Configuration#ALSA_MPD_software_volume_control 该文]设置。<br />
<br />
===特定程序的问题===<br />
对于不使用系统声音配置的程序,如 XMMS 和 Mplayer,必须特别设置以应用 ALSA 配置。<br />
<br />
对于 mplayer,将以下内容添加到 {{ic|~/.mplayer/config}} (或系统全局配置文件 {{ic|/etc/mplayer/mplayer.conf}}):<br />
ao=alsa<br />
<br />
对于 XMMS 和 Beep Media Player,打开选项窗口,要把声音驱动设置为 ALSA 而不是 OSS。<br />
<br />
XMMS 的设置方法:<br />
* 启动 XMMS<br />
** 选项 -> 首选项<br />
** 选择 Alsa 输出插件<br />
<br />
对于未提供 ALSA 输出的程序,可以使用 alsa-oss 软件包中的 aoss 工具启动。使用方法是:在要运行的程序的命令行前加上 {{ic|aoss}}。例如:<br />
aoss realplay<br />
<br />
pcm.!default{ ... } 貌似已经不管用了。但下面的设置有效:<br />
pcm.default pcm.dmixer<br />
<br />
===特定型号的配置===<br />
ALSA 会通过 BIOS 检测[http://www.kernel.org/doc/Documentation/sound/alsa/HD-Audio-Models.txt 声卡型号],少数情况下也可能无法正确识别。声卡芯片类型可以在 {{ic|alsamixer}} 界面上找到(如 ALC662),而型号则要设置在 {{ic|/etc/modprobe.d/modprobe.conf}} 或 {{ic|/etc/modprobe.d/sound.conf}} 文件中:<br />
<br />
options snd-hda-intel model=<型号><br />
<br />
具体设置因声卡型号而异。多数情况下不需要特地这样做。如果想知道具体声卡型号的设置,请查阅 [http://bugtrack.alsa-project.org/main/index.php/Matrix:Main Alsa Soundcard List],找到你的声卡型号,点击“Details”,然后查看“Setting up modprobe...”小节,将其中的内容插入 {{ic|/etc/modprobe.d/modprobe.conf}} 文件即可。例如,Intel AC97 的配置如下:<br />
<br />
{{bc|# ALSA portion<br />
alias char-major-116 snd<br />
alias snd-card-0 snd-intel8x0<br />
# module options should go here<br />
<br />
# OSS/Free portion<br />
alias char-major-14 soundcore<br />
alias sound-slot-0 snd-card-0<br />
<br />
# card #1<br />
alias sound-service-0-0 snd-mixer-oss<br />
alias sound-service-0-1 snd-seq-oss<br />
alias sound-service-0-3 snd-pcm-oss<br />
alias sound-service-0-8 snd-seq-oss<br />
alias sound-service-0-12 snd-pcm-oss}}<br />
<br />
===与蜂鸣器冲突===<br />
如果你确认所有声道都已解除静音,声卡驱动配置正确,且音量大小也合适,但还是听不到声音,那么请试试将下面的内容添加到 {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
(如果是 {{ic|via82xx}} 驱动)<br />
options snd-<模块名请自行补充> ac97_quirk=0<br />
<br />
(如果是 {{ic|snd_intel8x0}} 驱动)<br />
options snd-NAME-OF-MODULE ac97_quirk=1<br />
<br />
===No Microphone Input===<br />
In alsamixer, make sure that all the volume levels are up under recording, and that CAPTURE is toggled active on the microphone (e.g. Mic, Internal Mic) and/or on Capture (in alsamixer, select these items and press space). Try making positive Mic Boost and raising Capture and Digital levels higher; this make make static or distortion, but then you can adjust them back down once you are hearing ''something'' when you record<br />
<br />
As the pulseaudio wrapper is shown as "default" in alsamixer, you may have to press F6 to select your actual soundcard first. You may also need to enable and increase the volume of Line-in in the Playback section.<br />
<br />
To test the microphone, run these commands (see arecord's man page for further information):<br />
arecord -d 5 test-mic.wav<br />
aplay test-mic.wav<br />
<br />
If all fails, you may want to eliminate hardware failure by testing the microphone with a different device.<br />
<br />
For at least some computers, muting a microphone (MM) simply means its input does not go immediately to the speakers. It still receives input.<br />
<br />
Many Dell laptops need "-dmic" to be appended to the model name in {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-hda-intel model=dell-m6-dmic<br />
<br />
Some programs use try to use OSS as the main input software. Add the following lines to {{ic|/etc/modprobe.d/modprobe.conf}} to prevent OSS modules from being loaded:<br />
<br />
{{Note|The OSS modules are no longer autoloaded anyway.}}<br />
<br />
blacklist snd_pcm_oss<br />
blacklist snd_mixer_oss<br />
blacklist snd_seq_oss<br />
<br />
See also:<br />
* http://www.alsa-project.org/main/index.php/SoundcardTesting<br />
* http://alsa.opensrc.org/Record_from_mic<br />
<br />
===Setting the default Microphone/Capture Device===<br />
Some applications (Pidgin, Adobe Flash) do not provide an option to change the capture device. It becomes an issue if your microphone is on a separate device (e.g. USB webcam or microphone) than your internal sound card. To change only the default capture device, leaving the default playback device as is, you can modify your {{ic|~/.asoundrc}} file to include the following:<br />
<br />
pcm.usb<br />
{<br />
type hw<br />
card U0x46d0x81d<br />
}<br />
<br />
pcm.!default<br />
{<br />
type asym<br />
playback.pcm<br />
{<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
capture.pcm<br />
{<br />
type plug<br />
slave.pcm "usb"<br />
}<br />
}<br />
<br />
Replace "U0x46d0x81d" with your capture device's card name in ALSA. You can use {{ic|arecord -L}} to list all the capture devices detected by ALSA.<br />
<br />
===Internal Microphone not working===<br />
First make sure all the volume levels are up under recording in alsamixer. In my case adding the following option to /etc/sound.conf and reloading the snd-* module produced a new volume setting called Capture which was capturing for the internal mic. For eg, for snd-hda-intel add<br />
<br />
options snd-hda-intel enable_msi=1<br />
<br />
Then reload the module (as below), up the recording volume of Capture and then test.<br />
<br />
{{bc|# rmmod snd-hda-intel && modprobe snd-hda-intel}}<br />
<br />
===No Sound with Onboard Intel Sound Card===<br />
There may be an issue with two conflicting modules loaded, namely {{ic|snd_intel8x0}} and {{ic|snd_intel8x0m}}. In this case, blacklist snd_intel8x0m:<br />
<br />
{{hc|/etc/modprobe.d/modprobe.conf|blacklist snd_intel8x0m}}<br />
<br />
''Muting'' the "External Amplifier" in {{ic|alsamixer}} or {{ic|amixer}} may also help. See [http://alsa.opensrc.org/Intel8x0#Dell_Inspiron_8600_.28and_probably_others.29 the ALSA wiki].<br />
<br />
Unmuting the "Mix" setting in the mixer might help, also.<br />
<br />
===No Headphone Sound with Onboard Intel Sound Card===<br />
With '''Intel Corporation 82801 I (ICH9 Family) HD Audio Controller''' on laptop, you may need to add this line to modprobe or sound.conf:<br />
<br />
options snd-hda-intel model=$model<br />
<br />
Where $model is any one of the following (in order of possibility to work, but not merit):<br />
<br />
* dell-vostro<br />
* olpc-xo-1_5<br />
* laptop<br />
* dell-m6<br />
* laptop-hpsense<br />
<br />
Note: It may be necessary to put this "options" line below (after) any "alias" lines about your card.<br />
<br />
You can see all the available models in the kernel documentation. For example [http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.35.y.git;a=blob;f=Documentation/sound/alsa/HD-Audio-Models.txt;h=dc25bb84b83b49665a7ed850e7bf5423d50cd3ba;hb=HEAD here], but check that it is the correct version of that document for your kernel version.<br />
<br />
A list of available models is also available [http://www.mjmwired.net/kernel/Documentation/sound/alsa/HD-Audio-Models.txt here]. To know your chip name type the following command (with * being corrected to match your files). Note that some chips could have been renamed and do not directly match the available ones in the file.<br />
<br />
cat /proc/asound/card*/codec* | grep Codec<br />
<br />
Note that there is a high chance none of the input devices (all internal and external mics) will work if you choose to do this, so it is either your headphones or your mic. Please report to ALSA if you are affected by this bug.<br />
<br />
And also, if you have problems getting beeps to work (pcspkr):<br />
<br />
options snd-hda-intel model=$model enable=1 index=0<br />
<br />
===No sound when S/PDIF video card is installed===<br />
Discover available modules and their order:<br />
$ cat /proc/asound/modules<br />
0 snd_hda_intel<br />
1 snd_ca0106<br />
<br />
Disable the undesired video card audio codec in {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
# /etc/modprobe.d/modprobe.conf<br />
#<br />
install snd_hda_intel /bin/false<br />
<br />
If both devices use the same module, it might be possible to disable one of them in the BIOS.<br />
<br />
===Poor sound quality or clipping===<br />
If you experience poor sound quality, try setting the PCM volume (in alsamixer) to a level such that gain is 0.<br />
<br />
If snd-usb-audio driver has been loaded, you could try to enable {{ic|softvol}} in '''/etc/asound.conf''' file. Example configuration for the first audio device:<br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.dsnooper {<br />
type dsnoop<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
channels 2<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.softvol {<br />
type softvol<br />
slave { pcm "dmixer" }<br />
control {<br />
name "Master"<br />
card 0<br />
}<br />
}<br />
ctl.!default {<br />
type hw<br />
card 0<br />
}<br />
ctl.softvol {<br />
type hw<br />
card 0<br />
}<br />
ctl.dmixer {<br />
type hw<br />
card 0<br />
}<br />
<br />
===Pops When Starting and Stopping Playback===<br />
Some modules (e.g. snd_ac97_codec and snd_hda_intel) can power off your sound card when not in use. This can make an audible noise (like a crack/pop/scratch) when turning on/off your sound card. Sometimes even when move the slider volume, or open and close windows (KDE4). If you find this annoying try {{ic|modinfo snd_MY_MODULE}}, and look for a module option that adjusts or disables this feature.<br />
<br />
Example: to disable the power saving mode and solve cracking sound trough speakers issue, using snd_hda_intel add in {{ic|/etc/modprobe.d/modprobe.conf}}<br />
options snd_hda_intel power_save=0<br />
or<br />
options snd_hda_intel power_save=0 power_save_controller=N<br />
<br />
You can also try it with {{ic|1=modprobe snd_hda_intel power_save=0}} before.<br />
<br />
You may also have to unmute the 'Line' ALSA channel for this to work. Any value will do (other than '0' or something too high).<br />
<br />
''Example:'' on an onboard VIA VT1708S (using the snd_hda_intel module) these cracks occured even though 'power_save' was set to 0. Unmuting the 'Line' channel and setting a value of '1' solved the issue.<br />
<br />
Source: https://www.kernel.org/doc/Documentation/sound/alsa/powersave.txt<br />
<br />
If you use a laptop, pm-utils will change {{ic|power_save}} back to 1 when you go onto battery power even if you disable power saving in {{ic|/etc/modprobe.d}}. Disable this for pm-utils by disabling the script that makes the change (see [https://wiki.archlinux.org/index.php/Pm-utils#Disabling_a_hook Disabling a hook] for more information):<br />
# touch /etc/pm/power.d/intel-audio-powersave<br />
<br />
===S/PDIF Output Does Not Work===<br />
<br />
If the optical/coaxial digital output of your motherboard/sound card is not working or stopped working, and have already enabled and unmuted it in alsamixer, try running<br />
# iecset audio on<br />
<br />
as root.<br />
<br />
You can also put this command in {{Ic|rc.local}} as it sometimes it may stop working after a reboot.<br />
<br />
===HDMI Output Does Not Work===<br />
If the HDMI output of your motherboard/sound card is not working or stopped working, and have already enabled and unmuted it in alsamixer, try the following.<br />
<br />
Query for Playback Devices:<br />
<br />
$ aplay -l<br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: NVidia [HDA NVidia], device 0: ALC1200 Analog [ALC1200 Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 1: ALC1200 Digital [ALC1200 Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 3: NVIDIA HDMI [NVIDIA HDMI]<br />
Subdevices: 0/1<br />
Subdevice #0: subdevice #0<br />
<br />
Now that we have the info for the HDMI device, try a test, In the example below, 0 is the card number and 3 is the device number.<br />
<br />
$ aplay -D plughw:0,3 /usr/share/sounds/alsa/Front_Center.wav<br />
<br />
If aplay does not output any errors, but still no sound is heared, "reboot" the receiver, monitor or tv set. Since the HDMI interface executes a handshake on connection, it might have noticed before that there was no audio stream embedded, and disabled audio decoding. In particular, if you are using a standalone window manager (don’t know about Gnome or KDE), you may need to have some sound playing ''while'' plugging in the HDMI cable.<br />
<br />
{{Note|If you are using an ATI card and linux 3.0, check your kernel/grub settings, you most likely need to use the ''''radeon.audio'''<nowiki>=</nowiki>'''1'''' boot parameter, or alternatively set up the option in your modprobe configuration files.}}<br />
<br />
If the test is successful, create or edit your ~/.asoundrc file to set HDMI as the default audio device.<br />
<br />
{{hc|~/.asoundrc|<br />
pcm.!default {<br />
type hw<br />
card 0<br />
device 3<br />
}<br />
}}<br />
<br />
Or you above config does not work try:<br />
{{hc|~/.asoundrc|<br />
defaults.pcm.card 0<br />
defaults.pcm.device 3<br />
defaults.ctl.card 0<br />
}}<br />
<br />
===HDMI Multi-channel PCM output does not work (Intel) ===<br />
As of Linux 3.1 multi-channel PCM output through HDMI with a Intel card (Intel Eaglelake, IbexPeak/Ironlake,SandyBridge/CougarPoint and IvyBridge/PantherPoint) is not yet supported. Support for it has been recently added and expected to be available in Linux 3.2. To make it work in Linux 3.1 you need to apply the following patches:<br />
<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=76adaa34db407f174dd06370cb60f6029c33b465 drm: support routines for HDMI/DP ELD]<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=e0dac65ed45e72fe34cc7ccc76de0ba220bd38bb drm/i915: pass ELD to HDMI/DP audio driver]<br />
<br />
===HP TX2500===<br />
Add these 2 lines into {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-cmipci mpu_port=0x330 fm_port=0x388<br />
options snd-hda-intel index=0 model=toshiba position_fix=1<br />
<br />
options snd-hda-intel model=hp (works for tx2000cto)<br />
<br />
===Skipping Sound When Playing MP3===<br />
If you have sound skipping when playing MP3 files and you have more then 2 speakers attached to your computer (i.e. > 2 speaker system), run alsamixer and disable the channels for the speakers that you '''DO NOT''' have (i.e. do not enable the sound for the center speaker if you do not have a center speaker.<br />
<br />
===Using a USB Headset and External USB Sound Cards===<br />
If you are using a USB headset with ALSA you can try using {{AUR|asoundconf}} (currently only available from the [[AUR]]) to set the headset as the primary sound output. Before running make sure you have usb audio module enabled ({{ic|modprobe snd-usb-audio}}).<br />
<br />
# asoundconf is-active<br />
# asoundconf list<br />
# asoundconf set-default-card <chosen soundcard><br />
<br />
==== Crackling sound with USB devices ====<br />
If you experience crackling sound on USB devices, you can try tuning the snd-usb-audio for minimal latency.<br />
<br />
Add this to your {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
options snd-usb-audio nrpacks=1<br />
<br />
source: http://alsa.opensrc.org/Usb-audio#Tuning_USB_devices_for_minimal_latencies<br />
<br />
==== Hot-plugging a USB Sound Card ====<br />
In order to automatically make a USB Sound Card the primary output device, when the card is plugged in, you can use the following udev rules (e.g. add the following two lines to {{ic|/etc/udev/rules.d/00-local.rules}} and reboot).<br />
<br />
{{bc|1=KERNEL=="pcmC[D0-9cp]*", ACTION=="add", PROGRAM="/bin/sh -c 'K=%k; K=$${K#pcmC}; K=$${K%%D*}; echo defaults.ctl.card $$K > /etc/asound.conf; echo defaults.pcm.card $$K >>/etc/asound.conf'"<br />
KERNEL=="pcmC[D0-9cp]*", ACTION=="remove", PROGRAM="/bin/sh -c 'echo defaults.ctl.card 0 > /etc/asound.conf; echo defaults.pcm.card 0 >>/etc/asound.conf'"}}<br />
<br />
===Error 'Unknown hardware' Appears After a Kernel Update===<br />
The following messages may be displayed during the start-up ALSA after the kernel update:<br />
Unknown hardware "foo" "bar" ...<br />
Hardware is initialized using a guess method<br />
/usr/sbin/alsactl: set_control:nnnn:failed to obtain info for control #mm (No such file or directory)<br />
<br />
or<br />
Found hardware: "HDA-Intel" "VIA VT1705" "HDA:11064397,18490397,00100000" "0x1849" "0x0397"<br />
Hardware is initialized using a generic method<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #1 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #2 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #25 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #26 (No such file or directory)<br />
<br />
Simply store ALSA mixer settings again (as root):<br />
# alsactl -f /var/lib/alsa/asound.state store<br />
<br />
It may be necessary configure ALSA again with alsamixer<br />
<br />
===HDA Analyzer===<br />
If the mappings to your audio pins(plugs) do not correspond but ALSA works fine, you could try HDA Analyzer -- a pyGTK2 GUI for HD-audio control can be found [http://www.alsa-project.org/main/index.php/HDA_Analyzer at the ALSA wiki].<br />
Try tweaking the Widget Control section of the PIN nodes, to make microphones IN and headphone jacks OUT. Referring to the Config Defaults heading is a good idea.<br />
<br />
NOTE: the script is done by such way that it is incompatible with python3 (which is now shipped with ArchLinux) but tries to use it.<br />
The workaround is: open "run.py", find all occurences of "python" (2 occurences - one on the first line, and the second on the last line) and replace them all by "python2".<br />
<br />
NOTE2: the script requires root acces, but running it via su/sudo is bogus. Run it via {{ic|kdesu}} or {{ic|gksu}}.<br />
<br />
===ALSA with SDL===<br />
If you get no sound via SDL and ALSA cannot be chosen from the application. Try setting the environmental variable SDL_AUDIODRIVER to alsa.<br />
# export SDL_AUDIODRIVER=alsa<br />
<br />
===Low Sound Workaround===<br />
<br />
If you are facing low sound even after maxing out your speakers/headphones, you can give the softvol plugin a try. Add the following to {{ic|/etc/asound.conf}}.<br />
{{bc|<nowiki><br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
<br />
pcm.softvol {<br />
type softvol<br />
slave {<br />
pcm "dmix"<br />
}<br />
control {<br />
name "Pre-Amp"<br />
card 0<br />
}<br />
min_dB -5.0<br />
max_dB 20.0<br />
resolution 6<br />
}<br />
</nowiki>}}<br />
{{note| You will probably have to restart the computer, as restarting the alsa daemon did not load the new configuration for me. Also, if the configuration does not work even after restarting, try changing {{ic|plug}} with {{ic|hw}} in the above configuration.}}<br />
<br />
After the changes are loaded successfully, you will see a {{ic|Pre-Amp}} section in alsamixer. You can adjust the levels there.<br />
{{note|Setting a high value for {{ic|Pre-Amp}} can cause sound distortion, so adjust it according to the level that suits you.}}<br />
<br />
===Popping sound after resuming from suspension===<br />
You might hear a popping sound after resuming the computer from suspension. This can be fixed by editing {{ic|/etc/pm/sleep.d/90alsa}} and removing the line that says {{ic|aplay -d 1 /dev/zero}}<br />
<br />
==设置文件范例==<br />
参见:[[Advanced Linux Sound Architecture/Example Configurations]]。<br />
<br />
==相关阅读==<br />
* [http://www.mjmwired.net/kernel/Documentation/sound/alsa/ALSA-Configuration.txt ALSA 模块高级配置]<br />
* [http://alsa.opensrc.org/Main_Page 非官方的 ALSA Wiki]<br />
* [https://bbs.archlinux.org/viewtopic.php?id=36815 如何从 svn 获取驱动代码并编译]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Advanced_Linux_Sound_Architecture_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=251738
Advanced Linux Sound Architecture (简体中文)
2013-03-24T01:01:06Z
<p>Cuihao: /* 疑难解答 */</p>
<hr />
<div>[[Category:简体中文]]<br />
[[Category:Sound (简体中文)]]<br />
[[Category:Audio/Video (简体中文)]]<br />
[[cs:Advanced Linux Sound Architecture]]<br />
[[de:Alsa]]<br />
[[es:Advanced Linux Sound Architecture]]<br />
[[fa:ALSA]]<br />
[[fr:Alsa]]<br />
[[he:Advanced Linux Sound Architecture]]<br />
[[it:Advanced Linux Sound Architecture]]<br />
[[ja:Advanced Linux Sound Architecture]]<br />
[[nl:Advanced Linux Sound Architecture]]<br />
[[pt:Advanced Linux Sound Architecture]]<br />
[[ru:Advanced Linux Sound Architecture]]<br />
[[sk:Advanced Linux Sound Architecture]]<br />
[[th:Advanced Linux Sound Architecture]]<br />
[[tr:Gelişmiş_ses_mimarisi_(ALSA)]]<br />
[[en:Advanced Linux Sound Architecture]]<br />
<br />
{{Article summary start}}<br />
{{Article summary text|ALSA 的使用、配置及疑难解答。}}<br />
{{Article summary heading|相关页面}}<br />
{{Article summary wiki|Sound (简体中文)}}<br />
{{Article summary wiki|Disable PC Speaker Beep (简体中文)}}<br />
{{Article summary end}}<br />
<br />
{{TranslationStatus (简体中文)|Systemd|2013-03-14|250172}}<br />
<br />
[[Wikipedia:zh:ALSA|高级 Linux 声音体系]](Advanced Linux Sound Architecture,'''ALSA''')是Linux中提供声音设备驱动的内核组件,用来代替原来的开放声音系统(Open Sound System,OSSv3)。除了声音设备驱动,'''ALSA'''还包含一个用户空间的函数库,以方便开发者通过高级API使用驱动功能,而不必直接与内核驱动交互。<br />
<br />
{{注意|关于另一种声音体系,请阅读[[Open Sound System (简体中文)|开放声音系统(OSS)]]的页面。}}<br />
<br />
==安装==<br />
Arch 默认的内核已经通过一套模块提供了 ALSA,不必特别安装。<br />
<br />
[[udev (简体中文)|udev]]会在系统启动时自动检测硬件,并加载相应的声音设备驱动模块。这时,你的声卡已经可以工作了,只是所有声道默认都被设置成静音了。<br />
<br />
在本地登录(通过虚拟终端或登录管理器)的用户,都有权限播放音频并调整音量。要让远程登录的用户拥有这些权限,必须把用户[[Users and Groups (简体中文)#用户组管理|加入]] {{ic|audio}} 用户组。该组的成员可以直接访问声音设备,会导致某些程序独占音频输出(破坏软件混音),还可能影响用户快速切换和拖机(multiseat)。因此,除非真的有某些[https://wiki.ubuntu.com/Audio/TheAudioGroup 特殊需求],'''不建议'''把用户加入 {{ic|audio}} 用户组。<br />
<br />
===用户空间工具===<br />
从 [[Official Repositories (简体中文)|官方仓库]]:<br />
* [[pacman (简体中文)|安装]] {{Pkg|alsa-utils}} 软件包,其中的 {{ic|alsamixer}} 工具允许用户在控制台或终端中配置声音设备。<br />
* 安装 {{Pkg|alsa-oss}} 软件包,如果想让 OSS 应用程序与dmix(软件混音)协同工作。 <br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。}}<br />
*安装 {{Pkg|alsa-plugins}} 软件包,如果需要 [[#高质量重采样|高质量重采样]]、[[#Upmixing/Downmixing|软件模拟环绕立体声]]和其他高级特性。<br />
<br />
==解除各声道的静音==<br />
目前版本的 ALSA 安装后,所有声道'''默认是静音的''',必须手动解除。<br />
<br />
使用 {{ic|alsamixer}} 的 ncurses 界面,配置十分简单:<br />
<br />
$ alsamixer<br />
<br />
此外,还可以在命令行下使用 {{ic|amixer}}:<br />
<br />
$ amixer sset Master unmute<br />
<br />
下方标有 {{ic|MM}} 的声道是静音的,而标有 {{ic|00}} 的通道已经启用。<br />
<br />
使用左右方向键,选中 {{ic|Master}} 和 {{ic|PCM}} 声道。按下 {{keypress|m}} 键解除静音。使用上方向键增加音量,直到增益值为{{ic|0}}。该值显示在左上方 {{ic|Item:}} 字段后。过高的增益值会导致声音失真。<br />
<br />
要想得到完整的 5.1 或 7.1 环绕立体声,还得解除 Front、Surround、Center、LFE (subwoofer) 和 Side 这些声道的静音(上述名称是 Intel HD Audio 声卡使用的声道名,可能因设备不同而有所差异)。注意,仅有这些设置,系统不会自动将立体声源(多数音乐)提升(upmix)成环绕立体声。如果需要这些功能,请阅读[[#Upmixing/Downmixing]]。<br />
<br />
按下 {{Keypress|Esc}} 键退出 alsamixer。<br />
<br />
{{注意|<br />
* 有一些声卡,需要关闭数字输出或将其调成静音,才能输出模拟音频信号。对于 Soundblaster Audigy LS 声卡,需要把 IEC958 通道设成静音。 <br />
* 有些机器(如 Thinkpad T61),有一个 Speaker 通道,必须按照上述方法打开调整。<br />
* 有些机器(如 Dell E6400),可能还需要把 {{ic|Front}} 和 {{ic|Headphone}} 声道打开调整。}}<br />
<br />
接下来,测试声卡是否工作:<br />
<br />
$ speaker-test -c 2<br />
<br />
根据扬声器的实际情况,调整 -c 后的数值。对于 7.1 声道,这个数字是 8:<br />
<br />
$ speaker-test -c 8<br />
<br />
如果没有正常工作,请继续阅读 [[#配置]] 以及 [[#疑难解答]] 部分。<br />
<br />
{{pkg|alsa-utils}} 软件包提供了两个配置 alsa 的服务:{{ic|alsa-restore.service}} 和 {{ic|alsa-store.service}}。它们分别在开机和关机时自动运行。<br />
<br />
==配置==<br />
=== VirtualBox 虚拟机无声音 ===<br />
<br />
如果使用 VirtualBox 时碰上了麻烦,请尝试以下命令:<br />
<br />
{{hc|$ alsactl init|2=<nowiki><br />
<br />
Found hardware: "ICH" "SigmaTel STAC9700,83,84" "AC97a:83847600" "0x8086" "0x0000"<br />
Hardware is initialized using a generic method<br />
</nowiki><br />
}}<br />
<br />
如果还是不行,请检查所使用的音频软件是否激活了 ALSA 输出。<br />
<br />
===设置默认声卡===<br />
<br />
如果发现开机时声卡次序会发生变化,可以在通过 {{ic|/etc/modprobe.d}} 中的 {{ic|.conf}} 文件(比如 {{ic|/etc/modprobe.d/alsa-base.conf}})手动设置次序。<br />
比如,要让 mia 声卡成为 #0、Intel HDA 声卡成为 #1:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<nowiki><br />
options snd slots=snd_mia,snd_hda_intel<br />
options snd_mia index=0<br />
options snd_hda_intel index=1<br />
</nowiki>}}<br />
<br />
snd_mia 和 snd_hda_intel 分别是两个声卡的内核驱动模块。该配置仅适用于同时装有 mia 声卡和使用 snd_hda_intel 驱动的声卡(通常是板载声卡)的情况。<br />
<br />
如果使用 -2 的 index 值,ALSA 就不会将对应的设备作为主声卡使用。Linux Mint 和 Ubuntu 等发行版使用了以下配置,避免 USB 声卡和其他“非主流”设备变成 index 为 0 的主声卡:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<nowiki><br />
options bt87x index=-2<br />
options cx88_alsa index=-2<br />
options saa7134-alsa index=-2<br />
options snd-atiixp-modem index=-2<br />
options snd-intel8x0m index=-2<br />
options snd-via82xx-modem index=-2<br />
options snd-usb-audio index=-2<br />
options snd-usb-caiaq index=-2<br />
options snd-usb-ua101 index=-2<br />
options snd-usb-us122l index=-2<br />
options snd-usb-usx2y index=-2<br />
# Keep snd-pcsp from being loaded as first soundcard<br />
options snd-pcsp index=-2<br />
# Keep snd-usb-audio from beeing loaded as first soundcard<br />
options snd-usb-audio index=-2<br />
</nowiki>}}<br />
<br />
以上配置会在系统重启后生效。<br />
<br />
{{Out of date|据英文 Wiki 用户反应,{{ic|/etc/asound.conf}} 这个配置文件默认是不存在的,下列操作可能有误。}}<br />
<br />
首先,运行 {{ic|aplay -l}},获取声卡的声卡ID和设备ID:<br />
<br />
{{hc|$ aplay -l|2=<nowiki><br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: Intel [HDA Intel], device 0: CONEXANT Analog [CONEXANT Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: Intel [HDA Intel], device 1: Conexant Digital [Conexant Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 1: JamLab [JamLab], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 2: Audio [Altec Lansing XT1 - USB Audio], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
</nowiki>}}<br />
<br />
拿输出中的最后一项来说,这个声卡的声卡ID为2,设备ID为0。若要把这块声卡作为默认声卡,可以把下列配置添加到系统级别的 {{ic|/etc/asound.conf}} 或用户级别的 {{ic|~/.asoundrc}} 文件。如果文件不存在,可以手动创建。其中的各个ID,请根据实际情况调整:<br />
{{hc|~/.asoundrc|2=<nowiki><br />
defaults.pcm.card 2<br />
defaults.pcm.device 0<br />
defaults.ctl.card 2<br />
</nowiki>}}<br />
“pcm”选项决定用来播放音频的设备,而“ctl”选项决定那个声卡能够由控制工具(如 alsamixer)使用。<br />
<br />
上述配置在重启音频程序(如 mplayer)后立即生效。<br />
<br />
===确认所有声音模块都已经加载===<br />
<br />
一般 udev 都会自动识别出声卡。使用以下命令确认:<br />
<br />
{{hc|<nowiki>$ lsmod | grep '^snd' | column -t</nowiki>|<nowiki><br />
snd_hda_codec_hdmi 22378 4<br />
snd_hda_codec_realtek 294191 1<br />
snd_hda_intel 21738 1<br />
snd_hda_codec 73739 3 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel<br />
snd_hwdep 6134 1 snd_hda_codec<br />
snd_pcm 71032 3 snd_hda_codec_hdmi,snd_hda_intel,snd_hda_codec<br />
snd_timer 18992 1 snd_pcm<br />
snd 55132 9 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel,snd_hda_codec,snd_hwdep,snd_pcm,snd_timer<br />
snd_page_alloc 7017 2 snd_hda_intel,snd_pcm<br />
</nowiki>}}<br />
<br />
如果你的输出和上面类似,那就说明声卡已经被正确识别。<br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。如有需要,请[[Kernel_modules_(简体中文)#装入|手动加载]]。}}<br />
<br />
还可以检查一下 {{ic|/dev/snd/}} 目录,看看是否有这些设备文件:<br />
<br />
{{hc|$ ls -l /dev/snd|2=<nowiki><br />
total 0<br />
crw-rw---- 1 root audio 116, 0 Apr 8 14:17 controlC0<br />
crw-rw---- 1 root audio 116, 32 Apr 8 14:17 controlC1<br />
crw-rw---- 1 root audio 116, 24 Apr 8 14:17 pcmC0D0c<br />
crw-rw---- 1 root audio 116, 16 Apr 8 14:17 pcmC0D0p<br />
crw-rw---- 1 root audio 116, 25 Apr 8 14:17 pcmC0D1c<br />
crw-rw---- 1 root audio 116, 56 Apr 8 14:17 pcmC1D0c<br />
crw-rw---- 1 root audio 116, 48 Apr 8 14:17 pcmC1D0p<br />
crw-rw---- 1 root audio 116, 1 Apr 8 14:17 seq<br />
crw-rw---- 1 root audio 116, 33 Apr 8 14:17 timer<br />
</nowiki>}}<br />
<br />
{{注意|在 IRC 或论坛寻求这方面帮助时,别忘了贴出上面几个命令的输出。}}<br />
<br />
如果你的输出跟上面类似,或至少有 '''controlC0''' 和 '''pcmC0D0p''',那么声卡就已经正常加载了。<br />
<br />
如果出现问题,声卡模块没有正确加载,那么请尝试手动加载模块:<br />
<br />
* 确定声卡对应的驱动模块:[http://www.alsa-project.org/main/index.php/Matrix:Main ALSA Soundcard Matrix]。这些模块都会有一个”snd-“前缀(例如:{{ic|snd-via82xx}})。<br />
* [[Kernel_modules_(简体中文)#装入|加载模块]]。<br />
* 检查 {{ic|/dev/snd}} 目录中的设备文件(参见上文);或者,检查 {{ic|alsamixer}} 或 {{ic|amixer}} 的输出是否正确。<br />
* 修改配置,使 {{ic|snd-<模块名>}} 和 {{ic|snd-pcm-oss}} 模块[[Kernel_modules_(简体中文)#装入|开机自动加载]]。<br />
<br />
===启用 SPDIF 输出===<br />
<br />
(由 Gentoo 论坛的 gralves 提供)<br />
* 在 GNOME 音量控制的选项(Options)标签中,把 IEC958 改成 PCM。该选项可以在首选项中启用。<br />
* 不使用 GNOME 音量控制工具的话:<br />
** 编辑 {{ic|/var/lib/alsa/asound.state}}。该文件是 alsasound 保存混音器设置的地方。<br />
** 找到这一行:“IEC958 Playback Switch”。该行上下应该有一行内容为 {{ic|value:false}},将其改为 {{ic|value:true}}。<br />
** 再找到这一行:”IEC958 Playback AC97-SPSA“。把对应的值改为 0。<br />
** 重启 ALSA。<br />
<br />
{{Out of date|rc.local 在 systemd 中已经弃用,以下方法可能不再适用。}}<br />
<br />
还有另外一个开机自动启用 SPDIF 输出的方法(在 SoundBlaster Audigy 声卡上测试过):<br />
<br />
* 在 {{ic|/etc/rc.local}} 中添加如下内容:<br />
# Use COAX-digital output<br />
amixer set 'IEC958 Optical' 100 unmute<br />
amixer set 'Audigy Analog/Digital Output Jack' on<br />
<br />
使用以下命令,获取你的声卡的数字输出的名称:<br />
$ amixer scontrols<br />
<br />
===系统级均衡器===<br />
====使用 AlsaEqual(包含界面)====<br />
从 [[AUR (简体中文)|AUR]] 安装 {{AUR|alsaequal}}。<br />
<br />
{{注意|如果使用的是64位系统,而又安装了32位的 Flash 插件,这里的设置会导致 Flash 无声。如果发生了这种情况,请禁用 alsaequal 或编译32位版本的 alsaequal。}}<br />
<br />
安装后,把下列内容添加到 ALSA 配置文件({{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}}):<br />
<br />
ctl.equal {<br />
type equal;<br />
}<br />
<br />
pcm.plugequal {<br />
type equal;<br />
# Modify the line below if you do not<br />
#slave.pcm "plughw:0,0";<br />
#by default we want to play from more sources at time:<br />
slave.pcm "plug:dmix";<br />
}<br />
#pcm.equal {<br />
# If you do not want the equalizer to be your<br />
# default soundcard comment the following<br />
# line and uncomment the above line. (You can<br />
# choose it as the output device by addressing<br />
# it with specific apps,eg mpg123 -a equal 06.Back_In_Black.mp3)<br />
pcm.!default {<br />
type plug;<br />
slave.pcm plugequal;<br />
}<br />
<br />
设置好后,切换均衡器:<br />
$ alsamixer -D equal<br />
<br />
需要注意,每个用户的 alsaequal 配置文件都是独立的,位于 {{ic|~/.alsaequal.bin}}。因此,如果想在 [[mpd]] 之类的以独立用户身份运行的程序中使用 AlsaEqual,就得像下面这样:<br />
# su mpd -c 'alsamixer -D equal'<br />
或者,在其他用户的主目录下创建符号链接,指向你的 {{ic|.alsaequal.bin}}……<br />
<br />
=====管理 AlsaEqual 配置=====<br />
从 [http://xyne.archlinux.ca/repos/ Xyne 的软件仓库] 或 [https://aur.archlinux.org/packages.php?ID=62420 AUR] 安装 [http://xyne.archlinux.ca/projects/alsaequal-mgr/ alsaequal-mgr]。<br />
<br />
同上,配置均衡器: <br />
$alsamixer -D equal<br />
<br />
如果对效果感到满意,将其命名(如“foo”)并保存该配置:<br />
$alsaequal-mgr save foo<br />
<br />
重新加载“foo”中的配置:<br />
$alsaequal-mgr load foo<br />
<br />
这样,就可以为游戏、电影、不同音乐流派、网络电话等等创建不同的均衡器配置,以便随时切换。<br />
<br />
参见[http://xyne.archlinux.ca/projects/alsaequal-mgr/ 项目主页]和程序的帮助信息,了解更多设置。<br />
<br />
====使用 mbeq====<br />
{{注意|该软件用到了一个 ladspa 插件,可能导致播放音频时 CPU 占用率偏高。In addition, this was made with stereophonic sound (e.g. headphones) in mind.}}<br />
<br />
安装 {{Pkg|alsa-plugins}}、{{Pkg|ladspa}} 和 {{Pkg|swh-plugins}} 软件包。<br />
<br />
* 在 {{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}} 中添加如下内容:<br />
<br />
{{hc|/etc/asound.conf|2=<nowiki>pcm.eq {<br />
type ladspa<br />
<br />
# The output from the EQ can either go direct to a hardware device<br />
# (if you have a hardware mixer, e.g. SBLive/Audigy) or it can go<br />
# to the software mixer shown here.<br />
#slave.pcm "plughw:0,0"<br />
slave.pcm "plug:dmix"<br />
<br />
# Sometimes you may need to specify the path to the plugins,<br />
# especially if you have just installed them. Once you have logged<br />
# out/restarted this should not be necessary, but if you get errors<br />
# about being unable to find plugins, try uncommenting this.<br />
#path "/usr/lib/ladspa"<br />
<br />
plugins [<br />
{<br />
label mbeq<br />
id 1197<br />
input {<br />
#this setting is here by example, edit to your own taste<br />
#bands: 50hz, 100hz, 156hz, 220hz, 311hz, 440hz, 622hz, 880hz, 1250hz, 1750hz, 25000hz,<br />
#50000hz, 10000hz, 20000hz<br />
controls [ -5 -5 -5 -5 -5 -10 -20 -15 -10 -10 -10 -10 -10 -3 -2 ]<br />
}<br />
}<br />
]<br />
}<br />
<br />
# Redirect the default device to go via the EQ - you may want to do<br />
# this last, once you are sure everything is working. Otherwise all<br />
# your audio programs will break/crash if something has gone wrong.<br />
<br />
pcm.!default {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
<br />
# Redirect the OSS emulation through the EQ too (when programs are running through "aoss")<br />
<br />
pcm.dsp0 {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
</nowiki>}}<br />
<br />
* 完成。若有问题,欢迎前往论坛咨询。<br />
<br />
==高质量重采样==<br />
启用软件混音时,ALSA 会强制把所有音频重采样到相同的频率(如果系统支持,默认是 48000)。dmix 的重采样算法很糟糕,会导致显著的质量损失。<br />
<br />
安装 {{pkg|alsa-plugins}} 和 {{pkg|libsamplerate}}。<br />
<br />
将默认的采样率转换器更改为 libsamplerate:<br />
<br />
{{hc|/etc/asound.conf|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
或:<br />
<br />
{{hc|~/.asoundrc|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
'''samplerate_best''' 输出的声音质量最佳,但需要耗费大量CPU资源进行实时重采样。还有其他的算法可供选择(如 '''samplerate''' 等),但比起默认的算法,这些算法的改进并不明显。<br />
<br />
{{警告|某些系统中,启用 samplerate_best 可能导致 Flash 播放器无声音。}}<br />
<br />
==Upmixing/Downmixing==<br />
===Upmixing===<br />
In order for stereo sources like music to be able to saturate a 5.1 or 7.1 sound system, you need to use upmixing. In darker days this used to be tricky and error prone but nowadays plugins exist to easily take care of this task. Thus, install {{Pkg|alsa-plugins}}.<br />
<br />
Then add the following to your ALSA configuration file of choice (either {{ic|/etc/asound.conf}} or {{ic|~/.asoundrc}}):<br />
pcm.upmix71 {<br />
type upmix<br />
slave.pcm "surround71"<br />
delay 15<br />
channels 8<br />
}<br />
<br />
You can easily change this example for 7.1 upmixing to 5.1 or 4.0.<br />
<br />
This adds a new pcm that you can use for upmixing. If you want all sound sources to go through this pcm, add it as a default below the previous definition like so:<br />
<br />
pcm.!default "plug:upmix71"<br />
<br />
The plugin automatically allows multiple sources to play through it without problems so setting is as a default is actually a safe choice. <br />
If this is not working, you have to setup your own dmixer for the upmixing PCM like this: <br />
pcm.dmix6 {<br />
type asym<br />
playback.pcm {<br />
type dmix<br />
ipc_key 567829<br />
slave {<br />
pcm "hw:0,0"<br />
channels 6<br />
}<br />
}<br />
}<br />
<br />
and use "dmix6" instead of "surround71". <br />
If you experience skipping or distorted sound, consider increasing the buffer_size (to 32768, for example) or use a [[#高质量重采样|high quality resampler]].<br />
<br />
===Downmixing===<br />
If you want to downmix sources to stereo because you, for instance, want to watch a movie with 5.1 sound on a stereo system, you need to use the vdownmix plugin that alsa provides in the same package.<br />
<br />
Again, in your configuration file, add this:<br />
<br />
pcm.!surround51 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
pcm.!surround40 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
<br />
==混音==<br />
===软件混音(dmix)===<br />
<br />
{{注意|在 ALSA 1.0.9rc2 及以上版本中,如果声卡不支持硬件混音,模拟输出将默认使用 dmix 混音,无需额外设置。}}<br />
<br />
如果软件混音未正常启用,那么可以手动创建 {{ic|~/.asoundrc}},加入以下内容:<br />
<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
<br />
这样就启用了软件混音,多个程序能够同时使用声卡。<br />
<br />
至于 S/PDIF 之类的数字音频输出,目前的 ALSA 还不会默认启用 dmix。因此,必须像上面这样手动启用。<br />
<br />
常见问题的解答,参见[[#疑难解答]]部分。<br />
<br />
===硬件混音===<br />
====支持====<br />
如果声卡支持,一般无需任何配置即可启用硬件混音。绝大多数板载声卡都不支持硬件混音,必须得使用软件混音(见上文)。很多独立声卡都支持硬件混音,Linux 下支持较好的有:<br />
<br />
* Creative SoundBlaster Live! (5.1 型号)<br />
* Creative SoundBlaster Audigy (部分型号)<br />
* Creative SoundBlaster Audidy 2 (ZS 系列型号)<br />
* Creative SoundBlaster Audigy 4 (Pro 系列型号)<br />
<br />
{{注意|上述声卡的低端版本(Audigy SE、Audigy 2 NX、SoundBlaster Live! 24bit 以及 SoundBlaster Live! 7.1)使用了不同的芯片,'''不支持'''硬件混音。}}<br />
<br />
{{注意|板载 VIA8237 芯片支持4路硬件混音,但在某些主板上只能使用3路(第4路无声),甚至根本无法使用。即使能正常工作,质量也比不上其他混音方法。}}<br />
<br />
====问题修复====<br />
如果你的声卡是 Intel Corporation 82801I (ICH9 Family) HD Audio Controller (rev 02),且使用了64位系统,需要执行以下命令才能在 Enemy Territory 中听到声音:<br />
echo "et.x86 0 0 direct" > /proc/asound/card0/pcm0p/oss<br />
echo "et.x86 0 0 disable" > /proc/asound/card0/pcm0c/oss<br />
<br />
==疑难解答==<br />
===使用CPU频率动态调整时,音频发生跳跃===<br />
使用 {{ic|ondemand}} 或 {{ic|conservative}} 频率策略、启用CPU频率动态调整时,某些声卡和某些 ALSA 驱动会出现声音跳跃的情况。目前,唯一的解决方案是使用固定的频率,如 {{ic|performance}} 性能策略。<br />
<br />
详情参见 [[CPU Frequency Scaling (简体中文)]]。<br />
<br />
===同时只能有一个用户播放音频===<br />
有时会出现一个用户独占软件混音器的情况。多数情况下这没什么影响,但在使用 [[mpd]] 这样以独立用户身份运行的程序时,就会出现问题。当 mpd 运行时,用户自己就无法播放其他音频了。一种解决方法是,以正在使用的用户身份运行 mpd。另一种方法是,在 ALSA 配置文件的 {{ic|pcm.dmixer}} 部分添加 {{ic|ipc_key_add_uid 0}} 禁止独占。以下是 {{ic|asound.conf}} 示例的片段,省略的部分无需修改:<br />
<br />
...<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
slave {<br />
...<br />
<br />
===无法同时播放多个音频===<br />
如果安装了 [[PulseAudio (简体中文)|PulseAudio]](可能作为 [[GNOME (简体中文)|GNOME]] 的依赖被安装),可能出现无法同步播放的问题,因为 PulseAudio 的默认配置是“独占”声卡。ALSA 的用户可能对自己的配置已经很满意、不希望使用 [[PulseAudio (简体中文)|PulseAudio]]。解决方案是编辑 {{ic|/etc/asound.conf}} 并注释掉以下内容:<br />
# Use PulseAudio by default<br />
#pcm.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
# hint {<br />
# show on<br />
# description "Default ALSA Output (currently PulseAudio Sound Server)"<br />
# }<br />
#}<br />
<br />
可能还需要注释掉下面的内容:<br />
#ctl.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
#}<br />
<br />
比起完全卸载 [[PulseAudio (简体中文)|PulseAudio]],这大概是个更简单的方法。<br />
<br />
以下是 {{ic|/etc/asound.conf}} 配置文件的一个示例:<br />
{{bc|<nowiki>pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
}<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}</nowiki><br />
}}<br />
<br />
{{注意|本 {{ic|/etc/asound.conf}} 本来是配合 [[MPD]] 全局配置使用的。参见[[#同时只能有一个用户播放音频|该节]]关于多用户的内容。}}<br />
<br />
===有时候开机无声===<br />
如果会随机地出现开机无声的问题,原因可能是多声卡的顺序发生了变化。如果是这种情况,请参照[[#设置默认声卡]]进行设置。<br />
<br />
对于 mpd 用户,上述配置无效,请参考[http://mpd.wikia.com/wiki/Configuration#ALSA_MPD_software_volume_control 该文]设置。<br />
<br />
===特定程序的问题===<br />
对于不使用系统声音配置的程序,如 XMMS 和 Mplayer,必须特别设置以应用 ALSA 配置。<br />
<br />
对于 mplayer,将以下内容添加到 {{ic|~/.mplayer/config}} (或系统全局配置文件 {{ic|/etc/mplayer/mplayer.conf}}):<br />
ao=alsa<br />
<br />
对于 XMMS 和 Beep Media Player,打开选项窗口,要把声音驱动设置为 ALSA 而不是 OSS。<br />
<br />
XMMS 的设置方法:<br />
* 启动 XMMS<br />
** 选项 -> 首选项<br />
** 选择 Alsa 输出插件<br />
<br />
对于未提供 ALSA 输出的程序,可以使用 alsa-oss 软件包中的 aoss 工具启动。使用方法是:在要运行的程序的命令行前加上 {{ic|aoss}}。例如:<br />
aoss realplay<br />
<br />
pcm.!default{ ... } 貌似已经不管用了。但下面的设置有效:<br />
pcm.default pcm.dmixer<br />
<br />
===Model Settings===<br />
ALSA 会通过 BIOS 检测[http://www.kernel.org/doc/Documentation/sound/alsa/HD-Audio-Models.txt 声卡型号],少数情况下也可能无法正确识别。声卡芯片类型可以在 {{ic|alsamixer}} 界面上找到(如 ALC662),而型号则要设置在 {{ic|/etc/modprobe.d/modprobe.conf}} 或 {{ic|/etc/modprobe.d/sound.conf}} 文件中:<br />
<br />
options snd-hda-intel model=<型号><br />
<br />
具体设置因声卡型号而异。多数情况下不需要特地这样做。如果想知道具体声卡型号的设置,请查阅 [http://bugtrack.alsa-project.org/main/index.php/Matrix:Main Alsa Soundcard List],找到你的声卡型号,点击“Details”,然后查看“Setting up modprobe...”小节,将其中的内容插入 {{ic|/etc/modprobe.d/modprobe.conf}} 文件即可。例如,Intel AC97 的配置如下:<br />
<br />
{{bc|# ALSA portion<br />
alias char-major-116 snd<br />
alias snd-card-0 snd-intel8x0<br />
# module options should go here<br />
<br />
# OSS/Free portion<br />
alias char-major-14 soundcore<br />
alias sound-slot-0 snd-card-0<br />
<br />
# card #1<br />
alias sound-service-0-0 snd-mixer-oss<br />
alias sound-service-0-1 snd-seq-oss<br />
alias sound-service-0-3 snd-pcm-oss<br />
alias sound-service-0-8 snd-seq-oss<br />
alias sound-service-0-12 snd-pcm-oss}}<br />
<br />
===与蜂鸣器冲突===<br />
如果你确认所有声道都已解除静音,声卡驱动配置正确,且音量大小也合适,但还是听不到声音,那么请试试将下面的内容添加到 {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
(如果是 {{ic|via82xx}} 驱动)<br />
options snd-<模块名请自行补充> ac97_quirk=0<br />
<br />
(如果是 {{ic|snd_intel8x0}} 驱动)<br />
options snd-NAME-OF-MODULE ac97_quirk=1<br />
<br />
===No Microphone Input===<br />
In alsamixer, make sure that all the volume levels are up under recording, and that CAPTURE is toggled active on the microphone (e.g. Mic, Internal Mic) and/or on Capture (in alsamixer, select these items and press space). Try making positive Mic Boost and raising Capture and Digital levels higher; this make make static or distortion, but then you can adjust them back down once you are hearing ''something'' when you record<br />
<br />
As the pulseaudio wrapper is shown as "default" in alsamixer, you may have to press F6 to select your actual soundcard first. You may also need to enable and increase the volume of Line-in in the Playback section.<br />
<br />
To test the microphone, run these commands (see arecord's man page for further information):<br />
arecord -d 5 test-mic.wav<br />
aplay test-mic.wav<br />
<br />
If all fails, you may want to eliminate hardware failure by testing the microphone with a different device.<br />
<br />
For at least some computers, muting a microphone (MM) simply means its input does not go immediately to the speakers. It still receives input.<br />
<br />
Many Dell laptops need "-dmic" to be appended to the model name in {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-hda-intel model=dell-m6-dmic<br />
<br />
Some programs use try to use OSS as the main input software. Add the following lines to {{ic|/etc/modprobe.d/modprobe.conf}} to prevent OSS modules from being loaded:<br />
<br />
{{Note|The OSS modules are no longer autoloaded anyway.}}<br />
<br />
blacklist snd_pcm_oss<br />
blacklist snd_mixer_oss<br />
blacklist snd_seq_oss<br />
<br />
See also:<br />
* http://www.alsa-project.org/main/index.php/SoundcardTesting<br />
* http://alsa.opensrc.org/Record_from_mic<br />
<br />
===Setting the default Microphone/Capture Device===<br />
Some applications (Pidgin, Adobe Flash) do not provide an option to change the capture device. It becomes an issue if your microphone is on a separate device (e.g. USB webcam or microphone) than your internal sound card. To change only the default capture device, leaving the default playback device as is, you can modify your {{ic|~/.asoundrc}} file to include the following:<br />
<br />
pcm.usb<br />
{<br />
type hw<br />
card U0x46d0x81d<br />
}<br />
<br />
pcm.!default<br />
{<br />
type asym<br />
playback.pcm<br />
{<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
capture.pcm<br />
{<br />
type plug<br />
slave.pcm "usb"<br />
}<br />
}<br />
<br />
Replace "U0x46d0x81d" with your capture device's card name in ALSA. You can use {{ic|arecord -L}} to list all the capture devices detected by ALSA.<br />
<br />
===Internal Microphone not working===<br />
First make sure all the volume levels are up under recording in alsamixer. In my case adding the following option to /etc/sound.conf and reloading the snd-* module produced a new volume setting called Capture which was capturing for the internal mic. For eg, for snd-hda-intel add<br />
<br />
options snd-hda-intel enable_msi=1<br />
<br />
Then reload the module (as below), up the recording volume of Capture and then test.<br />
<br />
{{bc|# rmmod snd-hda-intel && modprobe snd-hda-intel}}<br />
<br />
===No Sound with Onboard Intel Sound Card===<br />
There may be an issue with two conflicting modules loaded, namely {{ic|snd_intel8x0}} and {{ic|snd_intel8x0m}}. In this case, blacklist snd_intel8x0m:<br />
<br />
{{hc|/etc/modprobe.d/modprobe.conf|blacklist snd_intel8x0m}}<br />
<br />
''Muting'' the "External Amplifier" in {{ic|alsamixer}} or {{ic|amixer}} may also help. See [http://alsa.opensrc.org/Intel8x0#Dell_Inspiron_8600_.28and_probably_others.29 the ALSA wiki].<br />
<br />
Unmuting the "Mix" setting in the mixer might help, also.<br />
<br />
===No Headphone Sound with Onboard Intel Sound Card===<br />
With '''Intel Corporation 82801 I (ICH9 Family) HD Audio Controller''' on laptop, you may need to add this line to modprobe or sound.conf:<br />
<br />
options snd-hda-intel model=$model<br />
<br />
Where $model is any one of the following (in order of possibility to work, but not merit):<br />
<br />
* dell-vostro<br />
* olpc-xo-1_5<br />
* laptop<br />
* dell-m6<br />
* laptop-hpsense<br />
<br />
Note: It may be necessary to put this "options" line below (after) any "alias" lines about your card.<br />
<br />
You can see all the available models in the kernel documentation. For example [http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.35.y.git;a=blob;f=Documentation/sound/alsa/HD-Audio-Models.txt;h=dc25bb84b83b49665a7ed850e7bf5423d50cd3ba;hb=HEAD here], but check that it is the correct version of that document for your kernel version.<br />
<br />
A list of available models is also available [http://www.mjmwired.net/kernel/Documentation/sound/alsa/HD-Audio-Models.txt here]. To know your chip name type the following command (with * being corrected to match your files). Note that some chips could have been renamed and do not directly match the available ones in the file.<br />
<br />
cat /proc/asound/card*/codec* | grep Codec<br />
<br />
Note that there is a high chance none of the input devices (all internal and external mics) will work if you choose to do this, so it is either your headphones or your mic. Please report to ALSA if you are affected by this bug.<br />
<br />
And also, if you have problems getting beeps to work (pcspkr):<br />
<br />
options snd-hda-intel model=$model enable=1 index=0<br />
<br />
===No sound when S/PDIF video card is installed===<br />
Discover available modules and their order:<br />
$ cat /proc/asound/modules<br />
0 snd_hda_intel<br />
1 snd_ca0106<br />
<br />
Disable the undesired video card audio codec in {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
# /etc/modprobe.d/modprobe.conf<br />
#<br />
install snd_hda_intel /bin/false<br />
<br />
If both devices use the same module, it might be possible to disable one of them in the BIOS.<br />
<br />
===Poor sound quality or clipping===<br />
If you experience poor sound quality, try setting the PCM volume (in alsamixer) to a level such that gain is 0.<br />
<br />
If snd-usb-audio driver has been loaded, you could try to enable {{ic|softvol}} in '''/etc/asound.conf''' file. Example configuration for the first audio device:<br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.dsnooper {<br />
type dsnoop<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
channels 2<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.softvol {<br />
type softvol<br />
slave { pcm "dmixer" }<br />
control {<br />
name "Master"<br />
card 0<br />
}<br />
}<br />
ctl.!default {<br />
type hw<br />
card 0<br />
}<br />
ctl.softvol {<br />
type hw<br />
card 0<br />
}<br />
ctl.dmixer {<br />
type hw<br />
card 0<br />
}<br />
<br />
===Pops When Starting and Stopping Playback===<br />
Some modules (e.g. snd_ac97_codec and snd_hda_intel) can power off your sound card when not in use. This can make an audible noise (like a crack/pop/scratch) when turning on/off your sound card. Sometimes even when move the slider volume, or open and close windows (KDE4). If you find this annoying try {{ic|modinfo snd_MY_MODULE}}, and look for a module option that adjusts or disables this feature.<br />
<br />
Example: to disable the power saving mode and solve cracking sound trough speakers issue, using snd_hda_intel add in {{ic|/etc/modprobe.d/modprobe.conf}}<br />
options snd_hda_intel power_save=0<br />
or<br />
options snd_hda_intel power_save=0 power_save_controller=N<br />
<br />
You can also try it with {{ic|1=modprobe snd_hda_intel power_save=0}} before.<br />
<br />
You may also have to unmute the 'Line' ALSA channel for this to work. Any value will do (other than '0' or something too high).<br />
<br />
''Example:'' on an onboard VIA VT1708S (using the snd_hda_intel module) these cracks occured even though 'power_save' was set to 0. Unmuting the 'Line' channel and setting a value of '1' solved the issue.<br />
<br />
Source: https://www.kernel.org/doc/Documentation/sound/alsa/powersave.txt<br />
<br />
If you use a laptop, pm-utils will change {{ic|power_save}} back to 1 when you go onto battery power even if you disable power saving in {{ic|/etc/modprobe.d}}. Disable this for pm-utils by disabling the script that makes the change (see [https://wiki.archlinux.org/index.php/Pm-utils#Disabling_a_hook Disabling a hook] for more information):<br />
# touch /etc/pm/power.d/intel-audio-powersave<br />
<br />
===S/PDIF Output Does Not Work===<br />
<br />
If the optical/coaxial digital output of your motherboard/sound card is not working or stopped working, and have already enabled and unmuted it in alsamixer, try running<br />
# iecset audio on<br />
<br />
as root.<br />
<br />
You can also put this command in {{Ic|rc.local}} as it sometimes it may stop working after a reboot.<br />
<br />
===HDMI Output Does Not Work===<br />
If the HDMI output of your motherboard/sound card is not working or stopped working, and have already enabled and unmuted it in alsamixer, try the following.<br />
<br />
Query for Playback Devices:<br />
<br />
$ aplay -l<br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: NVidia [HDA NVidia], device 0: ALC1200 Analog [ALC1200 Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 1: ALC1200 Digital [ALC1200 Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 3: NVIDIA HDMI [NVIDIA HDMI]<br />
Subdevices: 0/1<br />
Subdevice #0: subdevice #0<br />
<br />
Now that we have the info for the HDMI device, try a test, In the example below, 0 is the card number and 3 is the device number.<br />
<br />
$ aplay -D plughw:0,3 /usr/share/sounds/alsa/Front_Center.wav<br />
<br />
If aplay does not output any errors, but still no sound is heared, "reboot" the receiver, monitor or tv set. Since the HDMI interface executes a handshake on connection, it might have noticed before that there was no audio stream embedded, and disabled audio decoding. In particular, if you are using a standalone window manager (don’t know about Gnome or KDE), you may need to have some sound playing ''while'' plugging in the HDMI cable.<br />
<br />
{{Note|If you are using an ATI card and linux 3.0, check your kernel/grub settings, you most likely need to use the ''''radeon.audio'''<nowiki>=</nowiki>'''1'''' boot parameter, or alternatively set up the option in your modprobe configuration files.}}<br />
<br />
If the test is successful, create or edit your ~/.asoundrc file to set HDMI as the default audio device.<br />
<br />
{{hc|~/.asoundrc|<br />
pcm.!default {<br />
type hw<br />
card 0<br />
device 3<br />
}<br />
}}<br />
<br />
Or you above config does not work try:<br />
{{hc|~/.asoundrc|<br />
defaults.pcm.card 0<br />
defaults.pcm.device 3<br />
defaults.ctl.card 0<br />
}}<br />
<br />
===HDMI Multi-channel PCM output does not work (Intel) ===<br />
As of Linux 3.1 multi-channel PCM output through HDMI with a Intel card (Intel Eaglelake, IbexPeak/Ironlake,SandyBridge/CougarPoint and IvyBridge/PantherPoint) is not yet supported. Support for it has been recently added and expected to be available in Linux 3.2. To make it work in Linux 3.1 you need to apply the following patches:<br />
<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=76adaa34db407f174dd06370cb60f6029c33b465 drm: support routines for HDMI/DP ELD]<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=e0dac65ed45e72fe34cc7ccc76de0ba220bd38bb drm/i915: pass ELD to HDMI/DP audio driver]<br />
<br />
===HP TX2500===<br />
Add these 2 lines into {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-cmipci mpu_port=0x330 fm_port=0x388<br />
options snd-hda-intel index=0 model=toshiba position_fix=1<br />
<br />
options snd-hda-intel model=hp (works for tx2000cto)<br />
<br />
===Skipping Sound When Playing MP3===<br />
If you have sound skipping when playing MP3 files and you have more then 2 speakers attached to your computer (i.e. > 2 speaker system), run alsamixer and disable the channels for the speakers that you '''DO NOT''' have (i.e. do not enable the sound for the center speaker if you do not have a center speaker.<br />
<br />
===Using a USB Headset and External USB Sound Cards===<br />
If you are using a USB headset with ALSA you can try using {{AUR|asoundconf}} (currently only available from the [[AUR]]) to set the headset as the primary sound output. Before running make sure you have usb audio module enabled ({{ic|modprobe snd-usb-audio}}).<br />
<br />
# asoundconf is-active<br />
# asoundconf list<br />
# asoundconf set-default-card <chosen soundcard><br />
<br />
==== Crackling sound with USB devices ====<br />
If you experience crackling sound on USB devices, you can try tuning the snd-usb-audio for minimal latency.<br />
<br />
Add this to your {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
options snd-usb-audio nrpacks=1<br />
<br />
source: http://alsa.opensrc.org/Usb-audio#Tuning_USB_devices_for_minimal_latencies<br />
<br />
==== Hot-plugging a USB Sound Card ====<br />
In order to automatically make a USB Sound Card the primary output device, when the card is plugged in, you can use the following udev rules (e.g. add the following two lines to {{ic|/etc/udev/rules.d/00-local.rules}} and reboot).<br />
<br />
{{bc|1=KERNEL=="pcmC[D0-9cp]*", ACTION=="add", PROGRAM="/bin/sh -c 'K=%k; K=$${K#pcmC}; K=$${K%%D*}; echo defaults.ctl.card $$K > /etc/asound.conf; echo defaults.pcm.card $$K >>/etc/asound.conf'"<br />
KERNEL=="pcmC[D0-9cp]*", ACTION=="remove", PROGRAM="/bin/sh -c 'echo defaults.ctl.card 0 > /etc/asound.conf; echo defaults.pcm.card 0 >>/etc/asound.conf'"}}<br />
<br />
===Error 'Unknown hardware' Appears After a Kernel Update===<br />
The following messages may be displayed during the start-up ALSA after the kernel update:<br />
Unknown hardware "foo" "bar" ...<br />
Hardware is initialized using a guess method<br />
/usr/sbin/alsactl: set_control:nnnn:failed to obtain info for control #mm (No such file or directory)<br />
<br />
or<br />
Found hardware: "HDA-Intel" "VIA VT1705" "HDA:11064397,18490397,00100000" "0x1849" "0x0397"<br />
Hardware is initialized using a generic method<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #1 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #2 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #25 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #26 (No such file or directory)<br />
<br />
Simply store ALSA mixer settings again (as root):<br />
# alsactl -f /var/lib/alsa/asound.state store<br />
<br />
It may be necessary configure ALSA again with alsamixer<br />
<br />
===HDA Analyzer===<br />
If the mappings to your audio pins(plugs) do not correspond but ALSA works fine, you could try HDA Analyzer -- a pyGTK2 GUI for HD-audio control can be found [http://www.alsa-project.org/main/index.php/HDA_Analyzer at the ALSA wiki].<br />
Try tweaking the Widget Control section of the PIN nodes, to make microphones IN and headphone jacks OUT. Referring to the Config Defaults heading is a good idea.<br />
<br />
NOTE: the script is done by such way that it is incompatible with python3 (which is now shipped with ArchLinux) but tries to use it.<br />
The workaround is: open "run.py", find all occurences of "python" (2 occurences - one on the first line, and the second on the last line) and replace them all by "python2".<br />
<br />
NOTE2: the script requires root acces, but running it via su/sudo is bogus. Run it via {{ic|kdesu}} or {{ic|gksu}}.<br />
<br />
===ALSA with SDL===<br />
If you get no sound via SDL and ALSA cannot be chosen from the application. Try setting the environmental variable SDL_AUDIODRIVER to alsa.<br />
# export SDL_AUDIODRIVER=alsa<br />
<br />
===Low Sound Workaround===<br />
<br />
If you are facing low sound even after maxing out your speakers/headphones, you can give the softvol plugin a try. Add the following to {{ic|/etc/asound.conf}}.<br />
{{bc|<nowiki><br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
<br />
pcm.softvol {<br />
type softvol<br />
slave {<br />
pcm "dmix"<br />
}<br />
control {<br />
name "Pre-Amp"<br />
card 0<br />
}<br />
min_dB -5.0<br />
max_dB 20.0<br />
resolution 6<br />
}<br />
</nowiki>}}<br />
{{note| You will probably have to restart the computer, as restarting the alsa daemon did not load the new configuration for me. Also, if the configuration does not work even after restarting, try changing {{ic|plug}} with {{ic|hw}} in the above configuration.}}<br />
<br />
After the changes are loaded successfully, you will see a {{ic|Pre-Amp}} section in alsamixer. You can adjust the levels there.<br />
{{note|Setting a high value for {{ic|Pre-Amp}} can cause sound distortion, so adjust it according to the level that suits you.}}<br />
<br />
===Popping sound after resuming from suspension===<br />
You might hear a popping sound after resuming the computer from suspension. This can be fixed by editing {{ic|/etc/pm/sleep.d/90alsa}} and removing the line that says {{ic|aplay -d 1 /dev/zero}}<br />
<br />
==设置文件范例==<br />
参见:[[Advanced Linux Sound Architecture/Example Configurations]]。<br />
<br />
==相关阅读==<br />
* [http://www.mjmwired.net/kernel/Documentation/sound/alsa/ALSA-Configuration.txt ALSA 模块高级配置]<br />
* [http://alsa.opensrc.org/Main_Page 非官方的 ALSA Wiki]<br />
* [https://bbs.archlinux.org/viewtopic.php?id=36815 如何从 svn 获取驱动代码并编译]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Advanced_Linux_Sound_Architecture_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=250790
Advanced Linux Sound Architecture (简体中文)
2013-03-15T11:59:01Z
<p>Cuihao: /* 疑难解答 */</p>
<hr />
<div>[[Category:简体中文]]<br />
[[Category:Sound (简体中文)]]<br />
[[Category:Audio/Video (简体中文)]]<br />
[[cs:Advanced Linux Sound Architecture]]<br />
[[de:Alsa]]<br />
[[es:Advanced Linux Sound Architecture]]<br />
[[fa:ALSA]]<br />
[[fr:Alsa]]<br />
[[he:Advanced Linux Sound Architecture]]<br />
[[it:Advanced Linux Sound Architecture]]<br />
[[ja:Advanced Linux Sound Architecture]]<br />
[[nl:Advanced Linux Sound Architecture]]<br />
[[pt:Advanced Linux Sound Architecture]]<br />
[[ru:Advanced Linux Sound Architecture]]<br />
[[sk:Advanced Linux Sound Architecture]]<br />
[[th:Advanced Linux Sound Architecture]]<br />
[[tr:Gelişmiş_ses_mimarisi_(ALSA)]]<br />
[[en:Advanced Linux Sound Architecture]]<br />
<br />
{{Article summary start}}<br />
{{Article summary text|ALSA 的使用、配置及疑难解答。}}<br />
{{Article summary heading|相关页面}}<br />
{{Article summary wiki|Sound (简体中文)}}<br />
{{Article summary wiki|Disable PC Speaker Beep (简体中文)}}<br />
{{Article summary end}}<br />
<br />
{{TranslationStatus (简体中文)|Systemd|2013-03-14|250172}}<br />
<br />
[[Wikipedia:zh:ALSA|高级 Linux 声音体系]](Advanced Linux Sound Architecture,'''ALSA''')是Linux中提供声音设备驱动的内核组件,用来代替原来的开放声音系统(Open Sound System,OSSv3)。除了声音设备驱动,'''ALSA'''还包含一个用户空间的函数库,以方便开发者通过高级API使用驱动功能,而不必直接与内核驱动交互。<br />
<br />
{{注意|关于另一种声音体系,请阅读[[Open Sound System (简体中文)|开放声音系统(OSS)]]的页面。}}<br />
<br />
==安装==<br />
Arch 默认的内核已经通过一套模块提供了 ALSA,不必特别安装。<br />
<br />
[[udev (简体中文)|udev]]会在系统启动时自动检测硬件,并加载相应的声音设备驱动模块。这时,你的声卡已经可以工作了,只是所有声道默认都被设置成静音了。<br />
<br />
在本地登录(通过虚拟终端或登录管理器)的用户,都有权限播放音频并调整音量。要让远程登录的用户拥有这些权限,必须把用户[[Users and Groups (简体中文)#用户组管理|加入]] {{ic|audio}} 用户组。该组的成员可以直接访问声音设备,会导致某些程序独占音频输出(破坏软件混音),还可能影响用户快速切换和拖机(multiseat)。因此,除非真的有某些[https://wiki.ubuntu.com/Audio/TheAudioGroup 特殊需求],'''不建议'''把用户加入 {{ic|audio}} 用户组。<br />
<br />
===用户空间工具===<br />
从 [[Official Repositories (简体中文)|官方仓库]]:<br />
* [[pacman (简体中文)|安装]] {{Pkg|alsa-utils}} 软件包,其中的 {{ic|alsamixer}} 工具允许用户在控制台或终端中配置声音设备。<br />
* 安装 {{Pkg|alsa-oss}} 软件包,如果想让 OSS 应用程序与dmix(软件混音)协同工作。 <br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。}}<br />
*安装 {{Pkg|alsa-plugins}} 软件包,如果需要 [[#高质量重采样|高质量重采样]]、[[#Upmixing/Downmixing|软件模拟环绕立体声]]和其他高级特性。<br />
<br />
==解除各声道的静音==<br />
目前版本的 ALSA 安装后,所有声道'''默认是静音的''',必须手动解除。<br />
<br />
使用 {{ic|alsamixer}} 的 ncurses 界面,配置十分简单:<br />
<br />
$ alsamixer<br />
<br />
此外,还可以在命令行下使用 {{ic|amixer}}:<br />
<br />
$ amixer sset Master unmute<br />
<br />
下方标有 {{ic|MM}} 的声道是静音的,而标有 {{ic|00}} 的通道已经启用。<br />
<br />
使用左右方向键,选中 {{ic|Master}} 和 {{ic|PCM}} 声道。按下 {{keypress|m}} 键解除静音。使用上方向键增加音量,直到增益值为{{ic|0}}。该值显示在左上方 {{ic|Item:}} 字段后。过高的增益值会导致声音失真。<br />
<br />
要想得到完整的 5.1 或 7.1 环绕立体声,还得解除 Front、Surround、Center、LFE (subwoofer) 和 Side 这些声道的静音(上述名称是 Intel HD Audio 声卡使用的声道名,可能因设备不同而有所差异)。注意,仅有这些设置,系统不会自动将立体声源(多数音乐)提升(upmix)成环绕立体声。如果需要这些功能,请阅读[[#Upmixing/Downmixing]]。<br />
<br />
按下 {{Keypress|Esc}} 键退出 alsamixer。<br />
<br />
{{注意|<br />
* 有一些声卡,需要关闭数字输出或将其调成静音,才能输出模拟音频信号。对于 Soundblaster Audigy LS 声卡,需要把 IEC958 通道设成静音。 <br />
* 有些机器(如 Thinkpad T61),有一个 Speaker 通道,必须按照上述方法打开调整。<br />
* 有些机器(如 Dell E6400),可能还需要把 {{ic|Front}} 和 {{ic|Headphone}} 声道打开调整。}}<br />
<br />
接下来,测试声卡是否工作:<br />
<br />
$ speaker-test -c 2<br />
<br />
根据扬声器的实际情况,调整 -c 后的数值。对于 7.1 声道,这个数字是 8:<br />
<br />
$ speaker-test -c 8<br />
<br />
如果没有正常工作,请继续阅读 [[#配置]] 以及 [[#疑难解答]] 部分。<br />
<br />
{{pkg|alsa-utils}} 软件包提供了两个配置 alsa 的服务:{{ic|alsa-restore.service}} 和 {{ic|alsa-store.service}}。它们分别在开机和关机时自动运行。<br />
<br />
==配置==<br />
=== VirtualBox 虚拟机无声音 ===<br />
<br />
如果使用 VirtualBox 时碰上了麻烦,请尝试以下命令:<br />
<br />
{{hc|$ alsactl init|2=<nowiki><br />
<br />
Found hardware: "ICH" "SigmaTel STAC9700,83,84" "AC97a:83847600" "0x8086" "0x0000"<br />
Hardware is initialized using a generic method<br />
</nowiki><br />
}}<br />
<br />
如果还是不行,请检查所使用的音频软件是否激活了 ALSA 输出。<br />
<br />
===设置默认声卡===<br />
<br />
如果发现开机时声卡次序会发生变化,可以在通过 {{ic|/etc/modprobe.d}} 中的 {{ic|.conf}} 文件(比如 {{ic|/etc/modprobe.d/alsa-base.conf}})手动设置次序。<br />
比如,要让 mia 声卡成为 #0、Intel HDA 声卡成为 #1:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<nowiki><br />
options snd slots=snd_mia,snd_hda_intel<br />
options snd_mia index=0<br />
options snd_hda_intel index=1<br />
</nowiki>}}<br />
<br />
snd_mia 和 snd_hda_intel 分别是两个声卡的内核驱动模块。该配置仅适用于同时装有 mia 声卡和使用 snd_hda_intel 驱动的声卡(通常是板载声卡)的情况。<br />
<br />
如果使用 -2 的 index 值,ALSA 就不会将对应的设备作为主声卡使用。Linux Mint 和 Ubuntu 等发行版使用了以下配置,避免 USB 声卡和其他“非主流”设备变成 index 为 0 的主声卡:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<nowiki><br />
options bt87x index=-2<br />
options cx88_alsa index=-2<br />
options saa7134-alsa index=-2<br />
options snd-atiixp-modem index=-2<br />
options snd-intel8x0m index=-2<br />
options snd-via82xx-modem index=-2<br />
options snd-usb-audio index=-2<br />
options snd-usb-caiaq index=-2<br />
options snd-usb-ua101 index=-2<br />
options snd-usb-us122l index=-2<br />
options snd-usb-usx2y index=-2<br />
# Keep snd-pcsp from being loaded as first soundcard<br />
options snd-pcsp index=-2<br />
# Keep snd-usb-audio from beeing loaded as first soundcard<br />
options snd-usb-audio index=-2<br />
</nowiki>}}<br />
<br />
以上配置会在系统重启后生效。<br />
<br />
{{Out of date|据英文 Wiki 用户反应,{{ic|/etc/asound.conf}} 这个配置文件默认是不存在的,下列操作可能有误。}}<br />
<br />
首先,运行 {{ic|aplay -l}},获取声卡的声卡ID和设备ID:<br />
<br />
{{hc|$ aplay -l|2=<nowiki><br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: Intel [HDA Intel], device 0: CONEXANT Analog [CONEXANT Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: Intel [HDA Intel], device 1: Conexant Digital [Conexant Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 1: JamLab [JamLab], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 2: Audio [Altec Lansing XT1 - USB Audio], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
</nowiki>}}<br />
<br />
拿输出中的最后一项来说,这个声卡的声卡ID为2,设备ID为0。若要把这块声卡作为默认声卡,可以把下列配置添加到系统级别的 {{ic|/etc/asound.conf}} 或用户级别的 {{ic|~/.asoundrc}} 文件。如果文件不存在,可以手动创建。其中的各个ID,请根据实际情况调整:<br />
{{hc|~/.asoundrc|2=<nowiki><br />
defaults.pcm.card 2<br />
defaults.pcm.device 0<br />
defaults.ctl.card 2<br />
</nowiki>}}<br />
“pcm”选项决定用来播放音频的设备,而“ctl”选项决定那个声卡能够由控制工具(如 alsamixer)使用。<br />
<br />
上述配置在重启音频程序(如 mplayer)后立即生效。<br />
<br />
===确认所有声音模块都已经加载===<br />
<br />
一般 udev 都会自动识别出声卡。使用以下命令确认:<br />
<br />
{{hc|<nowiki>$ lsmod | grep '^snd' | column -t</nowiki>|<nowiki><br />
snd_hda_codec_hdmi 22378 4<br />
snd_hda_codec_realtek 294191 1<br />
snd_hda_intel 21738 1<br />
snd_hda_codec 73739 3 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel<br />
snd_hwdep 6134 1 snd_hda_codec<br />
snd_pcm 71032 3 snd_hda_codec_hdmi,snd_hda_intel,snd_hda_codec<br />
snd_timer 18992 1 snd_pcm<br />
snd 55132 9 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel,snd_hda_codec,snd_hwdep,snd_pcm,snd_timer<br />
snd_page_alloc 7017 2 snd_hda_intel,snd_pcm<br />
</nowiki>}}<br />
<br />
如果你的输出和上面类似,那就说明声卡已经被正确识别。<br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。如有需要,请[[Kernel_modules_(简体中文)#装入|手动加载]]。}}<br />
<br />
还可以检查一下 {{ic|/dev/snd/}} 目录,看看是否有这些设备文件:<br />
<br />
{{hc|$ ls -l /dev/snd|2=<nowiki><br />
total 0<br />
crw-rw---- 1 root audio 116, 0 Apr 8 14:17 controlC0<br />
crw-rw---- 1 root audio 116, 32 Apr 8 14:17 controlC1<br />
crw-rw---- 1 root audio 116, 24 Apr 8 14:17 pcmC0D0c<br />
crw-rw---- 1 root audio 116, 16 Apr 8 14:17 pcmC0D0p<br />
crw-rw---- 1 root audio 116, 25 Apr 8 14:17 pcmC0D1c<br />
crw-rw---- 1 root audio 116, 56 Apr 8 14:17 pcmC1D0c<br />
crw-rw---- 1 root audio 116, 48 Apr 8 14:17 pcmC1D0p<br />
crw-rw---- 1 root audio 116, 1 Apr 8 14:17 seq<br />
crw-rw---- 1 root audio 116, 33 Apr 8 14:17 timer<br />
</nowiki>}}<br />
<br />
{{注意|在 IRC 或论坛寻求这方面帮助时,别忘了贴出上面几个命令的输出。}}<br />
<br />
如果你的输出跟上面类似,或至少有 '''controlC0''' 和 '''pcmC0D0p''',那么声卡就已经正常加载了。<br />
<br />
如果出现问题,声卡模块没有正确加载,那么请尝试手动加载模块:<br />
<br />
* 确定声卡对应的驱动模块:[http://www.alsa-project.org/main/index.php/Matrix:Main ALSA Soundcard Matrix]。这些模块都会有一个”snd-“前缀(例如:{{ic|snd-via82xx}})。<br />
* [[Kernel_modules_(简体中文)#装入|加载模块]]。<br />
* 检查 {{ic|/dev/snd}} 目录中的设备文件(参见上文);或者,检查 {{ic|alsamixer}} 或 {{ic|amixer}} 的输出是否正确。<br />
* 修改配置,使 {{ic|snd-<模块名>}} 和 {{ic|snd-pcm-oss}} 模块[[Kernel_modules_(简体中文)#装入|开机自动加载]]。<br />
<br />
===启用 SPDIF 输出===<br />
<br />
(由 Gentoo 论坛的 gralves 提供)<br />
* 在 GNOME 音量控制的选项(Options)标签中,把 IEC958 改成 PCM。该选项可以在首选项中启用。<br />
* 不使用 GNOME 音量控制工具的话:<br />
** 编辑 {{ic|/var/lib/alsa/asound.state}}。该文件是 alsasound 保存混音器设置的地方。<br />
** 找到这一行:“IEC958 Playback Switch”。该行上下应该有一行内容为 {{ic|value:false}},将其改为 {{ic|value:true}}。<br />
** 再找到这一行:”IEC958 Playback AC97-SPSA“。把对应的值改为 0。<br />
** 重启 ALSA。<br />
<br />
{{Out of date|rc.local 在 systemd 中已经弃用,以下方法可能不再适用。}}<br />
<br />
还有另外一个开机自动启用 SPDIF 输出的方法(在 SoundBlaster Audigy 声卡上测试过):<br />
<br />
* 在 {{ic|/etc/rc.local}} 中添加如下内容:<br />
# Use COAX-digital output<br />
amixer set 'IEC958 Optical' 100 unmute<br />
amixer set 'Audigy Analog/Digital Output Jack' on<br />
<br />
使用以下命令,获取你的声卡的数字输出的名称:<br />
$ amixer scontrols<br />
<br />
===系统级均衡器===<br />
====使用 AlsaEqual(包含界面)====<br />
从 [[AUR (简体中文)|AUR]] 安装 {{AUR|alsaequal}}。<br />
<br />
{{注意|如果使用的是64位系统,而又安装了32位的 Flash 插件,这里的设置会导致 Flash 无声。如果发生了这种情况,请禁用 alsaequal 或编译32位版本的 alsaequal。}}<br />
<br />
安装后,把下列内容添加到 ALSA 配置文件({{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}}):<br />
<br />
ctl.equal {<br />
type equal;<br />
}<br />
<br />
pcm.plugequal {<br />
type equal;<br />
# Modify the line below if you do not<br />
#slave.pcm "plughw:0,0";<br />
#by default we want to play from more sources at time:<br />
slave.pcm "plug:dmix";<br />
}<br />
#pcm.equal {<br />
# If you do not want the equalizer to be your<br />
# default soundcard comment the following<br />
# line and uncomment the above line. (You can<br />
# choose it as the output device by addressing<br />
# it with specific apps,eg mpg123 -a equal 06.Back_In_Black.mp3)<br />
pcm.!default {<br />
type plug;<br />
slave.pcm plugequal;<br />
}<br />
<br />
设置好后,切换均衡器:<br />
$ alsamixer -D equal<br />
<br />
需要注意,每个用户的 alsaequal 配置文件都是独立的,位于 {{ic|~/.alsaequal.bin}}。因此,如果想在 [[mpd]] 之类的以独立用户身份运行的程序中使用 AlsaEqual,就得像下面这样:<br />
# su mpd -c 'alsamixer -D equal'<br />
或者,在其他用户的主目录下创建符号链接,指向你的 {{ic|.alsaequal.bin}}……<br />
<br />
=====管理 AlsaEqual 配置=====<br />
从 [http://xyne.archlinux.ca/repos/ Xyne 的软件仓库] 或 [https://aur.archlinux.org/packages.php?ID=62420 AUR] 安装 [http://xyne.archlinux.ca/projects/alsaequal-mgr/ alsaequal-mgr]。<br />
<br />
同上,配置均衡器: <br />
$alsamixer -D equal<br />
<br />
如果对效果感到满意,将其命名(如“foo”)并保存该配置:<br />
$alsaequal-mgr save foo<br />
<br />
重新加载“foo”中的配置:<br />
$alsaequal-mgr load foo<br />
<br />
这样,就可以为游戏、电影、不同音乐流派、网络电话等等创建不同的均衡器配置,以便随时切换。<br />
<br />
参见[http://xyne.archlinux.ca/projects/alsaequal-mgr/ 项目主页]和程序的帮助信息,了解更多设置。<br />
<br />
====使用 mbeq====<br />
{{注意|该软件用到了一个 ladspa 插件,可能导致播放音频时 CPU 占用率偏高。In addition, this was made with stereophonic sound (e.g. headphones) in mind.}}<br />
<br />
安装 {{Pkg|alsa-plugins}}、{{Pkg|ladspa}} 和 {{Pkg|swh-plugins}} 软件包。<br />
<br />
* 在 {{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}} 中添加如下内容:<br />
<br />
{{hc|/etc/asound.conf|2=<nowiki>pcm.eq {<br />
type ladspa<br />
<br />
# The output from the EQ can either go direct to a hardware device<br />
# (if you have a hardware mixer, e.g. SBLive/Audigy) or it can go<br />
# to the software mixer shown here.<br />
#slave.pcm "plughw:0,0"<br />
slave.pcm "plug:dmix"<br />
<br />
# Sometimes you may need to specify the path to the plugins,<br />
# especially if you have just installed them. Once you have logged<br />
# out/restarted this should not be necessary, but if you get errors<br />
# about being unable to find plugins, try uncommenting this.<br />
#path "/usr/lib/ladspa"<br />
<br />
plugins [<br />
{<br />
label mbeq<br />
id 1197<br />
input {<br />
#this setting is here by example, edit to your own taste<br />
#bands: 50hz, 100hz, 156hz, 220hz, 311hz, 440hz, 622hz, 880hz, 1250hz, 1750hz, 25000hz,<br />
#50000hz, 10000hz, 20000hz<br />
controls [ -5 -5 -5 -5 -5 -10 -20 -15 -10 -10 -10 -10 -10 -3 -2 ]<br />
}<br />
}<br />
]<br />
}<br />
<br />
# Redirect the default device to go via the EQ - you may want to do<br />
# this last, once you are sure everything is working. Otherwise all<br />
# your audio programs will break/crash if something has gone wrong.<br />
<br />
pcm.!default {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
<br />
# Redirect the OSS emulation through the EQ too (when programs are running through "aoss")<br />
<br />
pcm.dsp0 {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
</nowiki>}}<br />
<br />
* 完成。若有问题,欢迎前往论坛咨询。<br />
<br />
==高质量重采样==<br />
启用软件混音时,ALSA 会强制把所有音频重采样到相同的频率(如果系统支持,默认是 48000)。dmix 的重采样算法很糟糕,会导致显著的质量损失。<br />
<br />
安装 {{pkg|alsa-plugins}} 和 {{pkg|libsamplerate}}。<br />
<br />
将默认的采样率转换器更改为 libsamplerate:<br />
<br />
{{hc|/etc/asound.conf|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
或:<br />
<br />
{{hc|~/.asoundrc|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
'''samplerate_best''' 输出的声音质量最佳,但需要耗费大量CPU资源进行实时重采样。还有其他的算法可供选择(如 '''samplerate''' 等),但比起默认的算法,这些算法的改进并不明显。<br />
<br />
{{警告|某些系统中,启用 samplerate_best 可能导致 Flash 播放器无声音。}}<br />
<br />
==Upmixing/Downmixing==<br />
===Upmixing===<br />
In order for stereo sources like music to be able to saturate a 5.1 or 7.1 sound system, you need to use upmixing. In darker days this used to be tricky and error prone but nowadays plugins exist to easily take care of this task. Thus, install {{Pkg|alsa-plugins}}.<br />
<br />
Then add the following to your ALSA configuration file of choice (either {{ic|/etc/asound.conf}} or {{ic|~/.asoundrc}}):<br />
pcm.upmix71 {<br />
type upmix<br />
slave.pcm "surround71"<br />
delay 15<br />
channels 8<br />
}<br />
<br />
You can easily change this example for 7.1 upmixing to 5.1 or 4.0.<br />
<br />
This adds a new pcm that you can use for upmixing. If you want all sound sources to go through this pcm, add it as a default below the previous definition like so:<br />
<br />
pcm.!default "plug:upmix71"<br />
<br />
The plugin automatically allows multiple sources to play through it without problems so setting is as a default is actually a safe choice. <br />
If this is not working, you have to setup your own dmixer for the upmixing PCM like this: <br />
pcm.dmix6 {<br />
type asym<br />
playback.pcm {<br />
type dmix<br />
ipc_key 567829<br />
slave {<br />
pcm "hw:0,0"<br />
channels 6<br />
}<br />
}<br />
}<br />
<br />
and use "dmix6" instead of "surround71". <br />
If you experience skipping or distorted sound, consider increasing the buffer_size (to 32768, for example) or use a [[#高质量重采样|high quality resampler]].<br />
<br />
===Downmixing===<br />
If you want to downmix sources to stereo because you, for instance, want to watch a movie with 5.1 sound on a stereo system, you need to use the vdownmix plugin that alsa provides in the same package.<br />
<br />
Again, in your configuration file, add this:<br />
<br />
pcm.!surround51 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
pcm.!surround40 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
<br />
==混音==<br />
===软件混音(dmix)===<br />
<br />
{{注意|在 ALSA 1.0.9rc2 及以上版本中,如果声卡不支持硬件混音,模拟输出将默认使用 dmix 混音,无需额外设置。}}<br />
<br />
如果软件混音未正常启用,那么可以手动创建 {{ic|~/.asoundrc}},加入以下内容:<br />
<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
<br />
这样就启用了软件混音,多个程序能够同时使用声卡。<br />
<br />
至于 S/PDIF 之类的数字音频输出,目前的 ALSA 还不会默认启用 dmix。因此,必须像上面这样手动启用。<br />
<br />
常见问题的解答,参见[[#疑难解答]]部分。<br />
<br />
===硬件混音===<br />
====支持====<br />
如果声卡支持,一般无需任何配置即可启用硬件混音。绝大多数板载声卡都不支持硬件混音,必须得使用软件混音(见上文)。很多独立声卡都支持硬件混音,Linux 下支持较好的有:<br />
<br />
* Creative SoundBlaster Live! (5.1 型号)<br />
* Creative SoundBlaster Audigy (部分型号)<br />
* Creative SoundBlaster Audidy 2 (ZS 系列型号)<br />
* Creative SoundBlaster Audigy 4 (Pro 系列型号)<br />
<br />
{{注意|上述声卡的低端版本(Audigy SE、Audigy 2 NX、SoundBlaster Live! 24bit 以及 SoundBlaster Live! 7.1)使用了不同的芯片,'''不支持'''硬件混音。}}<br />
<br />
{{注意|板载 VIA8237 芯片支持4路硬件混音,但在某些主板上只能使用3路(第4路无声),甚至根本无法使用。即使能正常工作,质量也比不上其他混音方法。}}<br />
<br />
====问题修复====<br />
如果你的声卡是 Intel Corporation 82801I (ICH9 Family) HD Audio Controller (rev 02),且使用了64位系统,需要执行以下命令才能在 Enemy Territory 中听到声音:<br />
echo "et.x86 0 0 direct" > /proc/asound/card0/pcm0p/oss<br />
echo "et.x86 0 0 disable" > /proc/asound/card0/pcm0c/oss<br />
<br />
==疑难解答==<br />
===使用CPU频率动态调整时,音频发生跳跃===<br />
使用 {{ic|ondemand}} 或 {{ic|conservative}} 频率策略、启用CPU频率动态调整时,某些声卡和某些 ALSA 驱动会出现声音跳跃的情况。目前,唯一的解决方案是使用固定的频率,如 {{ic|performance}} 性能策略。<br />
<br />
详情参见 [[CPU Frequency Scaling (简体中文)]]。<br />
<br />
===同时只能有一个用户播放音频===<br />
有时会出现一个用户独占软件混音器的情况。多数情况下这没什么影响,但在使用 [[mpd]] 这样以独立用户身份运行的程序时,就会出现问题。当 mpd 运行时,用户自己就无法播放其他音频了。一种解决方法是,以正在使用的用户身份运行 mpd。另一种方法是,在 ALSA 配置文件的 {{ic|pcm.dmixer}} 部分添加 {{ic|ipc_key_add_uid 0}} 禁止独占。以下是 {{ic|asound.conf}} 示例的片段,省略的部分无需修改:<br />
<br />
...<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
slave {<br />
...<br />
<br />
===无法同时播放多个音频===<br />
如果安装了 [[PulseAudio (简体中文)|PulseAudio]](可能作为 [[GNOME (简体中文)|GNOME]] 的依赖被安装),可能出现无法同步播放的问题,因为 PulseAudio 的默认配置是“独占”声卡。ALSA 的用户可能对自己的配置已经很满意、不希望使用 [[PulseAudio (简体中文)|PulseAudio]]。解决方案是编辑 {{ic|/etc/asound.conf}} 并注释掉以下内容:<br />
# Use PulseAudio by default<br />
#pcm.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
# hint {<br />
# show on<br />
# description "Default ALSA Output (currently PulseAudio Sound Server)"<br />
# }<br />
#}<br />
<br />
可能还需要注释掉下面的内容:<br />
#ctl.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
#}<br />
<br />
比起完全卸载 [[PulseAudio (简体中文)|PulseAudio]],这大概是个更简单的方法。<br />
<br />
以下是 {{ic|/etc/asound.conf}} 配置文件的一个示例:<br />
{{bc|<nowiki>pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
}<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}</nowiki><br />
}}<br />
<br />
{{注意|本 {{ic|/etc/asound.conf}} 本来是配合 [[MPD]] 全局配置使用的。参见[[#同时只能有一个用户播放音频|该节]]关于多用户的内容。}}<br />
<br />
===有时候开机无声===<br />
如果会随机地出现开机无声的问题,原因可能是多声卡的顺序发生了变化。如果是这种情况,请参照[[#设置默认声卡]]进行设置。<br />
<br />
对于 mpd 用户,上述配置无效,请参考[http://mpd.wikia.com/wiki/Configuration#ALSA_MPD_software_volume_control 该文]设置。<br />
<br />
===特定程序的问题===<br />
对于不使用系统声音配置的程序,如 XMMS 和 Mplayer,必须特别设置以应用 ALSA 配置。<br />
<br />
对于 mplayer,将以下内容添加到 {{ic|~/.mplayer/config}} (或系统全局配置文件 {{ic|/etc/mplayer/mplayer.conf}}):<br />
ao=alsa<br />
<br />
对于 XMMS 和 Beep Media Player,打开选项窗口,要把声音驱动设置为 ALSA 而不是 OSS。<br />
<br />
XMMS 的设置方法:<br />
* 启动 XMMS<br />
** 选项 -> 首选项<br />
** 选择 Alsa 输出插件<br />
<br />
对于未提供 ALSA 输出的程序,可以使用 alsa-oss 软件包中的 aoss 工具启动。使用方法是:在要运行的程序的命令行前加上 {{ic|aoss}}。例如:<br />
aoss realplay<br />
<br />
pcm.!default{ ... } 貌似已经不管用了。但下面的设置有效:<br />
pcm.default pcm.dmixer<br />
<br />
===Model Settings===<br />
Although Alsa detects your soundcard through the BIOS at times Alsa may not be able to recognize your [http://www.kernel.org/doc/Documentation/sound/alsa/HD-Audio-Models.txt model type]. The soundcard chip can be found in {{ic|alsamixer}} (e.g. ALC662) and the model can be set in {{ic|/etc/modprobe.d/modprobe.conf}} or {{ic|/etc/modprobe.d/sound.conf}}. For example:<br />
<br />
options snd-hda-intel model=MODEL<br />
<br />
There are other model settings too. For most cases Alsa defaults will do. If you want to look at more specific settings for your soundcard take a look at the [http://bugtrack.alsa-project.org/main/index.php/Matrix:Main Alsa Soundcard List] find your model, then Details, then look at the "Setting up modprobe..." section. Enter these values in {{ic|/etc/modprobe.d/modprobe.conf}}. For example, for an Intel AC97 audio:<br />
<br />
{{bc|# ALSA portion<br />
alias char-major-116 snd<br />
alias snd-card-0 snd-intel8x0<br />
# module options should go here<br />
<br />
# OSS/Free portion<br />
alias char-major-14 soundcore<br />
alias sound-slot-0 snd-card-0<br />
<br />
# card #1<br />
alias sound-service-0-0 snd-mixer-oss<br />
alias sound-service-0-1 snd-seq-oss<br />
alias sound-service-0-3 snd-pcm-oss<br />
alias sound-service-0-8 snd-seq-oss<br />
alias sound-service-0-12 snd-pcm-oss}}<br />
<br />
===Conflicting PC Speaker===<br />
If you are sure nothing is muted, that your drivers are installed correctly, and that your volume is right, but you still do not hear anything, then try adding the following line to {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
options snd-NAME-OF-MODULE ac97_quirk=0<br />
<br />
The above fix has been observed to work with {{ic|via82xx}}<br />
options snd-NAME-OF-MODULE ac97_quirk=1<br />
The above fix has been reported to work with {{ic|snd_intel8x0}}<br />
<br />
===No Microphone Input===<br />
In alsamixer, make sure that all the volume levels are up under recording, and that CAPTURE is toggled active on the microphone (e.g. Mic, Internal Mic) and/or on Capture (in alsamixer, select these items and press space). Try making positive Mic Boost and raising Capture and Digital levels higher; this make make static or distortion, but then you can adjust them back down once you are hearing ''something'' when you record<br />
<br />
As the pulseaudio wrapper is shown as "default" in alsamixer, you may have to press F6 to select your actual soundcard first. You may also need to enable and increase the volume of Line-in in the Playback section.<br />
<br />
To test the microphone, run these commands (see arecord's man page for further information):<br />
arecord -d 5 test-mic.wav<br />
aplay test-mic.wav<br />
<br />
If all fails, you may want to eliminate hardware failure by testing the microphone with a different device.<br />
<br />
For at least some computers, muting a microphone (MM) simply means its input does not go immediately to the speakers. It still receives input.<br />
<br />
Many Dell laptops need "-dmic" to be appended to the model name in {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-hda-intel model=dell-m6-dmic<br />
<br />
Some programs use try to use OSS as the main input software. Add the following lines to {{ic|/etc/modprobe.d/modprobe.conf}} to prevent OSS modules from being loaded:<br />
<br />
{{Note|The OSS modules are no longer autoloaded anyway.}}<br />
<br />
blacklist snd_pcm_oss<br />
blacklist snd_mixer_oss<br />
blacklist snd_seq_oss<br />
<br />
See also:<br />
* http://www.alsa-project.org/main/index.php/SoundcardTesting<br />
* http://alsa.opensrc.org/Record_from_mic<br />
<br />
===Setting the default Microphone/Capture Device===<br />
Some applications (Pidgin, Adobe Flash) do not provide an option to change the capture device. It becomes an issue if your microphone is on a separate device (e.g. USB webcam or microphone) than your internal sound card. To change only the default capture device, leaving the default playback device as is, you can modify your {{ic|~/.asoundrc}} file to include the following:<br />
<br />
pcm.usb<br />
{<br />
type hw<br />
card U0x46d0x81d<br />
}<br />
<br />
pcm.!default<br />
{<br />
type asym<br />
playback.pcm<br />
{<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
capture.pcm<br />
{<br />
type plug<br />
slave.pcm "usb"<br />
}<br />
}<br />
<br />
Replace "U0x46d0x81d" with your capture device's card name in ALSA. You can use {{ic|arecord -L}} to list all the capture devices detected by ALSA.<br />
<br />
===Internal Microphone not working===<br />
First make sure all the volume levels are up under recording in alsamixer. In my case adding the following option to /etc/sound.conf and reloading the snd-* module produced a new volume setting called Capture which was capturing for the internal mic. For eg, for snd-hda-intel add<br />
<br />
options snd-hda-intel enable_msi=1<br />
<br />
Then reload the module (as below), up the recording volume of Capture and then test.<br />
<br />
{{bc|# rmmod snd-hda-intel && modprobe snd-hda-intel}}<br />
<br />
===No Sound with Onboard Intel Sound Card===<br />
There may be an issue with two conflicting modules loaded, namely {{ic|snd_intel8x0}} and {{ic|snd_intel8x0m}}. In this case, blacklist snd_intel8x0m:<br />
<br />
{{hc|/etc/modprobe.d/modprobe.conf|blacklist snd_intel8x0m}}<br />
<br />
''Muting'' the "External Amplifier" in {{ic|alsamixer}} or {{ic|amixer}} may also help. See [http://alsa.opensrc.org/Intel8x0#Dell_Inspiron_8600_.28and_probably_others.29 the ALSA wiki].<br />
<br />
Unmuting the "Mix" setting in the mixer might help, also.<br />
<br />
===No Headphone Sound with Onboard Intel Sound Card===<br />
With '''Intel Corporation 82801 I (ICH9 Family) HD Audio Controller''' on laptop, you may need to add this line to modprobe or sound.conf:<br />
<br />
options snd-hda-intel model=$model<br />
<br />
Where $model is any one of the following (in order of possibility to work, but not merit):<br />
<br />
* dell-vostro<br />
* olpc-xo-1_5<br />
* laptop<br />
* dell-m6<br />
* laptop-hpsense<br />
<br />
Note: It may be necessary to put this "options" line below (after) any "alias" lines about your card.<br />
<br />
You can see all the available models in the kernel documentation. For example [http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.35.y.git;a=blob;f=Documentation/sound/alsa/HD-Audio-Models.txt;h=dc25bb84b83b49665a7ed850e7bf5423d50cd3ba;hb=HEAD here], but check that it is the correct version of that document for your kernel version.<br />
<br />
A list of available models is also available [http://www.mjmwired.net/kernel/Documentation/sound/alsa/HD-Audio-Models.txt here]. To know your chip name type the following command (with * being corrected to match your files). Note that some chips could have been renamed and do not directly match the available ones in the file.<br />
<br />
cat /proc/asound/card*/codec* | grep Codec<br />
<br />
Note that there is a high chance none of the input devices (all internal and external mics) will work if you choose to do this, so it is either your headphones or your mic. Please report to ALSA if you are affected by this bug.<br />
<br />
And also, if you have problems getting beeps to work (pcspkr):<br />
<br />
options snd-hda-intel model=$model enable=1 index=0<br />
<br />
===No sound when S/PDIF video card is installed===<br />
Discover available modules and their order:<br />
$ cat /proc/asound/modules<br />
0 snd_hda_intel<br />
1 snd_ca0106<br />
<br />
Disable the undesired video card audio codec in {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
# /etc/modprobe.d/modprobe.conf<br />
#<br />
install snd_hda_intel /bin/false<br />
<br />
If both devices use the same module, it might be possible to disable one of them in the BIOS.<br />
<br />
===Poor sound quality or clipping===<br />
If you experience poor sound quality, try setting the PCM volume (in alsamixer) to a level such that gain is 0.<br />
<br />
If snd-usb-audio driver has been loaded, you could try to enable {{ic|softvol}} in '''/etc/asound.conf''' file. Example configuration for the first audio device:<br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.dsnooper {<br />
type dsnoop<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
channels 2<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.softvol {<br />
type softvol<br />
slave { pcm "dmixer" }<br />
control {<br />
name "Master"<br />
card 0<br />
}<br />
}<br />
ctl.!default {<br />
type hw<br />
card 0<br />
}<br />
ctl.softvol {<br />
type hw<br />
card 0<br />
}<br />
ctl.dmixer {<br />
type hw<br />
card 0<br />
}<br />
<br />
===Pops When Starting and Stopping Playback===<br />
Some modules (e.g. snd_ac97_codec and snd_hda_intel) can power off your sound card when not in use. This can make an audible noise (like a crack/pop/scratch) when turning on/off your sound card. Sometimes even when move the slider volume, or open and close windows (KDE4). If you find this annoying try {{ic|modinfo snd_MY_MODULE}}, and look for a module option that adjusts or disables this feature.<br />
<br />
Example: to disable the power saving mode and solve cracking sound trough speakers issue, using snd_hda_intel add in {{ic|/etc/modprobe.d/modprobe.conf}}<br />
options snd_hda_intel power_save=0<br />
or<br />
options snd_hda_intel power_save=0 power_save_controller=N<br />
<br />
You can also try it with {{ic|1=modprobe snd_hda_intel power_save=0}} before.<br />
<br />
You may also have to unmute the 'Line' ALSA channel for this to work. Any value will do (other than '0' or something too high).<br />
<br />
''Example:'' on an onboard VIA VT1708S (using the snd_hda_intel module) these cracks occured even though 'power_save' was set to 0. Unmuting the 'Line' channel and setting a value of '1' solved the issue.<br />
<br />
Source: https://www.kernel.org/doc/Documentation/sound/alsa/powersave.txt<br />
<br />
If you use a laptop, pm-utils will change {{ic|power_save}} back to 1 when you go onto battery power even if you disable power saving in {{ic|/etc/modprobe.d}}. Disable this for pm-utils by disabling the script that makes the change (see [https://wiki.archlinux.org/index.php/Pm-utils#Disabling_a_hook Disabling a hook] for more information):<br />
# touch /etc/pm/power.d/intel-audio-powersave<br />
<br />
===S/PDIF Output Does Not Work===<br />
<br />
If the optical/coaxial digital output of your motherboard/sound card is not working or stopped working, and have already enabled and unmuted it in alsamixer, try running<br />
# iecset audio on<br />
<br />
as root.<br />
<br />
You can also put this command in {{Ic|rc.local}} as it sometimes it may stop working after a reboot.<br />
<br />
===HDMI Output Does Not Work===<br />
If the HDMI output of your motherboard/sound card is not working or stopped working, and have already enabled and unmuted it in alsamixer, try the following.<br />
<br />
Query for Playback Devices:<br />
<br />
$ aplay -l<br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: NVidia [HDA NVidia], device 0: ALC1200 Analog [ALC1200 Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 1: ALC1200 Digital [ALC1200 Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 3: NVIDIA HDMI [NVIDIA HDMI]<br />
Subdevices: 0/1<br />
Subdevice #0: subdevice #0<br />
<br />
Now that we have the info for the HDMI device, try a test, In the example below, 0 is the card number and 3 is the device number.<br />
<br />
$ aplay -D plughw:0,3 /usr/share/sounds/alsa/Front_Center.wav<br />
<br />
If aplay does not output any errors, but still no sound is heared, "reboot" the receiver, monitor or tv set. Since the HDMI interface executes a handshake on connection, it might have noticed before that there was no audio stream embedded, and disabled audio decoding. In particular, if you are using a standalone window manager (don’t know about Gnome or KDE), you may need to have some sound playing ''while'' plugging in the HDMI cable.<br />
<br />
{{Note|If you are using an ATI card and linux 3.0, check your kernel/grub settings, you most likely need to use the ''''radeon.audio'''<nowiki>=</nowiki>'''1'''' boot parameter, or alternatively set up the option in your modprobe configuration files.}}<br />
<br />
If the test is successful, create or edit your ~/.asoundrc file to set HDMI as the default audio device.<br />
<br />
{{hc|~/.asoundrc|<br />
pcm.!default {<br />
type hw<br />
card 0<br />
device 3<br />
}<br />
}}<br />
<br />
Or you above config does not work try:<br />
{{hc|~/.asoundrc|<br />
defaults.pcm.card 0<br />
defaults.pcm.device 3<br />
defaults.ctl.card 0<br />
}}<br />
<br />
===HDMI Multi-channel PCM output does not work (Intel) ===<br />
As of Linux 3.1 multi-channel PCM output through HDMI with a Intel card (Intel Eaglelake, IbexPeak/Ironlake,SandyBridge/CougarPoint and IvyBridge/PantherPoint) is not yet supported. Support for it has been recently added and expected to be available in Linux 3.2. To make it work in Linux 3.1 you need to apply the following patches:<br />
<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=76adaa34db407f174dd06370cb60f6029c33b465 drm: support routines for HDMI/DP ELD]<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=e0dac65ed45e72fe34cc7ccc76de0ba220bd38bb drm/i915: pass ELD to HDMI/DP audio driver]<br />
<br />
===HP TX2500===<br />
Add these 2 lines into {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-cmipci mpu_port=0x330 fm_port=0x388<br />
options snd-hda-intel index=0 model=toshiba position_fix=1<br />
<br />
options snd-hda-intel model=hp (works for tx2000cto)<br />
<br />
===Skipping Sound When Playing MP3===<br />
If you have sound skipping when playing MP3 files and you have more then 2 speakers attached to your computer (i.e. > 2 speaker system), run alsamixer and disable the channels for the speakers that you '''DO NOT''' have (i.e. do not enable the sound for the center speaker if you do not have a center speaker.<br />
<br />
===Using a USB Headset and External USB Sound Cards===<br />
If you are using a USB headset with ALSA you can try using {{AUR|asoundconf}} (currently only available from the [[AUR]]) to set the headset as the primary sound output. Before running make sure you have usb audio module enabled ({{ic|modprobe snd-usb-audio}}).<br />
<br />
# asoundconf is-active<br />
# asoundconf list<br />
# asoundconf set-default-card <chosen soundcard><br />
<br />
==== Crackling sound with USB devices ====<br />
If you experience crackling sound on USB devices, you can try tuning the snd-usb-audio for minimal latency.<br />
<br />
Add this to your {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
options snd-usb-audio nrpacks=1<br />
<br />
source: http://alsa.opensrc.org/Usb-audio#Tuning_USB_devices_for_minimal_latencies<br />
<br />
==== Hot-plugging a USB Sound Card ====<br />
In order to automatically make a USB Sound Card the primary output device, when the card is plugged in, you can use the following udev rules (e.g. add the following two lines to {{ic|/etc/udev/rules.d/00-local.rules}} and reboot).<br />
<br />
{{bc|1=KERNEL=="pcmC[D0-9cp]*", ACTION=="add", PROGRAM="/bin/sh -c 'K=%k; K=$${K#pcmC}; K=$${K%%D*}; echo defaults.ctl.card $$K > /etc/asound.conf; echo defaults.pcm.card $$K >>/etc/asound.conf'"<br />
KERNEL=="pcmC[D0-9cp]*", ACTION=="remove", PROGRAM="/bin/sh -c 'echo defaults.ctl.card 0 > /etc/asound.conf; echo defaults.pcm.card 0 >>/etc/asound.conf'"}}<br />
<br />
===Error 'Unknown hardware' Appears After a Kernel Update===<br />
The following messages may be displayed during the start-up ALSA after the kernel update:<br />
Unknown hardware "foo" "bar" ...<br />
Hardware is initialized using a guess method<br />
/usr/sbin/alsactl: set_control:nnnn:failed to obtain info for control #mm (No such file or directory)<br />
<br />
or<br />
Found hardware: "HDA-Intel" "VIA VT1705" "HDA:11064397,18490397,00100000" "0x1849" "0x0397"<br />
Hardware is initialized using a generic method<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #1 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #2 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #25 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #26 (No such file or directory)<br />
<br />
Simply store ALSA mixer settings again (as root):<br />
# alsactl -f /var/lib/alsa/asound.state store<br />
<br />
It may be necessary configure ALSA again with alsamixer<br />
<br />
===HDA Analyzer===<br />
If the mappings to your audio pins(plugs) do not correspond but ALSA works fine, you could try HDA Analyzer -- a pyGTK2 GUI for HD-audio control can be found [http://www.alsa-project.org/main/index.php/HDA_Analyzer at the ALSA wiki].<br />
Try tweaking the Widget Control section of the PIN nodes, to make microphones IN and headphone jacks OUT. Referring to the Config Defaults heading is a good idea.<br />
<br />
NOTE: the script is done by such way that it is incompatible with python3 (which is now shipped with ArchLinux) but tries to use it.<br />
The workaround is: open "run.py", find all occurences of "python" (2 occurences - one on the first line, and the second on the last line) and replace them all by "python2".<br />
<br />
NOTE2: the script requires root acces, but running it via su/sudo is bogus. Run it via {{ic|kdesu}} or {{ic|gksu}}.<br />
<br />
===ALSA with SDL===<br />
If you get no sound via SDL and ALSA cannot be chosen from the application. Try setting the environmental variable SDL_AUDIODRIVER to alsa.<br />
# export SDL_AUDIODRIVER=alsa<br />
<br />
===Low Sound Workaround===<br />
<br />
If you are facing low sound even after maxing out your speakers/headphones, you can give the softvol plugin a try. Add the following to {{ic|/etc/asound.conf}}.<br />
{{bc|<nowiki><br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
<br />
pcm.softvol {<br />
type softvol<br />
slave {<br />
pcm "dmix"<br />
}<br />
control {<br />
name "Pre-Amp"<br />
card 0<br />
}<br />
min_dB -5.0<br />
max_dB 20.0<br />
resolution 6<br />
}<br />
</nowiki>}}<br />
{{note| You will probably have to restart the computer, as restarting the alsa daemon did not load the new configuration for me. Also, if the configuration does not work even after restarting, try changing {{ic|plug}} with {{ic|hw}} in the above configuration.}}<br />
<br />
After the changes are loaded successfully, you will see a {{ic|Pre-Amp}} section in alsamixer. You can adjust the levels there.<br />
{{note|Setting a high value for {{ic|Pre-Amp}} can cause sound distortion, so adjust it according to the level that suits you.}}<br />
<br />
===Popping sound after resuming from suspension===<br />
You might hear a popping sound after resuming the computer from suspension. This can be fixed by editing {{ic|/etc/pm/sleep.d/90alsa}} and removing the line that says {{ic|aplay -d 1 /dev/zero}}<br />
<br />
==设置文件范例==<br />
参见:[[Advanced Linux Sound Architecture/Example Configurations]]。<br />
<br />
==相关阅读==<br />
* [http://www.mjmwired.net/kernel/Documentation/sound/alsa/ALSA-Configuration.txt ALSA 模块高级配置]<br />
* [http://alsa.opensrc.org/Main_Page 非官方的 ALSA Wiki]<br />
* [https://bbs.archlinux.org/viewtopic.php?id=36815 如何从 svn 获取驱动代码并编译]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Advanced_Linux_Sound_Architecture_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=250747
Advanced Linux Sound Architecture (简体中文)
2013-03-15T07:02:57Z
<p>Cuihao: /* 疑难解答 */</p>
<hr />
<div>[[Category:简体中文]]<br />
[[Category:Sound (简体中文)]]<br />
[[Category:Audio/Video (简体中文)]]<br />
[[cs:Advanced Linux Sound Architecture]]<br />
[[de:Alsa]]<br />
[[es:Advanced Linux Sound Architecture]]<br />
[[fa:ALSA]]<br />
[[fr:Alsa]]<br />
[[he:Advanced Linux Sound Architecture]]<br />
[[it:Advanced Linux Sound Architecture]]<br />
[[ja:Advanced Linux Sound Architecture]]<br />
[[nl:Advanced Linux Sound Architecture]]<br />
[[pt:Advanced Linux Sound Architecture]]<br />
[[ru:Advanced Linux Sound Architecture]]<br />
[[sk:Advanced Linux Sound Architecture]]<br />
[[th:Advanced Linux Sound Architecture]]<br />
[[tr:Gelişmiş_ses_mimarisi_(ALSA)]]<br />
[[en:Advanced Linux Sound Architecture]]<br />
<br />
{{Article summary start}}<br />
{{Article summary text|ALSA 的使用、配置及疑难解答。}}<br />
{{Article summary heading|相关页面}}<br />
{{Article summary wiki|Sound (简体中文)}}<br />
{{Article summary wiki|Disable PC Speaker Beep (简体中文)}}<br />
{{Article summary end}}<br />
<br />
{{TranslationStatus (简体中文)|Systemd|2013-03-14|250172}}<br />
<br />
[[Wikipedia:zh:ALSA|高级 Linux 声音体系]](Advanced Linux Sound Architecture,'''ALSA''')是Linux中提供声音设备驱动的内核组件,用来代替原来的开放声音系统(Open Sound System,OSSv3)。除了声音设备驱动,'''ALSA'''还包含一个用户空间的函数库,以方便开发者通过高级API使用驱动功能,而不必直接与内核驱动交互。<br />
<br />
{{注意|关于另一种声音体系,请阅读[[Open Sound System (简体中文)|开放声音系统(OSS)]]的页面。}}<br />
<br />
==安装==<br />
Arch 默认的内核已经通过一套模块提供了 ALSA,不必特别安装。<br />
<br />
[[udev (简体中文)|udev]]会在系统启动时自动检测硬件,并加载相应的声音设备驱动模块。这时,你的声卡已经可以工作了,只是所有声道默认都被设置成静音了。<br />
<br />
在本地登录(通过虚拟终端或登录管理器)的用户,都有权限播放音频并调整音量。要让远程登录的用户拥有这些权限,必须把用户[[Users and Groups (简体中文)#用户组管理|加入]] {{ic|audio}} 用户组。该组的成员可以直接访问声音设备,会导致某些程序独占音频输出(破坏软件混音),还可能影响用户快速切换和拖机(multiseat)。因此,除非真的有某些[https://wiki.ubuntu.com/Audio/TheAudioGroup 特殊需求],'''不建议'''把用户加入 {{ic|audio}} 用户组。<br />
<br />
===用户空间工具===<br />
从 [[Official Repositories (简体中文)|官方仓库]]:<br />
* [[pacman (简体中文)|安装]] {{Pkg|alsa-utils}} 软件包,其中的 {{ic|alsamixer}} 工具允许用户在控制台或终端中配置声音设备。<br />
* 安装 {{Pkg|alsa-oss}} 软件包,如果想让 OSS 应用程序与dmix(软件混音)协同工作。 <br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。}}<br />
*安装 {{Pkg|alsa-plugins}} 软件包,如果需要 [[#高质量重采样|高质量重采样]]、[[#Upmixing/Downmixing|软件模拟环绕立体声]]和其他高级特性。<br />
<br />
==解除各声道的静音==<br />
目前版本的 ALSA 安装后,所有声道'''默认是静音的''',必须手动解除。<br />
<br />
使用 {{ic|alsamixer}} 的 ncurses 界面,配置十分简单:<br />
<br />
$ alsamixer<br />
<br />
此外,还可以在命令行下使用 {{ic|amixer}}:<br />
<br />
$ amixer sset Master unmute<br />
<br />
下方标有 {{ic|MM}} 的声道是静音的,而标有 {{ic|00}} 的通道已经启用。<br />
<br />
使用左右方向键,选中 {{ic|Master}} 和 {{ic|PCM}} 声道。按下 {{keypress|m}} 键解除静音。使用上方向键增加音量,直到增益值为{{ic|0}}。该值显示在左上方 {{ic|Item:}} 字段后。过高的增益值会导致声音失真。<br />
<br />
要想得到完整的 5.1 或 7.1 环绕立体声,还得解除 Front、Surround、Center、LFE (subwoofer) 和 Side 这些声道的静音(上述名称是 Intel HD Audio 声卡使用的声道名,可能因设备不同而有所差异)。注意,仅有这些设置,系统不会自动将立体声源(多数音乐)提升(upmix)成环绕立体声。如果需要这些功能,请阅读[[#Upmixing/Downmixing]]。<br />
<br />
按下 {{Keypress|Esc}} 键退出 alsamixer。<br />
<br />
{{注意|<br />
* 有一些声卡,需要关闭数字输出或将其调成静音,才能输出模拟音频信号。对于 Soundblaster Audigy LS 声卡,需要把 IEC958 通道设成静音。 <br />
* 有些机器(如 Thinkpad T61),有一个 Speaker 通道,必须按照上述方法打开调整。<br />
* 有些机器(如 Dell E6400),可能还需要把 {{ic|Front}} 和 {{ic|Headphone}} 声道打开调整。}}<br />
<br />
接下来,测试声卡是否工作:<br />
<br />
$ speaker-test -c 2<br />
<br />
根据扬声器的实际情况,调整 -c 后的数值。对于 7.1 声道,这个数字是 8:<br />
<br />
$ speaker-test -c 8<br />
<br />
如果没有正常工作,请继续阅读 [[#配置]] 以及 [[#疑难解答]] 部分。<br />
<br />
{{pkg|alsa-utils}} 软件包提供了两个配置 alsa 的服务:{{ic|alsa-restore.service}} 和 {{ic|alsa-store.service}}。它们分别在开机和关机时自动运行。<br />
<br />
==配置==<br />
=== VirtualBox 虚拟机无声音 ===<br />
<br />
如果使用 VirtualBox 时碰上了麻烦,请尝试以下命令:<br />
<br />
{{hc|$ alsactl init|2=<nowiki><br />
<br />
Found hardware: "ICH" "SigmaTel STAC9700,83,84" "AC97a:83847600" "0x8086" "0x0000"<br />
Hardware is initialized using a generic method<br />
</nowiki><br />
}}<br />
<br />
如果还是不行,请检查所使用的音频软件是否激活了 ALSA 输出。<br />
<br />
===设置默认声卡===<br />
<br />
如果发现开机时声卡次序会发生变化,可以在通过 {{ic|/etc/modprobe.d}} 中的 {{ic|.conf}} 文件(比如 {{ic|/etc/modprobe.d/alsa-base.conf}})手动设置次序。<br />
比如,要让 mia 声卡成为 #0、Intel HDA 声卡成为 #1:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<nowiki><br />
options snd slots=snd_mia,snd_hda_intel<br />
options snd_mia index=0<br />
options snd_hda_intel index=1<br />
</nowiki>}}<br />
<br />
snd_mia 和 snd_hda_intel 分别是两个声卡的内核驱动模块。该配置仅适用于同时装有 mia 声卡和使用 snd_hda_intel 驱动的声卡(通常是板载声卡)的情况。<br />
<br />
如果使用 -2 的 index 值,ALSA 就不会将对应的设备作为主声卡使用。Linux Mint 和 Ubuntu 等发行版使用了以下配置,避免 USB 声卡和其他“非主流”设备变成 index 为 0 的主声卡:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<nowiki><br />
options bt87x index=-2<br />
options cx88_alsa index=-2<br />
options saa7134-alsa index=-2<br />
options snd-atiixp-modem index=-2<br />
options snd-intel8x0m index=-2<br />
options snd-via82xx-modem index=-2<br />
options snd-usb-audio index=-2<br />
options snd-usb-caiaq index=-2<br />
options snd-usb-ua101 index=-2<br />
options snd-usb-us122l index=-2<br />
options snd-usb-usx2y index=-2<br />
# Keep snd-pcsp from being loaded as first soundcard<br />
options snd-pcsp index=-2<br />
# Keep snd-usb-audio from beeing loaded as first soundcard<br />
options snd-usb-audio index=-2<br />
</nowiki>}}<br />
<br />
以上配置会在系统重启后生效。<br />
<br />
{{Out of date|据英文 Wiki 用户反应,{{ic|/etc/asound.conf}} 这个配置文件默认是不存在的,下列操作可能有误。}}<br />
<br />
首先,运行 {{ic|aplay -l}},获取声卡的声卡ID和设备ID:<br />
<br />
{{hc|$ aplay -l|2=<nowiki><br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: Intel [HDA Intel], device 0: CONEXANT Analog [CONEXANT Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: Intel [HDA Intel], device 1: Conexant Digital [Conexant Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 1: JamLab [JamLab], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 2: Audio [Altec Lansing XT1 - USB Audio], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
</nowiki>}}<br />
<br />
拿输出中的最后一项来说,这个声卡的声卡ID为2,设备ID为0。若要把这块声卡作为默认声卡,可以把下列配置添加到系统级别的 {{ic|/etc/asound.conf}} 或用户级别的 {{ic|~/.asoundrc}} 文件。如果文件不存在,可以手动创建。其中的各个ID,请根据实际情况调整:<br />
{{hc|~/.asoundrc|2=<nowiki><br />
defaults.pcm.card 2<br />
defaults.pcm.device 0<br />
defaults.ctl.card 2<br />
</nowiki>}}<br />
“pcm”选项决定用来播放音频的设备,而“ctl”选项决定那个声卡能够由控制工具(如 alsamixer)使用。<br />
<br />
上述配置在重启音频程序(如 mplayer)后立即生效。<br />
<br />
===确认所有声音模块都已经加载===<br />
<br />
一般 udev 都会自动识别出声卡。使用以下命令确认:<br />
<br />
{{hc|<nowiki>$ lsmod | grep '^snd' | column -t</nowiki>|<nowiki><br />
snd_hda_codec_hdmi 22378 4<br />
snd_hda_codec_realtek 294191 1<br />
snd_hda_intel 21738 1<br />
snd_hda_codec 73739 3 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel<br />
snd_hwdep 6134 1 snd_hda_codec<br />
snd_pcm 71032 3 snd_hda_codec_hdmi,snd_hda_intel,snd_hda_codec<br />
snd_timer 18992 1 snd_pcm<br />
snd 55132 9 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel,snd_hda_codec,snd_hwdep,snd_pcm,snd_timer<br />
snd_page_alloc 7017 2 snd_hda_intel,snd_pcm<br />
</nowiki>}}<br />
<br />
如果你的输出和上面类似,那就说明声卡已经被正确识别。<br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。如有需要,请[[Kernel_modules_(简体中文)#装入|手动加载]]。}}<br />
<br />
还可以检查一下 {{ic|/dev/snd/}} 目录,看看是否有这些设备文件:<br />
<br />
{{hc|$ ls -l /dev/snd|2=<nowiki><br />
total 0<br />
crw-rw---- 1 root audio 116, 0 Apr 8 14:17 controlC0<br />
crw-rw---- 1 root audio 116, 32 Apr 8 14:17 controlC1<br />
crw-rw---- 1 root audio 116, 24 Apr 8 14:17 pcmC0D0c<br />
crw-rw---- 1 root audio 116, 16 Apr 8 14:17 pcmC0D0p<br />
crw-rw---- 1 root audio 116, 25 Apr 8 14:17 pcmC0D1c<br />
crw-rw---- 1 root audio 116, 56 Apr 8 14:17 pcmC1D0c<br />
crw-rw---- 1 root audio 116, 48 Apr 8 14:17 pcmC1D0p<br />
crw-rw---- 1 root audio 116, 1 Apr 8 14:17 seq<br />
crw-rw---- 1 root audio 116, 33 Apr 8 14:17 timer<br />
</nowiki>}}<br />
<br />
{{注意|在 IRC 或论坛寻求这方面帮助时,别忘了贴出上面几个命令的输出。}}<br />
<br />
如果你的输出跟上面类似,或至少有 '''controlC0''' 和 '''pcmC0D0p''',那么声卡就已经正常加载了。<br />
<br />
如果出现问题,声卡模块没有正确加载,那么请尝试手动加载模块:<br />
<br />
* 确定声卡对应的驱动模块:[http://www.alsa-project.org/main/index.php/Matrix:Main ALSA Soundcard Matrix]。这些模块都会有一个”snd-“前缀(例如:{{ic|snd-via82xx}})。<br />
* [[Kernel_modules_(简体中文)#装入|加载模块]]。<br />
* 检查 {{ic|/dev/snd}} 目录中的设备文件(参见上文);或者,检查 {{ic|alsamixer}} 或 {{ic|amixer}} 的输出是否正确。<br />
* 修改配置,使 {{ic|snd-<模块名>}} 和 {{ic|snd-pcm-oss}} 模块[[Kernel_modules_(简体中文)#装入|开机自动加载]]。<br />
<br />
===启用 SPDIF 输出===<br />
<br />
(由 Gentoo 论坛的 gralves 提供)<br />
* 在 GNOME 音量控制的选项(Options)标签中,把 IEC958 改成 PCM。该选项可以在首选项中启用。<br />
* 不使用 GNOME 音量控制工具的话:<br />
** 编辑 {{ic|/var/lib/alsa/asound.state}}。该文件是 alsasound 保存混音器设置的地方。<br />
** 找到这一行:“IEC958 Playback Switch”。该行上下应该有一行内容为 {{ic|value:false}},将其改为 {{ic|value:true}}。<br />
** 再找到这一行:”IEC958 Playback AC97-SPSA“。把对应的值改为 0。<br />
** 重启 ALSA。<br />
<br />
{{Out of date|rc.local 在 systemd 中已经弃用,以下方法可能不再适用。}}<br />
<br />
还有另外一个开机自动启用 SPDIF 输出的方法(在 SoundBlaster Audigy 声卡上测试过):<br />
<br />
* 在 {{ic|/etc/rc.local}} 中添加如下内容:<br />
# Use COAX-digital output<br />
amixer set 'IEC958 Optical' 100 unmute<br />
amixer set 'Audigy Analog/Digital Output Jack' on<br />
<br />
使用以下命令,获取你的声卡的数字输出的名称:<br />
$ amixer scontrols<br />
<br />
===系统级均衡器===<br />
====使用 AlsaEqual(包含界面)====<br />
从 [[AUR (简体中文)|AUR]] 安装 {{AUR|alsaequal}}。<br />
<br />
{{注意|如果使用的是64位系统,而又安装了32位的 Flash 插件,这里的设置会导致 Flash 无声。如果发生了这种情况,请禁用 alsaequal 或编译32位版本的 alsaequal。}}<br />
<br />
安装后,把下列内容添加到 ALSA 配置文件({{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}}):<br />
<br />
ctl.equal {<br />
type equal;<br />
}<br />
<br />
pcm.plugequal {<br />
type equal;<br />
# Modify the line below if you do not<br />
#slave.pcm "plughw:0,0";<br />
#by default we want to play from more sources at time:<br />
slave.pcm "plug:dmix";<br />
}<br />
#pcm.equal {<br />
# If you do not want the equalizer to be your<br />
# default soundcard comment the following<br />
# line and uncomment the above line. (You can<br />
# choose it as the output device by addressing<br />
# it with specific apps,eg mpg123 -a equal 06.Back_In_Black.mp3)<br />
pcm.!default {<br />
type plug;<br />
slave.pcm plugequal;<br />
}<br />
<br />
设置好后,切换均衡器:<br />
$ alsamixer -D equal<br />
<br />
需要注意,每个用户的 alsaequal 配置文件都是独立的,位于 {{ic|~/.alsaequal.bin}}。因此,如果想在 [[mpd]] 之类的以独立用户身份运行的程序中使用 AlsaEqual,就得像下面这样:<br />
# su mpd -c 'alsamixer -D equal'<br />
或者,在其他用户的主目录下创建符号链接,指向你的 {{ic|.alsaequal.bin}}……<br />
<br />
=====管理 AlsaEqual 配置=====<br />
从 [http://xyne.archlinux.ca/repos/ Xyne 的软件仓库] 或 [https://aur.archlinux.org/packages.php?ID=62420 AUR] 安装 [http://xyne.archlinux.ca/projects/alsaequal-mgr/ alsaequal-mgr]。<br />
<br />
同上,配置均衡器: <br />
$alsamixer -D equal<br />
<br />
如果对效果感到满意,将其命名(如“foo”)并保存该配置:<br />
$alsaequal-mgr save foo<br />
<br />
重新加载“foo”中的配置:<br />
$alsaequal-mgr load foo<br />
<br />
这样,就可以为游戏、电影、不同音乐流派、网络电话等等创建不同的均衡器配置,以便随时切换。<br />
<br />
参见[http://xyne.archlinux.ca/projects/alsaequal-mgr/ 项目主页]和程序的帮助信息,了解更多设置。<br />
<br />
====使用 mbeq====<br />
{{注意|该软件用到了一个 ladspa 插件,可能导致播放音频时 CPU 占用率偏高。In addition, this was made with stereophonic sound (e.g. headphones) in mind.}}<br />
<br />
安装 {{Pkg|alsa-plugins}}、{{Pkg|ladspa}} 和 {{Pkg|swh-plugins}} 软件包。<br />
<br />
* 在 {{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}} 中添加如下内容:<br />
<br />
{{hc|/etc/asound.conf|2=<nowiki>pcm.eq {<br />
type ladspa<br />
<br />
# The output from the EQ can either go direct to a hardware device<br />
# (if you have a hardware mixer, e.g. SBLive/Audigy) or it can go<br />
# to the software mixer shown here.<br />
#slave.pcm "plughw:0,0"<br />
slave.pcm "plug:dmix"<br />
<br />
# Sometimes you may need to specify the path to the plugins,<br />
# especially if you have just installed them. Once you have logged<br />
# out/restarted this should not be necessary, but if you get errors<br />
# about being unable to find plugins, try uncommenting this.<br />
#path "/usr/lib/ladspa"<br />
<br />
plugins [<br />
{<br />
label mbeq<br />
id 1197<br />
input {<br />
#this setting is here by example, edit to your own taste<br />
#bands: 50hz, 100hz, 156hz, 220hz, 311hz, 440hz, 622hz, 880hz, 1250hz, 1750hz, 25000hz,<br />
#50000hz, 10000hz, 20000hz<br />
controls [ -5 -5 -5 -5 -5 -10 -20 -15 -10 -10 -10 -10 -10 -3 -2 ]<br />
}<br />
}<br />
]<br />
}<br />
<br />
# Redirect the default device to go via the EQ - you may want to do<br />
# this last, once you are sure everything is working. Otherwise all<br />
# your audio programs will break/crash if something has gone wrong.<br />
<br />
pcm.!default {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
<br />
# Redirect the OSS emulation through the EQ too (when programs are running through "aoss")<br />
<br />
pcm.dsp0 {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
</nowiki>}}<br />
<br />
* 完成。若有问题,欢迎前往论坛咨询。<br />
<br />
==高质量重采样==<br />
启用软件混音时,ALSA 会强制把所有音频重采样到相同的频率(如果系统支持,默认是 48000)。dmix 的重采样算法很糟糕,会导致显著的质量损失。<br />
<br />
安装 {{pkg|alsa-plugins}} 和 {{pkg|libsamplerate}}。<br />
<br />
将默认的采样率转换器更改为 libsamplerate:<br />
<br />
{{hc|/etc/asound.conf|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
或:<br />
<br />
{{hc|~/.asoundrc|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
'''samplerate_best''' 输出的声音质量最佳,但需要耗费大量CPU资源进行实时重采样。还有其他的算法可供选择(如 '''samplerate''' 等),但比起默认的算法,这些算法的改进并不明显。<br />
<br />
{{警告|某些系统中,启用 samplerate_best 可能导致 Flash 播放器无声音。}}<br />
<br />
==Upmixing/Downmixing==<br />
===Upmixing===<br />
In order for stereo sources like music to be able to saturate a 5.1 or 7.1 sound system, you need to use upmixing. In darker days this used to be tricky and error prone but nowadays plugins exist to easily take care of this task. Thus, install {{Pkg|alsa-plugins}}.<br />
<br />
Then add the following to your ALSA configuration file of choice (either {{ic|/etc/asound.conf}} or {{ic|~/.asoundrc}}):<br />
pcm.upmix71 {<br />
type upmix<br />
slave.pcm "surround71"<br />
delay 15<br />
channels 8<br />
}<br />
<br />
You can easily change this example for 7.1 upmixing to 5.1 or 4.0.<br />
<br />
This adds a new pcm that you can use for upmixing. If you want all sound sources to go through this pcm, add it as a default below the previous definition like so:<br />
<br />
pcm.!default "plug:upmix71"<br />
<br />
The plugin automatically allows multiple sources to play through it without problems so setting is as a default is actually a safe choice. <br />
If this is not working, you have to setup your own dmixer for the upmixing PCM like this: <br />
pcm.dmix6 {<br />
type asym<br />
playback.pcm {<br />
type dmix<br />
ipc_key 567829<br />
slave {<br />
pcm "hw:0,0"<br />
channels 6<br />
}<br />
}<br />
}<br />
<br />
and use "dmix6" instead of "surround71". <br />
If you experience skipping or distorted sound, consider increasing the buffer_size (to 32768, for example) or use a [[#高质量重采样|high quality resampler]].<br />
<br />
===Downmixing===<br />
If you want to downmix sources to stereo because you, for instance, want to watch a movie with 5.1 sound on a stereo system, you need to use the vdownmix plugin that alsa provides in the same package.<br />
<br />
Again, in your configuration file, add this:<br />
<br />
pcm.!surround51 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
pcm.!surround40 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
<br />
==混音==<br />
===软件混音(dmix)===<br />
<br />
{{注意|在 ALSA 1.0.9rc2 及以上版本中,如果声卡不支持硬件混音,模拟输出将默认使用 dmix 混音,无需额外设置。}}<br />
<br />
如果软件混音未正常启用,那么可以手动创建 {{ic|~/.asoundrc}},加入以下内容:<br />
<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
<br />
这样就启用了软件混音,多个程序能够同时使用声卡。<br />
<br />
至于 S/PDIF 之类的数字音频输出,目前的 ALSA 还不会默认启用 dmix。因此,必须像上面这样手动启用。<br />
<br />
常见问题的解答,参见[[#疑难解答]]部分。<br />
<br />
===硬件混音===<br />
====支持====<br />
如果声卡支持,一般无需任何配置即可启用硬件混音。绝大多数板载声卡都不支持硬件混音,必须得使用软件混音(见上文)。很多独立声卡都支持硬件混音,Linux 下支持较好的有:<br />
<br />
* Creative SoundBlaster Live! (5.1 型号)<br />
* Creative SoundBlaster Audigy (部分型号)<br />
* Creative SoundBlaster Audidy 2 (ZS 系列型号)<br />
* Creative SoundBlaster Audigy 4 (Pro 系列型号)<br />
<br />
{{注意|上述声卡的低端版本(Audigy SE、Audigy 2 NX、SoundBlaster Live! 24bit 以及 SoundBlaster Live! 7.1)使用了不同的芯片,'''不支持'''硬件混音。}}<br />
<br />
{{注意|板载 VIA8237 芯片支持4路硬件混音,但在某些主板上只能使用3路(第4路无声),甚至根本无法使用。即使能正常工作,质量也比不上其他混音方法。}}<br />
<br />
====问题修复====<br />
如果你的声卡是 Intel Corporation 82801I (ICH9 Family) HD Audio Controller (rev 02),且使用了64位系统,需要执行以下命令才能在 Enemy Territory 中听到声音:<br />
echo "et.x86 0 0 direct" > /proc/asound/card0/pcm0p/oss<br />
echo "et.x86 0 0 disable" > /proc/asound/card0/pcm0c/oss<br />
<br />
==疑难解答==<br />
===使用CPU频率动态调整时,音频发生跳跃===<br />
使用 {{ic|ondemand}} 或 {{ic|conservative}} 频率策略、启用CPU频率动态调整时,某些声卡和某些 ALSA 驱动会出现声音跳跃的情况。目前,唯一的解决方案是使用固定的频率,如 {{ic|performance}} 性能策略。<br />
<br />
详情参见 [[CPU Frequency Scaling (简体中文)]]。<br />
<br />
===同时只能有一个用户播放音频===<br />
有时会出现一个用户独占软件混音器的情况。多数情况下这没什么影响,但在使用 [[mpd]] 这样以独立用户身份运行的程序时,就会出现问题。当 mpd 运行时,用户自己就无法播放其他音频了。一种解决方法是,以正在使用的用户身份运行 mpd。另一种方法是,在 ALSA 配置文件的 {{ic|pcm.dmixer}} 部分添加 {{ic|ipc_key_add_uid 0}} 禁止独占。以下是 {{ic|asound.conf}} 示例的片段,省略的部分无需修改:<br />
<br />
...<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
slave {<br />
...<br />
<br />
===无法同时播放多个音频===<br />
如果安装了 [[PulseAudio (简体中文)|PulseAudio]](可能作为 [[GNOME (简体中文)|GNOME]] 的依赖被安装),可能出现无法同步播放的问题,因为 PulseAudio 的默认配置是“独占”声卡。ALSA 的用户可能对自己的配置已经很满意、不希望使用 [[PulseAudio (简体中文)|PulseAudio]]。解决方案是编辑 {{ic|/etc/asound.conf}} 并注释掉以下内容:<br />
# Use PulseAudio by default<br />
#pcm.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
# hint {<br />
# show on<br />
# description "Default ALSA Output (currently PulseAudio Sound Server)"<br />
# }<br />
#}<br />
<br />
可能还需要注释掉下面的内容:<br />
#ctl.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
#}<br />
<br />
比起完全卸载 [[PulseAudio (简体中文)|PulseAudio]],这大概是个更简单的方法。<br />
<br />
以下是 {{ic|/etc/asound.conf}} 配置文件的一个示例:<br />
{{bc|<nowiki>pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
}<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}</nowiki><br />
}}<br />
<br />
{{注意|本 {{ic|/etc/asound.conf}} 本来是配合 [[MPD]] 全局配置使用的。参见[[#同时只能有一个用户播放音频|该节]]关于多用户的内容。}}<br />
<br />
===Random Lack of Sound on Startup===<br />
If you randomly have no sound on startup, it may be because your system has multiple sound cards, and their order may sometimes change on startup. If this is the case, try [[#Set the default sound card|setting the default sound card]].<br />
<br />
If you use mpd and the configuration tips above do not work for you, try [http://mpd.wikia.com/wiki/Configuration#ALSA_MPD_software_volume_control reading this] instead.<br />
<br />
===Specific Program Problems===<br />
For other programs who insist on their own audio setup, eg, XMMS or Mplayer, you would need to set their specific options.<br />
<br />
For mplayer, open up {{ic|~/.mplayer/config}} (or {{ic|/etc/mplayer/mplayer.conf}} for global setting) and add the following line:<br />
ao=alsa<br />
<br />
For XMMS/Beep Media Player, go into their options and make sure the sound driver is set to Alsa, not oss.<br />
<br />
To do this in XMMS:<br />
* Open XMMS<br />
** Options -> preferences.<br />
** Choose the Alsa output plugin.<br />
<br />
For applications which do not provide a ALSA output, you can use aoss from the alsa-oss package. To use aoss, when you run the program, prefix it with {{ic|aoss}}, eg:<br />
aoss realplay<br />
<br />
pcm.!default{ ... } doesnt work for me anymore. but this does:<br />
pcm.default pcm.dmixer<br />
<br />
===Model Settings===<br />
Although Alsa detects your soundcard through the BIOS at times Alsa may not be able to recognize your [http://www.kernel.org/doc/Documentation/sound/alsa/HD-Audio-Models.txt model type]. The soundcard chip can be found in {{ic|alsamixer}} (e.g. ALC662) and the model can be set in {{ic|/etc/modprobe.d/modprobe.conf}} or {{ic|/etc/modprobe.d/sound.conf}}. For example:<br />
<br />
options snd-hda-intel model=MODEL<br />
<br />
There are other model settings too. For most cases Alsa defaults will do. If you want to look at more specific settings for your soundcard take a look at the [http://bugtrack.alsa-project.org/main/index.php/Matrix:Main Alsa Soundcard List] find your model, then Details, then look at the "Setting up modprobe..." section. Enter these values in {{ic|/etc/modprobe.d/modprobe.conf}}. For example, for an Intel AC97 audio:<br />
<br />
{{bc|# ALSA portion<br />
alias char-major-116 snd<br />
alias snd-card-0 snd-intel8x0<br />
# module options should go here<br />
<br />
# OSS/Free portion<br />
alias char-major-14 soundcore<br />
alias sound-slot-0 snd-card-0<br />
<br />
# card #1<br />
alias sound-service-0-0 snd-mixer-oss<br />
alias sound-service-0-1 snd-seq-oss<br />
alias sound-service-0-3 snd-pcm-oss<br />
alias sound-service-0-8 snd-seq-oss<br />
alias sound-service-0-12 snd-pcm-oss}}<br />
<br />
===Conflicting PC Speaker===<br />
If you are sure nothing is muted, that your drivers are installed correctly, and that your volume is right, but you still do not hear anything, then try adding the following line to {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
options snd-NAME-OF-MODULE ac97_quirk=0<br />
<br />
The above fix has been observed to work with {{ic|via82xx}}<br />
options snd-NAME-OF-MODULE ac97_quirk=1<br />
The above fix has been reported to work with {{ic|snd_intel8x0}}<br />
<br />
===No Microphone Input===<br />
In alsamixer, make sure that all the volume levels are up under recording, and that CAPTURE is toggled active on the microphone (e.g. Mic, Internal Mic) and/or on Capture (in alsamixer, select these items and press space). Try making positive Mic Boost and raising Capture and Digital levels higher; this make make static or distortion, but then you can adjust them back down once you are hearing ''something'' when you record<br />
<br />
As the pulseaudio wrapper is shown as "default" in alsamixer, you may have to press F6 to select your actual soundcard first. You may also need to enable and increase the volume of Line-in in the Playback section.<br />
<br />
To test the microphone, run these commands (see arecord's man page for further information):<br />
arecord -d 5 test-mic.wav<br />
aplay test-mic.wav<br />
<br />
If all fails, you may want to eliminate hardware failure by testing the microphone with a different device.<br />
<br />
For at least some computers, muting a microphone (MM) simply means its input does not go immediately to the speakers. It still receives input.<br />
<br />
Many Dell laptops need "-dmic" to be appended to the model name in {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-hda-intel model=dell-m6-dmic<br />
<br />
Some programs use try to use OSS as the main input software. Add the following lines to {{ic|/etc/modprobe.d/modprobe.conf}} to prevent OSS modules from being loaded:<br />
<br />
{{Note|The OSS modules are no longer autoloaded anyway.}}<br />
<br />
blacklist snd_pcm_oss<br />
blacklist snd_mixer_oss<br />
blacklist snd_seq_oss<br />
<br />
See also:<br />
* http://www.alsa-project.org/main/index.php/SoundcardTesting<br />
* http://alsa.opensrc.org/Record_from_mic<br />
<br />
===Setting the default Microphone/Capture Device===<br />
Some applications (Pidgin, Adobe Flash) do not provide an option to change the capture device. It becomes an issue if your microphone is on a separate device (e.g. USB webcam or microphone) than your internal sound card. To change only the default capture device, leaving the default playback device as is, you can modify your {{ic|~/.asoundrc}} file to include the following:<br />
<br />
pcm.usb<br />
{<br />
type hw<br />
card U0x46d0x81d<br />
}<br />
<br />
pcm.!default<br />
{<br />
type asym<br />
playback.pcm<br />
{<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
capture.pcm<br />
{<br />
type plug<br />
slave.pcm "usb"<br />
}<br />
}<br />
<br />
Replace "U0x46d0x81d" with your capture device's card name in ALSA. You can use {{ic|arecord -L}} to list all the capture devices detected by ALSA.<br />
<br />
===Internal Microphone not working===<br />
First make sure all the volume levels are up under recording in alsamixer. In my case adding the following option to /etc/sound.conf and reloading the snd-* module produced a new volume setting called Capture which was capturing for the internal mic. For eg, for snd-hda-intel add<br />
<br />
options snd-hda-intel enable_msi=1<br />
<br />
Then reload the module (as below), up the recording volume of Capture and then test.<br />
<br />
{{bc|# rmmod snd-hda-intel && modprobe snd-hda-intel}}<br />
<br />
===No Sound with Onboard Intel Sound Card===<br />
There may be an issue with two conflicting modules loaded, namely {{ic|snd_intel8x0}} and {{ic|snd_intel8x0m}}. In this case, blacklist snd_intel8x0m:<br />
<br />
{{hc|/etc/modprobe.d/modprobe.conf|blacklist snd_intel8x0m}}<br />
<br />
''Muting'' the "External Amplifier" in {{ic|alsamixer}} or {{ic|amixer}} may also help. See [http://alsa.opensrc.org/Intel8x0#Dell_Inspiron_8600_.28and_probably_others.29 the ALSA wiki].<br />
<br />
Unmuting the "Mix" setting in the mixer might help, also.<br />
<br />
===No Headphone Sound with Onboard Intel Sound Card===<br />
With '''Intel Corporation 82801 I (ICH9 Family) HD Audio Controller''' on laptop, you may need to add this line to modprobe or sound.conf:<br />
<br />
options snd-hda-intel model=$model<br />
<br />
Where $model is any one of the following (in order of possibility to work, but not merit):<br />
<br />
* dell-vostro<br />
* olpc-xo-1_5<br />
* laptop<br />
* dell-m6<br />
* laptop-hpsense<br />
<br />
Note: It may be necessary to put this "options" line below (after) any "alias" lines about your card.<br />
<br />
You can see all the available models in the kernel documentation. For example [http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.35.y.git;a=blob;f=Documentation/sound/alsa/HD-Audio-Models.txt;h=dc25bb84b83b49665a7ed850e7bf5423d50cd3ba;hb=HEAD here], but check that it is the correct version of that document for your kernel version.<br />
<br />
A list of available models is also available [http://www.mjmwired.net/kernel/Documentation/sound/alsa/HD-Audio-Models.txt here]. To know your chip name type the following command (with * being corrected to match your files). Note that some chips could have been renamed and do not directly match the available ones in the file.<br />
<br />
cat /proc/asound/card*/codec* | grep Codec<br />
<br />
Note that there is a high chance none of the input devices (all internal and external mics) will work if you choose to do this, so it is either your headphones or your mic. Please report to ALSA if you are affected by this bug.<br />
<br />
And also, if you have problems getting beeps to work (pcspkr):<br />
<br />
options snd-hda-intel model=$model enable=1 index=0<br />
<br />
===No sound when S/PDIF video card is installed===<br />
Discover available modules and their order:<br />
$ cat /proc/asound/modules<br />
0 snd_hda_intel<br />
1 snd_ca0106<br />
<br />
Disable the undesired video card audio codec in {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
# /etc/modprobe.d/modprobe.conf<br />
#<br />
install snd_hda_intel /bin/false<br />
<br />
If both devices use the same module, it might be possible to disable one of them in the BIOS.<br />
<br />
===Poor sound quality or clipping===<br />
If you experience poor sound quality, try setting the PCM volume (in alsamixer) to a level such that gain is 0.<br />
<br />
If snd-usb-audio driver has been loaded, you could try to enable {{ic|softvol}} in '''/etc/asound.conf''' file. Example configuration for the first audio device:<br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.dsnooper {<br />
type dsnoop<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
channels 2<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.softvol {<br />
type softvol<br />
slave { pcm "dmixer" }<br />
control {<br />
name "Master"<br />
card 0<br />
}<br />
}<br />
ctl.!default {<br />
type hw<br />
card 0<br />
}<br />
ctl.softvol {<br />
type hw<br />
card 0<br />
}<br />
ctl.dmixer {<br />
type hw<br />
card 0<br />
}<br />
<br />
===Pops When Starting and Stopping Playback===<br />
Some modules (e.g. snd_ac97_codec and snd_hda_intel) can power off your sound card when not in use. This can make an audible noise (like a crack/pop/scratch) when turning on/off your sound card. Sometimes even when move the slider volume, or open and close windows (KDE4). If you find this annoying try {{ic|modinfo snd_MY_MODULE}}, and look for a module option that adjusts or disables this feature.<br />
<br />
Example: to disable the power saving mode and solve cracking sound trough speakers issue, using snd_hda_intel add in {{ic|/etc/modprobe.d/modprobe.conf}}<br />
options snd_hda_intel power_save=0<br />
or<br />
options snd_hda_intel power_save=0 power_save_controller=N<br />
<br />
You can also try it with {{ic|1=modprobe snd_hda_intel power_save=0}} before.<br />
<br />
You may also have to unmute the 'Line' ALSA channel for this to work. Any value will do (other than '0' or something too high).<br />
<br />
''Example:'' on an onboard VIA VT1708S (using the snd_hda_intel module) these cracks occured even though 'power_save' was set to 0. Unmuting the 'Line' channel and setting a value of '1' solved the issue.<br />
<br />
Source: https://www.kernel.org/doc/Documentation/sound/alsa/powersave.txt<br />
<br />
If you use a laptop, pm-utils will change {{ic|power_save}} back to 1 when you go onto battery power even if you disable power saving in {{ic|/etc/modprobe.d}}. Disable this for pm-utils by disabling the script that makes the change (see [https://wiki.archlinux.org/index.php/Pm-utils#Disabling_a_hook Disabling a hook] for more information):<br />
# touch /etc/pm/power.d/intel-audio-powersave<br />
<br />
===S/PDIF Output Does Not Work===<br />
<br />
If the optical/coaxial digital output of your motherboard/sound card is not working or stopped working, and have already enabled and unmuted it in alsamixer, try running<br />
# iecset audio on<br />
<br />
as root.<br />
<br />
You can also put this command in {{Ic|rc.local}} as it sometimes it may stop working after a reboot.<br />
<br />
===HDMI Output Does Not Work===<br />
If the HDMI output of your motherboard/sound card is not working or stopped working, and have already enabled and unmuted it in alsamixer, try the following.<br />
<br />
Query for Playback Devices:<br />
<br />
$ aplay -l<br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: NVidia [HDA NVidia], device 0: ALC1200 Analog [ALC1200 Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 1: ALC1200 Digital [ALC1200 Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 3: NVIDIA HDMI [NVIDIA HDMI]<br />
Subdevices: 0/1<br />
Subdevice #0: subdevice #0<br />
<br />
Now that we have the info for the HDMI device, try a test, In the example below, 0 is the card number and 3 is the device number.<br />
<br />
$ aplay -D plughw:0,3 /usr/share/sounds/alsa/Front_Center.wav<br />
<br />
If aplay does not output any errors, but still no sound is heared, "reboot" the receiver, monitor or tv set. Since the HDMI interface executes a handshake on connection, it might have noticed before that there was no audio stream embedded, and disabled audio decoding. In particular, if you are using a standalone window manager (don’t know about Gnome or KDE), you may need to have some sound playing ''while'' plugging in the HDMI cable.<br />
<br />
{{Note|If you are using an ATI card and linux 3.0, check your kernel/grub settings, you most likely need to use the ''''radeon.audio'''<nowiki>=</nowiki>'''1'''' boot parameter, or alternatively set up the option in your modprobe configuration files.}}<br />
<br />
If the test is successful, create or edit your ~/.asoundrc file to set HDMI as the default audio device.<br />
<br />
{{hc|~/.asoundrc|<br />
pcm.!default {<br />
type hw<br />
card 0<br />
device 3<br />
}<br />
}}<br />
<br />
Or you above config does not work try:<br />
{{hc|~/.asoundrc|<br />
defaults.pcm.card 0<br />
defaults.pcm.device 3<br />
defaults.ctl.card 0<br />
}}<br />
<br />
===HDMI Multi-channel PCM output does not work (Intel) ===<br />
As of Linux 3.1 multi-channel PCM output through HDMI with a Intel card (Intel Eaglelake, IbexPeak/Ironlake,SandyBridge/CougarPoint and IvyBridge/PantherPoint) is not yet supported. Support for it has been recently added and expected to be available in Linux 3.2. To make it work in Linux 3.1 you need to apply the following patches:<br />
<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=76adaa34db407f174dd06370cb60f6029c33b465 drm: support routines for HDMI/DP ELD]<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=e0dac65ed45e72fe34cc7ccc76de0ba220bd38bb drm/i915: pass ELD to HDMI/DP audio driver]<br />
<br />
===HP TX2500===<br />
Add these 2 lines into {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-cmipci mpu_port=0x330 fm_port=0x388<br />
options snd-hda-intel index=0 model=toshiba position_fix=1<br />
<br />
options snd-hda-intel model=hp (works for tx2000cto)<br />
<br />
===Skipping Sound When Playing MP3===<br />
If you have sound skipping when playing MP3 files and you have more then 2 speakers attached to your computer (i.e. > 2 speaker system), run alsamixer and disable the channels for the speakers that you '''DO NOT''' have (i.e. do not enable the sound for the center speaker if you do not have a center speaker.<br />
<br />
===Using a USB Headset and External USB Sound Cards===<br />
If you are using a USB headset with ALSA you can try using {{AUR|asoundconf}} (currently only available from the [[AUR]]) to set the headset as the primary sound output. Before running make sure you have usb audio module enabled ({{ic|modprobe snd-usb-audio}}).<br />
<br />
# asoundconf is-active<br />
# asoundconf list<br />
# asoundconf set-default-card <chosen soundcard><br />
<br />
==== Crackling sound with USB devices ====<br />
If you experience crackling sound on USB devices, you can try tuning the snd-usb-audio for minimal latency.<br />
<br />
Add this to your {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
options snd-usb-audio nrpacks=1<br />
<br />
source: http://alsa.opensrc.org/Usb-audio#Tuning_USB_devices_for_minimal_latencies<br />
<br />
==== Hot-plugging a USB Sound Card ====<br />
In order to automatically make a USB Sound Card the primary output device, when the card is plugged in, you can use the following udev rules (e.g. add the following two lines to {{ic|/etc/udev/rules.d/00-local.rules}} and reboot).<br />
<br />
{{bc|1=KERNEL=="pcmC[D0-9cp]*", ACTION=="add", PROGRAM="/bin/sh -c 'K=%k; K=$${K#pcmC}; K=$${K%%D*}; echo defaults.ctl.card $$K > /etc/asound.conf; echo defaults.pcm.card $$K >>/etc/asound.conf'"<br />
KERNEL=="pcmC[D0-9cp]*", ACTION=="remove", PROGRAM="/bin/sh -c 'echo defaults.ctl.card 0 > /etc/asound.conf; echo defaults.pcm.card 0 >>/etc/asound.conf'"}}<br />
<br />
===Error 'Unknown hardware' Appears After a Kernel Update===<br />
The following messages may be displayed during the start-up ALSA after the kernel update:<br />
Unknown hardware "foo" "bar" ...<br />
Hardware is initialized using a guess method<br />
/usr/sbin/alsactl: set_control:nnnn:failed to obtain info for control #mm (No such file or directory)<br />
<br />
or<br />
Found hardware: "HDA-Intel" "VIA VT1705" "HDA:11064397,18490397,00100000" "0x1849" "0x0397"<br />
Hardware is initialized using a generic method<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #1 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #2 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #25 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #26 (No such file or directory)<br />
<br />
Simply store ALSA mixer settings again (as root):<br />
# alsactl -f /var/lib/alsa/asound.state store<br />
<br />
It may be necessary configure ALSA again with alsamixer<br />
<br />
===HDA Analyzer===<br />
If the mappings to your audio pins(plugs) do not correspond but ALSA works fine, you could try HDA Analyzer -- a pyGTK2 GUI for HD-audio control can be found [http://www.alsa-project.org/main/index.php/HDA_Analyzer at the ALSA wiki].<br />
Try tweaking the Widget Control section of the PIN nodes, to make microphones IN and headphone jacks OUT. Referring to the Config Defaults heading is a good idea.<br />
<br />
NOTE: the script is done by such way that it is incompatible with python3 (which is now shipped with ArchLinux) but tries to use it.<br />
The workaround is: open "run.py", find all occurences of "python" (2 occurences - one on the first line, and the second on the last line) and replace them all by "python2".<br />
<br />
NOTE2: the script requires root acces, but running it via su/sudo is bogus. Run it via {{ic|kdesu}} or {{ic|gksu}}.<br />
<br />
===ALSA with SDL===<br />
If you get no sound via SDL and ALSA cannot be chosen from the application. Try setting the environmental variable SDL_AUDIODRIVER to alsa.<br />
# export SDL_AUDIODRIVER=alsa<br />
<br />
===Low Sound Workaround===<br />
<br />
If you are facing low sound even after maxing out your speakers/headphones, you can give the softvol plugin a try. Add the following to {{ic|/etc/asound.conf}}.<br />
{{bc|<nowiki><br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
<br />
pcm.softvol {<br />
type softvol<br />
slave {<br />
pcm "dmix"<br />
}<br />
control {<br />
name "Pre-Amp"<br />
card 0<br />
}<br />
min_dB -5.0<br />
max_dB 20.0<br />
resolution 6<br />
}<br />
</nowiki>}}<br />
{{note| You will probably have to restart the computer, as restarting the alsa daemon did not load the new configuration for me. Also, if the configuration does not work even after restarting, try changing {{ic|plug}} with {{ic|hw}} in the above configuration.}}<br />
<br />
After the changes are loaded successfully, you will see a {{ic|Pre-Amp}} section in alsamixer. You can adjust the levels there.<br />
{{note|Setting a high value for {{ic|Pre-Amp}} can cause sound distortion, so adjust it according to the level that suits you.}}<br />
<br />
===Popping sound after resuming from suspension===<br />
You might hear a popping sound after resuming the computer from suspension. This can be fixed by editing {{ic|/etc/pm/sleep.d/90alsa}} and removing the line that says {{ic|aplay -d 1 /dev/zero}}<br />
<br />
==设置文件范例==<br />
参见:[[Advanced Linux Sound Architecture/Example Configurations]]。<br />
<br />
==相关阅读==<br />
* [http://www.mjmwired.net/kernel/Documentation/sound/alsa/ALSA-Configuration.txt ALSA 模块高级配置]<br />
* [http://alsa.opensrc.org/Main_Page 非官方的 ALSA Wiki]<br />
* [https://bbs.archlinux.org/viewtopic.php?id=36815 如何从 svn 获取驱动代码并编译]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Advanced_Linux_Sound_Architecture_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=250605
Advanced Linux Sound Architecture (简体中文)
2013-03-14T08:08:17Z
<p>Cuihao: /* 使用 AlsaEqual(包含界面) */</p>
<hr />
<div>[[Category:简体中文]]<br />
[[Category:Sound (简体中文)]]<br />
[[Category:Audio/Video (简体中文)]]<br />
[[cs:Advanced Linux Sound Architecture]]<br />
[[de:Alsa]]<br />
[[es:Advanced Linux Sound Architecture]]<br />
[[fa:ALSA]]<br />
[[fr:Alsa]]<br />
[[he:Advanced Linux Sound Architecture]]<br />
[[it:Advanced Linux Sound Architecture]]<br />
[[ja:Advanced Linux Sound Architecture]]<br />
[[nl:Advanced Linux Sound Architecture]]<br />
[[pt:Advanced Linux Sound Architecture]]<br />
[[ru:Advanced Linux Sound Architecture]]<br />
[[sk:Advanced Linux Sound Architecture]]<br />
[[th:Advanced Linux Sound Architecture]]<br />
[[tr:Gelişmiş_ses_mimarisi_(ALSA)]]<br />
[[en:Advanced Linux Sound Architecture]]<br />
<br />
{{Article summary start}}<br />
{{Article summary text|ALSA 的使用、配置及疑难解答。}}<br />
{{Article summary heading|相关页面}}<br />
{{Article summary wiki|Sound (简体中文)}}<br />
{{Article summary wiki|Disable PC Speaker Beep (简体中文)}}<br />
{{Article summary end}}<br />
<br />
{{TranslationStatus (简体中文)|Systemd|2013-03-14|250172}}<br />
<br />
[[Wikipedia:zh:ALSA|高级 Linux 声音体系]](Advanced Linux Sound Architecture,'''ALSA''')是Linux中提供声音设备驱动的内核组件,用来代替原来的开放声音系统(Open Sound System,OSSv3)。除了声音设备驱动,'''ALSA'''还包含一个用户空间的函数库,以方便开发者通过高级API使用驱动功能,而不必直接与内核驱动交互。<br />
<br />
{{注意|关于另一种声音体系,请阅读[[Open Sound System (简体中文)|开放声音系统(OSS)]]的页面。}}<br />
<br />
==安装==<br />
Arch 默认的内核已经通过一套模块提供了 ALSA,不必特别安装。<br />
<br />
[[udev (简体中文)|udev]]会在系统启动时自动检测硬件,并加载相应的声音设备驱动模块。这时,你的声卡已经可以工作了,只是所有声道默认都被设置成静音了。<br />
<br />
在本地登录(通过虚拟终端或登录管理器)的用户,都有权限播放音频并调整音量。要让远程登录的用户拥有这些权限,必须把用户[[Users and Groups (简体中文)#用户组管理|加入]] {{ic|audio}} 用户组。该组的成员可以直接访问声音设备,会导致某些程序独占音频输出(破坏软件混音),还可能影响用户快速切换和拖机(multiseat)。因此,除非真的有某些[https://wiki.ubuntu.com/Audio/TheAudioGroup 特殊需求],'''不建议'''把用户加入 {{ic|audio}} 用户组。<br />
<br />
===用户空间工具===<br />
从 [[Official Repositories (简体中文)|官方仓库]]:<br />
* [[pacman (简体中文)|安装]] {{Pkg|alsa-utils}} 软件包,其中的 {{ic|alsamixer}} 工具允许用户在控制台或终端中配置声音设备。<br />
* 安装 {{Pkg|alsa-oss}} 软件包,如果想让 OSS 应用程序与dmix(软件混音)协同工作。 <br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。}}<br />
*安装 {{Pkg|alsa-plugins}} 软件包,如果需要 [[#高质量重采样|高质量重采样]]、[[#Upmixing/Downmixing|软件模拟环绕立体声]]和其他高级特性。<br />
<br />
==解除各声道的静音==<br />
目前版本的 ALSA 安装后,所有声道'''默认是静音的''',必须手动解除。<br />
<br />
使用 {{ic|alsamixer}} 的 ncurses 界面,配置十分简单:<br />
<br />
$ alsamixer<br />
<br />
此外,还可以在命令行下使用 {{ic|amixer}}:<br />
<br />
$ amixer sset Master unmute<br />
<br />
下方标有 {{ic|MM}} 的声道是静音的,而标有 {{ic|00}} 的通道已经启用。<br />
<br />
使用左右方向键,选中 {{ic|Master}} 和 {{ic|PCM}} 声道。按下 {{keypress|m}} 键解除静音。使用上方向键增加音量,直到增益值为{{ic|0}}。该值显示在左上方 {{ic|Item:}} 字段后。过高的增益值会导致声音失真。<br />
<br />
要想得到完整的 5.1 或 7.1 环绕立体声,还得解除 Front、Surround、Center、LFE (subwoofer) 和 Side 这些声道的静音(上述名称是 Intel HD Audio 声卡使用的声道名,可能因设备不同而有所差异)。注意,仅有这些设置,系统不会自动将立体声源(多数音乐)提升(upmix)成环绕立体声。如果需要这些功能,请阅读[[#Upmixing/Downmixing]]。<br />
<br />
按下 {{Keypress|Esc}} 键退出 alsamixer。<br />
<br />
{{注意|<br />
* 有一些声卡,需要关闭数字输出或将其调成静音,才能输出模拟音频信号。对于 Soundblaster Audigy LS 声卡,需要把 IEC958 通道设成静音。 <br />
* 有些机器(如 Thinkpad T61),有一个 Speaker 通道,必须按照上述方法打开调整。<br />
* 有些机器(如 Dell E6400),可能还需要把 {{ic|Front}} 和 {{ic|Headphone}} 声道打开调整。}}<br />
<br />
接下来,测试声卡是否工作:<br />
<br />
$ speaker-test -c 2<br />
<br />
根据扬声器的实际情况,调整 -c 后的数值。对于 7.1 声道,这个数字是 8:<br />
<br />
$ speaker-test -c 8<br />
<br />
如果没有正常工作,请继续阅读 [[#配置]] 以及 [[#疑难解答]] 部分。<br />
<br />
{{pkg|alsa-utils}} 软件包提供了两个配置 alsa 的服务:{{ic|alsa-restore.service}} 和 {{ic|alsa-store.service}}。它们分别在开机和关机时自动运行。<br />
<br />
==配置==<br />
=== VirtualBox 虚拟机无声音 ===<br />
<br />
如果使用 VirtualBox 时碰上了麻烦,请尝试以下命令:<br />
<br />
{{hc|$ alsactl init|2=<nowiki><br />
<br />
Found hardware: "ICH" "SigmaTel STAC9700,83,84" "AC97a:83847600" "0x8086" "0x0000"<br />
Hardware is initialized using a generic method<br />
</nowiki><br />
}}<br />
<br />
如果还是不行,请检查所使用的音频软件是否激活了 ALSA 输出。<br />
<br />
===设置默认声卡===<br />
<br />
如果发现开机时声卡次序会发生变化,可以在通过 {{ic|/etc/modprobe.d}} 中的 {{ic|.conf}} 文件(比如 {{ic|/etc/modprobe.d/alsa-base.conf}})手动设置次序。<br />
比如,要让 mia 声卡成为 #0、Intel HDA 声卡成为 #1:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<nowiki><br />
options snd slots=snd_mia,snd_hda_intel<br />
options snd_mia index=0<br />
options snd_hda_intel index=1<br />
</nowiki>}}<br />
<br />
snd_mia 和 snd_hda_intel 分别是两个声卡的内核驱动模块。该配置仅适用于同时装有 mia 声卡和使用 snd_hda_intel 驱动的声卡(通常是板载声卡)的情况。<br />
<br />
如果使用 -2 的 index 值,ALSA 就不会将对应的设备作为主声卡使用。Linux Mint 和 Ubuntu 等发行版使用了以下配置,避免 USB 声卡和其他“非主流”设备变成 index 为 0 的主声卡:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<nowiki><br />
options bt87x index=-2<br />
options cx88_alsa index=-2<br />
options saa7134-alsa index=-2<br />
options snd-atiixp-modem index=-2<br />
options snd-intel8x0m index=-2<br />
options snd-via82xx-modem index=-2<br />
options snd-usb-audio index=-2<br />
options snd-usb-caiaq index=-2<br />
options snd-usb-ua101 index=-2<br />
options snd-usb-us122l index=-2<br />
options snd-usb-usx2y index=-2<br />
# Keep snd-pcsp from being loaded as first soundcard<br />
options snd-pcsp index=-2<br />
# Keep snd-usb-audio from beeing loaded as first soundcard<br />
options snd-usb-audio index=-2<br />
</nowiki>}}<br />
<br />
以上配置会在系统重启后生效。<br />
<br />
{{Out of date|据英文 Wiki 用户反应,{{ic|/etc/asound.conf}} 这个配置文件默认是不存在的,下列操作可能有误。}}<br />
<br />
首先,运行 {{ic|aplay -l}},获取声卡的声卡ID和设备ID:<br />
<br />
{{hc|$ aplay -l|2=<nowiki><br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: Intel [HDA Intel], device 0: CONEXANT Analog [CONEXANT Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: Intel [HDA Intel], device 1: Conexant Digital [Conexant Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 1: JamLab [JamLab], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 2: Audio [Altec Lansing XT1 - USB Audio], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
</nowiki>}}<br />
<br />
拿输出中的最后一项来说,这个声卡的声卡ID为2,设备ID为0。若要把这块声卡作为默认声卡,可以把下列配置添加到系统级别的 {{ic|/etc/asound.conf}} 或用户级别的 {{ic|~/.asoundrc}} 文件。如果文件不存在,可以手动创建。其中的各个ID,请根据实际情况调整:<br />
{{hc|~/.asoundrc|2=<nowiki><br />
defaults.pcm.card 2<br />
defaults.pcm.device 0<br />
defaults.ctl.card 2<br />
</nowiki>}}<br />
“pcm”选项决定用来播放音频的设备,而“ctl”选项决定那个声卡能够由控制工具(如 alsamixer)使用。<br />
<br />
上述配置在重启音频程序(如 mplayer)后立即生效。<br />
<br />
===确认所有声音模块都已经加载===<br />
<br />
一般 udev 都会自动识别出声卡。使用以下命令确认:<br />
<br />
{{hc|<nowiki>$ lsmod | grep '^snd' | column -t</nowiki>|<nowiki><br />
snd_hda_codec_hdmi 22378 4<br />
snd_hda_codec_realtek 294191 1<br />
snd_hda_intel 21738 1<br />
snd_hda_codec 73739 3 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel<br />
snd_hwdep 6134 1 snd_hda_codec<br />
snd_pcm 71032 3 snd_hda_codec_hdmi,snd_hda_intel,snd_hda_codec<br />
snd_timer 18992 1 snd_pcm<br />
snd 55132 9 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel,snd_hda_codec,snd_hwdep,snd_pcm,snd_timer<br />
snd_page_alloc 7017 2 snd_hda_intel,snd_pcm<br />
</nowiki>}}<br />
<br />
如果你的输出和上面类似,那就说明声卡已经被正确识别。<br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。如有需要,请[[Kernel_modules_(简体中文)#装入|手动加载]]。}}<br />
<br />
还可以检查一下 {{ic|/dev/snd/}} 目录,看看是否有这些设备文件:<br />
<br />
{{hc|$ ls -l /dev/snd|2=<nowiki><br />
total 0<br />
crw-rw---- 1 root audio 116, 0 Apr 8 14:17 controlC0<br />
crw-rw---- 1 root audio 116, 32 Apr 8 14:17 controlC1<br />
crw-rw---- 1 root audio 116, 24 Apr 8 14:17 pcmC0D0c<br />
crw-rw---- 1 root audio 116, 16 Apr 8 14:17 pcmC0D0p<br />
crw-rw---- 1 root audio 116, 25 Apr 8 14:17 pcmC0D1c<br />
crw-rw---- 1 root audio 116, 56 Apr 8 14:17 pcmC1D0c<br />
crw-rw---- 1 root audio 116, 48 Apr 8 14:17 pcmC1D0p<br />
crw-rw---- 1 root audio 116, 1 Apr 8 14:17 seq<br />
crw-rw---- 1 root audio 116, 33 Apr 8 14:17 timer<br />
</nowiki>}}<br />
<br />
{{注意|在 IRC 或论坛寻求这方面帮助时,别忘了贴出上面几个命令的输出。}}<br />
<br />
如果你的输出跟上面类似,或至少有 '''controlC0''' 和 '''pcmC0D0p''',那么声卡就已经正常加载了。<br />
<br />
如果出现问题,声卡模块没有正确加载,那么请尝试手动加载模块:<br />
<br />
* 确定声卡对应的驱动模块:[http://www.alsa-project.org/main/index.php/Matrix:Main ALSA Soundcard Matrix]。这些模块都会有一个”snd-“前缀(例如:{{ic|snd-via82xx}})。<br />
* [[Kernel_modules_(简体中文)#装入|加载模块]]。<br />
* 检查 {{ic|/dev/snd}} 目录中的设备文件(参见上文);或者,检查 {{ic|alsamixer}} 或 {{ic|amixer}} 的输出是否正确。<br />
* 修改配置,使 {{ic|snd-<模块名>}} 和 {{ic|snd-pcm-oss}} 模块[[Kernel_modules_(简体中文)#装入|开机自动加载]]。<br />
<br />
===启用 SPDIF 输出===<br />
<br />
(由 Gentoo 论坛的 gralves 提供)<br />
* 在 GNOME 音量控制的选项(Options)标签中,把 IEC958 改成 PCM。该选项可以在首选项中启用。<br />
* 不使用 GNOME 音量控制工具的话:<br />
** 编辑 {{ic|/var/lib/alsa/asound.state}}。该文件是 alsasound 保存混音器设置的地方。<br />
** 找到这一行:“IEC958 Playback Switch”。该行上下应该有一行内容为 {{ic|value:false}},将其改为 {{ic|value:true}}。<br />
** 再找到这一行:”IEC958 Playback AC97-SPSA“。把对应的值改为 0。<br />
** 重启 ALSA。<br />
<br />
{{Out of date|rc.local 在 systemd 中已经弃用,以下方法可能不再适用。}}<br />
<br />
还有另外一个开机自动启用 SPDIF 输出的方法(在 SoundBlaster Audigy 声卡上测试过):<br />
<br />
* 在 {{ic|/etc/rc.local}} 中添加如下内容:<br />
# Use COAX-digital output<br />
amixer set 'IEC958 Optical' 100 unmute<br />
amixer set 'Audigy Analog/Digital Output Jack' on<br />
<br />
使用以下命令,获取你的声卡的数字输出的名称:<br />
$ amixer scontrols<br />
<br />
===系统级均衡器===<br />
====使用 AlsaEqual(包含界面)====<br />
从 [[AUR (简体中文)|AUR]] 安装 {{AUR|alsaequal}}。<br />
<br />
{{注意|如果使用的是64位系统,而又安装了32位的 Flash 插件,这里的设置会导致 Flash 无声。如果发生了这种情况,请禁用 alsaequal 或编译32位版本的 alsaequal。}}<br />
<br />
安装后,把下列内容添加到 ALSA 配置文件({{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}}):<br />
<br />
ctl.equal {<br />
type equal;<br />
}<br />
<br />
pcm.plugequal {<br />
type equal;<br />
# Modify the line below if you do not<br />
#slave.pcm "plughw:0,0";<br />
#by default we want to play from more sources at time:<br />
slave.pcm "plug:dmix";<br />
}<br />
#pcm.equal {<br />
# If you do not want the equalizer to be your<br />
# default soundcard comment the following<br />
# line and uncomment the above line. (You can<br />
# choose it as the output device by addressing<br />
# it with specific apps,eg mpg123 -a equal 06.Back_In_Black.mp3)<br />
pcm.!default {<br />
type plug;<br />
slave.pcm plugequal;<br />
}<br />
<br />
设置好后,切换均衡器:<br />
$ alsamixer -D equal<br />
<br />
需要注意,每个用户的 alsaequal 配置文件都是独立的,位于 {{ic|~/.alsaequal.bin}}。因此,如果想在 [[mpd]] 之类的以独立用户身份运行的程序中使用 AlsaEqual,就得像下面这样:<br />
# su mpd -c 'alsamixer -D equal'<br />
或者,在其他用户的主目录下创建符号链接,指向你的 {{ic|.alsaequal.bin}}……<br />
<br />
=====管理 AlsaEqual 配置=====<br />
从 [http://xyne.archlinux.ca/repos/ Xyne 的软件仓库] 或 [https://aur.archlinux.org/packages.php?ID=62420 AUR] 安装 [http://xyne.archlinux.ca/projects/alsaequal-mgr/ alsaequal-mgr]。<br />
<br />
同上,配置均衡器: <br />
$alsamixer -D equal<br />
<br />
如果对效果感到满意,将其命名(如“foo”)并保存该配置:<br />
$alsaequal-mgr save foo<br />
<br />
重新加载“foo”中的配置:<br />
$alsaequal-mgr load foo<br />
<br />
这样,就可以为游戏、电影、不同音乐流派、网络电话等等创建不同的均衡器配置,以便随时切换。<br />
<br />
参见[http://xyne.archlinux.ca/projects/alsaequal-mgr/ 项目主页]和程序的帮助信息,了解更多设置。<br />
<br />
====使用 mbeq====<br />
{{注意|该软件用到了一个 ladspa 插件,可能导致播放音频时 CPU 占用率偏高。In addition, this was made with stereophonic sound (e.g. headphones) in mind.}}<br />
<br />
安装 {{Pkg|alsa-plugins}}、{{Pkg|ladspa}} 和 {{Pkg|swh-plugins}} 软件包。<br />
<br />
* 在 {{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}} 中添加如下内容:<br />
<br />
{{hc|/etc/asound.conf|2=<nowiki>pcm.eq {<br />
type ladspa<br />
<br />
# The output from the EQ can either go direct to a hardware device<br />
# (if you have a hardware mixer, e.g. SBLive/Audigy) or it can go<br />
# to the software mixer shown here.<br />
#slave.pcm "plughw:0,0"<br />
slave.pcm "plug:dmix"<br />
<br />
# Sometimes you may need to specify the path to the plugins,<br />
# especially if you have just installed them. Once you have logged<br />
# out/restarted this should not be necessary, but if you get errors<br />
# about being unable to find plugins, try uncommenting this.<br />
#path "/usr/lib/ladspa"<br />
<br />
plugins [<br />
{<br />
label mbeq<br />
id 1197<br />
input {<br />
#this setting is here by example, edit to your own taste<br />
#bands: 50hz, 100hz, 156hz, 220hz, 311hz, 440hz, 622hz, 880hz, 1250hz, 1750hz, 25000hz,<br />
#50000hz, 10000hz, 20000hz<br />
controls [ -5 -5 -5 -5 -5 -10 -20 -15 -10 -10 -10 -10 -10 -3 -2 ]<br />
}<br />
}<br />
]<br />
}<br />
<br />
# Redirect the default device to go via the EQ - you may want to do<br />
# this last, once you are sure everything is working. Otherwise all<br />
# your audio programs will break/crash if something has gone wrong.<br />
<br />
pcm.!default {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
<br />
# Redirect the OSS emulation through the EQ too (when programs are running through "aoss")<br />
<br />
pcm.dsp0 {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
</nowiki>}}<br />
<br />
* 完成。若有问题,欢迎前往论坛咨询。<br />
<br />
==高质量重采样==<br />
启用软件混音时,ALSA 会强制把所有音频重采样到相同的频率(如果系统支持,默认是 48000)。dmix 的重采样算法很糟糕,会导致显著的质量损失。<br />
<br />
安装 {{pkg|alsa-plugins}} 和 {{pkg|libsamplerate}}。<br />
<br />
将默认的采样率转换器更改为 libsamplerate:<br />
<br />
{{hc|/etc/asound.conf|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
或:<br />
<br />
{{hc|~/.asoundrc|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
'''samplerate_best''' 输出的声音质量最佳,但需要耗费大量CPU资源进行实时重采样。还有其他的算法可供选择(如 '''samplerate''' 等),但比起默认的算法,这些算法的改进并不明显。<br />
<br />
{{警告|某些系统中,启用 samplerate_best 可能导致 Flash 播放器无声音。}}<br />
<br />
==Upmixing/Downmixing==<br />
===Upmixing===<br />
In order for stereo sources like music to be able to saturate a 5.1 or 7.1 sound system, you need to use upmixing. In darker days this used to be tricky and error prone but nowadays plugins exist to easily take care of this task. Thus, install {{Pkg|alsa-plugins}}.<br />
<br />
Then add the following to your ALSA configuration file of choice (either {{ic|/etc/asound.conf}} or {{ic|~/.asoundrc}}):<br />
pcm.upmix71 {<br />
type upmix<br />
slave.pcm "surround71"<br />
delay 15<br />
channels 8<br />
}<br />
<br />
You can easily change this example for 7.1 upmixing to 5.1 or 4.0.<br />
<br />
This adds a new pcm that you can use for upmixing. If you want all sound sources to go through this pcm, add it as a default below the previous definition like so:<br />
<br />
pcm.!default "plug:upmix71"<br />
<br />
The plugin automatically allows multiple sources to play through it without problems so setting is as a default is actually a safe choice. <br />
If this is not working, you have to setup your own dmixer for the upmixing PCM like this: <br />
pcm.dmix6 {<br />
type asym<br />
playback.pcm {<br />
type dmix<br />
ipc_key 567829<br />
slave {<br />
pcm "hw:0,0"<br />
channels 6<br />
}<br />
}<br />
}<br />
<br />
and use "dmix6" instead of "surround71". <br />
If you experience skipping or distorted sound, consider increasing the buffer_size (to 32768, for example) or use a [[#高质量重采样|high quality resampler]].<br />
<br />
===Downmixing===<br />
If you want to downmix sources to stereo because you, for instance, want to watch a movie with 5.1 sound on a stereo system, you need to use the vdownmix plugin that alsa provides in the same package.<br />
<br />
Again, in your configuration file, add this:<br />
<br />
pcm.!surround51 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
pcm.!surround40 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
<br />
==混音==<br />
===软件混音(dmix)===<br />
<br />
{{注意|在 ALSA 1.0.9rc2 及以上版本中,如果声卡不支持硬件混音,模拟输出将默认使用 dmix 混音,无需额外设置。}}<br />
<br />
如果软件混音未正常启用,那么可以手动创建 {{ic|~/.asoundrc}},加入以下内容:<br />
<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
<br />
这样就启用了软件混音,多个程序能够同时使用声卡。<br />
<br />
至于 S/PDIF 之类的数字音频输出,目前的 ALSA 还不会默认启用 dmix。因此,必须像上面这样手动启用。<br />
<br />
常见问题的解答,参见[[#疑难解答]]部分。<br />
<br />
===硬件混音===<br />
====支持====<br />
如果声卡支持,一般无需任何配置即可启用硬件混音。绝大多数板载声卡都不支持硬件混音,必须得使用软件混音(见上文)。很多独立声卡都支持硬件混音,Linux 下支持较好的有:<br />
<br />
* Creative SoundBlaster Live! (5.1 型号)<br />
* Creative SoundBlaster Audigy (部分型号)<br />
* Creative SoundBlaster Audidy 2 (ZS 系列型号)<br />
* Creative SoundBlaster Audigy 4 (Pro 系列型号)<br />
<br />
{{注意|上述声卡的低端版本(Audigy SE、Audigy 2 NX、SoundBlaster Live! 24bit 以及 SoundBlaster Live! 7.1)使用了不同的芯片,'''不支持'''硬件混音。}}<br />
<br />
{{注意|板载 VIA8237 芯片支持4路硬件混音,但在某些主板上只能使用3路(第4路无声),甚至根本无法使用。即使能正常工作,质量也比不上其他混音方法。}}<br />
<br />
====问题修复====<br />
如果你的声卡是 Intel Corporation 82801I (ICH9 Family) HD Audio Controller (rev 02),且使用了64位系统,需要执行以下命令才能在 Enemy Territory 中听到声音:<br />
echo "et.x86 0 0 direct" > /proc/asound/card0/pcm0p/oss<br />
echo "et.x86 0 0 disable" > /proc/asound/card0/pcm0c/oss<br />
<br />
==疑难解答==<br />
===使用CPU频率动态调整时,音频发生跳跃===<br />
使用 {{ic|ondemand}} 或 {{ic|conservative}} 频率策略、启用CPU频率动态调整时,某些声卡和某些 ALSA 驱动会出现声音跳跃的情况。目前,唯一的解决方案是使用固定的频率,如 {{ic|performance}} 性能策略。<br />
<br />
详情参见 [[CPU Frequency Scaling (简体中文)]]。<br />
<br />
===Problems with Availability to Only One User at a Time===<br />
You might find that only one user can use the dmixer at a time. This is probably ok for most, but for those who run [[mpd]] as a separate user this poses a problem. When mpd is playing a normal user cannot play sounds though the dmixer. While it's quite possible to just run mpd under a user's login account, another solution has been found. Adding the line {{ic|ipc_key_add_uid 0}} to the {{ic|pcm.dmixer}} block disables this locking. The following is a snippet of the {{ic|asound.conf}}, the rest is the same as above.<br />
...<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
slave {<br />
...<br />
<br />
===Simultaneous Playback Issues===<br />
<br />
If you are having simultaneous playback issues, and if [[PulseAudio]] is installed (i.e. by [[GNOME]]), its default configuration is set to "hijack" the soundcard. Some users of ALSA may not want to use [[PulseAudio]] and are quite content with their current ALSA settings. One fix is to edit {{ic|/etc/asound.conf}} and comment out the following lines:<br />
# Use PulseAudio by default<br />
#pcm.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
# hint {<br />
# show on<br />
# description "Default ALSA Output (currently PulseAudio Sound Server)"<br />
# }<br />
#}<br />
<br />
Commenting the following out also may help:<br />
#ctl.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
#}<br />
<br />
This may be a much simpler solution than completely uninstalling [[PulseAudio]].<br />
<br />
Effectively, here is an example of a working {{ic|/etc/asound.conf}}:<br />
{{bc|<nowiki>pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
}<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}</nowiki><br />
}}<br />
<br />
{{note|This {{ic|/etc/asound.conf}} file was intended for and used successfully with a global [[MPD]] configuration. See [[Alsa#Problems_with_Availability_to_Only_One_User_at_a_Time|this section]] on multiple users}}<br />
<br />
===Random Lack of Sound on Startup===<br />
If you randomly have no sound on startup, it may be because your system has multiple sound cards, and their order may sometimes change on startup. If this is the case, try [[#Set the default sound card|setting the default sound card]].<br />
<br />
If you use mpd and the configuration tips above do not work for you, try [http://mpd.wikia.com/wiki/Configuration#ALSA_MPD_software_volume_control reading this] instead.<br />
<br />
===Specific Program Problems===<br />
For other programs who insist on their own audio setup, eg, XMMS or Mplayer, you would need to set their specific options.<br />
<br />
For mplayer, open up {{ic|~/.mplayer/config}} (or {{ic|/etc/mplayer/mplayer.conf}} for global setting) and add the following line:<br />
ao=alsa<br />
<br />
For XMMS/Beep Media Player, go into their options and make sure the sound driver is set to Alsa, not oss.<br />
<br />
To do this in XMMS:<br />
* Open XMMS<br />
** Options -> preferences.<br />
** Choose the Alsa output plugin.<br />
<br />
For applications which do not provide a ALSA output, you can use aoss from the alsa-oss package. To use aoss, when you run the program, prefix it with {{ic|aoss}}, eg:<br />
aoss realplay<br />
<br />
pcm.!default{ ... } doesnt work for me anymore. but this does:<br />
pcm.default pcm.dmixer<br />
<br />
===Model Settings===<br />
Although Alsa detects your soundcard through the BIOS at times Alsa may not be able to recognize your [http://www.kernel.org/doc/Documentation/sound/alsa/HD-Audio-Models.txt model type]. The soundcard chip can be found in {{ic|alsamixer}} (e.g. ALC662) and the model can be set in {{ic|/etc/modprobe.d/modprobe.conf}} or {{ic|/etc/modprobe.d/sound.conf}}. For example:<br />
<br />
options snd-hda-intel model=MODEL<br />
<br />
There are other model settings too. For most cases Alsa defaults will do. If you want to look at more specific settings for your soundcard take a look at the [http://bugtrack.alsa-project.org/main/index.php/Matrix:Main Alsa Soundcard List] find your model, then Details, then look at the "Setting up modprobe..." section. Enter these values in {{ic|/etc/modprobe.d/modprobe.conf}}. For example, for an Intel AC97 audio:<br />
<br />
{{bc|# ALSA portion<br />
alias char-major-116 snd<br />
alias snd-card-0 snd-intel8x0<br />
# module options should go here<br />
<br />
# OSS/Free portion<br />
alias char-major-14 soundcore<br />
alias sound-slot-0 snd-card-0<br />
<br />
# card #1<br />
alias sound-service-0-0 snd-mixer-oss<br />
alias sound-service-0-1 snd-seq-oss<br />
alias sound-service-0-3 snd-pcm-oss<br />
alias sound-service-0-8 snd-seq-oss<br />
alias sound-service-0-12 snd-pcm-oss}}<br />
<br />
===Conflicting PC Speaker===<br />
If you are sure nothing is muted, that your drivers are installed correctly, and that your volume is right, but you still do not hear anything, then try adding the following line to {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
options snd-NAME-OF-MODULE ac97_quirk=0<br />
<br />
The above fix has been observed to work with {{ic|via82xx}}<br />
options snd-NAME-OF-MODULE ac97_quirk=1<br />
The above fix has been reported to work with {{ic|snd_intel8x0}}<br />
<br />
===No Microphone Input===<br />
In alsamixer, make sure that all the volume levels are up under recording, and that CAPTURE is toggled active on the microphone (e.g. Mic, Internal Mic) and/or on Capture (in alsamixer, select these items and press space). Try making positive Mic Boost and raising Capture and Digital levels higher; this make make static or distortion, but then you can adjust them back down once you are hearing ''something'' when you record<br />
<br />
As the pulseaudio wrapper is shown as "default" in alsamixer, you may have to press F6 to select your actual soundcard first. You may also need to enable and increase the volume of Line-in in the Playback section.<br />
<br />
To test the microphone, run these commands (see arecord's man page for further information):<br />
arecord -d 5 test-mic.wav<br />
aplay test-mic.wav<br />
<br />
If all fails, you may want to eliminate hardware failure by testing the microphone with a different device.<br />
<br />
For at least some computers, muting a microphone (MM) simply means its input does not go immediately to the speakers. It still receives input.<br />
<br />
Many Dell laptops need "-dmic" to be appended to the model name in {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-hda-intel model=dell-m6-dmic<br />
<br />
Some programs use try to use OSS as the main input software. Add the following lines to {{ic|/etc/modprobe.d/modprobe.conf}} to prevent OSS modules from being loaded:<br />
<br />
{{Note|The OSS modules are no longer autoloaded anyway.}}<br />
<br />
blacklist snd_pcm_oss<br />
blacklist snd_mixer_oss<br />
blacklist snd_seq_oss<br />
<br />
See also:<br />
* http://www.alsa-project.org/main/index.php/SoundcardTesting<br />
* http://alsa.opensrc.org/Record_from_mic<br />
<br />
===Setting the default Microphone/Capture Device===<br />
Some applications (Pidgin, Adobe Flash) do not provide an option to change the capture device. It becomes an issue if your microphone is on a separate device (e.g. USB webcam or microphone) than your internal sound card. To change only the default capture device, leaving the default playback device as is, you can modify your {{ic|~/.asoundrc}} file to include the following:<br />
<br />
pcm.usb<br />
{<br />
type hw<br />
card U0x46d0x81d<br />
}<br />
<br />
pcm.!default<br />
{<br />
type asym<br />
playback.pcm<br />
{<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
capture.pcm<br />
{<br />
type plug<br />
slave.pcm "usb"<br />
}<br />
}<br />
<br />
Replace "U0x46d0x81d" with your capture device's card name in ALSA. You can use {{ic|arecord -L}} to list all the capture devices detected by ALSA.<br />
<br />
===Internal Microphone not working===<br />
First make sure all the volume levels are up under recording in alsamixer. In my case adding the following option to /etc/sound.conf and reloading the snd-* module produced a new volume setting called Capture which was capturing for the internal mic. For eg, for snd-hda-intel add<br />
<br />
options snd-hda-intel enable_msi=1<br />
<br />
Then reload the module (as below), up the recording volume of Capture and then test.<br />
<br />
{{bc|# rmmod snd-hda-intel && modprobe snd-hda-intel}}<br />
<br />
===No Sound with Onboard Intel Sound Card===<br />
There may be an issue with two conflicting modules loaded, namely {{ic|snd_intel8x0}} and {{ic|snd_intel8x0m}}. In this case, blacklist snd_intel8x0m:<br />
<br />
{{hc|/etc/modprobe.d/modprobe.conf|blacklist snd_intel8x0m}}<br />
<br />
''Muting'' the "External Amplifier" in {{ic|alsamixer}} or {{ic|amixer}} may also help. See [http://alsa.opensrc.org/Intel8x0#Dell_Inspiron_8600_.28and_probably_others.29 the ALSA wiki].<br />
<br />
Unmuting the "Mix" setting in the mixer might help, also.<br />
<br />
===No Headphone Sound with Onboard Intel Sound Card===<br />
With '''Intel Corporation 82801 I (ICH9 Family) HD Audio Controller''' on laptop, you may need to add this line to modprobe or sound.conf:<br />
<br />
options snd-hda-intel model=$model<br />
<br />
Where $model is any one of the following (in order of possibility to work, but not merit):<br />
<br />
* dell-vostro<br />
* olpc-xo-1_5<br />
* laptop<br />
* dell-m6<br />
* laptop-hpsense<br />
<br />
Note: It may be necessary to put this "options" line below (after) any "alias" lines about your card.<br />
<br />
You can see all the available models in the kernel documentation. For example [http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.35.y.git;a=blob;f=Documentation/sound/alsa/HD-Audio-Models.txt;h=dc25bb84b83b49665a7ed850e7bf5423d50cd3ba;hb=HEAD here], but check that it is the correct version of that document for your kernel version.<br />
<br />
A list of available models is also available [http://www.mjmwired.net/kernel/Documentation/sound/alsa/HD-Audio-Models.txt here]. To know your chip name type the following command (with * being corrected to match your files). Note that some chips could have been renamed and do not directly match the available ones in the file.<br />
<br />
cat /proc/asound/card*/codec* | grep Codec<br />
<br />
Note that there is a high chance none of the input devices (all internal and external mics) will work if you choose to do this, so it is either your headphones or your mic. Please report to ALSA if you are affected by this bug.<br />
<br />
And also, if you have problems getting beeps to work (pcspkr):<br />
<br />
options snd-hda-intel model=$model enable=1 index=0<br />
<br />
===No sound when S/PDIF video card is installed===<br />
Discover available modules and their order:<br />
$ cat /proc/asound/modules<br />
0 snd_hda_intel<br />
1 snd_ca0106<br />
<br />
Disable the undesired video card audio codec in {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
# /etc/modprobe.d/modprobe.conf<br />
#<br />
install snd_hda_intel /bin/false<br />
<br />
If both devices use the same module, it might be possible to disable one of them in the BIOS.<br />
<br />
===Poor sound quality or clipping===<br />
If you experience poor sound quality, try setting the PCM volume (in alsamixer) to a level such that gain is 0.<br />
<br />
If snd-usb-audio driver has been loaded, you could try to enable {{ic|softvol}} in '''/etc/asound.conf''' file. Example configuration for the first audio device:<br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.dsnooper {<br />
type dsnoop<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
channels 2<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.softvol {<br />
type softvol<br />
slave { pcm "dmixer" }<br />
control {<br />
name "Master"<br />
card 0<br />
}<br />
}<br />
ctl.!default {<br />
type hw<br />
card 0<br />
}<br />
ctl.softvol {<br />
type hw<br />
card 0<br />
}<br />
ctl.dmixer {<br />
type hw<br />
card 0<br />
}<br />
<br />
===Pops When Starting and Stopping Playback===<br />
Some modules (e.g. snd_ac97_codec and snd_hda_intel) can power off your sound card when not in use. This can make an audible noise (like a crack/pop/scratch) when turning on/off your sound card. Sometimes even when move the slider volume, or open and close windows (KDE4). If you find this annoying try {{ic|modinfo snd_MY_MODULE}}, and look for a module option that adjusts or disables this feature.<br />
<br />
Example: to disable the power saving mode and solve cracking sound trough speakers issue, using snd_hda_intel add in {{ic|/etc/modprobe.d/modprobe.conf}}<br />
options snd_hda_intel power_save=0<br />
or<br />
options snd_hda_intel power_save=0 power_save_controller=N<br />
<br />
You can also try it with {{ic|1=modprobe snd_hda_intel power_save=0}} before.<br />
<br />
You may also have to unmute the 'Line' ALSA channel for this to work. Any value will do (other than '0' or something too high).<br />
<br />
''Example:'' on an onboard VIA VT1708S (using the snd_hda_intel module) these cracks occured even though 'power_save' was set to 0. Unmuting the 'Line' channel and setting a value of '1' solved the issue.<br />
<br />
Source: https://www.kernel.org/doc/Documentation/sound/alsa/powersave.txt<br />
<br />
If you use a laptop, pm-utils will change {{ic|power_save}} back to 1 when you go onto battery power even if you disable power saving in {{ic|/etc/modprobe.d}}. Disable this for pm-utils by disabling the script that makes the change (see [https://wiki.archlinux.org/index.php/Pm-utils#Disabling_a_hook Disabling a hook] for more information):<br />
# touch /etc/pm/power.d/intel-audio-powersave<br />
<br />
===S/PDIF Output Does Not Work===<br />
<br />
If the optical/coaxial digital output of your motherboard/sound card is not working or stopped working, and have already enabled and unmuted it in alsamixer, try running<br />
# iecset audio on<br />
<br />
as root.<br />
<br />
You can also put this command in {{Ic|rc.local}} as it sometimes it may stop working after a reboot.<br />
<br />
===HDMI Output Does Not Work===<br />
If the HDMI output of your motherboard/sound card is not working or stopped working, and have already enabled and unmuted it in alsamixer, try the following.<br />
<br />
Query for Playback Devices:<br />
<br />
$ aplay -l<br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: NVidia [HDA NVidia], device 0: ALC1200 Analog [ALC1200 Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 1: ALC1200 Digital [ALC1200 Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 3: NVIDIA HDMI [NVIDIA HDMI]<br />
Subdevices: 0/1<br />
Subdevice #0: subdevice #0<br />
<br />
Now that we have the info for the HDMI device, try a test, In the example below, 0 is the card number and 3 is the device number.<br />
<br />
$ aplay -D plughw:0,3 /usr/share/sounds/alsa/Front_Center.wav<br />
<br />
If aplay does not output any errors, but still no sound is heared, "reboot" the receiver, monitor or tv set. Since the HDMI interface executes a handshake on connection, it might have noticed before that there was no audio stream embedded, and disabled audio decoding. In particular, if you are using a standalone window manager (don’t know about Gnome or KDE), you may need to have some sound playing ''while'' plugging in the HDMI cable.<br />
<br />
{{Note|If you are using an ATI card and linux 3.0, check your kernel/grub settings, you most likely need to use the ''''radeon.audio'''<nowiki>=</nowiki>'''1'''' boot parameter, or alternatively set up the option in your modprobe configuration files.}}<br />
<br />
If the test is successful, create or edit your ~/.asoundrc file to set HDMI as the default audio device.<br />
<br />
{{hc|~/.asoundrc|<br />
pcm.!default {<br />
type hw<br />
card 0<br />
device 3<br />
}<br />
}}<br />
<br />
Or you above config does not work try:<br />
{{hc|~/.asoundrc|<br />
defaults.pcm.card 0<br />
defaults.pcm.device 3<br />
defaults.ctl.card 0<br />
}}<br />
<br />
===HDMI Multi-channel PCM output does not work (Intel) ===<br />
As of Linux 3.1 multi-channel PCM output through HDMI with a Intel card (Intel Eaglelake, IbexPeak/Ironlake,SandyBridge/CougarPoint and IvyBridge/PantherPoint) is not yet supported. Support for it has been recently added and expected to be available in Linux 3.2. To make it work in Linux 3.1 you need to apply the following patches:<br />
<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=76adaa34db407f174dd06370cb60f6029c33b465 drm: support routines for HDMI/DP ELD]<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=e0dac65ed45e72fe34cc7ccc76de0ba220bd38bb drm/i915: pass ELD to HDMI/DP audio driver]<br />
<br />
===HP TX2500===<br />
Add these 2 lines into {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-cmipci mpu_port=0x330 fm_port=0x388<br />
options snd-hda-intel index=0 model=toshiba position_fix=1<br />
<br />
options snd-hda-intel model=hp (works for tx2000cto)<br />
<br />
===Skipping Sound When Playing MP3===<br />
If you have sound skipping when playing MP3 files and you have more then 2 speakers attached to your computer (i.e. > 2 speaker system), run alsamixer and disable the channels for the speakers that you '''DO NOT''' have (i.e. do not enable the sound for the center speaker if you do not have a center speaker.<br />
<br />
===Using a USB Headset and External USB Sound Cards===<br />
If you are using a USB headset with ALSA you can try using {{AUR|asoundconf}} (currently only available from the [[AUR]]) to set the headset as the primary sound output. Before running make sure you have usb audio module enabled ({{ic|modprobe snd-usb-audio}}).<br />
<br />
# asoundconf is-active<br />
# asoundconf list<br />
# asoundconf set-default-card <chosen soundcard><br />
<br />
==== Crackling sound with USB devices ====<br />
If you experience crackling sound on USB devices, you can try tuning the snd-usb-audio for minimal latency.<br />
<br />
Add this to your {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
options snd-usb-audio nrpacks=1<br />
<br />
source: http://alsa.opensrc.org/Usb-audio#Tuning_USB_devices_for_minimal_latencies<br />
<br />
==== Hot-plugging a USB Sound Card ====<br />
In order to automatically make a USB Sound Card the primary output device, when the card is plugged in, you can use the following udev rules (e.g. add the following two lines to {{ic|/etc/udev/rules.d/00-local.rules}} and reboot).<br />
<br />
{{bc|1=KERNEL=="pcmC[D0-9cp]*", ACTION=="add", PROGRAM="/bin/sh -c 'K=%k; K=$${K#pcmC}; K=$${K%%D*}; echo defaults.ctl.card $$K > /etc/asound.conf; echo defaults.pcm.card $$K >>/etc/asound.conf'"<br />
KERNEL=="pcmC[D0-9cp]*", ACTION=="remove", PROGRAM="/bin/sh -c 'echo defaults.ctl.card 0 > /etc/asound.conf; echo defaults.pcm.card 0 >>/etc/asound.conf'"}}<br />
<br />
===Error 'Unknown hardware' Appears After a Kernel Update===<br />
The following messages may be displayed during the start-up ALSA after the kernel update:<br />
Unknown hardware "foo" "bar" ...<br />
Hardware is initialized using a guess method<br />
/usr/sbin/alsactl: set_control:nnnn:failed to obtain info for control #mm (No such file or directory)<br />
<br />
or<br />
Found hardware: "HDA-Intel" "VIA VT1705" "HDA:11064397,18490397,00100000" "0x1849" "0x0397"<br />
Hardware is initialized using a generic method<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #1 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #2 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #25 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #26 (No such file or directory)<br />
<br />
Simply store ALSA mixer settings again (as root):<br />
# alsactl -f /var/lib/alsa/asound.state store<br />
<br />
It may be necessary configure ALSA again with alsamixer<br />
<br />
===HDA Analyzer===<br />
If the mappings to your audio pins(plugs) do not correspond but ALSA works fine, you could try HDA Analyzer -- a pyGTK2 GUI for HD-audio control can be found [http://www.alsa-project.org/main/index.php/HDA_Analyzer at the ALSA wiki].<br />
Try tweaking the Widget Control section of the PIN nodes, to make microphones IN and headphone jacks OUT. Referring to the Config Defaults heading is a good idea.<br />
<br />
NOTE: the script is done by such way that it is incompatible with python3 (which is now shipped with ArchLinux) but tries to use it.<br />
The workaround is: open "run.py", find all occurences of "python" (2 occurences - one on the first line, and the second on the last line) and replace them all by "python2".<br />
<br />
NOTE2: the script requires root acces, but running it via su/sudo is bogus. Run it via {{ic|kdesu}} or {{ic|gksu}}.<br />
<br />
===ALSA with SDL===<br />
If you get no sound via SDL and ALSA cannot be chosen from the application. Try setting the environmental variable SDL_AUDIODRIVER to alsa.<br />
# export SDL_AUDIODRIVER=alsa<br />
<br />
===Low Sound Workaround===<br />
<br />
If you are facing low sound even after maxing out your speakers/headphones, you can give the softvol plugin a try. Add the following to {{ic|/etc/asound.conf}}.<br />
{{bc|<nowiki><br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
<br />
pcm.softvol {<br />
type softvol<br />
slave {<br />
pcm "dmix"<br />
}<br />
control {<br />
name "Pre-Amp"<br />
card 0<br />
}<br />
min_dB -5.0<br />
max_dB 20.0<br />
resolution 6<br />
}<br />
</nowiki>}}<br />
{{note| You will probably have to restart the computer, as restarting the alsa daemon did not load the new configuration for me. Also, if the configuration does not work even after restarting, try changing {{ic|plug}} with {{ic|hw}} in the above configuration.}}<br />
<br />
After the changes are loaded successfully, you will see a {{ic|Pre-Amp}} section in alsamixer. You can adjust the levels there.<br />
{{note|Setting a high value for {{ic|Pre-Amp}} can cause sound distortion, so adjust it according to the level that suits you.}}<br />
<br />
===Popping sound after resuming from suspension===<br />
You might hear a popping sound after resuming the computer from suspension. This can be fixed by editing {{ic|/etc/pm/sleep.d/90alsa}} and removing the line that says {{ic|aplay -d 1 /dev/zero}}<br />
<br />
==设置文件范例==<br />
参见:[[Advanced Linux Sound Architecture/Example Configurations]]。<br />
<br />
==相关阅读==<br />
* [http://www.mjmwired.net/kernel/Documentation/sound/alsa/ALSA-Configuration.txt ALSA 模块高级配置]<br />
* [http://alsa.opensrc.org/Main_Page 非官方的 ALSA Wiki]<br />
* [https://bbs.archlinux.org/viewtopic.php?id=36815 如何从 svn 获取驱动代码并编译]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Systemd_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=250604
Systemd (简体中文)
2013-03-14T08:01:10Z
<p>Cuihao: 更新翻译</p>
<hr />
<div>{{Lowercase title}}<br />
[[Category:Daemons and system services (简体中文)]]<br />
[[Category:Boot process (简体中文)]]<br />
[[en:Systemd]]<br />
[[es:Systemd]]<br />
[[fr:Systemd]]<br />
[[it:Systemd]]<br />
[[ja:Systemd]]<br />
[[ru:Systemd]]<br />
[[zh-TW:Systemd]]<br />
{{Article summary start|概览}}<br />
{{Article summary text|本文介绍 Systemd 的安装和配置。}}<br />
{{Article summary heading|相关页面}}<br />
{{Article summary wiki|Systemd/Services}}<br />
{{Article summary wiki|systemd/User}}<br />
{{Article summary wiki|Systemd FAQ}}<br />
{{Article summary wiki|init Rosetta}}<br />
{{Article summary wiki|udev (简体中文)}}<br />
{{Article summary end}}<br />
{{TranslationStatus (简体中文)|Systemd|2013-03-14|250286}}<br />
摘自[http://freedesktop.org/wiki/Software/systemd 项目主页]:<br />
<br />
'''''systemd''' 是 Linux 下的一款系统和服务管理器,兼容 SysV 和 LSB 的启动脚本。systemd 的特性有:支持并行化任务;同时采用 socket 式与 [[D-Bus (简体中文)|D-Bus]] 总线式激活服务;按需启动守护进程(daemon);利用 Linux 的 [[cgroups|cgroups]] 监视进程;支持快照和系统恢复;维护挂载点和自动挂载点;各服务间基于依赖关系进行精密控制。systemd 完全可以替代 Arch 曾经使用的 [[SysVinit (简体中文)|sysvinit]] 启动系统。''<br />
<br />
{{注意|[https://bbs.archlinux.org/viewtopic.php?pid&#61;1149530#p1149530 这个帖子]详细解释了 Arch 向 systemd 迁移的原因。}}<br />
<br />
另见[[Wikipedia:Systemd|维基百科上的介绍]]。<br />
<br />
== 迁移前需考虑 ==<br />
<br />
* 阅读[http://freedesktop.org/wiki/Software/systemd/ 该站],了解 systemd。<br />
* systemd 自己有一套日志('''journal''')系统,用于代替 '''syslog'''。两者也可以共存,参见后面的[[#日志|日志部分]]。<br />
* 虽然 systemd 可以替换 '''cron'''、'''acpid'''、'''xinetd''' 等的部分功能。至少目前还可以继续使用这些服务,无需立即切换。<br />
* 交互式 initscripts 启动脚本在 systemd 中无法工作。例如 '''netcfg-menu''' [https://bugs.archlinux.org/task/31377 无法]在启动时显示。<br />
<br />
== 安装 ==<br />
{{注意|从[https://www.archlinux.org/news/systemd-is-now-the-default-on-new-installations/ 2012-10-13版安装介质]开始,安装程序已经默认安装{{pkg|systemd}} 和 {{pkg|systemd-sysvcompat}}。}}<br />
{{注意|如果是在 VPS 中使用 Arch,请先阅读:[[Virtual_Private_Server#Moving_your_VPS_from_initscripts_to_systemd]]。}}<br />
<br />
本部分帮助还在使用{{pkg|sysvinit}} 和 {{pkg|initscripts}} 的用户迁移到{{pkg|systemd}}.<br />
<br />
# 从[[Official_Repositories_(简体中文)|官方软件仓库]]安装 {{pkg|systemd}} 并添加[[Kernel parameters|内核参数]]{{Ic|1=init=/usr/lib/systemd/systemd}}<br />
# 用 {{ic|systemctl enable ''daemonname''}} [[#使用单元|启用]] {{ic|/etc/rc.conf}} 中的守护进程。从 {{ic|/etc/rc.conf}} 到 systemd 服务的转换可以参考:[[Daemons_List_(简体中文)|守护进程列表]] 和 [[Systemd/Services|Services]]。<br />
# 重启系统,执行命令 {{ic|cat /proc/1/comm}},如果返回{{ic|systemd}},表示 systemd 已经正常启动。<br />
# 确认主机名已经正确设置:{{ic|hostnamectl set-hostname myhostname}}。<br />
# 删除 {{pkg|initscripts}} 和 {{pkg|sysvinit}},并安装{{pkg|systemd-sysvcompat}}.<br />
# (可选)删除{{ic|1=init=/usr/lib/systemd/systemd}}内核参数,现在已经不需要它了。{{pkg|systemd-sysvcompat}} 软件包提供了一个软链接,使 systemd 成为默认 init。<br />
<br />
=== 附加信息 ===<br />
* 如果内核参数中有 {{ic|quiet}},可以先删除它以便调试。<br />
{{Accuracy|reason=据有的用户称,systemd 并未对用户组有特殊要求。尽管有很多不常用的用户组,但从 consolekit 切换到 logind 后这些用户组没有发生改变。''(据译者理解:如果你碰到特殊的权限问题,可能仍需要加入这些用户组才能解决。)''}}<br />
* 使用 systemd 的时候'''无需'''将用户加入特殊[[Users and Groups (简体中文)|用户组]](如{{ic|sys}}、{{ic|disk}}、{{ic|lp}}、{{ic|network}}、{{ic|video}}、{{ic|audio}}、{{ic|optical}}、{{ic|storage}}、{{ic|scanner}}、{{ic|power}}等等)。加入这些组反而会有问题,例如audio组会导致程序阻塞软件混声。每个 PAM 登录都拥有一个 logind 会话,它通过[[Wikipedia:Access control list|POSIX ACLs]],赋予本地会话以声音设备访问权限、通过[[udisks]]挂载和卸载移动设备的权限等。<br />
{{注意|对于使用 {{ic|startx -- vt7}} 进入X环境的用户,由于 logind 会话无法应用于X环境,用户'''必须'''加入这些组。}}<br />
* 如果卸载 {{Pkg|initscripts}},{{ic|rc.conf}} 将不再有效。如果曾在其中设置了静态网络,或还未迁移到systemd的系统服务,请根据[[#Initscripts 模拟|该节]]配置。<br />
* 如果 fstab 中有 LVM 设备,首次启动时会因为等待超时进入急救控制台。输入管理员密码,执行 {{ic|systemctl enable lvm}} 启用相关服务,重启即可。<br />
<br />
== 原生 systemd 配置文件 ==<br />
{{注意|可能需要手动创建某些文件。所有文件的权限都是644,属主 root,属组 root。}}<br />
<br />
=== 主机名 ===<br />
主机名通过文件 {{ic|/etc/hostname}} 进行配置。文件中可以包含计算机域名(如果有),但目前的 {{ic|hostnamectl}} 尚不能设置完整的域名(FDQN)。使用如下命令设置短主机名:<br />
# hostnamectl set-hostname '''myhostname'''<br />
<br />
详情参见 {{ic|man 5 hostname}} 和 {{ic|man 1 hostnamectl}}。<br />
<br />
文件示例:<br />
{{hc|/etc/hostname|<br />
myhostname<br />
}}<br />
<br />
=== 本地化设置(locale) ===<br />
系统默认 locale 记录在 {{ic|/etc/locale.conf}} 文件中。通过如下命令配置:<br />
# localectl set-locale LANG="zh_CN.utf8"<br />
<br />
{{注意|设置默认locale前,请确保已经生成该locale。方法是:在 {{ic|/etc/locale.gen}} 中启用对应locale,然后以root权限执行 {{ic|locale-gen}}。}}<br />
<br />
详情参见 {{ic|man 1 localectl}} 和 {{ic|man 5 locale.conf}}。<br />
<br />
* 另参阅 [[Locale (简体中文)]]。<br />
<br />
文件示例:<br />
{{hc|/etc/locale.conf|<nowiki><br />
LANG=en_US.utf8<br />
</nowiki>}}<br />
<br />
=== 虚拟控制台 ===<br />
{{ic|/etc/vconsole.conf}} 文件用来配置虚拟控制台,包括键盘布局和控制台字体:<br />
{{hc|/etc/vconsole.conf|<nowiki><br />
KEYMAP=us<br />
FONT=lat9w-16<br />
FONT_MAP=8859-1_to_uni</nowiki>}}<br />
<br />
{{注意|{{ic|{{Pkg|systemd}}-194}} 默认使用内核字体和键盘布局,因此以后不必在上述文件中留下空的 {{ic|1=KEYMAP=}} 和 {{ic|1=FONT=}} 设置。}}<br />
<br />
还可以通过命令设置键盘布局:<br />
# localectl set-keymap de<br />
<br />
{{ic|localectl}} 也可以设置X11的键盘布局:<br />
<br />
# localectl set-x11-keymap de<br />
<br />
详情参见 {{ic|man 1 localectl}} 和 {{ic|man 5 vconsole.conf}}。<br />
<br />
* 另参阅 [[Fonts (简体中文)#控制台字体|控制台字体]] 和 [[KEYMAP (简体中文)#键盘布局|键盘布局]]。<br />
<br />
=== 时区 ===<br />
时区可通过创建适当的{{ic|/etc/localtime}}符号链接来进行配置,该链接需指向目录{{ic|/usr/share/zoneinfo/}}下的一个zoneinfo文件。也可执行以下命令直接完成这一操作:<br />
<br />
# timedatectl set-timezone Asia/Shanghai<br />
<br />
更多细节参见{{ic|man 1 timedatectl}},{{ic|man 5 localtime}}和{{ic|man 7 archlinux}}。<br />
<br />
手动创建链接也行:<br />
# ln -sf ../usr/share/zoneinfo/Asia/Shanghai /etc/localtime<br />
<br />
旧的配置文件 {{ic|/etc/timezone}} 已经没用了,可以安全删除。<br />
<br />
=== 硬件时钟 ===<br />
<br />
systemd 默认识别硬件时钟为协调世界时(UTC)。<br />
<br />
{{小贴士|推荐使用[[NTP (简体中文)|NTP服务]]来在线同步硬件时钟。}}<br />
<br />
==== 硬件时钟设定为地方时 ====<br />
<br />
将硬件时钟配置为地方时('''不建议'''):<br />
<br />
# timedatectl set-local-rtc true<br />
<br />
重新调整为 UTC:<br />
<br />
# timedatectl set-local-rtc false<br />
<br />
如果设置成本地时间,处理夏令时有些麻烦。如果夏令时调整发生在关机时,下次启动时时间会出现问题([http://www.cl.cam.ac.uk/~mgk25/mswish/ut-rtc.html 更多信息])。最新的内核直接从实时时钟芯片(RTC)读取时间,不使用 {{ic|hwclock}},内核把从 RTC 读取的时间当作 UTC 处理。所以如果硬件时间是地方时,系统启动一开始识别的时间是错误的,之后很快会进行矫正。这可能导致一些问题(尤其是时间倒退时)。<br />
<br />
如果同时安装了 Windows 操作系统([http://blogs.msdn.com/b/oldnewthing/archive/2004/09/02/224672.aspx 默认使用地方时]),那么一般 RTC 会被设置为地方时。Windows 其实也能处理 UTC,需要[[Time (简体中文)#Windows 系统使用 UTC|修改注册表]]。建议让 Windows 使用 UTC,而非让 Linux 使用地方时。Windows 使用 UTC 后,请记得禁用 Windows 的时间同步功能,以防 Windows 错误设置硬件时间。如上文所说,Linux 可以使用[[NTP (简体中文)|NTP服务]]来在线同步硬件时钟。<br />
<br />
* 详情参阅 [[Time (简体中文)]]。<br />
<br />
=== 内核模块 ===<br />
目前,所有必要模块的加载均由 [[udev]] 自动完成。所以,如果不需要使用任何额外的模块,就没有必要在任何配置文件中添加启动时加载的模块。但是,有些情况下可能需要在系统启动时加载某个额外的模块,或者将某个模块列入黑名单以便使系统正常运行。<br />
<br />
==== 开机加载 ====<br />
systemd 读取 {{ic|/etc/modules-load.d/}} 中的配置加载额外的内核模块。配置文件名称通常为 {{ic|/etc/modules-load.d/<program>.conf}}。格式很简单,一行一个要读取的模块名,而空行以及第一个非空格字符为{{ic|#}}或{{ic|;}}的行会被忽略,如:<br />
{{hc|/etc/modules-load.d/virtio-net.conf|<nowiki><br />
# Load virtio-net.ko at boot<br />
virtio-net</nowiki>}}<br />
<br />
另见{{ic|man 5 modules-load.d}}。<br />
<br />
==== 禁用内核模块 ====<br />
禁用内核模块的方法和 Arch 默认的 {{Pkg|initscripts}} 相同,因为该过程实际由 {{Pkg|kmod}} 处理。参见:[[Kernel modules (简体中文)#黑名单]]。<br />
<br />
=== 文件系统挂载 ===<br />
<br />
默认行为是:在启动一个需要挂载特定分区的服务之前,系统自动检查并挂载分区。{{ic|/etc/fstab}} 中设定的网络文件系统(如 [[NFS (简体中文)|NFS]]、[[Samba (简体中文)|Samba]])无需配置即可正常工作,systemd 将确保网络文件系统在网络链接就绪后挂载。<br />
<br />
详情参阅:{{ic|man 5 systemd.mount}}。<br />
<br />
==== 自动挂载 ====<br />
<br />
* 如果 {{ic|/home}} 分区较大,可以让不依赖 {{ic|/home}} 分区的服务先启动。把下面的参数添加到 {{ic|/etc/fstab}} 文件中 {{ic|/home}} 项目的参数部分即可:<br />
<br />
noauto,x-systemd.automount<br />
<br />
这样 {{ic|/home}} 分区只有需要访问时才会被挂载。内核会缓存所有的文件操作,直到 {{ic|/home}} 分区准备完成。<br />
<br />
{{注意|这样做会使 {{ic|/home}} 的文件系统类型被识别为 {{ic|autofs}},造成 [[mlocate]] 查询时忽略该目录。实际加速效果因配置而异,所以请自己权衡是否需要。}}<br />
<br />
* 挂载远程文件系统也是同理。如果你仅想在需要的时候才挂载,也可以添加 {{ic|noauto,x-systemd.automount}} 参数。另外,可以设置 {{ic|1=x-systemd.device-timeout=#}} 参数,设置超时时间,以防止网络资源不能访问的时候浪费时间。<br />
<br />
* 如果你的加密文件系统需要密钥,则需要添加 {{ic|noauto}} 参数到 {{ic|/etc/crypttab}} 文件中的对应位置。systemd 开机的时候就不会打开这个加密设备,会一直等待到设备被访问时再使用密钥文件挂载。比如在使用加密RAID设备的时候可以节省一定的时间,因为 systemd 不必等到设备可用后才能访问。例如:<br />
<br />
{{hc|/etc/crypttab|<br />
data /dev/md0 /root/key noauto}}<br />
<br />
==== LVM ====<br />
<br />
如果装有不通过 [[Mkinitcpio (简体中文)|initramfs]] 激活的[[LVM (简体中文)|LVM]]卷,则需启动 {{ic|lvm-monitoring}} 服务(由 {{pkg|lvm2}} 软件包提供):<br />
<br />
# systemctl enable lvm-monitoring<br />
<br />
类似地,如果LVM卷在加密设备上,并且启动后才激活,则需启动 {{ic|lvm-on-crypt}} 服务(同样由 {{pkg|lvm2}} 软件包提供):<br />
<br />
# systemctl enable lvm-on-crypt<br />
<br />
=== ACPI 电源管理 ===<br />
systemd 能够处理某些电源相关的 [[Wikipedia:Advanced_Configuration_and_Power_Interface|ACPI]] 事件,通过 {{ic|/etc/systemd/logind.conf}} 的下列选项配置:<br />
* {{ic|HandlePowerKey}}:按下电源键后的动作<br />
* {{ic|HandleSleepKey}}:按下挂起键后的动作<br />
* {{ic|HandleHibernateKey}}: 按下休眠键后的动作<br />
* {{ic|HandleLidSwitch}}:合上笔记本盖后待机<br />
<br />
动作可以是 {{ic|ignore}}、{{ic|poweroff}}、{{ic|reboot}}、{{ic|halt}}、{{ic|suspend}}、{{ic|hibernate}}、{{ic|hybrid-sleep}}、{{ic|lock}} 或 {{ic|kexec}}。<br />
<br />
系统默认设置为:<br />
HandlePowerKey=poweroff<br />
HandleSuspendKey=suspend<br />
HandleHibernateKey=hibernate<br />
HandleLidSwitch=suspend<br />
<br />
不用图形界面、或者使用 [[i3 (简体中文)|i3]]、[[awesome (简体中文)|awesome]] 这样简单的桌面管理器时,systemd 可以替代 [[acpid (简体中文)|acpid]] 处理 ACPI 事件。<br />
<br />
{{注意|运行 {{ic|systemctl restart systemd-logind.service}},使上述更改立即生效。}}<br />
<br />
{{注意|systemd 无法处理交流电源和电池 ACPI 事件,所以还得使用 [[Laptop Mode Tools (简体中文)|Laptop Mode Tools]] 或 [[acpid (简体中文)|acpid]] 工具。}}<br />
<br />
在当前版本的 systemd 中,这些 {{ic|Handle}} 选项将会被应用到整个系统当中,除非它们被别的程序——例如某个桌面环境中的电源管理器——给“阻止”(inhibited)。如果其它的程序没有阻止这些 {{ic|Handle}} ,你可能会先被 systemd 挂起你的系统,然后当系统被唤醒之后,电源管理器又会再次将系统挂起。<br />
<br />
{{警告|目前只有[[GNOME]] 和 [[KDE]] 支持 "inhibited" 命令。在其它的桌面管理器同样实现该功能之前,如果你想使用[[Xfce]]、[[acpid]] 或者其它程序来管理 ACPI 事件,你需要把 {{ic|Handle}} 选项设置为 {{ic|ignore}}。}}<br />
<br />
{{注意|除了内核默认的待机支持后端(用于处理待机/休眠),systemd 也可以使用其他后端(比如 [[Uswsusp]] 或 [[TuxOnIce]])。}}<br />
<br />
==== 休眠时执行的脚本 ====<br />
<br />
使用 {{ic|systemctl suspend}}、{{ic|systemctl hibernate}} 或 {{ic|systemctl hybrid-sleep}} 命令执行待机/休眠时,systemd 不会调用 [[pm-utils (简体中文)|pm-utils]]。[[pm-utils (简体中文)|pm-utils]] 的钩子扩展(hook)——包括 [[pm-utils (简体中文)#建立你自己的钩子|自定义钩子]]——会失效。不过,systemd 提供了两种类似的待机/休眠时执行脚本的机制。<br />
<br />
===== 使用服务文件 =====<br />
<br />
可以将服务文件附在 suspend.target、hibernate.target 或 sleep.target 中,这样就能在待机/休眠前后执行某些操作。用户级操作和root/系统级操作应该使用不同的服务文件。要启用用户级服务文件,使用 {{ic|# systemctl enable suspend@<用户名> && systemctl enable resume@<用户名>}}。例如:<br />
<br />
{{hc|/etc/systemd/system/suspend@.service|2=<nowiki><br />
[Unit]<br />
Description=User suspend actions<br />
Before=sleep.target<br />
<br />
[Service]<br />
User=%I<br />
Type=forking<br />
Environment=DISPLAY=:0<br />
ExecStartPre= -/usr/bin/pkill -u %u unison ; /usr/local/bin/music.sh stop ; /usr/bin/mysql -e 'slave stop'<br />
ExecStart=/usr/bin/sflock<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
{{hc|/etc/systemd/system/resume@.service|2=<nowiki><br />
[Unit]<br />
Description=User resume actions<br />
After=suspend.target<br />
<br />
[Service]<br />
User=%I<br />
Type=simple<br />
ExecStartPre=/usr/local/bin/ssh-connect.sh<br />
ExecStart=/usr/bin/mysql -e 'slave start'<br />
<br />
[Install]<br />
WantedBy=suspend.target</nowiki>}}<br />
<br />
至于root/系统级服务(使用 {{ic|# systemctl enable root-suspend}} 激活):<br />
<br />
{{hc|/etc/systemd/system/root-resume.service|2=<nowiki><br />
[Unit]<br />
Description=Local system resume actions<br />
After=suspend.target<br />
<br />
[Service]<br />
Type=simple<br />
ExecStart=/usr/bin/systemctl restart mnt-media.automount<br />
<br />
[Install]<br />
WantedBy=suspend.target</nowiki>}}<br />
<br />
{{hc|/etc/systemd/system/root-suspend.service|2=<nowiki><br />
[Unit]<br />
Description=Local system suspend actions<br />
Before=sleep.target<br />
<br />
[Service]<br />
Type=simple<br />
ExecStart=-/usr/bin/pkill sshfs<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
上述服务文件的一些解释(详见 {{ic|man systemd.service}}):<br />
* 如果设置 {{ic|1=<nowiki>Type=OneShot</nowiki>}},那么可以使用多个 {{ic|1=<nowiki>ExecStart=</nowiki>}} 参数。否则只能写一个,替代方案是在 {{ic|ExecStartPre}} 中添加命令,或使用分号分隔不同命令(见第一个例子,分号前后的空格都是'''必须'''的)。 <br />
* 若命令前加上一个“-”(半角减号),则命令返回非零值时会被忽略、当作正常执行处理。<br />
* 有关调试,最好的方法是用 {{ic|journalctl}} 查看日志。<br />
<br />
===== 合并待机和唤醒服务文件 =====<br />
<br />
利用下面这种自定义的一体化待机和唤醒服务,使用单一的钩子扩展即可对不同操作(待机/休眠/混合休眠)的不同阶段(进入/唤醒)进行控制。<br />
<br />
例子和解释:<br />
<br />
{{hc|/etc/systemd/system/wicd-sleep.service|2=<nowiki><br />
[Unit]<br />
Description=Wicd sleep hook<br />
Before=sleep.target<br />
StopWhenUnneeded=yes<br />
<br />
[Service]<br />
Type=oneshot<br />
RemainAfterExit=yes<br />
ExecStart=-/usr/share/wicd/daemon/suspend.py<br />
ExecStop=-/usr/share/wicd/daemon/autoconnect.py<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
* {{ic|1=<nowiki>RemainAfterExit=yes</nowiki>}}:服务启动后,除非显式地停止,否则就认为是活动的。<br />
* {{ic|1=<nowiki>StopWhenUnneeded=yes</nowiki>}}:服务活动时,如果无其他服务依赖该服务,就停止它。在本例中,该服务会在 sleep.target 停止后停止活动。<br />
* 由于 sleep.target 会被 suspend.target、hibernate.target、hybrid-sleep.target 调用,且 sleep.target 本身设置了 StopWhenUnneeded,该服务文件对以上各种操作都是有效的。<br />
<br />
===== 使用 /usr/lib/systemd/system-sleep =====<br />
<br />
systemd 在待机/休眠时执行 {{ic|/usr/lib/systemd/system-sleep/}} 里的所有脚本,传递下面两个参数:<br />
<br />
* 参数1:若是准备进入待机/休眠状态,则为 {{ic|pre}};唤醒时为 {{ic|post}}。<br />
* 参数2:事件名称,{{ic|suspend}},{{ic|hibernate}} 或 {{ic|hybrid-sleep}}。<br />
<br />
systemd 会同时执行所有脚本,而不是像 [[pm-utils (简体中文)|pm-utils]] 那样顺序执行。<br />
<br />
脚本输出会记录在相关服务({{ic|systemd-suspend.service}}、{{ic|systemd-hibernate.service}} 或 {{ic|systemd-hybrid-sleep.service}})中。通过[[#日志|日志]]查看:<br />
# journalctl -b -u systemd-suspend<br />
<br />
脚本示范:<br />
<br />
{{hc|/usr/lib/systemd/system-sleep/example.sh|<br />
#!/bin/sh<br />
case $1/$2 in<br />
pre/*)<br />
echo "进入 $2 状态..."<br />
;;<br />
post/*)<br />
echo "从 $2 状态唤醒..."<br />
;;<br />
esac}}<br />
<br />
记得添加可执行权限: <br />
# chmod a+x /usr/lib/systemd/system-sleep/example.sh<br />
<br />
详情参见 {{ic|man 7 systemd.special}} 和 {{ic|man 8 systemd-sleep}}。<br />
<br />
=== 临时文件 ===<br />
{{ic|/usr/lib/tmpfiles.d/}} 和 {{ic|/etc/tmpfiles.d/}} 中的文件描述了 systemd-tmpfiles 如何创建、清理、删除临时文件和目录,这些文件和目录通常存放在 {{ic|/run}} 和 {{ic|/tmp}} 中。配置文件名称为 {{ic|/etc/tmpfiles.d/<program>.conf}}。此处的配置能覆盖 {{ic|/usr/lib/tmpfiles.d/}} 目录中的同名配置。<br />
<br />
临时文件通常和服务文件同时提供,以生成守护进程需要的文件和目录。例如 [[Samba (简体中文)|Samba]] 服务需要目录 {{ic|/run/samba}} 存在并设置正确的权限位,就象这样:<br />
{{hc|/usr/lib/tmpfiles.d/samba.conf|<br />
D /run/samba 0755 root root<br />
}}<br />
<br />
此外,临时文件还可以用来在开机时向特定文件写入某些内容。比如,要禁止系统从USB设备唤醒,利用旧的 {{ic|/etc/rc.local}} 可以用 {{ic|echo USBE > /proc/acpi/wakeup}},而现在可以这么做:<br />
{{hc|/etc/tmpfiles.d/disable-usb-wake.conf|<br />
w /proc/acpi/wakeup - - - - USBE<br />
}}<br />
<br />
详情参见 {{ic|man 5 tmpfiles.d}}。<br />
<br />
=== 单元(unit) ===<br />
一个单元配置文件可以描述如下内容之一:系统服务、socket、系统设备、挂载点、交换分区/文件、启动目标(target)、文件系统路径、由 systemd 管理的计时器。文件格式受 .desktop 文件启发,而最初起源是 Windows 下的 .ini 文件。<br />
<br />
详情参阅 {{ic|man 5 systemd.unit}}.<br />
<br />
== 从 initscripts 迁移到 systemd ==<br />
<br />
=== initscripts 模拟 ===<br />
<br />
{{Pkg|initscripts}} 软件包提供了 systemd 和 Arch 传统配置的整合。当同时安装了{{Pkg|initscripts}}和systemd时,若以systemd启动系统,systemd将执行以下操作:<br />
<br />
# 解析{{ic|/etc/rc.conf}}中的{{ic|DAEMONS}}数组,并在系统启动时,启动所有列出的守护进程<br />
# 在启动过程中执行{{ic|/etc/rc.local}}<br />
# 在系统关闭过程中执行{{ic|/etc/rc.local.shutdown}}<br />
<br />
Initscripts模拟仅仅是作为用户迁移到systemd时的过渡措施,这一方案最终将'''不再可行'''。原生的systemd并不依赖于{{ic|rc.conf}}文件的集中式配置,因此推荐使用[[#原生 systemd 配置文件|原生的systemd配置文件]]。原生systemd配置文件比{{ic|/etc/rc.conf}}具有更高优先级。<br />
<br />
{{注意|替换{{ic|/etc/rc.local}}的推荐方式是针对系统启动时要运行的程序编写自定义的service文件,详见相关[[#自己编写_.service_文件|章节]].}}<br />
<br />
{{注意|如果你曾经在{{ic|/etc/inittab}}中禁用了以{{keypress|Ctrl+Alt+Del}}重启系统,则需要以root执行{{ic|systemctl mask ctrl-alt-del.target}}以使其对systemd重新生效。}}<br />
<br />
{{警告|如果同时安装 systemd(197-4 及后续版本)和 initscripts,那么 {{ic|/etc/rc.local}} 脚本会导致启动过程无法结束(原因是 getty@tty1.service 自 197-4 版本起不再等待 rc-local.service 执行,getty 无法启动)。请删除或重命名 {{ic|/etc/rc.local}}。}}<br />
<br />
=== 迁移 DAEMONS 列表 ===<br />
<br />
若要用纯 systemd 环境,则应当删除 {{ic|/etc/rc.conf}} 文件,并完全通过 {{ic|systemctl}} 命令来启动服务。对于 {{ic|DAEMONS}} 列表里面的每个 {{ic|<服务名>}},现在可以用以下命令设置成自动启动:<br />
<br />
# systemctl enable <服务名>.service<br />
<br />
{{小贴士|[[Daemons List|这里]]有一份 initscripts 和 systemd 下常见服务的对应列表。}}<br />
<br />
如果提示 {{ic|<service_name>.service}} 不存在:<br />
<br />
* 很有可能是 systemd 用了不同的服务名。比如 {{ic|crond}} 服务替换成 {{ic|cronie.service}};{{ic|alsa}} 服务替换成了 {{ic|alsa-store.service}} 和 {{ic|alsa-restore.service}} 两个服务。另一个值得注意的是 {{ic|network}} 服务,它被替换为一整套服务文件(详情参见 [[Configuring_Network_(简体中文)]])。<br />
* 另外,有些程序尚未提供 .service 文件,还得继续用 {{ic|rc.conf}} 来配置开机自动启动。<br />
<br />
{{小贴士|用下面这个命令来查看已安装软件包包含的服务文件:<br />
$ pacman -Ql cronie<br />
[...]<br />
cronie /etc/rc.d/crond # DAEMONS 列表中使用的 initscript 服务 (原生 systemd 配置中不再使用)<br />
[...]<br />
cronie /usr/lib/systemd/system/cronie.service # 原生 systemd 服务文件<br />
[...]<br />
}}<br />
<br />
* 有些服务不需要用户明确配置自启动。比如安装 {{ic|dbus-core}} 软件包后 {{ic|dbus.service}} 就会自动被配置为自启动。而 {{ic|alsa-store.service}} 和 {{ic|alsa-restore.service}} 也会由 systemd 自动启动。可以用 {{ic|systemctl list-unit-files}} 命令来查看服务列表并检查运行状态。<br />
<br />
== systemd 基本工具 ==<br />
<br />
检视和控制systemd的主要命令是{{ic|systemctl}}。该命令可用于查看系统状态和管理系统及服务。详见{{ic|man 1 systemctl}}。<br />
<br />
{{小贴士|在 {{ic|systemctl}} 参数中添加 {{ic|-H <用户名>@<主机名>}} 可以实现对其他机器的远程控制。该过程使用 [[SSH (简体中文)|SSH]] 链接。}}<br />
<br />
{{注意|{{ic|systemadm}} 是 systemd 的官方图形前端。由 [[AUR (简体中文)|AUR]] 中的软件包 {{AUR|systemd-ui-git}} 提供。}}<br />
<br />
=== 分析系统状态 ===<br />
<br />
输出激活的单元:<br />
<br />
$ systemctl<br />
<br />
以下命令等效:<br />
<br />
$ systemctl list-units<br />
<br />
输出运行失败的单元:<br />
<br />
$ systemctl --failed<br />
<br />
所有可用的单元文件存放在 {{ic|/usr/lib/systemd/system/}} 和 {{ic|/etc/systemd/system/}} 目录(后者优先级更高)。查看所有已安装服务:<br />
$ systemctl list-unit-files<br />
<br />
=== 使用单元 ===<br />
一个单元可以是系统服务({{ic|.service}})、挂载点({{ic|.mount}})、sockets({{ic|.sockets}})。<br />
<br />
使用 {{ic|systemctl}} 控制单元时,通常需要使用单元文件的全名,包括扩展名(例如 {{ic|sshd.service}})。但是有些单元可以在{{ic|systemctl}}中使用简写方式。<br />
* 如果无扩展名,systemctl 默认把扩展名当作 {{ic|.service}}。例如 {{ic|netcfg}} 和 {{ic|netcfg.service}} 是等价的。<br />
* 挂载点会自动转化为相应的 {{ic|.mount}} 单元。例如 {{ic|/home}} 等价于 {{ic|home.mount}}。<br />
* 设备会自动转化为相应的 {{ic|.device}} 单元,所以 {{ic|/dev/sda2}} 等价于 {{ic|dev-sda2.device}}。<br />
<br />
立即激活单元:<br />
<br />
# systemctl start <单元><br />
<br />
立即停止单元:<br />
# systemctl stop <单元><br />
<br />
重启单元:<br />
<br />
# systemctl restart <单元><br />
<br />
命令单元重新读取配置:<br />
<br />
# systemctl reload <单元><br />
<br />
输出单元运行状态:<br />
<br />
$ systemctl status <单元><br />
<br />
检查单元是否配置为自动启动:<br />
$ systemctl is-enabled <单元><br />
<br />
开机自动激活单元:<br />
<br />
# systemctl enable <单元><br />
<br />
{{注意|如果服务没有{{ic|Install}}段落,一般意味着应该通过其它服务自动调用它们。如果真的需要手动安装,可以直接连接服务,如下(将{{ic|foo}}替换为真实的服务名):<br />
{{bc|# ln -s /usr/lib/systemd/system/foo.service /etc/systemd/system/graphical.target.wants/}}<br />
}}<br />
<br />
取消开机自动激活单元:<br />
<br />
# systemctl disable <单元>}}<br />
<br />
显示单元的手册页(必须由单元文件提供):<br />
<br />
# systemctl help <单元><br />
<br />
重新载入 systemd,扫描新的或有变动的单元:<br />
# systemctl daemon-reload<br />
<br />
=== 电源管理 ===<br />
<br />
安装 {{ic|polkit}} 后才可使用电源管理。<br />
<br />
如果你正登录在一个本地的{{ic|systemd-logind}}用户会话,且当前没有其它活动的会话,那么以下命令无需root权限即可执行。否则(例如,当前有另一个用户登录在某个tty),systemd 将会自动请求输入root密码。<br />
<br />
重启:<br />
<br />
$ systemctl reboot<br />
<br />
退出系统并停止电源:<br />
<br />
$ systemctl poweroff<br />
<br />
待机:<br />
<br />
$ systemctl suspend<br />
<br />
休眠:<br />
<br />
$ systemctl hibernate<br />
<br />
混合休眠模式(同时休眠到硬盘并待机):<br />
<br />
$ systemctl hybrid-sleep<br />
<br />
== 启动桌面环境 ==<br />
<br />
=== 使用登录管理器 ===<br />
<br />
通过启动[[Display Manager (简体中文)|登录管理器]](或称显示管理器),即可进行图形界面登录。目前,Arch 提供了 [[GDM (简体中文)|GDM]]、[[KDM (简体中文)|KDM]]、[[SLiM (简体中文)|SLiM]]、[[XDM (简体中文)|XDM]]、[[LXDM (简体中文)|LXDM]] 和 [[LightDM (简体中文)|LightDM]] 的 systemd 服务文件。以 KDM 为例,配置开机启动:<br />
# systemctl enable kdm.service<br />
<br />
执行上述命令后,登录管理器应当能正常工作了。如果不是的话,很可能是因为你修改了{{ic|default.target}}。默认情况应当如下:<br />
<br />
{{hc|# ls -l /etc/systemd/system/default.target|/etc/systemd/system/default.target -> /usr/lib/systemd/system/graphical.target}}<br />
<br />
删除被修改的 {{ic|default.target}} 即可,systemd 会自动使用默认配置(即 {{ic|graphical.target}}):<br />
<br />
# rm /etc/systemd/system/default.target<br />
<br />
=== 使用 systemd-logind ===<br />
<br />
{{注意|自 2012-10-30 起, [[ConsoleKit]] 已被 [https://www.archlinux.org/news/consolekit-replaced-by-logind/ systemd-logind 取代] ,作为登录到桌面环境的默认方式。}}<br />
<br />
可使用 {{ic|loginctl}} 来查看用户会话的状态。所有 [[PolicyKit]] 操作,如挂起系统、挂载外部驱动器,都无需配置即可使用。<br />
<br />
$ loginctl show-session $XDG_SESSION_ID<br />
<br />
== 自己编写 .service 文件 ==<br />
''参见:[[Systemd/Services]]''<br />
<br />
=== 处理依赖关系 ===<br />
<br />
使用systemd时,可通过正确编写单元配置文件来解决其依赖关系。典型的情况是,单元{{ic|A}}要求单元{{ic|B}}在{{ic|A}}启动之前运行。在此情况下,向单元{{ic|A}}配置文件中的 {{ic|[Unit]}} 段添加 {{ic|1=Requires=B}} 和 {{ic|1=After=B}} 即可。若此依赖关系是可选的,可添加 {{ic|1=Wants=B}} 和 {{ic|1=After=B}}。请注意 {{ic|1=Wants=}} 和 {{ic|1=Requires=}} 并不意味着 {{ic|1=After=}},即如果 {{ic|1=After=}} 选项没有制定,这两个单元将被并行启动。<br />
<br />
依赖关系通常被用在服务(service)而不是[[#目标(target)|目标(target)]]上。例如, {{ic|network.target}} 一般会被某个配置网络接口的服务引入,所以,将自定义的单元排在该服务之后即可,因为 {{ic|network.target}} 已经启动。<br />
<br />
=== 启动方式 ===<br />
<br />
编写自定义的service文件时,可以选择几种不同的服务启动方式。启动方式可通过配置文件 {{ic|[Service]}} 段中的 {{ic|1=Type=}} 参数进行设置。具体的参数说明请参阅 {{ic|man systemd.service}} 。<br />
<br />
* {{ic|1=Type=simple}}:systemd认为该服务将立即启动。服务进程不会fork。如果该服务要启动其他服务,不要使用此类型启动,除非该服务是socket激活型。<br />
* {{ic|1=Type=forking}}:systemd认为当该服务进程fork,且父进程退出后服务启动成功。对于常规的守护进程(daemon),除非你确定此启动方式无法满足需求,使用此类型启动即可。使用此启动类型应同时指定 {{ic|1=PIDFile=}},以便systemd能够跟踪服务的主进程。<br />
* {{ic|1=Type=oneshot}}:这一选项适用于只执行一项任务随后退出的服务。你可能需要同时设置 {{ic|1=RemainAfterExit=yes}} 使得 systemd 在服务进程退出之后仍然认为服务处于激活状态。<br />
* {{ic|1=Type=notify}}:与 {{ic|1=Type=simple}} 相同,但约定服务会在就绪后向 systemd 发送一个信号。这一通知的实现由 {{ic|libsystemd-daemon.so}} 提供。<br />
* {{ic|1=Type=dbus}}:若以此方式启动,当指定的 {{ic|BusName}} 出现在DBus系统总线上时,systemd认为服务就绪。<br />
<br />
=== 替换现存单元文件 ===<br />
<br />
目录 {{ic|/etc/systemd/system/}} 中的单元配置文件比 {{ic|/usr/lib/systemd/system/}} 目录下的文件拥有更高优先级。<br />
若需要定制某个单元的启动(使配置不受软件包更新的影响),请将现有的单元配置文件从 {{ic|/usr/lib/}} 复制到 {{ic|/etc/}} 目录下,并在此目录中修改。或者,可以使用 {{ic|.include}} 来解析一个现有的配置文件,并覆盖原选项或添加新选项。例如,若想要某个服务的配置文件中添加额外依赖关系,可以编写:<br />
<br />
{{hc|/etc/systemd/system/<服务名>.service|2=<br />
.include /usr/lib/systemd/system/<服务名>.service<br />
<br />
[Unit]<br />
Requires=<新依赖><br />
After=<新依赖>}}<br />
<br />
然后运行以下命令使更改生效:<br />
<br />
# systemctl reenable <单元><br />
# systemctl restart <单元><br />
<br />
{{小贴士|可以用 {{ic|systemd-delta}} 命令来查看哪些单元文件被覆盖、哪些被修改。}}<br />
<br />
=== 单元配置文件的 vim 语法高亮支持 ===<br />
<br />
可从 [[Official Repositories|官方仓库]] 安装 {{Pkg|vim-systemd}} 软件包,使 unit 配置文件在 [[Vim]] 下支持语法高亮。<br />
<br />
== 目标(target) ==<br />
启动级别(runlevel)是一个旧的概念。现在,systemd 引入了一个和启动级别功能相似又不同的概念——目标(target)。不像数字表示的启动级别,每个''目标''都有名字和独特的功能,并且能同时启用多个。一些''目标''继承其他''目标''的服务,并启动新服务。systemd 提供了一些模仿 sysvinit 启动级别的''目标'',仍可以使用旧的 {{ic|telinit 启动级别}} 命令切换。<br />
<br />
=== 获取当前目标 ===<br />
不要使用 {{ic|runlevel}} 命令了:<br />
$ systemctl list-units --type=target<br />
<br />
=== 创建新目标 ===<br />
在 Fedora 中,启动级别 0、1、3、5、6 都被赋予特定用途,并且都对应一个 systemd 的''目标''。然而,没有什么很好的移植用户定义的启动级别(2、4)的方法。要实现类似功能,可以以原有的启动级别为基础,创建一个新的''目标'' {{ic|/etc/systemd/system/<新目标>}}(可以参考 {{ic|/usr/lib/systemd/system/graphical.target}}),创建 {{ic|/etc/systemd/system/<新目标>.wants}} 目录,向其中加入额外服务的链接(指向 {{ic|/usr/lib/systemd/system/}} 中的单元文件)。<br />
<br />
=== 目标表 ===<br />
{| border="1"<br />
!SysV 启动级别!!Systemd 目标!!注释<br />
|-<br />
| 0 || runlevel0.target, poweroff.target || 中断系统(halt)<br />
|-<br />
| 1, s, single || runlevel1.target, rescue.target || 单用户模式<br />
|-<br />
| 2, 4 || runlevel2.target, runlevel4.target, multi-user.target || 用户自定义启动级别,通常识别为级别3。<br />
|-<br />
| 3 || runlevel3.target, multi-user.target || 多用户,无图形界面。用户可以通过终端或网络登录。<br />
|-<br />
| 5 || runlevel5.target, graphical.target || 多用户,图形界面。继承级别3的服务,并启动图形界面服务。<br />
|-<br />
| 6 || runlevel6.target, reboot.target || 重启<br />
|-<br />
| emergency || emergency.target || 急救模式(Emergency shell)<br />
|-<br />
|}<br />
<br />
=== 切换启动级别/目标 ===<br />
systemd 中,启动级别通过“目标单元”访问。通过如下命令切换:<br />
# systemctl isolate graphical.target<br />
该命令对下次启动无影响。等价于{{ic|telinit 3}} 或 {{ic|telinit 5}}。<br />
<br />
=== 修改默认启动级别/目标 ===<br />
开机启动进的目标是 {{ic|default.target}},默认链接到 {{ic|graphical.target}} (大致相当于原来的启动级别5)。可以通过[[Kernel parameters (简体中文)|内核参数]]更改默认启动级别:<br />
<br />
{{小贴士|可以省略扩展名 {{ic|.target}}。}}<br />
<br />
* {{ic|1=systemd.unit=multi-user.target}} (大致相当于级别3)<br />
* {{ic|1=systemd.unit=rescue.target}} (大致相当于级别1)<br />
<br />
另一个方法是修改 {{ic|default.target}}。可以通过 {{ic|systemctl}} 修改它:<br />
<br />
# systemctl enable multi-user.target<br />
<br />
命令执行情况由 {{ic|systemctl}} 显示:链接 {{ic|/etc/systemd/system/default.target}} 被创建,指向新的默认启动级别。该方法当且仅当目标配置文件中有以下内容时有效:<br />
<br />
[Install]<br />
Alias=default.target<br />
<br />
目前,{{ic|multi-user.target}}、{{ic|graphical.target}} 都包含这段内容。<br />
<br />
== 日志 ==<br />
自版本 38 起,systemd 提供了自己日志系统(logging system),称为 journal. 使用 systemd 日志,无需额外安装日志服务(syslog)。读取日志的命令:<br />
# journalctl<br />
<br />
默认情况下(当 {{ic|Storage&#61;}} 在文件 {{ic|/etc/systemd/journald.conf}} 中被设置为 {{ic|auto}}),日志记录将被写入 {{ic|/var/log/journal/}}。该目录是 {{pkg|systemd}} 软件包的一部分。若被删除,systemd '''不会'''自动创建它,直到下次升级软件包时重建该目录。如果该目录缺失,systemd 会将日志记录写入 {{ic|/run/systemd/journal}}。这意味着,系统重启后日志将丢失。<br />
<br />
=== 过滤输出 ===<br />
<br />
{{ic|journalctl}}可以根据特定字段过滤输出,例如:<br />
<br />
显示本次启动后的所有日志:<br />
<br />
# journalctl -b<br />
<br />
不过,一般大家更关心的不是本次启动后的日志,而是上次启动时的(例如,刚刚系统崩溃了)。目前还没有这项功能,正在 [http://comments.gmane.org/gmane.comp.sysutils.systemd.devel/6608 systemd-devel@lists.freedesktop.org] 讨论中。<br />
<br />
目前的折中方案是:<br />
<br />
# journalctl --since=today | tac | sed -n '/-- Reboot --/{n;:r;/-- Reboot --/q;p;n;b r}' | tac<br />
<br />
以上命令输出本日内的所有启动信息。但要注意,如果日志很多,该命令执行时间会比较漫长。<br />
<br />
动态跟踪最新信息:<br />
# journalctl -f<br />
<br />
显示特定程序的所有消息:<br />
{{bc|# journalctl /usr/lib/systemd/systemd}}<br />
<br />
显示特定进程的所有消息:<br />
{{bc|1=# journalctl _PID=1}}<br />
<br />
显示指定单元的所有消息:<br />
# journalctl -u netcfg<br />
<br />
详情参阅{{ic|man journalctl}}、{{ic|man systemd.journal-fields}},以及 Lennert 的这篇[http://0pointer.de/blog/projects/journalctl.html 博文]。<br />
<br />
=== 日志大小限制 ===<br />
<br />
如果按上面的操作保留日志的话,默认日志最大限制为所在文件系统容量的 10%,即:如果 {{ic|/var/log/journal}} 储存在 50GiB 的根分区中,那么日志最多存储 5GiB 数据。可以修改 {{ic|/etc/systemd/journald.conf}} 中的 {{ic|SystemMaxUse}} 来指定该最大限制。如限制日志最大 50MiB:<br />
<br />
SystemMaxUse=50M<br />
<br />
详情参见 {{ic|man journald.conf}}.<br />
<br />
=== 配合 syslog 使用 ===<br />
systemd 提供了 socket {{ic|/run/systemd/journal/syslog}},以兼容传统日志服务。所有系统信息都会被传入。要使传统日志服务工作,需要让服务链接该 socket,而非 {{ic|/dev/log}}([http://lwn.net/Articles/474968/ 官方说明])。Arch 软件仓库中的 {{pkg|syslog-ng}} 已经包含了需要的配置。<br />
<br />
设置开机启动 syslog-ng:<br />
# systemctl enable syslog-ng<br />
<br />
[http://0pointer.de/blog/projects/ 这里]有一份很不错的 {{ic|journalctl}} 指南。<br />
<br />
== 优化 ==<br />
<br />
参阅:[[Improve Boot Performance (简体中文)]]。<br />
<br />
=== 启动过程分析 ===<br />
systemd 提供了一个分析启动过程的工具 —— {{ic|systemd-analyze}}。可以用它看看哪些单元拖慢了开机过程,并据此进行优化。安装 {{Pkg|python2-dbus}}、{{Pkg|python2-cairo}} 软件包后该工具才能工作。<br />
<br />
查看开机过程在内核/用户空间消耗的时间:<br />
$ systemd-analyze<br />
{{小贴士|如果在 {{ic|/etc/mkinitcpio.conf}} 的 {{ic|HOOKS}} 数组添加 {{ic|timestamp}},并重新生成启动内存镜像({{ic|mkinitcpio -p linux}}),{{ic|systemd-analyze}} 还可以显示处理该启动镜像花费的时间。}}<br />
<br />
按照耗费时间顺序,输出启动每个单元耗费的时间:<br />
$ systemd-analyze blame<br />
<br />
生成类似于 [[Bootchart (简体中文)|bootchart]] 的开机过程图表:<br />
$ systemd-analyze plot > plot.svg<br />
<br />
==== 使用 systemd-bootchart ====<br />
<br />
自2012年10月17日,bootchart 工具已经合并进 systemd 中,使用方法和原来的 bootchart 大同小异,添加下列内容到内核参数即可:<br />
<br />
initcall_debug printk.time=y init=/usr/lib/systemd/systemd-bootchart<br />
<br />
==== 使用 bootchart2 ====<br />
<br />
由于没有办法在内核参数设置两个 init,所以不能使用源里的 bootchart。不过,[[AUR (简体中文)|AUR]] 软件包 {{AUR|bootchart2}} 提供了一个的 systemd 服务,这样就可以和 systemd 一起使用。安装后启用即可:<br />
{{bc|# systemctl enable bootchart.service}}<br />
详情参阅 [https://github.com/mmeeks/bootchart bootchart 文档]。<br />
<br />
=== 预读 ===<br />
systemd 自己实现了一个 readahead,可以用来提高开机效率。不过,效果会因内核版本和硬件情况而不同(极少数情况下还会变慢)。开启 readahead:<br />
<br />
# systemctl enable systemd-readahead-collect.service systemd-readahead-replay.service<br />
<br />
要知道,readahead 的超级牛力只有在重启几次后才会显现。<br />
<br />
== 疑难解答 ==<br />
<br />
=== 关机/重启十分缓慢 ===<br />
<br />
如果关机特别慢(甚至跟死机了一样),很可能是某个拒不退出的服务在作怪。systemd 会等待一段时间,然后再尝试杀死它。请阅读[http://freedesktop.org/wiki/Software/systemd/Debugging#Shutdown_Completes_Eventually 这篇文章],确认你是否是该问题受害者。<br />
<br />
=== 短时进程无日志记录 ===<br />
<br />
若 {{ic|journalctl -u foounit.service}} 没有显示某个短时进程的任何输出,那么改用 PID 试试。例如,若 systemd-modules-load.service 执行失败,那么先用 {{ic|systemctl status systemd-modules-load}} 查询其 PID(比如是123),然后检索该 PID 相关的日志 {{ic|journalctl -b _PID&#61;123}}。运行时进程的日志元数据(诸如 _SYSTEMD_UNIT 和 _COMM)被乱序收集在 {{ic|/proc}} 目录。要修复该问题,必须修改内核,使其通过套接字连接来提供上述数据,该过程类似于 SCM_CREDENTIALS。<br />
<br />
== 相关资源 ==<br />
<br />
*[http://www.freedesktop.org/wiki/Software/systemd 官方网站]<br />
*[http://0pointer.de/public/systemd-man/ man 手册页]<br />
*[http://freedesktop.org/wiki/Software/systemd/Optimizations systemd 优化]<br />
*[http://www.freedesktop.org/wiki/Software/systemd/FrequentlyAskedQuestions 常见问题 FAQ]<br />
*[http://www.freedesktop.org/wiki/Software/systemd/TipsAndTricks 小技巧]<br />
*[http://0pointer.de/public/systemd-ebook-psankar.pdf systemd for Administrators (PDF)]<br />
*[http://fedoraproject.org/wiki/Systemd Fedora 项目对 systemd 的介绍]<br />
*[http://fedoraproject.org/wiki/How_to_debug_Systemd_problems 如何调试 systemd 故障]<br />
*''The H Open'' 杂志上的[http://www.h-online.com/open/features/Control-Centre-The-systemd-Linux-init-system-1565543.html 两][http://www.h-online.com/open/features/Booting-up-Tools-and-tips-for-systemd-1570630.html 篇]科普文章<br />
*[http://0pointer.de/blog/projects/systemd.html Lennart 的博文]<br />
*[http://0pointer.de/blog/projects/systemd-update.html 更新报告]<br />
*[http://0pointer.de/blog/projects/systemd-update-2.html 更新报告2]<br />
*[http://0pointer.de/blog/projects/systemd-update-3.html 更新报告3]<br />
*[http://0pointer.de/blog/projects/why.html 最新动态]<br />
*[https://fedoraproject.org/wiki/SysVinit_to_Systemd_Cheatsheet/zh Fedora Wiki 上的 SysVinit 和 systemd 命令对比表]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Advanced_Linux_Sound_Architecture_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=250601
Advanced Linux Sound Architecture (简体中文)
2013-03-14T07:45:34Z
<p>Cuihao: 翻译中</p>
<hr />
<div>[[Category:简体中文]]<br />
[[Category:Sound (简体中文)]]<br />
[[Category:Audio/Video (简体中文)]]<br />
[[cs:Advanced Linux Sound Architecture]]<br />
[[de:Alsa]]<br />
[[es:Advanced Linux Sound Architecture]]<br />
[[fa:ALSA]]<br />
[[fr:Alsa]]<br />
[[he:Advanced Linux Sound Architecture]]<br />
[[it:Advanced Linux Sound Architecture]]<br />
[[ja:Advanced Linux Sound Architecture]]<br />
[[nl:Advanced Linux Sound Architecture]]<br />
[[pt:Advanced Linux Sound Architecture]]<br />
[[ru:Advanced Linux Sound Architecture]]<br />
[[sk:Advanced Linux Sound Architecture]]<br />
[[th:Advanced Linux Sound Architecture]]<br />
[[tr:Gelişmiş_ses_mimarisi_(ALSA)]]<br />
[[en:Advanced Linux Sound Architecture]]<br />
<br />
{{Article summary start}}<br />
{{Article summary text|ALSA 的使用、配置及疑难解答。}}<br />
{{Article summary heading|相关页面}}<br />
{{Article summary wiki|Sound (简体中文)}}<br />
{{Article summary wiki|Disable PC Speaker Beep (简体中文)}}<br />
{{Article summary end}}<br />
<br />
{{TranslationStatus (简体中文)|Systemd|2013-03-14|250172}}<br />
<br />
[[Wikipedia:zh:ALSA|高级 Linux 声音体系]](Advanced Linux Sound Architecture,'''ALSA''')是Linux中提供声音设备驱动的内核组件,用来代替原来的开放声音系统(Open Sound System,OSSv3)。除了声音设备驱动,'''ALSA'''还包含一个用户空间的函数库,以方便开发者通过高级API使用驱动功能,而不必直接与内核驱动交互。<br />
<br />
{{注意|关于另一种声音体系,请阅读[[Open Sound System (简体中文)|开放声音系统(OSS)]]的页面。}}<br />
<br />
==安装==<br />
Arch 默认的内核已经通过一套模块提供了 ALSA,不必特别安装。<br />
<br />
[[udev (简体中文)|udev]]会在系统启动时自动检测硬件,并加载相应的声音设备驱动模块。这时,你的声卡已经可以工作了,只是所有声道默认都被设置成静音了。<br />
<br />
在本地登录(通过虚拟终端或登录管理器)的用户,都有权限播放音频并调整音量。要让远程登录的用户拥有这些权限,必须把用户[[Users and Groups (简体中文)#用户组管理|加入]] {{ic|audio}} 用户组。该组的成员可以直接访问声音设备,会导致某些程序独占音频输出(破坏软件混音),还可能影响用户快速切换和拖机(multiseat)。因此,除非真的有某些[https://wiki.ubuntu.com/Audio/TheAudioGroup 特殊需求],'''不建议'''把用户加入 {{ic|audio}} 用户组。<br />
<br />
===用户空间工具===<br />
从 [[Official Repositories (简体中文)|官方仓库]]:<br />
* [[pacman (简体中文)|安装]] {{Pkg|alsa-utils}} 软件包,其中的 {{ic|alsamixer}} 工具允许用户在控制台或终端中配置声音设备。<br />
* 安装 {{Pkg|alsa-oss}} 软件包,如果想让 OSS 应用程序与dmix(软件混音)协同工作。 <br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。}}<br />
*安装 {{Pkg|alsa-plugins}} 软件包,如果需要 [[#高质量重采样|高质量重采样]]、[[#Upmixing/Downmixing|软件模拟环绕立体声]]和其他高级特性。<br />
<br />
==解除各声道的静音==<br />
目前版本的 ALSA 安装后,所有声道'''默认是静音的''',必须手动解除。<br />
<br />
使用 {{ic|alsamixer}} 的 ncurses 界面,配置十分简单:<br />
<br />
$ alsamixer<br />
<br />
此外,还可以在命令行下使用 {{ic|amixer}}:<br />
<br />
$ amixer sset Master unmute<br />
<br />
下方标有 {{ic|MM}} 的声道是静音的,而标有 {{ic|00}} 的通道已经启用。<br />
<br />
使用左右方向键,选中 {{ic|Master}} 和 {{ic|PCM}} 声道。按下 {{keypress|m}} 键解除静音。使用上方向键增加音量,直到增益值为{{ic|0}}。该值显示在左上方 {{ic|Item:}} 字段后。过高的增益值会导致声音失真。<br />
<br />
要想得到完整的 5.1 或 7.1 环绕立体声,还得解除 Front、Surround、Center、LFE (subwoofer) 和 Side 这些声道的静音(上述名称是 Intel HD Audio 声卡使用的声道名,可能因设备不同而有所差异)。注意,仅有这些设置,系统不会自动将立体声源(多数音乐)提升(upmix)成环绕立体声。如果需要这些功能,请阅读[[#Upmixing/Downmixing]]。<br />
<br />
按下 {{Keypress|Esc}} 键退出 alsamixer。<br />
<br />
{{注意|<br />
* 有一些声卡,需要关闭数字输出或将其调成静音,才能输出模拟音频信号。对于 Soundblaster Audigy LS 声卡,需要把 IEC958 通道设成静音。 <br />
* 有些机器(如 Thinkpad T61),有一个 Speaker 通道,必须按照上述方法打开调整。<br />
* 有些机器(如 Dell E6400),可能还需要把 {{ic|Front}} 和 {{ic|Headphone}} 声道打开调整。}}<br />
<br />
接下来,测试声卡是否工作:<br />
<br />
$ speaker-test -c 2<br />
<br />
根据扬声器的实际情况,调整 -c 后的数值。对于 7.1 声道,这个数字是 8:<br />
<br />
$ speaker-test -c 8<br />
<br />
如果没有正常工作,请继续阅读 [[#配置]] 以及 [[#疑难解答]] 部分。<br />
<br />
{{pkg|alsa-utils}} 软件包提供了两个配置 alsa 的服务:{{ic|alsa-restore.service}} 和 {{ic|alsa-store.service}}。它们分别在开机和关机时自动运行。<br />
<br />
==配置==<br />
=== VirtualBox 虚拟机无声音 ===<br />
<br />
如果使用 VirtualBox 时碰上了麻烦,请尝试以下命令:<br />
<br />
{{hc|$ alsactl init|2=<nowiki><br />
<br />
Found hardware: "ICH" "SigmaTel STAC9700,83,84" "AC97a:83847600" "0x8086" "0x0000"<br />
Hardware is initialized using a generic method<br />
</nowiki><br />
}}<br />
<br />
如果还是不行,请检查所使用的音频软件是否激活了 ALSA 输出。<br />
<br />
===设置默认声卡===<br />
<br />
如果发现开机时声卡次序会发生变化,可以在通过 {{ic|/etc/modprobe.d}} 中的 {{ic|.conf}} 文件(比如 {{ic|/etc/modprobe.d/alsa-base.conf}})手动设置次序。<br />
比如,要让 mia 声卡成为 #0、Intel HDA 声卡成为 #1:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<nowiki><br />
options snd slots=snd_mia,snd_hda_intel<br />
options snd_mia index=0<br />
options snd_hda_intel index=1<br />
</nowiki>}}<br />
<br />
snd_mia 和 snd_hda_intel 分别是两个声卡的内核驱动模块。该配置仅适用于同时装有 mia 声卡和使用 snd_hda_intel 驱动的声卡(通常是板载声卡)的情况。<br />
<br />
如果使用 -2 的 index 值,ALSA 就不会将对应的设备作为主声卡使用。Linux Mint 和 Ubuntu 等发行版使用了以下配置,避免 USB 声卡和其他“非主流”设备变成 index 为 0 的主声卡:<br />
<br />
{{hc|/etc/modprobe.d/alsa-base.conf|2=<nowiki><br />
options bt87x index=-2<br />
options cx88_alsa index=-2<br />
options saa7134-alsa index=-2<br />
options snd-atiixp-modem index=-2<br />
options snd-intel8x0m index=-2<br />
options snd-via82xx-modem index=-2<br />
options snd-usb-audio index=-2<br />
options snd-usb-caiaq index=-2<br />
options snd-usb-ua101 index=-2<br />
options snd-usb-us122l index=-2<br />
options snd-usb-usx2y index=-2<br />
# Keep snd-pcsp from being loaded as first soundcard<br />
options snd-pcsp index=-2<br />
# Keep snd-usb-audio from beeing loaded as first soundcard<br />
options snd-usb-audio index=-2<br />
</nowiki>}}<br />
<br />
以上配置会在系统重启后生效。<br />
<br />
{{Out of date|据英文 Wiki 用户反应,{{ic|/etc/asound.conf}} 这个配置文件默认是不存在的,下列操作可能有误。}}<br />
<br />
首先,运行 {{ic|aplay -l}},获取声卡的声卡ID和设备ID:<br />
<br />
{{hc|$ aplay -l|2=<nowiki><br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: Intel [HDA Intel], device 0: CONEXANT Analog [CONEXANT Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: Intel [HDA Intel], device 1: Conexant Digital [Conexant Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 1: JamLab [JamLab], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 2: Audio [Altec Lansing XT1 - USB Audio], device 0: USB Audio [USB Audio]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
</nowiki>}}<br />
<br />
拿输出中的最后一项来说,这个声卡的声卡ID为2,设备ID为0。若要把这块声卡作为默认声卡,可以把下列配置添加到系统级别的 {{ic|/etc/asound.conf}} 或用户级别的 {{ic|~/.asoundrc}} 文件。如果文件不存在,可以手动创建。其中的各个ID,请根据实际情况调整:<br />
{{hc|~/.asoundrc|2=<nowiki><br />
defaults.pcm.card 2<br />
defaults.pcm.device 0<br />
defaults.ctl.card 2<br />
</nowiki>}}<br />
“pcm”选项决定用来播放音频的设备,而“ctl”选项决定那个声卡能够由控制工具(如 alsamixer)使用。<br />
<br />
上述配置在重启音频程序(如 mplayer)后立即生效。<br />
<br />
===确认所有声音模块都已经加载===<br />
<br />
一般 udev 都会自动识别出声卡。使用以下命令确认:<br />
<br />
{{hc|<nowiki>$ lsmod | grep '^snd' | column -t</nowiki>|<nowiki><br />
snd_hda_codec_hdmi 22378 4<br />
snd_hda_codec_realtek 294191 1<br />
snd_hda_intel 21738 1<br />
snd_hda_codec 73739 3 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel<br />
snd_hwdep 6134 1 snd_hda_codec<br />
snd_pcm 71032 3 snd_hda_codec_hdmi,snd_hda_intel,snd_hda_codec<br />
snd_timer 18992 1 snd_pcm<br />
snd 55132 9 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel,snd_hda_codec,snd_hwdep,snd_pcm,snd_timer<br />
snd_page_alloc 7017 2 snd_hda_intel,snd_pcm<br />
</nowiki>}}<br />
<br />
如果你的输出和上面类似,那就说明声卡已经被正确识别。<br />
{{注意|1=从[[udev (简体中文)|udev]]的171版本开始,默认情况下 OSS 仿真模块({{ic|snd_seq_oss}}、{{ic|snd_pcm_oss}}、{{ic|snd_mixer_oss}})不再自动加载。如有需要,请[[Kernel_modules_(简体中文)#装入|手动加载]]。}}<br />
<br />
还可以检查一下 {{ic|/dev/snd/}} 目录,看看是否有这些设备文件:<br />
<br />
{{hc|$ ls -l /dev/snd|2=<nowiki><br />
total 0<br />
crw-rw---- 1 root audio 116, 0 Apr 8 14:17 controlC0<br />
crw-rw---- 1 root audio 116, 32 Apr 8 14:17 controlC1<br />
crw-rw---- 1 root audio 116, 24 Apr 8 14:17 pcmC0D0c<br />
crw-rw---- 1 root audio 116, 16 Apr 8 14:17 pcmC0D0p<br />
crw-rw---- 1 root audio 116, 25 Apr 8 14:17 pcmC0D1c<br />
crw-rw---- 1 root audio 116, 56 Apr 8 14:17 pcmC1D0c<br />
crw-rw---- 1 root audio 116, 48 Apr 8 14:17 pcmC1D0p<br />
crw-rw---- 1 root audio 116, 1 Apr 8 14:17 seq<br />
crw-rw---- 1 root audio 116, 33 Apr 8 14:17 timer<br />
</nowiki>}}<br />
<br />
{{注意|在 IRC 或论坛寻求这方面帮助时,别忘了贴出上面几个命令的输出。}}<br />
<br />
如果你的输出跟上面类似,或至少有 '''controlC0''' 和 '''pcmC0D0p''',那么声卡就已经正常加载了。<br />
<br />
如果出现问题,声卡模块没有正确加载,那么请尝试手动加载模块:<br />
<br />
* 确定声卡对应的驱动模块:[http://www.alsa-project.org/main/index.php/Matrix:Main ALSA Soundcard Matrix]。这些模块都会有一个”snd-“前缀(例如:{{ic|snd-via82xx}})。<br />
* [[Kernel_modules_(简体中文)#装入|加载模块]]。<br />
* 检查 {{ic|/dev/snd}} 目录中的设备文件(参见上文);或者,检查 {{ic|alsamixer}} 或 {{ic|amixer}} 的输出是否正确。<br />
* 修改配置,使 {{ic|snd-<模块名>}} 和 {{ic|snd-pcm-oss}} 模块[[Kernel_modules_(简体中文)#装入|开机自动加载]]。<br />
<br />
===启用 SPDIF 输出===<br />
<br />
(由 Gentoo 论坛的 gralves 提供)<br />
* 在 GNOME 音量控制的选项(Options)标签中,把 IEC958 改成 PCM。该选项可以在首选项中启用。<br />
* 不使用 GNOME 音量控制工具的话:<br />
** 编辑 {{ic|/var/lib/alsa/asound.state}}。该文件是 alsasound 保存混音器设置的地方。<br />
** 找到这一行:“IEC958 Playback Switch”。该行上下应该有一行内容为 {{ic|value:false}},将其改为 {{ic|value:true}}。<br />
** 再找到这一行:”IEC958 Playback AC97-SPSA“。把对应的值改为 0。<br />
** 重启 ALSA。<br />
<br />
{{Out of date|rc.local 在 systemd 中已经弃用,以下方法可能不再适用。}}<br />
<br />
还有另外一个开机自动启用 SPDIF 输出的方法(在 SoundBlaster Audigy 声卡上测试过):<br />
<br />
* 在 {{ic|/etc/rc.local}} 中添加如下内容:<br />
# Use COAX-digital output<br />
amixer set 'IEC958 Optical' 100 unmute<br />
amixer set 'Audigy Analog/Digital Output Jack' on<br />
<br />
使用以下命令,获取你的声卡的数字输出的名称:<br />
$ amixer scontrols<br />
<br />
===系统级均衡器===<br />
====使用 AlsaEqual(包含界面)====<br />
从 [[AUR(简体中文)|AUR]] 安装 {{AUR|alsaequal}}。<br />
<br />
{{注意|如果使用的是64位系统,而又安装了32位的 Flash 插件,这里的设置会导致 Flash 无声。如果发生了这种情况,请禁用 alsaequal 或编译32位版本的 alsaequal。}}<br />
<br />
安装后,把下列内容添加到 ALSA 配置文件({{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}}):<br />
<br />
ctl.equal {<br />
type equal;<br />
}<br />
<br />
pcm.plugequal {<br />
type equal;<br />
# Modify the line below if you do not<br />
#slave.pcm "plughw:0,0";<br />
#by default we want to play from more sources at time:<br />
slave.pcm "plug:dmix";<br />
}<br />
#pcm.equal {<br />
# If you do not want the equalizer to be your<br />
# default soundcard comment the following<br />
# line and uncomment the above line. (You can<br />
# choose it as the output device by addressing<br />
# it with specific apps,eg mpg123 -a equal 06.Back_In_Black.mp3)<br />
pcm.!default {<br />
type plug;<br />
slave.pcm plugequal;<br />
}<br />
<br />
设置好后,切换均衡器:<br />
$ alsamixer -D equal<br />
<br />
需要注意,每个用户的 alsaequal 配置文件都是独立的,位于 {{ic|~/.alsaequal.bin}}。因此,如果想在 [[mpd]] 之类的以独立用户身份运行的程序中使用 AlsaEqual,就得像下面这样:<br />
# su mpd -c 'alsamixer -D equal'<br />
或者,在其他用户的主目录下创建符号链接,指向你的 {{ic|.alsaequal.bin}}……<br />
<br />
=====管理 AlsaEqual 配置=====<br />
从 [http://xyne.archlinux.ca/repos/ Xyne 的软件仓库] 或 [https://aur.archlinux.org/packages.php?ID=62420 AUR] 安装 [http://xyne.archlinux.ca/projects/alsaequal-mgr/ alsaequal-mgr]。<br />
<br />
同上,配置均衡器: <br />
$alsamixer -D equal<br />
<br />
如果对效果感到满意,将其命名(如“foo”)并保存该配置:<br />
$alsaequal-mgr save foo<br />
<br />
重新加载“foo”中的配置:<br />
$alsaequal-mgr load foo<br />
<br />
这样,就可以为游戏、电影、不同音乐流派、网络电话等等创建不同的均衡器配置,以便随时切换。<br />
<br />
参见[http://xyne.archlinux.ca/projects/alsaequal-mgr/ 项目主页]和程序的帮助信息,了解更多设置。<br />
<br />
====使用 mbeq====<br />
{{注意|该软件用到了一个 ladspa 插件,可能导致播放音频时 CPU 占用率偏高。In addition, this was made with stereophonic sound (e.g. headphones) in mind.}}<br />
<br />
安装 {{Pkg|alsa-plugins}}、{{Pkg|ladspa}} 和 {{Pkg|swh-plugins}} 软件包。<br />
<br />
* 在 {{ic|~/.asoundrc}} 或 {{ic|/etc/asound.conf}} 中添加如下内容:<br />
<br />
{{hc|/etc/asound.conf|2=<nowiki>pcm.eq {<br />
type ladspa<br />
<br />
# The output from the EQ can either go direct to a hardware device<br />
# (if you have a hardware mixer, e.g. SBLive/Audigy) or it can go<br />
# to the software mixer shown here.<br />
#slave.pcm "plughw:0,0"<br />
slave.pcm "plug:dmix"<br />
<br />
# Sometimes you may need to specify the path to the plugins,<br />
# especially if you have just installed them. Once you have logged<br />
# out/restarted this should not be necessary, but if you get errors<br />
# about being unable to find plugins, try uncommenting this.<br />
#path "/usr/lib/ladspa"<br />
<br />
plugins [<br />
{<br />
label mbeq<br />
id 1197<br />
input {<br />
#this setting is here by example, edit to your own taste<br />
#bands: 50hz, 100hz, 156hz, 220hz, 311hz, 440hz, 622hz, 880hz, 1250hz, 1750hz, 25000hz,<br />
#50000hz, 10000hz, 20000hz<br />
controls [ -5 -5 -5 -5 -5 -10 -20 -15 -10 -10 -10 -10 -10 -3 -2 ]<br />
}<br />
}<br />
]<br />
}<br />
<br />
# Redirect the default device to go via the EQ - you may want to do<br />
# this last, once you are sure everything is working. Otherwise all<br />
# your audio programs will break/crash if something has gone wrong.<br />
<br />
pcm.!default {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
<br />
# Redirect the OSS emulation through the EQ too (when programs are running through "aoss")<br />
<br />
pcm.dsp0 {<br />
type plug<br />
slave.pcm "eq"<br />
}<br />
</nowiki>}}<br />
<br />
* 完成。若有问题,欢迎前往论坛咨询。<br />
<br />
==高质量重采样==<br />
启用软件混音时,ALSA 会强制把所有音频重采样到相同的频率(如果系统支持,默认是 48000)。dmix 的重采样算法很糟糕,会导致显著的质量损失。<br />
<br />
安装 {{pkg|alsa-plugins}} 和 {{pkg|libsamplerate}}。<br />
<br />
将默认的采样率转换器更改为 libsamplerate:<br />
<br />
{{hc|/etc/asound.conf|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
或:<br />
<br />
{{hc|~/.asoundrc|defaults.pcm.rate_converter "samplerate_best"}}<br />
<br />
'''samplerate_best''' 输出的声音质量最佳,但需要耗费大量CPU资源进行实时重采样。还有其他的算法可供选择(如 '''samplerate''' 等),但比起默认的算法,这些算法的改进并不明显。<br />
<br />
{{警告|某些系统中,启用 samplerate_best 可能导致 Flash 播放器无声音。}}<br />
<br />
==Upmixing/Downmixing==<br />
===Upmixing===<br />
In order for stereo sources like music to be able to saturate a 5.1 or 7.1 sound system, you need to use upmixing. In darker days this used to be tricky and error prone but nowadays plugins exist to easily take care of this task. Thus, install {{Pkg|alsa-plugins}}.<br />
<br />
Then add the following to your ALSA configuration file of choice (either {{ic|/etc/asound.conf}} or {{ic|~/.asoundrc}}):<br />
pcm.upmix71 {<br />
type upmix<br />
slave.pcm "surround71"<br />
delay 15<br />
channels 8<br />
}<br />
<br />
You can easily change this example for 7.1 upmixing to 5.1 or 4.0.<br />
<br />
This adds a new pcm that you can use for upmixing. If you want all sound sources to go through this pcm, add it as a default below the previous definition like so:<br />
<br />
pcm.!default "plug:upmix71"<br />
<br />
The plugin automatically allows multiple sources to play through it without problems so setting is as a default is actually a safe choice. <br />
If this is not working, you have to setup your own dmixer for the upmixing PCM like this: <br />
pcm.dmix6 {<br />
type asym<br />
playback.pcm {<br />
type dmix<br />
ipc_key 567829<br />
slave {<br />
pcm "hw:0,0"<br />
channels 6<br />
}<br />
}<br />
}<br />
<br />
and use "dmix6" instead of "surround71". <br />
If you experience skipping or distorted sound, consider increasing the buffer_size (to 32768, for example) or use a [[#高质量重采样|high quality resampler]].<br />
<br />
===Downmixing===<br />
If you want to downmix sources to stereo because you, for instance, want to watch a movie with 5.1 sound on a stereo system, you need to use the vdownmix plugin that alsa provides in the same package.<br />
<br />
Again, in your configuration file, add this:<br />
<br />
pcm.!surround51 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
pcm.!surround40 {<br />
type vdownmix<br />
slave.pcm "default"<br />
}<br />
<br />
==混音==<br />
===软件混音(dmix)===<br />
<br />
{{注意|在 ALSA 1.0.9rc2 及以上版本中,如果声卡不支持硬件混音,模拟输出将默认使用 dmix 混音,无需额外设置。}}<br />
<br />
如果软件混音未正常启用,那么可以手动创建 {{ic|~/.asoundrc}},加入以下内容:<br />
<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
<br />
这样就启用了软件混音,多个程序能够同时使用声卡。<br />
<br />
至于 S/PDIF 之类的数字音频输出,目前的 ALSA 还不会默认启用 dmix。因此,必须像上面这样手动启用。<br />
<br />
常见问题的解答,参见[[#疑难解答]]部分。<br />
<br />
===硬件混音===<br />
====支持====<br />
如果声卡支持,一般无需任何配置即可启用硬件混音。绝大多数板载声卡都不支持硬件混音,必须得使用软件混音(见上文)。很多独立声卡都支持硬件混音,Linux 下支持较好的有:<br />
<br />
* Creative SoundBlaster Live! (5.1 型号)<br />
* Creative SoundBlaster Audigy (部分型号)<br />
* Creative SoundBlaster Audidy 2 (ZS 系列型号)<br />
* Creative SoundBlaster Audigy 4 (Pro 系列型号)<br />
<br />
{{注意|上述声卡的低端版本(Audigy SE、Audigy 2 NX、SoundBlaster Live! 24bit 以及 SoundBlaster Live! 7.1)使用了不同的芯片,'''不支持'''硬件混音。}}<br />
<br />
{{注意|板载 VIA8237 芯片支持4路硬件混音,但在某些主板上只能使用3路(第4路无声),甚至根本无法使用。即使能正常工作,质量也比不上其他混音方法。}}<br />
<br />
====问题修复====<br />
如果你的声卡是 Intel Corporation 82801I (ICH9 Family) HD Audio Controller (rev 02),且使用了64位系统,需要执行以下命令才能在 Enemy Territory 中听到声音:<br />
echo "et.x86 0 0 direct" > /proc/asound/card0/pcm0p/oss<br />
echo "et.x86 0 0 disable" > /proc/asound/card0/pcm0c/oss<br />
<br />
==疑难解答==<br />
===使用CPU频率动态调整时,音频发生跳跃===<br />
使用 {{ic|ondemand}} 或 {{ic|conservative}} 频率策略、启用CPU频率动态调整时,某些声卡和某些 ALSA 驱动会出现声音跳跃的情况。目前,唯一的解决方案是使用固定的频率,如 {{ic|performance}} 性能策略。<br />
<br />
详情参见 [[CPU Frequency Scaling (简体中文)]]。<br />
<br />
===Problems with Availability to Only One User at a Time===<br />
You might find that only one user can use the dmixer at a time. This is probably ok for most, but for those who run [[mpd]] as a separate user this poses a problem. When mpd is playing a normal user cannot play sounds though the dmixer. While it's quite possible to just run mpd under a user's login account, another solution has been found. Adding the line {{ic|ipc_key_add_uid 0}} to the {{ic|pcm.dmixer}} block disables this locking. The following is a snippet of the {{ic|asound.conf}}, the rest is the same as above.<br />
...<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
slave {<br />
...<br />
<br />
===Simultaneous Playback Issues===<br />
<br />
If you are having simultaneous playback issues, and if [[PulseAudio]] is installed (i.e. by [[GNOME]]), its default configuration is set to "hijack" the soundcard. Some users of ALSA may not want to use [[PulseAudio]] and are quite content with their current ALSA settings. One fix is to edit {{ic|/etc/asound.conf}} and comment out the following lines:<br />
# Use PulseAudio by default<br />
#pcm.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
# hint {<br />
# show on<br />
# description "Default ALSA Output (currently PulseAudio Sound Server)"<br />
# }<br />
#}<br />
<br />
Commenting the following out also may help:<br />
#ctl.!default {<br />
# type pulse<br />
# fallback "sysdefault"<br />
#}<br />
<br />
This may be a much simpler solution than completely uninstalling [[PulseAudio]].<br />
<br />
Effectively, here is an example of a working {{ic|/etc/asound.conf}}:<br />
{{bc|<nowiki>pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
ipc_key_add_uid 0<br />
ipc_perm 0660<br />
}<br />
pcm.dsp {<br />
type plug<br />
slave.pcm "dmix"<br />
}</nowiki><br />
}}<br />
<br />
{{note|This {{ic|/etc/asound.conf}} file was intended for and used successfully with a global [[MPD]] configuration. See [[Alsa#Problems_with_Availability_to_Only_One_User_at_a_Time|this section]] on multiple users}}<br />
<br />
===Random Lack of Sound on Startup===<br />
If you randomly have no sound on startup, it may be because your system has multiple sound cards, and their order may sometimes change on startup. If this is the case, try [[#Set the default sound card|setting the default sound card]].<br />
<br />
If you use mpd and the configuration tips above do not work for you, try [http://mpd.wikia.com/wiki/Configuration#ALSA_MPD_software_volume_control reading this] instead.<br />
<br />
===Specific Program Problems===<br />
For other programs who insist on their own audio setup, eg, XMMS or Mplayer, you would need to set their specific options.<br />
<br />
For mplayer, open up {{ic|~/.mplayer/config}} (or {{ic|/etc/mplayer/mplayer.conf}} for global setting) and add the following line:<br />
ao=alsa<br />
<br />
For XMMS/Beep Media Player, go into their options and make sure the sound driver is set to Alsa, not oss.<br />
<br />
To do this in XMMS:<br />
* Open XMMS<br />
** Options -> preferences.<br />
** Choose the Alsa output plugin.<br />
<br />
For applications which do not provide a ALSA output, you can use aoss from the alsa-oss package. To use aoss, when you run the program, prefix it with {{ic|aoss}}, eg:<br />
aoss realplay<br />
<br />
pcm.!default{ ... } doesnt work for me anymore. but this does:<br />
pcm.default pcm.dmixer<br />
<br />
===Model Settings===<br />
Although Alsa detects your soundcard through the BIOS at times Alsa may not be able to recognize your [http://www.kernel.org/doc/Documentation/sound/alsa/HD-Audio-Models.txt model type]. The soundcard chip can be found in {{ic|alsamixer}} (e.g. ALC662) and the model can be set in {{ic|/etc/modprobe.d/modprobe.conf}} or {{ic|/etc/modprobe.d/sound.conf}}. For example:<br />
<br />
options snd-hda-intel model=MODEL<br />
<br />
There are other model settings too. For most cases Alsa defaults will do. If you want to look at more specific settings for your soundcard take a look at the [http://bugtrack.alsa-project.org/main/index.php/Matrix:Main Alsa Soundcard List] find your model, then Details, then look at the "Setting up modprobe..." section. Enter these values in {{ic|/etc/modprobe.d/modprobe.conf}}. For example, for an Intel AC97 audio:<br />
<br />
{{bc|# ALSA portion<br />
alias char-major-116 snd<br />
alias snd-card-0 snd-intel8x0<br />
# module options should go here<br />
<br />
# OSS/Free portion<br />
alias char-major-14 soundcore<br />
alias sound-slot-0 snd-card-0<br />
<br />
# card #1<br />
alias sound-service-0-0 snd-mixer-oss<br />
alias sound-service-0-1 snd-seq-oss<br />
alias sound-service-0-3 snd-pcm-oss<br />
alias sound-service-0-8 snd-seq-oss<br />
alias sound-service-0-12 snd-pcm-oss}}<br />
<br />
===Conflicting PC Speaker===<br />
If you are sure nothing is muted, that your drivers are installed correctly, and that your volume is right, but you still do not hear anything, then try adding the following line to {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
options snd-NAME-OF-MODULE ac97_quirk=0<br />
<br />
The above fix has been observed to work with {{ic|via82xx}}<br />
options snd-NAME-OF-MODULE ac97_quirk=1<br />
The above fix has been reported to work with {{ic|snd_intel8x0}}<br />
<br />
===No Microphone Input===<br />
In alsamixer, make sure that all the volume levels are up under recording, and that CAPTURE is toggled active on the microphone (e.g. Mic, Internal Mic) and/or on Capture (in alsamixer, select these items and press space). Try making positive Mic Boost and raising Capture and Digital levels higher; this make make static or distortion, but then you can adjust them back down once you are hearing ''something'' when you record<br />
<br />
As the pulseaudio wrapper is shown as "default" in alsamixer, you may have to press F6 to select your actual soundcard first. You may also need to enable and increase the volume of Line-in in the Playback section.<br />
<br />
To test the microphone, run these commands (see arecord's man page for further information):<br />
arecord -d 5 test-mic.wav<br />
aplay test-mic.wav<br />
<br />
If all fails, you may want to eliminate hardware failure by testing the microphone with a different device.<br />
<br />
For at least some computers, muting a microphone (MM) simply means its input does not go immediately to the speakers. It still receives input.<br />
<br />
Many Dell laptops need "-dmic" to be appended to the model name in {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-hda-intel model=dell-m6-dmic<br />
<br />
Some programs use try to use OSS as the main input software. Add the following lines to {{ic|/etc/modprobe.d/modprobe.conf}} to prevent OSS modules from being loaded:<br />
<br />
{{Note|The OSS modules are no longer autoloaded anyway.}}<br />
<br />
blacklist snd_pcm_oss<br />
blacklist snd_mixer_oss<br />
blacklist snd_seq_oss<br />
<br />
See also:<br />
* http://www.alsa-project.org/main/index.php/SoundcardTesting<br />
* http://alsa.opensrc.org/Record_from_mic<br />
<br />
===Setting the default Microphone/Capture Device===<br />
Some applications (Pidgin, Adobe Flash) do not provide an option to change the capture device. It becomes an issue if your microphone is on a separate device (e.g. USB webcam or microphone) than your internal sound card. To change only the default capture device, leaving the default playback device as is, you can modify your {{ic|~/.asoundrc}} file to include the following:<br />
<br />
pcm.usb<br />
{<br />
type hw<br />
card U0x46d0x81d<br />
}<br />
<br />
pcm.!default<br />
{<br />
type asym<br />
playback.pcm<br />
{<br />
type plug<br />
slave.pcm "dmix"<br />
}<br />
capture.pcm<br />
{<br />
type plug<br />
slave.pcm "usb"<br />
}<br />
}<br />
<br />
Replace "U0x46d0x81d" with your capture device's card name in ALSA. You can use {{ic|arecord -L}} to list all the capture devices detected by ALSA.<br />
<br />
===Internal Microphone not working===<br />
First make sure all the volume levels are up under recording in alsamixer. In my case adding the following option to /etc/sound.conf and reloading the snd-* module produced a new volume setting called Capture which was capturing for the internal mic. For eg, for snd-hda-intel add<br />
<br />
options snd-hda-intel enable_msi=1<br />
<br />
Then reload the module (as below), up the recording volume of Capture and then test.<br />
<br />
{{bc|# rmmod snd-hda-intel && modprobe snd-hda-intel}}<br />
<br />
===No Sound with Onboard Intel Sound Card===<br />
There may be an issue with two conflicting modules loaded, namely {{ic|snd_intel8x0}} and {{ic|snd_intel8x0m}}. In this case, blacklist snd_intel8x0m:<br />
<br />
{{hc|/etc/modprobe.d/modprobe.conf|blacklist snd_intel8x0m}}<br />
<br />
''Muting'' the "External Amplifier" in {{ic|alsamixer}} or {{ic|amixer}} may also help. See [http://alsa.opensrc.org/Intel8x0#Dell_Inspiron_8600_.28and_probably_others.29 the ALSA wiki].<br />
<br />
Unmuting the "Mix" setting in the mixer might help, also.<br />
<br />
===No Headphone Sound with Onboard Intel Sound Card===<br />
With '''Intel Corporation 82801 I (ICH9 Family) HD Audio Controller''' on laptop, you may need to add this line to modprobe or sound.conf:<br />
<br />
options snd-hda-intel model=$model<br />
<br />
Where $model is any one of the following (in order of possibility to work, but not merit):<br />
<br />
* dell-vostro<br />
* olpc-xo-1_5<br />
* laptop<br />
* dell-m6<br />
* laptop-hpsense<br />
<br />
Note: It may be necessary to put this "options" line below (after) any "alias" lines about your card.<br />
<br />
You can see all the available models in the kernel documentation. For example [http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.35.y.git;a=blob;f=Documentation/sound/alsa/HD-Audio-Models.txt;h=dc25bb84b83b49665a7ed850e7bf5423d50cd3ba;hb=HEAD here], but check that it is the correct version of that document for your kernel version.<br />
<br />
A list of available models is also available [http://www.mjmwired.net/kernel/Documentation/sound/alsa/HD-Audio-Models.txt here]. To know your chip name type the following command (with * being corrected to match your files). Note that some chips could have been renamed and do not directly match the available ones in the file.<br />
<br />
cat /proc/asound/card*/codec* | grep Codec<br />
<br />
Note that there is a high chance none of the input devices (all internal and external mics) will work if you choose to do this, so it is either your headphones or your mic. Please report to ALSA if you are affected by this bug.<br />
<br />
And also, if you have problems getting beeps to work (pcspkr):<br />
<br />
options snd-hda-intel model=$model enable=1 index=0<br />
<br />
===No sound when S/PDIF video card is installed===<br />
Discover available modules and their order:<br />
$ cat /proc/asound/modules<br />
0 snd_hda_intel<br />
1 snd_ca0106<br />
<br />
Disable the undesired video card audio codec in {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
# /etc/modprobe.d/modprobe.conf<br />
#<br />
install snd_hda_intel /bin/false<br />
<br />
If both devices use the same module, it might be possible to disable one of them in the BIOS.<br />
<br />
===Poor sound quality or clipping===<br />
If you experience poor sound quality, try setting the PCM volume (in alsamixer) to a level such that gain is 0.<br />
<br />
If snd-usb-audio driver has been loaded, you could try to enable {{ic|softvol}} in '''/etc/asound.conf''' file. Example configuration for the first audio device:<br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
pcm.dmixer {<br />
type dmix<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.dsnooper {<br />
type dsnoop<br />
ipc_key 1024<br />
slave {<br />
pcm "hw:0"<br />
channels 2<br />
period_time 0<br />
period_size 4096<br />
buffer_size 131072<br />
rate 50000<br />
}<br />
bindings {<br />
0 0<br />
1 1<br />
}<br />
}<br />
pcm.softvol {<br />
type softvol<br />
slave { pcm "dmixer" }<br />
control {<br />
name "Master"<br />
card 0<br />
}<br />
}<br />
ctl.!default {<br />
type hw<br />
card 0<br />
}<br />
ctl.softvol {<br />
type hw<br />
card 0<br />
}<br />
ctl.dmixer {<br />
type hw<br />
card 0<br />
}<br />
<br />
===Pops When Starting and Stopping Playback===<br />
Some modules (e.g. snd_ac97_codec and snd_hda_intel) can power off your sound card when not in use. This can make an audible noise (like a crack/pop/scratch) when turning on/off your sound card. Sometimes even when move the slider volume, or open and close windows (KDE4). If you find this annoying try {{ic|modinfo snd_MY_MODULE}}, and look for a module option that adjusts or disables this feature.<br />
<br />
Example: to disable the power saving mode and solve cracking sound trough speakers issue, using snd_hda_intel add in {{ic|/etc/modprobe.d/modprobe.conf}}<br />
options snd_hda_intel power_save=0<br />
or<br />
options snd_hda_intel power_save=0 power_save_controller=N<br />
<br />
You can also try it with {{ic|1=modprobe snd_hda_intel power_save=0}} before.<br />
<br />
You may also have to unmute the 'Line' ALSA channel for this to work. Any value will do (other than '0' or something too high).<br />
<br />
''Example:'' on an onboard VIA VT1708S (using the snd_hda_intel module) these cracks occured even though 'power_save' was set to 0. Unmuting the 'Line' channel and setting a value of '1' solved the issue.<br />
<br />
Source: https://www.kernel.org/doc/Documentation/sound/alsa/powersave.txt<br />
<br />
If you use a laptop, pm-utils will change {{ic|power_save}} back to 1 when you go onto battery power even if you disable power saving in {{ic|/etc/modprobe.d}}. Disable this for pm-utils by disabling the script that makes the change (see [https://wiki.archlinux.org/index.php/Pm-utils#Disabling_a_hook Disabling a hook] for more information):<br />
# touch /etc/pm/power.d/intel-audio-powersave<br />
<br />
===S/PDIF Output Does Not Work===<br />
<br />
If the optical/coaxial digital output of your motherboard/sound card is not working or stopped working, and have already enabled and unmuted it in alsamixer, try running<br />
# iecset audio on<br />
<br />
as root.<br />
<br />
You can also put this command in {{Ic|rc.local}} as it sometimes it may stop working after a reboot.<br />
<br />
===HDMI Output Does Not Work===<br />
If the HDMI output of your motherboard/sound card is not working or stopped working, and have already enabled and unmuted it in alsamixer, try the following.<br />
<br />
Query for Playback Devices:<br />
<br />
$ aplay -l<br />
**** List of PLAYBACK Hardware Devices ****<br />
card 0: NVidia [HDA NVidia], device 0: ALC1200 Analog [ALC1200 Analog]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 1: ALC1200 Digital [ALC1200 Digital]<br />
Subdevices: 1/1<br />
Subdevice #0: subdevice #0<br />
card 0: NVidia [HDA NVidia], device 3: NVIDIA HDMI [NVIDIA HDMI]<br />
Subdevices: 0/1<br />
Subdevice #0: subdevice #0<br />
<br />
Now that we have the info for the HDMI device, try a test, In the example below, 0 is the card number and 3 is the device number.<br />
<br />
$ aplay -D plughw:0,3 /usr/share/sounds/alsa/Front_Center.wav<br />
<br />
If aplay does not output any errors, but still no sound is heared, "reboot" the receiver, monitor or tv set. Since the HDMI interface executes a handshake on connection, it might have noticed before that there was no audio stream embedded, and disabled audio decoding. In particular, if you are using a standalone window manager (don’t know about Gnome or KDE), you may need to have some sound playing ''while'' plugging in the HDMI cable.<br />
<br />
{{Note|If you are using an ATI card and linux 3.0, check your kernel/grub settings, you most likely need to use the ''''radeon.audio'''<nowiki>=</nowiki>'''1'''' boot parameter, or alternatively set up the option in your modprobe configuration files.}}<br />
<br />
If the test is successful, create or edit your ~/.asoundrc file to set HDMI as the default audio device.<br />
<br />
{{hc|~/.asoundrc|<br />
pcm.!default {<br />
type hw<br />
card 0<br />
device 3<br />
}<br />
}}<br />
<br />
Or you above config does not work try:<br />
{{hc|~/.asoundrc|<br />
defaults.pcm.card 0<br />
defaults.pcm.device 3<br />
defaults.ctl.card 0<br />
}}<br />
<br />
===HDMI Multi-channel PCM output does not work (Intel) ===<br />
As of Linux 3.1 multi-channel PCM output through HDMI with a Intel card (Intel Eaglelake, IbexPeak/Ironlake,SandyBridge/CougarPoint and IvyBridge/PantherPoint) is not yet supported. Support for it has been recently added and expected to be available in Linux 3.2. To make it work in Linux 3.1 you need to apply the following patches:<br />
<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=76adaa34db407f174dd06370cb60f6029c33b465 drm: support routines for HDMI/DP ELD]<br />
* [http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=patch;h=e0dac65ed45e72fe34cc7ccc76de0ba220bd38bb drm/i915: pass ELD to HDMI/DP audio driver]<br />
<br />
===HP TX2500===<br />
Add these 2 lines into {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
options snd-cmipci mpu_port=0x330 fm_port=0x388<br />
options snd-hda-intel index=0 model=toshiba position_fix=1<br />
<br />
options snd-hda-intel model=hp (works for tx2000cto)<br />
<br />
===Skipping Sound When Playing MP3===<br />
If you have sound skipping when playing MP3 files and you have more then 2 speakers attached to your computer (i.e. > 2 speaker system), run alsamixer and disable the channels for the speakers that you '''DO NOT''' have (i.e. do not enable the sound for the center speaker if you do not have a center speaker.<br />
<br />
===Using a USB Headset and External USB Sound Cards===<br />
If you are using a USB headset with ALSA you can try using {{AUR|asoundconf}} (currently only available from the [[AUR]]) to set the headset as the primary sound output. Before running make sure you have usb audio module enabled ({{ic|modprobe snd-usb-audio}}).<br />
<br />
# asoundconf is-active<br />
# asoundconf list<br />
# asoundconf set-default-card <chosen soundcard><br />
<br />
==== Crackling sound with USB devices ====<br />
If you experience crackling sound on USB devices, you can try tuning the snd-usb-audio for minimal latency.<br />
<br />
Add this to your {{ic|/etc/modprobe.d/modprobe.conf}}:<br />
<br />
options snd-usb-audio nrpacks=1<br />
<br />
source: http://alsa.opensrc.org/Usb-audio#Tuning_USB_devices_for_minimal_latencies<br />
<br />
==== Hot-plugging a USB Sound Card ====<br />
In order to automatically make a USB Sound Card the primary output device, when the card is plugged in, you can use the following udev rules (e.g. add the following two lines to {{ic|/etc/udev/rules.d/00-local.rules}} and reboot).<br />
<br />
{{bc|1=KERNEL=="pcmC[D0-9cp]*", ACTION=="add", PROGRAM="/bin/sh -c 'K=%k; K=$${K#pcmC}; K=$${K%%D*}; echo defaults.ctl.card $$K > /etc/asound.conf; echo defaults.pcm.card $$K >>/etc/asound.conf'"<br />
KERNEL=="pcmC[D0-9cp]*", ACTION=="remove", PROGRAM="/bin/sh -c 'echo defaults.ctl.card 0 > /etc/asound.conf; echo defaults.pcm.card 0 >>/etc/asound.conf'"}}<br />
<br />
===Error 'Unknown hardware' Appears After a Kernel Update===<br />
The following messages may be displayed during the start-up ALSA after the kernel update:<br />
Unknown hardware "foo" "bar" ...<br />
Hardware is initialized using a guess method<br />
/usr/sbin/alsactl: set_control:nnnn:failed to obtain info for control #mm (No such file or directory)<br />
<br />
or<br />
Found hardware: "HDA-Intel" "VIA VT1705" "HDA:11064397,18490397,00100000" "0x1849" "0x0397"<br />
Hardware is initialized using a generic method<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #1 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #2 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #25 (No such file or directory)<br />
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #26 (No such file or directory)<br />
<br />
Simply store ALSA mixer settings again (as root):<br />
# alsactl -f /var/lib/alsa/asound.state store<br />
<br />
It may be necessary configure ALSA again with alsamixer<br />
<br />
===HDA Analyzer===<br />
If the mappings to your audio pins(plugs) do not correspond but ALSA works fine, you could try HDA Analyzer -- a pyGTK2 GUI for HD-audio control can be found [http://www.alsa-project.org/main/index.php/HDA_Analyzer at the ALSA wiki].<br />
Try tweaking the Widget Control section of the PIN nodes, to make microphones IN and headphone jacks OUT. Referring to the Config Defaults heading is a good idea.<br />
<br />
NOTE: the script is done by such way that it is incompatible with python3 (which is now shipped with ArchLinux) but tries to use it.<br />
The workaround is: open "run.py", find all occurences of "python" (2 occurences - one on the first line, and the second on the last line) and replace them all by "python2".<br />
<br />
NOTE2: the script requires root acces, but running it via su/sudo is bogus. Run it via {{ic|kdesu}} or {{ic|gksu}}.<br />
<br />
===ALSA with SDL===<br />
If you get no sound via SDL and ALSA cannot be chosen from the application. Try setting the environmental variable SDL_AUDIODRIVER to alsa.<br />
# export SDL_AUDIODRIVER=alsa<br />
<br />
===Low Sound Workaround===<br />
<br />
If you are facing low sound even after maxing out your speakers/headphones, you can give the softvol plugin a try. Add the following to {{ic|/etc/asound.conf}}.<br />
{{bc|<nowiki><br />
pcm.!default {<br />
type plug<br />
slave.pcm "softvol"<br />
}<br />
<br />
pcm.softvol {<br />
type softvol<br />
slave {<br />
pcm "dmix"<br />
}<br />
control {<br />
name "Pre-Amp"<br />
card 0<br />
}<br />
min_dB -5.0<br />
max_dB 20.0<br />
resolution 6<br />
}<br />
</nowiki>}}<br />
{{note| You will probably have to restart the computer, as restarting the alsa daemon did not load the new configuration for me. Also, if the configuration does not work even after restarting, try changing {{ic|plug}} with {{ic|hw}} in the above configuration.}}<br />
<br />
After the changes are loaded successfully, you will see a {{ic|Pre-Amp}} section in alsamixer. You can adjust the levels there.<br />
{{note|Setting a high value for {{ic|Pre-Amp}} can cause sound distortion, so adjust it according to the level that suits you.}}<br />
<br />
===Popping sound after resuming from suspension===<br />
You might hear a popping sound after resuming the computer from suspension. This can be fixed by editing {{ic|/etc/pm/sleep.d/90alsa}} and removing the line that says {{ic|aplay -d 1 /dev/zero}}<br />
<br />
==设置文件范例==<br />
参见:[[Advanced Linux Sound Architecture/Example Configurations]]。<br />
<br />
==相关阅读==<br />
* [http://www.mjmwired.net/kernel/Documentation/sound/alsa/ALSA-Configuration.txt ALSA 模块高级配置]<br />
* [http://alsa.opensrc.org/Main_Page 非官方的 ALSA Wiki]<br />
* [https://bbs.archlinux.org/viewtopic.php?id=36815 如何从 svn 获取驱动代码并编译]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=ArchWiki:Translation_Team_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=250468
ArchWiki:Translation Team (简体中文)
2013-03-13T05:34:41Z
<p>Cuihao: /* 页面维护列表 */</p>
<hr />
<div>[[Category:简体中文]]<br />
[[Category:ArchWiki (简体中文)]]<br />
[[en:ArchWiki Translation Team]]<br />
[[es:ArchWiki Translation Team]]<br />
[[hr:ArchWiki Translation Team]]<br />
[[it:ArchWiki Translation Team]]<br />
[[pl:ArchWiki Translation Team]]<br />
[[tr:ArchWiki_Çeviri_Ekibi]]<br />
Arch Wiki 上有许多中文页面,这些页面是无数中文志愿者劳动的结晶。随着时间推移,有些页面因为没有及时维护,内容严重过时。而目前的翻译工作缺少组织,效率偏低。所以参照西班牙和意大利翻译组的做法,添加这个页面。<br />
<br />
如果希望进行翻译和维护,只需要编辑下面的[[#页面维护列表]],将自己加为页面的维护者。如果列表中还没有要认领翻译的页面,请自行添加。如果因为时间原因无法再维护页面,请及时将自己从维护者列表中删除。<br />
<br />
== 创建翻译 ==<br />
{{注意|如果不准备翻译页面的大部分内容,请尽量不要新建简体中文页面。检查英文页面的更新需要花费不少精力,没有翻译的页面会增加维护负担。}}<br />
# 如果还不知道如何编辑 wiki,请阅读 [[Help:Editing (简体中文)|编辑帮助]]。<br />
# 阅读 [[Help:i18n (简体中文)|i18n帮助]],文章给出了 ArchWiki 国际化和本地化的指南。<br />
# [[Special:UserLogin |登录]] 以进行编辑。<br />
# 选择要翻译的页面,例如从 [[Special:Random|随机页面]] 或[[#页面维护列表 | 页面维护列表]] 中选择一个未翻译完成的页面。假设要翻译 [[Some Page]].<br />
# 进入选择的英文页面,点击页面顶部的 '''编辑'''。<br />
# 添加要翻译文件的语言间链接 (参见[[Help:i18n#Interlanguage links]])。<br />
# 复制所有页面代码。<br />
# 保存页面 (新加了语言链接)<br />
# 访问页面右边新添加的语言链接,应该会进到 [[Some Page (简体中文)]] : {{ic|<nowiki>https://wiki.archlinux.org/index.php/Some_Page_(</nowiki>''简体中文'')}}<br />
# 因为页面不存在,点击 '''创建'''。<br />
# 将显示一个编辑器 - 粘贴复制的英文页面。<br />
# 将文章分类修改为本地化版本,例如将 {{ic|<nowiki>[[Category:Internationalization]]</nowiki>}} 修改为 {{ic|<nowiki>[[Category:Internationalization (简体中文)]]</nowiki>}}<br />
# 修改语言间链接,指向英文页面(将 {{ic|zh-CN}} 修改为 {{ic|en}},并将英文页面移到文章顶部。<br />
# 翻译页面,进行保存。<br />
# (推荐)给翻译完成的页面加上[[Template:TranslationStatus (简体中文)|翻译状态]],后有详细介绍。<br />
# 更新所有其它语言页面,加入刚翻译文章的语言间链接。<br />
# (可选)创建一个简体中文名称的页面,指向新创建的页面:访问 {{ic|<nowiki>https://wiki.archlinux.org/index.php/</nowiki>''页面的中文名称''}}.<br />
# (可选)建立新页面,并加入:{{bc|<nowiki>#REDIRECT [[Some Page (简体中文)</nowiki>]]}}<br />
<br />
== 完善翻译 ==<br />
[https://wiki.archlinux.org/index.php?title=Special:WhatLinksHere/Template:Translateme_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&limit=100 这个页面] 包含了需要完善翻译的简体中文页面。完善翻译的基本步骤:<br />
# 选择自己比较熟悉的文章进行翻译<br />
# 先检查英文页面的对应段落,更新成最新的英文后再翻译,避免翻译过时的内容,减少信息遗漏。<br />
# 翻译完成后删除页面中的 <nowiki>{{translateme (简体中文)}}</nowiki> 标记<br />
# (推荐)给翻译完成的页面加上[[Template:TranslationStatus (简体中文)|翻译状态]],后有详细介绍。<br />
<br />
== 维护翻译 ==<br />
完成页面的翻译只是初步完成任务,即时同步英文页面改动、更新翻译是一个持续性的工作,可能会耗费更多的时间。<br />
<br />
=== 页面认领 ===<br />
所有人都可以认领页面。认领后的责任包括进行翻译,关注英文页面的改动,及时同步翻译。<br />
<br />
为了更好的跟踪英文页面的修改,请务必在设置中启用监视列表邮件通知,并监视对应的英文页面(从设置中找到监视列表,加入英文页面。或者直接到英文页面点击页面顶端的监视标签。这样只要有改动,就会收到邮件通知)。<br />
<br />
{{小贴士|如果收到邮件通知后没有访问页面或者访问了页面却没有登录用户,下次页面改动时就不会再发邮件通知。可以点击监视列表中的'''标记所有页面为已读'''再次获取更新。}}<br />
<br />
如果页面有维护者但长期得不到更新,将会在维护列表中删除维护者。<br />
<br />
=== 发现过期页面怎么办? === <br />
如果发现有 Wiki 页面过期或错误:<br />
* 小的改动,有时间可以立即进行修改同步,维护者并不控制页面的编辑权限,越多的人参与维护越好。如果改动较大,请先联系维护者,避免重复劳动。<br />
* 没有时间查看更改,请给页面加上 {{ic|<nowiki>{{out of date}}</nowiki>}} 模版,这样其他贡献者更容易发现需要更新的页面,而读者看到过期标记就可以直接查看英文页面,以免被错误内容误导,白白耽误时间。<br />
* 没有时间翻译,请将过期的中文部分删去,从英文页面中复制更改的部分到中文页面的相应部分,去掉{{ic|<nowiki>{{out of date}}</nowiki>}}模板(如果页面上有的话)并加上{{ic|<nowiki>{{translateme (简体中文)}}</nowiki>}}模板,这样其他贡献者就更容易发现需要翻译的页面,而读者也不会被过期的内容误导。<br />
如果发现有页面未翻译:<br />
* 有时间的话,请将页面中的英文部分翻译为中文,并去掉{{ic|<nowiki>{{translateme (简体中文)}}</nowiki>}}模板。<br />
* 没有时间翻译,请为页面添加{{ic|<nowiki>{{translateme (简体中文)}}</nowiki>}}模板,这样其他的贡献者就能更容易发现需要翻译的页面。<br />
{{注意|在修改页面上的模板时,请同时更新页面维护列表的翻译状态。}}<br />
<br />
=== 翻译状态模板 ===<br />
Arch 作为滚动发行版,软件变化比较快,对应的文档变化也比较快。许多翻译的文章由于缺乏更新,会产生命令运行出错或不起作用等问题。而由于这些过期页面没有及时标记出来,所以用户无法及时获得更新。[[Template:TranslationStatus (简体中文)|翻译状态模板]]就是为了解决这个问题而创建。<br />
<br />
此模板可以起到如下作用:<br />
* 为用户提供翻译状况,包括翻译时间、英文页面的最后版本等<br />
* 用户可以点击查看翻译后,英文页面的改动,这样英文不是很好的用户可以只查看很小一部分英文内容,并判断出是否影响操作。<br />
* 翻译人员可以跟踪页面状况,通过[https://wiki.archlinux.org/index.php/Special:WhatLinksHere/Template:TranslationStatus_(简体中文) 模板的反向链接]可以查找到所有标记页面,查看需要更新翻译的部分。<br />
<br />
[[Template:TranslationStatus (简体中文)|模板页面]]有详细的使用方法。<br />
<br />
=== 页面维护列表 ===<br />
{{注意|请按照拉丁字母顺序添加页面。}}<br />
翻译状态说明:<br />
;过期:页面内容未与英文页面同步,对应{{ic|<nowiki>{{out of date}}</nowiki>}} 模版<br />
;未翻译:页面中含有英文内容,对应{{ic|<nowiki>{{translateme (简体中文)}}</nowiki>}}模板<br />
;完成:页面已与英文页面同步<br />
{| class="wikitable sortable collapsible" border="1"<br />
|-<br />
! 页面<br />
! 翻译状态<br />
! 维护者<br />
! class="unsortable" width="30%" | 备注<br />
|-<br />
| [[acpid (简体中文)]]<br />
| 完成<br />
| Cael<br />
| 无<br />
|-<br />
| [[ACPI hotkeys (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[ACPI modules (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[Activating Numlock on Bootup (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[Ad-hoc networking (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[Advanced Linux Sound Architecture (简体中文)]]<br />
| 翻译中<br />
| cuihao<br />
| 无<br />
|-<br />
| [[AHCI (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[Allow Users to Shutdown (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[aMule (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[Android (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[Apache, suEXEC and Virtual Hosts (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[Arch Based Distributions (Active) (简体中文)]]<br />
| 未翻译<br />
| 无<br />
| 无<br />
|-<br />
| [[Arch Build System (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[ArchWiki:About (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[ATI (简体中文)]]<br />
| 过期<br />
| 无<br />
| 无<br />
|-<br />
| [[AUR Helpers (简体中文)]]<br />
| 未翻译<br />
| 无<br />
| 无<br />
|-<br />
| [[Avant Window Navigator (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[awesome (简体中文)]]<br />
| 进行中<br />
| Cael<br />
| 无<br />
|-<br />
| [[Bash (简体中文)]]<br />
| 完成<br />
| Jaurung<br />
| 无<br />
|-<br />
| [[Bumblebee (简体中文)]]<br />
| 完成<br />
| Peter<br />
| 无<br />
|-<br />
| [[AMD Catalyst (简体中文)]]<br />
| 完成<br />
| Shibao Zhao<br />
| 无<br />
|-<br />
| [[Common Applications (简体中文)]]<br />
| 翻译中<br />
| DavidChen<br />
| 翻译中<br />
|-<br />
| [[Compiz (简体中文)]]<br />
| 过期<br />
| 无<br />
| 无<br />
|-<br />
| [[Configuring Network (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[CPU Frequency Scaling (简体中文)]]<br />
| 完成<br />
| Flockyrocky<br />
| 更新版本,补充了原来没翻译的内容<br />
|-<br />
| [[Creating Packages (简体中文)]]<br />
| 完成<br />
| Cael<br />
| 无<br />
|-<br />
| [[Disabling IPv6 (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[Downgrading Packages (简体中文)]]<br />
| 完成<br />
| Cael<br />
| 无<br />
|-<br />
| [[E17 (简体中文)]]<br />
| 完成<br />
| Aaron_chen<br />
| 同步翻译至2012年12月24日18:00英文页面<br />
|-<br />
| [[Emacs (简体中文)]]<br />
| 翻译中<br />
| Jaurung yuanhang<br />
| 未完成<br />
|-<br />
|-<br />
| [[Font Configuration (简体中文)]]<br />
| 翻译中<br />
| Jaurung<br />
| 完善中<br />
|-<br />
| [[Fonts (简体中文)]]<br />
| 未翻译<br />
| 无<br />
| 无<br />
|-<br />
| [[Fstab (简体中文)]]<br />
| 完成<br />
| Fengchao<br />
| 无<br />
|-<br />
| [[GRUB (简体中文)]]<br />
| 翻译中<br />
| 无<br />
| 无<br />
|-<br />
| [[Help:Style (简体中文)]]<br />
| 完成<br />
| Fengchao<br />
| 无<br />
|-<br />
| [[IBus (简体中文)]]<br />
| 未翻译<br />
| 无<br />
| 无<br />
|-<br />
| [[Improve_Pacman_Performance_(简体中文)]]<br />
| 未翻译<br />
| 无<br />
| 无<br />
|-<br />
| [[Intel Graphics (简体中文)]]<br />
| 完成<br />
| Shibao Zhao<br />
| 无<br />
|-<br />
| [[Kernel Compilation (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[Kernel Compilation/Arch Build System (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[LAMP (简体中文)]]<br />
| 完成<br />
| Liuzhengyi<br />
| 勘误中<br />
|-<br />
| [[Laptop Mode Tools (简体中文)]]<br />
| 未翻译<br />
| 无<br />
| 无<br />
|-<br />
| [[Lenovo ThinkPad T420 (简体中文)]]<br />
| 完成<br />
| Flockyrocky<br />
| 无<br />
|-<br />
| [[LibreOffice (简体中文)]]<br />
| 过期<br />
| 无<br />
| 无<br />
|-<br />
| [[Local Mirror (简体中文)]]<br />
| 过期<br />
| 无<br />
| 无<br />
|-<br />
| [[Makepkg (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[mkinitcpio (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[Network Time Protocol daemon (简体中文)]]<br />
| 未翻译<br />
| 无<br />
| 部分未翻译<br />
|-<br />
| [[Official Repositories (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[Openbox (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[OpenOffice (简体中文)]]<br />
| 过期<br />
| 无<br />
| 无<br />
|-<br />
| [[Pacman (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[Pacman GUI Frontends (简体中文)]]<br />
| 未翻译<br />
| 无<br />
| 无<br />
|-<br />
| [[pacman Tips (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[Pidgin (简体中文)]]<br />
| 进行中<br />
| Cael<br />
| 无 <br />
|- <br />
| [[Plasma (简体中文)]]<br />
| 未翻译<br />
| 无<br />
| 无<br />
|- <br />
| [[Polipo (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无 <br />
|-<br />
| [[Python (简体中文)]]<br />
| 完成<br />
| Fengchao<br />
| 无 <br />
|-<br />
| [[Smart Common Input Method platform (简体中文)]]<br />
| 过期<br />
| 无<br />
| 无<br />
|-<br />
| [[Common Applications/Science (简体中文)]]<br />
| 未翻译<br />
| 无<br />
| 无<br />
|-<br />
| [[Secure Shell (简体中文)]]<br />
| 未翻译<br />
| 无<br />
| 部分未翻译<br />
|-<br />
| [[Systemd (简体中文)]]<br />
| 完成<br />
| cuihao<br />
| 无<br />
|-<br />
| [[TeXLive (简体中文)]]<br />
| 完成<br />
| reverland<br />
| 无<br />
|-<br />
| [[Vim (简体中文)]]<br />
| 完成<br />
| 无<br />
| 无<br />
|-<br />
| [[VMware (简体中文)]]<br />
| 过期<br />
| 无<br />
| 无<br />
|-<br />
| [[Wine (简体中文)]]<br />
| 完成<br />
| cuihao<br />
| 无<br />
|-<br />
| [[Xscreensaver (简体中文)]]<br />
| 完成<br />
| liuyix<br />
| 无<br />
|-<br />
| [[Xmonad (简体中文)]]<br />
| 未翻译<br />
| Rns<br />
| 翻译中<br />
|-<br />
| [[LXDE (简体中文)]]<br />
| 翻译中<br />
| Tuxzz<br />
| 无<br />
<br />
|}<br />
<br />
== 贡献列表 ==<br />
为翻译做出贡献的用户请加入列表,感谢所有人做出的贡献。<br />
* [[User:Fengchao|Fengchao]] &ndash; [[Special:Contributions/Fengchao|贡献]] &ndash; [[Special:EmailUser/Fengchao|Send Email]] &ndash; [[ArchWiki:Maintainers|ArchWiki Maintainers]]<br />
* [[User:Skydiver|Skydiver]] &ndash; [[Special:Contributions/Skydiver|贡献]] &ndash; [[Special:EmailUser/Skydiver|Send Email]] &ndash; [[ArchWiki:Maintainers|ArchWiki Maintainers]]<br />
* [[User:Alswl|Alswl]] &ndash; [[Special:Contributions/Alswl|贡献]] &ndash; [[Special:EmailUser/Alswl|Send Email]]<br />
* [[User:Reverland|Reverland]] &ndash; [[Special:Contributions/Reverland|贡献]] &ndash; [[Special:EmailUser/Reverland|Send Email]]<br />
* [[User:Cuihao|cuihao]] &ndash; [[Special:Contributions/Cuihao|贡献]] &ndash; [[Special:EmailUser/Cuihao|Send Email]]<br />
* [[User:Cael|Cael]] &ndash; [[Special:Contributions/Cael|贡献]] &ndash; [[Special:EmailUser/Cael|Send Email]]<br />
* [[User:Flockyrocky|Flockyrocky]] &ndash; [[Special:Contributions/Flockyrocky|贡献]] &ndash; [[Special:EmailUser/Flockyrocky|Send Email]]<br />
<br />
* [[User:Tuxzz|Tuxzz]] &ndash; [[Special:Contributions/Tuxzz|贡献]] &ndash; [[Special:EmailUser/Tuxzz|Send Email]]<br />
* [[User:Aaron_chen|Aaron_chen]] &ndash; [[Special:Contributions/Aaron_chen|贡献]] &ndash; [[Special:E17/Aaron_chen|Send Email]]<br />
* [[User:Shibao Zhao|Shibao Zhao]] &ndash; [[Special:Contributions/Shibao Zhao|贡献]] &ndash; [[Special:EmailUser/Shibao Zhao|Send Email]] &ndash;<br />
* [[User:Radflum|Yk]] &ndash; [[Special:Contributions/Radflum|贡献]] &ndash; [[Special:EmailUser/Radflum|Send Email]]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Beginners%27_guide_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=248144
Beginners' guide (简体中文)
2013-02-22T08:43:24Z
<p>Cuihao: /* 挂载分区 */</p>
<hr />
<div><noinclude><br />
[[Category:Getting and installing Arch (简体中文)]]<br />
[[da:Beginners' Guide/Installation]]<br />
[[en:Beginners' Guide/Installation]]<br />
[[es:Beginners' Guide/Installation]]<br />
[[hr:Beginners' Guide/Installation]]<br />
[[hu:Beginners' Guide/Installation]]<br />
[[it:Beginners' Guide/Installation]]<br />
[[ja:Beginners' Guide/Installation]]<br />
[[ko:Beginners' Guide/Installation]]<br />
[[nl:Beginners' Guide/Installatie]]<br />
[[pl:Beginners' Guide/Installation]]<br />
[[pt:Beginners' Guide/Installation]]<br />
[[ro:Ghidul începătorilor/Instalare]]<br />
[[ru:Beginners' Guide/Installation]]<br />
[[sr:Beginners' Guide/Installation]]<br />
{{TranslationStatus (简体中文)|Beginners'_Guide/Installation|2013-02-11|246914}}<br />
{{小贴士|本文是多页文档 '''新手指南 ''' 的一部分,如果想阅读单页文档,请[[Beginners' Guide (简体中文)|点击这里]]。}}<br />
</noinclude><br />
==安装==<br />
<br />
您将看到一个命令行提示,自动以 root 登陆。<br />
<br />
===修改语言===<br />
{{提示|对于大部分用户,这些是非必需操作。只在这些情况下有需要:打算用母语编辑配置文件,在Wi-Fi密码中使用了变音记号,或需要母语显示的系统信息(如错误信息)。}}<br />
<br />
键盘布局默认设为{{ic|us}}(美式键盘映射),若正在使用非[[Wikipedia:File:KB United States-NoAltGr.svg|美式]]键盘布局,通过以下的命令选择相应的键盘映射表:<br />
{{bc|# loadkeys ''layout''}}<br />
把 ''layout'' 替换成你的键盘布局,如{{ic|fr}},{{ic|uk}}或{{ic|be-latin1}}。[[KEYMAP#Keyboard_layouts|这里]]有完整的键盘映射列表。<br />
<br />
多数语言使用多于26个[[Wikipedia:English alphabet|英文字母]]的字形,因此字体也需要进行相应的修改,否则这些语言的文字会显示成白方块。注意这些名称是区分大小写的:<br />
<br />
# setfont Lat2-Terminus16<br />
<br />
系统默认将语言设为美式英语。若想在安装过程中使用其他语言''(以德语为例)'',从{{ic|/etc/locale.gen}}中去掉相应[http://www.greendesktiny.com/support/knowledgebase_detail.php?ref=EUH-483 locale]前的{{ic|#}}。请选择含{{ic|UTF-8}}的行。<br />
<br />
按下{{Keypress|Ctrl+X}}退出,在保存提示出现时按下{{Keypress|Y}}和{{Keypress|Enter}}来使用相同的文件名。<br />
<br />
{{hc|# nano /etc/locale.gen|<br />
en_US.UTF-8 UTF-8<br />
de_DE.UTF-8 UTF-8}}<br />
<br />
# locale-gen<br />
# export LANG=de_DE.UTF-8<br />
<br />
记得按下{{Keypress|LAlt+LShift}}启用新的映射<br />
<br />
===建立网络连接===<br />
<br />
{{Warning| udev 不再按照wlanX, ethX 的方式分配网络接口的名字。如果你是从其他发行版转来,请不要认为有线接口名字就是eth0,无线接口是wlan0,请用ip命令查看接口的名字}}<br />
<br />
自从systemd-197以来,udev现在会分配稳定而可预测的网络接口名字,与之前的方式不同。新的命名会保证在重启之后命名不变,解决了以前重启之后接口命名改变的问题,更多信息请看:http://www.freedesktop.org/wiki/Software/systemd/PredictableNetworkInterfaceNames .<br />
<br />
安装程序会自动运行 {{ic|dhcpcd}} 尝试配置有线连接。可以通过 ping 一个网站检查是否成功。因为Google总是被屏蔽,所以。。。<br />
<br />
{{hc|# ping -c 3 www.baidu.com|2=<br />
PING www.a.shifen.com (61.135.169.105) 56(84) bytes of data.<br />
64 bytes from 61.135.169.105: icmp_seq=1 ttl=57 time=0.495 ms<br />
64 bytes from 61.135.169.105: icmp_seq=2 ttl=57 time=0.603 ms<br />
64 bytes from 61.135.169.105: icmp_seq=3 ttl=57 time=0.590 ms<br />
<br />
--- www.a.shifen.com ping statistics ---<br />
3 packets transmitted, 3 received, 0% packet loss, time 1999ms<br />
rtt min/avg/max/mdev = 0.495/0.562/0.603/0.055 ms<br />
}}<br />
如果出现:{{ic|ping: unknown host}} 错误,表示网络还不通,需要用下面方法手动配置。<br />
<br />
如果可以连通的话,继续 [[#准备硬盘]]。<br />
<br />
====有线连接====<br />
如果使用静态ip配置连接到有线网络,请使用如下方式配置。<br />
<br />
首先,用ip命令查看有线连接的接口名字。<br />
<br />
<br />
{{hc|# ip link|<br />
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT <br />
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00<br />
2: enp2s0f0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000<br />
link/ether 00:11:25:31:69:20 brd ff:ff:ff:ff:ff:ff<br />
3: wlp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DORMANT qlen 1000<br />
link/ether 01:02:03:04:05:06 brd ff:ff:ff:ff:ff:ff}}<br />
<br />
在这个例子中,有线连接的接口名字为enp2s0f0。如果你不确定的话,有线连接一般以"e"开头,一般不会以"lo"或"w"开头。你也可以用iwconfig命令查看哪些接口不是无线的来帮助你判断有线连接的接口名。<br />
<br />
{{hc|# iwconfig|2=<br />
enp2s0f0 no wireless extensions.<br />
wlp3s0 IEEE 802.11bgn ESSID:"NETGEAR97" <br />
Mode:Managed Frequency:2.427 GHz Access Point: 2C:B0:5D:9C:72:BF <br />
Bit Rate=65 Mb/s Tx-Power=16 dBm <br />
Retry long limit:7 RTS thr:off Fragment thr:off<br />
Power Management:on<br />
Link Quality=61/70 Signal level=-49 dBm <br />
Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0<br />
Tx excessive retries:0 Invalid misc:430 Missed beacon:0<br />
lo no wireless extensions.}}<br />
<br />
在这个例子中,enp2s0f0和lo都不是无线设备,所以enp2s0f0是有线接口。<br />
<br />
你也应该了解以下信息:<br />
* 静态IP地址,<br />
* 子网掩码,<br />
* 网关IP地址,<br />
* 域名服务器(DNS)IP地址,<br />
* 域名 (unless a local LAN, in which case you can make it up).<br />
<br />
激活接口,(例如{{ic|enp2s0f0}}):<br />
# ip link set enp2s0f0 up<br />
<br />
添加地址:<br />
# ip addr add <ip 地址>/<子网掩码> dev <接口名><br />
例如:<br />
# ip addr add 192.168.1.2/24 dev enp2s0f0<br />
<br />
更多信息参看: {{ic|man ip}}<br />
<br />
用类似下面的命令添加网关,ip 地址替换为实际的网关地址:<br />
# ip route add default via <ip 地址><br />
<br />
例如:<br />
# ip route add default via 192.168.1.1<br />
<br />
编辑 {{ic|/etc/resolv.conf}} 如下, 替换你的DNS服务器IP地址和本地域名:<br />
{{hc|# nano /etc/resolv.conf|<br />
nameserver 61.23.173.5<br />
nameserver 61.95.849.8<br />
search example.com}}<br />
<br />
{{注意|目前,你最多只能包含3行 {{ic|nameserver}} 字段.}}<br />
<br />
这时应该已经有一个可用的有线网络了,如果没有,请查阅详细的[[Configuring Network|配置网络]]页面。<br />
<br />
====无线网络====<br />
如果需要用Wi-Fi连接网络,请采用以下步骤。<br />
<br />
<br />
{{Warning| udev 不再按照wlanX, ethX 的方式分配网络接口的名字。如果你是从其他发行版转来,请不要认为有线接口名字是eth0,无线接口是wlan0,请用ip/iwconfig命令查看接口的名字}}<br />
<br />
现在在安装媒体中已经包含了无线网络驱动和工具。对无线网络硬件有一定的了解是完成无线网络设置的重要前提。注意下面的'''在安装时执行'''的快速设置过程中会初始化'''live环境'''中的无线网络硬件。安装好系统以后,无线网络设置(或其他无线管理操作)'''需要再次执行一遍'''。<br />
<br />
并且注意如果安装过程中并不需要无线网络,不需执行下面的步骤。可以在系统安装好之后再设置无线网络功能。<br />
<br />
{{注意|下面的示例中使用 {{ic|wlp3s0}} 作为接口,{{ic|linksys}} 作为 ESSID。请根据实际情况修改。}}<br />
<br />
基本过程为:<br />
* (可选)确定网络接口:<br />
# lspci | grep -i net<br />
<br />
如果使用 USB 适配器:<br />
# lsusb<br />
<br />
* 用 {{ic|iwconfig}} 确定 udev 已经载入驱动,而且驱动程序创建了可用的无线内核接口:<br />
<br />
{{注意|输出和下面不相似表示驱动没有载入,需要自己加入,详情请查看 [[Wireless Setup (简体中文)|无线设置]]。}}<br />
<br />
{{hc|# iwconfig|2=<br />
enp2s0f0 no wireless extensions.<br />
wlp3s0 IEEE 802.11bgn ESSID:"NETGEAR97" <br />
Mode:Managed Frequency:2.427 GHz Access Point: 2C:B0:5D:9C:72:BF <br />
Bit Rate=65 Mb/s Tx-Power=16 dBm <br />
Retry long limit:7 RTS thr:off Fragment thr:off<br />
Power Management:on<br />
Link Quality=61/70 Signal level=-49 dBm <br />
Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0<br />
Tx excessive retries:0 Invalid misc:430 Missed beacon:0<br />
lo no wireless extensions.}}<br />
<br />
本例中 {{ic|wlp3s0}} 为可用接口。<br />
<br />
* 启用接口: <br />
# ip link set wlp3s0 up<br />
<br />
除了驱动,一小部分无线网卡还需要固件。启用网络时,缺少固件的接口会显示:<br />
<br />
{{hc|# ip link set wlp3s0 up|<br />
SIOCSIFFLAGS: No such file or directory}}<br />
<br />
若不确定,可以用 {{ic|dmesg}} 查询内核日志,看看有没有来自无线网卡的固件请求。<br />
<br />
Intel 芯片组在启动时请求固件的例子:<br />
<br />
{{hc|# dmesg <nowiki>|</nowiki> grep firmware|<br />
firmware: requesting iwlwifi-5000-1.ucode}}<br />
<br />
如果没有输出,表示系统的无线芯片不需要固件。<br />
<br />
{{注意| 网卡需要的无线接口固件包在 live 环境中预先安装到了 {{ic|/lib/firmware}},'''但是必须特别安装到实际的系统才能在重启后提供无线功能!''' 向导的后面会介绍软件包的选择和安装。请确保在软件包选择的时候选中需要的无线模块和固件!如有任何疑问,请查看 [[Wireless Setup (简体中文)|无线设置]],这是常见错误。}}<br />
<br />
然后使用{{pkg|netcfg}}提供的 wifi-menu 连接到网络:<br />
# wifi-menu wlp3s0<br />
<br />
到此,无线网络应该已经可用,如遇问题,请查看[[Wireless Setup (简体中文)|无线设置]]。<br />
<br />
==== xDSL (PPPoE), 模拟调制解调器或者 ISDN ====<br />
<br />
如果你以桥接模式连接到ISP,调用:<br />
# pppoe-setup<br />
<br />
* 输入运营商提供的用户名<br />
* 按 {{Keypress|Enter}} 选择 "eth0".<br />
* 按 {{Keypress|Enter}} 选择"no",保持一直在线。<br />
* 输入 {{ic|server}} (大部分情况都是 server)。<br />
* 按 {{Keypress|1}} 选择防火墙<br />
* 输入运营商提供的密码<br />
* 最后按 {{Keypress|Y}}。<br />
<br />
如果配置正确,用以下命令就可以连接到 ISP:<br />
# pppoe-start<br />
<br />
可能需要在{{ic|resolv.conf}}设置域名解析服务器:<br />
# echo nameserver 8.8.8.8 > /etc/resolv.conf<br />
<br />
如果你使用拨号网络或者ISDN,请阅读[[Direct Modem Connection]]。<br />
<br />
==== 位于代理服务器后面 ====<br />
如果使用代理服务器,请 export {{ic|http_proxy}} 和 {{ic|ftp_proxy}} 变量。'''[[Proxy|查看这里]]''' 获取更多信息。<br />
<br />
===准备存储设备===<br />
<br />
{{警告|磁盘分区有时会毁掉原分区内的数据。'''强烈建议'''先备份重要的数据。}}<br />
<br />
新手建议使用图形化的分区工具。[http://gparted.sourceforge.net/download.php GParted] 是个很不错的选择。可以从 "live" Linux 发行版 (如 [[Wikipedia:Parted_Magic|Parted Magic]], [[Wikipedia:Ubuntu_(operating_system)|Ubuntu]], [[Wikipedia:Linux_Mint|Linux Mint]], 等等) 启动。硬盘在使用前需要先[[partitioning (简体中文)|分区]],然后将分区格式化为需要的[[File Systems (简体中文)|文件系统]].<br />
<br />
安装之后任何时候都可以建立一个交换文件。因此不需要在分区时决定交换分区的大小。如果你想现在设定交换分区,参见 [[Swap]] 获取更多信息 (但是注意修改文件的大小比修改分区大小更方便)。<br />
<br />
如果你已经分过区,继续 [[#挂载分区|挂载分区]].<br />
<br />
否则,参考下面的例子。<br />
<br />
====分区示例====<br />
<br />
当前安装盘包含如下工具:{{ic|fdisk}}, {{ic|gdisk}}, {{ic|cfdisk}}, {{ic|cgdisk}}, {{ic|parted}}.<br />
<br />
此例使用'''cfdisk''',将 cfdisk 替换为'''cgdisk'''可以使用新的[[GPT]]分区。<br />
<br />
{{注意|如果要设置 GRUB 为 BIOS-GPT 模式,需要创建一个 2 MiB 的 "BIOS 启动分区". 参阅[[GRUB#GPT_specific_instructions]].}}<br />
<br />
{{注意|如果使用(U)EFI,则需要一个独立的分区保存 UEFI 系统分区,参阅[[Unified_Extensible_Firmware_Interface#Create_an_UEFI_System_Partition_in_Linux|此文]]。推荐所有 UEFI 使用 GPT 分区格式,有些 UEFI 固件不支持 UEFI-MBR 启动。}}<br />
<br />
{{注意|如果你不打算与 windows 双系统启动,建议使用[[GPT]]而不是[[MBR]]. GPT 分区只能通过'''gdisk'''或'''parted'''完成. [[GPT]]中介绍了它的优点。}}<br />
<br />
{{注意|如果要安装 Arch Linux 到U盘(USB闪存盘),参见[[Installing Arch Linux on a USB key]]。}}<br />
<br />
示例硬盘将划分为15GB的根分区{{ic|/}}、剩下的作为{{ic|/home}},选择 [[MBR]] 或 [[GPT]] 其中的一个。<br />
<br />
需要再次强调的是,真正的分区取决于个人的选择,此处的示例仅供参考。详细分区介绍请参阅[[Partitioning]].<br />
<br />
{| class="wikitable"<br />
|-<br />
| rowspan="2" | '''MBR'''<br />
| rowspan="2"| {{ic|cfdisk&nbsp;/dev/sda}}<br />
| '''Root:'''<br />
<br />
* Choose New (or press {{Keypress|N}}) – {{Keypress|Enter}} for Primary – type in "15360" – {{Keypress|Enter}} for Beginning – {{Keypress|Enter}} for Bootable.<br />
|-<br />
|<br />
'''Home:'''<br />
<br />
* Press the down arrow to move to the free space area.<br />
* Choose New (or press {{Keypress|N}}) – {{Keypress|Enter}} for Primary – {{Keypress|Enter}} to use the rest of the drive (or you could type in the desired size).<br />
|-<br />
| rowspan="2" | '''GPT'''<br />
| rowspan="2"| {{ic|cgdisk&nbsp;/dev/sda}}<br />
| '''Root:'''<br />
<br />
* Choose New (or press {{Keypress|N}}) – {{Keypress|Enter}} for the first sector (2048) – type in "15360M" – {{Keypress|Enter}} for the default hex code (8300) – {{Keypress|Enter}} for a blank partition name.<br />
|-<br />
| '''Home:'''<br />
<br />
* Press the down arrow a couple of times to move to the larger free space area.<br />
* Choose New (or press {{Keypress|N}}) – {{Keypress|Enter}} for the first sector – {{Keypress|Enter}} to use the rest of the drive (or you could type in the desired size; for example "30G") – {{Keypress|Enter}} for the default hex code (8300) – {{Keypress|Enter}} for a blank partition name.<br />
|}<br />
<br />
完成之后,MBR 分区界面应该类似下面这样:<br />
<br />
Name Flags Part Type FS Type [Label] Size (MB)<br />
-------------------------------------------------------------------------<br />
sda1 Boot Primary Linux 15360 #root<br />
sda2 Primary Linux 133000 #/home<br />
<br />
GPT 分区类似:<br />
<br />
Part. # Size Partition Type Partition Name<br />
----------------------------------------------------------------<br />
1007.0 KiB free space<br />
1 15.0 GiB Linux filesystem<br />
2 123.45 GiB Linux filesystem<br />
<br />
请再次仔细检查分区大小和布局是否正确。<br />
<br />
如果要重新开始,可以选择 '''Q'''uit 退出分区程序。<br />
<br />
确认无误后,选择Write (或者按 {{Keypress|Shift+W}}) 完成分区,并输入{{ic|yes}}将修改写入磁盘,然后退出<br />
<br />
分区之后,还需要用 {{ic|mkfs}} 将分区格式化为选定的[[File Systems (简体中文)|文件系统]]。本例中 root 和 home 分区都使用 ext4:<br />
# mkfs.ext4 /dev/sda1<br />
# mkfs.ext4 /dev/sda2<br />
<br />
如果你分了一个swap区,不要忘了格式化和启用它:<br />
<br />
# mkswap /dev/sda''X''<br />
# swapon /dev/sda''X''<br />
<br />
=== 挂载分区 ===<br />
<br />
要检查当前磁盘的标识符和布局:<br />
# lsblk /dev/sda<br />
<br />
{{注意|不要在同一个目录挂载多个分区,而且按照顺序挂载很重要。}}<br />
<br />
记下将要安装上 Arch 的磁盘/分区。每个分区都能通过一个数字后缀来辨认。如:{{ic|sda1}}即指某一磁盘的第一分区,而{{ic|sda}}则表示整个磁盘。 <br />
<br />
注意要按照顺序挂载,先挂载根分区到{{ic|/mnt}}.<br />
# mount /dev/sda1 /mnt<br />
<br />
然后挂载{{ic|/home}}分区和其余单独分区({{ic|/boot}}, {{ic|/var}} 等)。<br />
<br />
然后在 {{ic|/mnt}} 中创建 home 目录并挂载分区:<br />
<br />
# mkdir /mnt/home<br />
# mount /dev/sda2 /mnt/home<br />
<br />
如果是 UEFI 主板,挂载 UEFI 分区:<br />
# mkdir /mnt/boot/efi<br />
# mount /dev/sda''X'' /mnt/boot/efi<br />
<br />
===选择安装镜像===<br />
<br />
安装前需要编辑 {{ic|/etc/pacman.d/mirrorlist}},将最想使用的镜像放到前面。mirrorlist 文件也会被{{ic|pacstrap}}复制到新系统,所以最好现在就设置好。<br />
{{注意|ftp.archlinux.org 限速 50KB/s.}}<br />
<br />
[https://www.archlinux.org/mirrors/status/ ArchLinux 镜像状态] 报告了镜像的各种状态,包括网络问题,数据收集问题、上次同步时间等等。如果需要最新的软件包,最好手动检查一下 {{ic|/etc/pacman.d/mirrorlist}},确保文件包含了最新的镜像。<br />
<br />
此外 [https://www.archlinux.org/mirrorlist/ 镜像生成器] 可以自动根据更新状况生成离你比较近的镜像列表。<br />
<br />
用编辑器打开 {{ic|/etc/pacman.d/mirrorlist}} 并取消离你近的镜像前的注释(删除前面的 '#' ),然后刷新软件包列表:<br />
<br />
# pacman -Syy<br />
<br />
传入两个 {{ic|--refresh}} 或 {{ic|-y}} 会强制 pacman 刷新所有软件包列表,即使刚刚更新过。一般情况下,只要源有了更改,就应该使用 {{ic|pacman -Syy}} 命令。<br />
<br />
详情请参阅 [[Mirrors]].<br />
<br />
{{注意|Some issues have been reported in the [https://bbs.archlinux.org/ Arch Linux forums] regarding network problems that prevent pacman from updating/synchronizing repositories (see [https://bbs.archlinux.org/viewtopic.php?id&#61;68944] and [https://bbs.archlinux.org/viewtopic.php?id&#61;65728]). When installing Arch Linux natively, these issues have been resolved by replacing the default pacman file downloader with an alternative (see [[Improve Pacman Performance]] for more details). When installing Arch Linux as a guest OS in [[VirtualBox]], this issue has also been addressed by using "Host interface" instead of "NAT" in the machine properties.<br />
}}<br />
<br />
=== 安装基本系统 ===<br />
{{注意|安装前请设置系统时间,否则密钥验证会认为密钥过期,验证失败。设置方法位于 [[Time|Time]]。}}<br />
<br />
使用 [https://github.com/falconindy/arch-install-scripts/blob/master/pacstrap.in pacstrap] 脚本安装基本系统:<br />
# pacstrap /mnt base base-devel<br />
<br />
如果 pacman 报告安装时遇到错误的签名(''error: failed to commit transaction (invalid or corrupted package)''),请运行如下命令:<br />
# pacman-key --init && pacman-key --populate archlinux <br />
<br />
; {{grp|base}} : 来自 [core] 软件源的最小基本系统。<br />
; {{grp|base-devel}} : 来自 [core] 的附加工具例如{{ic|make}}和 {{ic|automake}}。大部分新手都应该安装它,后续扩展系统会用到它,安装[[Arch User Repository|AUR]]中软件包时,{{grp|base-devel}}也是必须的。<br />
<br />
如果不知道还需要什么软件,可以先跳过,后面可以用 [[pacman]] 安装。<br />
<br />
{{注意| 如果需要连接无线网络,请选中{{Pkg|wireless_tools}}软件包。一些无线接口还需要 [[Wireless Setup#ndiswrapper|'''ndiswrapper''']] 和特定 [[Wireless Setup#Drivers_and_firmware|'''firmware''']]。如果使用 WPA 加密,需要 [[WPA Supplicant|'''wpa_supplicant''']]。 [[Wireless Setup|无线设置]] 页面将帮助您为无线设备选择正确的软件包。}}<br />
<br />
=== 生成 fstab ===<br />
用下面命令生成 [[fstab]]。如果想使用 UUIDs,使用 {{ic|-U}} 选项;如果想使用标签,用 {{ic|-L}} 选项,完成后最好检查一下生成的{{ic|/etc/fstab}}。<br />
{{Note|后面如果出现问题,请'''不要再次运行'''genfstab,如果需要,手动编辑/etc/fstab。}}<br />
<br />
# genfstab -U -p /mnt | sed 's/rw,relatime,data=ordered/defaults,relatime/' >> /mnt/etc/fstab<br />
# nano /mnt/etc/fstab<br />
<br />
使用 {{ic|-U}} 选项会用 UUID 指定分区,使用{{ic|-L}}会用标签指定分区。<br />
<br />
[[Wikipedia:fstab|fstab]]('''f'''ile '''s'''ystems '''tab'''le的缩写) 是系统配置的一部分,该文件列出所有可用的磁盘和分区,并指明它们初始化的方式或整合至全局文件系统的方式。{{ic|/etc/fstab}} 文件一般被'''mount'''命令读取,mount 以设备上的文件系统为参数,将其添加到主目录结构。在启动过程执行到 3/4 左右的时候,{{ic|/etc/fstab}} 会调用 '''mount -a''',mount 读取{{ic|/etc/fstab}} 以确定挂载时使用的选项。如果 '''noauto''' 位于 {{ic|/etc/fstab}} 中的文件系统之后,'''mount -a''' 不会在启动时挂载它。 仅根分区需要在最后使用 "1",其它可以使用 "2" 或 "0" (参阅[[Fstab#Field_definitions|字段定义]]).<br />
<br />
{{Warning|/etc/fstab文件在运行genfstab后应该被检查一下。如果之前你生成了一个EFI系统分区,那么 {{ic|genfstab}}给EFI分区添加了错误的选项,会导致无法启动。因此你需要移除EFI分区的所有选项,除了{{ic|noatime}}. 对其他分区, 替换{{ic|1="codepage=cp437"}} 为 {{ic|1="codepage=437"}} , 会挂载失败导致systemd进入恢复模式否则,在linux 3.8应该会被修复。}}<br />
<br />
此外,只有({{ic|/}})根分区需要将最后一列设为{{ic|1}}其它应该为{{ic|2}}或{{ic|0}} (参阅[[fstab#Field definitions]]).<br />
<br />
=== Chroot 到新系统 ===<br />
下面要 [[chroot]] 到新安装的系统:<br />
# arch-chroot /mnt<br />
<br />
{{注意|可以使用{{ic|arch-chroot /mnt /bin/bash}}进入 bash shell.}}<br />
<br />
到这一步之后,开始系统的主要配置,如果下面文件不存在,需要手动创建。<br />
<br />
理解并完全安装步骤设置是保证系统配置成功的关键。<br />
<br />
===配置系统===<br />
<br />
==== Locale ====<br />
<br />
'''glibc''' 和其他一些支持本地化的程序或者库使用 Locales 自动翻译输出各种语言的"独特" 文本, 并且合适的显示地域、货币、时区以及日期格式、字符排列方式和其他一些本地化的特定标准。<br />
<br />
需要编辑两个文件:{{ic|locale.gen}} 和 {{ic|locale.conf}}.<br />
* 默认情况下 {{ic|/etc/locale.gen}} 是一个仅包含注释文档的空文件。选定你需要的本地化类型(移除前面的#即可), 比如中文系统可以使用:<br />
en_US.UTF-8 UTF-8<br />
zh_CN.GB18030 GB18030<br />
zh_CN.GBK GBK<br />
zh_CN.UTF-8 UTF-8<br />
zh_CN GB2312<br />
然后运行:<br />
# locale-gen<br />
<br />
每次'''glibc'''更新之后就会运行 {{ic|locale-gen}} 一次, 重新生成 {{ic|/etc/locale.gen}} 指定的本地化文件。<br />
<br />
*{{ic|locale.conf}} 文件默认不存在,一般设置{{ic|LANG}}就行了,它是其它设置的默认值。<br />
<br />
{{hc|/etc/locale.conf|2=<br />
LANG=zh_CN.UTF-8<br />
LC_TIME=en_GB.UTF-8<br />
}}<br />
<br />
要使用其它 {{ic|LC_*}} 变量,先运行 {{ic|locale}} 查看可用选项。高级示例可以参阅[[Locale#Setting_system-wide_locale|here]].<br />
<br />
{{警告|强烈不建议使用{{ic|LC_ALL}}变量,因为它会覆盖全部设置。}}<br />
<br />
==== 终端字体和键盘映射 ====<br />
编辑{{ic|/etc/vconsole.conf}}.<br />
:; KEYMAP: 可用的键盘映射位于{{ic|/usr/share/kbd/keymaps}}. 注意此设置仅对 TTY 起作用,不改变图形窗口或'''X'''的设置。<br />
:; FONT : 可用字体位于 {{ic|/usr/share/kbd/consolefonts/}},一般可用留空。<br />
:; FONT_MAP : 可选设置,定义要加载的映射,请参考 {{ic|man setfont}}。可以删除或者留空。<br />
<br />
'''Example:'''<br />
KEYMAP=us<br />
FONT=<br />
<br />
{{Note|详情参阅 [[Fonts#Console_fonts|Console fonts]] 和 {{ic|man vconsole.conf}}.}}<br />
<br />
==== 时区 ====<br />
编辑文件 {{ic|/etc/timezone}}.示例:<br />
Asia/Shanghai<br />
更多选项请阅读{{ic|man 5 timezone}}。<br />
<br />
同时,将{{ic|/etc/localtime}} 软链接到 {{ic|/usr/share/zoneinfo/Zone/SubZone}}.其中 {{ic|Zone}} 和 {{ic|Subzone}} 替换为所在时区,例如<br />
<br />
# ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime<br />
<br />
==== 硬件时间 ====<br />
在 {{ic|/etc/adjtime}} 中设置,默认、推荐的设置为[[Wikipedia:Coordinated Universal Time|UTC]]。详细信息请阅读[[Time (简体中文)]]。<br />
<br />
请在同一个机器使用同一个硬件时钟模式,否则它们会覆盖时间导致时间错乱。<br />
<br />
可以用下面命令自动生成 {{ic|/etc/adjtime}}:<br />
<br />
UTC(推荐使用):<br />
: {{注意|硬件时钟使用 [[Wikipedia:Coordinated Universal Time|UTC]] 不代表显示时间时使用 UTC.}}<br />
: {{bc|# hwclock --systohc --utc}}<br />
<br />
localtime,不推荐,但 Windows 默认使用此方式:<br />
: {{警告|使用 ''localtime'' 可能导致一些无法修复的 bug。但目前还没有取消此设置选项的计划。}}<br />
: {{bc|# hwclock --systohc --localtime}}<br />
<br />
如果要和 Windows 进行双系统启动:<br />
<br />
* Recommended: Set both Arch Linux and Windows to use UTC. A quick [[Time#UTC_in_Windows|registry fix]] is needed. Also, be sure to prevent Windows from synchronizing the time on-line, because the hardware clock will default back to ''localtime''. If you want such functionality (NTP sync), you should use [[ntpd]] on your Arch Linux installation instead.<br />
<br />
* Not recommended: Set Arch Linux to ''localtime'' and disable any time-related services, like {{ic|ntpd.service}}. This will let Windows take care of hardware clock corrections and you will need to remember to boot into Windows at least two times a year (in Spring and Autumn) when [[Wikipedia:Daylight_savings_time|DST]] kicks in. So please don't ask on the forums why the clock is one hour behind or ahead if you usually go for days or weeks without booting into Windows.<br />
<br />
==== 内核模块 ====<br />
{{Tip|一般情况下 udev 会自动加载需要的模块,大部分用户都不需要手动修改。这里只需要加入真正需要的模块。}}<br />
{{ic|/etc/modules-load.d/}}中保存内核启动时加入模块的配置文件。每个配置文件以{{ic|/etc/modules-load.d/<program>.conf}}的格式命名。配置文件中包含需要装入的内核列表,每个一行。空行和以 {{ic|#}} 或 {{ic|;}} 开头的行直接被忽略。示例:<br />
{{hc|/etc/modules-load.d/virtio-net.conf|<nowiki><br />
# Load virtio-net.ko at boot<br />
virtio-net</nowiki>}}<br />
<br />
==== Hostname ====<br />
<br />
按喜好设置[[Wikipedia:hostname|主机名]]:<br />
<br />
# echo ''myhostname'' > /etc/hostname<br />
<br />
{{注意|现在已经不需要再编辑{{ic|/etc/hosts}},系统默认安装的软件包{{Pkg|nss-myhostname}}会提供主机名解析。}}<br />
<br />
=== 配置网络===<br />
<br />
你需要再次配置网络,但此次是为你所安装的系统而配置。 该过程与[[#Establish_an_internet_connection|前面的]]配置方法基本相同,不同之处在于我们此刻将使其在计算机启动时自动运行。<br />
<br />
{{注意|了解更多网络配置相关信息, 请访问 [[#Configuring Network|配置网络]] 及 [[Wireless Setup|Wireless 设置]].}}<br />
<br />
==== Wired ====<br />
<br />
; 动态 IP<br />
<br />
If you only use a single fixed wired network connection, you do not need a network management service and can simply enable the {{ic|dhcpcd}} service, Where <interface> is your wired interface:<br />
<br />
# systemctl enable dhcpcd@<interface>.service<br />
<br />
Alternatively, you can use {{Pkg|netcfg}}'s {{ic|net-auto-wired}}, which gracefully handles dynamic connections to new networks:<br />
<br />
Install {{Pkg|ifplugd}}, which is required for {{ic|net-auto-wired}}:<br />
<br />
# pacman -S ifplugd<br />
<br />
Set up the dhcp profile and enable the {{ic|net-auto-wired}} service:<br />
<br />
# cd /etc/network.d<br />
# ln -s examples/ethernet-dhcp .<br />
# systemctl enable net-auto-wired.service<br />
<br />
; Static IP<br />
<br />
Install {{Pkg|ifplugd}}, which is required for {{ic|net-auto-wired}}:<br />
<br />
# pacman -S ifplugd<br />
<br />
Copy a sample profile from {{ic|/etc/network.d/examples}} to {{ic|/etc/network.d}}:<br />
<br />
# cd /etc/network.d<br />
# cp examples/ethernet-static .<br />
<br />
Edit the profile as needed:<br />
<br />
# nano ethernet-static<br />
<br />
Edit {{ic|/etc/conf.d/netcfg}} and modify the network interface name, most likely it is not eth0. You can find out more about the naming in the warning above.<br />
WIRED_INTERFACE="<interface>"<br />
<br />
Enable the {{ic|net-auto-wired}} service:<br />
<br />
# systemctl enable net-auto-wired.service<br />
<br />
==== Wireless ====<br />
<br />
You will need to install additional programs to be able to configure and manage wireless network profiles for [[netcfg]].<br />
<br />
[[NetworkManager]] and [[Wicd]] are other popular alternatives.<br />
<br />
* Install the required packages:<br />
<br />
# pacman -S wireless_tools wpa_supplicant wpa_actiond dialog<br />
<br />
If your wireless adapter requires a firmware (as described in the above [[#Wireless|Establish an internet connection]] section and also [[Wireless_Setup#Drivers_and_firmware|here]]), install the package containing your firmware. For example:<br />
<br />
# pacman -S zd1211-firmware<br />
<br />
* After finishing the rest of this installation and rebooting, you can connect to the network with {{ic|wifi-menu <interface>}} (where <interface> is the interface of your wireless chipset), which will generate a profile file in {{ic|/etc/network.d}} named after the SSID. There are also templates available in {{ic|/etc/network.d/examples/}} for manual configuration. <br />
<br />
# wifi-menu <interface><br />
<br />
{{Warning|If you're using wifi-menu, this must be done *after* your reboot when you're no longer chrooted. The process spawned by this command will conflict with the one you have running outside of the chroot. Alternatively, you could just configure a network profile manually using the templates previously mentioned so that you don't have to worry about using wifi-menu at all.}} <br />
<br />
* Enable the {{ic|net-auto-wireless}} service, which will connect to known networks and gracefully handle roaming and disconnects:<br />
<br />
# systemctl enable net-auto-wireless.service<br />
<br />
{{Note|[[Netcfg]] also provides {{ic|net-auto-wired}}, which can be used in conjunction with {{ic|net-auto-wireless}}.}}<br />
<br />
* Make sure that the correct wireless interface (usually {{ic|wlp3s0}}) is set in {{ic|/etc/conf.d/netcfg}}:<br />
<br />
{{hc|# nano /etc/conf.d/netcfg|2=<br />
WIRELESS_INTERFACE="wlp3s0"}}<br />
<br />
It is also possible to define a list of network profiles that should be automatically connected, using the {{ic|AUTO_PROFILES}} variable in {{ic|/etc/conf.d/netcfg}}. If {{ic|AUTO_PROFILES}} is not set, all known wireless networks will be tried.<br />
<br />
==== xDSL (PPPoE), analog modem or ISDN ====<br />
<br />
For xDSL, dial-up and ISDN connections, see [[Direct Modem Connection]].<br />
<br />
=== 配置 pacman ===<br />
下面将使用 [[Pacman (简体中文)|pacman]] 升级系统。 [[Pacman (简体中文)|Pacman]] 是 Arch Linux 的软件包管理器('''pac'''kage '''man'''ager),负责管理 Arch linux 的整个软件包系统,处理安装、删除、软件包降级(通过 cache)、自定义编译软件包、自动解决依赖关系删除和本地查找等等。现在将用 Pacman 从远程源下载软件包并安装到系统。<br />
<br />
可以通过阅读{{ic|man pacman(8)}},[[Pacman (简体中文)|Pacman Wiki 页面]]和 [[Pacman Rosetta|与其它软件包管理器的对比]]熟悉了解其使用方法。<br />
<br />
要修改 pacman 源或者选项,请编辑 {{ic|/etc/pacman.conf}}.<br />
<br />
[[Wikipedia:software repository|软件包源]] 是一个可以下载安装程序软件包的存储位置。Arch Linux [[Package Maintainer|软件包维护者]] (开发者和 [[Trusted Users]]) 维护了一系列官方软件源,包含了关键和流行的软件,可以通过 [[Pacman (简体中文)|pacman]] 直接获取。本文档将列出官方支持的软件源,更多信息及每个源的目的请参阅 [[Official Repositories (简体中文)|官方源]]。<br />
<br />
大部分人会使用 [core], [extra] 和 [community]。如果想在 Arch x86_64 上运行 32 位应用程序,请在 /etc/pacman.conf 中加入如下内容以启用 [multilib] 源:<br />
[multilib]<br />
SigLevel = PackageRequired<br />
Include = /etc/pacman.d/mirrorlist<br />
<br />
修改完成后需要用{{ic|pacman}} 的 {{ic|-Sy}} 参数更新服务器信息,否则会出现 "warning: database file for 'multilib' does not exist" 错误。<br />
<br />
[[Official Repositories (简体中文)|官方软件仓库]]包含了更详细的信息和各个软件源的目的。<br />
<br />
'''[[Arch User Repository (简体中文)|Arch 用户仓库]]''' 包含 [community] 和 [unsupported] 分支。与其它分支不同,[unsupported] 不包含二进制文件,设计为无法通过 pacman 直接安装。此分支收集 Arch 用户通过 [[Arch Build System|Arch 编译系统]] 编译软件的 [[PKGBUILD]] bash 脚本。[unsupported] 中的软件通常别的分支都没有,但 [unsupported] 软件包获得足够的投票后,一个 [[Trusted Users|授信用户]] 会认领它,将其加入 [community] 二进制源。<br />
<br />
{{注意|一些 [[AUR Helpers|pacman 帮助程序]] 可以无缝访问 AUR.}}<br />
<br />
=== 创建初始 ramdisk 环境 ===<br />
{{注意|大部分用户都不需要在安装时修改此文件。}}<br />
<br />
如有需要,配置 {{ic|/etc/mkinitcpio.conf}}(参阅 [[mkinitcpio]]) ,然后执行以下命令创建RAM盘:<br />
<br />
# mkinitcpio -p linux<br />
<br />
此文件负责微调初始内存文件系统 initramfs。(曾被称为初始化内存磁盘或 "initrd")。initramfs 是启动时供内核读取的压缩镜像。目的是引导系统使其能够读取根文件系统。所以它需要装入 IDE、SCSI、SATA 或 USB/FW 等设备所需模块。initramfs 手动或通过 udev 装入所需的模块后,会将控制权交给内核,继续启动过程。因此 initramfs 仅需要包含读取根文件系统的必要文件。大部分内核模块都是在初始化过程中由 udev 装入。<br />
<br />
如果根文件系统使用 RAID 或 LVM,必须配置正确的 HOOKS。详情请查看[[Installing with Software RAID or LVM| RAID]] 和[[Configuring mkinitcpio | /etc/mkinitcpio]]。如果使用非 US 键盘,请添加 "{{ic|keymap}} " 钩子以载入本地键盘映射。如果使用 USB 键盘,请添加 "{{ic|usbinput}}" 。否则,如果出了什么问题无法启动,系统会要求输入密码以进行维护,但确无法使用键盘。将 Arch 安装到外部硬盘、CF 卡或 SD 卡时,记得添加 "{{ic|usb}}"。最好加入 {{ic|fsck}} 以在系统启动时进行磁盘检查。示例:<br />
HOOKS="base udev autodetect pata scsi sata usb filesystems keymap usbinput fsck"<br />
<br />
如果需要通过如下设备启动,请按需要配置 HOOKS: USB 设备、FireWire、PCMCIA、NFS、软 RAID、LVM2 卷、加密卷 和 DSDT 等。<br />
<br />
Here you need to set the right [[Mkinitcpio#HOOKS|hooks]] if the root is on a USB drive, if you use RAID, LVM, or if {{ic|/usr}} is on a separate partition.<br />
<br />
Edit {{ic|/etc/mkinitcpio.conf}} as needed and re-generate the initramfs image with:<br />
# mkinitcpio -p linux<br />
<br />
{{Note|Arch VPS installations on QEMU (e.g. when using {{ic|virt-manager}}) may need {{ic|virtio}} modules in {{ic|mkinitcpio.conf}} to be able to boot.<br />
<br />
{{hc|# nano /etc/mkinitcpio.conf|2=<br />
MODULES="virtio virtio_blk virtio_pci virtio_net"}}}}<br />
<br />
=== 设置 Root 密码并创建一般用户 ===<br />
用 {{ic|passwd}} 设置一个root密码:<br />
# passwd<br />
<br />
{{警告|Linux 是个多用户环境。请不要使用 root 登录进行日常工作。这不仅仅是坏习惯,还非常危险。Root 账户仅是用来做管理任务的。}}<br />
<br />
因此,下面将创建用户''archie'',交互方式请使用{{ic|adduser}}。<br />
<br />
# useradd -m -g users -G wheel -s /bin/bash ''archie''<br />
# passwd archie<br />
<br />
新非 root 用户创建完成,同时还建立了用户主目录和登录密码。<br />
<br />
如果你弄错了账户设置,或者你想删除一个账户,或者你想要换个账户名,或者任何其他什么原因,使用 {{ic|/usr/sbin/userdel}} :<br />
# userdel -r ''archie''<br />
<br />
更多信息位于[[Users and Groups (简体中文)|用户和组]],也可以查看 {{ic|usermod(8)}} 和 {{ic|gpasswd(8)}} 的 man page。<br />
<br />
=== 安装配置启动加载器 ===<br />
<br />
==== For BIOS motherboards ====<br />
BIOS 系统由三个供选择 - Syslinux, GRUB, 和 [[LILO]]. 按个人喜好选择一个启动加载器,下面仅介绍 Syslinux 和 GRUB。 <br />
* Syslinux is (currently) limited to loading only files from the partition where it was installed. Its configuration file is considered to be easier to understand. An example configuration can be found [https://bbs.archlinux.org/viewtopic.php?pid=1109328#p1109328 here].<br />
* GRUB is more feature-rich and supports more complex scenarios. Its configuration file(s) is similar more to a scripting language, which may be difficult for beginners to manually write. It is recommended that they automatically generate one.<br />
<br />
===== Syslinux =====<br />
Install the {{Pkg|syslinux}} package and then use the {{ic|syslinux-install_update}} script to automatically ''install'' the files ({{ic|-i}}), mark the partition ''active'' by setting the boot flag ({{ic|-a}}), and install the ''MBR'' boot code ({{ic|-m}}):<br />
<br />
{{Note|If you have partitioned the drive as GPT, install {{Pkg|gptfdisk}} package, as well ({{ic|pacman -S gptfdisk}}), because it contains {{ic|sgdisk}}, which will be used to set the GPT-specific boot flag.}}<br />
<br />
# pacman -S syslinux<br />
# syslinux-install_update -iam<br />
<br />
编辑 {{ic|/boot/syslinux/syslinux.cfg}},将 / 指向正确的根分区,这是必须的,否则 Arch 启动不起来。将"sda3"修改为实际的根分区。同样,修改{{ic|LABEL archfallback}}.<br />
{{hc|# nano /boot/syslinux/syslinux.cfg|2=<br />
...<br />
LABEL arch<br />
...<br />
APPEND root=/dev/sda3 ro<br />
}}<br />
<br />
Syslinux 的详细使用和配置请阅读[[Syslinux]]。<br />
<br />
===== Grub =====<br />
{{Note|For GPT-partitioned drives on BIOS motherboards, GRUB needs a 2 MiB "[[GRUB#GPT_specific_instructions|BIOS Boot Partition]]".}}<br />
<br />
{{Note|Please do not use something like {{ic|/dev/sda1}} in the below command.}}<br />
<br />
Install the {{Pkg|grub-bios}} package and then run {{ic|grub-install}}:<br />
<br />
# pacman -S grub-bios<br />
# grub-install --target=i386-pc --recheck /dev/sda<br />
# cp /usr/share/locale/en\@quot/LC_MESSAGES/grub.mo /boot/grub/locale/en.mo<br />
<br />
虽然手动配置{{ic|grub.cfg}}完全可以工作,建议用户自动生成这个文件。<br />
<br />
要搜索硬盘上安装的其它操作系统,请先用 {{ic|# pacman -S os-prober}} 安装 {{Pkg|os-prober}}。<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
详细配置信息请阅读[[GRUB]]。<br />
<br />
==== For UEFI motherboards ====<br />
<br />
For UEFI boot, the drive needs to be GPT-partitioned, and a UEFI System Partition (512 MiB or higher, FAT32, type {{ic|EF00}}) must be present and mounted on {{ic|/boot/efi}}. If you have followed this guide from the beginning, you've already done all of these.<br />
<br />
While there are other [[UEFI_Bootloaders|UEFI bootloaders]] available, using EFISTUB is recommended. Below are instructions for setting up EFISTUB and GRUB.<br />
<br />
{{Note|Syslinux does not yet support UEFI.}}<br />
<br />
===== EFISTUB =====<br />
<br />
The Linux kernel can act as its own bootloader using EFISTUB. This is the UEFI boot method recommended by developers and simpler compared to {{ic|grub-efi-x86_64}}. The below steps set up rEFInd (a fork of rEFIt) to provide a menu for EFISTUB kernels, as well as for booting other UEFI bootloaders. You can also use [[UEFI Bootloaders#Using gummiboot|gummiboot]] instead of rEFInd. Both rEFInd and gummiboot can detect Windows UEFI bootloader in case of dual-boot.<br />
<br />
1. Boot in UEFI mode and load {{ic|efivars}} kernel module before chrooting:<br />
<br />
# modprobe efivars # before chrooting<br />
<br />
2. Mount the UEFISYS partition at {{ic|/mnt/boot/efi}}, chroot and [[UEFI_Bootloaders#Setting_up_EFISTUB|copy the kernel and initramfs files]] as described below.<br />
<br />
* Create {{ic|/boot/efi/EFI/arch/}} directory.<br />
<br />
* Copy {{ic|/boot/vmlinuz-linux}} to {{ic|/boot/efi/EFI/arch/vmlinuz-arch.efi}}. The {{ic|.efi}} file extension is very important as some UEFI firmwares refuse to launch a file without this extension. '''Important:''' Remember that the file is called vmlinu'''z''', but not vmlinu'''x'''.<br />
<br />
* Copy {{ic|/boot/initramfs-linux.img}} to {{ic|/boot/efi/EFI/arch/initramfs-arch.img}}.<br />
<br />
* Copy {{ic|/boot/initramfs-linux-fallback.img}} to {{ic|/boot/efi/EFI/arch/initramfs-arch-fallback.img}}.<br />
<br />
Every time the kernel and initramfs files are updated in {{ic|/boot}}, they need to be updated in {{ic|/boot/efi/EFI/arch}}. This can be automated either [[UEFI Bootloaders#Sync EFISTUB Kernel in UEFISYS partition using Systemd|using systemd]] or [[UEFI Bootloaders#Sync EFISTUB Kernel in UEFISYS partition using Incron|using incron]] (for non-systemd setups).<br />
<br />
3. In this guide you set up a bootloader GUI called rEFInd. Alternative bootloaders can be found on the page [[UEFI Bootloaders#Booting EFISTUB]].<br />
For the recommended rEFInd bootloader install the following packages:<br />
# pacman -S refind-efi efibootmgr<br />
<br />
4. Install rEFInd to the UEFISYS partition (summarized from [[UEFI Bootloaders#Using rEFInd]]):<br />
<br />
# mkdir -p /boot/efi/EFI/refind<br />
# cp /usr/lib/refind/refind_x64.efi /boot/efi/EFI/refind/refind_x64.efi<br />
# cp /usr/lib/refind/config/refind.conf /boot/efi/EFI/refind/refind.conf<br />
# cp -r /usr/share/refind/icons /boot/efi/EFI/refind/icons<br />
<br />
5. Create a {{ic|refind_linux.conf}} file with the kernel parameters to be used by rEFInd:<br />
<br />
{{hc|# nano /boot/efi/EFI/arch/refind_linux.conf|2=<br />
"Boot to X" "root=/dev/sdaX ro rootfstype=ext4 systemd.unit=graphical.target"<br />
"Boot to console" "root=/dev/sdaX ro rootfstype=ext4 systemd.unit=multi-user.target"}}<br />
<br />
{{Note|{{ic|refind_linux.conf}} is copied in the directory {{ic|/boot/efi/EFI/arch/}} where the initramfs and the kernel have been copied to in step 2. }}<br />
{{Note| In {{ic|refind_linux.conf}}, sdaX refers to your root file system, not your boot partition, if you created them separately. }}<br />
<br />
6. Add rEFInd to UEFI boot menu using [[UEFI#efibootmgr|efibootmgr]]. <br />
<br />
{{Warning|Using {{ic|efibootmgr}} on Apple Macs may brick the firmware and may need reflash of the motherboard ROM. For Macs, use {{AUR|mactel-boot}}, or "bless" from within Mac OS X.}}<br />
<br />
# efibootmgr -c -g -d /dev/sdX -p Y -w -L "rEFInd" -l '\EFI\refind\refind_x64.efi'<br />
<br />
{{Note|In the above command, X and Y denote the drive and partition of the UEFISYS partition. For example, in {{ic|/dev/sdc5}}, X is "c" and Y is "5".}}<br />
<br />
7. (Optional) As a fallback, in case {{ic|efibootmgr}} created boot entry does not work, copy {{ic|refind_x64.efi}} to {{ic|/boot/efi/EFI/boot/boot_x64.efi}} as follows:<br />
<br />
# cp -r /boot/efi/EFI/refind/* /boot/efi/EFI/boot/<br />
# mv /boot/efi/EFI/boot/refind_x64.efi /boot/efi/EFI/boot/boot_x64.efi<br />
<br />
===== GRUB =====<br />
<br />
{{Note|In case you have a system with 32-bit EFI, like pre-2008 Macs, install {{ic|grub-efi-i386}} instead, and use {{ic|1=--target=i386-efi}}.}}<br />
<br />
# pacman -S grub-efi-x86_64 efibootmgr<br />
# grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=arch_grub --recheck<br />
# cp /usr/share/locale/en\@quot/LC_MESSAGES/grub.mo /boot/grub/locale/en.mo<br />
<br />
The next command creates a menu entry for GRUB in the UEFI boot menu. However, as of {{Pkg|grub-efi-x86_64}} version 2.00, {{ic|grub-install}} tries to create a menu entry, so running {{ic|efibootmgr}} may not be necessary. See [[UEFI#efibootmgr]] for more info.<br />
<br />
# efibootmgr -c -g -d /dev/sdX -p Y -w -L "Arch Linux (GRUB)" -l '\\EFI\\arch_grub\\grubx64.efi'<br />
<br />
While using a manually created {{ic|grub.cfg}} is absolutely fine, it's recommended that beginners automatically generate one:<br />
<br />
{{Tip|To automatically search for other operating systems on your computer, install {{Pkg|os-prober}} before generating it:<br />
# pacman -S os-prober<br />
}}<br />
<br />
# grub-mkconfig -o /boot/grub/grub.cfg<br />
<br />
For more information on configuring and using GRUB, see [[GRUB]].<br />
<br />
===卸载分区并重启系统===<br />
如果还在 chroot 环境,先用 exit 命令退出系统:<br />
# exit<br />
卸载{{ic|/mnt}}中挂载的系统: <br />
# umount /mnt/{boot,home,}<br />
<br />
重启:<br />
# reboot<br />
<br />
{{小贴士|确保移除安装媒介并修改 BIOS 中的启动选项;否则可能会启动后会回到安装界面!}}<noinclude><br />
{{Beginners' Guide navigation (简体中文)}}</noinclude></div>
Cuihao
https://wiki.archlinux.org/index.php?title=Systemd_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=247758
Systemd (简体中文)
2013-02-18T05:53:38Z
<p>Cuihao: </p>
<hr />
<div>{{Lowercase title}}<br />
[[Category:Daemons and system services (简体中文)]]<br />
[[Category:Boot process (简体中文)]]<br />
[[en:Systemd]]<br />
[[es:Systemd]]<br />
[[fr:Systemd]]<br />
[[it:Systemd]]<br />
[[ja:Systemd]]<br />
[[ru:Systemd]]<br />
[[zh-TW:Systemd]]<br />
{{Article summary start|概览}}<br />
{{Article summary text|本文介绍 Systemd 的安装和配置。}}<br />
{{Article summary heading|相关页面}}<br />
{{Article summary wiki|Systemd/Services}}<br />
{{Article summary wiki|systemd/User}}<br />
{{Article summary wiki|Systemd FAQ}}<br />
{{Article summary wiki|init Rosetta}}<br />
{{Article summary wiki|udev (简体中文)}}<br />
{{Article summary end}}<br />
{{TranslationStatus (简体中文)|Systemd|2013-02-12|247103}}<br />
摘自[http://freedesktop.org/wiki/Software/systemd 项目主页]:<br />
<br />
'''''systemd''' 是 Linux 下的一款系统和服务管理器,兼容 SysV 和 LSB 的启动脚本。systemd 的特性有:支持并行化任务;同时采用 socket 式与 [[D-Bus (简体中文)|D-Bus]] 总线式激活服务;按需启动守护进程(daemon);利用 Linux 的 [[cgroups|cgroups]] 监视进程;支持快照和系统恢复;维护挂载点和自动挂载点;各服务间基于依赖关系进行精密控制。systemd 完全可以替代 Arch 曾经使用的 [[SysVinit (简体中文)|sysvinit]] 启动系统。''<br />
<br />
{{注意|[https://bbs.archlinux.org/viewtopic.php?pid&#61;1149530#p1149530 这个帖子]详细解释了 Arch 向 systemd 迁移的原因。}}<br />
<br />
另见[[Wikipedia:Systemd|维基百科上的介绍]]。<br />
<br />
== 迁移前需考虑 ==<br />
<br />
* 阅读[http://freedesktop.org/wiki/Software/systemd/ 该站],了解 systemd。<br />
* systemd 自己有一套日志('''journal''')系统,用于代替 '''syslog'''。两者也可以共存,参见后面的[[#日志|日志部分]]。<br />
* 虽然 systemd 可以替换 '''cron'''、'''acpid'''、'''xinetd''' 等的部分功能。至少目前还可以继续使用这些服务,无需立即切换。<br />
* 交互式 initscripts 启动脚本在 systemd 中无法工作。例如 '''netcfg-menu''' [https://bugs.archlinux.org/task/31377 无法]在启动时显示。<br />
<br />
== 安装 ==<br />
{{注意|从[https://www.archlinux.org/news/systemd-is-now-the-default-on-new-installations/ 2012-10-13版安装介质]开始,安装程序已经默认安装{{pkg|systemd}} 和 {{pkg|systemd-sysvcompat}}。}}<br />
{{注意|如果是在 VPS 中使用 Arch,请先阅读:[[Virtual_Private_Server#Moving_your_VPS_from_initscripts_to_systemd]]。}}<br />
<br />
本部分帮助还在使用{{pkg|sysvinit}} 和 {{pkg|initscripts}} 的用户迁移到{{pkg|systemd}}.<br />
<br />
# 从[[Official_Repositories_(简体中文)|官方软件仓库]]安装 {{pkg|systemd}} 并添加[[Kernel parameters|内核参数]]{{Ic|1=init=/usr/lib/systemd/systemd}}<br />
# 用 {{ic|systemctl enable ''daemonname''}} [[#使用单元|启用]] {{ic|/etc/rc.conf}} 中的守护进程。从 {{ic|/etc/rc.conf}} 到 systemd 服务的转换可以参考:[[Daemons_List_(简体中文)|守护进程列表]] 和 [[Systemd/Services|Services]]。<br />
# 重启系统,执行命令 {{ic|cat /proc/1/comm}},如果返回{{ic|systemd}},表示 systemd 已经正常启动。<br />
# 确认主机名已经正确设置:{{ic|hostnamectl set-hostname myhostname}}。<br />
# 删除 {{pkg|initscripts}} 和 {{pkg|sysvinit}},并安装{{pkg|systemd-sysvcompat}}.<br />
# (可选)删除{{ic|1=init=/usr/lib/systemd/systemd}}内核参数,现在已经不需要它了。{{pkg|systemd-sysvcompat}} 软件包提供了一个软链接,使 systemd 成为默认 init。<br />
<br />
=== 附加信息 ===<br />
* 如果内核参数中有 {{ic|quiet}},可以先删除它以便调试。<br />
{{Accuracy|reason=据有的用户称,systemd 并未对用户组有特殊要求。尽管有很多不常用的用户组,但从 consolekit 切换到 logind 后这些用户组没有发生改变。''(据译者理解:如果你碰到特殊的权限问题,可能仍需要加入这些用户组才能解决。)''}}<br />
* 使用 systemd 的时候'''无需'''将用户加入特殊[[Users and Groups (简体中文)|用户组]](如{{ic|sys}}、{{ic|disk}}、{{ic|lp}}、{{ic|network}}、{{ic|video}}、{{ic|audio}}、{{ic|optical}}、{{ic|storage}}、{{ic|scanner}}、{{ic|power}}等等)。加入这些组反而会有问题,例如audio组会导致程序阻塞软件混声。每个 PAM 登录都拥有一个 logind 会话,它通过[[Wikipedia:Access control list|POSIX ACLs]],赋予本地会话以声音设备访问权限、通过[[udisks]]挂载和卸载移动设备的权限等。<br />
{{注意|对于使用 {{ic|startx -- vt7}} 进入X环境的用户,由于 logind 会话无法应用于X环境,用户'''必须'''加入这些组。}}<br />
* 如果卸载 {{Pkg|initscripts}},{{ic|rc.conf}} 将不再有效。如果曾在其中设置了静态网络,或还未迁移到systemd的系统服务,请根据[[#Initscripts 模拟|该节]]配置。<br />
* 如果 fstab 中有 LVM 设备,首次启动时会因为等待超时进入急救控制台。输入管理员密码,执行 {{ic|systemctl enable lvm}} 启用相关服务,重启即可。<br />
<br />
== 原生 systemd 配置文件 ==<br />
{{注意|可能需要手动创建某些文件。所有文件的权限都是644,属主 root,属组 root。}}<br />
<br />
=== 主机名 ===<br />
主机名通过文件 {{ic|/etc/hostname}} 进行配置。该文件不应包含系统的域名(如果有)。使用如下命令设置主机名:<br />
# hostnamectl set-hostname '''myhostname'''<br />
<br />
详情参见 {{ic|man 5 hostname}} 和 {{ic|man 1 hostnamectl}}。<br />
<br />
文件示例:<br />
{{hc|/etc/hostname|<br />
myhostname<br />
}}<br />
<br />
=== 本地化设置(locale) ===<br />
系统默认 locale 记录在 {{ic|/etc/locale.conf}} 文件中。通过如下命令配置:<br />
# localectl set-locale LANG="zh_CN.utf8"<br />
<br />
{{注意|设置默认locale前,请确保已经生成该locale。方法是:在 {{ic|/etc/locale.gen}} 中启用对应locale,然后以root权限执行 {{ic|locale-gen}}。}}<br />
<br />
详情参见 {{ic|man 1 localectl}} 和 {{ic|man 5 locale.conf}}。<br />
<br />
* 另参阅 [[Locale (简体中文)]]。<br />
<br />
文件示例:<br />
{{hc|/etc/locale.conf|<nowiki><br />
LANG=en_US.utf8<br />
</nowiki>}}<br />
<br />
=== 虚拟控制台 ===<br />
{{ic|/etc/vconsole.conf}} 文件用来配置虚拟控制台,包括键盘布局和控制台字体:<br />
{{hc|/etc/vconsole.conf|<nowiki><br />
KEYMAP=us<br />
FONT=lat9w-16<br />
FONT_MAP=8859-1_to_uni</nowiki>}}<br />
<br />
{{注意|{{ic|{{Pkg|systemd}}-194}} 默认使用内核字体和键盘布局,因此以后不必在上述文件中留下空的 {{ic|1=KEYMAP=}} 和 {{ic|1=FONT=}} 设置。}}<br />
<br />
还可以通过命令设置键盘布局:<br />
# localectl set-keymap de<br />
<br />
{{ic|localectl}} 也可以设置X11的键盘布局:<br />
<br />
# localectl set-x11-keymap de<br />
<br />
详情参见 {{ic|man 1 localectl}} 和 {{ic|man 5 vconsole.conf}}。<br />
<br />
* 另参阅 [[Fonts (简体中文)#控制台字体|控制台字体]] 和 [[KEYMAP (简体中文)#键盘布局|键盘布局]]。<br />
<br />
=== 时区 ===<br />
时区可通过创建适当的{{ic|/etc/localtime}}符号链接来进行配置,该链接需指向目录{{ic|/usr/share/zoneinfo/}}下的一个zoneinfo文件。也可执行以下命令直接完成这一操作:<br />
<br />
# timedatectl set-timezone Asia/Shanghai<br />
<br />
更多细节参见{{ic|man 1 timedatectl}},{{ic|man 5 localtime}}和{{ic|man 7 archlinux}}。<br />
<br />
手动创建链接也行:<br />
# ln -sf ../usr/share/zoneinfo/Asia/Shanghai /etc/localtime<br />
<br />
旧的配置文件 {{ic|/etc/timezone}} 已经没用了,可以安全删除。<br />
<br />
=== 硬件时钟 ===<br />
<br />
systemd 默认识别硬件时钟为协调世界时(UTC)。<br />
<br />
{{小贴士|推荐使用[[NTP (简体中文)|NTP服务]]来在线同步硬件时钟。}}<br />
<br />
==== 硬件时钟设定为地方时 ====<br />
<br />
将硬件时钟配置为地方时('''不建议'''):<br />
<br />
# timedatectl set-local-rtc true<br />
<br />
重新调整为 UTC:<br />
<br />
# timedatectl set-local-rtc false<br />
<br />
如果设置成本地时间,处理夏令时有些麻烦。如果夏令时调整发生在关机时,下次启动时时间会出现问题([http://www.cl.cam.ac.uk/~mgk25/mswish/ut-rtc.html 更多信息])。最新的内核直接从实时时钟芯片(RTC)读取时间,不使用 {{ic|hwclock}},内核把从 RTC 读取的时间当作 UTC 处理。所以如果硬件时间是地方时,系统启动一开始识别的时间是错误的,之后很快会进行矫正。这可能导致一些问题(尤其是时间倒退时)。<br />
<br />
如果同时安装了 Windows 操作系统([http://blogs.msdn.com/b/oldnewthing/archive/2004/09/02/224672.aspx 默认使用地方时]),那么一般 RTC 会被设置为地方时。Windows 其实也能处理 UTC,需要[[Time (简体中文)#Windows 系统使用 UTC|修改注册表]]。建议让 Windows 使用 UTC,而非让 Linux 使用地方时。Windows 使用 UTC 后,请记得禁用 Windows 的时间同步功能,以防 Windows 错误设置硬件时间。如上文所说,Linux 可以使用[[NTP (简体中文)|NTP服务]]来在线同步硬件时钟。<br />
<br />
* 详情参阅 [[Time (简体中文)]]。<br />
<br />
=== 内核模块 ===<br />
目前,所有必要模块的加载均由 [[udev]] 自动完成。所以,如果不需要使用任何额外的模块,就没有必要在任何配置文件中添加启动时加载的模块。但是,有些情况下可能需要在系统启动时加载某个额外的模块,或者将某个模块列入黑名单以便使系统正常运行。<br />
<br />
==== 开机加载 ====<br />
systemd 读取 {{ic|/etc/modules-load.d/}} 中的配置加载额外的内核模块。配置文件名称通常为 {{ic|/etc/modules-load.d/<program>.conf}}。格式很简单,一行一个要读取的模块名,而空行以及第一个非空格字符为{{ic|#}}或{{ic|;}}的行会被忽略,如:<br />
{{hc|/etc/modules-load.d/virtio-net.conf|<nowiki><br />
# Load virtio-net.ko at boot<br />
virtio-net</nowiki>}}<br />
<br />
另见{{ic|man 5 modules-load.d}}。<br />
<br />
==== 禁用内核模块 ====<br />
禁用内核模块的方法和 Arch 默认的 {{Pkg|initscripts}} 相同,因为该过程实际由 {{Pkg|kmod}} 处理。参见:[[Kernel modules (简体中文)#黑名单]]。<br />
<br />
=== 文件系统挂载 ===<br />
<br />
默认行为是:在启动一个需要挂载特定分区的服务之前,系统自动检查并挂载分区。{{ic|/etc/fstab}} 中设定的网络文件系统(如 [[NFS (简体中文)|NFS]]、[[Samba (简体中文)|Samba]])无需配置即可正常工作,systemd 将确保网络文件系统在网络链接就绪后挂载。<br />
<br />
详情参阅:{{ic|man 5 systemd.mount}}。<br />
<br />
==== 自动挂载 ====<br />
<br />
* 如果 {{ic|/home}} 分区较大,可以让不依赖 {{ic|/home}} 分区的服务先启动。把下面的参数添加到 {{ic|/etc/fstab}} 文件中 {{ic|/home}} 项目的参数部分即可:<br />
<br />
noauto,x-systemd.automount<br />
<br />
这样 {{ic|/home}} 分区只有需要访问时才会被挂载。内核会缓存所有的文件操作,直到 {{ic|/home}} 分区准备完成。<br />
<br />
{{注意|这样做会使 {{ic|/home}} 的文件系统类型被识别为 {{ic|autofs}},造成 [[mlocate]] 查询时忽略该目录。实际加速效果因配置而异,所以请自己权衡是否需要。}}<br />
<br />
* 挂载远程文件系统也是同理。如果你仅想在需要的时候才挂载,也可以添加 {{ic|noauto,x-systemd.automount}} 参数。另外,可以设置 {{ic|1=x-systemd.device-timeout=#}} 参数,设置超时时间,以防止网络资源不能访问的时候浪费时间。<br />
<br />
* 如果你的加密文件系统需要密钥,则需要添加 {{ic|noauto}} 参数到 {{ic|/etc/crypttab}} 文件中的对应位置。systemd 开机的时候就不会打开这个加密设备,会一直等待到设备被访问时再使用密钥文件挂载。比如在使用加密RAID设备的时候可以节省一定的时间,因为 systemd 不必等到设备可用后才能访问。例如:<br />
<br />
{{hc|/etc/crypttab|<br />
data /dev/md0 /root/key noauto}}<br />
<br />
==== LVM ====<br />
<br />
如果装有不通过 [[Mkinitcpio (简体中文)|initramfs]] 激活的[[LVM (简体中文)|LVM]]卷,则需启动 {{ic|lvm-monitoring}} 服务(由 {{pkg|lvm2}} 软件包提供):<br />
<br />
# systemctl enable lvm-monitoring<br />
<br />
类似地,如果LVM卷在加密设备上,并且启动后才激活,则需启动 {{ic|lvm-on-crypt}} 服务(同样由 {{pkg|lvm2}} 软件包提供):<br />
<br />
# systemctl enable lvm-on-crypt<br />
<br />
=== ACPI 电源管理 ===<br />
systemd 能够处理某些电源相关的 [[Wikipedia:Advanced_Configuration_and_Power_Interface|ACPI]] 事件,通过 {{ic|/etc/systemd/logind.conf}} 的下列选项配置:<br />
* {{ic|HandlePowerKey}}:按下电源键后的动作<br />
* {{ic|HandleSleepKey}}:按下挂起键后的动作<br />
* {{ic|HandleHibernateKey}}: 按下休眠键后的动作<br />
* {{ic|HandleLidSwitch}}:合上笔记本盖后待机<br />
<br />
动作可以是 {{ic|ignore}}、{{ic|poweroff}}、{{ic|reboot}}、{{ic|halt}}、{{ic|suspend}}、{{ic|hibernate}}、{{ic|hybrid-sleep}}、{{ic|lock}} 或 {{ic|kexec}}。<br />
<br />
系统默认设置为:<br />
HandlePowerKey=poweroff<br />
HandleSuspendKey=suspend<br />
HandleHibernateKey=hibernate<br />
HandleLidSwitch=suspend<br />
<br />
如果不用图形界面,或者使用 [[i3 (简体中文)|i3]]、[[awesome (简体中文)|awesome]] 这样简单的桌面管理器时,systemd 可以替代 [[acpid (简体中文)|acpid]] 处理 ACPI 事件。<br />
<br />
{{注意|systemd 无法处理交流电源和电池 ACPI 事件,所以还得使用 [[Laptop Mode Tools (简体中文)|Laptop Mode Tools]] 或 [[acpid (简体中文)|acpid]] 工具。}}<br />
<br />
在当前版本的 systemd 中,这些 {{ic|Handle}} 选项将会被应用到整个系统当中,除非它们被别的程序——例如某个桌面环境中的电源管理器——给“阻止”(inhibited)。如果其它的程序没有阻止这些 {{ic|Handle}} ,你可能会先被 systemd 挂起你的系统,然后当系统被唤醒之后,电源管理器又会再次将系统挂起。<br />
<br />
{{警告|目前只有[[GNOME]] 和 [[KDE]] 支持 "inhibited" 命令。在其它的桌面管理器同样实现该功能之前,如果你想使用[[Xfce]]、[[acpid]] 或者其它程序来管理 ACPI 事件,你需要把 {{ic|Handle}} 选项设置为 {{ic|ignore}} 。包含该功能的新版本还在开发当中。}}<br />
<br />
{{注意|除了内核默认的待机支持后端(用于处理待机/休眠),systemd 也可以使用其他后端(比如 [[Uswsusp]] 或 [[TuxOnIce]])。}}<br />
<br />
==== 休眠时执行的脚本 ====<br />
<br />
使用 {{ic|systemctl suspend}}、{{ic|systemctl hibernate}} 或 {{ic|systemctl hybrid-sleep}} 命令执行待机/休眠时,systemd 不会调用 [[pm-utils (简体中文)|pm-utils]]。[[pm-utils (简体中文)|pm-utils]] 的钩子扩展(hook)——包括 [[pm-utils (简体中文)#建立你自己的钩子|自定义钩子]]——会失效。不过,systemd 提供了两种类似的待机/休眠时执行脚本的机制。<br />
<br />
===== 使用服务文件 =====<br />
<br />
可以将服务文件附在 suspend.target、hibernate.target 或 sleep.target 中,这样就能在待机/休眠前后执行某些操作。用户级操作和root/系统级操作应该使用不同的服务文件。要启用用户级服务文件,使用 {{ic|# systemctl enable suspend@<用户名> && systemctl enable resume@<用户名>}}。例如:<br />
<br />
{{hc|/etc/systemd/system/suspend@.service|2=<nowiki><br />
[Unit]<br />
Description=User suspend actions<br />
Before=sleep.target<br />
<br />
[Service]<br />
User=%I<br />
Type=forking<br />
Environment=DISPLAY=:0<br />
ExecStartPre= -/usr/bin/pkill -u %u unison ; /usr/local/bin/music.sh stop ; /usr/bin/mysql -e 'slave stop'<br />
ExecStart=/usr/bin/sflock<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
{{hc|/etc/systemd/system/resume@.service|2=<nowiki><br />
[Unit]<br />
Description=User resume actions<br />
After=suspend.target<br />
<br />
[Service]<br />
User=%I<br />
Type=simple<br />
ExecStartPre=/usr/local/bin/ssh-connect.sh<br />
ExecStart=/usr/bin/mysql -e 'slave start'<br />
<br />
[Install]<br />
WantedBy=suspend.target</nowiki>}}<br />
<br />
至于root/系统级服务(使用 {{ic|# systemctl enable root-suspend}} 激活):<br />
<br />
{{hc|/etc/systemd/system/root-resume.service|2=<nowiki><br />
[Unit]<br />
Description=Local system resume actions<br />
After=suspend.target<br />
<br />
[Service]<br />
Type=simple<br />
ExecStart=/usr/bin/systemctl restart mnt-media.automount<br />
<br />
[Install]<br />
WantedBy=suspend.target</nowiki>}}<br />
<br />
{{hc|/etc/systemd/system/root-suspend.service|2=<nowiki><br />
[Unit]<br />
Description=Local system suspend actions<br />
Before=sleep.target<br />
<br />
[Service]<br />
Type=simple<br />
ExecStart=-/usr/bin/pkill sshfs<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
上述服务文件的一些解释(详见 {{ic|man systemd.service}}):<br />
* 如果设置 {{ic|1=<nowiki>Type=OneShot</nowiki>}},那么可以使用多个 {{ic|1=<nowiki>ExecStart=</nowiki>}} 参数。否则只能写一个,替代方案是在 {{ic|ExecStartPre}} 中添加命令,或使用分号分隔不同命令(见第一个例子,分号前后的空格都是'''必须'''的)。 <br />
* 若命令前加上一个“-”(半角减号),则命令返回非零值时会被忽略、当作正常执行处理。<br />
* 有关调试,最好的方法是用 {{ic|journalctl}} 查看日志。<br />
<br />
===== 合并待机和唤醒服务文件 =====<br />
<br />
利用下面这种自定义的一体化待机和唤醒服务,使用单一的钩子扩展即可对不同操作(待机/休眠/混合休眠)的不同阶段(进入/唤醒)进行控制。<br />
<br />
例子和解释:<br />
<br />
{{hc|/etc/systemd/system/wicd-sleep.service|2=<nowiki><br />
[Unit]<br />
Description=Wicd sleep hook<br />
Before=sleep.target<br />
StopWhenUnneeded=yes<br />
<br />
[Service]<br />
Type=oneshot<br />
RemainAfterExit=yes<br />
ExecStart=-/usr/share/wicd/daemon/suspend.py<br />
ExecStop=-/usr/share/wicd/daemon/autoconnect.py<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
* {{ic|1=<nowiki>RemainAfterExit=yes</nowiki>}}:服务启动后,除非显式地停止,否则就认为是活动的。<br />
* {{ic|1=<nowiki>StopWhenUnneeded=yes</nowiki>}}:服务活动时,如果无其他服务依赖该服务,就停止它。在本例中,该服务会在 sleep.target 停止后停止活动。<br />
* 由于 sleep.target 会被 suspend.target、hibernate.target、hybrid-sleep.target 调用,且 sleep.target 本身设置了 StopWhenUnneeded,该服务文件对以上各种操作都是有效的。<br />
<br />
===== 使用 /usr/lib/systemd/system-sleep =====<br />
<br />
systemd 在待机/休眠时执行 {{ic|/usr/lib/systemd/system-sleep/}} 里的所有脚本,传递下面两个参数:<br />
<br />
* 参数1:若是准备进入待机/休眠状态,则为 {{ic|pre}};唤醒时为 {{ic|post}}。<br />
* 参数2:事件名称,{{ic|suspend}},{{ic|hibernate}} 或 {{ic|hybrid-sleep}}。<br />
<br />
systemd 会同时执行所有脚本,而不是像 [[pm-utils (简体中文)|pm-utils]] 那样顺序执行。<br />
<br />
脚本输出会记录在相关服务({{ic|systemd-suspend.service}}、{{ic|systemd-hibernate.service}} 或 {{ic|systemd-hybrid-sleep.service}})中。通过[[#日志|日志]]查看:<br />
# journalctl -b -u systemd-suspend<br />
<br />
脚本示范:<br />
<br />
{{hc|/usr/lib/systemd/system-sleep/example.sh|<br />
#!/bin/sh<br />
case $1/$2 in<br />
pre/*)<br />
echo "进入 $2 状态..."<br />
;;<br />
post/*)<br />
echo "从 $2 状态唤醒..."<br />
;;<br />
esac}}<br />
<br />
记得添加可执行权限: <br />
# chmod a+x /usr/lib/systemd/system-sleep/example.sh<br />
<br />
详情参见 {{ic|man 7 systemd.special}} 和 {{ic|man 8 systemd-sleep}}。<br />
<br />
=== 临时文件 ===<br />
{{ic|/usr/lib/tmpfiles.d/}} 和 {{ic|/etc/tmpfiles.d/}} 中的文件描述了 systemd-tmpfiles 如何创建、清理、删除临时文件和目录,这些文件和目录通常存放在 {{ic|/run}} 和 {{ic|/tmp}} 中。配置文件名称为 {{ic|/etc/tmpfiles.d/<program>.conf}}。此处的配置能覆盖 {{ic|/usr/lib/tmpfiles.d/}} 目录中的同名配置。<br />
<br />
临时文件通常和服务文件同时提供,以生成守护进程需要的文件和目录。例如 [[Samba (简体中文)|Samba]] 服务需要目录 {{ic|/run/samba}} 存在并设置正确的权限位,就象这样:<br />
{{hc|/usr/lib/tmpfiles.d/samba.conf|<br />
D /run/samba 0755 root root<br />
}}<br />
<br />
此外,临时文件还可以用来在开机时向特定文件写入某些内容。比如,要禁止系统从USB设备唤醒,利用旧的 {{ic|/etc/rc.local}} 可以用 {{ic|echo USBE > /proc/acpi/wakeup}},而现在可以这么做:<br />
{{hc|/etc/tmpfiles.d/disable-usb-wake.conf|<br />
w /proc/acpi/wakeup - - - - USBE<br />
}}<br />
<br />
详情参见 {{ic|man 5 tmpfiles.d}}。<br />
<br />
=== 单元(unit) ===<br />
一个单元配置文件可以描述如下内容之一:系统服务、socket、系统设备、挂载点、交换分区/文件、启动目标(target)、文件系统路径、由 systemd 管理的计时器。文件格式受 .desktop 文件启发,而最初起源是 Windows 下的 .ini 文件。<br />
<br />
详情参阅 {{ic|man 5 systemd.unit}}.<br />
<br />
== 从 initscripts 迁移到 systemd ==<br />
<br />
=== initscripts 模拟 ===<br />
<br />
{{Pkg|initscripts}} 软件包提供了 systemd 和 Arch 传统配置的整合。当同时安装了{{Pkg|initscripts}}和systemd时,若以systemd启动系统,systemd将执行以下操作:<br />
<br />
# 解析{{ic|/etc/rc.conf}}中的{{ic|DAEMONS}}数组,并在系统启动时,启动所有列出的守护进程<br />
# 在启动过程中执行{{ic|/etc/rc.local}}<br />
# 在系统关闭过程中执行{{ic|/etc/rc.local.shutdown}}<br />
<br />
Initscripts模拟仅仅是作为用户迁移到systemd时的过渡措施,这一方案最终将'''不再可行'''。原生的systemd并不依赖于{{ic|rc.conf}}文件的集中式配置,因此推荐使用[[#原生 systemd 配置文件|原生的systemd配置文件]]。原生systemd配置文件比{{ic|/etc/rc.conf}}具有更高优先级。<br />
<br />
{{注意|替换{{ic|/etc/rc.local}}的推荐方式是针对系统启动时要运行的程序编写自定义的service文件,详见相关[[#自己编写_.service_文件|章节]].}}<br />
<br />
{{注意|如果你曾经在{{ic|/etc/inittab}}中禁用了以{{keypress|Ctrl+Alt+Del}}重启系统,则需要以root执行{{ic|systemctl mask ctrl-alt-del.target}}以使其对systemd重新生效。}}<br />
<br />
{{警告|如果同时安装 systemd(197-4 及后续版本)和 initscripts,那么 {{ic|/etc/rc.local}} 脚本会导致启动过程无法结束(原因是 getty@tty1.service 自 197-4 版本起不再等待 rc-local.service 执行,getty 无法启动)。请删除或重命名 {{ic|/etc/rc.local}}。}}<br />
<br />
=== 迁移 DAEMONS 列表 ===<br />
<br />
若要用纯 systemd 环境,则应当删除 {{ic|/etc/rc.conf}} 文件,并完全通过 {{ic|systemctl}} 命令来启动服务。对于 {{ic|DAEMONS}} 列表里面的每个 {{ic|<服务名>}},现在可以用以下命令设置成自动启动:<br />
<br />
# systemctl enable <服务名>.service<br />
<br />
{{小贴士|[[Daemons List|这里]]有一份 initscripts 和 systemd 下常见服务的对应列表。}}<br />
<br />
如果提示 {{ic|<service_name>.service}} 不存在:<br />
<br />
* 很有可能是 systemd 用了不同的服务名。比如 {{ic|crond}} 服务替换成 {{ic|cronie.service}};{{ic|alsa}} 服务替换成了 {{ic|alsa-store.service}} 和 {{ic|alsa-restore.service}} 两个服务。另一个值得注意的是 {{ic|network}} 服务,它被替换为一整套服务文件(详情参见 [[Configuring_Network_(简体中文)]])。<br />
* 另外,有些程序尚未提供 .service 文件,还得继续用 {{ic|rc.conf}} 来配置开机自动启动。<br />
<br />
{{小贴士|用下面这个命令来查看已安装软件包包含的服务文件:<br />
$ pacman -Ql cronie<br />
[...]<br />
cronie /etc/rc.d/crond # DAEMONS 列表中使用的 initscript 服务 (原生 systemd 配置中不再使用)<br />
[...]<br />
cronie /usr/lib/systemd/system/cronie.service # 原生 systemd 服务文件<br />
[...]<br />
}}<br />
<br />
* 有些服务不需要用户明确配置自启动。比如安装 {{ic|dbus-core}} 软件包后 {{ic|dbus.service}} 就会自动被配置为自启动。而 {{ic|alsa-store.service}} 和 {{ic|alsa-restore.service}} 也会由 systemd 自动启动。可以用 {{ic|systemctl list-unit-files}} 命令来查看服务列表并检查运行状态。<br />
<br />
== systemd 基本工具 ==<br />
<br />
检视和控制systemd的主要命令是{{ic|systemctl}}。该命令可用于查看系统状态和管理系统及服务。详见{{ic|man 1 systemctl}}。<br />
<br />
{{小贴士|在 {{ic|systemctl}} 参数中添加 {{ic|-H <用户名>@<主机名>}} 可以实现对其他机器的远程控制。该过程使用 [[SSH (简体中文)|SSH]] 链接。}}<br />
<br />
{{注意|{{ic|systemadm}} 是 systemd 的官方图形前端。由 [[AUR (简体中文)|AUR]] 中的软件包 {{AUR|systemd-ui-git}} 提供。}}<br />
<br />
=== 分析系统状态 ===<br />
<br />
输出激活的单元:<br />
<br />
$ systemctl<br />
<br />
以下命令等效:<br />
<br />
$ systemctl list-units<br />
<br />
输出运行失败的单元:<br />
<br />
$ systemctl --failed<br />
<br />
所有可用的单元文件存放在 {{ic|/usr/lib/systemd/system/}} 和 {{ic|/etc/systemd/system/}} 目录(后者优先级更高)。查看所有已安装服务:<br />
$ systemctl list-unit-files<br />
<br />
=== 使用单元 ===<br />
一个单元可以是系统服务({{ic|.service}})、挂载点({{ic|.mount}})、sockets({{ic|.sockets}})。<br />
<br />
使用 {{ic|systemctl}} 控制单元时,通常需要使用单元文件的全名,包括扩展名(例如 {{ic|sshd.service}})。但是有些单元可以在{{ic|systemctl}}中使用简写方式。<br />
* 如果无扩展名,systemctl 默认把扩展名当作 {{ic|.service}}。例如 {{ic|netcfg}} 和 {{ic|netcfg.service}} 是等价的。<br />
* 挂载点会自动转化为相应的 {{ic|.mount}} 单元。例如 {{ic|/home}} 等价于 {{ic|home.mount}}。<br />
* 设备会自动转化为相应的 {{ic|.device}} 单元,所以 {{ic|/dev/sda2}} 等价于 {{ic|dev-sda2.device}}。<br />
<br />
立即激活单元:<br />
<br />
# systemctl start <单元><br />
<br />
立即停止单元:<br />
# systemctl stop <单元><br />
<br />
重启单元:<br />
<br />
# systemctl restart <单元><br />
<br />
命令单元重新读取配置:<br />
<br />
# systemctl reload <单元><br />
<br />
输出单元运行状态:<br />
<br />
$ systemctl status <单元><br />
<br />
检查单元是否配置为自动启动:<br />
$ systemctl is-enabled <单元><br />
<br />
开机自动激活单元:<br />
<br />
# systemctl enable <单元><br />
<br />
{{注意|如果服务没有{{ic|Install}}段落,一般意味着应该通过其它服务自动调用它们。如果真的需要手动安装,可以直接连接服务,如下(将{{ic|foo}}替换为真实的服务名):<br />
{{bc|# ln -s /usr/lib/systemd/system/foo.service /etc/systemd/system/graphical.target.wants/}}<br />
}}<br />
<br />
取消开机自动激活单元:<br />
<br />
# systemctl disable <单元>}}<br />
<br />
显示单元的手册页(必须由单元文件提供):<br />
<br />
# systemctl help <单元><br />
<br />
重新载入 systemd,扫描新的或有变动的单元:<br />
# systemctl daemon-reload<br />
<br />
=== 电源管理 ===<br />
<br />
安装 {{ic|polkit}} 后才可使用电源管理。<br />
<br />
如果你正登录在一个本地的{{ic|systemd-logind}}用户会话,且当前没有其它活动的会话,那么以下命令无需root权限即可执行。否则(例如,当前有另一个用户登录在某个tty),systemd 将会自动请求输入root密码。<br />
<br />
重启:<br />
<br />
$ systemctl reboot<br />
<br />
退出系统并停止电源:<br />
<br />
$ systemctl poweroff<br />
<br />
待机:<br />
<br />
$ systemctl suspend<br />
<br />
休眠:<br />
<br />
$ systemctl hibernate<br />
<br />
混合休眠模式(同时休眠到硬盘并待机):<br />
<br />
$ systemctl hybrid-sleep<br />
<br />
== 启动桌面环境 ==<br />
<br />
=== 使用登录管理器 ===<br />
<br />
通过启动[[Display Manager (简体中文)|登录管理器]](或称显示管理器),即可进行图形界面登录。目前,Arch 提供了 [[GDM (简体中文)|GDM]]、[[KDM (简体中文)|KDM]]、[[SLiM (简体中文)|SLiM]]、[[XDM (简体中文)|XDM]]、[[LXDM (简体中文)|LXDM]] 和 [[LightDM (简体中文)|LightDM]] 的 systemd 服务文件。以 KDM 为例,配置开机启动:<br />
# systemctl enable kdm.service<br />
<br />
执行上述命令后,登录管理器应当能正常工作了。如果不是的话,很可能是因为你修改了{{ic|default.target}}。默认情况应当如下:<br />
<br />
{{hc|# ls -l /etc/systemd/system/default.target|/etc/systemd/system/default.target -> /usr/lib/systemd/system/graphical.target}}<br />
<br />
删除被修改的 {{ic|default.target}} 即可,systemd 会自动使用默认配置(即 {{ic|graphical.target}}):<br />
<br />
# rm /etc/systemd/system/default.target<br />
<br />
=== 使用 systemd-logind ===<br />
<br />
{{注意|自 2012-10-30 起, [[ConsoleKit]] 已被 [https://www.archlinux.org/news/consolekit-replaced-by-logind/ systemd-logind 取代] ,作为登录到桌面环境的默认方式。}}<br />
<br />
可使用 {{ic|loginctl}} 来查看用户会话的状态。所有 [[PolicyKit]] 操作,如挂起系统、挂载外部驱动器,都无需配置即可使用。<br />
<br />
$ loginctl show-session $XDG_SESSION_ID<br />
<br />
== 自己编写 .service 文件 ==<br />
''参见:[[Systemd/Services]]''<br />
<br />
=== 处理依赖关系 ===<br />
<br />
使用systemd时,可通过正确编写单元配置文件来解决其依赖关系。典型的情况是,单元{{ic|A}}要求单元{{ic|B}}在{{ic|A}}启动之前运行。在此情况下,向单元{{ic|A}}配置文件中的 {{ic|[Unit]}} 段添加 {{ic|1=Requires=B}} 和 {{ic|1=After=B}} 即可。若此依赖关系是可选的,可添加 {{ic|1=Wants=B}} 和 {{ic|1=After=B}}。请注意 {{ic|1=Wants=}} 和 {{ic|1=Requires=}} 并不意味着 {{ic|1=After=}},即如果 {{ic|1=After=}} 选项没有制定,这两个单元将被并行启动。<br />
<br />
依赖关系通常被用在服务(service)而不是[[#目标(target)|目标(target)]]上。例如, {{ic|network.target}} 一般会被某个配置网络接口的服务引入,所以,将自定义的单元排在该服务之后即可,因为 {{ic|network.target}} 已经启动。<br />
<br />
=== 启动方式 ===<br />
<br />
编写自定义的service文件时,可以选择几种不同的服务启动方式。启动方式可通过配置文件 {{ic|[Service]}} 段中的 {{ic|1=Type=}} 参数进行设置。具体的参数说明请参阅 {{ic|man systemd.service}} 。<br />
<br />
* {{ic|1=Type=simple}}:systemd认为该服务将立即启动。服务进程不会fork。如果该服务要启动其他服务,不要使用此类型启动,除非该服务是socket激活型。<br />
* {{ic|1=Type=forking}}:systemd认为当该服务进程fork,且父进程退出后服务启动成功。对于常规的守护进程(daemon),除非你确定此启动方式无法满足需求,使用此类型启动即可。使用此启动类型应同时指定 {{ic|1=PIDFile=}},以便systemd能够跟踪服务的主进程。<br />
* {{ic|1=Type=oneshot}}:这一选项适用于只执行一项任务随后退出的服务。你可能需要同时设置 {{ic|1=RemainAfterExit=yes}} 使得 systemd 在服务进程退出之后仍然认为服务处于激活状态。<br />
* {{ic|1=Type=notify}}:与 {{ic|1=Type=simple}} 相同,但约定服务会在就绪后向 systemd 发送一个信号。这一通知的实现由 {{ic|libsystemd-daemon.so}} 提供。<br />
* {{ic|1=Type=dbus}}:若以此方式启动,当指定的 {{ic|BusName}} 出现在DBus系统总线上时,systemd认为服务就绪。<br />
<br />
=== 替换现存单元文件 ===<br />
<br />
目录 {{ic|/etc/systemd/system/}} 中的单元配置文件比 {{ic|/usr/lib/systemd/system/}} 目录下的文件拥有更高优先级。<br />
若需要定制某个单元的启动(使配置不受软件包更新的影响),请将现有的单元配置文件从 {{ic|/usr/lib/}} 复制到 {{ic|/etc/}} 目录下,并在此目录中修改。或者,可以使用 {{ic|.include}} 来解析一个现有的配置文件,并覆盖原选项或添加新选项。例如,若想要某个服务的配置文件中添加额外依赖关系,可以编写:<br />
<br />
{{hc|/etc/systemd/system/<服务名>.service|2=<br />
.include /usr/lib/systemd/system/<服务名>.service<br />
<br />
[Unit]<br />
Requires=<新依赖><br />
After=<新依赖>}}<br />
<br />
然后运行以下命令使更改生效:<br />
<br />
# systemctl reenable <单元><br />
# systemctl restart <单元><br />
<br />
{{小贴士|可以用 {{ic|systemd-delta}} 命令来查看哪些单元文件被覆盖、哪些被修改。}}<br />
<br />
=== 单元配置文件的 vim 语法高亮支持 ===<br />
<br />
可从 [[Official Repositories|官方仓库]] 安装 {{Pkg|vim-systemd}} 软件包,使 unit 配置文件在 [[Vim]] 下支持语法高亮。<br />
<br />
== 目标(target) ==<br />
启动级别(runlevel)是一个旧的概念。现在,systemd 引入了一个和启动级别功能相似又不同的概念——目标(target)。不像数字表示的启动级别,每个''目标''都有名字和独特的功能,并且能同时启用多个。一些''目标''继承其他''目标''的服务,并启动新服务。systemd 提供了一些模仿 sysvinit 启动级别的''目标'',仍可以使用旧的 {{ic|telinit 启动级别}} 命令切换。<br />
<br />
=== 获取当前目标 ===<br />
不要使用 {{ic|runlevel}} 命令了:<br />
$ systemctl list-units --type=target<br />
<br />
=== 创建新目标 ===<br />
在 Fedora 中,启动级别 0、1、3、5、6 都被赋予特定用途,并且都对应一个 systemd 的''目标''。然而,没有什么很好的移植用户定义的启动级别(2、4)的方法。要实现类似功能,可以以原有的启动级别为基础,创建一个新的''目标'' {{ic|/etc/systemd/system/<新目标>}}(可以参考 {{ic|/usr/lib/systemd/system/graphical.target}}),创建 {{ic|/etc/systemd/system/<新目标>.wants}} 目录,向其中加入额外服务的链接(指向 {{ic|/usr/lib/systemd/system/}} 中的单元文件)。<br />
<br />
=== 目标表 ===<br />
{| border="1"<br />
!SysV 启动级别!!Systemd 目标!!注释<br />
|-<br />
| 0 || runlevel0.target, poweroff.target || 中断系统(halt)<br />
|-<br />
| 1, s, single || runlevel1.target, rescue.target || 单用户模式<br />
|-<br />
| 2, 4 || runlevel2.target, runlevel4.target, multi-user.target || 用户自定义启动级别,通常识别为级别3。<br />
|-<br />
| 3 || runlevel3.target, multi-user.target || 多用户,无图形界面。用户可以通过终端或网络登录。<br />
|-<br />
| 5 || runlevel5.target, graphical.target || 多用户,图形界面。继承级别3的服务,并启动图形界面服务。<br />
|-<br />
| 6 || runlevel6.target, reboot.target || 重启<br />
|-<br />
| emergency || emergency.target || 急救模式(Emergency shell)<br />
|-<br />
|}<br />
<br />
=== 切换启动级别/目标 ===<br />
systemd 中,启动级别通过“目标单元”访问。通过如下命令切换:<br />
# systemctl isolate graphical.target<br />
该命令对下次启动无影响。等价于{{ic|telinit 3}} 或 {{ic|telinit 5}}。<br />
<br />
=== 修改默认启动级别/目标 ===<br />
开机启动进的目标是 {{ic|default.target}},默认链接到 {{ic|graphical.target}} (大致相当于原来的启动级别5)。可以通过[[Kernel parameters (简体中文)|内核参数]]更改默认启动级别:<br />
<br />
{{小贴士|可以省略扩展名 {{ic|.target}}。}}<br />
<br />
* {{ic|1=systemd.unit=multi-user.target}} (大致相当于级别3)<br />
* {{ic|1=systemd.unit=rescue.target}} (大致相当于级别1)<br />
<br />
另一个方法是修改 {{ic|default.target}}。可以通过 {{ic|systemctl}} 修改它:<br />
<br />
# systemctl enable multi-user.target<br />
<br />
命令执行情况由 {{ic|systemctl}} 显示:链接 {{ic|/etc/systemd/system/default.target}} 被创建,指向新的默认启动级别。该方法当且仅当目标配置文件中有以下内容时有效:<br />
<br />
[Install]<br />
Alias=default.target<br />
<br />
目前,{{ic|multi-user.target}}、{{ic|graphical.target}} 都包含这段内容。<br />
<br />
== 日志 ==<br />
自版本 38 起,systemd 提供了自己日志系统(logging system),称为 journal. 使用 systemd 日志,无需额外安装日志服务(syslog)。读取日志的命令:<br />
# journalctl<br />
<br />
默认情况下(当 {{ic|Storage&#61;}} 在文件 {{ic|/etc/systemd/journald.conf}} 中被设置为 {{ic|auto}}),日志记录将被写入 {{ic|/var/log/journal/}}。若目录 {{ic|/var/log/journal/}} 不存在(例如,被用户或某个程序删除),systemd将'''不会'''自动创建它,而是将日志记录写入 {{ic|/run/systemd/journal}}。这意味着,系统重启后日志将丢失。<br />
<br />
=== 过滤输出 ===<br />
<br />
{{ic|journalctl}}可以根据特定字段过滤输出,例如:<br />
<br />
显示本次启动后的所有日志:<br />
<br />
# journalctl -b<br />
<br />
不过,一般大家更关心的不是本次启动后的日志,而是上次启动时的(例如,刚刚系统崩溃了)。目前还没有这项功能,正在 [http://comments.gmane.org/gmane.comp.sysutils.systemd.devel/6608 systemd-devel@lists.freedesktop.org] 讨论中。<br />
<br />
目前的折中方案是:<br />
<br />
# journalctl --since=today | tac | sed -n '/-- Reboot --/{n;:r;/-- Reboot --/q;p;n;b r}' | tac<br />
<br />
以上命令输出本日内的所有启动信息。但要注意,如果日志很多,该命令执行时间会比较漫长。<br />
<br />
动态跟踪最新信息:<br />
# journalctl -f<br />
<br />
显示特定程序的所有消息:<br />
{{bc|# journalctl /usr/lib/systemd/systemd}}<br />
<br />
显示特定进程的所有消息:<br />
{{bc|1=# journalctl _PID=1}}<br />
<br />
显示指定单元的所有消息:<br />
# journalctl -u netcfg<br />
<br />
详情参阅{{ic|man journalctl}}、{{ic|man systemd.journal-fields}},以及 Lennert 的这篇[http://0pointer.de/blog/projects/journalctl.html 博文]。<br />
<br />
=== 日志大小限制 ===<br />
<br />
如果按上面的操作保留日志的话,默认日志最大限制为所在文件系统容量的 10%,即:如果 {{ic|/var/log/journal}} 储存在 50GiB 的根分区中,那么日志最多存储 5GiB 数据。可以修改 {{ic|/etc/systemd/journald.conf}} 中的 {{ic|SystemMaxUse}} 来指定该最大限制。如限制日志最大 50MiB:<br />
<br />
SystemMaxUse=50M<br />
<br />
详情参见 {{ic|man journald.conf}}.<br />
<br />
=== 配合 syslog 使用 ===<br />
systemd 提供了 socket {{ic|/run/systemd/journal/syslog}},以兼容传统日志服务。所有系统信息都会被传入。要使传统日志服务工作,需要让服务链接该 socket,而非 {{ic|/dev/log}}([http://lwn.net/Articles/474968/ 官方说明])。Arch 软件仓库中的 {{pkg|syslog-ng}} 已经包含了需要的配置。<br />
<br />
设置开机启动 syslog-ng:<br />
# systemctl enable syslog-ng<br />
<br />
== 优化 ==<br />
<br />
参阅:[[Improve Boot Performance (简体中文)]]。<br />
<br />
=== 启动过程分析 ===<br />
systemd 提供了一个分析启动过程的工具 —— {{ic|systemd-analyze}}。可以用它看看哪些单元拖慢了开机过程,并据此进行优化。安装 {{Pkg|python2-dbus}}、{{Pkg|python2-cairo}} 软件包后该工具才能工作。<br />
<br />
查看开机过程在内核/用户空间消耗的时间:<br />
$ systemd-analyze<br />
{{小贴士|如果在 {{ic|/etc/mkinitcpio.conf}} 的 {{ic|HOOKS}} 数组添加 {{ic|timestamp}},并重新生成启动内存镜像({{ic|mkinitcpio -p linux}}),{{ic|systemd-analyze}} 还可以显示处理该启动镜像花费的时间。}}<br />
<br />
按照耗费时间顺序,输出启动每个单元耗费的时间:<br />
$ systemd-analyze blame<br />
<br />
生成类似于 [[Bootchart (简体中文)|bootchart]] 的开机过程图表:<br />
$ systemd-analyze plot > plot.svg<br />
<br />
==== 使用 systemd-bootchart ====<br />
<br />
自2012年10月17日,bootchart 工具已经合并进 systemd 中,使用方法和原来的 bootchart 大同小异,添加下列内容到内核参数即可:<br />
<br />
initcall_debug printk.time=y init=/usr/lib/systemd/systemd-bootchart<br />
<br />
==== 使用 bootchart2 ====<br />
<br />
由于没有办法在内核参数设置两个 init,所以不能使用源里的 bootchart。不过,[[AUR (简体中文)|AUR]] 软件包 {{AUR|bootchart2}} 提供了一个的 systemd 服务,这样就可以和 systemd 一起使用。安装后启用即可:<br />
{{bc|# systemctl enable bootchart.service}}<br />
详情参阅 [https://github.com/mmeeks/bootchart bootchart 文档]。<br />
<br />
=== 预读 ===<br />
systemd 自己实现了一个 readahead,可以用来提高开机效率。不过,效果会因内核版本和硬件情况而不同(极少数情况下还会变慢)。开启 readahead:<br />
<br />
# systemctl enable systemd-readahead-collect.service systemd-readahead-replay.service<br />
<br />
要知道,readahead 的超级牛力只有在重启几次后才会显现。<br />
<br />
== 疑难解答 ==<br />
<br />
=== 关机/重启十分缓慢 ===<br />
<br />
如果关机特别慢(甚至跟死机了一样),很可能是某个拒不退出的服务在作怪。systemd 会等待一段时间,然后再尝试杀死它。请阅读[http://freedesktop.org/wiki/Software/systemd/Debugging#Shutdown_Completes_Eventually 这篇文章],确认你是否是该问题受害者。<br />
<br />
=== 短时进程无日志记录 ===<br />
<br />
若 {{ic|journalctl -u foounit.service}} 没有显示某个短时进程的任何输出,那么改用 PID 试试。例如,若 systemd-modules-load.service 执行失败,那么先用 {{ic|systemctl status systemd-modules-load}} 查询其 PID(比如是123),然后检索该 PID 相关的日志 {{ic|journalctl -b _PID&#61;123}}。运行时进程的日志元数据(诸如 _SYSTEMD_UNIT 和 _COMM)被乱序收集在 {{ic|/proc}} 目录。要修复该问题,必须修改内核,使其通过套接字连接来提供上述数据,该过程类似于 SCM_CREDENTIALS。<br />
<br />
== 相关资源 ==<br />
<br />
*[http://www.freedesktop.org/wiki/Software/systemd 官方网站]<br />
*[http://0pointer.de/public/systemd-man/ man 手册页]<br />
*[http://freedesktop.org/wiki/Software/systemd/Optimizations systemd 优化]<br />
*[http://www.freedesktop.org/wiki/Software/systemd/FrequentlyAskedQuestions 常见问题 FAQ]<br />
*[http://www.freedesktop.org/wiki/Software/systemd/TipsAndTricks 小技巧]<br />
*[http://0pointer.de/public/systemd-ebook-psankar.pdf systemd for Administrators (PDF)]<br />
*[http://fedoraproject.org/wiki/Systemd Fedora 项目对 systemd 的介绍]<br />
*[http://fedoraproject.org/wiki/How_to_debug_Systemd_problems 如何调试 systemd 故障]<br />
*''The H Open'' 杂志上的[http://www.h-online.com/open/features/Control-Centre-The-systemd-Linux-init-system-1565543.html 两][http://www.h-online.com/open/features/Booting-up-Tools-and-tips-for-systemd-1570630.html 篇]科普文章<br />
*[http://0pointer.de/blog/projects/systemd.html Lennart 的博文]<br />
*[http://0pointer.de/blog/projects/systemd-update.html 更新报告]<br />
*[http://0pointer.de/blog/projects/systemd-update-2.html 更新报告2]<br />
*[http://0pointer.de/blog/projects/systemd-update-3.html 更新报告3]<br />
*[http://0pointer.de/blog/projects/why.html 最新动态]<br />
*[https://fedoraproject.org/wiki/SysVinit_to_Systemd_Cheatsheet/zh Fedora Wiki 上的 SysVinit 和 systemd 命令对比表]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Systemd_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=247132
Systemd (简体中文)
2013-02-12T14:33:40Z
<p>Cuihao: </p>
<hr />
<div>{{Lowercase title}}<br />
[[Category:Daemons and system services (简体中文)]]<br />
[[Category:Boot process (简体中文)]]<br />
[[en:Systemd]]<br />
[[es:Systemd]]<br />
[[fr:Systemd]]<br />
[[it:Systemd]]<br />
[[ja:Systemd]]<br />
[[ru:Systemd]]<br />
[[zh-TW:Systemd]]<br />
{{Article summary start}}<br />
{{Article summary text|本文介绍 Systemd 的安装和配置。}}<br />
{{Article summary heading|相关页面}}<br />
{{Article summary wiki|Systemd/Services}}<br />
{{Article summary wiki|systemd/User}}<br />
{{Article summary wiki|Systemd FAQ}}<br />
{{Article summary wiki|init Rosetta}}<br />
{{Article summary wiki|udev (简体中文)}}<br />
{{Article summary end}}<br />
{{TranslationStatus (简体中文)|Systemd|2013-02-12|247103}}<br />
摘自[http://freedesktop.org/wiki/Software/systemd 项目主页]:<br />
<br />
'''''systemd''' 是 Linux 下的一款系统和服务管理器,兼容 SysV 和 LSB 的启动脚本。systemd 的特性有:支持并行化任务;同时采用 socket 式与 [[D-Bus (简体中文)|D-Bus]] 总线式激活服务;按需启动守护进程(daemon);利用 Linux 的 [[cgroups|cgroups]] 监视进程;支持快照和系统恢复;维护挂载点和自动挂载点;各服务间基于依赖关系进行精密控制。systemd 完全可以替代 Arch 曾经使用的 [[SysVinit (简体中文)|sysvinit]] 启动系统。''<br />
<br />
{{注意|[https://bbs.archlinux.org/viewtopic.php?pid&#61;1149530#p1149530 这个帖子]详细解释了 Arch 向 systemd 迁移的原因。}}<br />
<br />
另见[[Wikipedia:Systemd|维基百科上的介绍]]。<br />
<br />
== 迁移前需考虑 ==<br />
<br />
* 阅读[http://freedesktop.org/wiki/Software/systemd/ 该站],了解 systemd。<br />
* systemd 自己有一套日志('''journal''')系统,用于代替 '''syslog'''。两者也可以共存,参见后面的[[#日志|日志部分]]。<br />
* 虽然 systemd 可以替换 '''cron'''、'''acpid'''、'''xinetd''' 等的部分功能。至少目前还可以继续使用这些服务,无需立即切换。<br />
* 交互式 initscripts 启动脚本在 systemd 中无法工作。例如 '''netcfg-menu''' [https://bugs.archlinux.org/task/31377 无法]在启动时显示。<br />
<br />
== 安装 ==<br />
{{注意|从[https://www.archlinux.org/news/systemd-is-now-the-default-on-new-installations/ 2012-10-13版安装介质]开始,安装程序已经默认安装{{pkg|systemd}} 和 {{pkg|systemd-sysvcompat}}。}}<br />
{{注意|如果是在 VPS 中使用 Arch,请先阅读:[[Virtual_Private_Server#Moving_your_VPS_from_initscripts_to_systemd]]。}}<br />
<br />
本部分帮助还在使用{{pkg|sysvinit}} 和 {{pkg|initscripts}} 的用户迁移到{{pkg|systemd}}.<br />
<br />
# 从[[Official_Repositories_(简体中文)|官方软件仓库]]安装 {{pkg|systemd}} 并添加[[Kernel parameters|内核参数]]{{Ic|1=init=/usr/lib/systemd/systemd}}<br />
# 用 {{ic|systemctl enable ''daemonname''}} [[#使用单元|启用]] {{ic|/etc/rc.conf}} 中的守护进程。从 {{ic|/etc/rc.conf}} 到 systemd 服务的转换可以参考:[[Daemons_List_(简体中文)|守护进程列表]] 和 [[Systemd/Services|Services]]。<br />
# 重启系统,执行命令 {{ic|cat /proc/1/comm}},如果返回{{ic|systemd}},表示 systemd 已经正常启动。<br />
# 确认主机名已经正确设置:{{ic|hostnamectl set-hostname myhostname}}。<br />
# 删除 {{pkg|initscripts}} 和 {{pkg|sysvinit}},并安装{{pkg|systemd-sysvcompat}}.<br />
# (可选)删除{{ic|1=init=/usr/lib/systemd/systemd}}内核参数,现在已经不需要它了。{{pkg|systemd-sysvcompat}} 软件包提供了一个软链接,使 systemd 成为默认 init。<br />
<br />
=== 附加信息 ===<br />
* 如果内核参数中有 {{ic|quiet}},可以先删除它以便调试。<br />
{{Accuracy|reason=据有的用户称,systemd 并未对用户组有特殊要求。尽管有很多不常用的用户组,但从 consolekit 切换到 logind 后这些用户组没有发生改变。''(据译者理解:如果你碰到特殊的权限问题,可能仍需要加入这些用户组才能解决。)''}}<br />
* 使用 systemd 的时候'''无需'''将用户加入特殊[[Users and Groups (简体中文)|用户组]](如{{ic|sys}}、{{ic|disk}}、{{ic|lp}}、{{ic|network}}、{{ic|video}}、{{ic|audio}}、{{ic|optical}}、{{ic|storage}}、{{ic|scanner}}、{{ic|power}}等等)。加入这些组反而会有问题,例如audio组会导致程序阻塞软件混声。每个 PAM 登录都拥有一个 logind 会话,它通过[[Wikipedia:Access control list|POSIX ACLs]],赋予本地会话以声音设备访问权限、通过[[udisks]]挂载和卸载移动设备的权限等。<br />
{{注意|对于使用 {{ic|startx -- vt7}} 进入X环境的用户,由于 logind 会话无法应用于X环境,用户'''必须'''加入这些组。}}<br />
* 如果卸载 {{Pkg|initscripts}},{{ic|rc.conf}} 将不再有效。如果曾在其中设置了静态网络,或还未迁移到systemd的系统服务,请根据[[#Initscripts 模拟|该节]]配置。<br />
* 如果 fstab 中有 LVM 设备,首次启动时会因为等待超时进入急救控制台。输入管理员密码,执行 {{ic|systemctl enable lvm}} 启用相关服务,重启即可。<br />
<br />
== 原生 systemd 配置文件 ==<br />
{{注意|可能需要手动创建某些文件。所有文件的权限都是644,属主 root,属组 root。}}<br />
<br />
=== 主机名 ===<br />
主机名通过文件 {{ic|/etc/hostname}} 进行配置。该文件不应包含系统的域名(如果有)。使用如下命令设置主机名:<br />
# hostnamectl set-hostname '''myhostname'''<br />
<br />
详情参见 {{ic|man 5 hostname}} 和 {{ic|man 1 hostnamectl}}。<br />
<br />
文件示例:<br />
{{hc|/etc/hostname|<br />
myhostname<br />
}}<br />
<br />
=== 本地化设置(locale) ===<br />
系统默认 locale 记录在 {{ic|/etc/locale.conf}} 文件中。通过如下命令配置:<br />
# localectl set-locale LANG="zh_CN.utf8"<br />
<br />
{{注意|设置默认locale前,请确保已经生成该locale。方法是:在 {{ic|/etc/locale.gen}} 中启用对应locale,然后以root权限执行 {{ic|locale-gen}}。}}<br />
<br />
详情参见 {{ic|man 1 localectl}} 和 {{ic|man 5 locale.conf}}。<br />
<br />
* 另参阅 [[Locale (简体中文)]]。<br />
<br />
文件示例:<br />
{{hc|/etc/locale.conf|<nowiki><br />
LANG=en_US.utf8<br />
</nowiki>}}<br />
<br />
=== 虚拟控制台 ===<br />
{{ic|/etc/vconsole.conf}} 文件用来配置虚拟控制台,包括键盘布局和控制台字体:<br />
{{hc|/etc/vconsole.conf|<nowiki><br />
KEYMAP=us<br />
FONT=lat9w-16<br />
FONT_MAP=8859-1_to_uni</nowiki>}}<br />
<br />
{{注意|{{ic|{{Pkg|systemd}}-194}} 默认使用内核字体和键盘布局,因此以后不必在上述文件中留下空的 {{ic|1=KEYMAP=}} 和 {{ic|1=FONT=}} 设置。}}<br />
<br />
还可以通过命令设置键盘布局:<br />
# localectl set-keymap de<br />
<br />
{{ic|localectl}} 也可以设置X11的键盘布局:<br />
<br />
# localectl set-x11-keymap de<br />
<br />
详情参见 {{ic|man 1 localectl}} 和 {{ic|man 5 vconsole.conf}}。<br />
<br />
* 另参阅 [[Fonts (简体中文)#控制台字体|控制台字体]] 和 [[KEYMAP (简体中文)#键盘布局|键盘布局]]。<br />
<br />
=== 时区 ===<br />
时区可通过创建适当的{{ic|/etc/localtime}}符号链接来进行配置,该链接需指向目录{{ic|/usr/share/zoneinfo/}}下的一个zoneinfo文件。也可执行以下命令直接完成这一操作:<br />
<br />
# timedatectl set-timezone Asia/Shanghai<br />
<br />
更多细节参见{{ic|man 1 timedatectl}},{{ic|man 5 localtime}}和{{ic|man 7 archlinux}}。<br />
<br />
手动创建链接也行:<br />
# ln -sf ../usr/share/zoneinfo/Asia/Shanghai /etc/localtime<br />
<br />
旧的配置文件 {{ic|/etc/timezone}} 已经没用了,可以安全删除。<br />
<br />
=== 硬件时钟 ===<br />
<br />
systemd 默认识别硬件时钟为协调世界时(UTC)。<br />
<br />
{{小贴士|推荐使用[[NTP (简体中文)|NTP服务]]来在线同步硬件时钟。}}<br />
<br />
==== 硬件时钟设定为地方时 ====<br />
<br />
将硬件时钟配置为地方时('''不建议'''):<br />
<br />
# timedatectl set-local-rtc true<br />
<br />
重新调整为 UTC:<br />
<br />
# timedatectl set-local-rtc false<br />
<br />
如果设置成本地时间,处理夏令时有些麻烦。如果夏令时调整发生在关机时,下次启动时时间会出现问题([http://www.cl.cam.ac.uk/~mgk25/mswish/ut-rtc.html 更多信息])。最新的内核直接从实时时钟芯片(RTC)读取时间,不使用 {{ic|hwclock}},内核把从 RTC 读取的时间当作 UTC 处理。所以如果硬件时间是地方时,系统启动一开始识别的时间是错误的,之后很快会进行矫正。这可能导致一些问题(尤其是时间倒退时)。<br />
<br />
如果同时安装了 Windows 操作系统([http://blogs.msdn.com/b/oldnewthing/archive/2004/09/02/224672.aspx 默认使用地方时]),那么一般 RTC 会被设置为地方时。Windows 其实也能处理 UTC,需要[[Time (简体中文)#Windows 系统使用 UTC|修改注册表]]。建议让 Windows 使用 UTC,而非让 Linux 使用地方时。Windows 使用 UTC 后,请记得禁用 Windows 的时间同步功能,以防 Windows 错误设置硬件时间。如上文所说,Linux 可以使用[[NTP (简体中文)|NTP服务]]来在线同步硬件时钟。<br />
<br />
* 详情参阅 [[Time (简体中文)]]。<br />
<br />
=== 内核模块 ===<br />
目前,所有必要模块的加载均由 [[udev]] 自动完成。所以,如果不需要使用任何额外的模块,就没有必要在任何配置文件中添加启动时加载的模块。但是,有些情况下可能需要在系统启动时加载某个额外的模块,或者将某个模块列入黑名单以便使系统正常运行。<br />
<br />
==== 开机加载 ====<br />
systemd 读取 {{ic|/etc/modules-load.d/}} 中的配置加载额外的内核模块。配置文件名称通常为 {{ic|/etc/modules-load.d/<program>.conf}}。格式很简单,一行一个要读取的模块名,而空行以及第一个非空格字符为{{ic|#}}或{{ic|;}}的行会被忽略,如:<br />
{{hc|/etc/modules-load.d/virtio-net.conf|<nowiki><br />
# Load virtio-net.ko at boot<br />
virtio-net</nowiki>}}<br />
<br />
另见{{ic|man 5 modules-load.d}}。<br />
<br />
==== 禁用内核模块 ====<br />
禁用内核模块的方法和 Arch 默认的 {{Pkg|initscripts}} 相同,因为该过程实际由 {{Pkg|kmod}} 处理。参见:[[Kernel modules (简体中文)#黑名单]]。<br />
<br />
=== 文件系统挂载 ===<br />
<br />
默认行为是:在启动一个需要挂载特定分区的服务之前,系统自动检查并挂载分区。{{ic|/etc/fstab}} 中设定的网络文件系统(如 [[NFS (简体中文)|NFS]]、[[Samba (简体中文)|Samba]])无需配置即可正常工作,systemd 将确保网络文件系统在网络链接就绪后挂载。<br />
<br />
详情参阅:{{ic|man 5 systemd.mount}}。<br />
<br />
==== 自动挂载 ====<br />
<br />
* 如果 {{ic|/home}} 分区较大,可以让不依赖 {{ic|/home}} 分区的服务先启动。把下面的参数添加到 {{ic|/etc/fstab}} 文件中 {{ic|/home}} 项目的参数部分即可:<br />
<br />
noauto,x-systemd.automount<br />
<br />
这样 {{ic|/home}} 分区只有需要访问时才会被挂载。内核会缓存所有的文件操作,直到 {{ic|/home}} 分区准备完成。<br />
<br />
{{注意|这样做会使 {{ic|/home}} 的文件系统类型被识别为 {{ic|autofs}},造成 [[mlocate]] 查询时忽略该目录。实际加速效果因配置而异,所以请自己权衡是否需要。}}<br />
<br />
* 挂载远程文件系统也是同理。如果你仅想在需要的时候才挂载,也可以添加 {{ic|noauto,x-systemd.automount}} 参数。另外,可以设置 {{ic|1=x-systemd.device-timeout=#}} 参数,设置超时时间,以防止网络资源不能访问的时候浪费时间。<br />
<br />
* 如果你的加密文件系统需要密钥,则需要添加 {{ic|noauto}} 参数到 {{ic|/etc/crypttab}} 文件中的对应位置。systemd 开机的时候就不会打开这个加密设备,会一直等待到设备被访问时再使用密钥文件挂载。比如在使用加密RAID设备的时候可以节省一定的时间,因为 systemd 不必等到设备可用后才能访问。例如:<br />
<br />
{{hc|/etc/crypttab|<br />
data /dev/md0 /root/key noauto}}<br />
<br />
==== LVM ====<br />
<br />
如果装有不通过 [[Mkinitcpio (简体中文)|initramfs]] 激活的[[LVM (简体中文)|LVM]]卷,则需启动 {{ic|lvm-monitoring}} 服务(由 {{pkg|lvm2}} 软件包提供):<br />
<br />
# systemctl enable lvm-monitoring<br />
<br />
类似地,如果LVM卷在加密设备上,并且启动后才激活,则需启动 {{ic|lvm-on-crypt}} 服务(同样由 {{pkg|lvm2}} 软件包提供):<br />
<br />
# systemctl enable lvm-on-crypt<br />
<br />
=== ACPI 电源管理 ===<br />
systemd 能够处理某些电源相关的 [[Wikipedia:Advanced_Configuration_and_Power_Interface|ACPI]] 事件,通过 {{ic|/etc/systemd/logind.conf}} 的下列选项配置:<br />
* {{ic|HandlePowerKey}}:按下电源键后的动作<br />
* {{ic|HandleSleepKey}}:按下挂起键后的动作<br />
* {{ic|HandleHibernateKey}}: 按下休眠键后的动作<br />
* {{ic|HandleLidSwitch}}:合上笔记本盖后待机<br />
<br />
动作可以是 {{ic|ignore}}、{{ic|poweroff}}、{{ic|reboot}}、{{ic|halt}}、{{ic|suspend}}、{{ic|hibernate}}、{{ic|hybrid-sleep}}、{{ic|lock}} 或 {{ic|kexec}}。<br />
<br />
系统默认设置为:<br />
HandlePowerKey=poweroff<br />
HandleSuspendKey=suspend<br />
HandleHibernateKey=hibernate<br />
HandleLidSwitch=suspend<br />
<br />
如果不用图形界面,或者使用 [[i3 (简体中文)|i3]]、[[awesome (简体中文)|awesome]] 这样简单的桌面管理器时,systemd 可以替代 [[acpid (简体中文)|acpid]] 处理 ACPI 事件。<br />
<br />
{{注意|systemd 无法处理交流电源和电池 ACPI 事件,所以还得使用 [[Laptop Mode Tools (简体中文)|Laptop Mode Tools]] 或 [[acpid (简体中文)|acpid]] 工具。}}<br />
<br />
在当前版本的 systemd 中,这些 {{ic|Handle}} 选项将会被应用到整个系统当中,除非它们被别的程序——例如某个桌面环境中的电源管理器——给“阻止”(inhibited)。如果其它的程序没有阻止这些 {{ic|Handle}} ,你可能会先被 systemd 挂起你的系统,然后当系统被唤醒之后,电源管理器又会再次将系统挂起。<br />
<br />
{{警告|目前只有[[GNOME]] 和 [[KDE]] 支持 "inhibited" 命令。在其它的桌面管理器同样实现该功能之前,如果你想使用[[Xfce]]、[[acpid]] 或者其它程序来管理 ACPI 事件,你需要把 {{ic|Handle}} 选项设置为 {{ic|ignore}} 。包含该功能的新版本还在开发当中。}}<br />
<br />
{{注意|除了内核默认的待机支持后端(用于处理待机/休眠),systemd 也可以使用其他后端(比如 [[Uswsusp]] 或 [[TuxOnIce]])。}}<br />
<br />
==== 休眠时执行的脚本 ====<br />
<br />
使用 {{ic|systemctl suspend}}、{{ic|systemctl hibernate}} 或 {{ic|systemctl hybrid-sleep}} 命令执行待机/休眠时,systemd 不会调用 [[pm-utils (简体中文)|pm-utils]]。[[pm-utils (简体中文)|pm-utils]] 的钩子扩展(hook)——包括 [[pm-utils (简体中文)#建立你自己的钩子|自定义钩子]]——会失效。不过,systemd 提供了两种类似的待机/休眠时执行脚本的机制。<br />
<br />
===== 使用服务文件 =====<br />
<br />
可以将服务文件附在 suspend.target、hibernate.target 或 sleep.target 中,这样就能在待机/休眠前后执行某些操作。用户级操作和root/系统级操作应该使用不同的服务文件。要启用用户级服务文件,使用 {{ic|# systemctl enable suspend@<用户名> && systemctl enable resume@<用户名>}}。例如:<br />
<br />
{{hc|/etc/systemd/system/suspend@.service|2=<nowiki><br />
[Unit]<br />
Description=User suspend actions<br />
Before=sleep.target<br />
<br />
[Service]<br />
User=%I<br />
Type=forking<br />
Environment=DISPLAY=:0<br />
ExecStartPre= -/usr/bin/pkill -u %u unison ; /usr/local/bin/music.sh stop ; /usr/bin/mysql -e 'slave stop'<br />
ExecStart=/usr/bin/sflock<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
{{hc|/etc/systemd/system/resume@.service|2=<nowiki><br />
[Unit]<br />
Description=User resume actions<br />
After=suspend.target<br />
<br />
[Service]<br />
User=%I<br />
Type=simple<br />
ExecStartPre=/usr/local/bin/ssh-connect.sh<br />
ExecStart=/usr/bin/mysql -e 'slave start'<br />
<br />
[Install]<br />
WantedBy=suspend.target</nowiki>}}<br />
<br />
至于root/系统级服务(使用 {{ic|# systemctl enable root-suspend}} 激活):<br />
<br />
{{hc|/etc/systemd/system/root-resume.service|2=<nowiki><br />
[Unit]<br />
Description=Local system resume actions<br />
After=suspend.target<br />
<br />
[Service]<br />
Type=simple<br />
ExecStart=/usr/bin/systemctl restart mnt-media.automount<br />
<br />
[Install]<br />
WantedBy=suspend.target</nowiki>}}<br />
<br />
{{hc|/etc/systemd/system/root-suspend.service|2=<nowiki><br />
[Unit]<br />
Description=Local system suspend actions<br />
Before=sleep.target<br />
<br />
[Service]<br />
Type=simple<br />
ExecStart=-/usr/bin/pkill sshfs<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
上述服务文件的一些解释(详见 {{ic|man systemd.service}}):<br />
* 如果设置 {{ic|1=<nowiki>Type=OneShot</nowiki>}},那么可以使用多个 {{ic|1=<nowiki>ExecStart=</nowiki>}} 参数。否则只能写一个,替代方案是在 {{ic|ExecStartPre}} 中添加命令,或使用分号分隔不同命令(见第一个例子,分号前后的空格都是'''必须'''的)。 <br />
* 若命令前加上一个“-”(半角减号),则命令返回非零值时会被忽略、当作正常执行处理。<br />
* 有关调试,最好的方法是用 {{ic|journalctl}} 查看日志。<br />
<br />
===== 合并待机和唤醒服务文件 =====<br />
<br />
利用下面这种自定义的一体化待机和唤醒服务,使用单一的钩子扩展即可对不同操作(待机/休眠/混合休眠)的不同阶段(进入/唤醒)进行控制。<br />
<br />
例子和解释:<br />
<br />
{{hc|/etc/systemd/system/wicd-sleep.service|2=<nowiki><br />
[Unit]<br />
Description=Wicd sleep hook<br />
Before=sleep.target<br />
StopWhenUnneeded=yes<br />
<br />
[Service]<br />
Type=oneshot<br />
RemainAfterExit=yes<br />
ExecStart=-/usr/share/wicd/daemon/suspend.py<br />
ExecStop=-/usr/share/wicd/daemon/autoconnect.py<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
* {{ic|1=<nowiki>RemainAfterExit=yes</nowiki>}}:服务启动后,除非显式地停止,否则就认为是活动的。<br />
* {{ic|1=<nowiki>StopWhenUnneeded=yes</nowiki>}}:服务活动时,如果无其他服务依赖该服务,就停止它。在本例中,该服务会在 sleep.target 停止后停止活动。<br />
* 由于 sleep.target 会被 suspend.target、hibernate.target、hybrid-sleep.target 调用,且 sleep.target 本身设置了 StopWhenUnneeded,该服务文件对以上各种操作都是有效的。<br />
<br />
===== 使用 /usr/lib/systemd/system-sleep =====<br />
<br />
systemd 在待机/休眠时执行 {{ic|/usr/lib/systemd/system-sleep/}} 里的所有脚本,传递下面两个参数:<br />
<br />
* 参数1:若是准备进入待机/休眠状态,则为 {{ic|pre}};唤醒时为 {{ic|post}}。<br />
* 参数2:事件名称,{{ic|suspend}},{{ic|hibernate}} 或 {{ic|hybrid-sleep}}。<br />
<br />
systemd 会同时执行所有脚本,而不是像 [[pm-utils (简体中文)|pm-utils]] 那样顺序执行。<br />
<br />
脚本输出会记录在相关服务({{ic|systemd-suspend.service}}、{{ic|systemd-hibernate.service}} 或 {{ic|systemd-hybrid-sleep.service}})中。通过[[#日志|日志]]查看:<br />
# journalctl -b -u systemd-suspend<br />
<br />
脚本示范:<br />
<br />
{{hc|/usr/lib/systemd/system-sleep/example.sh|<br />
#!/bin/sh<br />
case $1/$2 in<br />
pre/*)<br />
echo "进入 $2 状态..."<br />
;;<br />
post/*)<br />
echo "从 $2 状态唤醒..."<br />
;;<br />
esac}}<br />
<br />
记得添加可执行权限: <br />
# chmod a+x /usr/lib/systemd/system-sleep/example.sh<br />
<br />
详情参见 {{ic|man 7 systemd.special}} 和 {{ic|man 8 systemd-sleep}}。<br />
<br />
=== 临时文件 ===<br />
{{ic|/usr/lib/tmpfiles.d/}} 和 {{ic|/etc/tmpfiles.d/}} 中的文件描述了 systemd-tmpfiles 如何创建、清理、删除临时文件和目录,这些文件和目录通常存放在 {{ic|/run}} 和 {{ic|/tmp}} 中。配置文件名称为 {{ic|/etc/tmpfiles.d/<program>.conf}}。此处的配置能覆盖 {{ic|/usr/lib/tmpfiles.d/}} 目录中的同名配置。<br />
<br />
临时文件通常和服务文件同时提供,以生成守护进程需要的文件和目录。例如 [[Samba (简体中文)|Samba]] 服务需要目录 {{ic|/run/samba}} 存在并设置正确的权限位,就象这样:<br />
{{hc|/usr/lib/tmpfiles.d/samba.conf|<br />
D /run/samba 0755 root root<br />
}}<br />
<br />
此外,临时文件还可以用来在开机时向特定文件写入某些内容。比如,要禁止系统从USB设备唤醒,利用旧的 {{ic|/etc/rc.local}} 可以用 {{ic|echo USBE > /proc/acpi/wakeup}},而现在可以这么做:<br />
{{hc|/etc/tmpfiles.d/disable-usb-wake.conf|<br />
w /proc/acpi/wakeup - - - - USBE<br />
}}<br />
<br />
详情参见 {{ic|man 5 tmpfiles.d}}。<br />
<br />
=== 单元(unit) ===<br />
一个单元配置文件可以描述如下内容之一:系统服务、socket、系统设备、挂载点、交换分区/文件、启动目标(target)、文件系统路径、由 systemd 管理的计时器。文件格式受 .desktop 文件启发,而最初起源是 Windows 下的 .ini 文件。<br />
<br />
详情参阅 {{ic|man 5 systemd.unit}}.<br />
<br />
== 从 initscripts 迁移到 systemd ==<br />
<br />
=== initscripts 模拟 ===<br />
<br />
{{Pkg|initscripts}} 软件包提供了 systemd 和 Arch 传统配置的整合。当同时安装了{{Pkg|initscripts}}和systemd时,若以systemd启动系统,systemd将执行以下操作:<br />
<br />
# 解析{{ic|/etc/rc.conf}}中的{{ic|DAEMONS}}数组,并在系统启动时,启动所有列出的守护进程<br />
# 在启动过程中执行{{ic|/etc/rc.local}}<br />
# 在系统关闭过程中执行{{ic|/etc/rc.local.shutdown}}<br />
<br />
Initscripts模拟仅仅是作为用户迁移到systemd时的过渡措施,这一方案最终将'''不再可行'''。原生的systemd并不依赖于{{ic|rc.conf}}文件的集中式配置,因此推荐使用[[#原生 systemd 配置文件|原生的systemd配置文件]]。原生systemd配置文件比{{ic|/etc/rc.conf}}具有更高优先级。<br />
<br />
{{注意|替换{{ic|/etc/rc.local}}的推荐方式是针对系统启动时要运行的程序编写自定义的service文件,详见相关[[#自己编写_.service_文件|章节]].}}<br />
<br />
{{注意|如果你曾经在{{ic|/etc/inittab}}中禁用了以{{keypress|Ctrl+Alt+Del}}重启系统,则需要以root执行{{ic|systemctl mask ctrl-alt-del.target}}以使其对systemd重新生效。}}<br />
<br />
{{警告|如果同时安装 systemd(197-4 及后续版本)和 initscripts,那么 {{ic|/etc/rc.local}} 脚本会导致启动过程无法结束(原因是 getty@tty1.service 自 197-4 版本起不再等待 rc-local.service 执行,getty 无法启动)。请删除或重命名 {{ic|/etc/rc.local}}。}}<br />
<br />
=== 迁移 DAEMONS 列表 ===<br />
<br />
若要用纯 systemd 环境,则应当删除 {{ic|/etc/rc.conf}} 文件,并完全通过 {{ic|systemctl}} 命令来启动服务。对于 {{ic|DAEMONS}} 列表里面的每个 {{ic|<服务名>}},现在可以用以下命令设置成自动启动:<br />
<br />
# systemctl enable <服务名>.service<br />
<br />
{{小贴士|[[Daemons List|这里]]有一份 initscripts 和 systemd 下常见服务的对应列表。}}<br />
<br />
如果提示 {{ic|<service_name>.service}} 不存在:<br />
<br />
* 很有可能是 systemd 用了不同的服务名。比如 {{ic|crond}} 服务替换成 {{ic|cronie.service}};{{ic|alsa}} 服务替换成了 {{ic|alsa-store.service}} 和 {{ic|alsa-restore.service}} 两个服务。另一个值得注意的是 {{ic|network}} 服务,它被替换为一整套服务文件(详情参见 [[Configuring_Network_(简体中文)]])。<br />
* 另外,有些程序尚未提供 .service 文件,还得继续用 {{ic|rc.conf}} 来配置开机自动启动。<br />
<br />
{{小贴士|用下面这个命令来查看已安装软件包包含的服务文件:<br />
$ pacman -Ql cronie<br />
[...]<br />
cronie /etc/rc.d/crond # DAEMONS 列表中使用的 initscript 服务 (原生 systemd 配置中不再使用)<br />
[...]<br />
cronie /usr/lib/systemd/system/cronie.service # 原生 systemd 服务文件<br />
[...]<br />
}}<br />
<br />
* 有些服务不需要用户明确配置自启动。比如安装 {{ic|dbus-core}} 软件包后 {{ic|dbus.service}} 就会自动被配置为自启动。而 {{ic|alsa-store.service}} 和 {{ic|alsa-restore.service}} 也会由 systemd 自动启动。可以用 {{ic|systemctl list-unit-files}} 命令来查看服务列表并检查运行状态。<br />
<br />
== systemd 基本工具 ==<br />
<br />
检视和控制systemd的主要命令是{{ic|systemctl}}。该命令可用于查看系统状态和管理系统及服务。详见{{ic|man 1 systemctl}}。<br />
<br />
{{小贴士|在 {{ic|systemctl}} 参数中添加 {{ic|-H <用户名>@<主机名>}} 可以实现对其他机器的远程控制。该过程使用 [[SSH (简体中文)|SSH]] 链接。}}<br />
<br />
{{注意|{{ic|systemadm}} 是 systemd 的官方图形前端。由 [[AUR (简体中文)|AUR]] 中的软件包 {{AUR|systemd-ui-git}} 提供。}}<br />
<br />
=== 分析系统状态 ===<br />
<br />
输出激活的单元:<br />
<br />
$ systemctl<br />
<br />
以下命令等效:<br />
<br />
$ systemctl list-units<br />
<br />
输出运行失败的单元:<br />
<br />
$ systemctl --failed<br />
<br />
所有可用的单元文件存放在 {{ic|/usr/lib/systemd/system/}} 和 {{ic|/etc/systemd/system/}} 目录(后者优先级更高)。查看所有已安装服务:<br />
$ systemctl list-unit-files<br />
<br />
=== 使用单元 ===<br />
一个单元可以是系统服务({{ic|.service}})、挂载点({{ic|.mount}})、sockets({{ic|.sockets}})。<br />
<br />
使用 {{ic|systemctl}} 控制单元时,通常需要使用单元文件的全名,包括扩展名(例如 {{ic|sshd.service}})。但是有些单元可以在{{ic|systemctl}}中使用简写方式。<br />
* 如果无扩展名,systemctl 默认把扩展名当作 {{ic|.service}}。例如 {{ic|netcfg}} 和 {{ic|netcfg.service}} 是等价的。<br />
* 挂载点会自动转化为相应的 {{ic|.mount}} 单元。例如 {{ic|/home}} 等价于 {{ic|home.mount}}。<br />
* 设备会自动转化为相应的 {{ic|.device}} 单元,所以 {{ic|/dev/sda2}} 等价于 {{ic|dev-sda2.device}}。<br />
<br />
立即激活单元:<br />
<br />
# systemctl start <单元><br />
<br />
立即停止单元:<br />
# systemctl stop <单元><br />
<br />
重启单元:<br />
<br />
# systemctl restart <单元><br />
<br />
命令单元重新读取配置:<br />
<br />
# systemctl reload <单元><br />
<br />
输出单元运行状态:<br />
<br />
$ systemctl status <单元><br />
<br />
检查单元是否配置为自动启动:<br />
$ systemctl is-enabled <单元><br />
<br />
开机自动激活单元:<br />
<br />
# systemctl enable <单元><br />
<br />
{{注意|如果服务没有{{ic|Install}}段落,一般意味着应该通过其它服务自动调用它们。如果真的需要手动安装,可以直接连接服务,如下(将{{ic|foo}}替换为真实的服务名):<br />
{{bc|# ln -s /usr/lib/systemd/system/foo.service /etc/systemd/system/graphical.target.wants/}}<br />
}}<br />
<br />
取消开机自动激活单元:<br />
<br />
# systemctl disable <单元>}}<br />
<br />
显示单元的手册页(必须由单元文件提供):<br />
<br />
# systemctl help <单元><br />
<br />
重新载入 systemd,扫描新的或有变动的单元:<br />
# systemctl daemon-reload<br />
<br />
=== 电源管理 ===<br />
<br />
安装 {{ic|polkit}} 后才可使用电源管理。<br />
<br />
如果你正登录在一个本地的{{ic|systemd-logind}}用户会话,且当前没有其它活动的会话,那么以下命令无需root权限即可执行。否则(例如,当前有另一个用户登录在某个tty),systemd 将会自动请求输入root密码。<br />
<br />
重启:<br />
<br />
$ systemctl reboot<br />
<br />
退出系统并停止电源:<br />
<br />
$ systemctl poweroff<br />
<br />
待机:<br />
<br />
$ systemctl suspend<br />
<br />
休眠:<br />
<br />
$ systemctl hibernate<br />
<br />
混合休眠模式(同时休眠到硬盘并待机):<br />
<br />
$ systemctl hybrid-sleep<br />
<br />
== 启动桌面环境 ==<br />
<br />
=== 使用登录管理器 ===<br />
<br />
通过启动[[Display Manager (简体中文)|登录管理器]](或称显示管理器),即可进行图形界面登录。目前,Arch 提供了 [[GDM (简体中文)|GDM]]、[[KDM (简体中文)|KDM]]、[[SLiM (简体中文)|SLiM]]、[[XDM (简体中文)|XDM]]、[[LXDM (简体中文)|LXDM]] 和 [[LightDM (简体中文)|LightDM]] 的 systemd 服务文件。以 KDM 为例,配置开机启动:<br />
# systemctl enable kdm.service<br />
<br />
执行上述命令后,登录管理器应当能正常工作了。如果不是的话,很可能是因为你修改了{{ic|default.target}}。默认情况应当如下:<br />
<br />
{{hc|# ls -l /etc/systemd/system/default.target|/etc/systemd/system/default.target -> /usr/lib/systemd/system/graphical.target}}<br />
<br />
删除被修改的 {{ic|default.target}} 即可,systemd 会自动使用默认配置(即 {{ic|graphical.target}}):<br />
<br />
# rm /etc/systemd/system/default.target<br />
<br />
=== 使用 systemd-logind ===<br />
<br />
{{注意|自 2012-10-30 起, [[ConsoleKit]] 已被 [https://www.archlinux.org/news/consolekit-replaced-by-logind/ systemd-logind 取代] ,作为登录到桌面环境的默认方式。}}<br />
<br />
可使用 {{ic|loginctl}} 来查看用户会话的状态。所有 [[PolicyKit]] 操作,如挂起系统、挂载外部驱动器,都无需配置即可使用。<br />
<br />
$ loginctl show-session $XDG_SESSION_ID<br />
<br />
== 自己编写 .service 文件 ==<br />
''参见:[[Systemd/Services]]''<br />
<br />
=== 处理依赖关系 ===<br />
<br />
使用systemd时,可通过正确编写单元配置文件来解决其依赖关系。典型的情况是,单元{{ic|A}}要求单元{{ic|B}}在{{ic|A}}启动之前运行。在此情况下,向单元{{ic|A}}配置文件中的 {{ic|[Unit]}} 段添加 {{ic|1=Requires=B}} 和 {{ic|1=After=B}} 即可。若此依赖关系是可选的,可添加 {{ic|1=Wants=B}} 和 {{ic|1=After=B}}。请注意 {{ic|1=Wants=}} 和 {{ic|1=Requires=}} 并不意味着 {{ic|1=After=}},即如果 {{ic|1=After=}} 选项没有制定,这两个单元将被并行启动。<br />
<br />
依赖关系通常被用在服务(service)而不是[[#目标(target)|目标(target)]]上。例如, {{ic|network.target}} 一般会被某个配置网络接口的服务引入,所以,将自定义的单元排在该服务之后即可,因为 {{ic|network.target}} 已经启动。<br />
<br />
=== 启动方式 ===<br />
<br />
编写自定义的service文件时,可以选择几种不同的服务启动方式。启动方式可通过配置文件 {{ic|[Service]}} 段中的 {{ic|1=Type=}} 参数进行设置。具体的参数说明请参阅 {{ic|man systemd.service}} 。<br />
<br />
* {{ic|1=Type=simple}}:systemd认为该服务将立即启动。服务进程不会fork。如果该服务要启动其他服务,不要使用此类型启动,除非该服务是socket激活型。<br />
* {{ic|1=Type=forking}}:systemd认为当该服务进程fork,且父进程退出后服务启动成功。对于常规的守护进程(daemon),除非你确定此启动方式无法满足需求,使用此类型启动即可。使用此启动类型应同时指定 {{ic|1=PIDFile=}},以便systemd能够跟踪服务的主进程。<br />
* {{ic|1=Type=oneshot}}:这一选项适用于只执行一项任务随后退出的服务。你可能需要同时设置 {{ic|1=RemainAfterExit=yes}} 使得 systemd 在服务进程退出之后仍然认为服务处于激活状态。<br />
* {{ic|1=Type=notify}}:与 {{ic|1=Type=simple}} 相同,但约定服务会在就绪后向 systemd 发送一个信号。这一通知的实现由 {{ic|libsystemd-daemon.so}} 提供。<br />
* {{ic|1=Type=dbus}}:若以此方式启动,当指定的 {{ic|BusName}} 出现在DBus系统总线上时,systemd认为服务就绪。<br />
<br />
=== 替换现存单元文件 ===<br />
<br />
目录 {{ic|/etc/systemd/system/}} 中的单元配置文件比 {{ic|/usr/lib/systemd/system/}} 目录下的文件拥有更高优先级。<br />
若需要定制某个单元的启动(使配置不受软件包更新的影响),请将现有的单元配置文件从 {{ic|/usr/lib/}} 复制到 {{ic|/etc/}} 目录下,并在此目录中修改。或者,可以使用 {{ic|.include}} 来解析一个现有的配置文件,并覆盖原选项或添加新选项。例如,若想要某个服务的配置文件中添加额外依赖关系,可以编写:<br />
<br />
{{hc|/etc/systemd/system/<服务名>.service|2=<br />
.include /usr/lib/systemd/system/<服务名>.service<br />
<br />
[Unit]<br />
Requires=<新依赖><br />
After=<新依赖>}}<br />
<br />
然后运行以下命令使更改生效:<br />
<br />
# systemctl reenable <单元><br />
# systemctl restart <单元><br />
<br />
{{小贴士|可以用 {{ic|systemd-delta}} 命令来查看哪些单元文件被覆盖、哪些被修改。}}<br />
<br />
=== 单元配置文件的 vim 语法高亮支持 ===<br />
<br />
可从 [[Official Repositories|官方仓库]] 安装 {{Pkg|vim-systemd}} 软件包,使 unit 配置文件在 [[Vim]] 下支持语法高亮。<br />
<br />
== 目标(target) ==<br />
启动级别(runlevel)是一个旧的概念。现在,systemd 引入了一个和启动级别功能相似又不同的概念——目标(target)。不像数字表示的启动级别,每个''目标''都有名字和独特的功能,并且能同时启用多个。一些''目标''继承其他''目标''的服务,并启动新服务。systemd 提供了一些模仿 sysvinit 启动级别的''目标'',仍可以使用旧的 {{ic|telinit 启动级别}} 命令切换。<br />
<br />
=== 获取当前目标 ===<br />
不要使用 {{ic|runlevel}} 命令了:<br />
$ systemctl list-units --type=target<br />
<br />
=== 创建新目标 ===<br />
在 Fedora 中,启动级别 0、1、3、5、6 都被赋予特定用途,并且都对应一个 systemd 的''目标''。然而,没有什么很好的移植用户定义的启动级别(2、4)的方法。要实现类似功能,可以以原有的启动级别为基础,创建一个新的''目标'' {{ic|/etc/systemd/system/<新目标>}}(可以参考 {{ic|/usr/lib/systemd/system/graphical.target}}),创建 {{ic|/etc/systemd/system/<新目标>.wants}} 目录,向其中加入额外服务的链接(指向 {{ic|/usr/lib/systemd/system/}} 中的单元文件)。<br />
<br />
=== 目标表 ===<br />
{| border="1"<br />
!SysV 启动级别!!Systemd 目标!!注释<br />
|-<br />
| 0 || runlevel0.target, poweroff.target || 中断系统(halt)<br />
|-<br />
| 1, s, single || runlevel1.target, rescue.target || 单用户模式<br />
|-<br />
| 2, 4 || runlevel2.target, runlevel4.target, multi-user.target || 用户自定义启动级别,通常识别为级别3。<br />
|-<br />
| 3 || runlevel3.target, multi-user.target || 多用户,无图形界面。用户可以通过终端或网络登录。<br />
|-<br />
| 5 || runlevel5.target, graphical.target || 多用户,图形界面。继承级别3的服务,并启动图形界面服务。<br />
|-<br />
| 6 || runlevel6.target, reboot.target || 重启<br />
|-<br />
| emergency || emergency.target || 急救模式(Emergency shell)<br />
|-<br />
|}<br />
<br />
=== 切换启动级别/目标 ===<br />
systemd 中,启动级别通过“目标单元”访问。通过如下命令切换:<br />
# systemctl isolate graphical.target<br />
该命令对下次启动无影响。等价于{{ic|telinit 3}} 或 {{ic|telinit 5}}。<br />
<br />
=== 修改默认启动级别/目标 ===<br />
开机启动进的目标是 {{ic|default.target}},默认链接到 {{ic|graphical.target}} (大致相当于原来的启动级别5)。可以通过[[Kernel parameters (简体中文)|内核参数]]更改默认启动级别:<br />
<br />
{{小贴士|可以省略扩展名 {{ic|.target}}。}}<br />
<br />
* {{ic|1=systemd.unit=multi-user.target}} (大致相当于级别3)<br />
* {{ic|1=systemd.unit=rescue.target}} (大致相当于级别1)<br />
<br />
另一个方法是修改 {{ic|default.target}}。可以通过 {{ic|systemctl}} 修改它:<br />
<br />
# systemctl enable multi-user.target<br />
<br />
命令执行情况由 {{ic|systemctl}} 显示:链接 {{ic|/etc/systemd/system/default.target}} 被创建,指向新的默认启动级别。该方法当且仅当目标配置文件中有以下内容时有效:<br />
<br />
[Install]<br />
Alias=default.target<br />
<br />
目前,{{ic|multi-user.target}}、{{ic|graphical.target}} 都包含这段内容。<br />
<br />
== 日志 ==<br />
自版本 38 起,systemd 提供了自己日志系统(logging system),称为 journal. 使用 systemd 日志,无需额外安装日志服务(syslog)。读取日志的命令:<br />
# journalctl<br />
<br />
默认情况下(当 {{ic|Storage&#61;}} 在文件 {{ic|/etc/systemd/journald.conf}} 中被设置为 {{ic|auto}}),日志记录将被写入 {{ic|/var/log/journal/}}。若目录 {{ic|/var/log/journal/}} 不存在(例如,被用户或某个程序删除),systemd将'''不会'''自动创建它,而是将日志记录写入 {{ic|/run/systemd/journal}}。这意味着,系统重启后日志将丢失。<br />
<br />
=== 过滤输出 ===<br />
<br />
{{ic|journalctl}}可以根据特定字段过滤输出,例如:<br />
<br />
显示本次启动后的所有日志:<br />
<br />
# journalctl -b<br />
<br />
不过,一般大家更关心的不是本次启动后的日志,而是上次启动时的(例如,刚刚系统崩溃了)。目前还没有这项功能,正在 [http://comments.gmane.org/gmane.comp.sysutils.systemd.devel/6608 systemd-devel@lists.freedesktop.org] 讨论中。<br />
<br />
目前的折中方案是:<br />
<br />
# journalctl --since=today | tac | sed -n '/-- Reboot --/{n;:r;/-- Reboot --/q;p;n;b r}' | tac<br />
<br />
以上命令输出本日内的所有启动信息。但要注意,如果日志很多,该命令执行时间会比较漫长。<br />
<br />
动态跟踪最新信息:<br />
# journalctl -f<br />
<br />
显示特定程序的所有消息:<br />
{{bc|# journalctl /usr/lib/systemd/systemd}}<br />
<br />
显示特定进程的所有消息:<br />
{{bc|1=# journalctl _PID=1}}<br />
<br />
显示指定单元的所有消息:<br />
# journalctl -u netcfg<br />
<br />
详情参阅{{ic|man journalctl}}、{{ic|man systemd.journal-fields}},以及 Lennert 的这篇[http://0pointer.de/blog/projects/journalctl.html 博文]。<br />
<br />
=== 日志大小限制 ===<br />
<br />
如果按上面的操作保留日志的话,默认日志最大限制为所在文件系统容量的 10%,即:如果 {{ic|/var/log/journal}} 储存在 50GiB 的根分区中,那么日志最多存储 5GiB 数据。可以修改 {{ic|/etc/systemd/journald.conf}} 中的 {{ic|SystemMaxUse}} 来指定该最大限制。如限制日志最大 50MiB:<br />
<br />
SystemMaxUse=50M<br />
<br />
详情参见 {{ic|man journald.conf}}.<br />
<br />
=== 配合 syslog 使用 ===<br />
systemd 提供了 socket {{ic|/run/systemd/journal/syslog}},以兼容传统日志服务。所有系统信息都会被传入。要使传统日志服务工作,需要让服务链接该 socket,而非 {{ic|/dev/log}}([http://lwn.net/Articles/474968/ 官方说明])。Arch 软件仓库中的 {{pkg|syslog-ng}} 已经包含了需要的配置。<br />
<br />
设置开机启动 syslog-ng:<br />
# systemctl enable syslog-ng<br />
<br />
== 优化 ==<br />
<br />
参阅:[[Improve Boot Performance (简体中文)]]。<br />
<br />
=== 启动过程分析 ===<br />
systemd 提供了一个分析启动过程的工具 —— {{ic|systemd-analyze}}。可以用它看看哪些单元拖慢了开机过程,并据此进行优化。安装 {{Pkg|python2-dbus}}、{{Pkg|python2-cairo}} 软件包后该工具才能工作。<br />
<br />
查看开机过程在内核/用户空间消耗的时间:<br />
$ systemd-analyze<br />
{{小贴士|如果在 {{ic|/etc/mkinitcpio.conf}} 的 {{ic|HOOKS}} 数组添加 {{ic|timestamp}},并重新生成启动内存镜像({{ic|mkinitcpio -p linux}}),{{ic|systemd-analyze}} 还可以显示处理该启动镜像花费的时间。}}<br />
<br />
按照耗费时间顺序,输出启动每个单元耗费的时间:<br />
$ systemd-analyze blame<br />
<br />
生成类似于 [[Bootchart (简体中文)|bootchart]] 的开机过程图表:<br />
$ systemd-analyze plot > plot.svg<br />
<br />
==== 使用 systemd-bootchart ====<br />
<br />
自2012年10月17日,bootchart 工具已经合并进 systemd 中,使用方法和原来的 bootchart 大同小异,添加下列内容到内核参数即可:<br />
<br />
initcall_debug printk.time=y init=/usr/lib/systemd/systemd-bootchart<br />
<br />
==== 使用 bootchart2 ====<br />
<br />
由于没有办法在内核参数设置两个 init,所以不能使用源里的 bootchart。不过,[[AUR (简体中文)|AUR]] 软件包 {{AUR|bootchart2}} 提供了一个的 systemd 服务,这样就可以和 systemd 一起使用。安装后启用即可:<br />
{{bc|# systemctl enable bootchart.service}}<br />
详情参阅 [https://github.com/mmeeks/bootchart bootchart 文档]。<br />
<br />
=== 预读 ===<br />
systemd 自己实现了一个 readahead,可以用来提高开机效率。不过,效果会因内核版本和硬件情况而不同(极少数情况下还会变慢)。开启 readahead:<br />
<br />
# systemctl enable systemd-readahead-collect.service systemd-readahead-replay.service<br />
<br />
要知道,readahead 的超级牛力只有在重启几次后才会显现。<br />
<br />
== 疑难解答 ==<br />
<br />
=== 关机/重启十分缓慢 ===<br />
<br />
如果关机特别慢(甚至跟死机了一样),很可能是某个拒不退出的服务在作怪。systemd 会等待一段时间,然后再尝试杀死它。请阅读[http://freedesktop.org/wiki/Software/systemd/Debugging#Shutdown_Completes_Eventually 这篇文章],确认你是否是该问题受害者。<br />
<br />
=== 短时进程无日志记录 ===<br />
<br />
若 {{ic|journalctl -u foounit.service}} 没有显示某个短时进程的任何输出,那么改用 PID 试试。例如,若 systemd-modules-load.service 执行失败,那么先用 {{ic|systemctl status systemd-modules-load}} 查询其 PID(比如是123),然后检索该 PID 相关的日志 {{ic|journalctl -b _PID&#61;123}}。运行时进程的日志元数据(诸如 _SYSTEMD_UNIT 和 _COMM)被乱序收集在 {{ic|/proc}} 目录。要修复该问题,必须修改内核,使其通过套接字连接来提供上述数据,该过程类似于 SCM_CREDENTIALS。<br />
<br />
== 相关资源 ==<br />
<br />
*[http://www.freedesktop.org/wiki/Software/systemd 官方网站]<br />
*[http://0pointer.de/public/systemd-man/ man 手册页]<br />
*[http://freedesktop.org/wiki/Software/systemd/Optimizations systemd 优化]<br />
*[http://www.freedesktop.org/wiki/Software/systemd/FrequentlyAskedQuestions 常见问题 FAQ]<br />
*[http://www.freedesktop.org/wiki/Software/systemd/TipsAndTricks 小技巧]<br />
*[http://0pointer.de/public/systemd-ebook-psankar.pdf systemd for Administrators (PDF)]<br />
*[http://fedoraproject.org/wiki/Systemd Fedora 项目对 systemd 的介绍]<br />
*[http://fedoraproject.org/wiki/How_to_debug_Systemd_problems 如何调试 systemd 故障]<br />
*''The H Open'' 杂志上的[http://www.h-online.com/open/features/Control-Centre-The-systemd-Linux-init-system-1565543.html 两][http://www.h-online.com/open/features/Booting-up-Tools-and-tips-for-systemd-1570630.html 篇]科普文章<br />
*[http://0pointer.de/blog/projects/systemd.html Lennart 的博文]<br />
*[http://0pointer.de/blog/projects/systemd-update.html 更新报告]<br />
*[http://0pointer.de/blog/projects/systemd-update-2.html 更新报告2]<br />
*[http://0pointer.de/blog/projects/systemd-update-3.html 更新报告3]<br />
*[http://0pointer.de/blog/projects/why.html 最新动态]<br />
*[https://fedoraproject.org/wiki/SysVinit_to_Systemd_Cheatsheet/zh Fedora Wiki 上的 SysVinit 和 systemd 命令对比表]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Systemd_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=247131
Systemd (简体中文)
2013-02-12T14:30:15Z
<p>Cuihao: </p>
<hr />
<div>{{Lowercase title}}<br />
[[Category:Daemons and system services (简体中文)]]<br />
[[Category:Boot process (简体中文)]]<br />
[[en:Systemd]]<br />
[[es:Systemd]]<br />
[[fr:Systemd]]<br />
[[it:Systemd]]<br />
[[ja:Systemd]]<br />
[[ru:Systemd]]<br />
[[zh-TW:Systemd]]<br />
{{Article summary start}}<br />
{{Article summary text|本文介绍 Systemd 的安装和配置。}}<br />
{{Article summary heading|相关页面}}<br />
{{Article summary wiki|Systemd/Services}}<br />
{{Article summary wiki|systemd/User}}<br />
{{Article summary wiki|Systemd FAQ}}<br />
{{Article summary wiki|init Rosetta}}<br />
{{Article summary wiki|udev (简体中文)}}<br />
{{Article summary end}}<br />
{{TranslationStatus (简体中文)|Systemd|2013-02-05|246247}}<br />
摘自[http://freedesktop.org/wiki/Software/systemd 项目主页]:<br />
<br />
'''''systemd''' 是 Linux 下的一款系统和服务管理器,兼容 SysV 和 LSB 的启动脚本。systemd 的特性有:支持并行化任务;同时采用 socket 式与 [[D-Bus (简体中文)|D-Bus]] 总线式激活服务;按需启动守护进程(daemon);利用 Linux 的 [[cgroups|cgroups]] 监视进程;支持快照和系统恢复;维护挂载点和自动挂载点;各服务间基于依赖关系进行精密控制。systemd 完全可以替代 Arch 曾经使用的 [[SysVinit (简体中文)|sysvinit]] 启动系统。''<br />
<br />
{{注意|[https://bbs.archlinux.org/viewtopic.php?pid&#61;1149530#p1149530 这个帖子]详细解释了 Arch 向 systemd 迁移的原因。}}<br />
<br />
另见[[Wikipedia:Systemd|维基百科上的介绍]]。<br />
<br />
== 迁移前需考虑 ==<br />
<br />
* 阅读[http://freedesktop.org/wiki/Software/systemd/ 该站],了解 systemd。<br />
* systemd 自己有一套日志('''journal''')系统,用于代替 '''syslog'''。两者也可以共存,参见后面的[[#日志|日志部分]]。<br />
* 虽然 systemd 可以替换 '''cron'''、'''acpid'''、'''xinetd''' 等的部分功能。至少目前还可以继续使用这些服务,无需立即切换。<br />
* 交互式 initscripts 启动脚本在 systemd 中无法工作。例如 '''netcfg-menu''' [https://bugs.archlinux.org/task/31377 无法]在启动时显示。<br />
<br />
== 安装 ==<br />
{{注意|从[https://www.archlinux.org/news/systemd-is-now-the-default-on-new-installations/ 2012-10-13版安装介质]开始,安装程序已经默认安装{{pkg|systemd}} 和 {{pkg|systemd-sysvcompat}}。}}<br />
{{注意|如果是在 VPS 中使用 Arch,请先阅读:[[Virtual_Private_Server#Moving_your_VPS_from_initscripts_to_systemd]]。}}<br />
<br />
本部分帮助还在使用{{pkg|sysvinit}} 和 {{pkg|initscripts}} 的用户迁移到{{pkg|systemd}}.<br />
<br />
# 从[[Official_Repositories_(简体中文)|官方软件仓库]]安装 {{pkg|systemd}} 并添加[[Kernel parameters|内核参数]]{{Ic|1=init=/usr/lib/systemd/systemd}}<br />
# 用 {{ic|systemctl enable ''daemonname''}} [[#使用单元|启用]] {{ic|/etc/rc.conf}} 中的守护进程。从 {{ic|/etc/rc.conf}} 到 systemd 服务的转换可以参考:[[Daemons_List_(简体中文)|守护进程列表]] 和 [[Systemd/Services|Services]]。<br />
# 重启系统,执行命令 {{ic|cat /proc/1/comm}},如果返回{{ic|systemd}},表示 systemd 已经正常启动。<br />
# 确认主机名已经正确设置:{{ic|hostnamectl set-hostname myhostname}}。<br />
# 删除 {{pkg|initscripts}} 和 {{pkg|sysvinit}},并安装{{pkg|systemd-sysvcompat}}.<br />
# 可选,删除{{ic|1=init=/usr/lib/systemd/systemd}}内核参数,现在已经不需要它了,{{pkg|systemd-sysvcompat}}提供了默认的 init.<br />
<br />
=== 附加信息 ===<br />
* 如果内核参数中有 {{ic|quiet}},可以先删除它以便调试。<br />
{{Accuracy|reason=据有的用户称,systemd 并未对用户组有特殊要求。尽管有很多不常用的用户组,但从 consolekit 切换到 logind 后这些用户组没有发生改变。''(据译者理解:如果你碰到特殊的权限问题,可能仍需要加入这些用户组才能解决。)''}}<br />
* 使用 systemd 的时候'''无需'''将用户加入特殊[[Users and Groups (简体中文)|用户组]](如{{ic|sys}}、{{ic|disk}}、{{ic|lp}}、{{ic|network}}、{{ic|video}}、{{ic|audio}}、{{ic|optical}}、{{ic|storage}}、{{ic|scanner}}、{{ic|power}}等等)。加入这些组反而会有问题,例如audio组会导致程序阻塞软件混声。每个 PAM 登录都拥有一个 logind 会话,它通过[[Wikipedia:Access control list|POSIX ACLs]],赋予本地会话以声音设备访问权限、通过[[udisks]]挂载和卸载移动设备的权限等。<br />
{{注意|对于使用 {{ic|startx -- vt7}} 进入X环境的用户,由于 logind 会话无法应用于X环境,用户'''必须'''加入这些组。}}<br />
* 如果卸载 {{Pkg|initscripts}},{{ic|rc.conf}} 将不再有效。如果曾在其中设置了静态网络,或还未迁移到systemd的系统服务,请根据[[#Initscripts 模拟|该节]]配置。<br />
* 如果 fstab 中有 LVM 设备,首次启动时会因为等待超时进入急救控制台。输入管理员密码,执行 {{ic|systemctl enable lvm}} 启用相关服务,重启即可。<br />
<br />
== 原生 systemd 配置文件 ==<br />
{{注意|可能需要手动创建某些文件。所有文件的权限都是644,属主 root,属组 root。}}<br />
<br />
=== 主机名 ===<br />
主机名通过文件 {{ic|/etc/hostname}} 进行配置。该文件不应包含系统的域名(如果有)。使用如下命令设置主机名:<br />
# hostnamectl set-hostname '''myhostname'''<br />
<br />
详情参见 {{ic|man 5 hostname}} 和 {{ic|man 1 hostnamectl}}。<br />
<br />
文件示例:<br />
{{hc|/etc/hostname|<br />
myhostname<br />
}}<br />
<br />
=== 本地化设置(locale) ===<br />
系统默认 locale 记录在 {{ic|/etc/locale.conf}} 文件中。通过如下命令配置:<br />
# localectl set-locale LANG="zh_CN.utf8"<br />
<br />
{{注意|设置默认locale前,请确保已经生成该locale。方法是:在 {{ic|/etc/locale.gen}} 中启用对应locale,然后以root权限执行 {{ic|locale-gen}}。}}<br />
<br />
详情参见 {{ic|man 1 localectl}} 和 {{ic|man 5 locale.conf}}。<br />
<br />
* 另参阅 [[Locale (简体中文)]]。<br />
<br />
文件示例:<br />
{{hc|/etc/locale.conf|<nowiki><br />
LANG=en_US.utf8<br />
</nowiki>}}<br />
<br />
=== 虚拟控制台 ===<br />
{{ic|/etc/vconsole.conf}} 文件用来配置虚拟控制台,包括键盘布局和控制台字体:<br />
{{hc|/etc/vconsole.conf|<nowiki><br />
KEYMAP=us<br />
FONT=lat9w-16<br />
FONT_MAP=8859-1_to_uni</nowiki>}}<br />
<br />
{{注意|{{ic|{{Pkg|systemd}}-194}} 默认使用内核字体和键盘布局,因此以后不必在上述文件中留下空的 {{ic|1=KEYMAP=}} 和 {{ic|1=FONT=}} 设置。}}<br />
<br />
还可以通过命令设置键盘布局:<br />
# localectl set-keymap de<br />
<br />
{{ic|localectl}} 也可以设置X11的键盘布局:<br />
<br />
# localectl set-x11-keymap de<br />
<br />
详情参见 {{ic|man 1 localectl}} 和 {{ic|man 5 vconsole.conf}}。<br />
<br />
* 另参阅 [[Fonts (简体中文)#控制台字体|控制台字体]] 和 [[KEYMAP (简体中文)#键盘布局|键盘布局]]。<br />
<br />
=== 时区 ===<br />
时区可通过创建适当的{{ic|/etc/localtime}}符号链接来进行配置,该链接需指向目录{{ic|/usr/share/zoneinfo/}}下的一个zoneinfo文件。也可执行以下命令直接完成这一操作:<br />
<br />
# timedatectl set-timezone Asia/Shanghai<br />
<br />
更多细节参见{{ic|man 1 timedatectl}},{{ic|man 5 localtime}}和{{ic|man 7 archlinux}}。<br />
<br />
手动创建链接也行:<br />
# ln -sf ../usr/share/zoneinfo/Asia/Shanghai /etc/localtime<br />
<br />
旧的配置文件 {{ic|/etc/timezone}} 已经没用了,可以安全删除。<br />
<br />
=== 硬件时钟 ===<br />
<br />
systemd 默认识别硬件时钟为协调世界时(UTC)。<br />
<br />
{{小贴士|推荐使用[[NTP (简体中文)|NTP服务]]来在线同步硬件时钟。}}<br />
<br />
==== 硬件时钟设定为地方时 ====<br />
<br />
将硬件时钟配置为地方时('''不建议'''):<br />
<br />
# timedatectl set-local-rtc true<br />
<br />
重新调整为 UTC:<br />
<br />
# timedatectl set-local-rtc false<br />
<br />
如果设置成本地时间,处理夏令时有些麻烦。如果夏令时调整发生在关机时,下次启动时时间会出现问题([http://www.cl.cam.ac.uk/~mgk25/mswish/ut-rtc.html 更多信息])。最新的内核直接从实时时钟芯片(RTC)读取时间,不使用 {{ic|hwclock}},内核把从 RTC 读取的时间当作 UTC 处理。所以如果硬件时间是地方时,系统启动一开始识别的时间是错误的,之后很快会进行矫正。这可能导致一些问题(尤其是时间倒退时)。<br />
<br />
如果同时安装了 Windows 操作系统([http://blogs.msdn.com/b/oldnewthing/archive/2004/09/02/224672.aspx 默认使用地方时]),那么一般 RTC 会被设置为地方时。Windows 其实也能处理 UTC,需要[[Time (简体中文)#Windows 系统使用 UTC|修改注册表]]。建议让 Windows 使用 UTC,而非让 Linux 使用地方时。Windows 使用 UTC 后,请记得禁用 Windows 的时间同步功能,以防 Windows 错误设置硬件时间。如上文所说,Linux 可以使用[[NTP (简体中文)|NTP服务]]来在线同步硬件时钟。<br />
<br />
* 详情参阅 [[Time (简体中文)]]。<br />
<br />
=== 内核模块 ===<br />
目前,所有必要模块的加载均由 [[udev]] 自动完成。所以,如果不需要使用任何额外的模块,就没有必要在任何配置文件中添加启动时加载的模块。但是,有些情况下可能需要在系统启动时加载某个额外的模块,或者将某个模块列入黑名单以便使系统正常运行。<br />
<br />
==== 开机加载 ====<br />
systemd 读取 {{ic|/etc/modules-load.d/}} 中的配置加载额外的内核模块。配置文件名称通常为 {{ic|/etc/modules-load.d/<program>.conf}}。格式很简单,一行一个要读取的模块名,而空行以及第一个非空格字符为{{ic|#}}或{{ic|;}}的行会被忽略,如:<br />
{{hc|/etc/modules-load.d/virtio-net.conf|<nowiki><br />
# Load virtio-net.ko at boot<br />
virtio-net</nowiki>}}<br />
<br />
另见{{ic|man 5 modules-load.d}}。<br />
<br />
==== 禁用内核模块 ====<br />
禁用内核模块的方法和 Arch 默认的 {{Pkg|initscripts}} 相同,因为该过程实际由 {{Pkg|kmod}} 处理。参见:[[Kernel modules (简体中文)#黑名单]]。<br />
<br />
=== 文件系统挂载 ===<br />
<br />
默认行为是:在启动一个需要挂载特定分区的服务之前,系统自动检查并挂载分区。{{ic|/etc/fstab}} 中设定的网络文件系统(如 [[NFS (简体中文)|NFS]]、[[Samba (简体中文)|Samba]])无需配置即可正常工作,systemd 将确保网络文件系统在网络链接就绪后挂载。<br />
<br />
详情参阅:{{ic|man 5 systemd.mount}}。<br />
<br />
==== 自动挂载 ====<br />
<br />
* 如果 {{ic|/home}} 分区较大,可以让不依赖 {{ic|/home}} 分区的服务先启动。把下面的参数添加到 {{ic|/etc/fstab}} 文件中 {{ic|/home}} 项目的参数部分即可:<br />
<br />
noauto,x-systemd.automount<br />
<br />
这样 {{ic|/home}} 分区只有需要访问时才会被挂载。内核会缓存所有的文件操作,直到 {{ic|/home}} 分区准备完成。<br />
<br />
{{注意|这样做会使 {{ic|/home}} 的文件系统类型被识别为 {{ic|autofs}},造成 [[mlocate]] 查询时忽略该目录。实际加速效果因配置而异,所以请自己权衡是否需要。}}<br />
<br />
* 挂载远程文件系统也是同理。如果你仅想在需要的时候才挂载,也可以添加 {{ic|noauto,x-systemd.automount}} 参数。另外,可以设置 {{ic|1=x-systemd.device-timeout=#}} 参数,设置超时时间,以防止网络资源不能访问的时候浪费时间。<br />
<br />
* 如果你的加密文件系统需要密钥,则需要添加 {{ic|noauto}} 参数到 {{ic|/etc/crypttab}} 文件中的对应位置。systemd 开机的时候就不会打开这个加密设备,会一直等待到设备被访问时再使用密钥文件挂载。比如在使用加密RAID设备的时候可以节省一定的时间,因为 systemd 不必等到设备可用后才能访问。例如:<br />
<br />
{{hc|/etc/crypttab|<br />
data /dev/md0 /root/key noauto}}<br />
<br />
==== LVM ====<br />
<br />
如果装有不通过 [[Mkinitcpio (简体中文)|initramfs]] 激活的[[LVM (简体中文)|LVM]]卷,则需启动 {{ic|lvm-monitoring}} 服务(由 {{pkg|lvm2}} 软件包提供):<br />
<br />
# systemctl enable lvm-monitoring<br />
<br />
类似地,如果LVM卷在加密设备上,并且启动后才激活,则需启动 {{ic|lvm-on-crypt}} 服务(同样由 {{pkg|lvm2}} 软件包提供):<br />
<br />
# systemctl enable lvm-on-crypt<br />
<br />
=== ACPI 电源管理 ===<br />
systemd 能够处理某些电源相关的 [[Wikipedia:Advanced_Configuration_and_Power_Interface|ACPI]] 事件,通过 {{ic|/etc/systemd/logind.conf}} 的下列选项配置:<br />
* {{ic|HandlePowerKey}}:按下电源键后的动作<br />
* {{ic|HandleSleepKey}}:按下挂起键后的动作<br />
* {{ic|HandleHibernateKey}}: 按下休眠键后的动作<br />
* {{ic|HandleLidSwitch}}:合上笔记本盖后待机<br />
<br />
动作可以是 {{ic|ignore}}、{{ic|poweroff}}、{{ic|reboot}}、{{ic|halt}}、{{ic|suspend}}、{{ic|hibernate}}、{{ic|hybrid-sleep}}、{{ic|lock}} 或 {{ic|kexec}}。<br />
<br />
系统默认设置为:<br />
HandlePowerKey=poweroff<br />
HandleSuspendKey=suspend<br />
HandleHibernateKey=hibernate<br />
HandleLidSwitch=suspend<br />
<br />
如果不用图形界面,或者使用 [[i3 (简体中文)|i3]]、[[awesome (简体中文)|awesome]] 这样简单的桌面管理器时,systemd 可以替代 [[acpid (简体中文)|acpid]] 处理 ACPI 事件。<br />
<br />
{{注意|systemd 无法处理交流电源和电池 ACPI 事件,所以还得使用 [[Laptop Mode Tools (简体中文)|Laptop Mode Tools]] 或 [[acpid (简体中文)|acpid]] 工具。}}<br />
<br />
在当前版本的 systemd 中,这些 {{ic|Handle}} 选项将会被应用到整个系统当中,除非它们被别的程序——例如某个桌面环境中的电源管理器——给“阻止”(inhibited)。如果其它的程序没有阻止这些 {{ic|Handle}} ,你可能会先被 systemd 挂起你的系统,然后当系统被唤醒之后,电源管理器又会再次将系统挂起。<br />
<br />
{{警告|目前只有[[GNOME]] 和 [[KDE]] 支持 "inhibited" 命令。在其它的桌面管理器同样实现该功能之前,如果你想使用[[Xfce]]、[[acpid]] 或者其它程序来管理 ACPI 事件,你需要把 {{ic|Handle}} 选项设置为 {{ic|ignore}} 。包含该功能的新版本还在开发当中。}}<br />
<br />
{{注意|除了内核默认的待机支持后端(用于处理待机/休眠),systemd 也可以使用其他后端(比如 [[Uswsusp]] 或 [[TuxOnIce]])。}}<br />
<br />
==== 休眠时执行的脚本 ====<br />
<br />
使用 {{ic|systemctl suspend}}、{{ic|systemctl hibernate}} 或 {{ic|systemctl hybrid-sleep}} 命令执行待机/休眠时,systemd 不会调用 [[pm-utils (简体中文)|pm-utils]]。[[pm-utils (简体中文)|pm-utils]] 的钩子扩展(hook)——包括 [[pm-utils (简体中文)#建立你自己的钩子|自定义钩子]]——会失效。不过,systemd 提供了两种类似的待机/休眠时执行脚本的机制。<br />
<br />
===== 使用服务文件 =====<br />
<br />
可以将服务文件附在 suspend.target、hibernate.target 或 sleep.target 中,这样就能在待机/休眠前后执行某些操作。用户级操作和root/系统级操作应该使用不同的服务文件。要启用用户级服务文件,使用 {{ic|# systemctl enable suspend@<用户名> && systemctl enable resume@<用户名>}}。例如:<br />
<br />
{{hc|/etc/systemd/system/suspend@.service|2=<nowiki><br />
[Unit]<br />
Description=User suspend actions<br />
Before=sleep.target<br />
<br />
[Service]<br />
User=%I<br />
Type=forking<br />
Environment=DISPLAY=:0<br />
ExecStartPre= -/usr/bin/pkill -u %u unison ; /usr/local/bin/music.sh stop ; /usr/bin/mysql -e 'slave stop'<br />
ExecStart=/usr/bin/sflock<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
{{hc|/etc/systemd/system/resume@.service|2=<nowiki><br />
[Unit]<br />
Description=User resume actions<br />
After=suspend.target<br />
<br />
[Service]<br />
User=%I<br />
Type=simple<br />
ExecStartPre=/usr/local/bin/ssh-connect.sh<br />
ExecStart=/usr/bin/mysql -e 'slave start'<br />
<br />
[Install]<br />
WantedBy=suspend.target</nowiki>}}<br />
<br />
至于root/系统级服务(使用 {{ic|# systemctl enable root-suspend}} 激活):<br />
<br />
{{hc|/etc/systemd/system/root-resume.service|2=<nowiki><br />
[Unit]<br />
Description=Local system resume actions<br />
After=suspend.target<br />
<br />
[Service]<br />
Type=simple<br />
ExecStart=/usr/bin/systemctl restart mnt-media.automount<br />
<br />
[Install]<br />
WantedBy=suspend.target</nowiki>}}<br />
<br />
{{hc|/etc/systemd/system/root-suspend.service|2=<nowiki><br />
[Unit]<br />
Description=Local system suspend actions<br />
Before=sleep.target<br />
<br />
[Service]<br />
Type=simple<br />
ExecStart=-/usr/bin/pkill sshfs<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
上述服务文件的一些解释(详见 {{ic|man systemd.service}}):<br />
* 如果设置 {{ic|1=<nowiki>Type=OneShot</nowiki>}},那么可以使用多个 {{ic|1=<nowiki>ExecStart=</nowiki>}} 参数。否则只能写一个,替代方案是在 {{ic|ExecStartPre}} 中添加命令,或使用分号分隔不同命令(见第一个例子,分号前后的空格都是'''必须'''的)。 <br />
* 若命令前加上一个“-”(半角减号),则命令返回非零值时会被忽略、当作正常执行处理。<br />
* 有关调试,最好的方法是用 {{ic|journalctl}} 查看日志。<br />
<br />
===== 合并待机和唤醒服务文件 =====<br />
<br />
利用下面这种自定义的一体化待机和唤醒服务,使用单一的钩子扩展即可对不同操作(待机/休眠/混合休眠)的不同阶段(进入/唤醒)进行控制。<br />
<br />
例子和解释:<br />
<br />
{{hc|/etc/systemd/system/wicd-sleep.service|2=<nowiki><br />
[Unit]<br />
Description=Wicd sleep hook<br />
Before=sleep.target<br />
StopWhenUnneeded=yes<br />
<br />
[Service]<br />
Type=oneshot<br />
RemainAfterExit=yes<br />
ExecStart=-/usr/share/wicd/daemon/suspend.py<br />
ExecStop=-/usr/share/wicd/daemon/autoconnect.py<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
* {{ic|1=<nowiki>RemainAfterExit=yes</nowiki>}}:服务启动后,除非显式地停止,否则就认为是活动的。<br />
* {{ic|1=<nowiki>StopWhenUnneeded=yes</nowiki>}}:服务活动时,如果无其他服务依赖该服务,就停止它。在本例中,该服务会在 sleep.target 停止后停止活动。<br />
* 由于 sleep.target 会被 suspend.target、hibernate.target、hybrid-sleep.target 调用,且 sleep.target 本身设置了 StopWhenUnneeded,该服务文件对以上各种操作都是有效的。<br />
<br />
===== 使用 /usr/lib/systemd/system-sleep =====<br />
<br />
systemd 在待机/休眠时执行 {{ic|/usr/lib/systemd/system-sleep/}} 里的所有脚本,传递下面两个参数:<br />
<br />
* 参数1:若是准备进入待机/休眠状态,则为 {{ic|pre}};唤醒时为 {{ic|post}}。<br />
* 参数2:事件名称,{{ic|suspend}},{{ic|hibernate}} 或 {{ic|hybrid-sleep}}。<br />
<br />
systemd 会同时执行所有脚本,而不是像 [[pm-utils (简体中文)|pm-utils]] 那样顺序执行。<br />
<br />
脚本输出会记录在相关服务({{ic|systemd-suspend.service}}、{{ic|systemd-hibernate.service}} 或 {{ic|systemd-hybrid-sleep.service}})中。通过[[#日志|日志]]查看:<br />
# journalctl -b -u systemd-suspend<br />
<br />
脚本示范:<br />
<br />
{{hc|/usr/lib/systemd/system-sleep/example.sh|<br />
#!/bin/sh<br />
case $1/$2 in<br />
pre/*)<br />
echo "进入 $2 状态..."<br />
;;<br />
post/*)<br />
echo "从 $2 状态唤醒..."<br />
;;<br />
esac}}<br />
<br />
记得添加可执行权限: <br />
# chmod a+x /usr/lib/systemd/system-sleep/example.sh<br />
<br />
详情参见 {{ic|man 7 systemd.special}} 和 {{ic|man 8 systemd-sleep}}。<br />
<br />
=== 临时文件 ===<br />
{{ic|/usr/lib/tmpfiles.d/}} 和 {{ic|/etc/tmpfiles.d/}} 中的文件描述了 systemd-tmpfiles 如何创建、清理、删除临时文件和目录,这些文件和目录通常存放在 {{ic|/run}} 和 {{ic|/tmp}} 中。配置文件名称为 {{ic|/etc/tmpfiles.d/<program>.conf}}。此处的配置能覆盖 {{ic|/usr/lib/tmpfiles.d/}} 目录中的同名配置。<br />
<br />
临时文件通常和服务文件同时提供,以生成守护进程需要的文件和目录。例如 [[Samba (简体中文)|Samba]] 服务需要目录 {{ic|/run/samba}} 存在并设置正确的权限位,就象这样:<br />
{{hc|/usr/lib/tmpfiles.d/samba.conf|<br />
D /run/samba 0755 root root<br />
}}<br />
<br />
此外,临时文件还可以用来在开机时向特定文件写入某些内容。比如,要禁止系统从USB设备唤醒,利用旧的 {{ic|/etc/rc.local}} 可以用 {{ic|echo USBE > /proc/acpi/wakeup}},而现在可以这么做:<br />
{{hc|/etc/tmpfiles.d/disable-usb-wake.conf|<br />
w /proc/acpi/wakeup - - - - USBE<br />
}}<br />
<br />
详情参见 {{ic|man 5 tmpfiles.d}}。<br />
<br />
=== 单元(unit) ===<br />
一个单元配置文件可以描述如下内容之一:系统服务、socket、系统设备、挂载点、交换分区/文件、启动目标(target)、文件系统路径、由 systemd 管理的计时器。文件格式受 .desktop 文件启发,而最初起源是 Windows 下的 .ini 文件。<br />
<br />
详情参阅 {{ic|man 5 systemd.unit}}.<br />
<br />
== 从 initscripts 迁移到 systemd ==<br />
<br />
=== initscripts 模拟 ===<br />
<br />
{{Pkg|initscripts}} 软件包提供了 systemd 和 Arch 传统配置的整合。当同时安装了{{Pkg|initscripts}}和systemd时,若以systemd启动系统,systemd将执行以下操作:<br />
<br />
# 解析{{ic|/etc/rc.conf}}中的{{ic|DAEMONS}}数组,并在系统启动时,启动所有列出的守护进程<br />
# 在启动过程中执行{{ic|/etc/rc.local}}<br />
# 在系统关闭过程中执行{{ic|/etc/rc.local.shutdown}}<br />
<br />
Initscripts模拟仅仅是作为用户迁移到systemd时的过渡措施,这一方案最终将'''不再可行'''。原生的systemd并不依赖于{{ic|rc.conf}}文件的集中式配置,因此推荐使用[[#原生 systemd 配置文件|原生的systemd配置文件]]。原生systemd配置文件比{{ic|/etc/rc.conf}}具有更高优先级。<br />
<br />
{{注意|替换{{ic|/etc/rc.local}}的推荐方式是针对系统启动时要运行的程序编写自定义的service文件,详见相关[[#自己编写_.service_文件|章节]].}}<br />
<br />
{{注意|如果你曾经在{{ic|/etc/inittab}}中禁用了以{{keypress|Ctrl+Alt+Del}}重启系统,则需要以root执行{{ic|systemctl mask ctrl-alt-del.target}}以使其对systemd重新生效。}}<br />
<br />
{{警告|如果同时安装 systemd(197-4 及后续版本)和 initscripts,那么 {{ic|/etc/rc.local}} 脚本会导致启动过程无法结束(原因是 getty@tty1.service 自 197-4 版本起不再等待 rc-local.service 执行,getty 无法启动)。请删除或重命名 {{ic|/etc/rc.local}}。}}<br />
<br />
=== 迁移 DAEMONS 列表 ===<br />
<br />
若要用纯 systemd 环境,则应当删除 {{ic|/etc/rc.conf}} 文件,并完全通过 {{ic|systemctl}} 命令来启动服务。对于 {{ic|DAEMONS}} 列表里面的每个 {{ic|<服务名>}},现在可以用以下命令设置成自动启动:<br />
<br />
# systemctl enable <服务名>.service<br />
<br />
{{小贴士|[[Daemons List|这里]]有一份 initscripts 和 systemd 下常见服务的对应列表。}}<br />
<br />
如果提示 {{ic|<service_name>.service}} 不存在:<br />
<br />
* 很有可能是 systemd 用了不同的服务名。比如 {{ic|crond}} 服务替换成 {{ic|cronie.service}};{{ic|alsa}} 服务替换成了 {{ic|alsa-store.service}} 和 {{ic|alsa-restore.service}} 两个服务。另一个值得注意的是 {{ic|network}} 服务,它被替换为一整套服务文件(详情参见 [[Configuring_Network_(简体中文)]])。<br />
* 另外,有些程序尚未提供 .service 文件,还得继续用 {{ic|rc.conf}} 来配置开机自动启动。<br />
<br />
{{小贴士|用下面这个命令来查看已安装软件包包含的服务文件:<br />
$ pacman -Ql cronie<br />
[...]<br />
cronie /etc/rc.d/crond # DAEMONS 列表中使用的 initscript 服务 (原生 systemd 配置中不再使用)<br />
[...]<br />
cronie /usr/lib/systemd/system/cronie.service # 原生 systemd 服务文件<br />
[...]<br />
}}<br />
<br />
* 有些服务不需要用户明确配置自启动。比如安装 {{ic|dbus-core}} 软件包后 {{ic|dbus.service}} 就会自动被配置为自启动。而 {{ic|alsa-store.service}} 和 {{ic|alsa-restore.service}} 也会由 systemd 自动启动。可以用 {{ic|systemctl list-unit-files}} 命令来查看服务列表并检查运行状态。<br />
<br />
== systemd 基本工具 ==<br />
<br />
检视和控制systemd的主要命令是{{ic|systemctl}}。该命令可用于查看系统状态和管理系统及服务。详见{{ic|man 1 systemctl}}。<br />
<br />
{{小贴士|在 {{ic|systemctl}} 参数中添加 {{ic|-H <用户名>@<主机名>}} 可以实现对其他机器的远程控制。该过程使用 [[SSH (简体中文)|SSH]] 链接。}}<br />
<br />
{{注意|{{ic|systemadm}} 是 systemd 的官方图形前端。由 [[AUR (简体中文)|AUR]] 中的软件包 {{AUR|systemd-ui-git}} 提供。}}<br />
<br />
=== 分析系统状态 ===<br />
<br />
输出激活的单元:<br />
<br />
$ systemctl<br />
<br />
以下命令等效:<br />
<br />
$ systemctl list-units<br />
<br />
输出运行失败的单元:<br />
<br />
$ systemctl --failed<br />
<br />
所有可用的单元文件存放在 {{ic|/usr/lib/systemd/system/}} 和 {{ic|/etc/systemd/system/}} 目录(后者优先级更高)。查看所有已安装服务:<br />
$ systemctl list-unit-files<br />
<br />
=== 使用单元 ===<br />
一个单元可以是系统服务({{ic|.service}})、挂载点({{ic|.mount}})、sockets({{ic|.sockets}})。<br />
<br />
使用 {{ic|systemctl}} 控制单元时,通常需要使用单元文件的全名,包括扩展名(例如 {{ic|sshd.service}})。但是有些单元可以在{{ic|systemctl}}中使用简写方式。<br />
* 如果无扩展名,systemctl 默认把扩展名当作 {{ic|.service}}。例如 {{ic|netcfg}} 和 {{ic|netcfg.service}} 是等价的。<br />
* 挂载点会自动转化为相应的 {{ic|.mount}} 单元。例如 {{ic|/home}} 等价于 {{ic|home.mount}}。<br />
* 设备会自动转化为相应的 {{ic|.device}} 单元,所以 {{ic|/dev/sda2}} 等价于 {{ic|dev-sda2.device}}。<br />
<br />
立即激活单元:<br />
<br />
# systemctl start <单元><br />
<br />
立即停止单元:<br />
# systemctl stop <单元><br />
<br />
重启单元:<br />
<br />
# systemctl restart <单元><br />
<br />
命令单元重新读取配置:<br />
<br />
# systemctl reload <单元><br />
<br />
输出单元运行状态:<br />
<br />
$ systemctl status <单元><br />
<br />
检查单元是否配置为自动启动:<br />
$ systemctl is-enabled <单元><br />
<br />
开机自动激活单元:<br />
<br />
# systemctl enable <单元><br />
<br />
{{注意|如果服务没有{{ic|Install}}段落,一般意味着应该通过其它服务自动调用它们。如果真的需要手动安装,可以直接连接服务,如下(将{{ic|foo}}替换为真实的服务名):<br />
{{bc|# ln -s /usr/lib/systemd/system/foo.service /etc/systemd/system/graphical.target.wants/}}<br />
}}<br />
<br />
取消开机自动激活单元:<br />
<br />
# systemctl disable <单元>}}<br />
<br />
显示单元的手册页(必须由单元文件提供):<br />
<br />
# systemctl help <单元><br />
<br />
重新载入 systemd,扫描新的或有变动的单元:<br />
# systemctl daemon-reload<br />
<br />
=== 电源管理 ===<br />
<br />
安装 {{ic|polkit}} 后才可使用电源管理。<br />
<br />
如果你正登录在一个本地的{{ic|systemd-logind}}用户会话,且当前没有其它活动的会话,那么以下命令无需root权限即可执行。否则(例如,当前有另一个用户登录在某个tty),systemd 将会自动请求输入root密码。<br />
<br />
重启:<br />
<br />
$ systemctl reboot<br />
<br />
退出系统并停止电源:<br />
<br />
$ systemctl poweroff<br />
<br />
待机:<br />
<br />
$ systemctl suspend<br />
<br />
休眠:<br />
<br />
$ systemctl hibernate<br />
<br />
混合休眠模式(同时休眠到硬盘并待机):<br />
<br />
$ systemctl hybrid-sleep<br />
<br />
== 启动桌面环境 ==<br />
<br />
=== 使用登录管理器 ===<br />
<br />
通过启动[[Display Manager (简体中文)|登录管理器]](或称显示管理器),即可进行图形界面登录。目前,Arch 提供了 [[GDM (简体中文)|GDM]]、[[KDM (简体中文)|KDM]]、[[SLiM (简体中文)|SLiM]]、[[XDM (简体中文)|XDM]]、[[LXDM (简体中文)|LXDM]] 和 [[LightDM (简体中文)|LightDM]] 的 systemd 服务文件。以 KDM 为例,配置开机启动:<br />
# systemctl enable kdm.service<br />
<br />
执行上述命令后,登录管理器应当能正常工作了。如果不是的话,很可能是因为你修改了{{ic|default.target}}。默认情况应当如下:<br />
<br />
{{hc|# ls -l /etc/systemd/system/default.target|/etc/systemd/system/default.target -> /usr/lib/systemd/system/graphical.target}}<br />
<br />
删除被修改的 {{ic|default.target}} 即可,systemd 会自动使用默认配置(即 {{ic|graphical.target}}):<br />
<br />
# rm /etc/systemd/system/default.target<br />
<br />
=== 使用 systemd-logind ===<br />
<br />
{{注意|自 2012-10-30 起, [[ConsoleKit]] 已被 [https://www.archlinux.org/news/consolekit-replaced-by-logind/ systemd-logind 取代] ,作为登录到桌面环境的默认方式。}}<br />
<br />
可使用 {{ic|loginctl}} 来查看用户会话的状态。所有 [[PolicyKit]] 操作,如挂起系统、挂载外部驱动器,都无需配置即可使用。<br />
<br />
$ loginctl show-session $XDG_SESSION_ID<br />
<br />
== 自己编写 .service 文件 ==<br />
''参见:[[Systemd/Services]]''<br />
<br />
=== 处理依赖关系 ===<br />
<br />
使用systemd时,可通过正确编写单元配置文件来解决其依赖关系。典型的情况是,单元{{ic|A}}要求单元{{ic|B}}在{{ic|A}}启动之前运行。在此情况下,向单元{{ic|A}}配置文件中的 {{ic|[Unit]}} 段添加 {{ic|1=Requires=B}} 和 {{ic|1=After=B}} 即可。若此依赖关系是可选的,可添加 {{ic|1=Wants=B}} 和 {{ic|1=After=B}}。请注意 {{ic|1=Wants=}} 和 {{ic|1=Requires=}} 并不意味着 {{ic|1=After=}},即如果 {{ic|1=After=}} 选项没有制定,这两个单元将被并行启动。<br />
<br />
依赖关系通常被用在服务(service)而不是[[#目标(target)|目标(target)]]上。例如, {{ic|network.target}} 一般会被某个配置网络接口的服务引入,所以,将自定义的单元排在该服务之后即可,因为 {{ic|network.target}} 已经启动。<br />
<br />
=== 启动方式 ===<br />
<br />
编写自定义的service文件时,可以选择几种不同的服务启动方式。启动方式可通过配置文件 {{ic|[Service]}} 段中的 {{ic|1=Type=}} 参数进行设置。具体的参数说明请参阅 {{ic|man systemd.service}} 。<br />
<br />
* {{ic|1=Type=simple}}:systemd认为该服务将立即启动。服务进程不会fork。如果该服务要启动其他服务,不要使用此类型启动,除非该服务是socket激活型。<br />
* {{ic|1=Type=forking}}:systemd认为当该服务进程fork,且父进程退出后服务启动成功。对于常规的守护进程(daemon),除非你确定此启动方式无法满足需求,使用此类型启动即可。使用此启动类型应同时指定 {{ic|1=PIDFile=}},以便systemd能够跟踪服务的主进程。<br />
* {{ic|1=Type=oneshot}}:这一选项适用于只执行一项任务随后退出的服务。你可能需要同时设置 {{ic|1=RemainAfterExit=yes}} 使得 systemd 在服务进程退出之后仍然认为服务处于激活状态。<br />
* {{ic|1=Type=notify}}:与 {{ic|1=Type=simple}} 相同,但约定服务会在就绪后向 systemd 发送一个信号。这一通知的实现由 {{ic|libsystemd-daemon.so}} 提供。<br />
* {{ic|1=Type=dbus}}:若以此方式启动,当指定的 {{ic|BusName}} 出现在DBus系统总线上时,systemd认为服务就绪。<br />
<br />
=== 替换现存单元文件 ===<br />
<br />
目录 {{ic|/etc/systemd/system/}} 中的单元配置文件比 {{ic|/usr/lib/systemd/system/}} 目录下的文件拥有更高优先级。<br />
若需要定制某个单元的启动(使配置不受软件包更新的影响),请将现有的单元配置文件从 {{ic|/usr/lib/}} 复制到 {{ic|/etc/}} 目录下,并在此目录中修改。或者,可以使用 {{ic|.include}} 来解析一个现有的配置文件,并覆盖原选项或添加新选项。例如,若想要某个服务的配置文件中添加额外依赖关系,可以编写:<br />
<br />
{{hc|/etc/systemd/system/<服务名>.service|2=<br />
.include /usr/lib/systemd/system/<服务名>.service<br />
<br />
[Unit]<br />
Requires=<新依赖><br />
After=<新依赖>}}<br />
<br />
然后运行以下命令使更改生效:<br />
<br />
# systemctl reenable <单元><br />
# systemctl restart <单元><br />
<br />
{{小贴士|可以用 {{ic|systemd-delta}} 命令来查看哪些单元文件被覆盖、哪些被修改。}}<br />
<br />
=== 单元配置文件的 vim 语法高亮支持 ===<br />
<br />
可从 [[Official Repositories|官方仓库]] 安装 {{Pkg|vim-systemd}} 软件包,使 unit 配置文件在 [[Vim]] 下支持语法高亮。<br />
<br />
== 目标(target) ==<br />
启动级别(runlevel)是一个旧的概念。现在,systemd 引入了一个和启动级别功能相似又不同的概念——目标(target)。不像数字表示的启动级别,每个''目标''都有名字和独特的功能,并且能同时启用多个。一些''目标''继承其他''目标''的服务,并启动新服务。systemd 提供了一些模仿 sysvinit 启动级别的''目标'',仍可以使用旧的 {{ic|telinit 启动级别}} 命令切换。<br />
<br />
=== 获取当前目标 ===<br />
不要使用 {{ic|runlevel}} 命令了:<br />
$ systemctl list-units --type=target<br />
<br />
=== 创建新目标 ===<br />
在 Fedora 中,启动级别 0、1、3、5、6 都被赋予特定用途,并且都对应一个 systemd 的''目标''。然而,没有什么很好的移植用户定义的启动级别(2、4)的方法。要实现类似功能,可以以原有的启动级别为基础,创建一个新的''目标'' {{ic|/etc/systemd/system/<新目标>}}(可以参考 {{ic|/usr/lib/systemd/system/graphical.target}}),创建 {{ic|/etc/systemd/system/<新目标>.wants}} 目录,向其中加入额外服务的链接(指向 {{ic|/usr/lib/systemd/system/}} 中的单元文件)。<br />
<br />
=== 目标表 ===<br />
{| border="1"<br />
!SysV 启动级别!!Systemd 目标!!注释<br />
|-<br />
| 0 || runlevel0.target, poweroff.target || 中断系统(halt)<br />
|-<br />
| 1, s, single || runlevel1.target, rescue.target || 单用户模式<br />
|-<br />
| 2, 4 || runlevel2.target, runlevel4.target, multi-user.target || 用户自定义启动级别,通常识别为级别3。<br />
|-<br />
| 3 || runlevel3.target, multi-user.target || 多用户,无图形界面。用户可以通过终端或网络登录。<br />
|-<br />
| 5 || runlevel5.target, graphical.target || 多用户,图形界面。继承级别3的服务,并启动图形界面服务。<br />
|-<br />
| 6 || runlevel6.target, reboot.target || 重启<br />
|-<br />
| emergency || emergency.target || 急救模式(Emergency shell)<br />
|-<br />
|}<br />
<br />
=== 切换启动级别/目标 ===<br />
systemd 中,启动级别通过“目标单元”访问。通过如下命令切换:<br />
# systemctl isolate graphical.target<br />
该命令对下次启动无影响。等价于{{ic|telinit 3}} 或 {{ic|telinit 5}}。<br />
<br />
=== 修改默认启动级别/目标 ===<br />
开机启动进的目标是 {{ic|default.target}},默认链接到 {{ic|graphical.target}} (大致相当于原来的启动级别5)。可以通过[[Kernel parameters (简体中文)|内核参数]]更改默认启动级别:<br />
<br />
{{小贴士|可以省略扩展名 {{ic|.target}}。}}<br />
<br />
* {{ic|1=systemd.unit=multi-user.target}} (大致相当于级别3)<br />
* {{ic|1=systemd.unit=rescue.target}} (大致相当于级别1)<br />
<br />
另一个方法是修改 {{ic|default.target}}。可以通过 {{ic|systemctl}} 修改它:<br />
<br />
# systemctl enable multi-user.target<br />
<br />
命令执行情况由 {{ic|systemctl}} 显示:链接 {{ic|/etc/systemd/system/default.target}} 被创建,指向新的默认启动级别。该方法当且仅当目标配置文件中有以下内容时有效:<br />
<br />
[Install]<br />
Alias=default.target<br />
<br />
目前,{{ic|multi-user.target}}、{{ic|graphical.target}} 都包含这段内容。<br />
<br />
== 日志 ==<br />
自版本 38 起,systemd 提供了自己日志系统(logging system),称为 journal. 使用 systemd 日志,无需额外安装日志服务(syslog)。读取日志的命令:<br />
# journalctl<br />
<br />
默认情况下(当 {{ic|Storage&#61;}} 在文件 {{ic|/etc/systemd/journald.conf}} 中被设置为 {{ic|auto}}),日志记录将被写入 {{ic|/var/log/journal/}}。若目录 {{ic|/var/log/journal/}} 不存在(例如,被用户或某个程序删除),systemd将'''不会'''自动创建它,而是将日志记录写入 {{ic|/run/systemd/journal}}。这意味着,系统重启后日志将丢失。<br />
<br />
=== 过滤输出 ===<br />
<br />
{{ic|journalctl}}可以根据特定字段过滤输出,例如:<br />
<br />
显示本次启动后的所有日志:<br />
<br />
# journalctl -b<br />
<br />
不过,一般大家更关心的不是本次启动后的日志,而是上次启动时的(例如,刚刚系统崩溃了)。目前还没有这项功能,正在 [http://comments.gmane.org/gmane.comp.sysutils.systemd.devel/6608 systemd-devel@lists.freedesktop.org] 讨论中。<br />
<br />
目前的折中方案是:<br />
<br />
# journalctl --since=today | tac | sed -n '/-- Reboot --/{n;:r;/-- Reboot --/q;p;n;b r}' | tac<br />
<br />
以上命令输出本日内的所有启动信息。但要注意,如果日志很多,该命令执行时间会比较漫长。<br />
<br />
动态跟踪最新信息:<br />
# journalctl -f<br />
<br />
显示特定程序的所有消息:<br />
{{bc|# journalctl /usr/lib/systemd/systemd}}<br />
<br />
显示特定进程的所有消息:<br />
{{bc|1=# journalctl _PID=1}}<br />
<br />
显示指定单元的所有消息:<br />
# journalctl -u netcfg<br />
<br />
详情参阅{{ic|man journalctl}}、{{ic|man systemd.journal-fields}},以及 Lennert 的这篇[http://0pointer.de/blog/projects/journalctl.html 博文]。<br />
<br />
=== 日志大小限制 ===<br />
<br />
如果按上面的操作保留日志的话,默认日志最大限制为所在文件系统容量的 10%,即:如果 {{ic|/var/log/journal}} 储存在 50GiB 的根分区中,那么日志最多存储 5GiB 数据。可以修改 {{ic|/etc/systemd/journald.conf}} 中的 {{ic|SystemMaxUse}} 来指定该最大限制。如限制日志最大 50MiB:<br />
<br />
SystemMaxUse=50M<br />
<br />
详情参见 {{ic|man journald.conf}}.<br />
<br />
=== 配合 syslog 使用 ===<br />
systemd 提供了 socket {{ic|/run/systemd/journal/syslog}},以兼容传统日志服务。所有系统信息都会被传入。要使传统日志服务工作,需要让服务链接该 socket,而非 {{ic|/dev/log}}([http://lwn.net/Articles/474968/ 官方说明])。Arch 软件仓库中的 {{pkg|syslog-ng}} 已经包含了需要的配置。<br />
<br />
设置开机启动 syslog-ng:<br />
# systemctl enable syslog-ng<br />
<br />
== 优化 ==<br />
<br />
参阅:[[Improve Boot Performance (简体中文)]]。<br />
<br />
=== 启动过程分析 ===<br />
systemd 提供了一个分析启动过程的工具 —— {{ic|systemd-analyze}}。可以用它看看哪些单元拖慢了开机过程,并据此进行优化。安装 {{Pkg|python2-dbus}}、{{Pkg|python2-cairo}} 软件包后该工具才能工作。<br />
<br />
查看开机过程在内核/用户空间消耗的时间:<br />
$ systemd-analyze<br />
{{小贴士|如果在 {{ic|/etc/mkinitcpio.conf}} 的 {{ic|HOOKS}} 数组添加 {{ic|timestamp}},并重新生成启动内存镜像({{ic|mkinitcpio -p linux}}),{{ic|systemd-analyze}} 还可以显示处理该启动镜像花费的时间。}}<br />
<br />
按照耗费时间顺序,输出启动每个单元耗费的时间:<br />
$ systemd-analyze blame<br />
<br />
生成类似于 [[Bootchart (简体中文)|bootchart]] 的开机过程图表:<br />
$ systemd-analyze plot > plot.svg<br />
<br />
==== 使用 systemd-bootchart ====<br />
<br />
自2012年10月17日,bootchart 工具已经合并进 systemd 中,使用方法和原来的 bootchart 大同小异,添加下列内容到内核参数即可:<br />
<br />
initcall_debug printk.time=y init=/usr/lib/systemd/systemd-bootchart<br />
<br />
==== 使用 bootchart2 ====<br />
<br />
由于没有办法在内核参数设置两个 init,所以不能使用源里的 bootchart。不过,[[AUR (简体中文)|AUR]] 软件包 {{AUR|bootchart2}} 提供了一个的 systemd 服务,这样就可以和 systemd 一起使用。安装后启用即可:<br />
{{bc|# systemctl enable bootchart.service}}<br />
详情参阅 [https://github.com/mmeeks/bootchart bootchart 文档]。<br />
<br />
=== 预读 ===<br />
systemd 自己实现了一个 readahead,可以用来提高开机效率。不过,效果会因内核版本和硬件情况而不同(极少数情况下还会变慢)。开启 readahead:<br />
<br />
# systemctl enable systemd-readahead-collect.service systemd-readahead-replay.service<br />
<br />
要知道,readahead 的超级牛力只有在重启几次后才会显现。<br />
<br />
== 疑难解答 ==<br />
<br />
=== 关机/重启十分缓慢 ===<br />
<br />
如果关机特别慢(甚至跟死机了一样),很可能是某个拒不退出的服务在作怪。systemd 会等待一段时间,然后再尝试杀死它。请阅读[http://freedesktop.org/wiki/Software/systemd/Debugging#Shutdown_Completes_Eventually 这篇文章],确认你是否是该问题受害者。<br />
<br />
=== 短时进程无日志记录 ===<br />
<br />
若 {{ic|journalctl -u foounit.service}} 没有显示某个短时进程的任何输出,那么改用 PID 试试。例如,若 systemd-modules-load.service 执行失败,那么先用 {{ic|systemctl status systemd-modules-load}} 查询其 PID(比如是123),然后检索该 PID 相关的日志 {{ic|journalctl -b _PID&#61;123}}。运行时进程的日志元数据(诸如 _SYSTEMD_UNIT 和 _COMM)被乱序收集在 {{ic|/proc}} 目录。要修复该问题,必须修改内核,使其通过套接字连接来提供上述数据,该过程类似于 SCM_CREDENTIALS。<br />
<br />
== 相关资源 ==<br />
<br />
*[http://www.freedesktop.org/wiki/Software/systemd 官方网站]<br />
*[http://0pointer.de/public/systemd-man/ man 手册页]<br />
*[http://freedesktop.org/wiki/Software/systemd/Optimizations systemd 优化]<br />
*[http://www.freedesktop.org/wiki/Software/systemd/FrequentlyAskedQuestions 常见问题 FAQ]<br />
*[http://www.freedesktop.org/wiki/Software/systemd/TipsAndTricks 小技巧]<br />
*[http://0pointer.de/public/systemd-ebook-psankar.pdf systemd for Administrators (PDF)]<br />
*[http://fedoraproject.org/wiki/Systemd Fedora 项目对 systemd 的介绍]<br />
*[http://fedoraproject.org/wiki/How_to_debug_Systemd_problems 如何调试 systemd 故障]<br />
*''The H Open'' 杂志上的[http://www.h-online.com/open/features/Control-Centre-The-systemd-Linux-init-system-1565543.html 两][http://www.h-online.com/open/features/Booting-up-Tools-and-tips-for-systemd-1570630.html 篇]科普文章<br />
*[http://0pointer.de/blog/projects/systemd.html Lennart 的博文]<br />
*[http://0pointer.de/blog/projects/systemd-update.html 更新报告]<br />
*[http://0pointer.de/blog/projects/systemd-update-2.html 更新报告2]<br />
*[http://0pointer.de/blog/projects/systemd-update-3.html 更新报告3]<br />
*[http://0pointer.de/blog/projects/why.html 最新动态]<br />
*[https://fedoraproject.org/wiki/SysVinit_to_Systemd_Cheatsheet/zh Fedora Wiki 上的 SysVinit 和 systemd 命令对比表]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Wine_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=246639
Wine (简体中文)
2013-02-07T02:31:45Z
<p>Cuihao: try wiki-monkey</p>
<hr />
<div>[[Category:Wine (简体中文)]]<br />
[[cs:Wine]]<br />
[[de:Wine]]<br />
[[en:Wine]]<br />
[[es:Wine]]<br />
[[fr:Wine]]<br />
[[it:Wine]]<br />
[[ja:Wine]]<br />
[[ru:Wine]]<br />
[[zh-TW:Wine]]<br />
{{TranslationStatus (简体中文)|Wine|2013-01-30|244275}}<br />
{{Article summary start}}<br />
{{Article summary text|[[Wikipedia:Wine (software)|Wine]] 是类UNIX系统下运行微软Windows程序的"兼容层"。在Wine中运行的Windows程序,就如同运行原生Linux程序一样,不会有模拟器那样的性能问题。}}<br />
{{Article summary heading|Related}}<br />
{{Article summary wiki|Steam}}<br />
{{Article summary wiki|CrossOver}}<br />
{{Article summary end}}<br />
<br />
获取更详细的介绍请浏览[http://www.winehq.org/ 项目官方网站]和[http://wiki.winehq.org/ wiki]页面。<br />
<br />
== 安装 ==<br />
{{警告|如果您的账户能浏览某些文件或资源,Wine运行的程序也可以。Wine不是[[wikipedia:Sandbox_%28computer_security%29 |沙箱]]。如果很重视安全,请考虑使用[[wikipedia:Virtualization|虚拟化]]。}}<br />
<br />
最新版的Wine软件包收录在:i686平台的<nowiki>[</nowiki>[[community]]]仓库,x86_64的<nowiki>[</nowiki>[[multilib]]]仓库。<br />
<br />
# pacman -S wine<br />
<br />
另外,您可能需要安装{{pkg|wine_gecko}}以模拟 Internet Explorer 支持(很多网络程序需要):<br />
# pacman -S wine_gecko<br />
<br />
或使用[http://wiki.winehq.org/Mono Mono] 以使用 .NET 程序。[[AUR]]中的软件包{{AUR|wine-mono}}会将安装程序放到{{ic|/usr/share/wine/mono/}},这样Wine就可以自动检查到,不会再次下载。<br />
<br />
'''平台差异'''<br />
<br />
默认的Wine是32位的程序,也是i686的Arch软件包。所以它不能运行64位的Windows程序(反正是罕见的)。<br />
<br />
然而,x86_64的Wine软件包目前以{{ic| --enable-win64}}方式编译。这个参数激活了[[Wikipedia:WoW64|WoW64]]的Wine版本。<br />
<br />
*在Windows中,这个复杂的子系统允许用户同时使用32位和64位的Windows程序,甚至是在同一目录。<br />
<br />
*在Wine中,用户将必须建立单独分开的目录/前缀。这项Wine功能仍是试验阶段,并建议用户使用一个win32{{ic|WINEPREFIX}}。浏览[http://wiki.winehq.org/Wine64 Wine64]以获取有关这个的详细信息。<br />
<br />
总结一下,配置{{ic|1=WINEARCH=win32}}后,x86_64平台的Arch和i686平台的Arch完全相同。<br />
<br />
{{注意|如果在64位环境中执行{{ic|winetricks}}或其它程序出现问题,请试试创建一个新的32位{{ic|WINEPREFIX}}. 参见下面的[[#使用 WINEARCH]]}}<br />
<br />
== 配置 ==<br />
Wine默认将配置文件和安装的Windows程序保存在{{ic|~/.wine}}。这样的目录称为一个"Wine prefix"或"Wine bottle"(保留原文,下文称“系统目录”)。每次运行Windows程序(包括内置程序,如{{ic|winecfg}})时,系统目录会自动创建(如果缺失)或更新。系统目录中存放有相当于Windows下{{ic | C:\}}C盘(更确切的说应是系统盘)的文件夹。<br />
<br />
通过设置{{ic|WINEPREFIX}}环境变量,可以更改Wine系统目录的位置。如果希望让不同的Windows程序使用不同的系统环境或配置,这一变量会非常有用。<br />
<br />
例如,如果您使用 <br />
{{ic |1= $ env WINEPREFIX=~/.win-a wine-A程序.exe}}参数来运行一个程序。另一个使用<br />
{{ic |1= $ env WINEPREFIX=~/.win-b wine-B程序.exe}}参数,这两个程序将使用独立的C盘和注册表配置。<br />
<br />
以下命令会建立一个默认的系统目录,且不启动任何Windows程序:<br />
$ env WINEPREFIX=~/.customprefix wineboot -u<br />
<br />
配置Wine的方式通常有:<br />
* [http://wiki.winehq.org/winecfg winecfg]是Wine的图形界面配置程序。控制台下调用{{ic|$ winecfg}}(或指定系统目录:{{ic|1=$ WINEPREFIX=~/.系统目录 winecfg}})即可启动<br />
* [http://wiki.winehq.org/control control.exe]是Windows控制面板的Wine实现,通过{{ic|$ wine control}}命令启动<br />
* [http://wiki.winehq.org/regedit regedit]是Wine的注册表编辑器,比较前两者,该工具能配置更多东西。部分常用键值参见:[http://wiki.winehq.org/UsefulRegistryKeys WineHQ's article on Useful Registry Keys]<br />
<br />
=== 使用 WINEARCH ===<br />
对于64位用户,如果使用[multilib]仓库里的Wine,默认创建的系统目录是64位环境的。若想使用纯32位环境,修改{{ic|WINEARCH}} 变量win32为即可:<br />
{{ic |1=$ WINEARCH=win32 winecfg}}这样就会生成32位Wine环境。若不设置{{ic|WINEARCH}}得到的就是64位环境。<br />
<br />
通过{{ic|WINEPREFIX}}变量,在不同的系统目录分别创建32位和64位环境:<br />
$ WINEARCH=win32 WINEPREFIX=~/win32 winecfg <br />
$ WINEPREFIX=~/win64 winecfg<br />
<br />
{{注意|系统目录创建过程中,64位版本的wine将视全部目录如同64位系统目录,也将不会在已存在的目录中创建任何32位的.创建32位系统目录,您必须让Wine创建指定的{{ic|WINEPREFIX}}目录。}}<br />
<br />
winetricks也接受{{ic|WINEPREFIX}}变量,以安装Steam为例:<br />
env WINEARCH=win32 WINEPREFIX=~/.local/share/wineprefixes/steam winetricks steam<br />
{{注意|不必手动在{{ic|wineprefixes}}文件夹下建立steam文件夹,Wine会自动创建不存在的系统目录.}}<br />
<br />
=== 显卡驱动 ===<br />
使用Wine运行Windows游戏时,可能需要高性能的显卡驱动。[[Nvidia (简体中文)|Nvidia]]、[[ATI (简体中文)|Amd/ATI]]用户最好使用闭源驱动。[[Intel (简体中文)|Intel]]显卡用户也可以选择开源驱动,它已经非常成熟。<br />
<br />
要是显卡驱动有问题或者相关配置有误,控制台用Wine运行某些程序时会输出:<br />
Direct rendering is disabled, most likely your OpenGL drivers have not been installed correctly<br />
<br />
x86-64用户需要从[multilib]或[[AUR (简体中文)|AUR]]安装额外的32位库:<br />
* '''NVIDIA''':{{ic|# pacman -S lib32-nvidia-utils}} 关于旧版本lib32-nvidia-utils(例如nvidia-96xx驱动),请到[https://aur.archlinux.org/packages.php?K=lib32-nvidia-utils 这里]查找。<br />
<br />
* '''NVIDIA (使用nouveau-dri驱动)''': {{ic|# pacman -S lib32-nouveau-dri}} <br />
<br />
* '''Intel''':{{ic|# pacman -S lib32-intel-dri}} 运行Wine时需要手动添加{{ic|1=LIBGL_DRIVERS_PATH=/usr/lib32/xorg/modules/dri}}<br />
<br />
* '''AMD/ATI''':{{ic|# pacman -S lib32-ati-dri}} 如果用的是闭源驱动:{{ic|# pacman -S {{Pkg|lib32-catalyst-utils}}}}。<br />
<br />
{{注意|安装上述软件包后,可能需要重启X才能生效!}}<br />
<br />
=== 声音 ===<br />
Wine程序有可能遇到某些声音问题。首先,确保{{ic|winecfg}}中只启用了一种声卡驱动。目前,Wine对[[Alsa (简体中文)|Alsa]]的支持最好。<br />
<br />
x86_64平台下使用[[Alsa (简体中文)|Alsa]]的话,需要安装{{Pkg|lib32-alsa-lib}}。如果还要使用PulseAudio,则需安装{{Pkg|lib32-libpulse}}。<br />
<br />
若使用[[OSS (简体中文)|OSS]],需要安装{{Pkg|lib32-alsa-oss}}。仅靠内核驱动是不行的。<br />
<br />
安装上述软件包后,若{{ic|winecfg}}'''仍'''无法识别声卡(Selected driver: (none)),请尝试[http://wiki.jswindle.com/index.php/Wine_Registry#Configuring_Sound registry 通过注册表配置]。<br />
<br />
运行使用某些高级声音系统的游戏,可能还需要安装{{Pkg|lib32-openal}}。<br />
<br />
=== 其他函数库 ===<br />
<br />
某些程序(如 Office 2003)需要解析HTML、XML(使用MSXML库),需要安装{{Pkg|lib32-libxml2}}。<br />
<br />
播放音频的程序可能依赖{{Pkg|lib32-mpg123}}。<br />
<br />
对于使用图像处理库的程序,可能依赖{{Pkg|lib32-giflib}}和{{Pkg|lib32-libpng}},<br />
<br />
x86_64的加密支持需要{{Pkg|lib32-gnutls}}软件包。<br />
<br />
=== 字体 ===<br />
<br />
如果没有安装微软Truetype字体,Wine程序的字体显示可能会一团糟,参见[[MS Fonts (简体中文)]]。如果还是不行,试试{{ic|winetricks allfonts}}。<br />
<br />
上述操作后,杀死wine相关进程再运行{{ic|winecfg}},字体应该变好看了。<br />
<br />
如果字体看起来很毛糙,试试用[http://wiki.winehq.org/regedit regedit]导入下列文本文件:<br />
[HKEY_CURRENT_USER\Software\Wine\X11 Driver]<br />
"ClientSideWithRender"="N"<br />
<br />
=== 启动器和菜单 ===<br />
<br />
Wine不会为内置程序(如{{ic|winecfg}}、{{ic|winebrowser}})创建桌面启动器和菜单项。但手动安装的Windows程序通常会自动创建启动器和菜单项。在Windows下,安装程序(如{{ic|setup.exe}})通常会在桌面和开始菜单建立快捷方式,而Wine下会创建遵循freedesktop.org规范的.desktop文件(即启动器,相当于快捷方式)。<br />
<br />
{{小贴士|如果启动器''没有''自动创建,或者这些文件丢失了,可以尝试使用[http://wiki.winehq.org/winemenubuilder winemenubuilder]修复。}}<br />
<br />
Ubuntu下,Wine项目以子菜单形式出现在系统菜单。以下步骤将实现这个效果:<br />
<br />
==== 创建菜单项 ====<br />
首先,用Wine安装一个Windows程序,以建立基本的菜单。完成后,向其中添加菜单项。桌面右键选择{{ic|"创建启动器..."}}(不同桌面环境操作有所差异),设置如下:<br />
'''类型(Type)''': 应用程序(Application)<br />
'''名称(Name)''': 配置<br />
'''命令(Command)''': winecfg<br />
'''备注(Comment)''': Wine配置工具<br />
<br />
'''类型''': 应用程序<br />
'''名称''': 卸载程序<br />
'''命令''': wine uninstaller<br />
'''备注''': 卸载Wine下的Windows程序<br />
<br />
'''类型''': 应用程序<br />
'''名称''': 浏览 C:\<br />
'''命令''': wine winebrowser c:\\<br />
'''备注''': 浏览Wine中虚拟的C盘<br />
现在,桌面上出现了三个启动器,下面将把它们移入菜单。不过首先,我们给这些启动器加上动态图标(由图标主题提供)。方法是,用文本编辑器打开启动器,编辑Icon项目:<br />
<br />
{{ic|配置}} 启动器:<br />
Icon=wine-winecfg<br />
{{ic|卸载程序}} 启动器:<br />
Icon=wine-uninstaller<br />
{{ic|浏览 C:\}} 启动器:<br />
Icon=wine-winefile<br />
{{小贴士|多数桌面环境在上述“创建启动器”步骤即可设置图标。以第一个启动器为例,在选择图标窗口中搜索wine-winecfg,选择即可,无需手动编辑。 ——译者注}}<br />
如果图标无法显示或者你觉得很丑陋,换成其他图标也可以。右键设置启动器,应该有更改图标的地方。很多图标主题,例如[http://www.gnome-look.org/content/show.php/GNOME-colors?content=82562 GNOME-colors],都提供这些图标。<br />
<br />
现在,将启动器移入菜单。把启动器复制到 {{ic|~/.local/share/applications/wine/}} 目录即可。<br />
<br />
诶?图标还没出现在菜单中!还剩下最后一步,创建下列文本文件:<br />
{{hc|~/.config/menus/applications-merged/wine-utilities.menu|<nowiki><br />
<!DOCTYPE Menu PUBLIC "-//freedesktop//DTD Menu 1.0//EN"<br />
"http://www.freedesktop.org/standards/menu-spec/menu-1.0.dtd"><br />
<Menu><br />
<Name>Applications</Name><br />
<Menu><br />
<Name>wine-wine</Name><br />
<Directory>wine-wine.directory</Directory><br />
<Include><br />
<Filename>wine-Configuration.desktop</Filename><br />
</Include><br />
<Include><br />
<Filename>wine-Browse C:\.desktop</Filename><br />
</Include><br />
<Include><br />
<Filename>wine-Uninstall Programs.desktop</Filename><br />
</Include><br />
</Menu><br />
</Menu><br />
</nowiki>}}<br />
再看看菜单,应该万事大吉了。<br />
<br />
==== Gnome3 中清理 Wine 菜单启动项 ====<br />
系统全局的菜单启动器安装在 {{ic|/usr/share/applications/}},清除相应程序的“.desktop”文件即可从整个系统删除该启动器。<br />
<br />
如果这样还是无法解决问题,那么很可能 Wine 的启动器存放在用户级别的 {{ic|~/.local/share/applications/wine/Programs/}} 目录中。删除相应的“.desktop”文件即可清理对应启动项。删除整个 Programs 文件夹将清理所有 Wine 程序的启动项。<br />
<br />
==== 修复 KDE 4 菜单问题[https://bugs.launchpad.net/ubuntu/+source/wine/+bug/263041] ====<br />
Wine菜单项有可能错误地出现在{{ic|"Lost & Found(其他)"}} ,而非Wine子菜单。原因是kde-applications.menu文件缺失{{ic|MergeDir}}配置。<br />
<br />
编辑{{ic|/etc/xdg/menus/kde-applications.menu}}。<br />
<br />
在文件末尾处,{{ic|<DefaultMergeDirs/>}}后添加{{ic|<MergeDir>applications-merged</MergeDir>}}。修改后内容大致如下:<br />
<Menu><br />
<Include><br />
<And><br />
<Category>KDE</Category><br />
<Category>Core</Category><br />
</And><br />
</Include><br />
<DefaultMergeDirs/><br />
'''<MergeDir>applications-merged</MergeDir>'''<br />
<MergeFile>applications-kmenuedit.menu</MergeFile><br />
</Menu><br />
<br />
另一个方法是:<br />
ln -s ~/.config/menus/applications-merged ~/.config/menus/kde-applications-merged<br />
<br />
这样的好处是,不会因为KDE升级而重置配置。但该方法只对一个用户有效。<br />
<br />
== 运行 Windows 程序 ==<br />
{{警告|千万不要以root身份运行Wine!详情参见[http://wiki.winehq.org/FAQ#run_as_root 本文]。}}<br />
运行Windows程序:<br />
$ wine <exe文件><br />
<br />
内置的msiexec程序可以运行MSI安装包:<br />
$ msiexec installername.msi<br />
== 技巧 ==<br />
{{小贴士|此外您可能会感兴趣以下文章的开始所提供的链接<br />
* [http://appdb.winehq.org/ Wine程序数据库 (Wine Application Database, AppDB)] —— 特定Windows程序的Wine兼容情况(运行时的已知问题、用户评分、指南等等)<br />
* [http://forum.winehq.org/ WineHQ论坛] —— 要是看完上述网页还有问题,可以到这里咨询<br />
}}<br />
<br />
这里介绍一些安装Windows组件的工具。由于这些工具可能严重破坏Wine配置,没有需要时最好不要使用。<br />
<br />
=== 安装 Microsoft Office ===<br />
<br />
安装Office套装前,需要先安装某些Windows组件:<br />
<br />
$ WINEARCH=win32 WINEPREFIX=/path/to/wineprefix winecfg<br />
# pacman -S winetricks<br />
$ winetricks msxml3 # For MS Office 2007<br />
$ winetricks msxml3 msxml6 # For MS Office 2010<br />
$ wine /path/to/office_cd/setup.exe<br />
<br />
更多信息,参见[http://appdb.winehq.org/appview.php?iVersionId=4992 WineHQ上的文章]。<br />
<br />
=== OpenGL 模式 ===<br />
很多游戏(比如魔兽争霸啦)都支持OpenGL模式,在Wine下''可能''比默认DirectX模式性能更好。一般添加{{Ic|-opengl}}启动程序即可,但''不同程序可能有所不同'':<br />
$ wine /path/to/3d_game.exe -opengl<br />
<br />
请参考[http://appdb.winehq.org AppDB],了解特定程序的相关信息。<br />
<br />
=== 将 Wine 作为 Win16/Win32 程序的解释器 ===<br />
可以通知内核把Wine作为Win16/Win32程序的解释器。<br />
<br />
设置过程在 [[systemd (简体中文)|systemd]] 和 [[initscripts (简体中文)|initscripts]] 下有所差异。<br />
<br />
==== systemd ====<br />
通知内核识别和执行 Win16/Win32 程序的方式:<br />
echo ':DOSWin:M::MZ::/usr/bin/wine:' > /proc/sys/fs/binfmt_misc/register<br />
To make the setting permanent, create a configuration file in {{ic|/etc/tmpfiles.d}} with the following contents:<br />
测试效果,若一切正常,可以使该设置永久生效。在 {{ic|/etc/tmpfiles.d}} 目录创建新的配置文件,内容为:<br />
{{hc|/etc/tmpfiles.d/enable-doswin-exe.conf|<br />
w /proc/sys/fs/binfmt_misc/register - - - - :DOSWin:M::MZ::/usr/bin/wine:}}<br />
<br />
说明一下,和 initscripts 不同,systemd 会自动挂载 {{ic|/proc/sys/fs/binfmt_misc}},所以只需要通过临时文件机制向内核写入配置即可。<br />
<br />
更多信息,参见 [[Systemd (简体中文)#临时文件]]。<br />
<br />
==== initscripts ====<br />
首先,挂载 {{ic|binfmt_misc}}文件系统:<br />
# mount -t binfmt_misc none /proc/sys/fs/binfmt_misc<br />
或者,如果你想一直这么做,将以下内容加入{{ic|/etc/fstab}}:<br />
none /proc/sys/fs/binfmt_misc binfmt_misc defaults 0 0<br />
然后,告知内核如何解释(运行)Win16/Win32程序:<br />
echo ':DOSWin:M::MZ::/usr/bin/wine:' > /proc/sys/fs/binfmt_misc/register<br />
如果你想一直这么做,添加上述内容到{{ic|/etc/rc.local}}即可。最后的重定向是为了避免切换启动级别时的错误信息:<br />
{ echo ':DOSWin:M::MZ::/usr/bin/wine:' > /proc/sys/fs/binfmt_misc/register; } 2>/dev/null<br />
<br />
==== 效果测试 ====<br />
现在,直接运行Windows程序试试:<br />
chmod 755 exefile.exe<br />
./exefile.exe<br />
<br />
=== Wine 控制台 ===<br />
<br />
有些时候,可能需要运行{{ic|.exe}}给游戏打补丁,比如给古董游戏添加宽屏支持。这时直接通过Wine运行可能没有用。那么,打开终端,运行一下命令:<br />
<br />
$ wineconsole cmd<br />
<br />
将进入一个和Windows下cmd一样的命令行环境。在该环境下试试也许就可以了。<br />
<br />
=== Winetricks ===<br />
使用[http://wiki.winehq.org/winetricks Winetricks]快速脚本,能够方便地安装许多Windows组件,包括DirectX、msxml(被Office 2007、IE浏览器依赖)visual运行库还有其他更多的。<br />
<br />
您可以使用[[pacman (简体中文)|pacman]]或者从[[AUR (简体中文)|AUR]]上获取{{AUR|winetricks-svn}}软件包来安装该工具:<br />
<br />
运行:<br />
<br />
$ winetricks<br />
<br />
== 第三方工具 ==<br />
这些程序有其自己的主页和支持论坛。<br />
<br />
=== CrossOver ===<br />
<br />
[http://www.codeweavers.com/about/ CrossOver] 有单独的[[CrossOver|wiki 页面]].<br />
<br />
=== PlayOnLinux ===<br />
[http://www.playonlinux.com/ PlayOnLinux]是一个图形界面的Windows/DOS程序管理器。它提供了一些帮助配置/运行程序的脚本,能够管理多个不同版本的Wine,甚至能对不同程序使用不同Wine版本。参考[http://appdb.winehq.org AppDB],看看哪个Wine版本对你要运行的程序兼容最好。从[[AUR (简体中文)|AUR]]安装{{AUR|playonlinux}}。<br />
<br />
=== PyWinery ===<br />
[http://code.google.com/p/pywinery/ PyWinery]是一个简单的、图形界面的Wine系统目录管理器,用它可以方便地管理不同系统目录,并从不同系统目录运行程序。同时可以开启winetricks在同一系统目录,打开系统目录所在文件夹, {{ic|winecfg}}, 软件卸载程序和wineDOS。[[AUR (简体中文)|AUR]]中提供了软件包{{AUR|pywinery}}。当你使用很多系统目录(一个打游戏用、一个编程用……)时,这个程序会非常有用。<br />
<br />
它在默认情况下使用winetricks打开{{ic|.exe}}文件,所以你可以选择你有的任何Wine的配置。<br />
<br />
=== Q4wine ===<br />
[http://q4wine.brezblock.org.ua/ Q4Wine] 是一个图形界面的系统目录(wine-prefix)管理器。它的特色是可以把 QT 主题导入 Wine 配置,使两者完美整合。{{Pkg|q4wine}} 软件包在 <nowiki>[</nowiki>[[multilib]]] 仓库中提供。<br />
<br />
== 相关链接 ==<br />
* [http://www.winehq.com/ Wine官方网站]<br />
* [http://appdb.winehq.org/ Wine程序数据库]<br />
* [http://linuxgamingtoday.wordpress.com/2008/02/16/quick-tips-to-speed-up-your-gaming-in-wine/ 加速Wine,显卡及OpenGL高级配置]<br />
* [http://wiki.gotux.net/code:perl:fileinfo FileInfo] —— Find Win32 PE/COFF headers in EXE/DLL/OCX files under linux/unix environment.</div>
Cuihao
https://wiki.archlinux.org/index.php?title=User:Cuihao&diff=246638
User:Cuihao
2013-02-07T02:11:28Z
<p>Cuihao: </p>
<hr />
<div>{{Article summary start}}<br />
{{Article summary text|这是'''cuihao'''的个人页面,受到他本人保护,不要企图破坏这个页面。}}<br />
{{Article summary end}}<br />
<br />
你好,我是cuihao,[http://cuihao.tk/ 我的Blog]。<br />
<br />
{{User:Cuihao/SandBox|SiS}}<br />
<br />
{{User:Cuihao/SandBox|SiS|矽统}}<br />
<br />
{{User:Cuihao/SandBox|Intel}}<br />
<br />
== 子页面 ==<br />
[[User:Cuihao/CategorieNaming]]<br><br />
[[User:Cuihao/Record]]<br><br />
[[User:Cuihao/SandBox]]<br />
<br />
== To 我朝子民 ==<br />
{{注意|如果发现我翻译到一半的文章,只要没打translateme,不要担心烂尾,我会尽快搞定的。}}<br />
{{注意|汝若见鄙人以floodfill之势多次编辑某页,那是在翻译…… 嗯,多保存少意外。}}<br />
欢迎指正翻译不足之处。<br />
<br />
== To 老外 ==<br />
{{Box RED|To 老外:|This article kontains Chinese characters. Do not attempt to read it, even using [http://translate.google.cn/ Google Translate].}} <br />
{{Box BLUE|To 老外:|This article kontains Chinese characters. Do not attempt to read it, even using [http://translate.google.cn/ Google Translate].}} <br />
{{Box GREEN|To 老外:|This article kontains Chinese characters. Do not attempt to read it, even using [http://translate.google.cn/ Google Translate].}} <br />
<br />
== me's who ==<br />
=== Section A ===<br />
{{Message_box<br />
|id = translateme<br />
|signal = [[Image:Tango-document-new.png]]<br />
|heading = 我是 CUI Hao。<br />
|message = Cui是姓,Hao是名,cuihao是网名。}}<br />
<br />
=== Section B ===<br />
{{Message_box<br />
|id = translateme<br />
|signal = [[Image:Tango-view-fullscreen.png]]<br />
|heading = 我来自中国。<br />
|message = 中华人民共和国河南省郑州市。}}<br />
<br />
=== Section C ===<br />
{{Message_box<br />
|id = translateme<br />
|signal = [[Image:Tango-preferences-desktop-locale.png]]<br />
|heading = 我是一名高中生。<br />
|message = 河南省实验中学高三年级学生。我已经放羊了 \o/}}<br />
<br />
=== Section D ===<br />
{{Message_box<br />
|id = out of date<br />
|signal = [[Image:Tango-dialog-warning.png]]<br />
|heading = 我是一名OIer。<br />
|message = 学过Pascal、C/C++、Python语言。我并不是纯OIer,不热衷OI,因为程序员不是我的梦想。}}<br />
<br />
=== Section E ===<br />
{{Message_box<br />
|id = deletion<br />
|signal = [[Image:Tango-user-trash-full.png]]<br />
|heading = 我在这里业余翻译文章。<br />
|message = 希望借翻译提高我的英语水平、汉语水平、计算机水平,并帮助更多的中国Archer、Linuxer。}}</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Systemd_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=246372
Systemd (简体中文)
2013-02-05T02:56:59Z
<p>Cuihao: </p>
<hr />
<div>{{Lowercase title}}<br />
[[Category:Daemons and system services (简体中文)]]<br />
[[Category:Boot process (简体中文)]]<br />
[[en:Systemd]]<br />
[[es:Systemd]]<br />
[[fr:Systemd]]<br />
[[it:Systemd]]<br />
[[ja:Systemd]]<br />
[[ru:Systemd]]<br />
[[zh-TW:Systemd]]<br />
{{Article summary start}}<br />
{{Article summary text|本文介绍 Systemd 的安装和配置。}}<br />
{{Article summary heading|相关页面}}<br />
{{Article summary wiki|Systemd/Services}}<br />
{{Article summary wiki|systemd/User}}<br />
{{Article summary wiki|Systemd FAQ}}<br />
{{Article summary wiki|init Rosetta}}<br />
{{Article summary wiki|udev (简体中文)}}<br />
{{Article summary end}}<br />
{{TranslationStatus (简体中文)|Systemd|2013-02-05|246247}}<br />
摘自[http://freedesktop.org/wiki/Software/systemd 项目主页]:<br />
<br />
'''''systemd''' 是 Linux 下的一款系统和服务管理器,兼容 SysV 和 LSB 的启动脚本。systemd 的特性有:支持并行化任务;同时采用 socket 式与 [[D-Bus (简体中文)|D-Bus]] 总线式激活服务;按需启动守护进程(daemon);利用 Linux 的 [[cgroups|cgroups]] 监视进程;支持快照和系统恢复;维护挂载点和自动挂载点;各服务间基于依赖关系进行精密控制。systemd 完全可以替代 Arch 曾经使用的 [[SysVinit (简体中文)|sysvinit]] 启动系统。''<br />
<br />
{{注意|[https://bbs.archlinux.org/viewtopic.php?pid&#61;1149530#p1149530 这个帖子]详细解释了 Arch 向 systemd 迁移的原因。}}<br />
<br />
另见[[Wikipedia:Systemd|维基百科上的介绍]]。<br />
<br />
== 迁移前需考虑 ==<br />
<br />
* 阅读[http://freedesktop.org/wiki/Software/systemd/ 该站],了解 systemd。<br />
* systemd 自己有一套日志('''journal''')系统,用于代替 '''syslog'''。两者也可以共存,参见后面的[[#日志|日志部分]]。<br />
* 虽然 systemd 可以替换 '''cron'''、'''acpid'''、'''xinetd''' 等的部分功能。至少目前还可以继续使用这些服务,无需立即切换。<br />
* 交互式 initscripts 启动脚本在 systemd 中无法工作。例如 '''netcfg-menu''' [https://bugs.archlinux.org/task/31377 无法]在启动时显示。<br />
<br />
== 安装 ==<br />
{{注意|从[https://www.archlinux.org/news/systemd-is-now-the-default-on-new-installations/ 2012-10-13版安装介质]开始,安装程序已经默认安装{{pkg|systemd}} 和 {{pkg|systemd-sysvcompat}}。}}<br />
{{注意|如果是在 VPS 中使用 Arch,请先阅读:[[Virtual_Private_Server#Moving_your_VPS_from_initscripts_to_systemd]]。}}<br />
<br />
本部分帮助还在使用{{pkg|sysvinit}} 和 {{pkg|initscripts}} 的用户迁移到{{pkg|systemd}}.<br />
<br />
# 从[[Official_Repositories_(简体中文)|官方软件仓库]]安装 {{pkg|systemd}} 并添加[[Kernel parameters|内核参数]]{{Ic|1=init=/usr/lib/systemd/systemd}}<br />
# 用 {{ic|systemctl enable ''daemonname''}} [[#使用单元|启用]] {{ic|/etc/rc.conf}} 中的守护进程。从 {{ic|/etc/rc.conf}} 到 systemd 服务的转换可以参考:[[Daemons_List_(简体中文)|守护进程列表]] 和 [[Systemd/Services|Services]]。<br />
# 重启系统,执行命令 {{ic|cat /proc/1/comm}},如果返回{{ic|systemd}},表示 systemd 已经正常启动。<br />
# 确认主机名已经正确设置:{{ic|hostnamectl set-hostname myhostname}}。<br />
# 删除 {{pkg|initscripts}} 和 {{pkg|sysvinit}},并安装{{pkg|systemd-sysvcompat}}.<br />
# 可选,删除{{ic|1=init=/usr/lib/systemd/systemd}}内核参数,现在已经不需要它了,{{pkg|systemd-sysvcompat}}提供了默认的 init.<br />
<br />
=== 附加信息 ===<br />
* 如果内核参数中有 {{ic|quiet}},可以先删除它以便调试。<br />
* 使用 Systemd 的时候无需将用户加入{{ic|optical}}, {{ic|audio}}, {{ic|scanner}} 等[[Users and Groups (简体中文)|用户组]]。加入这些组反而会有问题,例如audio组会导致程序阻塞软件混声。每个 PAM 登录都拥有一个 logind 会话,它通过[[Wikipedia:Access control list|POSIX ACLs]],赋予本地会话以声音设备访问权限、通过[[udisks]]挂载和卸载移动设备的权限等。<br />
{{注意|对于使用 {{ic|startx -- vt7}} 进入X环境的用户,由于 logind 会话无法应用于X环境,用户'''必须'''加入这些组。}}<br />
* 如果卸载 {{Pkg|initscripts}},{{ic|rc.conf}} 将不再有效。如果曾在其中设置了静态网络,或还未迁移到systemd的系统服务,请根据[[#Initscripts 模拟|该节]]配置。<br />
* 如果 fstab 中有 LVM 设备,首次启动时会因为等待超时进入急救控制台。输入管理员密码,执行 {{ic|systemctl enable lvm}} 启用相关服务,重启即可。<br />
<br />
== 原生 systemd 配置文件 ==<br />
{{注意|可能需要手动创建某些文件。所有文件的权限都是644,属主 root,属组 root。}}<br />
<br />
=== 主机名 ===<br />
主机名通过文件 {{ic|/etc/hostname}} 进行配置。该文件不应包含系统的域名(如果有)。使用如下命令设置主机名:<br />
# hostnamectl set-hostname '''myhostname'''<br />
<br />
详情参见 {{ic|man 5 hostname}} 和 {{ic|man 1 hostnamectl}}。<br />
<br />
文件示例:<br />
{{hc|/etc/hostname|<br />
myhostname<br />
}}<br />
<br />
=== 本地化设置(locale) ===<br />
系统默认 locale 记录在 {{ic|/etc/locale.conf}} 文件中。通过如下命令配置:<br />
# localectl set-locale LANG="zh_CN.utf8"<br />
<br />
{{注意|设置默认locale前,请确保已经生成该locale。方法是:在 {{ic|/etc/locale.gen}} 中启用对应locale,然后以root权限执行 {{ic|locale-gen}}。}}<br />
<br />
详情参见 {{ic|man 1 localectl}} 和 {{ic|man 5 locale.conf}}。<br />
<br />
* 另参阅 [[Locale (简体中文)]]。<br />
<br />
文件示例:<br />
{{hc|/etc/locale.conf|<nowiki><br />
LANG=en_US.utf8<br />
</nowiki>}}<br />
<br />
=== 虚拟控制台 ===<br />
{{ic|/etc/vconsole.conf}} 文件用来配置虚拟控制台,包括键盘布局和控制台字体:<br />
{{hc|/etc/vconsole.conf|<nowiki><br />
KEYMAP=us<br />
FONT=lat9w-16<br />
FONT_MAP=8859-1_to_uni</nowiki>}}<br />
<br />
{{注意|{{ic|{{Pkg|systemd}}-194}} 默认使用内核字体和键盘布局,因此以后不必在上述文件中留下空的 {{ic|1=KEYMAP=}} 和 {{ic|1=FONT=}} 设置。}}<br />
<br />
还可以通过命令设置键盘布局:<br />
# localectl set-keymap de<br />
<br />
{{ic|localectl}} 也可以设置X11的键盘布局:<br />
<br />
# localectl set-x11-keymap de<br />
<br />
详情参见 {{ic|man 1 localectl}} 和 {{ic|man 5 vconsole.conf}}。<br />
<br />
* 另参阅 [[Fonts (简体中文)#控制台字体|控制台字体]] 和 [[KEYMAP (简体中文)#键盘布局|键盘布局]]。<br />
<br />
=== 时区 ===<br />
时区可通过创建适当的{{ic|/etc/localtime}}符号链接来进行配置,该链接需指向目录{{ic|/usr/share/zoneinfo/}}下的一个zoneinfo文件。也可执行以下命令直接完成这一操作:<br />
<br />
# timedatectl set-timezone Asia/Shanghai<br />
<br />
更多细节参见{{ic|man 1 timedatectl}},{{ic|man 5 localtime}}和{{ic|man 7 archlinux}}。<br />
<br />
手动创建链接也行:<br />
# ln -sf ../usr/share/zoneinfo/Asia/Shanghai /etc/localtime<br />
<br />
旧的配置文件 {{ic|/etc/timezone}} 已经没用了,可以安全删除。<br />
<br />
=== 硬件时钟 ===<br />
<br />
systemd 默认识别硬件时钟为协调世界时(UTC)。<br />
<br />
{{小贴士|推荐使用[[NTP (简体中文)|NTP服务]]来在线同步硬件时钟。}}<br />
<br />
==== 硬件时钟设定为地方时 ====<br />
<br />
将硬件时钟配置为地方时('''不建议'''):<br />
<br />
# timedatectl set-local-rtc true<br />
<br />
重新调整为 UTC:<br />
<br />
# timedatectl set-local-rtc false<br />
<br />
如果设置成本地时间,处理夏令时有些麻烦。如果夏令时调整发生在关机时,下次启动时时间会出现问题([http://www.cl.cam.ac.uk/~mgk25/mswish/ut-rtc.html 更多信息])。最新的内核直接从实时时钟芯片(RTC)读取时间,不使用 {{ic|hwclock}},内核把从 RTC 读取的时间当作 UTC 处理。所以如果硬件时间是地方时,系统启动一开始识别的时间是错误的,之后很快会进行矫正。这可能导致一些问题(尤其是时间倒退时)。<br />
<br />
如果同时安装了 Windows 操作系统([http://blogs.msdn.com/b/oldnewthing/archive/2004/09/02/224672.aspx 默认使用地方时]),那么一般 RTC 会被设置为地方时。Windows 其实也能处理 UTC,需要[[Time (简体中文)#Windows 系统使用 UTC|修改注册表]]。建议让 Windows 使用 UTC,而非让 Linux 使用地方时。Windows 使用 UTC 后,请记得禁用 Windows 的时间同步功能,以防 Windows 错误设置硬件时间。如上文所说,Linux 可以使用[[NTP (简体中文)|NTP服务]]来在线同步硬件时钟。<br />
<br />
* 详情参阅 [[Time (简体中文)]]。<br />
<br />
=== 内核模块 ===<br />
目前,所有必要模块的加载均由 [[udev]] 自动完成。所以,如果不需要使用任何额外的模块,就没有必要在任何配置文件中添加启动时加载的模块。但是,有些情况下可能需要在系统启动时加载某个额外的模块,或者将某个模块列入黑名单以便使系统正常运行。<br />
<br />
==== 开机加载 ====<br />
systemd 读取 {{ic|/etc/modules-load.d/}} 中的配置加载额外的内核模块。配置文件名称通常为 {{ic|/etc/modules-load.d/<program>.conf}}。格式很简单,一行一个要读取的模块名,而空行以及第一个非空格字符为{{ic|#}}或{{ic|;}}的行会被忽略,如:<br />
{{hc|/etc/modules-load.d/virtio-net.conf|<nowiki><br />
# Load virtio-net.ko at boot<br />
virtio-net</nowiki>}}<br />
<br />
另见{{ic|man 5 modules-load.d}}。<br />
<br />
==== 禁用内核模块 ====<br />
禁用内核模块的方法和 Arch 默认的 {{Pkg|initscripts}} 相同,因为该过程实际由 {{Pkg|kmod}} 处理。参见:[[Kernel modules (简体中文)#黑名单]]。<br />
<br />
=== 文件系统挂载 ===<br />
<br />
默认行为是:在启动一个需要挂载特定分区的服务之前,系统自动检查并挂载分区。{{ic|/etc/fstab}} 中设定的网络文件系统(如 [[NFS (简体中文)|NFS]]、[[Samba (简体中文)|Samba]])无需配置即可正常工作,systemd 将确保网络文件系统在网络链接就绪后挂载。<br />
<br />
详情参阅:{{ic|man 5 systemd.mount}}。<br />
<br />
==== 自动挂载 ====<br />
<br />
* 如果 {{ic|/home}} 分区较大,可以让不依赖 {{ic|/home}} 分区的服务先启动。把下面的参数添加到 {{ic|/etc/fstab}} 文件中 {{ic|/home}} 项目的参数部分即可:<br />
<br />
noauto,x-systemd.automount<br />
<br />
这样 {{ic|/home}} 分区只有需要访问时才会被挂载。内核会缓存所有的文件操作,直到 {{ic|/home}} 分区准备完成。<br />
<br />
{{注意|这样做会使 {{ic|/home}} 的文件系统类型被识别为 {{ic|autofs}},造成 [[mlocate]] 查询时忽略该目录。实际加速效果因配置而异,所以请自己权衡是否需要。}}<br />
<br />
* 挂载远程文件系统也是同理。如果你仅想在需要的时候才挂载,也可以添加 {{ic|noauto,x-systemd.automount}} 参数。另外,可以设置 {{ic|1=x-systemd.device-timeout=#}} 参数,设置超时时间,以防止网络资源不能访问的时候浪费时间。<br />
<br />
* 如果你的加密文件系统需要密钥,则需要添加 {{ic|noauto}} 参数到 {{ic|/etc/crypttab}} 文件中的对应位置。systemd 开机的时候就不会打开这个加密设备,会一直等待到设备被访问时再使用密钥文件挂载。比如在使用加密RAID设备的时候可以节省一定的时间,因为 systemd 不必等到设备可用后才能访问。例如:<br />
<br />
{{hc|/etc/crypttab|<br />
data /dev/md0 /root/key noauto}}<br />
<br />
==== LVM ====<br />
<br />
如果装有不通过 [[Mkinitcpio (简体中文)|initramfs]] 激活的[[LVM (简体中文)|LVM]]卷,则需启动 {{ic|lvm-monitoring}} 服务(由 {{pkg|lvm2}} 软件包提供):<br />
<br />
# systemctl enable lvm-monitoring<br />
<br />
类似地,如果LVM卷在加密设备上,并且启动后才激活,则需启动 {{ic|lvm-on-crypt}} 服务(同样由 {{pkg|lvm2}} 软件包提供):<br />
<br />
# systemctl enable lvm-on-crypt<br />
<br />
=== ACPI 电源管理 ===<br />
systemd 能够处理某些电源相关的 [[Wikipedia:Advanced_Configuration_and_Power_Interface|ACPI]] 事件,通过 {{ic|/etc/systemd/logind.conf}} 的下列选项配置:<br />
* {{ic|HandlePowerKey}}:按下电源键后的动作<br />
* {{ic|HandleSleepKey}}:按下挂起键后的动作<br />
* {{ic|HandleHibernateKey}}: 按下休眠键后的动作<br />
* {{ic|HandleLidSwitch}}:合上笔记本盖后待机<br />
<br />
动作可以是 {{ic|ignore}}, {{ic|poweroff}}, {{ic|reboot}}, {{ic|halt}}, {{ic|suspend}}, {{ic|hibernate}} 或 {{ic|kexec}}.<br />
<br />
系统默认设置为:<br />
HandlePowerKey=poweroff<br />
HandleSuspendKey=suspend<br />
HandleHibernateKey=hibernate<br />
HandleLidSwitch=suspend<br />
<br />
如果不用图形界面,或者使用 [[i3 (简体中文)|i3]]、[[awesome (简体中文)|awesome]] 这样简单的桌面管理器时,systemd 可以替代 [[acpid (简体中文)|acpid]] 处理 ACPI 事件。<br />
<br />
{{注意|systemd 无法处理交流电源和电池 ACPI 事件,所以还得使用 [[Laptop Mode Tools (简体中文)|Laptop Mode Tools]] 或 [[acpid (简体中文)|acpid]] 工具。}}<br />
<br />
在当前版本的 systemd 中,这些 {{ic|Handle}} 选项将会被应用到整个系统当中,除非它们被别的程序——例如某个桌面环境中的电源管理器——给“阻止”(inhibited)。如果其它的程序没有阻止这些 {{ic|Handle}} ,你可能会先被 systemd 挂起你的系统,然后当系统被唤醒之后,电源管理器又会再次将系统挂起。<br />
<br />
{{警告|目前只有[[GNOME]] 和 [[KDE]] 支持 "inhibited" 命令。在其它的桌面管理器同样实现该功能之前,如果你想使用[[Xfce]]、[[acpid]] 或者其它程序来管理 ACPI 事件,你需要把 {{ic|Handle}} 选项设置为 {{ic|ignore}} 。包含该功能的新版本还在开发当中。}}<br />
<br />
{{注意|除了内核默认的待机支持后端(用于处理待机/休眠),systemd 也可以使用其他后端(比如 [[Uswsusp]] 或 [[TuxOnIce]])。}}<br />
<br />
==== 休眠时执行的脚本 ====<br />
<br />
使用 {{ic|systemctl suspend}}、{{ic|systemctl hibernate}} 或 {{ic|systemctl hybrid-sleep}} 命令执行待机/休眠时,systemd 不会调用 [[pm-utils (简体中文)|pm-utils]]。[[pm-utils (简体中文)|pm-utils]] 的钩子扩展(hook)——包括 [[pm-utils (简体中文)#建立你自己的钩子|自定义钩子]]——会失效。不过,systemd 提供了两种类似的待机/休眠时执行脚本的机制。<br />
<br />
===== 使用服务文件 =====<br />
<br />
可以将服务文件附在 suspend.target、hibernate.target 或 sleep.target 中,这样就能在待机/休眠前后执行某些操作。用户级操作和root/系统级操作应该使用不同的服务文件。要启用用户级服务文件,使用 {{ic|# systemctl enable suspend@<用户名> && systemctl enable resume@<用户名>}}。例如:<br />
<br />
{{hc|/etc/systemd/system/suspend@.service|2=<nowiki><br />
[Unit]<br />
Description=User suspend actions<br />
Before=sleep.target<br />
<br />
[Service]<br />
User=%I<br />
Type=forking<br />
Environment=DISPLAY=:0<br />
ExecStartPre= -/usr/bin/pkill -u %u unison ; /usr/local/bin/music.sh stop ; /usr/bin/mysql -e 'slave stop'<br />
ExecStart=/usr/bin/sflock<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
{{hc|/etc/systemd/system/resume@.service|2=<nowiki><br />
[Unit]<br />
Description=User resume actions<br />
After=suspend.target<br />
<br />
[Service]<br />
User=%I<br />
Type=simple<br />
ExecStartPre=/usr/local/bin/ssh-connect.sh<br />
ExecStart=/usr/bin/mysql -e 'slave start'<br />
<br />
[Install]<br />
WantedBy=suspend.target</nowiki>}}<br />
<br />
至于root/系统级服务(使用 {{ic|# systemctl enable root-suspend}} 激活):<br />
<br />
{{hc|/etc/systemd/system/root-resume.service|2=<nowiki><br />
[Unit]<br />
Description=Local system resume actions<br />
After=suspend.target<br />
<br />
[Service]<br />
Type=simple<br />
ExecStart=/usr/bin/systemctl restart mnt-media.automount<br />
<br />
[Install]<br />
WantedBy=suspend.target</nowiki>}}<br />
<br />
{{hc|/etc/systemd/system/root-suspend.service|2=<nowiki><br />
[Unit]<br />
Description=Local system suspend actions<br />
Before=sleep.target<br />
<br />
[Service]<br />
Type=simple<br />
ExecStart=-/usr/bin/pkill sshfs<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
上述服务文件的一些解释(详见 {{ic|man systemd.service}}):<br />
* 如果设置 {{ic|1=<nowiki>Type=OneShot</nowiki>}},那么可以使用多个 {{ic|1=<nowiki>ExecStart=</nowiki>}} 参数。否则只能写一个,替代方案是在 {{ic|ExecStartPre}} 中添加命令,或使用分号分隔不同命令(见第一个例子,分号前后的空格都是'''必须'''的)。 <br />
* 若命令前加上一个“-”(半角减号),则命令返回非零值时会被忽略、当作正常执行处理。<br />
* 有关调试,最好的方法是用 {{ic|journalctl}} 查看日志。<br />
<br />
===== 合并待机和唤醒服务文件 =====<br />
<br />
利用下面这种自定义的一体化待机和唤醒服务,使用单一的钩子扩展即可对不同操作(待机/休眠/混合休眠)的不同阶段(进入/唤醒)进行控制。<br />
<br />
例子和解释:<br />
<br />
{{hc|/etc/systemd/system/wicd-sleep.service|2=<nowiki><br />
[Unit]<br />
Description=Wicd sleep hook<br />
Before=sleep.target<br />
StopWhenUnneeded=yes<br />
<br />
[Service]<br />
Type=oneshot<br />
RemainAfterExit=yes<br />
ExecStart=-/usr/share/wicd/daemon/suspend.py<br />
ExecStop=-/usr/share/wicd/daemon/autoconnect.py<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
* {{ic|1=<nowiki>RemainAfterExit=yes</nowiki>}}:服务启动后,除非显式地停止,否则就认为是活动的。<br />
* {{ic|1=<nowiki>StopWhenUnneeded=yes</nowiki>}}:服务活动时,如果无其他服务依赖该服务,就停止它。在本例中,该服务会在 sleep.target 停止后停止活动。<br />
* 由于 sleep.target 会被 suspend.target、hibernate.target、hybrid-sleep.target 调用,且 sleep.target 本身设置了 StopWhenUnneeded,该服务文件对以上各种操作都是有效的。<br />
<br />
===== 使用 /usr/lib/systemd/system-sleep =====<br />
<br />
systemd 在待机/休眠时执行 {{ic|/usr/lib/systemd/system-sleep/}} 里的所有脚本,传递下面两个参数:<br />
<br />
* 参数1:若是准备进入待机/休眠状态,则为 {{ic|pre}};唤醒时为 {{ic|post}}。<br />
* 参数2:事件名称,{{ic|suspend}},{{ic|hibernate}} 或 {{ic|hybrid-sleep}}。<br />
<br />
systemd 会同时执行所有脚本,而不是像 [[pm-utils (简体中文)|pm-utils]] 那样顺序执行。<br />
<br />
脚本输出会记录在相关服务({{ic|systemd-suspend.service}}、{{ic|systemd-hibernate.service}} 或 {{ic|systemd-hybrid-sleep.service}})中。通过[[#日志|日志]]查看:<br />
# journalctl -b -u systemd-suspend<br />
<br />
脚本示范:<br />
<br />
{{hc|/usr/lib/systemd/system-sleep/example.sh|<br />
#!/bin/sh<br />
case $1/$2 in<br />
pre/*)<br />
echo "进入 $2 状态..."<br />
;;<br />
post/*)<br />
echo "从 $2 状态唤醒..."<br />
;;<br />
esac}}<br />
<br />
记得添加可执行权限: <br />
# chmod a+x /usr/lib/systemd/system-sleep/example.sh<br />
<br />
详情参见 {{ic|man 7 systemd.special}} 和 {{ic|man 8 systemd-sleep}}。<br />
<br />
=== 临时文件 ===<br />
{{ic|/usr/lib/tmpfiles.d/}} 和 {{ic|/etc/tmpfiles.d/}} 中的文件描述了 systemd-tmpfiles 如何创建、清理、删除临时文件和目录,这些文件和目录通常存放在 {{ic|/run}} 和 {{ic|/tmp}} 中。配置文件名称为 {{ic|/etc/tmpfiles.d/<program>.conf}}。此处的配置能覆盖 {{ic|/usr/lib/tmpfiles.d/}} 目录中的同名配置。<br />
<br />
临时文件通常和服务文件同时提供,以生成守护进程需要的文件和目录。例如 [[Samba (简体中文)|Samba]] 服务需要目录 {{ic|/run/samba}} 存在并设置正确的权限位,就象这样:<br />
{{hc|/usr/lib/tmpfiles.d/samba.conf|<br />
D /run/samba 0755 root root<br />
}}<br />
<br />
此外,临时文件还可以用来在开机时向特定文件写入某些内容。比如,要禁止系统从USB设备唤醒,利用旧的 {{ic|/etc/rc.local}} 可以用 {{ic|echo USBE > /proc/acpi/wakeup}},而现在可以这么做:<br />
{{hc|/etc/tmpfiles.d/disable-usb-wake.conf|<br />
w /proc/acpi/wakeup - - - - USBE<br />
}}<br />
<br />
详情参见 {{ic|man 5 tmpfiles.d}}。<br />
<br />
=== 单元(unit) ===<br />
一个单元配置文件可以描述如下内容之一:系统服务、socket、系统设备、挂载点、交换分区/文件、启动目标(target)、文件系统路径、由 systemd 管理的计时器。文件格式受 .desktop 文件启发,而最初起源是 Windows 下的 .ini 文件。<br />
<br />
详情参阅 {{ic|man 5 systemd.unit}}.<br />
<br />
== 从 initscripts 迁移到 systemd ==<br />
<br />
=== initscripts 模拟 ===<br />
<br />
{{Pkg|initscripts}} 软件包提供了 systemd 和 Arch 传统配置的整合。当同时安装了{{Pkg|initscripts}}和systemd时,若以systemd启动系统,systemd将执行以下操作:<br />
<br />
# 解析{{ic|/etc/rc.conf}}中的{{ic|DAEMONS}}数组,并在系统启动时,启动所有列出的守护进程<br />
# 在启动过程中执行{{ic|/etc/rc.local}}<br />
# 在系统关闭过程中执行{{ic|/etc/rc.local.shutdown}}<br />
<br />
Initscripts模拟仅仅是作为用户迁移到systemd时的过渡措施,这一方案最终将'''不再可行'''。原生的systemd并不依赖于{{ic|rc.conf}}文件的集中式配置,因此推荐使用[[#原生 systemd 配置文件|原生的systemd配置文件]]。原生systemd配置文件比{{ic|/etc/rc.conf}}具有更高优先级。<br />
<br />
{{注意|替换{{ic|/etc/rc.local}}的推荐方式是针对系统启动时要运行的程序编写自定义的service文件,详见相关[[#自己编写_.service_文件|章节]].}}<br />
<br />
{{注意|如果你曾经在{{ic|/etc/inittab}}中禁用了以{{keypress|Ctrl+Alt+Del}}重启系统,则需要以root执行{{ic|systemctl mask ctrl-alt-del.target}}以使其对systemd重新生效。}}<br />
<br />
{{警告|如果同时安装 systemd(197-4 及后续版本)和 initscripts,那么 {{ic|/etc/rc.local}} 脚本会导致启动过程无法结束(原因是 getty@tty1.service 自 197-4 版本起不再等待 rc-local.service 执行,getty 无法启动)。请删除或重命名 {{ic|/etc/rc.local}}。}}<br />
<br />
=== 迁移 DAEMONS 列表 ===<br />
<br />
若要用纯 systemd 环境,则应当删除 {{ic|/etc/rc.conf}} 文件,并完全通过 {{ic|systemctl}} 命令来启动服务。对于 {{ic|DAEMONS}} 列表里面的每个 {{ic|<服务名>}},现在可以用以下命令设置成自动启动:<br />
<br />
# systemctl enable <服务名>.service<br />
<br />
{{小贴士|[[Daemons List|这里]]有一份 initscripts 和 systemd 下常见服务的对应列表。}}<br />
<br />
如果提示 {{ic|<service_name>.service}} 不存在:<br />
<br />
* 很有可能是 systemd 用了不同的服务名。比如 {{ic|crond}} 服务替换成 {{ic|cronie.service}};{{ic|alsa}} 服务替换成了 {{ic|alsa-store.service}} 和 {{ic|alsa-restore.service}} 两个服务。另一个值得注意的是 {{ic|network}} 服务,它被替换为一整套服务文件(详情参见 [[Configuring_Network_(简体中文)]])。<br />
* 另外,有些程序尚未提供 .service 文件,还得继续用 {{ic|rc.conf}} 来配置开机自动启动。<br />
<br />
{{小贴士|用下面这个命令来查看已安装软件包包含的服务文件:<br />
$ pacman -Ql cronie<br />
[...]<br />
cronie /etc/rc.d/crond # DAEMONS 列表中使用的 initscript 服务 (原生 systemd 配置中不再使用)<br />
[...]<br />
cronie /usr/lib/systemd/system/cronie.service # 原生 systemd 服务文件<br />
[...]<br />
}}<br />
<br />
* 有些服务不需要用户明确配置自启动。比如安装 {{ic|dbus-core}} 软件包后 {{ic|dbus.service}} 就会自动被配置为自启动。而 {{ic|alsa-store.service}} 和 {{ic|alsa-restore.service}} 也会由 systemd 自动启动。可以用 {{ic|systemctl status <服务名>}} 命令来查看服务列表跟它们的运行状态。<br />
<br />
== systemd 基本工具 ==<br />
<br />
检视和控制systemd的主要命令是{{ic|systemctl}}。该命令可用于查看系统状态和管理系统及服务。详见{{ic|man 1 systemctl}}。<br />
<br />
{{小贴士|在 {{ic|systemctl}} 参数中添加 {{ic|-H <用户名>@<主机名>}} 可以实现对其他机器的远程控制。该过程使用 [[SSH (简体中文)|SSH]] 链接。}}<br />
<br />
{{注意|{{ic|systemadm}} 是 systemd 的官方图形前端。由 [[AUR (简体中文)|AUR]] 中的软件包 {{AUR|systemd-ui-git}} 提供。}}<br />
<br />
=== 分析系统状态 ===<br />
<br />
输出激活的单元:<br />
<br />
$ systemctl<br />
<br />
以下命令等效:<br />
<br />
$ systemctl list-units<br />
<br />
输出运行失败的单元:<br />
<br />
$ systemctl --failed<br />
<br />
所有可用的单元文件存放在 {{ic|/usr/lib/systemd/system/}} 和 {{ic|/etc/systemd/system/}} 目录(后者优先级更高)。查看所有已安装服务:<br />
$ systemctl list-unit-files<br />
<br />
=== 使用单元 ===<br />
一个单元可以是系统服务({{ic|.service}})、挂载点({{ic|.mount}})、sockets({{ic|.sockets}})。<br />
<br />
使用 {{ic|systemctl}} 控制单元时,通常需要使用单元文件的全名,包括扩展名(例如 {{ic|sshd.service}})。但是有些单元可以在{{ic|systemctl}}中使用简写方式。<br />
* 如果无扩展名,systemctl 默认把扩展名当作 {{ic|.service}}。例如 {{ic|netcfg}} 和 {{ic|netcfg.service}} 是等价的。<br />
* 挂载点会自动转化为相应的 {{ic|.mount}} 单元。例如 {{ic|/home}} 等价于 {{ic|home.mount}}。<br />
* 设备会自动转化为相应的 {{ic|.device}} 单元,所以 {{ic|/dev/sda2}} 等价于 {{ic|dev-sda2.device}}。<br />
<br />
立即激活单元:<br />
<br />
# systemctl start <单元><br />
<br />
立即停止单元:<br />
# systemctl stop <单元><br />
<br />
重启单元:<br />
<br />
# systemctl restart <单元><br />
<br />
命令单元重新读取配置:<br />
<br />
# systemctl reload <单元><br />
<br />
输出单元运行状态:<br />
<br />
$ systemctl status <单元><br />
<br />
检查单元是否配置为自动启动:<br />
$ systemctl is-enabled <单元><br />
<br />
开机自动激活单元:<br />
<br />
# systemctl enable <单元><br />
<br />
{{注意|如果服务没有{{ic|Install}}段落,一般意味着应该通过其它服务自动调用它们。如果真的需要手动安装,可以直接连接服务,如下(将{{ic|foo}}替换为真实的服务名):<br />
{{bc|# ln -s /usr/lib/systemd/system/foo.service /etc/systemd/system/graphical.target.wants/}}<br />
}}<br />
<br />
取消开机自动激活单元:<br />
<br />
# systemctl disable <单元>}}<br />
<br />
显示单元的手册页(必须由单元文件提供):<br />
<br />
# systemctl help <单元><br />
<br />
重新载入 systemd,扫描新的或有变动的单元:<br />
# systemctl daemon-reload<br />
<br />
=== 电源管理 ===<br />
<br />
安装 {{ic|polkit}} 后才可使用电源管理。<br />
<br />
如果你正登录在一个本地的{{ic|systemd-logind}}用户会话,且当前没有其它活动的会话,那么以下命令无需root权限即可执行。否则(例如,当前有另一个用户登录在某个tty),systemd 将会自动请求输入root密码。<br />
<br />
重启:<br />
<br />
$ systemctl reboot<br />
<br />
退出系统并停止电源:<br />
<br />
$ systemctl poweroff<br />
<br />
待机:<br />
<br />
$ systemctl suspend<br />
<br />
休眠:<br />
<br />
$ systemctl hibernate<br />
<br />
混合休眠模式(同时休眠到硬盘并待机):<br />
<br />
$ systemctl hybrid-sleep<br />
<br />
== 启动桌面环境 ==<br />
<br />
=== 使用登录管理器 ===<br />
<br />
通过启动[[Display Manager (简体中文)|登录管理器]](或称显示管理器),即可进行图形界面登录。目前,Arch 提供了 [[GDM (简体中文)|GDM]]、[[KDM (简体中文)|KDM]]、[[SLiM (简体中文)|SLiM]]、[[XDM (简体中文)|XDM]]、[[LXDM (简体中文)|LXDM]] 和 [[LightDM (简体中文)|LightDM]] 的 systemd 服务文件。以 KDM 为例,配置开机启动:<br />
# systemctl enable kdm.service<br />
<br />
执行上述命令后,登录管理器应当能正常工作了。如果不是的话,很可能是因为你修改了{{ic|default.target}}。默认情况应当如下:<br />
<br />
{{hc|# ls -l /etc/systemd/system/default.target|/etc/systemd/system/default.target -> /usr/lib/systemd/system/graphical.target}}<br />
<br />
删除被修改的 {{ic|default.target}} 即可,systemd 会自动使用默认配置(即 {{ic|graphical.target}}):<br />
<br />
# rm /etc/systemd/system/default.target<br />
<br />
=== 使用 systemd-logind ===<br />
<br />
{{注意|自 2012-10-30 起, [[ConsoleKit]] 已被 [https://www.archlinux.org/news/consolekit-replaced-by-logind/ systemd-logind 取代] ,作为登录到桌面环境的默认方式。}}<br />
<br />
可使用 {{ic|loginctl}} 来查看用户会话的状态。所有 [[PolicyKit]] 操作,如挂起系统、挂载外部驱动器,都无需配置即可使用。<br />
<br />
$ loginctl show-session $XDG_SESSION_ID<br />
<br />
== 自己编写 .service 文件 ==<br />
''参见:[[Systemd/Services]]''<br />
<br />
=== 处理依赖关系 ===<br />
<br />
使用systemd时,可通过正确编写单元配置文件来解决其依赖关系。典型的情况是,单元{{ic|A}}要求单元{{ic|B}}在{{ic|A}}启动之前运行。在此情况下,向单元{{ic|A}}配置文件中的 {{ic|[Unit]}} 段添加 {{ic|1=Requires=B}} 和 {{ic|1=After=B}} 即可。若此依赖关系是可选的,可添加 {{ic|1=Wants=B}} 和 {{ic|1=After=B}}。请注意 {{ic|1=Wants=}} 和 {{ic|1=Requires=}} 并不意味着 {{ic|1=After=}},即如果 {{ic|1=After=}} 选项没有制定,这两个单元将被并行启动。<br />
<br />
依赖关系通常被用在服务(service)而不是[[#目标(target)|目标(target)]]上。例如, {{ic|network.target}} 一般会被某个配置网络接口的服务引入,所以,将自定义的单元排在该服务之后即可,因为 {{ic|network.target}} 已经启动。<br />
<br />
=== 启动方式 ===<br />
<br />
编写自定义的service文件时,可以选择几种不同的服务启动方式。启动方式可通过配置文件 {{ic|[Service]}} 段中的 {{ic|1=Type=}} 参数进行设置。具体的参数说明请参阅 {{ic|man systemd.service}} 。<br />
<br />
* {{ic|1=Type=simple}}:systemd认为该服务将立即启动。服务进程不会fork。如果该服务要启动其他服务,不要使用此类型启动,除非该服务是socket激活型。<br />
* {{ic|1=Type=forking}}:systemd认为当该服务进程fork,且父进程退出后服务启动成功。对于常规的守护进程(daemon),除非你确定此启动方式无法满足需求,使用此类型启动即可。使用此启动类型应同时指定 {{ic|1=PIDFile=}},以便systemd能够跟踪服务的主进程。<br />
* {{ic|1=Type=oneshot}}:这一选项适用于只执行一项任务随后退出的服务。你可能需要同时设置 {{ic|1=RemainAfterExit=yes}} 使得 systemd 在服务进程退出之后仍然认为服务处于激活状态。<br />
* {{ic|1=Type=notify}}:与 {{ic|1=Type=simple}} 相同,但约定服务会在就绪后向 systemd 发送一个信号。这一通知的实现由 {{ic|libsystemd-daemon.so}} 提供。<br />
* {{ic|1=Type=dbus}}:若以此方式启动,当指定的 {{ic|BusName}} 出现在DBus系统总线上时,systemd认为服务就绪。<br />
<br />
=== 替换现存单元文件 ===<br />
<br />
目录 {{ic|/etc/systemd/system/}} 中的单元配置文件比 {{ic|/usr/lib/systemd/system/}} 目录下的文件拥有更高优先级。<br />
若需要定制某个单元的启动(使配置不受软件包更新的影响),请将现有的单元配置文件从 {{ic|/usr/lib/}} 复制到 {{ic|/etc/}} 目录下,并在此目录中修改。或者,可以使用 {{ic|.include}} 来解析一个现有的配置文件,并覆盖原选项或添加新选项。例如,若想要某个服务的配置文件中添加额外依赖关系,可以编写:<br />
<br />
{{hc|/etc/systemd/system/<服务名>.service|2=<br />
.include /usr/lib/systemd/system/<服务名>.service<br />
<br />
[Unit]<br />
Requires=<新依赖><br />
After=<新依赖>}}<br />
<br />
然后运行以下命令使更改生效:<br />
<br />
# systemctl reenable <单元><br />
# systemctl restart <单元><br />
<br />
{{小贴士|可以用 {{ic|systemd-delta}} 命令来查看哪些单元文件被覆盖、哪些被修改。}}<br />
<br />
=== 单元配置文件的 vim 语法高亮支持 ===<br />
<br />
可从 [[Official Repositories|官方仓库]] 安装 {{Pkg|vim-systemd}} 软件包,使 unit 配置文件在 [[Vim]] 下支持语法高亮。<br />
<br />
== 目标(target) ==<br />
启动级别(runlevel)是一个旧的概念。现在,systemd 引入了一个和启动级别功能相似又不同的概念——目标(target)。不像数字表示的启动级别,每个''目标''都有名字和独特的功能,并且能同时启用多个。一些''目标''继承其他''目标''的服务,并启动新服务。systemd 提供了一些模仿 sysvinit 启动级别的''目标'',仍可以使用旧的 {{ic|telinit 启动级别}} 命令切换。<br />
<br />
=== 获取当前目标 ===<br />
不要使用 {{ic|runlevel}} 命令了:<br />
$ systemctl list-units --type=target<br />
<br />
=== 创建新目标 ===<br />
在 Fedora 中,启动级别 0、1、3、5、6 都被赋予特定用途,并且都对应一个 systemd 的''目标''。然而,没有什么很好的移植用户定义的启动级别(2、4)的方法。要实现类似功能,可以以原有的启动级别为基础,创建一个新的''目标'' {{ic|/etc/systemd/system/<新目标>}}(可以参考 {{ic|/usr/lib/systemd/system/graphical.target}}),创建 {{ic|/etc/systemd/system/<新目标>.wants}} 目录,向其中加入额外服务的链接(指向 {{ic|/usr/lib/systemd/system/}} 中的单元文件)。<br />
<br />
=== 目标表 ===<br />
{| border="1"<br />
!SysV 启动级别!!Systemd 目标!!注释<br />
|-<br />
| 0 || runlevel0.target, poweroff.target || 中断系统(halt)<br />
|-<br />
| 1, s, single || runlevel1.target, rescue.target || 单用户模式<br />
|-<br />
| 2, 4 || runlevel2.target, runlevel4.target, multi-user.target || 用户自定义启动级别,通常识别为级别3。<br />
|-<br />
| 3 || runlevel3.target, multi-user.target || 多用户,无图形界面。用户可以通过终端或网络登录。<br />
|-<br />
| 5 || runlevel5.target, graphical.target || 多用户,图形界面。继承级别3的服务,并启动图形界面服务。<br />
|-<br />
| 6 || runlevel6.target, reboot.target || 重启<br />
|-<br />
| emergency || emergency.target || 急救模式(Emergency shell)<br />
|-<br />
|}<br />
<br />
=== 切换启动级别/目标 ===<br />
systemd 中,启动级别通过“目标单元”访问。通过如下命令切换:<br />
# systemctl isolate graphical.target<br />
该命令对下次启动无影响。等价于{{ic|telinit 3}} 或 {{ic|telinit 5}}。<br />
<br />
=== 修改默认启动级别/目标 ===<br />
开机启动进的目标是 {{ic|default.target}},默认链接到 {{ic|graphical.target}} (大致相当于原来的启动级别5)。可以通过[[Kernel parameters (简体中文)|内核参数]]更改默认启动级别:<br />
<br />
{{小贴士|可以省略扩展名 {{ic|.target}}。}}<br />
<br />
* {{ic|1=systemd.unit=multi-user.target}} (大致相当于级别3)<br />
* {{ic|1=systemd.unit=rescue.target}} (大致相当于级别1)<br />
<br />
另一个方法是修改 {{ic|default.target}}。可以通过 {{ic|systemctl}} 修改它:<br />
<br />
# systemctl enable multi-user.target<br />
<br />
命令执行情况由 {{ic|systemctl}} 显示:链接 {{ic|/etc/systemd/system/default.target}} 被创建,指向新的默认启动级别。该方法当且仅当目标配置文件中有以下内容时有效:<br />
<br />
[Install]<br />
Alias=default.target<br />
<br />
目前,{{ic|multi-user.target}}、{{ic|graphical.target}} 都包含这段内容。<br />
<br />
== 日志 ==<br />
自版本 38 起,systemd 提供了自己日志系统(logging system),称为 journal. 使用 systemd 日志,无需额外安装日志服务(syslog)。读取日志的命令:<br />
# journalctl<br />
<br />
默认情况下(当 {{ic|Storage&#61;}} 在文件 {{ic|/etc/systemd/journald.conf}} 中被设置为 {{ic|auto}}),日志记录将被写入 {{ic|/var/log/journal/}}。若目录 {{ic|/var/log/journal/}} 不存在(例如,被用户或某个程序删除),systemd将'''不会'''自动创建它,而是将日志记录写入 {{ic|/run/systemd/journal}}。这意味着,系统重启后日志将丢失。<br />
<br />
=== 过滤输出 ===<br />
<br />
{{ic|journalctl}}可以根据特定字段过滤输出,例如:<br />
<br />
显示本次启动后的所有日志:<br />
<br />
# journalctl -b<br />
<br />
不过,一般大家更关心的不是本次启动后的日志,而是上次启动时的(例如,刚刚系统崩溃了)。目前还没有这项功能,正在 [http://comments.gmane.org/gmane.comp.sysutils.systemd.devel/6608 systemd-devel@lists.freedesktop.org] 讨论中。<br />
<br />
目前的折中方案是:<br />
<br />
# journalctl --since=today | tac | sed -n '/-- Reboot --/{n;:r;/-- Reboot --/q;p;n;b r}' | tac<br />
<br />
以上命令输出本日内的所有启动信息。但要注意,如果日志很多,该命令执行时间会比较漫长。<br />
<br />
动态跟踪最新信息:<br />
# journalctl -f<br />
<br />
显示特定程序的所有消息:<br />
{{bc|# journalctl /usr/lib/systemd/systemd}}<br />
<br />
显示特定进程的所有消息:<br />
{{bc|1=# journalctl _PID=1}}<br />
<br />
显示指定单元的所有消息:<br />
# journalctl -u netcfg<br />
<br />
详情参阅{{ic|man journalctl}}、{{ic|man systemd.journal-fields}},以及 Lennert 的这篇[http://0pointer.de/blog/projects/journalctl.html 博文]。<br />
<br />
=== 日志大小限制 ===<br />
<br />
如果按上面的操作保留日志的话,默认日志最大限制为所在文件系统容量的 10%,即:如果 {{ic|/var/log/journal}} 储存在 50GiB 的根分区中,那么日志最多存储 5GiB 数据。可以修改 {{ic|/etc/systemd/journald.conf}} 中的 {{ic|SystemMaxUse}} 来指定该最大限制。如限制日志最大 50MiB:<br />
<br />
SystemMaxUse=50M<br />
<br />
详情参见 {{ic|man journald.conf}}.<br />
<br />
=== 配合 syslog 使用 ===<br />
systemd 提供了 socket {{ic|/run/systemd/journal/syslog}},以兼容传统日志服务。所有系统信息都会被传入。要使传统日志服务工作,需要让服务链接该 socket,而非 {{ic|/dev/log}}([http://lwn.net/Articles/474968/ 官方说明])。Arch 软件仓库中的 {{pkg|syslog-ng}} 已经包含了需要的配置。<br />
<br />
设置开机启动 syslog-ng:<br />
# systemctl enable syslog-ng<br />
<br />
== 优化 ==<br />
<br />
参阅:[[Improve Boot Performance (简体中文)]]。<br />
<br />
=== 启动过程分析 ===<br />
systemd 提供了一个分析启动过程的工具 —— {{ic|systemd-analyze}}。可以用它看看哪些单元拖慢了开机过程,并据此进行优化。安装 {{Pkg|python2-dbus}}、{{Pkg|python2-cairo}} 软件包后该工具才能工作。<br />
<br />
查看开机过程在内核/用户空间消耗的时间:<br />
$ systemd-analyze<br />
{{小贴士|如果在 {{ic|/etc/mkinitcpio.conf}} 的 {{ic|HOOKS}} 数组添加 {{ic|timestamp}},并重新生成启动内存镜像({{ic|mkinitcpio -p linux}}),{{ic|systemd-analyze}} 还可以显示处理该启动镜像花费的时间。}}<br />
<br />
按照耗费时间顺序,输出启动每个单元耗费的时间:<br />
$ systemd-analyze blame<br />
<br />
生成类似于 [[Bootchart (简体中文)|bootchart]] 的开机过程图表:<br />
$ systemd-analyze plot > plot.svg<br />
<br />
==== 使用 systemd-bootchart ====<br />
<br />
自2012年10月17日,bootchart 工具已经合并进 systemd 中,使用方法和原来的 bootchart 大同小异,添加下列内容到内核参数即可:<br />
<br />
initcall_debug printk.time=y init=/usr/lib/systemd/systemd-bootchart<br />
<br />
==== 使用 bootchart2 ====<br />
<br />
由于没有办法在内核参数设置两个 init,所以不能使用源里的 bootchart。不过,[[AUR (简体中文)|AUR]] 软件包 {{AUR|bootchart2}} 提供了一个的 systemd 服务,这样就可以和 systemd 一起使用。安装后启用即可:<br />
{{bc|# systemctl enable bootchart.service}}<br />
详情参阅 [https://github.com/mmeeks/bootchart bootchart 文档]。<br />
<br />
=== 预读 ===<br />
systemd 自己实现了一个 readahead,可以用来提高开机效率。不过,效果会因内核版本和硬件情况而不同(极少数情况下还会变慢)。开启 readahead:<br />
<br />
# systemctl enable systemd-readahead-collect.service systemd-readahead-replay.service<br />
<br />
要知道,readahead 的超级牛力只有在重启几次后才会显现。<br />
<br />
== 疑难解答 ==<br />
<br />
=== 关机/重启十分缓慢 ===<br />
<br />
如果关机特别慢(甚至跟死机了一样),很可能是某个拒不退出的服务在作怪。systemd 会等待一段时间,然后再尝试杀死它。请阅读[http://freedesktop.org/wiki/Software/systemd/Debugging#Shutdown_Completes_Eventually 这篇文章],确认你是否是该问题受害者。<br />
<br />
=== 短时进程无日志记录 ===<br />
<br />
若 {{ic|journalctl -u foounit.service}} 没有显示某个短时进程的任何输出,那么改用 PID 试试。例如,若 systemd-modules-load.service 执行失败,那么先用 {{ic|systemctl status systemd-modules-load}} 查询其 PID(比如是123),然后检索该 PID 相关的日志 {{ic|journalctl -b _PID&#61;123}}。运行时进程的日志元数据(诸如 _SYSTEMD_UNIT 和 _COMM)被乱序收集在 {{ic|/proc}} 目录。要修复该问题,必须修改内核,使其通过套接字连接来提供上述数据,该过程类似于 SCM_CREDENTIALS。<br />
<br />
== 相关资源 ==<br />
<br />
*[http://www.freedesktop.org/wiki/Software/systemd 官方网站]<br />
*[http://0pointer.de/public/systemd-man/ man 手册页]<br />
*[http://freedesktop.org/wiki/Software/systemd/Optimizations systemd 优化]<br />
*[http://www.freedesktop.org/wiki/Software/systemd/FrequentlyAskedQuestions 常见问题 FAQ]<br />
*[http://www.freedesktop.org/wiki/Software/systemd/TipsAndTricks 小技巧]<br />
*[http://0pointer.de/public/systemd-ebook-psankar.pdf systemd for Administrators (PDF)]<br />
*[http://fedoraproject.org/wiki/Systemd Fedora 项目对 systemd 的介绍]<br />
*[http://fedoraproject.org/wiki/How_to_debug_Systemd_problems 如何调试 systemd 故障]<br />
*''The H Open'' 杂志上的[http://www.h-online.com/open/features/Control-Centre-The-systemd-Linux-init-system-1565543.html 两][http://www.h-online.com/open/features/Booting-up-Tools-and-tips-for-systemd-1570630.html 篇]科普文章<br />
*[http://0pointer.de/blog/projects/systemd.html Lennart 的博文]<br />
*[http://0pointer.de/blog/projects/systemd-update.html 更新报告]<br />
*[http://0pointer.de/blog/projects/systemd-update-2.html 更新报告2]<br />
*[http://0pointer.de/blog/projects/systemd-update-3.html 更新报告3]<br />
*[http://0pointer.de/blog/projects/why.html 最新动态]<br />
*[https://fedoraproject.org/wiki/SysVinit_to_Systemd_Cheatsheet/zh Fedora Wiki 上的 SysVinit 和 systemd 命令对比表]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Systemd_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=246371
Systemd (简体中文)
2013-02-05T02:51:47Z
<p>Cuihao: </p>
<hr />
<div>{{Lowercase title}}<br />
[[Category:Daemons and system services (简体中文)]]<br />
[[Category:Boot process (简体中文)]]<br />
[[en:Systemd]]<br />
[[es:Systemd]]<br />
[[fr:Systemd]]<br />
[[it:Systemd]]<br />
[[ja:Systemd]]<br />
[[ru:Systemd]]<br />
[[zh-TW:Systemd]]<br />
{{Article summary start}}<br />
{{Article summary text|本文介绍 Systemd 的安装和配置。}}<br />
{{Article summary heading|相关页面}}<br />
{{Article summary wiki|Systemd/Services}}<br />
{{Article summary wiki|systemd/User}}<br />
{{Article summary wiki|Systemd FAQ}}<br />
{{Article summary wiki|init Rosetta}}<br />
{{Article summary wiki|udev (简体中文)}}<br />
{{Article summary end}}<br />
{{TranslationStatus (简体中文)|Systemd|2013-01-29|245346}}<br />
摘自[http://freedesktop.org/wiki/Software/systemd 项目主页]:<br />
<br />
'''''systemd''' 是 Linux 下的一款系统和服务管理器,兼容 SysV 和 LSB 的启动脚本。systemd 的特性有:支持并行化任务;同时采用 socket 式与 [[D-Bus (简体中文)|D-Bus]] 总线式激活服务;按需启动守护进程(daemon);利用 Linux 的 [[cgroups|cgroups]] 监视进程;支持快照和系统恢复;维护挂载点;各服务间基于依赖关系进行精密控制。systemd 完全可以替代 Arch 默认的[[SysVinit|sysvinit]]启动系统。''<br />
<br />
{{注意|[https://bbs.archlinux.org/viewtopic.php?pid&#61;1149530#p1149530 这个帖子]详细解释了 Arch 向 systemd 迁移的原因。}}<br />
<br />
另见[[Wikipedia:Systemd|维基百科上的介绍]]。<br />
<br />
== 迁移前需考虑 ==<br />
<br />
* 阅读[http://freedesktop.org/wiki/Software/systemd/ 该站],了解 systemd。<br />
* systemd 自己有一套日志('''journal''')系统,用于代替 '''syslog'''。两者也可以共存,参见后面的[[#日志|日志部分]]。<br />
* 虽然 systemd 可以替换 '''cron'''、'''acpid'''、'''xinetd''' 等的部分功能。至少目前还可以继续使用这些服务,无需立即切换。<br />
* 交互式 initscripts 启动脚本在 systemd 中无法工作。例如 '''netcfg-menu''' [https://bugs.archlinux.org/task/31377 无法]在启动时显示。<br />
<br />
== 安装 ==<br />
{{注意|从[https://www.archlinux.org/news/systemd-is-now-the-default-on-new-installations/ 2012-10-13版安装介质]开始,安装程序已经默认安装{{pkg|systemd}} 和 {{pkg|systemd-sysvcompat}}。}}<br />
{{注意|如果是在 VPS 中使用 Arch,请先阅读:[[Virtual_Private_Server#Moving_your_VPS_from_initscripts_to_systemd]]。}}<br />
<br />
本部分帮助还在使用{{pkg|sysvinit}} 和 {{pkg|initscripts}} 的用户迁移到{{pkg|systemd}}.<br />
<br />
# 从[[Official_Repositories_(简体中文)|官方软件仓库]]安装 {{pkg|systemd}} 并添加[[Kernel parameters|内核参数]]{{Ic|1=init=/usr/lib/systemd/systemd}}<br />
# 用 {{ic|systemctl enable ''daemonname''}} [[#使用单元|启用]] {{ic|/etc/rc.conf}} 中的守护进程。从 {{ic|/etc/rc.conf}} 到 systemd 服务的转换可以参考:[[Daemons_List_(简体中文)|守护进程列表]] 和 [[Systemd/Services|Services]]。<br />
# 重启系统,执行命令 {{ic|cat /proc/1/comm}},如果返回{{ic|systemd}},表示 systemd 已经正常启动。<br />
# 确认主机名已经正确设置:{{ic|hostnamectl set-hostname myhostname}}。<br />
# 删除 {{pkg|initscripts}} 和 {{pkg|sysvinit}},并安装{{pkg|systemd-sysvcompat}}.<br />
# 可选,删除{{ic|1=init=/usr/lib/systemd/systemd}}内核参数,现在已经不需要它了,{{pkg|systemd-sysvcompat}}提供了默认的 init.<br />
<br />
=== 附加信息 ===<br />
* 如果内核参数中有 {{ic|quiet}},可以先删除它以便调试。<br />
* 使用 Systemd 的时候无需将用户加入{{ic|optical}}, {{ic|audio}}, {{ic|scanner}} 等[[Users and Groups (简体中文)|用户组]]。加入这些组反而会有问题,例如audio组会导致程序阻塞软件混声。每个 PAM 登录都拥有一个 logind 会话,它通过[[Wikipedia:Access control list|POSIX ACLs]],赋予本地会话以声音设备访问权限、通过[[udisks]]挂载和卸载移动设备的权限等。<br />
{{注意|对于使用 {{ic|startx -- vt7}} 进入X环境的用户,由于 logind 会话无法应用于X环境,用户'''必须'''加入这些组。}}<br />
* 如果卸载 {{Pkg|initscripts}},{{ic|rc.conf}} 将不再有效。如果曾在其中设置了静态网络,或还未迁移到systemd的系统服务,请根据[[#Initscripts 模拟|该节]]配置。<br />
* 如果 fstab 中有 LVM 设备,首次启动时会因为等待超时进入急救控制台。输入管理员密码,执行 {{ic|systemctl enable lvm}} 启用相关服务,重启即可。<br />
<br />
== 原生 systemd 配置文件 ==<br />
{{注意|可能需要手动创建某些文件。所有文件的权限都是644,属主 root,属组 root。}}<br />
<br />
=== 主机名 ===<br />
主机名通过文件 {{ic|/etc/hostname}} 进行配置。该文件不应包含系统的域名(如果有)。使用如下命令设置主机名:<br />
# hostnamectl set-hostname '''myhostname'''<br />
<br />
详情参见 {{ic|man 5 hostname}} 和 {{ic|man 1 hostnamectl}}。<br />
<br />
文件示例:<br />
{{hc|/etc/hostname|<br />
myhostname<br />
}}<br />
<br />
=== 本地化设置(locale) ===<br />
系统默认 locale 记录在 {{ic|/etc/locale.conf}} 文件中。通过如下命令配置:<br />
# localectl set-locale LANG="zh_CN.utf8"<br />
<br />
{{注意|设置默认locale前,请确保已经生成该locale。方法是:在 {{ic|/etc/locale.gen}} 中启用对应locale,然后以root权限执行 {{ic|locale-gen}}。}}<br />
<br />
详情参见 {{ic|man 1 localectl}} 和 {{ic|man 5 locale.conf}}。<br />
<br />
* 另参阅 [[Locale (简体中文)]]。<br />
<br />
文件示例:<br />
{{hc|/etc/locale.conf|<nowiki><br />
LANG=en_US.utf8<br />
</nowiki>}}<br />
<br />
=== 虚拟控制台 ===<br />
{{ic|/etc/vconsole.conf}} 文件用来配置虚拟控制台,包括键盘布局和控制台字体:<br />
{{hc|/etc/vconsole.conf|<nowiki><br />
KEYMAP=us<br />
FONT=lat9w-16<br />
FONT_MAP=8859-1_to_uni</nowiki>}}<br />
<br />
{{注意|{{ic|{{Pkg|systemd}}-194}} 默认使用内核字体和键盘布局,因此以后不必在上述文件中留下空的 {{ic|1=KEYMAP=}} 和 {{ic|1=FONT=}} 设置。}}<br />
<br />
还可以通过命令设置键盘布局:<br />
# localectl set-keymap de<br />
<br />
{{ic|localectl}} 也可以设置X11的键盘布局:<br />
<br />
# localectl set-x11-keymap de<br />
<br />
详情参见 {{ic|man 1 localectl}} 和 {{ic|man 5 vconsole.conf}}。<br />
<br />
* 另参阅 [[Fonts (简体中文)#控制台字体|控制台字体]] 和 [[KEYMAP (简体中文)#键盘布局|键盘布局]]。<br />
<br />
=== 时区 ===<br />
时区可通过创建适当的{{ic|/etc/localtime}}符号链接来进行配置,该链接需指向目录{{ic|/usr/share/zoneinfo/}}下的一个zoneinfo文件。也可执行以下命令直接完成这一操作:<br />
<br />
# timedatectl set-timezone Asia/Shanghai<br />
<br />
更多细节参见{{ic|man 1 timedatectl}},{{ic|man 5 localtime}}和{{ic|man 7 archlinux}}。<br />
<br />
手动创建链接也行:<br />
# ln -sf ../usr/share/zoneinfo/Asia/Shanghai /etc/localtime<br />
<br />
旧的配置文件 {{ic|/etc/timezone}} 已经没用了,可以安全删除。<br />
<br />
=== 硬件时钟 ===<br />
<br />
systemd 默认识别硬件时钟为协调世界时(UTC)。<br />
<br />
{{小贴士|推荐使用[[NTP (简体中文)|NTP服务]]来在线同步硬件时钟。}}<br />
<br />
==== 硬件时钟设定为地方时 ====<br />
<br />
将硬件时钟配置为地方时('''不建议'''):<br />
<br />
# timedatectl set-local-rtc true<br />
<br />
重新调整为 UTC:<br />
<br />
# timedatectl set-local-rtc false<br />
<br />
如果设置成本地时间,处理夏令时有些麻烦。如果夏令时调整发生在关机时,下次启动时时间会出现问题([http://www.cl.cam.ac.uk/~mgk25/mswish/ut-rtc.html 更多信息])。最新的内核直接从实时时钟芯片(RTC)读取时间,不使用 {{ic|hwclock}},内核把从 RTC 读取的时间当作 UTC 处理。所以如果硬件时间是地方时,系统启动一开始识别的时间是错误的,之后很快会进行矫正。这可能导致一些问题(尤其是时间倒退时)。<br />
<br />
如果同时安装了 Windows 操作系统([http://blogs.msdn.com/b/oldnewthing/archive/2004/09/02/224672.aspx 默认使用地方时]),那么一般 RTC 会被设置为地方时。Windows 其实也能处理 UTC,需要[[Time (简体中文)#Windows 系统使用 UTC|修改注册表]]。建议让 Windows 使用 UTC,而非让 Linux 使用地方时。Windows 使用 UTC 后,请记得禁用 Windows 的时间同步功能,以防 Windows 错误设置硬件时间。如上文所说,Linux 可以使用[[NTP (简体中文)|NTP服务]]来在线同步硬件时钟。<br />
<br />
* 详情参阅 [[Time (简体中文)]]。<br />
<br />
=== 内核模块 ===<br />
目前,所有必要模块的加载均由 [[udev]] 自动完成。所以,如果不需要使用任何额外的模块,就没有必要在任何配置文件中添加启动时加载的模块。但是,有些情况下可能需要在系统启动时加载某个额外的模块,或者将某个模块列入黑名单以便使系统正常运行。<br />
<br />
==== 开机加载 ====<br />
systemd 读取 {{ic|/etc/modules-load.d/}} 中的配置加载额外的内核模块。配置文件名称通常为 {{ic|/etc/modules-load.d/<program>.conf}}。格式很简单,一行一个要读取的模块名,而空行以及第一个非空格字符为{{ic|#}}或{{ic|;}}的行会被忽略,如:<br />
{{hc|/etc/modules-load.d/virtio-net.conf|<nowiki><br />
# Load virtio-net.ko at boot<br />
virtio-net</nowiki>}}<br />
<br />
另见{{ic|man 5 modules-load.d}}。<br />
<br />
==== 禁用内核模块 ====<br />
禁用内核模块的方法和 Arch 默认的 {{Pkg|initscripts}} 相同,因为该过程实际由 {{Pkg|kmod}} 处理。参见:[[Kernel modules (简体中文)#黑名单]]。<br />
<br />
=== 文件系统挂载 ===<br />
<br />
默认行为是:在启动一个需要挂载特定分区的服务之前,系统自动检查并挂载分区。{{ic|/etc/fstab}} 中设定的网络文件系统(如 [[NFS (简体中文)|NFS]]、[[Samba (简体中文)|Samba]])无需配置即可正常工作,systemd 将确保网络文件系统在网络链接就绪后挂载。<br />
<br />
详情参阅:{{ic|man 5 systemd.mount}}。<br />
<br />
==== 自动挂载 ====<br />
<br />
* 如果 {{ic|/home}} 分区较大,可以让不依赖 {{ic|/home}} 分区的服务先启动。把下面的参数添加到 {{ic|/etc/fstab}} 文件中 {{ic|/home}} 项目的参数部分即可:<br />
<br />
noauto,x-systemd.automount<br />
<br />
这样 {{ic|/home}} 分区只有需要访问时才会被挂载。内核会缓存所有的文件操作,直到 {{ic|/home}} 分区准备完成。<br />
<br />
{{注意|这样做会使 {{ic|/home}} 的文件系统类型被识别为 {{ic|autofs}},造成 [[mlocate]] 查询时忽略该目录。实际加速效果因配置而异,所以请自己权衡是否需要。}}<br />
<br />
* 挂载远程文件系统也是同理。如果你仅想在需要的时候才挂载,也可以添加 {{ic|noauto,x-systemd.automount}} 参数。另外,可以设置 {{ic|1=x-systemd.device-timeout=#}} 参数,设置超时时间,以防止网络资源不能访问的时候浪费时间。<br />
<br />
* 如果你的加密文件系统需要密钥,则需要添加 {{ic|noauto}} 参数到 {{ic|/etc/crypttab}} 文件中的对应位置。systemd 开机的时候就不会打开这个加密设备,会一直等待到设备被访问时再使用密钥文件挂载。比如在使用加密RAID设备的时候可以节省一定的时间,因为 systemd 不必等到设备可用后才能访问。例如:<br />
<br />
{{hc|/etc/crypttab|<br />
data /dev/md0 /root/key noauto}}<br />
<br />
==== LVM ====<br />
<br />
如果装有不通过 [[Mkinitcpio (简体中文)|initramfs]] 激活的[[LVM (简体中文)|LVM]]卷,则需启动 {{ic|lvm-monitoring}} 服务(由 {{pkg|lvm2}} 软件包提供):<br />
<br />
# systemctl enable lvm-monitoring<br />
<br />
类似地,如果LVM卷在加密设备上,并且启动后才激活,则需启动 {{ic|lvm-on-crypt}} 服务(同样由 {{pkg|lvm2}} 软件包提供):<br />
<br />
# systemctl enable lvm-on-crypt<br />
<br />
=== ACPI 电源管理 ===<br />
systemd 能够处理某些电源相关的 [[Wikipedia:Advanced_Configuration_and_Power_Interface|ACPI]] 事件,通过 {{ic|/etc/systemd/logind.conf}} 的下列选项配置:<br />
* {{ic|HandlePowerKey}}:按下电源键后的动作<br />
* {{ic|HandleSleepKey}}:按下挂起键后的动作<br />
* {{ic|HandleHibernateKey}}: 按下休眠键后的动作<br />
* {{ic|HandleLidSwitch}}:合上笔记本盖后待机<br />
<br />
动作可以是 {{ic|ignore}}, {{ic|poweroff}}, {{ic|reboot}}, {{ic|halt}}, {{ic|suspend}}, {{ic|hibernate}} 或 {{ic|kexec}}.<br />
<br />
系统默认设置为:<br />
HandlePowerKey=poweroff<br />
HandleSuspendKey=suspend<br />
HandleHibernateKey=hibernate<br />
HandleLidSwitch=suspend<br />
<br />
如果不用图形界面,或者使用 [[i3 (简体中文)|i3]]、[[awesome (简体中文)|awesome]] 这样简单的桌面管理器时,systemd 可以替代 [[acpid (简体中文)|acpid]] 处理 ACPI 事件。<br />
<br />
{{注意|systemd 无法处理交流电源和电池 ACPI 事件,所以还得使用 [[Laptop Mode Tools (简体中文)|Laptop Mode Tools]] 或 [[acpid (简体中文)|acpid]] 工具。}}<br />
<br />
在当前版本的 systemd 中,这些 {{ic|Handle}} 选项将会被应用到整个系统当中,除非它们被别的程序——例如某个桌面环境中的电源管理器——给“阻止”(inhibited)。如果其它的程序没有阻止这些 {{ic|Handle}} ,你可能会先被 systemd 挂起你的系统,然后当系统被唤醒之后,电源管理器又会再次将系统挂起。<br />
<br />
{{警告|目前只有[[GNOME]] 和 [[KDE]] 支持 "inhibited" 命令。在其它的桌面管理器同样实现该功能之前,如果你想使用[[Xfce]]、[[acpid]] 或者其它程序来管理 ACPI 事件,你需要把 {{ic|Handle}} 选项设置为 {{ic|ignore}} 。包含该功能的新版本还在开发当中。}}<br />
<br />
{{注意|除了内核默认的待机支持后端(用于处理待机/休眠),systemd 也可以使用其他后端(比如 [[Uswsusp]] 或 [[TuxOnIce]])。}}<br />
<br />
==== 休眠时执行的脚本 ====<br />
<br />
使用 {{ic|systemctl suspend}}、{{ic|systemctl hibernate}} 或 {{ic|systemctl hybrid-sleep}} 命令执行待机/休眠时,systemd 不会调用 [[pm-utils (简体中文)|pm-utils]]。[[pm-utils (简体中文)|pm-utils]] 的钩子扩展(hook)——包括 [[pm-utils (简体中文)#建立你自己的钩子|自定义钩子]]——会失效。不过,systemd 提供了两种类似的待机/休眠时执行脚本的机制。<br />
<br />
===== 使用服务文件 =====<br />
<br />
可以将服务文件附在 suspend.target、hibernate.target 或 sleep.target 中,这样就能在待机/休眠前后执行某些操作。用户级操作和root/系统级操作应该使用不同的服务文件。要启用用户级服务文件,使用 {{ic|# systemctl enable suspend@<用户名> && systemctl enable resume@<用户名>}}。例如:<br />
<br />
{{hc|/etc/systemd/system/suspend@.service|2=<nowiki><br />
[Unit]<br />
Description=User suspend actions<br />
Before=sleep.target<br />
<br />
[Service]<br />
User=%I<br />
Type=forking<br />
Environment=DISPLAY=:0<br />
ExecStartPre= -/usr/bin/pkill -u %u unison ; /usr/local/bin/music.sh stop ; /usr/bin/mysql -e 'slave stop'<br />
ExecStart=/usr/bin/sflock<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
{{hc|/etc/systemd/system/resume@.service|2=<nowiki><br />
[Unit]<br />
Description=User resume actions<br />
After=suspend.target<br />
<br />
[Service]<br />
User=%I<br />
Type=simple<br />
ExecStartPre=/usr/local/bin/ssh-connect.sh<br />
ExecStart=/usr/bin/mysql -e 'slave start'<br />
<br />
[Install]<br />
WantedBy=suspend.target</nowiki>}}<br />
<br />
至于root/系统级服务(使用 {{ic|# systemctl enable root-suspend}} 激活):<br />
<br />
{{hc|/etc/systemd/system/root-resume.service|2=<nowiki><br />
[Unit]<br />
Description=Local system resume actions<br />
After=suspend.target<br />
<br />
[Service]<br />
Type=simple<br />
ExecStart=/usr/bin/systemctl restart mnt-media.automount<br />
<br />
[Install]<br />
WantedBy=suspend.target</nowiki>}}<br />
<br />
{{hc|/etc/systemd/system/root-suspend.service|2=<nowiki><br />
[Unit]<br />
Description=Local system suspend actions<br />
Before=sleep.target<br />
<br />
[Service]<br />
Type=simple<br />
ExecStart=-/usr/bin/pkill sshfs<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
上述服务文件的一些解释(详见 {{ic|man systemd.service}}):<br />
* 如果设置 {{ic|1=<nowiki>Type=OneShot</nowiki>}},那么可以使用多个 {{ic|1=<nowiki>ExecStart=</nowiki>}} 参数。否则只能写一个,替代方案是在 {{ic|ExecStartPre}} 中添加命令,或使用分号分隔不同命令(见第一个例子,分号前后的空格都是'''必须'''的)。 <br />
* 若命令前加上一个“-”(半角减号),则命令返回非零值时会被忽略、当作正常执行处理。<br />
* 有关调试,最好的方法是用 {{ic|journalctl}} 查看日志。<br />
<br />
===== 合并待机和唤醒服务文件 =====<br />
<br />
利用下面这种自定义的一体化待机和唤醒服务,使用单一的钩子扩展即可对不同操作(待机/休眠/混合休眠)的不同阶段(进入/唤醒)进行控制。<br />
<br />
例子和解释:<br />
<br />
{{hc|/etc/systemd/system/wicd-sleep.service|2=<nowiki><br />
[Unit]<br />
Description=Wicd sleep hook<br />
Before=sleep.target<br />
StopWhenUnneeded=yes<br />
<br />
[Service]<br />
Type=oneshot<br />
RemainAfterExit=yes<br />
ExecStart=-/usr/share/wicd/daemon/suspend.py<br />
ExecStop=-/usr/share/wicd/daemon/autoconnect.py<br />
<br />
[Install]<br />
WantedBy=sleep.target</nowiki>}}<br />
<br />
* {{ic|1=<nowiki>RemainAfterExit=yes</nowiki>}}:服务启动后,除非显式地停止,否则就认为是活动的。<br />
* {{ic|1=<nowiki>StopWhenUnneeded=yes</nowiki>}}:服务活动时,如果无其他服务依赖该服务,就停止它。在本例中,该服务会在 sleep.target 停止后停止活动。<br />
* 由于 sleep.target 会被 suspend.target、hibernate.target、hybrid-sleep.target 调用,且 sleep.target 本身设置了 StopWhenUnneeded,该服务文件对以上各种操作都是有效的。<br />
<br />
===== 使用 /usr/lib/systemd/system-sleep =====<br />
<br />
systemd 在待机/休眠时执行 {{ic|/usr/lib/systemd/system-sleep/}} 里的所有脚本,传递下面两个参数:<br />
<br />
* 参数1:若是准备进入待机/休眠状态,则为 {{ic|pre}};唤醒时为 {{ic|post}}。<br />
* 参数2:事件名称,{{ic|suspend}},{{ic|hibernate}} 或 {{ic|hybrid-sleep}}。<br />
<br />
systemd 会同时执行所有脚本,而不是像 [[pm-utils (简体中文)|pm-utils]] 那样顺序执行。<br />
<br />
脚本输出会记录在相关服务({{ic|systemd-suspend.service}}、{{ic|systemd-hibernate.service}} 或 {{ic|systemd-hybrid-sleep.service}})中。通过[[#日志|日志]]查看:<br />
# journalctl -b -u systemd-suspend<br />
<br />
脚本示范:<br />
<br />
{{hc|/usr/lib/systemd/system-sleep/example.sh|<br />
#!/bin/sh<br />
case $1/$2 in<br />
pre/*)<br />
echo "进入 $2 状态..."<br />
;;<br />
post/*)<br />
echo "从 $2 状态唤醒..."<br />
;;<br />
esac}}<br />
<br />
记得添加可执行权限: <br />
# chmod a+x /usr/lib/systemd/system-sleep/example.sh<br />
<br />
详情参见 {{ic|man 7 systemd.special}} 和 {{ic|man 8 systemd-sleep}}。<br />
<br />
=== 临时文件 ===<br />
{{ic|/usr/lib/tmpfiles.d/}} 和 {{ic|/etc/tmpfiles.d/}} 中的文件描述了 systemd-tmpfiles 如何创建、清理、删除临时文件和目录,这些文件和目录通常存放在 {{ic|/run}} 和 {{ic|/tmp}} 中。配置文件名称为 {{ic|/etc/tmpfiles.d/<program>.conf}}。此处的配置能覆盖 {{ic|/usr/lib/tmpfiles.d/}} 目录中的同名配置。<br />
<br />
临时文件通常和服务文件同时提供,以生成守护进程需要的文件和目录。例如 [[Samba (简体中文)|Samba]] 服务需要目录 {{ic|/run/samba}} 存在并设置正确的权限位,就象这样:<br />
{{hc|/usr/lib/tmpfiles.d/samba.conf|<br />
D /run/samba 0755 root root<br />
}}<br />
<br />
此外,临时文件还可以用来在开机时向特定文件写入某些内容。比如,要禁止系统从USB设备唤醒,利用旧的 {{ic|/etc/rc.local}} 可以用 {{ic|echo USBE > /proc/acpi/wakeup}},而现在可以这么做:<br />
{{hc|/etc/tmpfiles.d/disable-usb-wake.conf|<br />
w /proc/acpi/wakeup - - - - USBE<br />
}}<br />
<br />
详情参见 {{ic|man 5 tmpfiles.d}}。<br />
<br />
=== 单元(unit) ===<br />
一个单元配置文件可以描述如下内容之一:系统服务、socket、系统设备、挂载点、交换分区/文件、启动目标(target)、文件系统路径、由 systemd 管理的计时器。文件格式受 .desktop 文件启发,而最初起源是 Windows 下的 .ini 文件。<br />
<br />
详情参阅 {{ic|man 5 systemd.unit}}.<br />
<br />
== 从 initscripts 迁移到 systemd ==<br />
<br />
=== initscripts 模拟 ===<br />
<br />
{{Pkg|initscripts}} 软件包提供了 systemd 和 Arch 传统配置的整合。当同时安装了{{Pkg|initscripts}}和systemd时,若以systemd启动系统,systemd将执行以下操作:<br />
<br />
# 解析{{ic|/etc/rc.conf}}中的{{ic|DAEMONS}}数组,并在系统启动时,启动所有列出的守护进程<br />
# 在启动过程中执行{{ic|/etc/rc.local}}<br />
# 在系统关闭过程中执行{{ic|/etc/rc.local.shutdown}}<br />
<br />
Initscripts模拟仅仅是作为用户迁移到systemd时的过渡措施,这一方案最终将'''不再可行'''。原生的systemd并不依赖于{{ic|rc.conf}}文件的集中式配置,因此推荐使用[[#原生 systemd 配置文件|原生的systemd配置文件]]。原生systemd配置文件比{{ic|/etc/rc.conf}}具有更高优先级。<br />
<br />
{{注意|替换{{ic|/etc/rc.local}}的推荐方式是针对系统启动时要运行的程序编写自定义的service文件,详见相关[[#自己编写_.service_文件|章节]].}}<br />
<br />
{{注意|如果你曾经在{{ic|/etc/inittab}}中禁用了以{{keypress|Ctrl+Alt+Del}}重启系统,则需要以root执行{{ic|systemctl mask ctrl-alt-del.target}}以使其对systemd重新生效。}}<br />
<br />
{{警告|如果同时安装 systemd(197-4 及后续版本)和 initscripts,那么 {{ic|/etc/rc.local}} 脚本会导致启动过程无法结束(原因是 getty@tty1.service 自 197-4 版本起不再等待 rc-local.service 执行,getty 无法启动)。请删除或重命名 {{ic|/etc/rc.local}}。}}<br />
<br />
=== 迁移 DAEMONS 列表 ===<br />
<br />
若要用纯 systemd 环境,则应当删除 {{ic|/etc/rc.conf}} 文件,并完全通过 {{ic|systemctl}} 命令来启动服务。对于 {{ic|DAEMONS}} 列表里面的每个 {{ic|<服务名>}},现在可以用以下命令设置成自动启动:<br />
<br />
# systemctl enable <服务名>.service<br />
<br />
{{小贴士|[[Daemons List|这里]]有一份 initscripts 和 systemd 下常见服务的对应列表。}}<br />
<br />
如果提示 {{ic|<service_name>.service}} 不存在:<br />
<br />
* 很有可能是 systemd 用了不同的服务名。比如 {{ic|crond}} 服务替换成 {{ic|cronie.service}};{{ic|alsa}} 服务替换成了 {{ic|alsa-store.service}} 和 {{ic|alsa-restore.service}} 两个服务。另一个值得注意的是 {{ic|network}} 服务,它被替换为一整套服务文件(详情参见 [[Configuring_Network_(简体中文)]])。<br />
* 另外,有些程序尚未提供 .service 文件,还得继续用 {{ic|rc.conf}} 来配置开机自动启动。<br />
<br />
{{小贴士|用下面这个命令来查看已安装软件包包含的服务文件:<br />
$ pacman -Ql cronie<br />
[...]<br />
cronie /etc/rc.d/crond # DAEMONS 列表中使用的 initscript 服务 (原生 systemd 配置中不再使用)<br />
[...]<br />
cronie /usr/lib/systemd/system/cronie.service # 原生 systemd 服务文件<br />
[...]<br />
}}<br />
<br />
* 有些服务不需要用户明确配置自启动。比如安装 {{ic|dbus-core}} 软件包后 {{ic|dbus.service}} 就会自动被配置为自启动。而 {{ic|alsa-store.service}} 和 {{ic|alsa-restore.service}} 也会由 systemd 自动启动。可以用 {{ic|systemctl status <服务名>}} 命令来查看服务列表跟它们的运行状态。<br />
<br />
== systemd 基本工具 ==<br />
<br />
检视和控制systemd的主要命令是{{ic|systemctl}}。该命令可用于查看系统状态和管理系统及服务。详见{{ic|man 1 systemctl}}。<br />
<br />
{{小贴士|在 {{ic|systemctl}} 参数中添加 {{ic|-H <用户名>@<主机名>}} 可以实现对其他机器的远程控制。该过程使用 [[SSH (简体中文)|SSH]] 链接。}}<br />
<br />
{{注意|{{ic|systemadm}} 是 systemd 的官方图形前端。由 [[AUR (简体中文)|AUR]] 中的软件包 {{AUR|systemd-ui-git}} 提供。}}<br />
<br />
=== 分析系统状态 ===<br />
<br />
输出激活的单元:<br />
<br />
$ systemctl<br />
<br />
以下命令等效:<br />
<br />
$ systemctl list-units<br />
<br />
输出运行失败的单元:<br />
<br />
$ systemctl --failed<br />
<br />
所有可用的单元文件存放在 {{ic|/usr/lib/systemd/system/}} 和 {{ic|/etc/systemd/system/}} 目录(后者优先级更高)。查看所有已安装服务:<br />
$ systemctl list-unit-files<br />
<br />
=== 使用单元 ===<br />
一个单元可以是系统服务({{ic|.service}})、挂载点({{ic|.mount}})、sockets({{ic|.sockets}})。<br />
<br />
使用 {{ic|systemctl}} 控制单元时,通常需要使用单元文件的全名,包括扩展名(例如 {{ic|sshd.service}})。但是有些单元可以在{{ic|systemctl}}中使用简写方式。<br />
* 如果无扩展名,systemctl 默认把扩展名当作 {{ic|.service}}。例如 {{ic|netcfg}} 和 {{ic|netcfg.service}} 是等价的。<br />
* 挂载点会自动转化为相应的 {{ic|.mount}} 单元。例如 {{ic|/home}} 等价于 {{ic|home.mount}}。<br />
* 设备会自动转化为相应的 {{ic|.device}} 单元,所以 {{ic|/dev/sda2}} 等价于 {{ic|dev-sda2.device}}。<br />
<br />
立即激活单元:<br />
<br />
# systemctl start <单元><br />
<br />
立即停止单元:<br />
# systemctl stop <单元><br />
<br />
重启单元:<br />
<br />
# systemctl restart <单元><br />
<br />
命令单元重新读取配置:<br />
<br />
# systemctl reload <单元><br />
<br />
输出单元运行状态:<br />
<br />
$ systemctl status <单元><br />
<br />
检查单元是否配置为自动启动:<br />
$ systemctl is-enabled <单元><br />
<br />
开机自动激活单元:<br />
<br />
# systemctl enable <单元><br />
<br />
{{注意|如果服务没有{{ic|Install}}段落,一般意味着应该通过其它服务自动调用它们。如果真的需要手动安装,可以直接连接服务,如下(将{{ic|foo}}替换为真实的服务名):<br />
{{bc|# ln -s /usr/lib/systemd/system/foo.service /etc/systemd/system/graphical.target.wants/}}<br />
}}<br />
<br />
取消开机自动激活单元:<br />
<br />
# systemctl disable <单元>}}<br />
<br />
显示单元的手册页(必须由单元文件提供):<br />
<br />
# systemctl help <单元><br />
<br />
重新载入 systemd,扫描新的或有变动的单元:<br />
# systemctl daemon-reload<br />
<br />
=== 电源管理 ===<br />
<br />
安装 {{ic|polkit}} 后才可使用电源管理。<br />
<br />
如果你正登录在一个本地的{{ic|systemd-logind}}用户会话,且当前没有其它活动的会话,那么以下命令无需root权限即可执行。否则(例如,当前有另一个用户登录在某个tty),systemd 将会自动请求输入root密码。<br />
<br />
重启:<br />
<br />
$ systemctl reboot<br />
<br />
退出系统并停止电源:<br />
<br />
$ systemctl poweroff<br />
<br />
待机:<br />
<br />
$ systemctl suspend<br />
<br />
休眠:<br />
<br />
$ systemctl hibernate<br />
<br />
混合休眠模式(同时休眠到硬盘并待机):<br />
<br />
$ systemctl hybrid-sleep<br />
<br />
== 启动桌面环境 ==<br />
<br />
=== 使用登录管理器 ===<br />
<br />
通过启动[[Display Manager (简体中文)|登录管理器]](或称显示管理器),即可进行图形界面登录。目前,Arch 提供了 [[GDM (简体中文)|GDM]]、[[KDM (简体中文)|KDM]]、[[SLiM (简体中文)|SLiM]]、[[XDM (简体中文)|XDM]]、[[LXDM (简体中文)|LXDM]] 和 [[LightDM (简体中文)|LightDM]] 的 systemd 服务文件。以 KDM 为例,配置开机启动:<br />
# systemctl enable kdm.service<br />
<br />
执行上述命令后,登录管理器应当能正常工作了。如果不是的话,很可能是因为你修改了{{ic|default.target}}。默认情况应当如下:<br />
<br />
{{hc|# ls -l /etc/systemd/system/default.target|/etc/systemd/system/default.target -> /usr/lib/systemd/system/graphical.target}}<br />
<br />
删除被修改的 {{ic|default.target}} 即可,systemd 会自动使用默认配置(即 {{ic|graphical.target}}):<br />
<br />
# rm /etc/systemd/system/default.target<br />
<br />
=== 使用 systemd-logind ===<br />
<br />
{{注意|自 2012-10-30 起, [[ConsoleKit]] 已被 [https://www.archlinux.org/news/consolekit-replaced-by-logind/ systemd-logind 取代] ,作为登录到桌面环境的默认方式。}}<br />
<br />
可使用 {{ic|loginctl}} 来查看用户会话的状态。所有 [[PolicyKit]] 操作,如挂起系统、挂载外部驱动器,都无需配置即可使用。<br />
<br />
$ loginctl show-session $XDG_SESSION_ID<br />
<br />
== 自己编写 .service 文件 ==<br />
''参见:[[Systemd/Services]]''<br />
<br />
=== 处理依赖关系 ===<br />
<br />
使用systemd时,可通过正确编写单元配置文件来解决其依赖关系。典型的情况是,单元{{ic|A}}要求单元{{ic|B}}在{{ic|A}}启动之前运行。在此情况下,向单元{{ic|A}}配置文件中的 {{ic|[Unit]}} 段添加 {{ic|1=Requires=B}} 和 {{ic|1=After=B}} 即可。若此依赖关系是可选的,可添加 {{ic|1=Wants=B}} 和 {{ic|1=After=B}}。请注意 {{ic|1=Wants=}} 和 {{ic|1=Requires=}} 并不意味着 {{ic|1=After=}},即如果 {{ic|1=After=}} 选项没有制定,这两个单元将被并行启动。<br />
<br />
依赖关系通常被用在服务(service)而不是[[#目标(target)|目标(target)]]上。例如, {{ic|network.target}} 一般会被某个配置网络接口的服务引入,所以,将自定义的单元排在该服务之后即可,因为 {{ic|network.target}} 已经启动。<br />
<br />
=== 启动方式 ===<br />
<br />
编写自定义的service文件时,可以选择几种不同的服务启动方式。启动方式可通过配置文件 {{ic|[Service]}} 段中的 {{ic|1=Type=}} 参数进行设置。具体的参数说明请参阅 {{ic|man systemd.service}} 。<br />
<br />
* {{ic|1=Type=simple}}:systemd认为该服务将立即启动。服务进程不会fork。如果该服务要启动其他服务,不要使用此类型启动,除非该服务是socket激活型。<br />
* {{ic|1=Type=forking}}:systemd认为当该服务进程fork,且父进程退出后服务启动成功。对于常规的守护进程(daemon),除非你确定此启动方式无法满足需求,使用此类型启动即可。使用此启动类型应同时指定 {{ic|1=PIDFile=}},以便systemd能够跟踪服务的主进程。<br />
* {{ic|1=Type=oneshot}}:这一选项适用于只执行一项任务随后退出的服务。你可能需要同时设置 {{ic|1=RemainAfterExit=yes}} 使得 systemd 在服务进程退出之后仍然认为服务处于激活状态。<br />
* {{ic|1=Type=notify}}:与 {{ic|1=Type=simple}} 相同,但约定服务会在就绪后向 systemd 发送一个信号。这一通知的实现由 {{ic|libsystemd-daemon.so}} 提供。<br />
* {{ic|1=Type=dbus}}:若以此方式启动,当指定的 {{ic|BusName}} 出现在DBus系统总线上时,systemd认为服务就绪。<br />
<br />
=== 替换现存单元文件 ===<br />
<br />
目录 {{ic|/etc/systemd/system/}} 中的单元配置文件比 {{ic|/usr/lib/systemd/system/}} 目录下的文件拥有更高优先级。<br />
若需要定制某个单元的启动(使配置不受软件包更新的影响),请将现有的单元配置文件从 {{ic|/usr/lib/}} 复制到 {{ic|/etc/}} 目录下,并在此目录中修改。或者,可以使用 {{ic|.include}} 来解析一个现有的配置文件,并覆盖原选项或添加新选项。例如,若想要某个服务的配置文件中添加额外依赖关系,可以编写:<br />
<br />
{{hc|/etc/systemd/system/<服务名>.service|2=<br />
.include /usr/lib/systemd/system/<服务名>.service<br />
<br />
[Unit]<br />
Requires=<新依赖><br />
After=<新依赖>}}<br />
<br />
然后运行以下命令使更改生效:<br />
<br />
# systemctl reenable <单元><br />
# systemctl restart <单元><br />
<br />
{{小贴士|可以用 {{ic|systemd-delta}} 命令来查看哪些单元文件被覆盖、哪些被修改。}}<br />
<br />
=== 单元配置文件的 vim 语法高亮支持 ===<br />
<br />
可从 [[Official Repositories|官方仓库]] 安装 {{Pkg|vim-systemd}} 软件包,使 unit 配置文件在 [[Vim]] 下支持语法高亮。<br />
<br />
== 目标(target) ==<br />
启动级别(runlevel)是一个旧的概念。现在,systemd 引入了一个和启动级别功能相似又不同的概念——目标(target)。不像数字表示的启动级别,每个''目标''都有名字和独特的功能,并且能同时启用多个。一些''目标''继承其他''目标''的服务,并启动新服务。systemd 提供了一些模仿 sysvinit 启动级别的''目标'',仍可以使用旧的 {{ic|telinit 启动级别}} 命令切换。<br />
<br />
=== 获取当前目标 ===<br />
不要使用 {{ic|runlevel}} 命令了:<br />
$ systemctl list-units --type=target<br />
<br />
=== 创建新目标 ===<br />
在 Fedora 中,启动级别 0、1、3、5、6 都被赋予特定用途,并且都对应一个 systemd 的''目标''。然而,没有什么很好的移植用户定义的启动级别(2、4)的方法。要实现类似功能,可以以原有的启动级别为基础,创建一个新的''目标'' {{ic|/etc/systemd/system/<新目标>}}(可以参考 {{ic|/usr/lib/systemd/system/graphical.target}}),创建 {{ic|/etc/systemd/system/<新目标>.wants}} 目录,向其中加入额外服务的链接(指向 {{ic|/usr/lib/systemd/system/}} 中的单元文件)。<br />
<br />
=== 目标表 ===<br />
{| border="1"<br />
!SysV 启动级别!!Systemd 目标!!注释<br />
|-<br />
| 0 || runlevel0.target, poweroff.target || 中断系统(halt)<br />
|-<br />
| 1, s, single || runlevel1.target, rescue.target || 单用户模式<br />
|-<br />
| 2, 4 || runlevel2.target, runlevel4.target, multi-user.target || 用户自定义启动级别,通常识别为级别3。<br />
|-<br />
| 3 || runlevel3.target, multi-user.target || 多用户,无图形界面。用户可以通过终端或网络登录。<br />
|-<br />
| 5 || runlevel5.target, graphical.target || 多用户,图形界面。继承级别3的服务,并启动图形界面服务。<br />
|-<br />
| 6 || runlevel6.target, reboot.target || 重启<br />
|-<br />
| emergency || emergency.target || 急救模式(Emergency shell)<br />
|-<br />
|}<br />
<br />
=== 切换启动级别/目标 ===<br />
systemd 中,启动级别通过“目标单元”访问。通过如下命令切换:<br />
# systemctl isolate graphical.target<br />
该命令对下次启动无影响。等价于{{ic|telinit 3}} 或 {{ic|telinit 5}}。<br />
<br />
=== 修改默认启动级别/目标 ===<br />
开机启动进的目标是 {{ic|default.target}},默认链接到 {{ic|graphical.target}} (大致相当于原来的启动级别5)。可以通过[[Kernel parameters (简体中文)|内核参数]]更改默认启动级别:<br />
<br />
{{小贴士|可以省略扩展名 {{ic|.target}}。}}<br />
<br />
* {{ic|1=systemd.unit=multi-user.target}} (大致相当于级别3)<br />
* {{ic|1=systemd.unit=rescue.target}} (大致相当于级别1)<br />
<br />
另一个方法是修改 {{ic|default.target}}。可以通过 {{ic|systemctl}} 修改它:<br />
<br />
# systemctl enable multi-user.target<br />
<br />
命令执行情况由 {{ic|systemctl}} 显示:链接 {{ic|/etc/systemd/system/default.target}} 被创建,指向新的默认启动级别。该方法当且仅当目标配置文件中有以下内容时有效:<br />
<br />
[Install]<br />
Alias=default.target<br />
<br />
目前,{{ic|multi-user.target}}、{{ic|graphical.target}} 都包含这段内容。<br />
<br />
== 日志 ==<br />
自版本 38 起,systemd 提供了自己日志系统(logging system),称为 journal. 使用 systemd 日志,无需额外安装日志服务(syslog)。读取日志的命令:<br />
# journalctl<br />
<br />
默认情况下(当 {{ic|Storage&#61;}} 在文件 {{ic|/etc/systemd/journald.conf}} 中被设置为 {{ic|auto}}),日志记录将被写入 {{ic|/var/log/journal/}}。若目录 {{ic|/var/log/journal/}} 不存在(例如,被用户或某个程序删除),systemd将'''不会'''自动创建它,而是将日志记录写入 {{ic|/run/systemd/journal}}。这意味着,系统重启后日志将丢失。<br />
<br />
=== 过滤输出 ===<br />
<br />
{{ic|journalctl}}可以根据特定字段过滤输出,例如:<br />
<br />
显示本次启动后的所有日志:<br />
<br />
# journalctl -b<br />
<br />
不过,一般大家更关心的不是本次启动后的日志,而是上次启动时的(例如,刚刚系统崩溃了)。目前还没有这项功能,正在 [http://comments.gmane.org/gmane.comp.sysutils.systemd.devel/6608 systemd-devel@lists.freedesktop.org] 讨论中。<br />
<br />
目前的折中方案是:<br />
<br />
# journalctl --since=today | tac | sed -n '/-- Reboot --/{n;:r;/-- Reboot --/q;p;n;b r}' | tac<br />
<br />
以上命令输出本日内的所有启动信息。但要注意,如果日志很多,该命令执行时间会比较漫长。<br />
<br />
动态跟踪最新信息:<br />
# journalctl -f<br />
<br />
显示特定程序的所有消息:<br />
{{bc|# journalctl /usr/lib/systemd/systemd}}<br />
<br />
显示特定进程的所有消息:<br />
{{bc|1=# journalctl _PID=1}}<br />
<br />
显示指定单元的所有消息:<br />
# journalctl -u netcfg<br />
<br />
详情参阅{{ic|man journalctl}}、{{ic|man systemd.journal-fields}},以及 Lennert 的这篇[http://0pointer.de/blog/projects/journalctl.html 博文]。<br />
<br />
=== 日志大小限制 ===<br />
<br />
如果按上面的操作保留日志的话,默认日志最大限制为所在文件系统容量的 10%,即:如果 {{ic|/var/log/journal}} 储存在 50GiB 的根分区中,那么日志最多存储 5GiB 数据。可以修改 {{ic|/etc/systemd/journald.conf}} 中的 {{ic|SystemMaxUse}} 来指定该最大限制。如限制日志最大 50MiB:<br />
<br />
SystemMaxUse=50M<br />
<br />
详情参见 {{ic|man journald.conf}}.<br />
<br />
=== 配合 syslog 使用 ===<br />
systemd 提供了 socket {{ic|/run/systemd/journal/syslog}},以兼容传统日志服务。所有系统信息都会被传入。要使传统日志服务工作,需要让服务链接该 socket,而非 {{ic|/dev/log}}([http://lwn.net/Articles/474968/ 官方说明])。Arch 软件仓库中的 {{pkg|syslog-ng}} 已经包含了需要的配置。<br />
<br />
设置开机启动 syslog-ng:<br />
# systemctl enable syslog-ng<br />
<br />
== 优化 ==<br />
<br />
参阅:[[Improve Boot Performance (简体中文)]]。<br />
<br />
=== 启动过程分析 ===<br />
systemd 提供了一个分析启动过程的工具 —— {{ic|systemd-analyze}}。可以用它看看哪些单元拖慢了开机过程,并据此进行优化。安装 {{Pkg|python2-dbus}}、{{Pkg|python2-cairo}} 软件包后该工具才能工作。<br />
<br />
查看开机过程在内核/用户空间消耗的时间:<br />
$ systemd-analyze<br />
{{小贴士|如果在 {{ic|/etc/mkinitcpio.conf}} 的 {{ic|HOOKS}} 数组添加 {{ic|timestamp}},并重新生成启动内存镜像({{ic|mkinitcpio -p linux}}),{{ic|systemd-analyze}} 还可以显示处理该启动镜像花费的时间。}}<br />
<br />
按照耗费时间顺序,输出启动每个单元耗费的时间:<br />
$ systemd-analyze blame<br />
<br />
生成类似于 [[Bootchart (简体中文)|bootchart]] 的开机过程图表:<br />
$ systemd-analyze plot > plot.svg<br />
<br />
==== 使用 systemd-bootchart ====<br />
<br />
自2012年10月17日,bootchart 工具已经合并进 systemd 中,使用方法和原来的 bootchart 大同小异,添加下列内容到内核参数即可:<br />
<br />
initcall_debug printk.time=y init=/usr/lib/systemd/systemd-bootchart<br />
<br />
==== 使用 bootchart2 ====<br />
<br />
由于没有办法在内核参数设置两个 init,所以不能使用源里的 bootchart。不过,[[AUR (简体中文)|AUR]] 软件包 {{AUR|bootchart2}} 提供了一个的 systemd 服务,这样就可以和 systemd 一起使用。安装后启用即可:<br />
{{bc|# systemctl enable bootchart.service}}<br />
详情参阅 [https://github.com/mmeeks/bootchart bootchart 文档]。<br />
<br />
=== 预读 ===<br />
systemd 自己实现了一个 readahead,可以用来提高开机效率。不过,效果会因内核版本和硬件情况而不同(极少数情况下还会变慢)。开启 readahead:<br />
<br />
# systemctl enable systemd-readahead-collect.service systemd-readahead-replay.service<br />
<br />
要知道,readahead 的超级牛力只有在重启几次后才会显现。<br />
<br />
== 疑难解答 ==<br />
<br />
=== 关机/重启十分缓慢 ===<br />
<br />
如果关机特别慢(甚至跟死机了一样),很可能是某个拒不退出的服务在作怪。systemd 会等待一段时间,然后再尝试杀死它。请阅读[http://freedesktop.org/wiki/Software/systemd/Debugging#Shutdown_Completes_Eventually 这篇文章],确认你是否是该问题受害者。<br />
<br />
=== 短时进程无日志记录 ===<br />
<br />
若 {{ic|journalctl -u foounit.service}} 没有显示某个短时进程的任何输出,那么改用 PID 试试。例如,若 systemd-modules-load.service 执行失败,那么先用 {{ic|systemctl status systemd-modules-load}} 查询其 PID(比如是123),然后检索该 PID 相关的日志 {{ic|journalctl -b _PID&#61;123}}。运行时进程的日志元数据(诸如 _SYSTEMD_UNIT 和 _COMM)被乱序收集在 {{ic|/proc}} 目录。要修复该问题,必须修改内核,使其通过套接字连接来提供上述数据,该过程类似于 SCM_CREDENTIALS。<br />
<br />
== 相关资源 ==<br />
<br />
*[http://www.freedesktop.org/wiki/Software/systemd 官方网站]<br />
*[http://0pointer.de/public/systemd-man/ man 手册页]<br />
*[http://freedesktop.org/wiki/Software/systemd/Optimizations systemd 优化]<br />
*[http://www.freedesktop.org/wiki/Software/systemd/FrequentlyAskedQuestions 常见问题 FAQ]<br />
*[http://www.freedesktop.org/wiki/Software/systemd/TipsAndTricks 小技巧]<br />
*[http://0pointer.de/public/systemd-ebook-psankar.pdf systemd for Administrators (PDF)]<br />
*[http://fedoraproject.org/wiki/Systemd Fedora 项目对 systemd 的介绍]<br />
*[http://fedoraproject.org/wiki/How_to_debug_Systemd_problems 如何调试 systemd 故障]<br />
*''The H Open'' 杂志上的[http://www.h-online.com/open/features/Control-Centre-The-systemd-Linux-init-system-1565543.html 两][http://www.h-online.com/open/features/Booting-up-Tools-and-tips-for-systemd-1570630.html 篇]科普文章<br />
*[http://0pointer.de/blog/projects/systemd.html Lennart 的博文]<br />
*[http://0pointer.de/blog/projects/systemd-update.html 更新报告]<br />
*[http://0pointer.de/blog/projects/systemd-update-2.html 更新报告2]<br />
*[http://0pointer.de/blog/projects/systemd-update-3.html 更新报告3]<br />
*[http://0pointer.de/blog/projects/why.html 最新动态]<br />
*[https://fedoraproject.org/wiki/SysVinit_to_Systemd_Cheatsheet/zh Fedora Wiki 上的 SysVinit 和 systemd 命令对比表]</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Wine_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=245632
Wine (简体中文)
2013-01-30T02:15:13Z
<p>Cuihao: /* 第三方界面 */</p>
<hr />
<div>[[Category:Wine (简体中文)]]<br />
[[cs:Wine]]<br />
[[de:Wine]]<br />
[[en:Wine]]<br />
[[es:Wine]]<br />
[[fr:Wine]]<br />
[[it:Wine]]<br />
[[ja:Wine]]<br />
[[ru:Wine]]<br />
[[zh-TW:Wine]]<br />
{{TranslationStatus (简体中文)|Wine|2013-01-30|244275}}<br />
{{Article summary start}}<br />
{{Article summary text|[[Wikipedia:Wine (software)|Wine]] 是类UNIX系统下运行微软Windows程序的"兼容层"。在Wine中运行的Windows程序,就如同运行原生Linux程序一样,不会有模拟器那样的性能问题。}}<br />
{{Article summary heading|Related}}<br />
{{Article summary wiki|Steam}}<br />
{{Article summary wiki|CrossOver}}<br />
{{Article summary end}}<br />
<br />
获取更详细的介绍请浏览[http://www.winehq.org/ 项目官方网站]和[http://wiki.winehq.org/ wiki]页面。<br />
<br />
== 安装 ==<br />
{{警告|如果您的账户能浏览某些文件或资源,Wine运行的程序也可以。Wine不是[[wikipedia:Sandbox_%28computer_security%29 |沙箱]]。如果很重视安全,请考虑使用[[wikipedia:Virtualization|虚拟化]]。}}<br />
<br />
最新版的Wine软件包收录在:i686平台的<nowiki>[</nowiki>[[community]]]仓库,x86_64的<nowiki>[</nowiki>[[multilib]]]仓库。<br />
<br />
# pacman -S wine<br />
<br />
另外,您可能需要安装{{pkg|wine_gecko}}以模拟 Internet Explorer 支持(很多网络程序需要):<br />
# pacman -S wine_gecko<br />
<br />
或使用[http://wiki.winehq.org/Mono Mono] 以使用 .NET 程序。[[AUR]]中的软件包{{AUR|wine-mono}}会将安装程序放到{{ic|/usr/share/wine/mono/}},这样Wine就可以自动检查到,不会再次下载。<br />
<br />
'''平台差异'''<br />
<br />
默认的Wine是32位的程序,也是i686的Arch软件包。所以它不能运行64位的Windows程序(反正是罕见的)。<br />
<br />
然而,x86_64的Wine软件包目前以{{ic| --enable-win64}}方式编译。这个参数激活了[[Wikipedia:WoW64|WoW64]]的Wine版本。<br />
<br />
*在Windows中,这个复杂的子系统允许用户同时使用32位和64位的Windows程序,甚至是在同一目录。<br />
<br />
*在Wine中,用户将必须建立单独分开的目录/前缀。这项Wine功能仍是试验阶段,并建议用户使用一个win32{{ic|WINEPREFIX}}。浏览[http://wiki.winehq.org/Wine64 Wine64]以获取有关这个的详细信息。<br />
<br />
总结一下,配置{{ic|1=WINEARCH=win32}}后,x86_64平台的Arch和i686平台的Arch完全相同。<br />
<br />
{{注意|如果在64位环境中执行{{ic|winetricks}}或其它程序出现问题,请试试创建一个新的32位{{ic|WINEPREFIX}}. 参见下面的[[#使用 WINEARCH]]}}<br />
<br />
== 配置 ==<br />
Wine默认将配置文件和安装的Windows程序保存在{{ic|~/.wine}}。这样的目录称为一个"Wine prefix"或"Wine bottle"(保留原文,下文称“系统目录”)。每次运行Windows程序(包括内置程序,如{{ic|winecfg}})时,系统目录会自动创建(如果缺失)或更新。系统目录中存放有相当于Windows下{{ic | C:\}}C盘(更确切的说应是系统盘)的文件夹。<br />
<br />
通过设置{{ic|WINEPREFIX}}环境变量,可以更改Wine系统目录的位置。如果希望让不同的Windows程序使用不同的系统环境或配置,这一变量会非常有用。<br />
<br />
例如,如果您使用 <br />
{{ic |1= $ env WINEPREFIX=~/.win-a wine-A程序.exe}}参数来运行一个程序。另一个使用<br />
{{ic |1= $ env WINEPREFIX=~/.win-b wine-B程序.exe}}参数,这两个程序将使用独立的C盘和注册表配置。<br />
<br />
以下命令会建立一个默认的系统目录,且不启动任何Windows程序:<br />
$ env WINEPREFIX=~/.customprefix wineboot -u<br />
<br />
配置Wine的方式通常有:<br />
* [http://wiki.winehq.org/winecfg winecfg]是Wine的图形界面配置程序。控制台下调用{{ic|$ winecfg}}(或指定系统目录:{{ic|1=$ WINEPREFIX=~/.系统目录 winecfg}})即可启动<br />
* [http://wiki.winehq.org/control control.exe]是Windows控制面板的Wine实现,通过{{ic|$ wine control}}命令启动<br />
* [http://wiki.winehq.org/regedit regedit]是Wine的注册表编辑器,比较前两者,该工具能配置更多东西。部分常用键值参见:[http://wiki.winehq.org/UsefulRegistryKeys WineHQ's article on Useful Registry Keys]<br />
<br />
=== 使用 WINEARCH ===<br />
对于64位用户,如果使用[multilib]仓库里的Wine,默认创建的系统目录是64位环境的。若想使用纯32位环境,修改{{ic|WINEARCH}} 变量win32为即可:<br />
{{ic |1=$ WINEARCH=win32 winecfg}}这样就会生成32位Wine环境。若不设置{{ic|WINEARCH}}得到的就是64位环境。<br />
<br />
通过{{ic|WINEPREFIX}}变量,在不同的系统目录分别创建32位和64位环境:<br />
$ WINEARCH=win32 WINEPREFIX=~/win32 winecfg <br />
$ WINEPREFIX=~/win64 winecfg<br />
<br />
{{注意|系统目录创建过程中,64位版本的wine将视全部目录如同64位系统目录,也将不会在已存在的目录中创建任何32位的.创建32位系统目录,您必须让Wine创建指定的{{ic|WINEPREFIX}}目录。}}<br />
<br />
winetricks也接受{{ic|WINEPREFIX}}变量,以安装Steam为例:<br />
env WINEARCH=win32 WINEPREFIX=~/.local/share/wineprefixes/steam winetricks steam<br />
{{注意|不必手动在{{ic|wineprefixes}}文件夹下建立steam文件夹,Wine会自动创建不存在的系统目录.}}<br />
<br />
=== 显卡驱动 ===<br />
使用Wine运行Windows游戏时,可能需要高性能的显卡驱动。[[Nvidia (简体中文)|Nvidia]]、[[ATI (简体中文)|Amd/ATI]]用户最好使用闭源驱动。[[Intel (简体中文)|Intel]]显卡用户也可以选择开源驱动,它已经非常成熟。<br />
<br />
要是显卡驱动有问题或者相关配置有误,控制台用Wine运行某些程序时会输出:<br />
Direct rendering is disabled, most likely your OpenGL drivers have not been installed correctly<br />
<br />
x86-64用户需要从[multilib]或[[AUR (简体中文)|AUR]]安装额外的32位库:<br />
* '''NVIDIA''':{{ic|# pacman -S lib32-nvidia-utils}} 关于旧版本lib32-nvidia-utils(例如nvidia-96xx驱动),请到[https://aur.archlinux.org/packages.php?K=lib32-nvidia-utils 这里]查找。<br />
<br />
* '''NVIDIA (使用nouveau-dri驱动)''': {{ic|# pacman -S lib32-nouveau-dri}} <br />
<br />
* '''Intel''':{{ic|# pacman -S lib32-intel-dri}} 运行Wine时需要手动添加{{ic|1=LIBGL_DRIVERS_PATH=/usr/lib32/xorg/modules/dri}}<br />
<br />
* '''AMD/ATI''':{{ic|# pacman -S lib32-ati-dri}} 如果用的是闭源驱动:{{ic|# pacman -S {{Pkg|lib32-catalyst-utils}}}}。<br />
<br />
{{注意|安装上述软件包后,可能需要重启X才能生效!}}<br />
<br />
=== 声音 ===<br />
Wine程序有可能遇到某些声音问题。首先,确保{{ic|winecfg}}中只启用了一种声卡驱动。目前,Wine对[[Alsa (简体中文)|Alsa]]的支持最好。<br />
<br />
x86_64平台下使用[[Alsa (简体中文)|Alsa]]的话,需要安装{{Pkg|lib32-alsa-lib}}。如果还要使用PulseAudio,则需安装{{Pkg|lib32-libpulse}}。<br />
<br />
若使用[[OSS (简体中文)|OSS]],需要安装{{Pkg|lib32-alsa-oss}}。仅靠内核驱动是不行的。<br />
<br />
安装上述软件包后,若{{ic|winecfg}}'''仍'''无法识别声卡(Selected driver: (none)),请尝试[http://wiki.jswindle.com/index.php/Wine_Registry#Configuring_Sound registry 通过注册表配置]。<br />
<br />
<br />
运行使用某些高级声音系统的游戏,可能还需要安装{{Pkg|lib32-openal}}。<br />
<br />
=== 其他函数库 ===<br />
<br />
某些程序(如 Office 2003)需要解析HTML、XML(使用MSXML库),需要安装{{Pkg|lib32-libxml2}}。<br />
<br />
播放音频的程序可能依赖{{Pkg|lib32-mpg123}}。<br />
<br />
对于使用图像处理库的程序,可能依赖{{Pkg|lib32-giflib}}和{{Pkg|lib32-libpng}},<br />
<br />
x86_64的加密支持需要{{Pkg|lib32-gnutls}}软件包。<br />
<br />
=== 字体 ===<br />
<br />
如果没有安装微软Truetype字体,Wine程序的字体显示可能会一团糟,参见[[MS Fonts (简体中文)]]。如果还是不行,试试{{ic|winetricks allfonts}}。<br />
<br />
上述操作后,杀死wine相关进程再运行{{ic|winecfg}},字体应该变好看了。<br />
<br />
如果字体看起来很毛糙,试试用[http://wiki.winehq.org/regedit regedit]导入下列文本文件:<br />
[HKEY_CURRENT_USER\Software\Wine\X11 Driver]<br />
"ClientSideWithRender"="N"<br />
<br />
=== 启动器和菜单 ===<br />
<br />
Wine不会为内置程序(如{{ic|winecfg}}、{{ic|winebrowser}})创建桌面启动器和菜单项。但手动安装的Windows程序通常会自动创建启动器和菜单项。在Windows下,安装程序(如{{ic|setup.exe}})通常会在桌面和开始菜单建立快捷方式,而Wine下会创建遵循freedesktop.org规范的.desktop文件(即启动器,相当于快捷方式)。<br />
<br />
{{小贴士|如果启动器''没有''自动创建,或者这些文件丢失了,可以尝试使用[http://wiki.winehq.org/winemenubuilder winemenubuilder]修复。}}<br />
<br />
Ubuntu下,Wine项目以子菜单形式出现在系统菜单。以下步骤将实现这个效果:<br />
<br />
==== 创建菜单项 ====<br />
首先,用Wine安装一个Windows程序,以建立基本的菜单。完成后,向其中添加菜单项。桌面右键选择{{ic|"创建启动器..."}}(不同桌面环境操作有所差异),设置如下:<br />
'''类型(Type)''': 应用程序(Application)<br />
'''名称(Name)''': 配置<br />
'''命令(Command)''': winecfg<br />
'''备注(Comment)''': Wine配置工具<br />
<br />
'''类型''': 应用程序<br />
'''名称''': 卸载程序<br />
'''命令''': wine uninstaller<br />
'''备注''': 卸载Wine下的Windows程序<br />
<br />
'''类型''': 应用程序<br />
'''名称''': 浏览 C:\<br />
'''命令''': wine winebrowser c:\\<br />
'''备注''': 浏览Wine中虚拟的C盘<br />
现在,桌面上出现了三个启动器,下面将把它们移入菜单。不过首先,我们给这些启动器加上动态图标(由图标主题提供)。方法是,用文本编辑器打开启动器,编辑Icon项目:<br />
<br />
{{ic|配置}} 启动器:<br />
Icon=wine-winecfg<br />
{{ic|卸载程序}} 启动器:<br />
Icon=wine-uninstaller<br />
{{ic|浏览 C:\}} 启动器:<br />
Icon=wine-winefile<br />
{{小贴士|多数桌面环境在上述“创建启动器”步骤即可设置图标。以第一个启动器为例,在选择图标窗口中搜索wine-winecfg,选择即可,无需手动编辑。 ——译者注}}<br />
如果图标无法显示或者你觉得很丑陋,换成其他图标也可以。右键设置启动器,应该有更改图标的地方。很多图标主题,例如[http://www.gnome-look.org/content/show.php/GNOME-colors?content=82562 GNOME-colors],都提供这些图标。<br />
<br />
现在,将启动器移入菜单。把启动器复制到 {{ic|~/.local/share/applications/wine/}} 目录即可。<br />
<br />
诶?图标还没出现在菜单中!还剩下最后一步,创建下列文本文件:<br />
{{hc|~/.config/menus/applications-merged/wine-utilities.menu|<nowiki><br />
<!DOCTYPE Menu PUBLIC "-//freedesktop//DTD Menu 1.0//EN"<br />
"http://www.freedesktop.org/standards/menu-spec/menu-1.0.dtd"><br />
<Menu><br />
<Name>Applications</Name><br />
<Menu><br />
<Name>wine-wine</Name><br />
<Directory>wine-wine.directory</Directory><br />
<Include><br />
<Filename>wine-Configuration.desktop</Filename><br />
</Include><br />
<Include><br />
<Filename>wine-Browse C:\.desktop</Filename><br />
</Include><br />
<Include><br />
<Filename>wine-Uninstall Programs.desktop</Filename><br />
</Include><br />
</Menu><br />
</Menu><br />
</nowiki>}}<br />
再看看菜单,应该万事大吉了。<br />
<br />
==== Gnome3 中清理 Wine 菜单启动项 ====<br />
系统全局的菜单启动器安装在 {{ic|/usr/share/applications/}},清除相应程序的“.desktop”文件即可从整个系统删除该启动器。<br />
<br />
如果这样还是无法解决问题,那么很可能 Wine 的启动器存放在用户级别的 {{ic|~/.local/share/applications/wine/Programs/}} 目录中。删除相应的“.desktop”文件即可清理对应启动项。删除整个 Programs 文件夹将清理所有 Wine 程序的启动项。<br />
<br />
==== 修复 KDE 4 菜单问题[https://bugs.launchpad.net/ubuntu/+source/wine/+bug/263041] ====<br />
Wine菜单项有可能错误地出现在{{ic|"Lost & Found(其他)"}} ,而非Wine子菜单。原因是kde-applications.menu文件缺失{{ic|MergeDir}}配置。<br />
<br />
编辑{{ic|/etc/xdg/menus/kde-applications.menu}}。<br />
<br />
在文件末尾处,{{ic|<DefaultMergeDirs/>}}后添加{{ic|<MergeDir>applications-merged</MergeDir>}}。修改后内容大致如下:<br />
<Menu><br />
<Include><br />
<And><br />
<Category>KDE</Category><br />
<Category>Core</Category><br />
</And><br />
</Include><br />
<DefaultMergeDirs/><br />
'''<MergeDir>applications-merged</MergeDir>'''<br />
<MergeFile>applications-kmenuedit.menu</MergeFile><br />
</Menu><br />
<br />
另一个方法是:<br />
ln -s ~/.config/menus/applications-merged ~/.config/menus/kde-applications-merged<br />
<br />
这样的好处是,不会因为KDE升级而重置配置。但该方法只对一个用户有效。<br />
<br />
== 运行 Windows 程序 ==<br />
{{警告|千万不要以root身份运行Wine!详情参见[http://wiki.winehq.org/FAQ#run_as_root 本文]。}}<br />
运行Windows程序:<br />
$ wine <exe文件><br />
<br />
内置的msiexec程序可以运行MSI安装包:<br />
$ msiexec installername.msi<br />
== 技巧 ==<br />
{{小贴士|此外您可能会感兴趣以下文章的开始所提供的链接<br />
* [http://appdb.winehq.org/ Wine程序数据库 (Wine Application Database, AppDB)] —— 特定Windows程序的Wine兼容情况(运行时的已知问题、用户评分、指南等等)<br />
* [http://forum.winehq.org/ WineHQ论坛] —— 要是看完上述网页还有问题,可以到这里咨询<br />
}}<br />
<br />
<br />
这里介绍一些安装Windows组件的工具。由于这些工具可能严重破坏Wine配置,没有需要时最好不要使用。<br />
<br />
=== 安装 Microsoft Office ===<br />
<br />
安装Office套装前,需要先安装某些Windows组件:<br />
<br />
$ WINEARCH=win32 WINEPREFIX=/path/to/wineprefix winecfg<br />
# pacman -S winetricks<br />
$ winetricks msxml3 # For MS Office 2007<br />
$ winetricks msxml3 msxml6 # For MS Office 2010<br />
$ wine /path/to/office_cd/setup.exe<br />
<br />
更多信息,参见[http://appdb.winehq.org/appview.php?iVersionId=4992 WineHQ上的文章]。<br />
<br />
=== OpenGL 模式 ===<br />
很多游戏(比如魔兽争霸啦)都支持OpenGL模式,在Wine下''可能''比默认DirectX模式性能更好。一般添加{{Ic|-opengl}}启动程序即可,但''不同程序可能有所不同'':<br />
$ wine /path/to/3d_game.exe -opengl<br />
<br />
请参考[http://appdb.winehq.org AppDB],了解特定程序的相关信息。<br />
<br />
=== 将 Wine 作为 Win16/Win32 程序的解释器 ===<br />
可以通知内核把Wine作为Win16/Win32程序的解释器。<br />
<br />
设置过程在 [[systemd (简体中文)|systemd]] 和 [[initscripts (简体中文)|initscripts]] 下有所差异。<br />
<br />
==== systemd ====<br />
通知内核识别和执行 Win16/Win32 程序的方式:<br />
echo ':DOSWin:M::MZ::/usr/bin/wine:' > /proc/sys/fs/binfmt_misc/register<br />
To make the setting permanent, create a configuration file in {{ic|/etc/tmpfiles.d}} with the following contents:<br />
测试效果,若一切正常,可以使该设置永久生效。在 {{ic|/etc/tmpfiles.d}} 目录创建新的配置文件,内容为:<br />
{{hc|/etc/tmpfiles.d/enable-doswin-exe.conf|<br />
w /proc/sys/fs/binfmt_misc/register - - - - :DOSWin:M::MZ::/usr/bin/wine:}}<br />
<br />
说明一下,和 initscripts 不同,systemd 会自动挂载 {{ic|/proc/sys/fs/binfmt_misc}},所以只需要通过临时文件机制向内核写入配置即可。<br />
<br />
更多信息,参见 [[Systemd (简体中文)#临时文件]]。<br />
<br />
==== initscripts ====<br />
首先,挂载 {{ic|binfmt_misc}}文件系统:<br />
# mount -t binfmt_misc none /proc/sys/fs/binfmt_misc<br />
或者,如果你想一直这么做,将以下内容加入{{ic|/etc/fstab}}:<br />
none /proc/sys/fs/binfmt_misc binfmt_misc defaults 0 0<br />
然后,告知内核如何解释(运行)Win16/Win32程序:<br />
echo ':DOSWin:M::MZ::/usr/bin/wine:' > /proc/sys/fs/binfmt_misc/register<br />
如果你想一直这么做,添加上述内容到{{ic|/etc/rc.local}}即可。最后的重定向是为了避免切换启动级别时的错误信息:<br />
{ echo ':DOSWin:M::MZ::/usr/bin/wine:' > /proc/sys/fs/binfmt_misc/register; } 2>/dev/null<br />
<br />
==== 效果测试 ====<br />
现在,直接运行Windows程序试试:<br />
chmod 755 exefile.exe<br />
./exefile.exe<br />
<br />
=== Wine 控制台 ===<br />
<br />
有些时候,可能需要运行{{ic|.exe}}给游戏打补丁,比如给古董游戏添加宽屏支持。这时直接通过Wine运行可能没有用。那么,打开终端,运行一下命令:<br />
<br />
$ wineconsole cmd<br />
<br />
将进入一个和Windows下cmd一样的命令行环境。在该环境下试试也许就可以了。<br />
<br />
=== Winetricks ===<br />
使用[http://wiki.winehq.org/winetricks Winetricks]快速脚本,能够方便地安装许多Windows组件,包括DirectX、msxml(被Office 2007、IE浏览器依赖)visual运行库还有其他更多的。<br />
<br />
您可以使用[[pacman (简体中文)|pacman]]或者从[[AUR (简体中文)|AUR]]上获取{{AUR|winetricks-svn}}软件包来安装该工具:<br />
<br />
运行:<br />
<br />
$ winetricks<br />
<br />
== 第三方工具 ==<br />
这些程序有其自己的主页和支持论坛。<br />
<br />
=== CrossOver ===<br />
<br />
[http://www.codeweavers.com/about/ CrossOver] 有单独的[[CrossOver|wiki 页面]].<br />
<br />
=== PlayOnLinux ===<br />
[http://www.playonlinux.com/ PlayOnLinux]是一个图形界面的Windows/DOS程序管理器。它提供了一些帮助配置/运行程序的脚本,能够管理多个不同版本的Wine,甚至能对不同程序使用不同Wine版本。参考[http://appdb.winehq.org AppDB],看看哪个Wine版本对你要运行的程序兼容最好。从[[AUR (简体中文)|AUR]]安装{{AUR|playonlinux}}。<br />
<br />
=== PyWinery ===<br />
[http://code.google.com/p/pywinery/ PyWinery]是一个简单的、图形界面的Wine系统目录管理器,用它可以方便地管理不同系统目录,并从不同系统目录运行程序。同时可以开启winetricks在同一系统目录,打开系统目录所在文件夹, {{ic|winecfg}}, 软件卸载程序和wineDOS。[[AUR (简体中文)|AUR]]中提供了软件包{{AUR|pywinery}}。当你使用很多系统目录(一个打游戏用、一个编程用……)时,这个程序会非常有用。<br />
<br />
它在默认情况下使用winetricks打开{{ic|.exe}}文件,所以你可以选择你有的任何Wine的配置。<br />
<br />
=== Q4wine ===<br />
[http://q4wine.brezblock.org.ua/ Q4Wine] 是一个图形界面的系统目录(wine-prefix)管理器。它的特色是可以把 QT 主题导入 Wine 配置,使两者完美整合。{{Pkg|q4wine}} 软件包在 <nowiki>[</nowiki>[[multilib]]] 仓库中提供。<br />
<br />
== 相关链接 ==<br />
* [http://www.winehq.com/ Wine官方网站]<br />
* [http://appdb.winehq.org/ Wine程序数据库]<br />
* [http://linuxgamingtoday.wordpress.com/2008/02/16/quick-tips-to-speed-up-your-gaming-in-wine/ 加速Wine,显卡及OpenGL高级配置]<br />
* [http://wiki.gotux.net/code:perl:fileinfo FileInfo] —— Find Win32 PE/COFF headers in EXE/DLL/OCX files under linux/unix environment.</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Wine_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=245631
Wine (简体中文)
2013-01-30T02:14:15Z
<p>Cuihao: /* 安装 */</p>
<hr />
<div>[[Category:Wine (简体中文)]]<br />
[[cs:Wine]]<br />
[[de:Wine]]<br />
[[en:Wine]]<br />
[[es:Wine]]<br />
[[fr:Wine]]<br />
[[it:Wine]]<br />
[[ja:Wine]]<br />
[[ru:Wine]]<br />
[[zh-TW:Wine]]<br />
{{TranslationStatus (简体中文)|Wine|2013-01-30|244275}}<br />
{{Article summary start}}<br />
{{Article summary text|[[Wikipedia:Wine (software)|Wine]] 是类UNIX系统下运行微软Windows程序的"兼容层"。在Wine中运行的Windows程序,就如同运行原生Linux程序一样,不会有模拟器那样的性能问题。}}<br />
{{Article summary heading|Related}}<br />
{{Article summary wiki|Steam}}<br />
{{Article summary wiki|CrossOver}}<br />
{{Article summary end}}<br />
<br />
获取更详细的介绍请浏览[http://www.winehq.org/ 项目官方网站]和[http://wiki.winehq.org/ wiki]页面。<br />
<br />
== 安装 ==<br />
{{警告|如果您的账户能浏览某些文件或资源,Wine运行的程序也可以。Wine不是[[wikipedia:Sandbox_%28computer_security%29 |沙箱]]。如果很重视安全,请考虑使用[[wikipedia:Virtualization|虚拟化]]。}}<br />
<br />
最新版的Wine软件包收录在:i686平台的<nowiki>[</nowiki>[[community]]]仓库,x86_64的<nowiki>[</nowiki>[[multilib]]]仓库。<br />
<br />
# pacman -S wine<br />
<br />
另外,您可能需要安装{{pkg|wine_gecko}}以模拟 Internet Explorer 支持(很多网络程序需要):<br />
# pacman -S wine_gecko<br />
<br />
或使用[http://wiki.winehq.org/Mono Mono] 以使用 .NET 程序。[[AUR]]中的软件包{{AUR|wine-mono}}会将安装程序放到{{ic|/usr/share/wine/mono/}},这样Wine就可以自动检查到,不会再次下载。<br />
<br />
'''平台差异'''<br />
<br />
默认的Wine是32位的程序,也是i686的Arch软件包。所以它不能运行64位的Windows程序(反正是罕见的)。<br />
<br />
然而,x86_64的Wine软件包目前以{{ic| --enable-win64}}方式编译。这个参数激活了[[Wikipedia:WoW64|WoW64]]的Wine版本。<br />
<br />
*在Windows中,这个复杂的子系统允许用户同时使用32位和64位的Windows程序,甚至是在同一目录。<br />
<br />
*在Wine中,用户将必须建立单独分开的目录/前缀。这项Wine功能仍是试验阶段,并建议用户使用一个win32{{ic|WINEPREFIX}}。浏览[http://wiki.winehq.org/Wine64 Wine64]以获取有关这个的详细信息。<br />
<br />
总结一下,配置{{ic|1=WINEARCH=win32}}后,x86_64平台的Arch和i686平台的Arch完全相同。<br />
<br />
{{注意|如果在64位环境中执行{{ic|winetricks}}或其它程序出现问题,请试试创建一个新的32位{{ic|WINEPREFIX}}. 参见下面的[[#使用 WINEARCH]]}}<br />
<br />
== 配置 ==<br />
Wine默认将配置文件和安装的Windows程序保存在{{ic|~/.wine}}。这样的目录称为一个"Wine prefix"或"Wine bottle"(保留原文,下文称“系统目录”)。每次运行Windows程序(包括内置程序,如{{ic|winecfg}})时,系统目录会自动创建(如果缺失)或更新。系统目录中存放有相当于Windows下{{ic | C:\}}C盘(更确切的说应是系统盘)的文件夹。<br />
<br />
通过设置{{ic|WINEPREFIX}}环境变量,可以更改Wine系统目录的位置。如果希望让不同的Windows程序使用不同的系统环境或配置,这一变量会非常有用。<br />
<br />
例如,如果您使用 <br />
{{ic |1= $ env WINEPREFIX=~/.win-a wine-A程序.exe}}参数来运行一个程序。另一个使用<br />
{{ic |1= $ env WINEPREFIX=~/.win-b wine-B程序.exe}}参数,这两个程序将使用独立的C盘和注册表配置。<br />
<br />
以下命令会建立一个默认的系统目录,且不启动任何Windows程序:<br />
$ env WINEPREFIX=~/.customprefix wineboot -u<br />
<br />
配置Wine的方式通常有:<br />
* [http://wiki.winehq.org/winecfg winecfg]是Wine的图形界面配置程序。控制台下调用{{ic|$ winecfg}}(或指定系统目录:{{ic|1=$ WINEPREFIX=~/.系统目录 winecfg}})即可启动<br />
* [http://wiki.winehq.org/control control.exe]是Windows控制面板的Wine实现,通过{{ic|$ wine control}}命令启动<br />
* [http://wiki.winehq.org/regedit regedit]是Wine的注册表编辑器,比较前两者,该工具能配置更多东西。部分常用键值参见:[http://wiki.winehq.org/UsefulRegistryKeys WineHQ's article on Useful Registry Keys]<br />
<br />
=== 使用 WINEARCH ===<br />
对于64位用户,如果使用[multilib]仓库里的Wine,默认创建的系统目录是64位环境的。若想使用纯32位环境,修改{{ic|WINEARCH}} 变量win32为即可:<br />
{{ic |1=$ WINEARCH=win32 winecfg}}这样就会生成32位Wine环境。若不设置{{ic|WINEARCH}}得到的就是64位环境。<br />
<br />
通过{{ic|WINEPREFIX}}变量,在不同的系统目录分别创建32位和64位环境:<br />
$ WINEARCH=win32 WINEPREFIX=~/win32 winecfg <br />
$ WINEPREFIX=~/win64 winecfg<br />
<br />
{{注意|系统目录创建过程中,64位版本的wine将视全部目录如同64位系统目录,也将不会在已存在的目录中创建任何32位的.创建32位系统目录,您必须让Wine创建指定的{{ic|WINEPREFIX}}目录。}}<br />
<br />
winetricks也接受{{ic|WINEPREFIX}}变量,以安装Steam为例:<br />
env WINEARCH=win32 WINEPREFIX=~/.local/share/wineprefixes/steam winetricks steam<br />
{{注意|不必手动在{{ic|wineprefixes}}文件夹下建立steam文件夹,Wine会自动创建不存在的系统目录.}}<br />
<br />
=== 显卡驱动 ===<br />
使用Wine运行Windows游戏时,可能需要高性能的显卡驱动。[[Nvidia (简体中文)|Nvidia]]、[[ATI (简体中文)|Amd/ATI]]用户最好使用闭源驱动。[[Intel (简体中文)|Intel]]显卡用户也可以选择开源驱动,它已经非常成熟。<br />
<br />
要是显卡驱动有问题或者相关配置有误,控制台用Wine运行某些程序时会输出:<br />
Direct rendering is disabled, most likely your OpenGL drivers have not been installed correctly<br />
<br />
x86-64用户需要从[multilib]或[[AUR (简体中文)|AUR]]安装额外的32位库:<br />
* '''NVIDIA''':{{ic|# pacman -S lib32-nvidia-utils}} 关于旧版本lib32-nvidia-utils(例如nvidia-96xx驱动),请到[https://aur.archlinux.org/packages.php?K=lib32-nvidia-utils 这里]查找。<br />
<br />
* '''NVIDIA (使用nouveau-dri驱动)''': {{ic|# pacman -S lib32-nouveau-dri}} <br />
<br />
* '''Intel''':{{ic|# pacman -S lib32-intel-dri}} 运行Wine时需要手动添加{{ic|1=LIBGL_DRIVERS_PATH=/usr/lib32/xorg/modules/dri}}<br />
<br />
* '''AMD/ATI''':{{ic|# pacman -S lib32-ati-dri}} 如果用的是闭源驱动:{{ic|# pacman -S {{Pkg|lib32-catalyst-utils}}}}。<br />
<br />
{{注意|安装上述软件包后,可能需要重启X才能生效!}}<br />
<br />
=== 声音 ===<br />
Wine程序有可能遇到某些声音问题。首先,确保{{ic|winecfg}}中只启用了一种声卡驱动。目前,Wine对[[Alsa (简体中文)|Alsa]]的支持最好。<br />
<br />
x86_64平台下使用[[Alsa (简体中文)|Alsa]]的话,需要安装{{Pkg|lib32-alsa-lib}}。如果还要使用PulseAudio,则需安装{{Pkg|lib32-libpulse}}。<br />
<br />
若使用[[OSS (简体中文)|OSS]],需要安装{{Pkg|lib32-alsa-oss}}。仅靠内核驱动是不行的。<br />
<br />
安装上述软件包后,若{{ic|winecfg}}'''仍'''无法识别声卡(Selected driver: (none)),请尝试[http://wiki.jswindle.com/index.php/Wine_Registry#Configuring_Sound registry 通过注册表配置]。<br />
<br />
<br />
运行使用某些高级声音系统的游戏,可能还需要安装{{Pkg|lib32-openal}}。<br />
<br />
=== 其他函数库 ===<br />
<br />
某些程序(如 Office 2003)需要解析HTML、XML(使用MSXML库),需要安装{{Pkg|lib32-libxml2}}。<br />
<br />
播放音频的程序可能依赖{{Pkg|lib32-mpg123}}。<br />
<br />
对于使用图像处理库的程序,可能依赖{{Pkg|lib32-giflib}}和{{Pkg|lib32-libpng}},<br />
<br />
x86_64的加密支持需要{{Pkg|lib32-gnutls}}软件包。<br />
<br />
=== 字体 ===<br />
<br />
如果没有安装微软Truetype字体,Wine程序的字体显示可能会一团糟,参见[[MS Fonts (简体中文)]]。如果还是不行,试试{{ic|winetricks allfonts}}。<br />
<br />
上述操作后,杀死wine相关进程再运行{{ic|winecfg}},字体应该变好看了。<br />
<br />
如果字体看起来很毛糙,试试用[http://wiki.winehq.org/regedit regedit]导入下列文本文件:<br />
[HKEY_CURRENT_USER\Software\Wine\X11 Driver]<br />
"ClientSideWithRender"="N"<br />
<br />
=== 启动器和菜单 ===<br />
<br />
Wine不会为内置程序(如{{ic|winecfg}}、{{ic|winebrowser}})创建桌面启动器和菜单项。但手动安装的Windows程序通常会自动创建启动器和菜单项。在Windows下,安装程序(如{{ic|setup.exe}})通常会在桌面和开始菜单建立快捷方式,而Wine下会创建遵循freedesktop.org规范的.desktop文件(即启动器,相当于快捷方式)。<br />
<br />
{{小贴士|如果启动器''没有''自动创建,或者这些文件丢失了,可以尝试使用[http://wiki.winehq.org/winemenubuilder winemenubuilder]修复。}}<br />
<br />
Ubuntu下,Wine项目以子菜单形式出现在系统菜单。以下步骤将实现这个效果:<br />
<br />
==== 创建菜单项 ====<br />
首先,用Wine安装一个Windows程序,以建立基本的菜单。完成后,向其中添加菜单项。桌面右键选择{{ic|"创建启动器..."}}(不同桌面环境操作有所差异),设置如下:<br />
'''类型(Type)''': 应用程序(Application)<br />
'''名称(Name)''': 配置<br />
'''命令(Command)''': winecfg<br />
'''备注(Comment)''': Wine配置工具<br />
<br />
'''类型''': 应用程序<br />
'''名称''': 卸载程序<br />
'''命令''': wine uninstaller<br />
'''备注''': 卸载Wine下的Windows程序<br />
<br />
'''类型''': 应用程序<br />
'''名称''': 浏览 C:\<br />
'''命令''': wine winebrowser c:\\<br />
'''备注''': 浏览Wine中虚拟的C盘<br />
现在,桌面上出现了三个启动器,下面将把它们移入菜单。不过首先,我们给这些启动器加上动态图标(由图标主题提供)。方法是,用文本编辑器打开启动器,编辑Icon项目:<br />
<br />
{{ic|配置}} 启动器:<br />
Icon=wine-winecfg<br />
{{ic|卸载程序}} 启动器:<br />
Icon=wine-uninstaller<br />
{{ic|浏览 C:\}} 启动器:<br />
Icon=wine-winefile<br />
{{小贴士|多数桌面环境在上述“创建启动器”步骤即可设置图标。以第一个启动器为例,在选择图标窗口中搜索wine-winecfg,选择即可,无需手动编辑。 ——译者注}}<br />
如果图标无法显示或者你觉得很丑陋,换成其他图标也可以。右键设置启动器,应该有更改图标的地方。很多图标主题,例如[http://www.gnome-look.org/content/show.php/GNOME-colors?content=82562 GNOME-colors],都提供这些图标。<br />
<br />
现在,将启动器移入菜单。把启动器复制到 {{ic|~/.local/share/applications/wine/}} 目录即可。<br />
<br />
诶?图标还没出现在菜单中!还剩下最后一步,创建下列文本文件:<br />
{{hc|~/.config/menus/applications-merged/wine-utilities.menu|<nowiki><br />
<!DOCTYPE Menu PUBLIC "-//freedesktop//DTD Menu 1.0//EN"<br />
"http://www.freedesktop.org/standards/menu-spec/menu-1.0.dtd"><br />
<Menu><br />
<Name>Applications</Name><br />
<Menu><br />
<Name>wine-wine</Name><br />
<Directory>wine-wine.directory</Directory><br />
<Include><br />
<Filename>wine-Configuration.desktop</Filename><br />
</Include><br />
<Include><br />
<Filename>wine-Browse C:\.desktop</Filename><br />
</Include><br />
<Include><br />
<Filename>wine-Uninstall Programs.desktop</Filename><br />
</Include><br />
</Menu><br />
</Menu><br />
</nowiki>}}<br />
再看看菜单,应该万事大吉了。<br />
<br />
==== Gnome3 中清理 Wine 菜单启动项 ====<br />
系统全局的菜单启动器安装在 {{ic|/usr/share/applications/}},清除相应程序的“.desktop”文件即可从整个系统删除该启动器。<br />
<br />
如果这样还是无法解决问题,那么很可能 Wine 的启动器存放在用户级别的 {{ic|~/.local/share/applications/wine/Programs/}} 目录中。删除相应的“.desktop”文件即可清理对应启动项。删除整个 Programs 文件夹将清理所有 Wine 程序的启动项。<br />
<br />
==== 修复 KDE 4 菜单问题[https://bugs.launchpad.net/ubuntu/+source/wine/+bug/263041] ====<br />
Wine菜单项有可能错误地出现在{{ic|"Lost & Found(其他)"}} ,而非Wine子菜单。原因是kde-applications.menu文件缺失{{ic|MergeDir}}配置。<br />
<br />
编辑{{ic|/etc/xdg/menus/kde-applications.menu}}。<br />
<br />
在文件末尾处,{{ic|<DefaultMergeDirs/>}}后添加{{ic|<MergeDir>applications-merged</MergeDir>}}。修改后内容大致如下:<br />
<Menu><br />
<Include><br />
<And><br />
<Category>KDE</Category><br />
<Category>Core</Category><br />
</And><br />
</Include><br />
<DefaultMergeDirs/><br />
'''<MergeDir>applications-merged</MergeDir>'''<br />
<MergeFile>applications-kmenuedit.menu</MergeFile><br />
</Menu><br />
<br />
另一个方法是:<br />
ln -s ~/.config/menus/applications-merged ~/.config/menus/kde-applications-merged<br />
<br />
这样的好处是,不会因为KDE升级而重置配置。但该方法只对一个用户有效。<br />
<br />
== 运行 Windows 程序 ==<br />
{{警告|千万不要以root身份运行Wine!详情参见[http://wiki.winehq.org/FAQ#run_as_root 本文]。}}<br />
运行Windows程序:<br />
$ wine <exe文件><br />
<br />
内置的msiexec程序可以运行MSI安装包:<br />
$ msiexec installername.msi<br />
== 技巧 ==<br />
{{小贴士|此外您可能会感兴趣以下文章的开始所提供的链接<br />
* [http://appdb.winehq.org/ Wine程序数据库 (Wine Application Database, AppDB)] —— 特定Windows程序的Wine兼容情况(运行时的已知问题、用户评分、指南等等)<br />
* [http://forum.winehq.org/ WineHQ论坛] —— 要是看完上述网页还有问题,可以到这里咨询<br />
}}<br />
<br />
<br />
这里介绍一些安装Windows组件的工具。由于这些工具可能严重破坏Wine配置,没有需要时最好不要使用。<br />
<br />
=== 安装 Microsoft Office ===<br />
<br />
安装Office套装前,需要先安装某些Windows组件:<br />
<br />
$ WINEARCH=win32 WINEPREFIX=/path/to/wineprefix winecfg<br />
# pacman -S winetricks<br />
$ winetricks msxml3 # For MS Office 2007<br />
$ winetricks msxml3 msxml6 # For MS Office 2010<br />
$ wine /path/to/office_cd/setup.exe<br />
<br />
更多信息,参见[http://appdb.winehq.org/appview.php?iVersionId=4992 WineHQ上的文章]。<br />
<br />
=== OpenGL 模式 ===<br />
很多游戏(比如魔兽争霸啦)都支持OpenGL模式,在Wine下''可能''比默认DirectX模式性能更好。一般添加{{Ic|-opengl}}启动程序即可,但''不同程序可能有所不同'':<br />
$ wine /path/to/3d_game.exe -opengl<br />
<br />
请参考[http://appdb.winehq.org AppDB],了解特定程序的相关信息。<br />
<br />
=== 将 Wine 作为 Win16/Win32 程序的解释器 ===<br />
可以通知内核把Wine作为Win16/Win32程序的解释器。<br />
<br />
设置过程在 [[systemd (简体中文)|systemd]] 和 [[initscripts (简体中文)|initscripts]] 下有所差异。<br />
<br />
==== systemd ====<br />
通知内核识别和执行 Win16/Win32 程序的方式:<br />
echo ':DOSWin:M::MZ::/usr/bin/wine:' > /proc/sys/fs/binfmt_misc/register<br />
To make the setting permanent, create a configuration file in {{ic|/etc/tmpfiles.d}} with the following contents:<br />
测试效果,若一切正常,可以使该设置永久生效。在 {{ic|/etc/tmpfiles.d}} 目录创建新的配置文件,内容为:<br />
{{hc|/etc/tmpfiles.d/enable-doswin-exe.conf|<br />
w /proc/sys/fs/binfmt_misc/register - - - - :DOSWin:M::MZ::/usr/bin/wine:}}<br />
<br />
说明一下,和 initscripts 不同,systemd 会自动挂载 {{ic|/proc/sys/fs/binfmt_misc}},所以只需要通过临时文件机制向内核写入配置即可。<br />
<br />
更多信息,参见 [[Systemd (简体中文)#临时文件]]。<br />
<br />
==== initscripts ====<br />
首先,挂载 {{ic|binfmt_misc}}文件系统:<br />
# mount -t binfmt_misc none /proc/sys/fs/binfmt_misc<br />
或者,如果你想一直这么做,将以下内容加入{{ic|/etc/fstab}}:<br />
none /proc/sys/fs/binfmt_misc binfmt_misc defaults 0 0<br />
然后,告知内核如何解释(运行)Win16/Win32程序:<br />
echo ':DOSWin:M::MZ::/usr/bin/wine:' > /proc/sys/fs/binfmt_misc/register<br />
如果你想一直这么做,添加上述内容到{{ic|/etc/rc.local}}即可。最后的重定向是为了避免切换启动级别时的错误信息:<br />
{ echo ':DOSWin:M::MZ::/usr/bin/wine:' > /proc/sys/fs/binfmt_misc/register; } 2>/dev/null<br />
<br />
==== 效果测试 ====<br />
现在,直接运行Windows程序试试:<br />
chmod 755 exefile.exe<br />
./exefile.exe<br />
<br />
=== Wine 控制台 ===<br />
<br />
有些时候,可能需要运行{{ic|.exe}}给游戏打补丁,比如给古董游戏添加宽屏支持。这时直接通过Wine运行可能没有用。那么,打开终端,运行一下命令:<br />
<br />
$ wineconsole cmd<br />
<br />
将进入一个和Windows下cmd一样的命令行环境。在该环境下试试也许就可以了。<br />
<br />
=== Winetricks ===<br />
使用[http://wiki.winehq.org/winetricks Winetricks]快速脚本,能够方便地安装许多Windows组件,包括DirectX、msxml(被Office 2007、IE浏览器依赖)visual运行库还有其他更多的。<br />
<br />
您可以使用[[pacman (简体中文)|pacman]]或者从[[AUR (简体中文)|AUR]]上获取{{AUR|winetricks-svn}}软件包来安装该工具:<br />
<br />
运行:<br />
<br />
$ winetricks<br />
<br />
== 第三方界面 ==<br />
这些程序有其自己的主页和支持论坛。<br />
<br />
=== CrossOver ===<br />
<br />
[http://www.codeweavers.com/about/ CrossOver] 有单独的[[CrossOver|wiki 页面]].<br />
<br />
=== PlayOnLinux ===<br />
[http://www.playonlinux.com/ PlayOnLinux]是一个图形界面的Windows/DOS程序管理器。它提供了一些帮助配置/运行程序的脚本,能够管理多个不同版本的Wine,甚至能对不同程序使用不同Wine版本。参考[http://appdb.winehq.org AppDB],看看哪个Wine版本对你要运行的程序兼容最好。从[[AUR (简体中文)|AUR]]安装{{AUR|playonlinux}}。<br />
<br />
=== PyWinery ===<br />
[http://code.google.com/p/pywinery/ PyWinery]是一个简单的、图形界面的Wine系统目录管理器,用它可以方便地管理不同系统目录,并从不同系统目录运行程序。同时可以开启winetricks在同一系统目录,打开系统目录所在文件夹, {{ic|winecfg}}, 软件卸载程序和wineDOS。[[AUR (简体中文)|AUR]]中提供了软件包{{AUR|pywinery}}。当你使用很多系统目录(一个打游戏用、一个编程用……)时,这个程序会非常有用。<br />
<br />
它在默认情况下使用winetricks打开{{ic|.exe}}文件,所以你可以选择你有的任何Wine的配置。<br />
<br />
=== Q4wine ===<br />
[http://q4wine.brezblock.org.ua/ Q4Wine] 是一个图形界面的系统目录(wine-prefix)管理器。它的特色是可以把 QT 主题导入 Wine 配置,使两者完美整合。{{Pkg|q4wine}} 软件包在 <nowiki>[</nowiki>[[multilib]]] 仓库中提供。<br />
<br />
== 相关链接 ==<br />
* [http://www.winehq.com/ Wine官方网站]<br />
* [http://appdb.winehq.org/ Wine程序数据库]<br />
* [http://linuxgamingtoday.wordpress.com/2008/02/16/quick-tips-to-speed-up-your-gaming-in-wine/ 加速Wine,显卡及OpenGL高级配置]<br />
* [http://wiki.gotux.net/code:perl:fileinfo FileInfo] —— Find Win32 PE/COFF headers in EXE/DLL/OCX files under linux/unix environment.</div>
Cuihao
https://wiki.archlinux.org/index.php?title=Wine_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)&diff=245630
Wine (简体中文)
2013-01-30T02:12:50Z
<p>Cuihao: </p>
<hr />
<div>[[Category:Wine (简体中文)]]<br />
[[cs:Wine]]<br />
[[de:Wine]]<br />
[[en:Wine]]<br />
[[es:Wine]]<br />
[[fr:Wine]]<br />
[[it:Wine]]<br />
[[ja:Wine]]<br />
[[ru:Wine]]<br />
[[zh-TW:Wine]]<br />
{{TranslationStatus (简体中文)|Wine|2013-01-30|244275}}<br />
{{Article summary start}}<br />
{{Article summary text|[[Wikipedia:Wine (software)|Wine]] 是类UNIX系统下运行微软Windows程序的"兼容层"。在Wine中运行的Windows程序,就如同运行原生Linux程序一样,不会有模拟器那样的性能问题。}}<br />
{{Article summary heading|Related}}<br />
{{Article summary wiki|Steam}}<br />
{{Article summary wiki|CrossOver}}<br />
{{Article summary end}}<br />
<br />
获取更详细的介绍请浏览[http://www.winehq.org/ 项目官方网站]和[http://wiki.winehq.org/ wiki]页面。<br />
<br />
== 安装 ==<br />
{{警告|如果您能用您的账户浏览文件或资源,Wine运行的程序也可以。Wine不是 [[wikipedia:Sandbox_%28computer_security%29 | 沙箱]]。如果很重视安全,请考虑使用[[wikipedia:Virtualization|虚拟化]]。}}<br />
<br />
最新版的Wine软件包收录在:i686平台的<nowiki>[</nowiki>[[community]]]仓库,x86_64的<nowiki>[</nowiki>[[multilib]]]仓库。<br />
<br />
# pacman -S wine<br />
<br />
另外,您可能需要安装{{pkg|wine_gecko}}以模拟 Internet Explorer 支持(很多网络程序需要):<br />
# pacman -S wine_gecko<br />
<br />
或使用[http://wiki.winehq.org/Mono Mono] 以使用 .NET 程序。[[AUR]]中的软件包{{AUR|wine-mono}}会将安装程序放到{{ic|/usr/share/wine/mono/}},这样Wine就可以自动检查到,不会再次下载。<br />
<br />
'''平台差异'''<br />
<br />
默认的Wine是32位的程序,也是i686的Arch软件包。所以它不能运行64位的Windows程序(反正是罕见的)。<br />
<br />
然而,x86_64的Wine软件包目前以{{ic| --enable-win64}}方式编译。这个参数激活了[[Wikipedia:WoW64|WoW64]]的Wine版本。<br />
<br />
*在Windows中,这个复杂的子系统允许用户同时使用32位和64位的Windows程序,甚至是在同一目录。<br />
<br />
*在Wine中,用户将必须建立单独分开的目录/前缀。这项Wine功能仍是试验阶段,并建议用户使用一个win32{{ic|WINEPREFIX}}。浏览[http://wiki.winehq.org/Wine64 Wine64]以获取有关这个的详细信息。<br />
<br />
总结一下,配置{{ic|1=WINEARCH=win32}}后,x86_64平台的Arch和i686平台的Arch完全相同。<br />
<br />
{{注意|如果在64位环境中执行{{ic|winetricks}}或其它程序出现问题,请试试创建一个新的32位{{ic|WINEPREFIX}}. 参见下面的[[#使用 WINEARCH]]}}<br />
<br />
== 配置 ==<br />
Wine默认将配置文件和安装的Windows程序保存在{{ic|~/.wine}}。这样的目录称为一个"Wine prefix"或"Wine bottle"(保留原文,下文称“系统目录”)。每次运行Windows程序(包括内置程序,如{{ic|winecfg}})时,系统目录会自动创建(如果缺失)或更新。系统目录中存放有相当于Windows下{{ic | C:\}}C盘(更确切的说应是系统盘)的文件夹。<br />
<br />
通过设置{{ic|WINEPREFIX}}环境变量,可以更改Wine系统目录的位置。如果希望让不同的Windows程序使用不同的系统环境或配置,这一变量会非常有用。<br />
<br />
例如,如果您使用 <br />
{{ic |1= $ env WINEPREFIX=~/.win-a wine-A程序.exe}}参数来运行一个程序。另一个使用<br />
{{ic |1= $ env WINEPREFIX=~/.win-b wine-B程序.exe}}参数,这两个程序将使用独立的C盘和注册表配置。<br />
<br />
以下命令会建立一个默认的系统目录,且不启动任何Windows程序:<br />
$ env WINEPREFIX=~/.customprefix wineboot -u<br />
<br />
配置Wine的方式通常有:<br />
* [http://wiki.winehq.org/winecfg winecfg]是Wine的图形界面配置程序。控制台下调用{{ic|$ winecfg}}(或指定系统目录:{{ic|1=$ WINEPREFIX=~/.系统目录 winecfg}})即可启动<br />
* [http://wiki.winehq.org/control control.exe]是Windows控制面板的Wine实现,通过{{ic|$ wine control}}命令启动<br />
* [http://wiki.winehq.org/regedit regedit]是Wine的注册表编辑器,比较前两者,该工具能配置更多东西。部分常用键值参见:[http://wiki.winehq.org/UsefulRegistryKeys WineHQ's article on Useful Registry Keys]<br />
<br />
=== 使用 WINEARCH ===<br />
对于64位用户,如果使用[multilib]仓库里的Wine,默认创建的系统目录是64位环境的。若想使用纯32位环境,修改{{ic|WINEARCH}} 变量win32为即可:<br />
{{ic |1=$ WINEARCH=win32 winecfg}}这样就会生成32位Wine环境。若不设置{{ic|WINEARCH}}得到的就是64位环境。<br />
<br />
通过{{ic|WINEPREFIX}}变量,在不同的系统目录分别创建32位和64位环境:<br />
$ WINEARCH=win32 WINEPREFIX=~/win32 winecfg <br />
$ WINEPREFIX=~/win64 winecfg<br />
<br />
{{注意|系统目录创建过程中,64位版本的wine将视全部目录如同64位系统目录,也将不会在已存在的目录中创建任何32位的.创建32位系统目录,您必须让Wine创建指定的{{ic|WINEPREFIX}}目录。}}<br />
<br />
winetricks也接受{{ic|WINEPREFIX}}变量,以安装Steam为例:<br />
env WINEARCH=win32 WINEPREFIX=~/.local/share/wineprefixes/steam winetricks steam<br />
{{注意|不必手动在{{ic|wineprefixes}}文件夹下建立steam文件夹,Wine会自动创建不存在的系统目录.}}<br />
<br />
=== 显卡驱动 ===<br />
使用Wine运行Windows游戏时,可能需要高性能的显卡驱动。[[Nvidia (简体中文)|Nvidia]]、[[ATI (简体中文)|Amd/ATI]]用户最好使用闭源驱动。[[Intel (简体中文)|Intel]]显卡用户也可以选择开源驱动,它已经非常成熟。<br />
<br />
要是显卡驱动有问题或者相关配置有误,控制台用Wine运行某些程序时会输出:<br />
Direct rendering is disabled, most likely your OpenGL drivers have not been installed correctly<br />
<br />
x86-64用户需要从[multilib]或[[AUR (简体中文)|AUR]]安装额外的32位库:<br />
* '''NVIDIA''':{{ic|# pacman -S lib32-nvidia-utils}} 关于旧版本lib32-nvidia-utils(例如nvidia-96xx驱动),请到[https://aur.archlinux.org/packages.php?K=lib32-nvidia-utils 这里]查找。<br />
<br />
* '''NVIDIA (使用nouveau-dri驱动)''': {{ic|# pacman -S lib32-nouveau-dri}} <br />
<br />
* '''Intel''':{{ic|# pacman -S lib32-intel-dri}} 运行Wine时需要手动添加{{ic|1=LIBGL_DRIVERS_PATH=/usr/lib32/xorg/modules/dri}}<br />
<br />
* '''AMD/ATI''':{{ic|# pacman -S lib32-ati-dri}} 如果用的是闭源驱动:{{ic|# pacman -S {{Pkg|lib32-catalyst-utils}}}}。<br />
<br />
{{注意|安装上述软件包后,可能需要重启X才能生效!}}<br />
<br />
=== 声音 ===<br />
Wine程序有可能遇到某些声音问题。首先,确保{{ic|winecfg}}中只启用了一种声卡驱动。目前,Wine对[[Alsa (简体中文)|Alsa]]的支持最好。<br />
<br />
x86_64平台下使用[[Alsa (简体中文)|Alsa]]的话,需要安装{{Pkg|lib32-alsa-lib}}。如果还要使用PulseAudio,则需安装{{Pkg|lib32-libpulse}}。<br />
<br />
若使用[[OSS (简体中文)|OSS]],需要安装{{Pkg|lib32-alsa-oss}}。仅靠内核驱动是不行的。<br />
<br />
安装上述软件包后,若{{ic|winecfg}}'''仍'''无法识别声卡(Selected driver: (none)),请尝试[http://wiki.jswindle.com/index.php/Wine_Registry#Configuring_Sound registry 通过注册表配置]。<br />
<br />
<br />
运行使用某些高级声音系统的游戏,可能还需要安装{{Pkg|lib32-openal}}。<br />
<br />
=== 其他函数库 ===<br />
<br />
某些程序(如 Office 2003)需要解析HTML、XML(使用MSXML库),需要安装{{Pkg|lib32-libxml2}}。<br />
<br />
播放音频的程序可能依赖{{Pkg|lib32-mpg123}}。<br />
<br />
对于使用图像处理库的程序,可能依赖{{Pkg|lib32-giflib}}和{{Pkg|lib32-libpng}},<br />
<br />
x86_64的加密支持需要{{Pkg|lib32-gnutls}}软件包。<br />
<br />
=== 字体 ===<br />
<br />
如果没有安装微软Truetype字体,Wine程序的字体显示可能会一团糟,参见[[MS Fonts (简体中文)]]。如果还是不行,试试{{ic|winetricks allfonts}}。<br />
<br />
上述操作后,杀死wine相关进程再运行{{ic|winecfg}},字体应该变好看了。<br />
<br />
如果字体看起来很毛糙,试试用[http://wiki.winehq.org/regedit regedit]导入下列文本文件:<br />
[HKEY_CURRENT_USER\Software\Wine\X11 Driver]<br />
"ClientSideWithRender"="N"<br />
<br />
=== 启动器和菜单 ===<br />
<br />
Wine不会为内置程序(如{{ic|winecfg}}、{{ic|winebrowser}})创建桌面启动器和菜单项。但手动安装的Windows程序通常会自动创建启动器和菜单项。在Windows下,安装程序(如{{ic|setup.exe}})通常会在桌面和开始菜单建立快捷方式,而Wine下会创建遵循freedesktop.org规范的.desktop文件(即启动器,相当于快捷方式)。<br />
<br />
{{小贴士|如果启动器''没有''自动创建,或者这些文件丢失了,可以尝试使用[http://wiki.winehq.org/winemenubuilder winemenubuilder]修复。}}<br />
<br />
Ubuntu下,Wine项目以子菜单形式出现在系统菜单。以下步骤将实现这个效果:<br />
<br />
==== 创建菜单项 ====<br />
首先,用Wine安装一个Windows程序,以建立基本的菜单。完成后,向其中添加菜单项。桌面右键选择{{ic|"创建启动器..."}}(不同桌面环境操作有所差异),设置如下:<br />
'''类型(Type)''': 应用程序(Application)<br />
'''名称(Name)''': 配置<br />
'''命令(Command)''': winecfg<br />
'''备注(Comment)''': Wine配置工具<br />
<br />
'''类型''': 应用程序<br />
'''名称''': 卸载程序<br />
'''命令''': wine uninstaller<br />
'''备注''': 卸载Wine下的Windows程序<br />
<br />
'''类型''': 应用程序<br />
'''名称''': 浏览 C:\<br />
'''命令''': wine winebrowser c:\\<br />
'''备注''': 浏览Wine中虚拟的C盘<br />
现在,桌面上出现了三个启动器,下面将把它们移入菜单。不过首先,我们给这些启动器加上动态图标(由图标主题提供)。方法是,用文本编辑器打开启动器,编辑Icon项目:<br />
<br />
{{ic|配置}} 启动器:<br />
Icon=wine-winecfg<br />
{{ic|卸载程序}} 启动器:<br />
Icon=wine-uninstaller<br />
{{ic|浏览 C:\}} 启动器:<br />
Icon=wine-winefile<br />
{{小贴士|多数桌面环境在上述“创建启动器”步骤即可设置图标。以第一个启动器为例,在选择图标窗口中搜索wine-winecfg,选择即可,无需手动编辑。 ——译者注}}<br />
如果图标无法显示或者你觉得很丑陋,换成其他图标也可以。右键设置启动器,应该有更改图标的地方。很多图标主题,例如[http://www.gnome-look.org/content/show.php/GNOME-colors?content=82562 GNOME-colors],都提供这些图标。<br />
<br />
现在,将启动器移入菜单。把启动器复制到 {{ic|~/.local/share/applications/wine/}} 目录即可。<br />
<br />
诶?图标还没出现在菜单中!还剩下最后一步,创建下列文本文件:<br />
{{hc|~/.config/menus/applications-merged/wine-utilities.menu|<nowiki><br />
<!DOCTYPE Menu PUBLIC "-//freedesktop//DTD Menu 1.0//EN"<br />
"http://www.freedesktop.org/standards/menu-spec/menu-1.0.dtd"><br />
<Menu><br />
<Name>Applications</Name><br />
<Menu><br />
<Name>wine-wine</Name><br />
<Directory>wine-wine.directory</Directory><br />
<Include><br />
<Filename>wine-Configuration.desktop</Filename><br />
</Include><br />
<Include><br />
<Filename>wine-Browse C:\.desktop</Filename><br />
</Include><br />
<Include><br />
<Filename>wine-Uninstall Programs.desktop</Filename><br />
</Include><br />
</Menu><br />
</Menu><br />
</nowiki>}}<br />
再看看菜单,应该万事大吉了。<br />
<br />
==== Gnome3 中清理 Wine 菜单启动项 ====<br />
系统全局的菜单启动器安装在 {{ic|/usr/share/applications/}},清除相应程序的“.desktop”文件即可从整个系统删除该启动器。<br />
<br />
如果这样还是无法解决问题,那么很可能 Wine 的启动器存放在用户级别的 {{ic|~/.local/share/applications/wine/Programs/}} 目录中。删除相应的“.desktop”文件即可清理对应启动项。删除整个 Programs 文件夹将清理所有 Wine 程序的启动项。<br />
<br />
==== 修复 KDE 4 菜单问题[https://bugs.launchpad.net/ubuntu/+source/wine/+bug/263041] ====<br />
Wine菜单项有可能错误地出现在{{ic|"Lost & Found(其他)"}} ,而非Wine子菜单。原因是kde-applications.menu文件缺失{{ic|MergeDir}}配置。<br />
<br />
编辑{{ic|/etc/xdg/menus/kde-applications.menu}}。<br />
<br />
在文件末尾处,{{ic|<DefaultMergeDirs/>}}后添加{{ic|<MergeDir>applications-merged</MergeDir>}}。修改后内容大致如下:<br />
<Menu><br />
<Include><br />
<And><br />
<Category>KDE</Category><br />
<Category>Core</Category><br />
</And><br />
</Include><br />
<DefaultMergeDirs/><br />
'''<MergeDir>applications-merged</MergeDir>'''<br />
<MergeFile>applications-kmenuedit.menu</MergeFile><br />
</Menu><br />
<br />
另一个方法是:<br />
ln -s ~/.config/menus/applications-merged ~/.config/menus/kde-applications-merged<br />
<br />
这样的好处是,不会因为KDE升级而重置配置。但该方法只对一个用户有效。<br />
<br />
== 运行 Windows 程序 ==<br />
{{警告|千万不要以root身份运行Wine!详情参见[http://wiki.winehq.org/FAQ#run_as_root 本文]。}}<br />
运行Windows程序:<br />
$ wine <exe文件><br />
<br />
内置的msiexec程序可以运行MSI安装包:<br />
$ msiexec installername.msi<br />
== 技巧 ==<br />
{{小贴士|此外您可能会感兴趣以下文章的开始所提供的链接<br />
* [http://appdb.winehq.org/ Wine程序数据库 (Wine Application Database, AppDB)] —— 特定Windows程序的Wine兼容情况(运行时的已知问题、用户评分、指南等等)<br />
* [http://forum.winehq.org/ WineHQ论坛] —— 要是看完上述网页还有问题,可以到这里咨询<br />
}}<br />
<br />
<br />
这里介绍一些安装Windows组件的工具。由于这些工具可能严重破坏Wine配置,没有需要时最好不要使用。<br />
<br />
=== 安装 Microsoft Office ===<br />
<br />
安装Office套装前,需要先安装某些Windows组件:<br />
<br />
$ WINEARCH=win32 WINEPREFIX=/path/to/wineprefix winecfg<br />
# pacman -S winetricks<br />
$ winetricks msxml3 # For MS Office 2007<br />
$ winetricks msxml3 msxml6 # For MS Office 2010<br />
$ wine /path/to/office_cd/setup.exe<br />
<br />
更多信息,参见[http://appdb.winehq.org/appview.php?iVersionId=4992 WineHQ上的文章]。<br />
<br />
=== OpenGL 模式 ===<br />
很多游戏(比如魔兽争霸啦)都支持OpenGL模式,在Wine下''可能''比默认DirectX模式性能更好。一般添加{{Ic|-opengl}}启动程序即可,但''不同程序可能有所不同'':<br />
$ wine /path/to/3d_game.exe -opengl<br />
<br />
请参考[http://appdb.winehq.org AppDB],了解特定程序的相关信息。<br />
<br />
=== 将 Wine 作为 Win16/Win32 程序的解释器 ===<br />
可以通知内核把Wine作为Win16/Win32程序的解释器。<br />
<br />
设置过程在 [[systemd (简体中文)|systemd]] 和 [[initscripts (简体中文)|initscripts]] 下有所差异。<br />
<br />
==== systemd ====<br />
通知内核识别和执行 Win16/Win32 程序的方式:<br />
echo ':DOSWin:M::MZ::/usr/bin/wine:' > /proc/sys/fs/binfmt_misc/register<br />
To make the setting permanent, create a configuration file in {{ic|/etc/tmpfiles.d}} with the following contents:<br />
测试效果,若一切正常,可以使该设置永久生效。在 {{ic|/etc/tmpfiles.d}} 目录创建新的配置文件,内容为:<br />
{{hc|/etc/tmpfiles.d/enable-doswin-exe.conf|<br />
w /proc/sys/fs/binfmt_misc/register - - - - :DOSWin:M::MZ::/usr/bin/wine:}}<br />
<br />
说明一下,和 initscripts 不同,systemd 会自动挂载 {{ic|/proc/sys/fs/binfmt_misc}},所以只需要通过临时文件机制向内核写入配置即可。<br />
<br />
更多信息,参见 [[Systemd (简体中文)#临时文件]]。<br />
<br />
==== initscripts ====<br />
首先,挂载 {{ic|binfmt_misc}}文件系统:<br />
# mount -t binfmt_misc none /proc/sys/fs/binfmt_misc<br />
或者,如果你想一直这么做,将以下内容加入{{ic|/etc/fstab}}:<br />
none /proc/sys/fs/binfmt_misc binfmt_misc defaults 0 0<br />
然后,告知内核如何解释(运行)Win16/Win32程序:<br />
echo ':DOSWin:M::MZ::/usr/bin/wine:' > /proc/sys/fs/binfmt_misc/register<br />
如果你想一直这么做,添加上述内容到{{ic|/etc/rc.local}}即可。最后的重定向是为了避免切换启动级别时的错误信息:<br />
{ echo ':DOSWin:M::MZ::/usr/bin/wine:' > /proc/sys/fs/binfmt_misc/register; } 2>/dev/null<br />
<br />
==== 效果测试 ====<br />
现在,直接运行Windows程序试试:<br />
chmod 755 exefile.exe<br />
./exefile.exe<br />
<br />
=== Wine 控制台 ===<br />
<br />
有些时候,可能需要运行{{ic|.exe}}给游戏打补丁,比如给古董游戏添加宽屏支持。这时直接通过Wine运行可能没有用。那么,打开终端,运行一下命令:<br />
<br />
$ wineconsole cmd<br />
<br />
将进入一个和Windows下cmd一样的命令行环境。在该环境下试试也许就可以了。<br />
<br />
=== Winetricks ===<br />
使用[http://wiki.winehq.org/winetricks Winetricks]快速脚本,能够方便地安装许多Windows组件,包括DirectX、msxml(被Office 2007、IE浏览器依赖)visual运行库还有其他更多的。<br />
<br />
您可以使用[[pacman (简体中文)|pacman]]或者从[[AUR (简体中文)|AUR]]上获取{{AUR|winetricks-svn}}软件包来安装该工具:<br />
<br />
运行:<br />
<br />
$ winetricks<br />
<br />
== 第三方界面 ==<br />
这些程序有其自己的主页和支持论坛。<br />
<br />
=== CrossOver ===<br />
<br />
[http://www.codeweavers.com/about/ CrossOver] 有单独的[[CrossOver|wiki 页面]].<br />
<br />
=== PlayOnLinux ===<br />
[http://www.playonlinux.com/ PlayOnLinux]是一个图形界面的Windows/DOS程序管理器。它提供了一些帮助配置/运行程序的脚本,能够管理多个不同版本的Wine,甚至能对不同程序使用不同Wine版本。参考[http://appdb.winehq.org AppDB],看看哪个Wine版本对你要运行的程序兼容最好。从[[AUR (简体中文)|AUR]]安装{{AUR|playonlinux}}。<br />
<br />
=== PyWinery ===<br />
[http://code.google.com/p/pywinery/ PyWinery]是一个简单的、图形界面的Wine系统目录管理器,用它可以方便地管理不同系统目录,并从不同系统目录运行程序。同时可以开启winetricks在同一系统目录,打开系统目录所在文件夹, {{ic|winecfg}}, 软件卸载程序和wineDOS。[[AUR (简体中文)|AUR]]中提供了软件包{{AUR|pywinery}}。当你使用很多系统目录(一个打游戏用、一个编程用……)时,这个程序会非常有用。<br />
<br />
它在默认情况下使用winetricks打开{{ic|.exe}}文件,所以你可以选择你有的任何Wine的配置。<br />
<br />
=== Q4wine ===<br />
[http://q4wine.brezblock.org.ua/ Q4Wine] 是一个图形界面的系统目录(wine-prefix)管理器。它的特色是可以把 QT 主题导入 Wine 配置,使两者完美整合。{{Pkg|q4wine}} 软件包在 <nowiki>[</nowiki>[[multilib]]] 仓库中提供。<br />
<br />
== 相关链接 ==<br />
* [http://www.winehq.com/ Wine官方网站]<br />
* [http://appdb.winehq.org/ Wine程序数据库]<br />
* [http://linuxgamingtoday.wordpress.com/2008/02/16/quick-tips-to-speed-up-your-gaming-in-wine/ 加速Wine,显卡及OpenGL高级配置]<br />
* [http://wiki.gotux.net/code:perl:fileinfo FileInfo] —— Find Win32 PE/COFF headers in EXE/DLL/OCX files under linux/unix environment.</div>
Cuihao