Merge pull request #1 from LCTT/master

pull origin
This commit is contained in:
外卖小哥 2020-06-18 09:04:43 +08:00 committed by GitHub
commit 1271933498
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 808 additions and 241 deletions

View File

@ -1,241 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: (geekpi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (5 ways to examine the content of files on Linux)
[#]: via: (https://www.networkworld.com/article/3561490/5-ways-to-examine-the-content-of-files-on-linux.html)
[#]: author: (Sandra Henry-Stocker https://www.networkworld.com/author/Sandra-Henry_Stocker/)
5 ways to examine the content of files on Linux
======
How to use the cat, more, head and tail commands to look at the content of Linux files, not just text files.
[mari lezhava / Unsplash][1] [(CC0)][2]
Linux provides many commands for examining the contents of files including **cat**, **more,** **head** and **tail,** but that's just a start.
For one thing, even the most obvious commands have a lot more options than many Linux users ever get around to using. And there are some less obvious commands that offer some unique features. In this post, well both at commands for viewing the contents of files and options for tailoring those views to better cater to your needs.
### cat
The cat command sends the entire contents of text files to your terminal window for viewing. In fact, if you type “cat” followed by the name of a file with thousands of lines, those lines will whiz by your window so fast, you wont be able to make out much more of it than the last screenful of text. Yet, as familiar as the cat command is to Linux users, even this basic command provides a lot of useful options such as numbering the lines in the output that many of us likely havent ever used. To expand on that, not only can you number lines; you have some choices in how you do the numbering.
Numbering every line looks like this:
```
$ cat -n msg
1 Hello --
2
3 I hope you are having a wonderful day!
4
5
6 That's it for ... now
7
8 bye!
9
10 s.
```
You can also number only lines with content. Note that a line containing only blanks is not considered “empty” for this command and would be numbered.
```
$ cat -b msg
1 Hello --
2 I hope you are having a wonderful day!
3 That's it for ... now
4 bye!
5 s.
```
The cat command allows you to ignore repeated blank lines with the -s option, but you have to add another command to suppress blanks lines altogether.
```
$ cat -s msg
Hello --
I hope you are having a wonderful day!
That's it for ... now
bye!
s.
```
To ignore all of the blank lines, just pipe the output from **cat** to a **grep** command as follows. The dot (.) matches text including blank characters, so it will display lines that contain only blanks and only look empty.
```
$ cat msg | grep .
Hello --
I hope you are having a wonderful day!
That's it for ... now
bye!
s.
```
The **-E** optoin provides a visual cue to show whether there are extra blanks at the ends of lines by sticking a $ at the end of every line.
```
$ cat -E msg
Hello --$
$
I hope you are having a wonderful day! $
$
$
That's it for ... now$
$
bye!$
$
s.$
```
With **-A**, you get both the $ characters at the end of each line and tabs showing up as **^I** rather than empty space.
```
$ cat -A msg
Hello --$
$
I hope you are having a wonderful day!$
$
$
Thats it for …^Inow$
$
bye!$
$
s.$
```
### Displaying portions of files with head and tail
The **head** and **tail** commands show the tops or bottoms of files and default to ten lines. You can specify a different number of lines to view by using strings like -3 (show 3 lines) or -11 (show 11 lines). The **tail** command works the same way as **head** but displays the bottoms of files rather than the tops.
```
$ head -3 msg
Hello --
I hope you are having a wonderful day!
$ tail -3 msg
bye!
s.
```
You can also combine **head** and **tail** commands to view text between the tops of bottoms of files. You just have to pick your starting point and how many lines you want to see. In this example, the command would display the second hundred lines in a file and, with **cat**'s help, numbering those lines.
```
$ cat -b mybigfile | head -200 | tail -100
101 Invoice #2020-06-07a sent to vendor
...
```
### Browsing text a screenful at a time with more or less
The **more** command is an obvious choice for browsing through a files content a screenful at a time while **less** adds the ability to move up and down in a file by using the up and down keyboard arrows so that you can start scanning and then back up in the file.
### Looking at text files two ways at once with od
The **od** (octal dump) command allows you to view a file both as normal text and as a series of ASCII values (i.e., how that text is actually encoded in the files). As you can see in the example below, the numbered lines show the numeric ASCII values and the alternate lines show the text and non-printable characters.
```
$ od -bc msg
0000000 110 145 154 154 157 040 055 055 012 012 111 040 150 157 160 145
H e l l o - - \n \n I h o p e
0000020 040 171 157 165 040 141 162 145 040 150 141 166 151 156 147 040
y o u a r e h a v i n g
0000040 141 040 167 157 156 144 145 162 146 165 154 040 144 141 171 041
a w o n d e r f u l d a y !
0000060 012 012 012 124 150 141 164 047 163 040 151 164 040 146 157 162
\n \n \n T h a t ' s i t f o r
0000100 040 056 056 056 011 156 157 167 012 012 142 171 145 041 012 012
. . . \t n o w \n \n b y e ! \n \n
0000120 163 056 012
s . \n
```
Notice how newline characters are shown as **\n** (octal 012) and tabs are shown as **\t** (octal 011).
One of the especially helpful uses of the **od** command is for looking at non-text files for information that identifies the file types. In this case, we see the **JFIF** (JPEG File Interchange Format) label that identifies it as a jpg file to commands like **file** that report on file types. There's a lot of other useful information in there as well, especially if you're curious about how these files are formatted.
In this next command, we are looking at the beginning portion of a jpg file.
```
$ od -bc arrow.jpg | head -12
0000000 377 330 377 340 000 020 112 106 111 106 000 001 001 000 000 001
377 330 377 340 \0 020 J F I F \0 001 001 \0 \0 001
0000020 000 001 000 000 377 333 000 103 000 003 002 002 002 002 002 003
\0 001 \0 \0 377 333 \0 C \0 003 002 002 002 002 002 003
0000040 002 002 002 003 003 003 003 004 006 004 004 004 004 004 010 006
002 002 002 003 003 003 003 004 006 004 004 004 004 004 \b 006
0000060 006 005 006 011 010 012 012 011 010 011 011 012 014 017 014 012
006 005 006 \t \b \n \n \t \b \t \t \n \f 017 \f \n
0000100 013 016 013 011 011 015 021 015 016 017 020 020 021 020 012 014
\v 016 \v \t \t \r 021 \r 016 017 020 020 021 020 \n \f
0000120 022 023 022 020 023 017 020 020 020 377 333 000 103 001 003 003
022 023 022 020 023 017 020 020 020 377 333 \0 C 001 003 003
```
If we were to ask the **file** command for information on this image file, we might see something like this. The file command is pulling all this descriptive information from data at the beginning of the file.
```
$ file arrow.jpg
arrow.png: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, baseline, precision 8, 500x375, components 3
```
### Looking at files as text-based images with jp2a
If you are limited to the command line and want to get an idea what a particular image file contains, you can use a tool like **jp2a** (jpeg to ascii) to give you a character rendition. How recognizable the image will be in this format depends on the file. Dont expect much as the version of the image you will see is going to put “low res” to the test! This ones a very low resolution emperor penguin.
```
$ jp2a Emperor_Penguin.jpg
MMMMMMMMWOdkNMMMMMMMMMMMMMMMMMMM
MMMXK0kc.... ,OKMMMMMMMMMMMMMMMM
MMNK0Ol... :Xx'dNMMMMMMMMMMMMM
MMMMMMMd;lx00Oo. ..xMMMMMMMMMMMM
MMMMMMK.OXMMMMMN,...lMMMMMMMMMMM
MMMMMMx'KXNNMMMMK....0MMMMMMMMMM
MMMMMMx:kkKNWWMMMl.. 'NMMMMMMMMM
MMMMMMddx0NNNWMMMK'...;NMMMMMMMM
MMMMMMck0NNWWWWWMMd ..lMMMMMMMM
MMMMMM.d0KXNWWWWMMo ...WMMMMMMM
MMMMMM.xOXNNWNMMMW. ....KMMMMMMM
MMMMMM'kKNKWXWMMMK ..'.0MMMMMMM
MMMMMMxckXNNNNMMMX .:..XMMMMMMM
MMMMMMW;xKNWWWMMMM. .;. NMMMMMMM
MMMMMMMok0NNWNWMMMx .l..MMMMMMMM
MMMMMMMkxOKXWXNMMMMl.:'dMMMMMMMM
MMMMMMM0dKOdKXXNMMMMNx,WMMMMMMMM
MMMMMMMWoKxldXKNNMMMMM;MMMMMMMMM
MMMMMMMMxxxxdNWNXNMMMM;MMMMMMMMM
MMMMMMMMxOcoo0XOOOOWMW,kMMMMMMMM
MMMMMMM0xK;.cO0dNX:0XXd;NMMMMMMM
MMMNkdd:,'ldXXO0xl;x0kx:;lKMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
```
#### Wrap-Up
There are quite a few commands on Linux for examining the content of files in various ways. Some have options which can prove very helpful when you need to manipulate file contents. Others are just … interesting.
Join the Network World communities on [Facebook][3] and [LinkedIn][4] to comment on topics that are top of mind.
--------------------------------------------------------------------------------
via: https://www.networkworld.com/article/3561490/5-ways-to-examine-the-content-of-files-on-linux.html
作者:[Sandra Henry-Stocker][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.networkworld.com/author/Sandra-Henry_Stocker/
[b]: https://github.com/lujun9972
[1]: https://unsplash.com/photos/q65bNe9fW-w
[2]: https://creativecommons.org/publicdomain/zero/1.0/
[3]: https://www.facebook.com/NetworkWorld/
[4]: https://www.linkedin.com/company/network-world

View File

@ -0,0 +1,105 @@
[#]: collector: (lujun9972)
[#]: translator: ( )
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (4 essential tools to set up your Python environment for success)
[#]: via: (https://opensource.com/article/20/6/python-tools)
[#]: author: (Moshe Zadka https://opensource.com/users/moshez)
4 essential tools to set up your Python environment for success
======
This selection of tools will streamline your Python environment for
smooth and consistent development practices.
![Python programming language logo with question marks][1]
Python is a wonderful general-purpose programming language, often taught as a first programming language. Twenty years in, multiple books written, and it remains [my language of choice][2]. While the language is often said to be straight-forward, configuring Python for development has not been described as such (as documented by [xkcd][3]).
![xkcd python illustration][4]
A complex Python environment: [xkcd][3]
There are many ways to use Python in your day-to-day life. I will explain how I use the Python ecosystem tools, and I will be honest where I am still looking for alternatives.
### Use pyenv to manage Python versions
The best way I have found to get a Python version working on your machine is `pyenv`. This software will work on Linux, Mac OS X, and WSL2: the three "UNIX-like" environments that I usually care about.
Installing `pyenv` itself can be a little tricky at times. One way is to use the dedicated [pyenv installer][5], which uses a `curl | bash` method to bootstrap (see the instructions for more details).
If you're on a Mac (or another system where you run Homebrew), you can follow instructions on how to install and use pyenv [here][6].
Once you install and set up `pyenv` per the directions, you can use `pyenv global` to set a "default Python" version. In general, you will want to select your "favorite" version. This will usually be the latest stable, but other considerations can change that.
### Make virtual environments simpler with virtualenvwrapper
One advantage of using `pyenv` to install Python is that all subsequent Python interpreter installations you care about are owned by you instead of the operating system you use.
Though installing things inside Python itself is usually not the best option, there is one exception: in your "favorite" Python chosen above, install and configure `virtualenvwrapper`. This gives you the ability to create and switch to virtual environments at a moment's notice.
I walk through exactly how to install and use `virtualenvwrapper` [in this article][7].
Here is where I recommend a unique workflow. There is one virtual environment that you will want to make so that you can reuse it a lot—`runner`. In this environment, install your favorite `runner`; that is, software that you will regularly use to run other software. As of today, my preference is `tox`.
### Use tox as a Python runner
[tox][8] is a great tool to automate your test runs of Python. In each Python environment, I create a `tox.ini` file. Whatever system I use for continuous integration will run it, and I can run the same locally with `virtualenvwrapper`'s workon syntax described in the article above:
```
$ workon runner
$ tox
```
The reason this workflow is important is that I test my code against multiple versions of Python and multiple versions of the library dependencies. That means there are going to be multiple environments in the tox runner. Some will try running against the latest dependencies. Some will try running against frozen dependencies (more on that next), and I might also generate those locally with `pip-compile`.
Side note: I am currently [looking at `nox`][9] as a replacement for `tox`. The reasons are beyond the scope of this article, but it's worth taking a look at.
### Use pip-compile for Python dependency management
Python is a dynamic programming language, which means it loads its dependencies on every execution of the code. Understanding exactly what version of each dependency is running could mean the difference between smoothly running code and an unexpected crash. That means we have to think about dependency management tooling.
For each new project, I include a `requirements.in` file that is (usually) only the following:
```
`.`
```
Yes, that's right. A single line with a single dot. I document "loose" dependencies, such as `Twisted>=17.5` in the `setup.py`file. That is in contrast to exact dependencies like `Twisted==18.1`, which make it harder to upgrade to new versions of the library when you need a feature or a bug fix.
The `.` means "current directory," which uses the current directory's `setup.py` as the source for dependencies.
This means that using `pip-compile requirements.in > requirements.txt` will create a frozen dependencies file. You can use this dependencies file either in a virtual environment created by `virtualenvwrapper` or in `tox.ini`.
Sometimes it is useful to have `requirements-dev.txt`, generated from `requirements-dev.in` (contents: `.[dev]`) or `requirements-test.txt`, generated from `requirements-test.in` (contents: `.[test]`).
I am looking to see if `pip-compile` should be replaced in this flow by [`dephell`][10]. The `dephell` tool has a bunch of interesting things about it, like the use of asynchronous HTTP requests to speak dependency downloads.
### Conclusion
Python is as powerful as it is pleasing on the eyes. In order to write that code, I lean on a particular toolchain that has worked well for me. The tools `pyenv`, `virtualenvwrapper`, `tox`, and `pip-compile` are all separate. However, they each have their own role, with no overlaps, and together, they deliver a powerful Python workflow.
--------------------------------------------------------------------------------
via: https://opensource.com/article/20/6/python-tools
作者:[Moshe Zadka][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/moshez
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/python_programming_question.png?itok=cOeJW-8r (Python programming language logo with question marks)
[2]: https://opensource.com/article/19/10/why-love-python
[3]: https://xkcd.com/1987/
[4]: https://opensource.com/sites/default/files/uploads/python_environment_xkcd_1.png (xkcd python illustration)
[5]: https://github.com/pyenv/pyenv-installer
[6]: https://opensource.com/article/20/4/pyenv
[7]: https://opensource.com/article/19/6/python-virtual-environments-mac
[8]: https://opensource.com/article/19/5/python-tox
[9]: https://nox.thea.codes/en/stable/
[10]: https://github.com/dephell/dephell

View File

@ -0,0 +1,298 @@
[#]: collector: (lujun9972)
[#]: translator: ( )
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (How to handle dynamic and static libraries in Linux)
[#]: via: (https://opensource.com/article/20/6/linux-libraries)
[#]: author: (Stephan Avenwedde https://opensource.com/users/hansic99)
How to handle dynamic and static libraries in Linux
======
Knowing how Linux uses libraries, including the difference between
static and dynamic linking, can help you fix dependency problems.
![Hand putting a Linux file folder into a drawer][1]
Linux, in a way, is a series of static and dynamic libraries that depend on each other. For new users of Linux-based systems, the whole handling of libraries can be a mystery. But with experience, the massive amount of shared code built into the operating system can be an advantage when writing new applications.
To help you get in touch with this topic, I prepared a small [application example][2] that shows the most common methods that work on common Linux distributions (these have not been tested on other systems). To follow along with this hands-on tutorial using the example application, open a command prompt and type:
```
$ git clone <https://github.com/hANSIc99/library\_sample>
$ cd library_sample/
$ make
cc -c main.c -Wall -Werror
cc -c libmy_static_a.c -o libmy_static_a.o -Wall -Werror
cc -c libmy_static_b.c -o libmy_static_b.o -Wall -Werror
ar -rsv libmy_static.a libmy_static_a.o libmy_static_b.o
ar: creating libmy_static.a
a - libmy_static_a.o
a - libmy_static_b.o
cc -c -fPIC libmy_shared.c -o libmy_shared.o
cc -shared -o libmy_shared.so libmy_shared.o
$ make clean
rm *.o
```
After executing these commands, these files should be added to the directory (run `ls` to see them):
```
my_app
libmy_static.a
libmy_shared.so
```
### About static linking
When your application links against a static library, the library's code becomes part of the resulting executable. This is performed only once at linking time, and these static libraries usually end with a `.a` extension.
A static library is an archive ([ar][3]) of object files. The object files are usually in the ELF format. ELF is short for [Executable and Linkable Format][4], which is compatible with many operating systems.
The output of the `file` command tells you that the static library `libmy_static.a` is the `ar` archive type:
```
$ file libmy_static.a
libmy_static.a: current ar archive
```
With `ar -t`, you can look into this archive; it shows two object files:
```
$ ar -t libmy_static.a
libmy_static_a.o
libmy_static_b.o
```
You can extract the archive's files with `ar -x <archive-file>`. The extracted files are object files in ELF format:
```
$ ar -x libmy_static.a
$ file libmy_static_a.o
libmy_static_a.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
```
### About dynamic linking
Dynamic linking means the use of shared libraries. Shared libraries usually end with `.so` (short for "shared object").
Shared libraries are the most common way to manage dependencies on Linux systems. These shared resources are loaded into memory before the application starts, and when several processes require the same library, it will be loaded only once on the system. This feature saves on memory usage by the application.
Another thing to note is that when a bug is fixed in a shared library, every application that references this library will profit from it. This also means that if the bug remains undetected, each referencing application will suffer from it (if the application uses the affected parts).
It can be very hard for beginners when an application requires a specific version of the library, but the linker only knows the location of an incompatible version. In this case, you must help the linker find the path to the correct version.
Although this is not an everyday issue, understanding dynamic linking will surely help you in fixing such problems.
Fortunately, the mechanics for this are quite straightforward.
To detect which libraries are required for an application to start, you can use `ldd`, which will print out the shared libraries used by a given file:
```
$ ldd my_app
        linux-vdso.so.1 (0x00007ffd1299c000)
        libmy_shared.so =&gt; not found
        libc.so.6 =&gt; /lib64/libc.so.6 (0x00007f56b869b000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f56b8881000)
```
Note that the library `libmy_shared.so` is part of the repository but is not found. This is because the dynamic linker, which is responsible for loading all dependencies into memory before executing the application, cannot find this library in the standard locations it searches.
Errors associated with linkers finding incompatible versions of common libraries (like `bzip2`, for example) can be quite confusing for a new user. One way around this is to add the repository folder to the environment variable `LD_LIBRARY_PATH` to tell the linker where to look for the correct version. In this case, the right version is in this folder, so you can export it:
```
$ LD_LIBRARY_PATH=$(pwd):$LD_LIBRARY_PATH
$ export LD_LIBRARY_PATH
```
Now the dynamic linker knows where to find the library, and the application can be executed. You can rerun `ldd` to invoke the dynamic linker, which inspects the application's dependencies and loads them into memory. The memory address is shown after the object path:
```
$ ldd my_app
        linux-vdso.so.1 (0x00007ffd385f7000)
        libmy_shared.so =&gt; /home/stephan/library_sample/libmy_shared.so (0x00007f3fad401000)
        libc.so.6 =&gt; /lib64/libc.so.6 (0x00007f3fad21d000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f3fad408000)
```
To find out which linker is invoked, you can use `file`:
```
$ file my_app
my_app: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=26c677b771122b4c99f0fd9ee001e6c743550fa6, for GNU/Linux 3.2.0, not stripped
```
The linker `/lib64/ld-linux-x8664.so.2` is a symbolic link to `ld-2.30.so`, which is the default linker for my Linux distribution:
```
$ file /lib64/ld-linux-x86-64.so.2
/lib64/ld-linux-x86-64.so.2: symbolic link to ld-2.31.so
```
Looking back to the output of `ldd`, you can also see (next to `libmy_shared.so`) that each dependency ends with a number (e.g., `/lib64/libc.so.6`). The usual naming scheme of shared objects is:
```
`**lib** XYZ.so **.<MAJOR>** . **<MINOR>**`
```
On my system, `libc.so.6` is also a symbolic link to the shared object `libc-2.30.so` in the same folder:
```
$ file /lib64/libc.so.6
/lib64/libc.so.6: symbolic link to libc-2.31.so
```
If you are facing the issue that an application will not start because the loaded library has the wrong version, it is very likely that you can fix this issue by inspecting and rearranging the symbolic links or specifying the correct search path (see "The dynamic loader: ld.so" below).
For more information, look on the [`ldd` man page][5].
#### Dynamic loading
Dynamic loading means that a library (e.g., a `.so` file) is loaded during a program's runtime. This is done using a certain programming scheme.
Dynamic loading is applied when an application uses plugins that can be modified during runtime.
See the [`dlopen` man page][6] for more information.
#### The dynamic loader: ld.so
On Linux, you mostly are dealing with shared objects, so there must be a mechanism that detects an application's dependencies and loads them into memory.
`ld.so` looks for shared objects in these places in the following order:
1. The relative or absolute path in the application (hardcoded with the `-rpath` compiler option on GCC)
2. In the environment variable `LD_LIBRARY_PATH`
3. In the file `/etc/ld.so.cache`
Keep in mind that adding a library to the systems library archive `/usr/lib64` requires administrator privileges. You could copy `libmy_shared.so` manually to the library archive and make the application work without setting `LD_LIBRARY_PATH`:
```
unset LD_LIBRARY_PATH
sudo cp libmy_shared.so /usr/lib64/
```
When you run `ldd`, you can see the path to the library archive shows up now:
```
$ ldd my_app
        linux-vdso.so.1 (0x00007ffe82fab000)
        libmy_shared.so =&gt; /lib64/libmy_shared.so (0x00007f0a963e0000)
        libc.so.6 =&gt; /lib64/libc.so.6 (0x00007f0a96216000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f0a96401000)
```
### Customize the shared library at compile time
If you want your application to use your shared libraries, you can specify an absolute or relative path during compile time.
Modify the makefile (line 10) and recompile the program by invoking `make -B` . Then, the output of `ldd` shows `libmy_shared.so` is listed with its absolute path.
Change this:
```
`CFLAGS =-Wall -Werror -Wl,-rpath,$(shell pwd)`
```
To this (be sure to edit the username):
```
`CFLAGS =/home/stephan/library_sample/libmy_shared.so`
```
Then recompile:
```
`$ make`
```
Confirm it is using the absolute path you set, which you can see on line 2 of the output:
```
$ ldd my_app
    linux-vdso.so.1 (0x00007ffe143ed000)
        libmy_shared.so =&gt; /lib64/libmy_shared.so (0x00007fe50926d000)
        /home/stephan/library_sample/libmy_shared.so (0x00007fe509268000)
        libc.so.6 =&gt; /lib64/libc.so.6 (0x00007fe50909e000)
        /lib64/ld-linux-x86-64.so.2 (0x00007fe50928e000)
```
This is a good example, but how would this work if you were making a library for others to use? New library locations can be registered by writing them to `/etc/ld.so.conf` or creating a `<library-name>.conf` file containing the location under `/etc/ld.so.conf.d/`. Afterward, `ldconfig` must be executed to rewrite the `ld.so.cache` file. This step is sometimes necessary after you install a program that brings some special shared libraries with it.
See the [`ld.so` man page][7] for more information.
### How to handle multiple architectures
Usually, there are different libraries for the 32-bit and 64-bit versions of applications. The following list shows their standard locations for different Linux distributions:
**Red Hat family**
* 32 bit: `/usr/lib`
* 64 bit: `/usr/lib64`
**Debian family**
* 32 bit: `/usr/lib/i386-linux-gnu`
* 64 bit: `/usr/lib/x86_64-linux-gnu`
**Arch Linux family**
* 32 bit: `/usr/lib32`
* 64 bit: `/usr/lib64`
[**FreeBSD**][8] (technical not a Linux distribution)
* 32bit: `/usr/lib32`
* 64bit: `/usr/lib`
Knowing where to look for these key libraries can make broken library links a problem of the past.
While it may be confusing at first, understanding dependency management in Linux libraries is a way to feel in control of the operating system. Run through these steps with other applications to become familiar with common libraries, and continue to learn how to fix any library challenges that could come up along your way.
--------------------------------------------------------------------------------
via: https://opensource.com/article/20/6/linux-libraries
作者:[Stephan Avenwedde][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/hansic99
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/yearbook-haff-rx-linux-file-lead_0.png?itok=-i0NNfDC (Hand putting a Linux file folder into a drawer)
[2]: https://github.com/hANSIc99/library_sample
[3]: https://en.wikipedia.org/wiki/Ar_%28Unix%29
[4]: https://linuxhint.com/understanding_elf_file_format/
[5]: https://www.man7.org/linux/man-pages/man1/ldd.1.html
[6]: https://www.man7.org/linux/man-pages/man3/dlopen.3.html
[7]: https://www.man7.org/linux/man-pages/man8/ld.so.8.html
[8]: https://opensource.com/article/20/5/furybsd-linux

View File

@ -0,0 +1,163 @@
[#]: collector: (lujun9972)
[#]: translator: ( )
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Internet connection sharing with NetworkManager)
[#]: via: (https://fedoramagazine.org/internet-connection-sharing-networkmanager/)
[#]: author: (bengal https://fedoramagazine.org/author/bengal/)
Internet connection sharing with NetworkManager
======
![][1]
NetworkManager is the network configuration daemon used on Fedora and many other distributions. It provides a consistent way to configure network interfaces and other network-related aspects on a Linux machine. Among many other features, it provides a Internet connection sharing functionality that can be very useful in different situations.
For example, suppose you are in a place without Wi-Fi and want to share your laptops mobile data connection with friends. Or maybe you have a laptop with broken Wi-Fi and want to connect it via Ethernet cable to another laptop; in this way the first laptop become able to reach the Internet and maybe download new Wi-Fi drivers.
In cases like these it is useful to share Internet connectivity with other devices. On smartphones this feature is called “Tethering” and allows sharing a cellular connection via Wi-Fi, Bluetooth or a USB cable.
This article shows how the connection sharing mode offered by NetworkManager can be set up easily; it addition, it explains how to configure some more advanced features for power users.
### How connection sharing works
The basic idea behind connection sharing is that there is an _upstream_ interface with Internet access and a _downstream_ interface that needs connectivity. These interfaces can be of a different type—for example, Wi-Fi and Ethernet.
If the upstream interface is connected to a LAN, it is possible to configure our computer to act as a _bridge_; a bridge is the software version of an Ethernet switch. In this way, you “extend” the LAN to the downstream network. However this solution doesnt always play well with all interface types; moreover, it works only if the upstream network uses private addresses.
A more general approach consists in assigning a private IPv4 subnet to the downstream network and turning on routing between the two interfaces. In this case, NAT (Network Address Translation) is also necessary. The purpose of NAT is to modify the source of packets coming from the downstream network so that they look as if they originate from your computer.
It would be inconvenient to configure manually all the devices in the downstream network. Therefore, you need a DHCP server to assign addresses automatically and configure hosts to route all traffic through your computer. In addition, in case the sharing happens through Wi-Fi, the wireless network adapter must be configured as an access point.
There are many tutorials out there explaining how to achieve this, with different degrees of difficulty. NetworkManager hides all this complexity and provides a _shared_ mode that makes this configuration quick and convenient.
### Configuring connection sharing
The configuration paradigm of NetworkManager is based on the concept of connection (or connection profile). A connection is a group of settings to apply on a network interface.
This article shows how to create and modify such connections using _nmcli_, the NetworkManager command line utility, and the GTK connection editor. If you prefer, other tools are available such as _nmtui_ (a text-based user interface), GNOME control center or the KDE network applet.
A reasonable prerequisite to share Internet access is to have it available in the first place; this implies that there is already a NetworkManager connection active. If you are reading this, you probably already have a working Internet connection. If not, see [this article][2] for a more comprehensive introduction to NetworkManager.
The rest of this article assumes you already have a Wi-Fi connection profile configured and that connectivity must be shared over an Ethernet interface _enp1s0_.
To enable sharing, create a connection for interface enp1s0 and set the ipv4.method property to _shared_ instead of the usual _auto_:
```
$ nmcli connection add type ethernet ifname enp1s0 ipv4.method shared con-name local
```
The shared IPv4 method does multiple things:
* enables IP forwarding for the interface;
* adds firewall rules and enables masquerading;
* starts dnsmasq as a DHCP and DNS server.
NetworkManager connection profiles, unless configured otherwise, are activated automatically. The new connection you have added should be already active in the device status:
```
$ nmcli device
DEVICE TYPE STATE CONNECTION
enp1s0 ethernet connected local
wlp4s0 wifi connected home-wifi
```
If that is not the case, activate the profile manually with _nmcli connection up local_.
### Changing the shared IP range
Now look at how NetworkManager configured the downstream interface enp1s0:
```
$ ip -o addr show enp1s0
8: enp1s0 inet 10.42.0.1/24 brd 10.42.0.255 ...
```
10.42.0.1/24 is the default address set by NetworkManager for a device in shared mode. Addresses in this range are also distributed via DHCP to other computers. If the range conflicts with other private networks in your environment, change it by modifying the _ipv4.addresses_ property:
```
$ nmcli connection modify local ipv4.addresses 192.168.42.1/24
```
Remember to activate again the connection profile after any change to apply the new values:
```
$ nmcli connection up local
$ ip -o addr show enp1s0
8: enp1s0 inet 192.168.42.1/24 brd 192.168.42.255 ...
```
If you prefer using a graphical tool to edit connections, install the _nm-connection-editor_ package. Launch the program and open the connection to edit; then select the _Shared to other computers_ method in the _IPv4 Settings_ tab. Finally, if you want to use a specific IP subnet, click _Add_ and insert an address and a netmask.
![][3]
### Adding custom dnsmasq options
In case you want to further extend the dnsmasq configuration, you can add new configuration snippets in _/etc/NetworkManager/dnsmasq-shared.d/_. For example, the following configuration:
```
dhcp-option=option:ntp-server,192.168.42.1
dhcp-host=52:54:00:a4:65:c8,192.168.42.170
```
tells dnsmasq to advertise a NTP server via DHCP. In addition, it assigns a static IP to a client with a certain MAC.
There are many other useful options in the dnsmasq manual page. However, remember that some of them may conflict with the rest of the configuration; so please use custom options only if you know what you are doing.
### Other useful tricks
If you want to set up sharing via Wi-Fi, you could create a connection in Access Point mode, manually configure the security, and then enable connection sharing. Actually, there is a quicker way, the hotspot mode:
```
$ nmcli device wifi hotspot [ifname $dev] [password $pw]
```
This does everything needed to create a functional access point with connection sharing. The interface and password options are optional; if they are not specified, _nmcli_ chooses the first Wi-Fi device available and generates a random password. Use the _nmcli device wifi show-password_ command to display information for the active hotspot; the output includes the password and a text-based QR code that you can scan with a phone:
![][4]
### What about IPv6?
Until now this article discussed sharing IPv4 connectivity. NetworkManager also supports sharing IPv6 connectivity through DHCP prefix delegation. Using prefix delegation, a computer can request additional IPv6 prefixes from the DHCP server. Those public routable addresses are assigned to local networks via Router Advertisements. Again, NetworkManager makes all this easier through the shared IPv6 mode:
```
$ nmcli connection modify local ipv6.method shared
```
Note that IPv6 sharing requires support from the Internet Service Provider, which should give out prefix delegations through DHCP. If the ISP doesnt provides delegations, IPv6 sharing will not work; in such case NM will report in the journal that no prefixes are available:
```
policy: ipv6-pd: none of 0 prefixes of wlp1s0 can be shared on enp1s0
```
Also, note that the Wi-Fi hotspot command described above only enables IPv4 sharing; if you want to also use IPv6 sharing you must edit the connection manually.
### Conclusion
Remember, the next time you need to share your Internet connection, NetworkManager will make it easy for you.
If you have suggestions on how to improve this feature or any other feedback, please reach out to the NM community using the [mailing list][5], the [issue tracker][6] or joining the _#nm_ IRC channel on _freenode_.
--------------------------------------------------------------------------------
via: https://fedoramagazine.org/internet-connection-sharing-networkmanager/
作者:[bengal][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://fedoramagazine.org/author/bengal/
[b]: https://github.com/lujun9972
[1]: https://fedoramagazine.org/wp-content/uploads/2020/06/networkmanager-connection_sharing-816x345.png
[2]: https://www.redhat.com/sysadmin/becoming-friends-networkmanager
[3]: https://fedoramagazine.org/wp-content/uploads/2020/06/nmce.png
[4]: https://fedoramagazine.org/wp-content/uploads/2020/06/hotspot-password.png
[5]: https://mail.gnome.org/mailman/listinfo/networkmanager-list
[6]: https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/issues

View File

@ -0,0 +1,242 @@
[#]: collector: (lujun9972)
[#]: translator: (geekpi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (5 ways to examine the content of files on Linux)
[#]: via: (https://www.networkworld.com/article/3561490/5-ways-to-examine-the-content-of-files-on-linux.html)
[#]: author: (Sandra Henry-Stocker https://www.networkworld.com/author/Sandra-Henry_Stocker/)
在 Linux 上查看文件内容的 5 种方法
======
如何使用 cat、more、head 和 tail 命令查看 Linux 文件的内容,而不仅仅是文本文件。
[mari lezhava / Unsplash][1] [(CC0)][2]
Linux 提供了许多命令来查看文件的内容,包 括**cat**、**more**、**head** 和 **tail**,但这只是一个开始。
一方面,即使是最显而易见的命令也有很多许多用户不会去使用的选项。还有一些不太明显的命令提供了一些独特的功能。在本文中,我们将同时提供查看文件内容的命令和用于定制这些视图以更好地满足你的需求的选项。
### cat
cat 命令将文本文件的全部内容发送到终端窗口以供查看。实际上,如果你输入 “cat”然后输入包含数千行的文件名那么这些行将以极快的速度在你的窗口中滚动你将无法看到除了最后一屏外的其他文本。对于 Linux 用户来说 cat 命令很熟悉,但即使这个基本命令也提供了许多有用的选项,例如对输出中的行进行编号,这是我们许多人可能从未使用过的。扩展一下,不仅可以对行进行编号,你还可以选择如何编号。
对每行进行编号就像这样:
```
$ cat -n msg
1 Hello --
2
3 I hope you are having a wonderful day!
4
5
6 That's it for ... now
7
8 bye!
9
10 s.
```
你也可以仅对内容行编号。 请注意,对于此命令,仅包含空格的行不被视为“空”,而是会被编号。
```
$ cat -b msg
1 Hello --
2 I hope you are having a wonderful day!
3 That's it for ... now
4 bye!
5 s.
```
cat 命令允许你使用 -s 选项忽略重复的空白行,但是你必须添加另一个命令来完全禁止空白行。
```
$ cat -s msg
Hello --
I hope you are having a wonderful day!
That's it for ... now
bye!
s.
```
要忽略所有空白行,只需如下将 **cat** 的输出通过管道传递给 **grep** 命令。 点(.)匹配包含空白字符的文本,因此它将显示仅包含空格且看起来为空的行。
```
$ cat msg | grep .
Hello --
I hope you are having a wonderful day!
That's it for ... now
bye!
s.
```
**-E** 选项通过在每行末尾加 $ 符提供视觉提示,来显示行尾是否还有多余的空格。
```
$ cat -E msg
Hello --$
$
I hope you are having a wonderful day! $
$
$
That's it for ... now$
$
bye!$
$
s.$
```
使用 **-A** 时,每行的末尾都有 $ 字符,并且制表符都显示为 **^I** 而不是空白。
```
$ cat -A msg
Hello --$
$
I hope you are having a wonderful day!$
$
$
Thats it for …^Inow$
$
bye!$
$
s.$
```
### 使用 head 和 tail 显示文件部分内容
**head** 和 **tail** 显示文件的头部或底部,默认为十行。 你可以使用 -3显示 3 行)或 -11显示 11 行)之类的字符串来指定要查看的其他行数。**tail** 命令与 **head** 的工作方式相同,但是显示文件的底部而不是头部。
```
$ head -3 msg
Hello --
I hope you are having a wonderful day!
$ tail -3 msg
bye!
s.
```
你还可以结合使用 head 和 tail 命令来查看文件中间的文本。 你只需要选择起点和想要查看行数即可。 在此例中,命令将在文件中显示第二个一百行,并在 **cat** 的帮助下为这些行编号。
```
$ cat -b mybigfile | head -200 | tail -100
101 Invoice #2020-06-07a sent to vendor
...
```
### 使用 more 或者 less 浏览一屏文本
**more** 命令是一次浏览一屏内容的选择,而 **less** 通过使用上下键盘箭头增加了在文件中上下移动的能力,这样你就可以扫描,然后备份到文件中。
### 使用 od 查看文本的两种方法
**od**(八进制转储)命令能够以常规文本和一系列 ASCII 值(即该文本在文件中的实际编码方式)的形式查看。 在下面的例子中可以看到,带编号的行显示了 ASCII 数字值,而其他各行则显示了文本和不可打印的字符。
```
$ od -bc msg
0000000 110 145 154 154 157 040 055 055 012 012 111 040 150 157 160 145
H e l l o - - \n \n I h o p e
0000020 040 171 157 165 040 141 162 145 040 150 141 166 151 156 147 040
y o u a r e h a v i n g
0000040 141 040 167 157 156 144 145 162 146 165 154 040 144 141 171 041
a w o n d e r f u l d a y !
0000060 012 012 012 124 150 141 164 047 163 040 151 164 040 146 157 162
\n \n \n T h a t ' s i t f o r
0000100 040 056 056 056 011 156 157 167 012 012 142 171 145 041 012 012
. . . \t n o w \n \n b y e ! \n \n
0000120 163 056 012
s . \n
```
请注意,换行符显示为 **\n** (八进制 012而制表符显示为 **\t**(八进制 011
**od** 命令特别有用的用途之一是查看非文本文件以获取标识文件类型的信息。 在这里,我们看到 **JFIF**JPEG 文件交换格式)标签,该标签让 **file** 之类报告文件类型的命令将它标示为 jpg 文件。 这里还有很多其他有用的信息,特别是如果你对这些文件的格式感到好奇的话。
在接下来的命令中,我们查看 jpg 文件的开始部分。
```
$ od -bc arrow.jpg | head -12
0000000 377 330 377 340 000 020 112 106 111 106 000 001 001 000 000 001
377 330 377 340 \0 020 J F I F \0 001 001 \0 \0 001
0000020 000 001 000 000 377 333 000 103 000 003 002 002 002 002 002 003
\0 001 \0 \0 377 333 \0 C \0 003 002 002 002 002 002 003
0000040 002 002 002 003 003 003 003 004 006 004 004 004 004 004 010 006
002 002 002 003 003 003 003 004 006 004 004 004 004 004 \b 006
0000060 006 005 006 011 010 012 012 011 010 011 011 012 014 017 014 012
006 005 006 \t \b \n \n \t \b \t \t \n \f 017 \f \n
0000100 013 016 013 011 011 015 021 015 016 017 020 020 021 020 012 014
\v 016 \v \t \t \r 021 \r 016 017 020 020 021 020 \n \f
0000120 022 023 022 020 023 017 020 020 020 377 333 000 103 001 003 003
022 023 022 020 023 017 020 020 020 377 333 \0 C 001 003 003
```
如果我们要 **file** 命令提供有关此图像的信息我们可能会看到类似这样的信息。file 命令从文件开头的数据中提取所有这些描述性信息。
```
$ file arrow.jpg
arrow.png: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, baseline, precision 8, 500x375, components 3
```
### 使用 jp2a 将文件视为基于文本的图像
如果你只能在命令行,并且想了解特定图像包含的内容,那么可以使用 **jp2a**jpeg to ascii之类的工具提供字符再现。 图像在这种格式下的识别程度取决于文件。不要太期待,因为你将看到的图像版本是“低分辨率”下的测试! 这是一只分辨率很低的帝王企鹅。
```
$ jp2a Emperor_Penguin.jpg
MMMMMMMMWOdkNMMMMMMMMMMMMMMMMMMM
MMMXK0kc.... ,OKMMMMMMMMMMMMMMMM
MMNK0Ol... :Xx'dNMMMMMMMMMMMMM
MMMMMMMd;lx00Oo. ..xMMMMMMMMMMMM
MMMMMMK.OXMMMMMN,...lMMMMMMMMMMM
MMMMMMx'KXNNMMMMK....0MMMMMMMMMM
MMMMMMx:kkKNWWMMMl.. 'NMMMMMMMMM
MMMMMMddx0NNNWMMMK'...;NMMMMMMMM
MMMMMMck0NNWWWWWMMd ..lMMMMMMMM
MMMMMM.d0KXNWWWWMMo ...WMMMMMMM
MMMMMM.xOXNNWNMMMW. ....KMMMMMMM
MMMMMM'kKNKWXWMMMK ..'.0MMMMMMM
MMMMMMxckXNNNNMMMX .:..XMMMMMMM
MMMMMMW;xKNWWWMMMM. .;. NMMMMMMM
MMMMMMMok0NNWNWMMMx .l..MMMMMMMM
MMMMMMMkxOKXWXNMMMMl.:'dMMMMMMMM
MMMMMMM0dKOdKXXNMMMMNx,WMMMMMMMM
MMMMMMMWoKxldXKNNMMMMM;MMMMMMMMM
MMMMMMMMxxxxdNWNXNMMMM;MMMMMMMMM
MMMMMMMMxOcoo0XOOOOWMW,kMMMMMMMM
MMMMMMM0xK;.cO0dNX:0XXd;NMMMMMMM
MMMNkdd:,'ldXXO0xl;x0kx:;lKMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
```
#### 总结
Linux 上有很多命令可以通过各种方式查看文件的内容。 其中一些选项在你需要处理文件内容时可能会非常有用。 其他的只是……有趣。
加入 [Facebook][3] 和 [LinkedIn][4] 上的 Network World 社区,评论热门主题。
--------------------------------------------------------------------------------
via: https://www.networkworld.com/article/3561490/5-ways-to-examine-the-content-of-files-on-linux.html
作者:[Sandra Henry-Stocker][a]
选题:[lujun9972][b]
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.networkworld.com/author/Sandra-Henry_Stocker/
[b]: https://github.com/lujun9972
[1]: https://unsplash.com/photos/q65bNe9fW-w
[2]: https://creativecommons.org/publicdomain/zero/1.0/
[3]: https://www.facebook.com/NetworkWorld/
[4]: https://www.linkedin.com/company/network-world