2
0
mirror of https://github.com/LCTT/TranslateProject.git synced 2025-03-24 02:20:09 +08:00

Merge remote-tracking branch 'LCTT/master'

This commit is contained in:
Xingyu.Wang 2018-08-22 11:20:00 +08:00
commit 29cf724d89
9 changed files with 802 additions and 810 deletions

View File

@ -1,13 +1,15 @@
重温 wallabagInstapaper 的开源替代品
重温 wallabagInstapaper 的开源替代品
======
> 这个稍后阅读应用增加了功能,使其成为诸如 Pocket、Paper 和 Instapaper 之类应用的可靠替代品。
![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/email_paper_envelope_document.png?itok=uPj_kouJ)
早在 2014 年,我[写了篇关于 wallabag 的文章][1],它是稍后阅读应用如 Instapaper 和 Pocket 的开源替代品。如果你愿意,去看看那篇文章吧。别担心,我会等你的。
早在 2014 年,我[写了篇关于 wallabag 的文章][1],它是诸如 Instapaper 和 Pocket 这样的稍后阅读应用的开源替代品。如果你愿意,去看看那篇文章吧。别担心,我会等你的。
好了么?很好
好了么?很好
自从我写这篇文章的四年来,[wallabag][2]的很多东西都发生了变化。现在是时候看看 wallabag 是如何成熟的。
自从我写这篇文章的四年来,[wallabag][2] 的很多东西都发生了变化。现在是时候悄悄看一下 wallabag 是如何成熟的。
### 有什么新的
@ -15,11 +17,11 @@
那么这些变化有哪些呢?有[很多][3]。以下是我发现最有趣和最有用的内容。
除了使 wallabag 更加快速和稳定之外,程序的导入和导出内容的能力也得到了提高。你可以从 Pocket 和 Instapaper 导入文章,也可导入书签服务 [Pinboard][4] 中标记为 “To read” 的文章。你还可以导入 Firefox 和 Chrome 书签。
除了使 wallabag 更加快速和稳定之外,该应用的导入和导出内容的能力也得到了提高。你可以从 Pocket 和 Instapaper 导入文章,也可导入书签服务 [Pinboard][4] 中标记为 “To read” 的文章。你还可以导入 Firefox 和 Chrome 书签。
你还可以以多种格式导出文章,包括 EPUB、MOBI、PDF 和纯文本。你可以为单篇文章、所有未读文章或所有已读和未读执行此操作。我四年前使用的 wallabag 版本可以导出到 EPUB 和 PDF但有时导出很糟糕。现在这些导出快速而顺利。
Web 界面中的注释和高亮显示现在可以更好,更一致地工作。不可否认,我并不经常使用它们 - 但它们不会像 wallabag v1 那样随机消失。
Web 界面中的注释和高亮显示现在可以更好、更一致地工作。不可否认,我并不经常使用它们 —— 但它们不会像 wallabag v1 那样随机消失。
![](https://opensource.com/sites/default/files/uploads/wallabag-annotation.png)
@ -27,7 +29,7 @@ wallabag 的外观和感觉也有所改善。这要归功于受 [Material Design
![](https://opensource.com/sites/default/files/uploads/wallabag-theme.png)
其中一个最大的变化是引入了 wallabag 的[托管版本][6]。不止一些人(包括你在内)没有服务器来运行网络程序,并且不太愿意这样做。当遇到任何技术问题时,我很窘迫。我不介意每年花 9 欧元(我写这篇文章的时候只要 10 美元),以获得一个我不需要关注的程序的完整工作版本。
其中一个最大的变化是引入了 wallabag 的[托管版本][6]。不是只有少数人(包括你在内)没有服务器来运行网络程序,并且也不太愿意维护台服务器。当遇到任何技术问题时,我很窘迫。我不介意每年花 9 欧元(我写这篇文章的时候只要 10 美元),以获得一个我不需要关注的程序的完整工作版本。
### 没有改变什么
@ -37,11 +39,11 @@ Wallabag 的[浏览器扩展][7]以同样的方式完成同样的工作。我发
### 有什么令人失望的
移动应用良好,但没有很棒。它在渲染文章方面做得很好,并且有一些配置选项。但是你不能高亮或注释文章。也就是说,你可以使用该程序浏览你的存档文章。
移动应用良好,但不算很棒。它在渲染文章方面做得很好,并且有一些配置选项。但是你不能高亮或注释文章。也就是说,你可以使用该程序浏览你的存档文章。
![](https://opensource.com/sites/default/files/uploads/wallabag-android.png)
虽然 wallabag 在收藏文章方面做得很好,但有些网站的内容却无法保存。我没有碰到很多这样的网站,但已经遇到让人烦恼的情况。我不确定与 wallabag 有多大关系。相反,我怀疑它与网站的编码方式有关 - 我在使用几个专有的稍后阅读工具时遇到了同样的问题。
虽然 wallabag 在收藏文章方面做得很好,但有些网站的内容却无法保存。我没有碰到很多这样的网站,但已经遇到让人烦恼的情况。我不确定与 wallabag 有多大关系。相反,我怀疑它与网站的编码方式有关 —— 我在使用几个专有的稍后阅读工具时遇到了同样的问题。
Wallabag 可能不是 Pocket 或 Instapaper 的等功能的替代品,但它做得很好。自从我第一次写这篇文章以来的四年里,它已经有了明显的改善。它仍然有改进的余地,但要做好它宣传的。
@ -56,7 +58,7 @@ via: https://opensource.com/article/18/7/wallabag
作者:[Scott Nesbitt][a]
选题:[lujun9972](https://github.com/lujun9972)
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
校对:[wxy](https://github.com/wxy)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出

View File

@ -0,0 +1,234 @@
在 Linux 中如何以人性化的方式显示数据
======
> 许多 Linux 命令现在都有使其输出更易于理解的选项。让我们了解一些可以让我们心爱的操作系统更友好的东西。
![](https://images.idgesg.net/images/article/2018/08/smile-face-on-hand-100767756-large.jpg)
不是每个人都以二进制方式思考他们不想在大脑中给大数字插入逗号来了解文件的大小。因此Linux 命令在过去的几十年里不断发展,以更人性化的方式向用户显示信息,这一点也不奇怪。在今天的文章中,我们将看一看各种命令所提供的一些选项,它们使得数据变得更容易理解。
### 为什么默认显示不更友好一些?
如果你想知道为什么默认不显示得更人性化,毕竟,我们人类才是计算机的默认用户啊。你可能会问自己:“为什么我们不竭尽全力输出对每个人都有意义的命令的响应?”主要的答案是:改变命令的默认输出可能会干扰许多其它进程,这些进程是在期望默认响应之上构建的。其它的工具,以及过去几十年开发的脚本,如果突然以一种完全不同的格式输出,而不是它们过去所期望的那样,可能会被一种非常丑陋的方式破坏。
说真的,也许我们中的一些人可能更愿意看文件大小中的所有数字,即 1338277310 而不是 1.3G。在任何情况下,切换默认习惯都可能造成破坏,但是为更加人性化的响应提供一些简单的选项只需要让我们学习一些命令选项而已。
### 可以显示人性化数据的命令
有哪些简单的选项可以使 Unix 命令的输出更容易解析呢?让我们来看一些命令。
#### top
你可能没有注意到这个命令,但是在 top 命令中,你可以通过输入 `E`(大写字母 E来更改显示全部内存使用的方式。连续按下将数字显示从 KiB 到 MiB再到 GiB接着是 TiB、PiB、EiB最后回到 KiB。
认识这些单位吧?这里有一组定义:
```
2`10 = 1,024 = 1 KiB (kibibyte)
2`20 = 1,048,576 = 1 MiB (mebibyte)
2`30 = 1,073,741,824 = 1 GiB (gibibyte)
2`40 = 1,099,511,627,776 = 1 TiB (tebibyte)
2`50 = 1,125,899,906,842,624 = PiB (pebibyte)
2`60 = 1,152,921,504,606,846,976 = EiB (exbibyte)
2`70 = 1,180,591,620,717,411,303,424 = 1 ZiB (zebibyte)
2`80 = 1,208,925,819,614,629,174,706,176 = 1 YiB (yobibyte)
```
这些单位与千字节KB、兆字节MB和千兆字节GB密切相关。虽然它们很接近但是它们之间仍有很大的区别一组是基于 10 的幂,另一组是基于 2 的幂。例如,比较千字节和千兆字节,我们可以看看它们不同点:
```
KB = 1000 = 10`3
KiB = 1024 = 2`10
```
以下是 `top` 命令输出示例,使用 KiB 为单位默认显示:
```
top - 10:49:06 up 5 days, 35 min, 1 user, load average: 0.05, 0.04, 0.01
Tasks: 158 total, 1 running, 118 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.2 sy, 0.0 ni, 99.8 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 6102680 total, 4634980 free, 392244 used, 1075456 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 5407432 avail Mem
```
在按下 `E` 之后,单位变成了 MiB
```
top - 10:49:31 up 5 days, 36 min, 1 user, load average: 0.03, 0.04, 0.01
Tasks: 158 total, 2 running, 118 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.6 sy, 0.0 ni, 99.4 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 5959.648 total, 4526.348 free, 383.055 used, 1050.246 buff/cache
MiB Swap: 2047.996 total, 2047.996 free, 0.000 used. 5280.684 avail Mem
```
再次按下 `E`,单位变为 GiB
```
top - 10:49:49 up 5 days, 36 min, 1 user, load average: 0.02, 0.03, 0.01
Tasks: 158 total, 1 running, 118 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
GiB Mem : 5.820 total, 4.420 free, 0.374 used, 1.026 buff/cache
GiB Swap: 2.000 total, 2.000 free, 0.000 used. 5.157 avail Mem
```
你还可以通过按字母 `e` 来更改为显示每个进程使用内存的数字单位。它将从默认的 KiB 到 MiB再到 GiB、TiB接着到 PiB估计你能看到小数点后的很多 0然后返回 KiB。下面是按了一下 `e` 之后的 `top` 输出:
```
top - 08:45:28 up 4 days, 22:32, 1 user, load average: 0.02, 0.03, 0.00
Tasks: 167 total, 1 running, 118 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.2 us, 0.0 sy, 0.0 ni, 99.8 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 6102680 total, 4641836 free, 393348 used, 1067496 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 5406396 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
784 root 20 0 543.2m 26.8m 16.1m S 0.9 0.5 0:22.20 snapd
733 root 20 0 107.8m 2.0m 1.8m S 0.4 0.0 0:18.49 irqbalance
22574 shs 20 0 107.5m 5.5m 4.6m S 0.4 0.1 0:00.09 sshd
1 root 20 0 156.4m 9.3m 6.7m S 0.0 0.2 0:05.59 systemd
```
#### du
`du` 命令显示磁盘空间文件或目录使用了多少,如果使用 `-h` 选项则将输出大小调整为最合适的单位。默认情况下它以千字节KB为单位。
```
$ du camper*
360 camper_10.jpg
5684 camper.jpg
240 camper_small.jpg
$ du -h camper*
360K camper_10.jpg
5.6M camper.jpg
240K camper_small.jpg
```
#### df
`df` 命令也提供了一个 `-h` 选项。请注意在下面的示例中是如何以千兆字节GB和兆字节MB输出的
```
$ df -h | grep -v loop
Filesystem Size Used Avail Use% Mounted on
udev 2.9G 0 2.9G 0% /dev
tmpfs 596M 1.7M 595M 1% /run
/dev/sda1 110G 9.0G 95G 9% /
tmpfs 3.0G 0 3.0G 0% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 3.0G 0 3.0G 0% /sys/fs/cgroup
tmpfs 596M 16K 596M 1% /run/user/121
/dev/sdb2 457G 73M 434G 1% /apps
tmpfs 596M 0 596M 0% /run/user/1000
```
下面的命令使用了 `-h` 选项,同时使用 `-T` 选项来显示我们正在查看的文件系统的类型。
```
$ df -hT /mnt2
Filesystem Type Size Used Avail Use% Mounted on
/dev/sdb2 ext4 457G 73M 434G 1% /apps
```
#### ls
即使是 `ls`,它也为我们提供了调整大小显示的选项,保证是最合理的单位。
```
$ ls -l camper*
-rw-rw-r-- 1 shs shs 365091 Jul 14 19:42 camper_10.jpg
-rw-rw-r-- 1 shs shs 5818597 Jul 14 19:41 camper.jpg
-rw-rw-r-- 1 shs shs 241844 Jul 14 19:45 camper_small.jpg
$ ls -lh camper*
-rw-rw-r-- 1 shs shs 357K Jul 14 19:42 camper_10.jpg
-rw-rw-r-- 1 shs shs 5.6M Jul 14 19:41 camper.jpg
-rw-rw-r-- 1 shs shs 237K Jul 14 19:45 camper_small.jpg
```
#### free
`free` 命令允许你以字节B千字节KB兆字节MB和千兆字节GB为单位查看内存使用情况。
```
$ free -b
total used free shared buff/cache available
Mem: 6249144320 393076736 4851625984 1654784 1004441600 5561253888
Swap: 2147479552 0 2147479552
$ free -k
total used free shared buff/cache available
Mem: 6102680 383836 4737924 1616 980920 5430932
Swap: 2097148 0 2097148
$ free -m
total used free shared buff/cache available
Mem: 5959 374 4627 1 957 5303
Swap: 2047 0 2047
$ free -g
total used free shared buff/cache available
Mem: 5 0 4 0 0 5
Swap: 1 0 1
```
#### tree
虽然 `tree` 命令与文件或内存计算无关但它也提供了非常人性化的文件视图它分层显示文件以说明文件是如何组织的。当你试图了解如何安排目录内容时这种显示方式非常有用。LCTT 译注:也可以看看 `pstree`,它以树状结构显示进程树。)
```
$ tree
.g to
├── 123
├── appended.png
├── appts
├── arrow.jpg
├── arrow.png
├── bin
│ ├── append
│ ├── cpuhog1
│ ├── cpuhog2
│ ├── loop
│ ├── mkhome
│ ├── runme
```
#### stat
`stat` 命令是另一个以非常人性化的格式显示信息的命令。它提供了更多关于文件的元数据,包括文件大小(以字节和块为单位)、文件类型、设备和 inode索引节点、文件所有者和组名称和数字 ID、以数字和 rwx 格式显示的文件权限以及文件的最后访问和修改日期。在某些情况下,它也可能显示最初创建文件的时间。
```
$ stat camper*
File: camper_10.jpg
Size: 365091 Blocks: 720 IO Block: 4096 regular file
Device: 801h/2049d Inode: 796059 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/ shs) Gid: ( 1000/ shs)
Access: 2018-07-19 18:56:31.841013385 -0400
Modify: 2018-07-14 19:42:25.230519509 -0400
Change: 2018-07-14 19:42:25.230519509 -0400
Birth: -
File: camper.jpg
Size: 5818597 Blocks: 11368 IO Block: 4096 regular file
Device: 801h/2049d Inode: 796058 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/ shs) Gid: ( 1000/ shs)
Access: 2018-07-19 18:56:31.845013872 -0400
Modify: 2018-07-14 19:41:46.882024039 -0400
Change: 2018-07-14 19:41:46.882024039 -0400
Birth: -
```
### 总结
Linux 命令提供了许多选项,可以让用户更容易理解或比较它们的输出。对于许多命令,`-h` 选项会显示更友好的输出格式。对于其它的,你可能必须通过使用某些特定选项或者按下某个键来查看你希望的输出。我希望这其中一些选项会让你的 Linux 系统看起来更友好一点。
加入[Facebook][1] 和 [LinkedIn][2] 上的网络世界社区,一起来评论重要的话题吧。
--------------------------------------------------------------------------------
via: https://www.networkworld.com/article/3296631/linux/displaying-data-in-a-human-friendly-way-on-linux.html
作者:[Sandra Henry-Stocker][a]
选题:[lujun9972](https://github.com/lujun9972)
译者:[MjSeven](https://github.com/MjSeven)
校对:[wxy](https://github.com/wxy)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://www.networkworld.com/author/Sandra-Henry_Stocker/
[1]:https://www.facebook.com/NetworkWorld/
[2]:https://www.linkedin.com/company/network-world

View File

@ -1,3 +1,5 @@
translating---geekpi
Getting started with Etcher.io
======

View File

@ -1,373 +0,0 @@
Translting by MjSeven
6 Easy Ways to Check User Name And Other Information in Linux
======
This is very basic topic, everyone knows how to find a user information in Linux using **id** command. Some of the users are filtering a user information from **/etc/passwd** file.
We also using these commands to get a user information.
You may ask, Why are you discussing this basic topic? Even i thought the same, there is no other ways except this two but we are having some good alternatives too.
Those are giving more detailed information compared with those two, which is very helpful for newbies.
This is one of the basic command which helps admin to find out a user information in Linux. Everything is file in Linux, even user information were stored in a file.
**Suggested Read :**
**(#)** [How To Check User Created Date On Linux][1]
**(#)** [How To Check Which Groups A User Belongs To On Linux][2]
**(#)** [How To Force User To Change Password On Next Login In Linux][3]
All the users are added in `/etc/passwd` file. This keep user name and other related details. Users details will be stored in /etc/passwd file when you created a user in Linux. The passwd file contain each/every user details as a single line with seven fields.
We can find a user information using the below six methods.
* `id :`Print user and group information for the specified username.
* `getent :`Get entries from Name Service Switch libraries.
* `/etc/passwd file :`The /etc/passwd file contain each/every user details as a single line with seven fields.
* `finger :`User information lookup program
* `lslogins :`lslogins display information about known users in the system
* `compgen :`compgen is bash built-in command and it will show all available commands for the user.
### 1) Using id Command
id stands for identity. print real and effective user and group IDs. To print user and group information for the specified user, or for the current user.
```
# id daygeek
uid=1000(daygeek) gid=1000(daygeek) groups=1000(daygeek),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),118(lpadmin),128(sambashare)
```
Below are the detailed information for the above output.
* **`uid (1000/daygeek):`** It displays user ID & Name
* **`gid (1000/daygeek):`** It displays users primary group ID & Name
* **`groups:`** It displays users secondary groups ID & Name
### 2) Using getent Command
The getent command displays entries from databases supported by the Name Service Switch libraries, which are configured in /etc/nsswitch.conf.
getent command shows user details similar to /etc/passwd file, it shows every user details as a single line with seven fields.
```
# getent passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
centos:x:500:500:Cloud User:/home/centos:/bin/bash
prakash:x:501:501:2018/04/12:/home/prakash:/bin/bash
apache:x:48:48:Apache:/var/www:/sbin/nologin
nagios:x:498:498::/var/spool/nagios:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
nrpe:x:497:497:NRPE user for the NRPE service:/var/run/nrpe:/sbin/nologin
magesh:x:502:503:2g Admin - Magesh M:/home/magesh:/bin/bash
thanu:x:503:504:2g Editor - Thanisha M:/home/thanu:/bin/bash
sudha:x:504:505:2g Editor - Sudha M:/home/sudha:/bin/bash
```
Below are the detailed information about seven fields.
```
magesh:x:502:503:2g Admin - Magesh M:/home/magesh:/bin/bash
```
* **`Username (magesh):`** Username of created user. Characters length should be between 1 to 32.
* **`Password (x):`** It indicates that encrypted password is stored at /etc/shadow file.
* **`User ID (UID-502):`** It indicates the user ID (UID) each user should be contain unique UID. UID (0-Zero) is reserved for root, UID (1-99) reserved for system users and UID (100-999) reserved for system accounts/groups
* **`Group ID (GID-503):`** It indicates the group ID (GID) each group should be contain unique GID is stored at /etc/group file.
* **`User ID Info (2g Admin - Magesh M):`** It indicates the command field. This field can be used to describe the user information.
* **`Home Directory (/home/magesh):`** It indicates the user home directory.
* **`shell (/bin/bash):`** It indicates the users bash shell.
If you would like to display only user names from the getent command output, use the below format.
```
# getent passwd | cut -d: -f1
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
uucp
operator
games
gopher
ftp
nobody
dbus
vcsa
abrt
haldaemon
ntp
saslauth
postfix
sshd
tcpdump
centos
prakash
apache
nagios
rpc
nrpe
magesh
thanu
sudha
```
To display only home directory users, use the below format.
```
# getent passwd | grep '/home' | cut -d: -f1
centos
prakash
magesh
thanu
sudha
```
### 3) Using /etc/passwd file
The `/etc/passwd` is a text file that contains each user information, which is necessary to login Linux system. It maintain useful information about users such as username, password, user ID, group ID, user ID info, home directory and shell. The /etc/passwd file contain every user details as a single line with seven fields as described below.
```
# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
centos:x:500:500:Cloud User:/home/centos:/bin/bash
prakash:x:501:501:2018/04/12:/home/prakash:/bin/bash
apache:x:48:48:Apache:/var/www:/sbin/nologin
nagios:x:498:498::/var/spool/nagios:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
nrpe:x:497:497:NRPE user for the NRPE service:/var/run/nrpe:/sbin/nologin
magesh:x:502:503:2g Admin - Magesh M:/home/magesh:/bin/bash
thanu:x:503:504:2g Editor - Thanisha M:/home/thanu:/bin/bash
sudha:x:504:505:2g Editor - Sudha M:/home/sudha:/bin/bash
```
Below are the detailed information about seven fields.
```
magesh:x:502:503:2g Admin - Magesh M:/home/magesh:/bin/bash
```
* **`Username (magesh):`** Username of created user. Characters length should be between 1 to 32.
* **`Password (x):`** It indicates that encrypted password is stored at /etc/shadow file.
* **`User ID (UID-502):`** It indicates the user ID (UID) each user should be contain unique UID. UID (0-Zero) is reserved for root, UID (1-99) reserved for system users and UID (100-999) reserved for system accounts/groups
* **`Group ID (GID-503):`** It indicates the group ID (GID) each group should be contain unique GID is stored at /etc/group file.
* **`User ID Info (2g Admin - Magesh M):`** It indicates the command field. This field can be used to describe the user information.
* **`Home Directory (/home/magesh):`** It indicates the user home directory.
* **`shell (/bin/bash):`** It indicates the users bash shell.
If you would like to display only user names from the /etc/passwd file, use the below format.
```
# cut -d: -f1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
uucp
operator
games
gopher
ftp
nobody
dbus
vcsa
abrt
haldaemon
ntp
saslauth
postfix
sshd
tcpdump
centos
prakash
apache
nagios
rpc
nrpe
magesh
thanu
sudha
```
To display only home directory users, use the below format.
```
# cat /etc/passwd | grep '/home' | cut -d: -f1
centos
prakash
magesh
thanu
sudha
```
### 4) Using finger Command
The finger comamnd displays information about the system users. It displays the users real name, terminal name and write status (as a * after the terminal name if write permission is denied), idle time and login time.
```
# finger magesh
Login: magesh Name: 2g Admin - Magesh M
Directory: /home/magesh Shell: /bin/bash
Last login Tue Jul 17 22:46 (EDT) on pts/2 from 103.5.134.167
No mail.
No Plan.
```
Below are the detailed information for the above output.
* **`Login:`** Users login name
* **`Name:`** Additional/Other information about the user
* **`Directory:`** User home directory information
* **`Shell:`** Users shell information
* **`LAST-LOGIN:`** Date of last login and other information
### 5) Using lslogins Command
It displays information about known users in the system. By default it will list information about all the users in the system.
The lslogins utility is inspired by the logins utility, which first appeared in FreeBSD 4.10.
```
# lslogins -u
UID USER PWD-LOCK PWD-DENY LAST-LOGIN GECOS
0 root 0 0 00:17:28 root
500 centos 0 1 Cloud User
501 prakash 0 0 Apr12/04:08 2018/04/12
502 magesh 0 0 Jul17/22:46 2g Admin - Magesh M
503 thanu 0 0 Jul18/00:40 2g Editor - Thanisha M
504 sudha 0 0 Jul18/01:18 2g Editor - Sudha M
```
Below are the detailed information for the above output.
* **`UID:`** User id
* **`USER:`** Name of the user
* **`PWD-LOCK:`** password defined, but locked
* **`PWD-DENY:`** login by password disabled
* **`LAST-LOGIN:`** Date of last login
* **`GECOS:`** Other information about the user
### 6) Using compgen Command
compgen is bash built-in command and it will show all available commands, aliases, and functions for you.
```
# compgen -u
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
uucp
operator
games
gopher
ftp
nobody
dbus
vcsa
abrt
haldaemon
ntp
saslauth
postfix
sshd
tcpdump
centos
prakash
apache
nagios
rpc
nrpe
magesh
thanu
sudha
```
--------------------------------------------------------------------------------
via: https://www.2daygeek.com/6-easy-ways-to-check-user-name-and-other-information-in-linux/
作者:[Prakash Subramanian][a]
选题:[lujun9972](https://github.com/lujun9972)
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://www.2daygeek.com/author/prakash/
[1]:https://www.2daygeek.com/how-to-check-user-created-date-on-linux/
[2]:https://www.2daygeek.com/how-to-check-which-groups-a-user-belongs-to-on-linux/
[3]:https://www.2daygeek.com/how-to-force-user-to-change-password-on-next-login-in-linux/

View File

@ -1,67 +0,0 @@
translating---geekpi
Convert file systems with Fstransform
======
![](https://fedoramagazine.org/wp-content/uploads/2018/08/converting-filesystems-816x345.jpg)
Few people know that they can convert their filesystems from one type to another without losing data, i.e. non-destructively. It may sound like magic, but [Fstransform][1] can convert an ext2, ext3, ext4, jfs, reiserfs or xfs partition to another type from the list in almost any combination. More importantly, it does so in-place, without formatting or copying data anywhere. Atop of all this goodness, there is a little bonus: Fstransform can also handle ntfs, btrfs, fat and exfat partitions as well.
### Before you run it
There are certain caveats and limitations in Fstransform, so it is strongly advised to back up before attempting a conversion. Additionally, there are some limitations to be aware of when using Fstransform:
* Both the source and target filesystems must be supported by your Linux kernel. Sounds like an obvious thing and exposes zero risk in case you want to use ext2, ext3, ext4, reiserfs, jfs and xfs partitions. Fedora supports all of that just fine.
* Upgrading ext2 to ext3 or ext4 does not require Fstransform. Use the Tune2fs utility instead.
* The device with source file system must have at least 5% of free space.
* You need to be able to unmount the source filesystem before you begin.
* The more data your source file system stores, the longer the conversion will last. The actual speed depends on your device, but expect it to be around one gigabyte per minute. The large amount of hard links can also slow down the conversion.
* Although Fstransform is proved to be stable, please back up data on your source filesystem.
### Installation instructions
Fstransform is already a part of Fedora. Install with the command:
```
sudo dnf install fstransform
```
### Time to convert something
![][2]
The syntax of the fstransform command is very simple: fstransform <source device> <target file system>. Keep in mind that it needs root privileges to run, so dont forget to add sudo in the beginning. Here goes an example:
```
sudo fstransform /dev/sdb1 ext4
```
Note that it is not possible to convert a root file system, which is a security measure. Use a test partition or an experimental thumb drive instead. In the meantime, Fstransform will through a lot of auxiliary output in the console. The most useful part is the estimated time of completion, which keep you informed about how long the process will take. Again, few small files on an almost empty drive will make Fstransform do its job in a minute or so, whereas more real-world tasks may involve hours of wait time.
### More file systems are supported
As mentioned above, it is possible to try Fstransform with ntfs, btrfs, fat and exfat partitions. These types are very experimental, and nobody can guarantee that the converion will flow perfect. Still, there are many success stories, and you can add your own by testing Fstransform with a sample data set on a test partition. Those additional file systems can be enabled by the use of the force-untested-file-systems parameter:
```
sudo fstransform /dev/sdb1 ntfs --force-untested-file-systems
```
Sometimes the process may iterrupt with an error. Feel free to repeat the command again — it may eventually complete the conversion from second or third attempt.
--------------------------------------------------------------------------------
via: https://fedoramagazine.org/transform-file-systems-in-linux/
作者:[atolstoy][a]
选题:[lujun9972](https://github.com/lujun9972)
译者:[译者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/atolstoy/
[1]:https://github.com/cosmos72/fstransform
[2]:https://fedoramagazine.org/wp-content/uploads/2018/08/Screenshot_20180805_230116.png

View File

@ -1,155 +1,149 @@
使用 Ptrace 去监听和仿真 Linux 系统调用 « null program
使用 Ptrace 去拦截和仿真 Linux 系统调用
======
`ptrace(2)`(”进程跟踪“)系统调用通常都与调试有关。它是类 Unix 系统上通过原生调试器监测调试进程的主要机制。它也是实现 [strace][1](系统调用跟踪)的常见方法。使用 Ptrace跟踪器可以暂停跟踪过程[检查和设置寄存器和内存][2],监视系统调用,甚至可以监听系统调用。
`ptrace(2)`(”<ruby>进程跟踪<rt>process trace</rt></ruby>“)系统调用通常都与调试有关。它是类 Unix 系统上通过原生调试器监测调试进程的主要机制。它也是实现 [strace][1]<ruby>系统调用跟踪<rt>system call trace</rt></ruby>)的常见方法。使用 Ptrace跟踪器可以暂停跟踪过程,[检查和设置寄存器和内存][2],监视系统调用,甚至可以拦截系统调用。
通过监听功能,意味着跟踪器可以修改系统调用参数,修改系统调用的返回值,甚至监听某些系统调用。言外之意就是,一个跟踪器可以完全服务于系统调用本身。这是件非常有趣的事,因为这意味着**一个跟踪器可以仿真一个完整的外部操作系统**,而这些都是在没有得到内核任何帮助的情况下由 Ptrace 实现的。
通过拦截功能,意味着跟踪器可以篡改系统调用参数,篡改系统调用的返回值,甚至阻塞某些系统调用。言外之意就是,一个跟踪器本身完全可以提供系统调用服务。这是件非常有趣的事,因为这意味着**一个跟踪器可以仿真一个完整的外部操作系统**,而这些都是在没有得到内核任何帮助的情况下由 Ptrace 实现的。
问题是,在同一时间一个进程只能被一个跟踪器附着,因此在那个进程的调试期间,不可能再使用诸如 GDB 这样的工具去仿真一个外部操作系统。另外的问题是,仿真系统调用的开销非常高。
在本文中,我们将专注于 x86-64 [Linux 的 Ptrace][3],并将使用一些 Linux 专用的扩展。同时,在本文中,我们将忽略掉一些错误检查,但是完整的源代码仍然会包含这些错误检查。
在本文中,我们将专注于 x86-64 [Linux 的 Ptrace][3],并将使用一些 Linux 专用的扩展。同时,在本文中,我们将忽略掉一些错误检查,但是完整的源代码仍然会包含这些错误检查。
本文中的可直接运行的示例代码在这里:
**<https://github.com/skeeto/ptrace-examples>**
本文中的可直接运行的示例代码在这里:<https://github.com/skeeto/ptrace-examples>
### strace
在进入到最有趣的部分之前,我们先从回顾 strace 的基本实现来开始。它不是 [DTrace][4],但 strace 仍然非常有用。
在进入到最有趣的部分之前,我们先从回顾 strace 的基本实现来开始。它[不是 DTrace][4],但 strace 仍然非常有用。
Ptrace 一直没有被标准化。它的接口在不同的操作系统上非常类似,尤其是在核心功能方面,但是在不同的系统之间仍然存在细微的差别。`ptrace(2)` 的原型基本上应该像下面这样,但特定的类型可能有些差别。
Ptrace 还没有被标准化。它的界面在不同的操作系统上非常类似,尤其是在核心功能方面,但是在不同的系统之间仍然存在细微的差别。`ptrace(2)` 的样子看起来应该像下面这样,但特定的类型可能有些差别。
```
long ptrace(int request, pid_t pid, void *addr, void *data);
```
`pid` 是跟踪进程 ID。虽然**同一个时间**只有一个跟踪器可以附着到进程上,但是一个跟踪器可以附着跟踪多个进程。
`pid`跟踪进程 ID。虽然**同一个时间**只有一个跟踪器可以附着到进程上,但是一个跟踪器可以附着跟踪多个进程。
`request` 字段选择一个具体的 Ptrace 函数,比如 `ioctl(2)` 接口。对于 strace只需要两个
* `PTRACE_TRACEME`:这个进程被它的父进程跟踪。
* `PTRACE_SYSCALL`:继续跟踪,但是在下一下系统调用入口或出口时停止。
* `PTRACE_GETREGS`:取得被跟踪进程的寄存器内容副本。
* `PTRACE_TRACEME`:这个进程被它的父进程跟踪。
* `PTRACE_SYSCALL`:继续跟踪,但是在下一下系统调用入口或出口时停止。
* `PTRACE_GETREGS`:取得被跟踪进程的寄存器内容副本。
另外两个字段,`addr` 和 `data`,作为所选的 Ptrace 函数的一般参数。一般情况下,可以忽略一个或全部忽略,在那种情况下,传递零个参数。
strace 接口实质上是另一个命令的前缀。
strace 接口实质上是前缀到另一个命令之前。
```
$ strace [strace options] program [arguments]
```
最小化的 strace 不需要任何选项,因此需要做的第一件事情是 — 假设它至少有一个参数 — 在 `argv` 尾部的 `fork(2)``exec(2)` 被跟踪进程。但是在加载目标程序之前,新的进程将告知内核,目标程序将被它的父进程继续跟踪。被跟踪进程将被这个 Ptrace 系统调用暂停。
最小化的 strace 不需要任何选项,因此需要做的第一件事情是 —— 假设它至少有一个参数 —— 在 `argv` 尾部的 `fork(2)``exec(2)` 被跟踪进程。但是在加载目标程序之前,新的进程将告知内核,目标程序将被它的父进程继续跟踪。被跟踪进程将被这个 Ptrace 系统调用暂停。
```
pid_t pid = fork();
switch (pid) {
case -1: /* error */
FATAL("%s", strerror(errno));
case 0: /* child */
ptrace(PTRACE_TRACEME, 0, 0, 0);
execvp(argv[1], argv + 1);
FATAL("%s", strerror(errno));
case -1: /* error */
FATAL("%s", strerror(errno));
case 0: /* child */
ptrace(PTRACE_TRACEME, 0, 0, 0);
execvp(argv[1], argv + 1);
FATAL("%s", strerror(errno));
}
```
父进程使用 `wait(2)` 等待子进程的 `PTRACE_TRACEME`,当 `wait(2)` 返回后,子进程将被暂停。
```
waitpid(pid, 0, 0);
```
在允许子进程继续运行之前,我们告诉操作系统,被跟踪进程被它的父进程的跟踪应该被终止。一个真实的 strace 实现可能会设置其它的选择,比如: `PTRACE_O_TRACEFORK`
在允许子进程继续运行之前,我们告诉操作系统,被跟踪进程和它的父进程应该一同被终止。一个真实的 strace 实现可能会设置其它的选择,比如: `PTRACE_O_TRACEFORK`
```
ptrace(PTRACE_SETOPTIONS, pid, 0, PTRACE_O_EXITKILL);
```
剩余部分就是一个简单的、无休止的循环了,每循环一次捕获一个系统调用。循环体总共有四步:
1. 等待进程进入下一个系统调用。
2. 输出一个系统调用的描述。
3. 允许系统调用去运行和等待返回。
4. 输出系统调用返回值。
1. 等待进程进入下一个系统调用。
2. 输出系统调用的一个描述。
3. 允许系统调用去运行并等待返回。
4. 输出系统调用返回值。
这个 `PTRACE_SYSCALL` 请求被用于等待下一个系统调用时开始,和等待那个系统调用退出。和前面一样,需要一个 `wait(2)` 去等待被跟踪进程进入期望的状态。
`PTRACE_SYSCALL` 要求用于等待下一个系统调用时开始,和等待那个系统调用去退出。和前面一样,需要一个 `wait(2)` 去等待跟踪进入期望的状态。
```
ptrace(PTRACE_SYSCALL, pid, 0, 0);
waitpid(pid, 0, 0);
```
`wait(2)` 返回时,线程寄存器中写入了被系统调用所产生的系统调用号和它的参数。尽管如此,操作系统将不再为这个系统调用提供服务。线程寄存器中的详细内容对后续操作很重要。
`wait(2)` 返回时,进行了系统调用的线程的寄存器中写入了该系统调用的系统调用号及其参数。尽管如此,*操作系统仍然没有为这个系统调用提供服务*。这个细节对后续操作很重要。
接下来的一步是采集系统调用信息。这是各个系统架构不同的地方。在 x86-64 上,[系统调用号是在 `rax` 中传递的][5],而参数(最多 6 个)是在 `rdi`、`rsi`、`rdx`、`r10`、`r8` 和 `r9` 中传递的。这些寄存器是由另外的 Ptrace 调用读取的,不过这里再也不需要 `wait(2)` 了,因为被跟踪进程的状态再也不会发生变化了。
接下来的一步是采集系统调用信息。这是得到特定系统架构的地方。在 x86-64 上,[系统调用号是在 `rax` 中传递的][5],而参数(最多 6 个)是在 `rdi`、`rsi`、`rdx`、`r10`、`r8`、和 `r9` 中传递的。另外的 Ptrace 调用将读取这些寄存器,不过这里再也不需要 `wait(2)` 了,因为跟踪状态再也不会发生变化了。
```
struct user_regs_struct regs;
ptrace(PTRACE_GETREGS, pid, 0, &regs);
long syscall = regs.orig_rax;
fprintf(stderr, "%ld(%ld, %ld, %ld, %ld, %ld, %ld)",
syscall,
(long)regs.rdi, (long)regs.rsi, (long)regs.rdx,
(long)regs.r10, (long)regs.r8, (long)regs.r9);
syscall,
(long)regs.rdi, (long)regs.rsi, (long)regs.rdx,
(long)regs.r10, (long)regs.r8, (long)regs.r9);
```
这里有一个告。由于 [内核的内部用途][6],系统调用号是保存在 `orig_rax` 中而不是 `rax` 中。而所有的其它系统调用参数都是非常简单明了的。
这里有一个告。由于 [内核的内部用途][6],系统调用号是保存在 `orig_rax` 中而不是 `rax` 中。而所有的其它系统调用参数都是非常简单明了的。
接下来是它的另一个 `PTRACE_SYSCALL``wait(2)`,然后是另一个 `PTRACE_GETREGS` 去获取结果。结果保存在 `rax` 中。
```
ptrace(PTRACE_GETREGS, pid, 0, &regs);
fprintf(stderr, " = %ld\n", (long)regs.rax);
```
这个简单程序的输出也是非常粗糙的。这里的系统调用都没有符号名,并且所有的参数都是以数字形式输出,甚至是一个指向缓冲区的指针。更完整的 strace 输出将能知道哪个参数是指针,以及 `process_vm_readv(2)` 为了从跟踪中正确输出内容而读取了哪些缓冲区
这个简单程序的输出也是非常粗糙的。这里的系统调用都没有符号名,并且所有的参数都是以数字形式输出,甚至是一个指向缓冲区的指针也是如此。更完整的 strace 输出将能知道哪个参数是指针,并使用 `process_vm_readv(2)` 从被跟踪进程中读取哪些缓冲区,以便正确输出它们
后,这些仅仅是系统调用监听的基础工作。
而,这些仅仅是系统调用拦截的基础工作。
### 系统调用监听
### 系统调用拦截
假设我们想使用 Ptrace 去实现如 OpenBSD 的 [`pledge(2)`][7] 这样的功能,它是 [一个进程承诺只使用一套受限的系统调用][8]。初步想法是,许多程序一般都有一个初始化阶段,这个阶段它们都需要进行许多的系统访问(比如,打开文件、绑定套接字、等等)。初始化完成以后,它们进行一个主循环,在主循环中它们处理输入,并且仅使用所需的、很少的一套系统调用。
假设我们想使用 Ptrace 去实现如 OpenBSD 的 [`pledge(2)`][7] 这样的功能,它是 [一个进程<ruby>承诺<rt>pledge</rt></ruby>只使用一套受限的系统调用][8]。初步想法是,许多程序一般都有一个初始化阶段,这个阶段它们都需要进行许多的系统访问(比如,打开文件、绑定套接字、等等)。初始化完成以后,它们进行一个主循环,在主循环中它们处理输入,并且仅使用所需的、很少的一套系统调用。
在进入主循环之前,可以限制一个进程只能运行它自己所需要的几个操作。如果 [程序有 Bug][9],允许通过恶意的输入去利用这个 Bug这个承诺可以有效地限制漏洞利用的实现。
在进入主循环之前,一个进程可以限制它自己只能运行所需要的几个操作。如果 [程序有缺陷][9],能够通过恶意的输入去利用该缺陷,这个**承诺**可以有效地限制漏洞利用的实现。
使用与 strace 相同的模型,但不是输出所有的系统调用,我们既能够拦截某些系统调用,也可以在它的行为异常时简单地终止被跟踪进程。终止它很容易:只需要在跟踪器中调用 `exit(2)`。因此,它也可以被设置为去终止被跟踪进程。拦截系统调用和允许子进程继续运行都只是些雕虫小技而已。
使用与 strace 相同的模型,但不是输出所有的系统调用,我们既能够阻塞某些系统调用,也可以在它的行为异常时简单地终止被跟踪进程。终止它很容易:只需要在跟踪器中调用 `exit(2)`。因此,它也可以被设置为去终止被跟踪进程。阻塞系统调用和允许子进程继续运行都只是些雕虫小技而已。
最棘手的部分是**当系统调用启动后没有办法去中断它**。进入系统调用之后,当跟踪器从 `wait(2)` 中返回,停止一个系统调用的仅有方式是,发生被跟踪进程终止的情况。
最棘手的部分是**当系统调用启动后没有办法去中断它**。当跟踪器在入口从 `wait(2)` 中返回到系统调用时,从一开始停止一个系统调用的仅有方式是,终止被跟踪进程。
然而,我们不仅可以“搞乱”系统调用的参数,也可以改变系统调用号本身,将它修改为一个不存在的系统调用。返回时,在 `errno` 中 [通过正常的内部信号][10],我们就可以报告一个“友好的”错误信息。
然而,我们不仅可以“搞乱”系统调用的参数,也可以改变系统调用号本身,将它修改为一个不存在的系统调用。返回时,在 `errno` 中 [通过正常的内部信号][10],我们就可以报告一个“友好的”错误信息。
```
for (;;) {
/* Enter next system call */
ptrace(PTRACE_SYSCALL, pid, 0, 0);
waitpid(pid, 0, 0);
/* Enter next system call */
ptrace(PTRACE_SYSCALL, pid, 0, 0);
waitpid(pid, 0, 0);
struct user_regs_struct regs;
ptrace(PTRACE_GETREGS, pid, 0, &regs);
struct user_regs_struct regs;
ptrace(PTRACE_GETREGS, pid, 0, &regs);
/* Is this system call permitted? */
int blocked = 0;
if (is_syscall_blocked(regs.orig_rax)) {
blocked = 1;
regs.orig_rax = -1; // set to invalid syscall
ptrace(PTRACE_SETREGS, pid, 0, &regs);
}
/* Is this system call permitted? */
int blocked = 0;
if (is_syscall_blocked(regs.orig_rax)) {
blocked = 1;
regs.orig_rax = -1; // set to invalid syscall
ptrace(PTRACE_SETREGS, pid, 0, &regs);
}
/* Run system call and stop on exit */
ptrace(PTRACE_SYSCALL, pid, 0, 0);
waitpid(pid, 0, 0);
/* Run system call and stop on exit */
ptrace(PTRACE_SYSCALL, pid, 0, 0);
waitpid(pid, 0, 0);
if (blocked) {
/* errno = EPERM */
regs.rax = -EPERM; // Operation not permitted
ptrace(PTRACE_SETREGS, pid, 0, &regs);
}
if (blocked) {
/* errno = EPERM */
regs.rax = -EPERM; // Operation not permitted
ptrace(PTRACE_SETREGS, pid, 0, &regs);
}
}
```
这个简单的示例只是检查了系统调用是否违反白名单或黑名单。而它们在这里并没有差别,比如,允许文件以只读而不是读写方式打开(`open(2)`),允许匿名内存映射但不允许非匿名映射等等。但是这里仍然没有办法去动态撤销被跟踪进程的权限。
@ -158,13 +152,14 @@ for (;;) {
### 创建一个人为的系统调用
对于我的这个类似于 pledge 的系统调用 — 我可以通过调用 `xpledge()` 将它与真实的系统调用区分开 — 我设置 10000 作为它的系统调用号,这是一个非常大的数字,真实的系统调用中从来不会用到它。
对于我的这个类似于 pledge 的系统调用 —— 我可以通过调用 `xpledge()` 将它与真实的系统调用区分开 —— 我设置 10000 作为它的系统调用号,这是一个非常大的数字,真实的系统调用中从来不会用到它。
```
#define SYS_xpledge 10000
```
为演示需要,我同时构建了一个非常小的界面,这在实践中并不是个好主意。它与 OpenBSD 的 `pledge(2)` 稍有一些相似之处,它使用了一个 [字符串界面][11]。事实上,设计一个健壮且安全的权限集是非常复杂的,正如在 `pledge(2)` 的手册页面上所显示的那样。下面是对被跟踪进程的完整界面和系统调用的实现:
为演示需要,我同时构建了一个非常小的接口,这在实践中并不是个好主意。它与 OpenBSD 的 `pledge(2)` 稍有一些相似之处,它使用了一个 [字符串接口][11]。*事实上*,设计一个健壮且安全的权限集是非常复杂的,正如在 `pledge(2)` 的手册页面上所显示的那样。下面是对被跟踪进程的系统调用的完整接口*和*实现:
```
#define _GNU_SOURCE
#include <unistd.h>
@ -173,36 +168,36 @@ for (;;) {
#define XPLEDGE_OPEN (1 << 1)
#define xpledge(arg) syscall(SYS_xpledge, arg)
```
如果给它传递个参数,仅允许一些基本的系统调用,包括那些用于去分配内存的系统调用(比如 `brk(2)`)。 `PLEDGE_RDWR` 位允许 [各种][12] 读和写的系统调用(`read(2)`、`readv(2)`、`pread(2)`、`preadv(2)` 等等)。`PLEDGE_OPEN` 位允许 `open(2)`
如果给它传递个参数 0 ,仅允许一些基本的系统调用,包括那些用于去分配内存的系统调用(比如 `brk(2)`)。 `PLEDGE_RDWR` 位允许 [各种][12] 读和写的系统调用(`read(2)`、`readv(2)`、`pread(2)`、`preadv(2)` 等等)。`PLEDGE_OPEN` 位允许 `open(2)`
为防止发生提升权限的行为,`pledge()` 会拦截它自己 — 但这样也防止了权限撤销,以后再细说这方面内容。
为防止发生提升权限的行为,`pledge()` 会拦截它自己 — 但这样也防止了权限撤销,以后再细说这方面内容。
在 xpledge 跟踪器中,我需要去检查这个系统调用:
```
/* Handle entrance */
switch (regs.orig_rax) {
case SYS_pledge:
register_pledge(regs.rdi);
break;
case SYS_pledge:
register_pledge(regs.rdi);
break;
}
```
操作系统将返回 `ENOSYS`(因为函数还没有实现),因此它不是一个真实的系统调用。为此在退出时我用一个 `success (0)` 去覆写它。
操作系统将返回 `ENOSYS`(函数尚未实现),因为它不是一个*真实的*系统调用。为此在退出时我用一个 `success(0)` 去覆写它。
```
/* Handle exit */
switch (regs.orig_rax) {
case SYS_pledge:
ptrace(PTRACE_POKEUSER, pid, RAX * 8, 0);
break;
case SYS_pledge:
ptrace(PTRACE_POKEUSER, pid, RAX * 8, 0);
break;
}
```
我写了一小段测试程序去打开 `/dev/urandom`,做一个读操作,尝试去承诺后,我第二次打开 `/dev/urandom`,然后确认它能够读取原始的 `/dev/urandom` 文件描述符。在没有承诺跟踪器的情况下运行,输出如下:
我写了一小段测试程序去打开 `/dev/urandom`,做一个读操作,尝试去承诺后,然后试着第二次打开 `/dev/urandom`,然后确认它能够读取原始的 `/dev/urandom` 文件描述符。在没有承诺跟踪器的情况下运行,输出如下:
```
$ ./example
fread("/dev/urandom")[1] = 0xcd2508c7
@ -210,61 +205,64 @@ XPledging...
XPledge failed: Function not implemented
fread("/dev/urandom")[2] = 0x0be4a986
fread("/dev/urandom")[1] = 0x03147604
```
做一个无效的系统调用并不会让应用程序崩溃。它只是失败,这是一个很方便的返回方式。当它在跟踪器下运行时,它的输出如下:
```
$ ./xpledge ./example
fread("/dev/urandom")[1] = 0xb2ac39c4
XPledging...
fopen("/dev/urandom")[2]: Operation not permitted
fread("/dev/urandom")[1] = 0x2e1bd1c4
```
这个承诺很成功,第二次的 `fopen(3)` 并没有实现,因为跟踪器用一个 `EPERM` 拦截了它。
这个承诺很成功,第二次的 `fopen(3)` 并没有进行,因为跟踪器用一个 `EPERM` 阻塞了它。
可以将这种思路进一步发扬光大,比如,改变文件路径或返回一个假的结果。一个跟踪器可以很高效地 chroot 它的被跟踪进程,通过一个系统调用将任意路径传递给 root 从而实现 chroot 路径。它甚至可以对用户进行欺骗,告诉用户它以 root 运行。事实上,这些就是 [Fakeroot NG][13] 程序所做的事情。
### 仿真外部系统
假设你不满足于仅监听一些系统调用,而是想监听全部系统调用。你收到 [一个打算在其它操作系统上运行的二进制程序][14],因为没有系统调用,这个二进制程序将无法正常运行。
假设你不满足于仅拦截一些系统调用,而是想拦截*全部*系统调用。你就会有了 [一个打算在其它操作系统上运行的二进制程序][14],无需系统调用,这个二进制程序可以一直运行。
使用我在前面所描述的这些内容你就可以管理这一切。跟踪器可以使用一个假冒的东西去代替系统调用号,允许它去失败,以及为系统调用本身提供服务。但那样做的效率很低。其实质上是对每个系统调用做了三个上下文切换:一个是在入口上停止,一个是让系统调用总是以失败告终,还有一个是在系统调用退出时停止。
使用我在前面所描述的这些内容你就可以管理这一切。跟踪器可以使用一个假冒的东西去代替系统调用号,允许它失败,以及为系统调用本身提供服务。但那样做的效率很低。其实质上是对每个系统调用做了三个上下文切换:一个是在入口上停止,一个是让系统调用总是以失败告终,还有一个是在系统调用退出时停止。
从 2005 年以后对于这个技术PTrace 的 Linux 版本有更高效的操作:`PTRACE_SYSEMU`。PTrace 仅在每个系统调用发出时停止*一次*,在允许被跟踪进程继续运行之前,由跟踪器为系统调用提供服务。
从 2005 年以后对于这个技术PTrace 的 Linux 版本有更高效的操作:`PTRACE_SYSEMU`。PTrace 仅在每个系统调用发出时停止一次,在允许被跟踪进程继续运行之前,由跟踪器为系统调用提供服务。
```
for (;;) {
ptrace(PTRACE_SYSEMU, pid, 0, 0);
waitpid(pid, 0, 0);
ptrace(PTRACE_SYSEMU, pid, 0, 0);
waitpid(pid, 0, 0);
struct user_regs_struct regs;
ptrace(PTRACE_GETREGS, pid, 0, &regs);
struct user_regs_struct regs;
ptrace(PTRACE_GETREGS, pid, 0, &regs);
switch (regs.orig_rax) {
case OS_read:
/* ... */
switch (regs.orig_rax) {
case OS_read:
/* ... */
case OS_write:
/* ... */
case OS_write:
/* ... */
case OS_open:
/* ... */
case OS_open:
/* ... */
case OS_exit:
/* ... */
case OS_exit:
/* ... */
/* ... and so on ... */
}
/* ... and so on ... */
}
}
```
从任何使用(足够)稳定的系统调用 ABI译注:应用程序二进制接口),在相同架构的机器上运行一个二进制程序时,你只需要 `PTRACE_SYSEMU` 跟踪器,一个加载器(用于代替 `exec(2)`),和这个二进制程序所需要(或仅运行静态的二进制程序)的任何系统库即可。
从任何具有(足够)稳定的系统调用 ABILCTT 译注:应用程序二进制接口),在相同架构的机器上运行一个二进制程序时,你只需要 `PTRACE_SYSEMU` 跟踪器、一个加载器(用于代替 `exec(2)`),和这个二进制程序所需要(或仅运行静态的二进制程序)的任何系统库即可。
事实上,这听起来有点像一个有趣的周末项目。
**参见**
- [给 Linux 内核克隆实现一个 OpenBSD 承诺][15]
--------------------------------------------------------------------------------
via: http://nullprogram.com/blog/2018/06/23/
@ -272,22 +270,23 @@ via: http://nullprogram.com/blog/2018/06/23/
作者:[Chris Wellons][a]
选题:[lujun9972](https://github.com/lujun9972)
译者:[qhwdw](https://github.com/qhwdw)
校对:[校对者ID](https://github.com/校对者ID)
校对:[wxy](https://github.com/wxy)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:http://nullprogram.com
[1]:https://blog.plover.com/Unix/strace-groff.html
[2]:http://nullprogram.com/blog/2016/09/03/
[3]:http://man7.org/linux/man-pages/man2/ptrace.2.html
[4]:http://nullprogram.com/blog/2018/01/17/
[5]:http://nullprogram.com/blog/2015/05/15/
[6]:https://stackoverflow.com/a/6469069
[7]:https://man.openbsd.org/pledge.2
[8]:http://www.openbsd.org/papers/hackfest2015-pledge/mgp00001.html
[9]:http://nullprogram.com/blog/2017/07/19/
[10]:http://nullprogram.com/blog/2016/09/23/
[11]:https://www.tedunangst.com/flak/post/string-interfaces
[12]:http://nullprogram.com/blog/2017/03/01/
[13]:https://fakeroot-ng.lingnu.com/index.php/Home_Page
[14]:http://nullprogram.com/blog/2017/11/30/
[a]: http://nullprogram.com
[1]: https://blog.plover.com/Unix/strace-groff.html
[2]: http://nullprogram.com/blog/2016/09/03/
[3]: http://man7.org/linux/man-pages/man2/ptrace.2.html
[4]: http://nullprogram.com/blog/2018/01/17/
[5]: http://nullprogram.com/blog/2015/05/15/
[6]: https://stackoverflow.com/a/6469069
[7]: https://man.openbsd.org/pledge.2
[8]: http://www.openbsd.org/papers/hackfest2015-pledge/mgp00001.html
[9]: http://nullprogram.com/blog/2017/07/19/
[10]: http://nullprogram.com/blog/2016/09/23/
[11]: https://www.tedunangst.com/flak/post/string-interfaces
[12]: http://nullprogram.com/blog/2017/03/01/
[13]: https://fakeroot-ng.lingnu.com/index.php/Home_Page
[14]: http://nullprogram.com/blog/2017/11/30/
[15]: https://www.youtube.com/watch?v=uXgxMDglxVM

View File

@ -0,0 +1,362 @@
6 个简单的方式来查看 Linux 中的用户名和其它信息
======
这是一个非常基础的话题,在 Linux 中,每个人都知道如何使用 **id** 来查找用户信息。一些用户也从 **/etc/passwd** 文件中过滤用户信息。
我们还使用其它命令来获取用户信息。
你可能会问,为什么要讨论这个基础话题呢?即使我也这么认为,因为除了这两个之外没有更好的方法了,但是我们也有一些不错的选择。
与这两者相比,那些命令提供了更详细的信息,这对于新手非常有帮助。
这是帮助管理员在 Linux 中查找用户信息的基本命令之一。Linux 中的一切都是文件,甚至用户信息都存储在一个文件中。
**建议阅读:**
**(#)** [怎样在 Linux 上查看用户创建的日期][1]
**(#)** [怎样在 Linux 上查看用户属于哪个组][2]
**(#)** [怎样在 Linux 上查看强制用户在下次登录时改变密码][3]
所有用户都被添加在 `/etc/passwd` 文件中,这里保留了用户名和其它相关详细信息。在 Linux 中创建用户时,用户详细信息将存储在 /etc/passwd 文件中。passwd 文件将每个用户详细信息保存为一行,包含 7 字段。
我们可以使用以下 6 种方法来查看用户信息。
* `id :`为指定的用户名打印用户和组信息。
* `getent :`从 Name Service Switch 库中获取条目。
* `/etc/passwd file :` /etc/passwd 文件包含每个用户的详细信息,每个用户详情是一行,包含 7 个字段。
* `finger :`用户信息查询程序
* `lslogins :`lslogins 显示系统中已有用户的信息
* `compgen :`compgen 是 bash 内置命令,它将显示用户的所有可用命令。
### 1) 使用 id 命令
id 代表身份。它输出真实有效的用户和组 ID。也可以输出指定用户或当前用户的用户和组信息。
```
# id daygeek
uid=1000(daygeek) gid=1000(daygeek) groups=1000(daygeek),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),118(lpadmin),128(sambashare)
```
下面是上述输出的详细信息:
* **`uid (1000/daygeek):`** 它显示用户 ID 和用户名
* **`gid (1000/daygeek):`** 它显示用户的组 ID 和名称
* **`groups:`** 它显示用户的附加组 ID 和名称
### 2) 使用 getent 命令
getent 命令显示 Name Service Switch 库支持的数据库中的条目,这些库在 /etc/nsswitch.conf 中配置。
getent 命令会显示类似于 /etc/passwd 文件的用户详情,它将每个用户的详细信息放在一行,包含 7 个字段。
```
# getent passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
centos:x:500:500:Cloud User:/home/centos:/bin/bash
prakash:x:501:501:2018/04/12:/home/prakash:/bin/bash
apache:x:48:48:Apache:/var/www:/sbin/nologin
nagios:x:498:498::/var/spool/nagios:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
nrpe:x:497:497:NRPE user for the NRPE service:/var/run/nrpe:/sbin/nologin
magesh:x:502:503:2g Admin - Magesh M:/home/magesh:/bin/bash
thanu:x:503:504:2g Editor - Thanisha M:/home/thanu:/bin/bash
sudha:x:504:505:2g Editor - Sudha M:/home/sudha:/bin/bash
```
下面是关于 7 个字段的详细信息。
```
magesh:x:502:503:2g Admin - Magesh M:/home/magesh:/bin/bash
```
* **`Username (magesh):`** 已创建的用户名。字符长度应该在 1 到 32 之间。
* **`Password (x):`** 它表明加密密码存储在 /etc/shadow 文件中。
* **`User ID (UID-502):`** 它表示用户 IDUID每个用户应包含唯一的 UID。UID (0-Zero) 保留给 rootUID1-99是为系统用户保留的UID100-999是为系统账户/组保留的。
* **`Group ID (GID-503):`** 它表示组 IDGID每个组应该包含唯一的 GID它存储在 /etc/group 文件中。
* **`User ID Info (2g Admin - Magesh M):`** 它表示命令字段。这个字段可用于描述用户信息。
* **`Home Directory (/home/magesh):`** 它表示用户家目录。
* **`shell (/bin/bash):`** 它表示用户的 bash shell。
如果你只想在 getent 命令的输出中显示用户名,使用以下命令格式:
```
# getent passwd | cut -d: -f1
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
uucp
operator
games
gopher
ftp
nobody
dbus
vcsa
abrt
haldaemon
ntp
saslauth
postfix
sshd
tcpdump
centos
prakash
apache
nagios
rpc
nrpe
magesh
thanu
sudha
```
只显示用户的家目录,使用以下命令格式:
```
# getent passwd | grep '/home' | cut -d: -f1
centos
prakash
magesh
thanu
sudha
```
### 3) 使用 /etc/passwd 文件
`/etc/passwd` 是一个文本文件,它包含每个用户登录 Linux 系统所必需的的信息。它维护用户的有用信息,如用户名,密码,用户 ID组 ID用户 ID 信息,家目录和 shell。/etc/passwd 文件将每个用户详细信息放在一行中,包含 7 个字段,如下所示:
```
# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
centos:x:500:500:Cloud User:/home/centos:/bin/bash
prakash:x:501:501:2018/04/12:/home/prakash:/bin/bash
apache:x:48:48:Apache:/var/www:/sbin/nologin
nagios:x:498:498::/var/spool/nagios:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
nrpe:x:497:497:NRPE user for the NRPE service:/var/run/nrpe:/sbin/nologin
magesh:x:502:503:2g Admin - Magesh M:/home/magesh:/bin/bash
thanu:x:503:504:2g Editor - Thanisha M:/home/thanu:/bin/bash
sudha:x:504:505:2g Editor - Sudha M:/home/sudha:/bin/bash
```
以下是 7 个字段的详细信息。
```
magesh:x:502:503:2g Admin - Magesh M:/home/magesh:/bin/bash
```
* **`Username (magesh):`** 已创建的用户名。字符长度应该在 1 到 32 之间。
* **`Password (x):`** 它表明加密密码存储在 /etc/shadow 文件中。
* **`User ID (UID-502):`** 它表示用户 IDUID每个用户应包含唯一的 UID。UID (0-Zero) 保留给 rootUID1-99是为系统用户保留的UID100-999是为系统账户/组保留的。
* **`Group ID (GID-503):`** 它表示组 IDGID每个组应该包含唯一的 GID它存储在 /etc/group 文件中。
* **`User ID Info (2g Admin - Magesh M):`** 它表示命令字段。这个字段可用于描述用户信息。
* **`Home Directory (/home/magesh):`** 它表示用户家目录。
* **`shell (/bin/bash):`** 它表示用户的 bash shell。
如果你只想显示 /etc/passwd 文件中的用户名,使用以下格式:
```
# cut -d: -f1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
uucp
operator
games
gopher
ftp
nobody
dbus
vcsa
abrt
haldaemon
ntp
saslauth
postfix
sshd
tcpdump
centos
prakash
apache
nagios
rpc
nrpe
magesh
thanu
sudha
```
只显示用户的家目录,使用以下格式:
```
# cat /etc/passwd | grep '/home' | cut -d: -f1
centos
prakash
magesh
thanu
sudha
```
### 4) 使用 finger 命令
finger 命令显示有关系统用户的信息。它显示用户的真实姓名,终端名称和写入状态(如果没有写入权限,那么最为终端名称后面的 "*"),空闲时间和登录时间。
```
# finger magesh
Login: magesh Name: 2g Admin - Magesh M
Directory: /home/magesh Shell: /bin/bash
Last login Tue Jul 17 22:46 (EDT) on pts/2 from 103.5.134.167
No mail.
No Plan.
```
以下是上述输出的详细信息:
* **`Login:`** 用户名
* **`Name:`** 附加/有关用户的其它信息
* **`Directory:`** 用户家目录的信息
* **`Shell:`** 用户的 shell 信息
* **`LAST-LOGIN:`** 上次登录日期和其它信息
### 5) 使用 lslogins 命令
它显示系统已知用户的信息。默认情况下,它将列出系统中所有用户的信息。
lslogins 使用程序的灵感来自于 logins 实用程序,该实用程序最初出现在 FreeBSD 4.10 中。
```
# lslogins -u
UID USER PWD-LOCK PWD-DENY LAST-LOGIN GECOS
0 root 0 0 00:17:28 root
500 centos 0 1 Cloud User
501 prakash 0 0 Apr12/04:08 2018/04/12
502 magesh 0 0 Jul17/22:46 2g Admin - Magesh M
503 thanu 0 0 Jul18/00:40 2g Editor - Thanisha M
504 sudha 0 0 Jul18/01:18 2g Editor - Sudha M
```
以下是上述输出的详细信息:
* **`UID:`** 用户 id
* **`USER:`** 用户名
* **`PWD-LOCK:`** 密码已设置,但是已锁定
* **`PWD-DENY:`** 登录密码是否禁用
* **`LAST-LOGIN:`** 上次登录日期
* **`GECOS:`** 有关用户的其它信息
### 6) 使用 compgen 命令
compgen 是 bash 内置命令,它将显示所有可用的命令,别名和函数。(to 校正:这个命令在 CentOS 中有,但是我没有搞懂它的输出)
```
# compgen -u
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
uucp
operator
games
gopher
ftp
nobody
dbus
vcsa
abrt
haldaemon
ntp
saslauth
postfix
sshd
tcpdump
centos
prakash
apache
nagios
rpc
nrpe
magesh
thanu
sudha
```
--------------------------------------------------------------------------------
via: https://www.2daygeek.com/6-easy-ways-to-check-user-name-and-other-information-in-linux/
作者:[Prakash Subramanian][a]
选题:[lujun9972](https://github.com/lujun9972)
译者:[MjSeven](https://github.com/MjSeven)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://www.2daygeek.com/author/prakash/
[1]:https://www.2daygeek.com/how-to-check-user-created-date-on-linux/
[2]:https://www.2daygeek.com/how-to-check-which-groups-a-user-belongs-to-on-linux/
[3]:https://www.2daygeek.com/how-to-force-user-to-change-password-on-next-login-in-linux/

View File

@ -0,0 +1,65 @@
使用 Fstransform 转换文件系统
======
![](https://fedoramagazine.org/wp-content/uploads/2018/08/converting-filesystems-816x345.jpg)
很少有人知道他们可以将文件系统从一种类型转换为另一种类型而不会丢失数据,即非破坏性的。这可能听起来像魔术,但 [Fstransform][1] 可以几乎以任意组合将 ext2、ext3、ext4、jfs、reiserfs 或 xfs 分区转换成另一类型。更重要的是它可以直接执行而无需格式化或复制数据。除此之外还有一点好处Fstransform 也可以处理 ntfs、btrfs、fat 和 exfat 分区。
### 在运行之前
Fstransform 中存在一些警告和限制,因此强烈建议在尝试转换之前进行备份。此外,使用 Fstransform 时需要注意一些限制:
* Linux 内核必须支持源文件系统和目标文件系统。听起来很明显,如果你想使用 ext2、ext3、ext4、reiserfs、jfs 和 xfs 分区这样不会出现风险。Fedora 支持所有分区,所以没问题。
* 将 ext2 升级到 ext3 或 ext4 不需要 Fstransform。请使用 Tune2fs。
* 源文件系统的设备必须至少有 5 的可用空间。
* 你需要在开始之前卸载源文件系统。
* 源文件系统存储的数据越多,转换的时间就越长。实际速度取决于你的设备,但预计它大约为每分钟 1GB。大量的硬链接也会降低转换速度。
* 虽然 Fstransform 被证明是稳定的,但请备份源文件系统上的数据。
### 安装说明
Fstransform 已经是 Fedora 的一部分。使用以下命令安装:
```
sudo dnf install fstransform
```
### 转换时间
![][2]
fstransform 命令的语法非常简单fstransform <源设备> <目标文件系统>。请记住,它需要 root 权限才能运行,所以不要忘记在开头添加 sudo。这是一个例子
```
sudo fstransform /dev/sdb1 ext4
```
请注意,无法转换根文件系统,这是一种安全措施。请改用测试分区或实验性 USB。与此同时Fstransform 会在控制台中有许多辅助输出。最有用的部分是预计完成时间,让你随时了解该过程需要多长时间。同样,几乎空的驱动器上的几个小文件将使 Fstransform 在一分钟左右完成其工作,而更多真实世界的任务可能需要数小时的等待时间。
### 更多支持的文件系统
如上所述,可以尝试在 ntfs、btrfs、fat 和 exfat 分区使用 Fstransform。这些类型是非常实验性的没有人能保证完美转换。尽管如此还是有许多成功案例你可以通过在测试分区上使用示例数据集测试 Fstransform 来添加自己的成功案例。可以使用 -force-untested-file-systems 参数启用这些额外的文件系统:
```
sudo fstransform /dev/sdb1 ntfs --force-untested-file-systems
```
有时,该过程可能会因错误而中断。请放心再次执行命令 - 它可能最终会在 23 次尝试后完成转换。
--------------------------------------------------------------------------------
via: https://fedoramagazine.org/transform-file-systems-in-linux/
作者:[atolstoy][a]
选题:[lujun9972](https://github.com/lujun9972)
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://fedoramagazine.org/author/atolstoy/
[1]:https://github.com/cosmos72/fstransform
[2]:https://fedoramagazine.org/wp-content/uploads/2018/08/Screenshot_20180805_230116.png

View File

@ -1,232 +0,0 @@
在 Linux 中如何以人性化的方式显示数据
======
![](https://images.idgesg.net/images/article/2018/08/smile-face-on-hand-100767756-large.jpg)
不是每个人都以二进制方式思考他们不想在大脑中插入逗号来处理大量数字并获得文件的大小。因此Linux 命令在过去的几十年里不断发展,以更人性化的方式向用户显示信息,这一点也不奇怪。在今天的文章中,我们将看一看各种命令所提供的一些选项,它们使得数据变得更容易理解。
### 为什么默认显示不更友好一些?
如果你想知道为什么默认不显示得更人性化原因是我们人类毕竟是计算机的默认用户to 校正:这里分很不理解,感觉不对,既然人类是默认用户,那么为什么不人性化呢)。你可能会问自己:“为什么我们不竭尽全力去获得对每个人都有意义的命令的响应?”答案是:改变命令的默认输出可能会干扰许多其它进程,这些进程是在期望默认响应之上构建的。其它的工具,以及过去几十年开发的脚本,如果突然以一种完全不同的格式输出,而不是它们过去所期望的那样,可能会被一种非常丑陋的方式破坏。
说真的,也许我们中的一些人可能更愿意看文件大小中的所有数字,即 1338277310 而不是 1.3G。在任何情况下,切换默认值都可能造成破坏,但是为更加人性化的响应提供一些简单的选项只会让我们学习一些命令选项。
### 可以显示人性化数据的命令
有哪些简单的选项可以使 Unix 命令的输出更容易解析呢?让我们来看一些命令。
#### top
你可能没有注意到这个命令,但是在 top 命令中,你可以通过输入 “**E**”(大写字母 E来更改显示全部内存使用的方式。连续按下将数字显示从 KiB 到 MiB, 再到 GiB, 接着是 TiB, PiB, EiB最后回到 KiB。
认识这些单位吧?这里有一组定义:
```
2**10 = 1,024 = 1 KiB (kibibyte)
2**20 = 1,048,576 = 1 MiB (mebibyte)
2**30 = 1,073,741,824 = 1 GiB (gibibyte)
2**40 = 1,099,511,627,776 = 1 TiB (tebibyte)
2**50 = 1,125,899,906,842,624 = PiB (pebibyte)
2**60 = 1,152,921,504,606,846,976 = EiB (exbibyte)
2**70 = 1,180,591,620,717,411,303,424 = 1 ZiB (zebibyte)
2**80 = 1,208,925,819,614,629,174,706,176 = 1 YiB (yobibyte)
```
这些单位与千字节,兆字节和千兆字节密切相关。虽然它们很接近,但是它们之间仍有很大的区别:一组是基于 10 的幂,另一组是基于 2 的幂。例如,比较千字节和千比字节,我们可以看看它们不同点:
```
KB = 1000 = 10**3
KiB = 1024 = 2**10
```
以下是 top 命令输出示例,使用 KiB 为单位默认显示:
```
top - 10:49:06 up 5 days, 35 min, 1 user, load average: 0.05, 0.04, 0.01
Tasks: 158 total, 1 running, 118 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.2 sy, 0.0 ni, 99.8 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 6102680 total, 4634980 free, 392244 used, 1075456 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 5407432 avail Mem
```
在按下 E 之后,单位变成了 MiB
```
top - 10:49:31 up 5 days, 36 min, 1 user, load average: 0.03, 0.04, 0.01
Tasks: 158 total, 2 running, 118 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.6 sy, 0.0 ni, 99.4 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 5959.648 total, 4526.348 free, 383.055 used, 1050.246 buff/cache
MiB Swap: 2047.996 total, 2047.996 free, 0.000 used. 5280.684 avail Mem
```
再次按下 E单位变为 GiB
```
top - 10:49:49 up 5 days, 36 min, 1 user, load average: 0.02, 0.03, 0.01
Tasks: 158 total, 1 running, 118 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
GiB Mem : 5.820 total, 4.420 free, 0.374 used, 1.026 buff/cache
GiB Swap: 2.000 total, 2.000 free, 0.000 used. 5.157 avail Mem
```
你还可以通过按字母 “**e**” 来更改为显示每个进程使用内存的数字单位。它将从默认的 KiB 到 MiB再到 GiB, TiB接着到 PiB期望看到很多0 (to 校正者:意思是以 PB 为单位前面很多 0)),然后返回 KiB。下面是按了一下 “**e**” 之后的 top 输出:
```
top - 08:45:28 up 4 days, 22:32, 1 user, load average: 0.02, 0.03, 0.00
Tasks: 167 total, 1 running, 118 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.2 us, 0.0 sy, 0.0 ni, 99.8 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 6102680 total, 4641836 free, 393348 used, 1067496 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 5406396 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
784 root 20 0 543.2m 26.8m 16.1m S 0.9 0.5 0:22.20 snapd
733 root 20 0 107.8m 2.0m 1.8m S 0.4 0.0 0:18.49 irqbalance
22574 shs 20 0 107.5m 5.5m 4.6m S 0.4 0.1 0:00.09 sshd
1 root 20 0 156.4m 9.3m 6.7m S 0.0 0.2 0:05.59 systemd
```
#### du
du 命令显示磁盘空间文件或目录使用了多少,如果使用 **-h** 选项则将输出大小调整为最合适的单位。默认情况下它以千字节KB为单位。
```
$ du camper*
360 camper_10.jpg
5684 camper.jpg
240 camper_small.jpg
$ du -h camper*
360K camper_10.jpg
5.6M camper.jpg
240K camper_small.jpg
```
#### df
df 命令也提供了一个 -h 选项。请注意在下面的示例中是如何以千兆字节GB和兆字节MB输出的
```
$ df -h | grep -v loop
Filesystem Size Used Avail Use% Mounted on
udev 2.9G 0 2.9G 0% /dev
tmpfs 596M 1.7M 595M 1% /run
/dev/sda1 110G 9.0G 95G 9% /
tmpfs 3.0G 0 3.0G 0% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 3.0G 0 3.0G 0% /sys/fs/cgroup
tmpfs 596M 16K 596M 1% /run/user/121
/dev/sdb2 457G 73M 434G 1% /apps
tmpfs 596M 0 596M 0% /run/user/1000
```
下面的命令使用了 **-h** 选项,同时使用 **-T** 选项来显示我们正在查看的文件系统的类型。
```
$ df -hT /mnt2
Filesystem Type Size Used Avail Use% Mounted on
/dev/sdb2 ext4 457G 73M 434G 1% /apps
```
#### ls
即使是 ls它也为我们提供了调整大小显示的选项保证是最合理的。
```
$ ls -l camper*
-rw-rw-r-- 1 shs shs 365091 Jul 14 19:42 camper_10.jpg
-rw-rw-r-- 1 shs shs 5818597 Jul 14 19:41 camper.jpg
-rw-rw-r-- 1 shs shs 241844 Jul 14 19:45 camper_small.jpg
$ ls -lh camper*
-rw-rw-r-- 1 shs shs 357K Jul 14 19:42 camper_10.jpg
-rw-rw-r-- 1 shs shs 5.6M Jul 14 19:41 camper.jpg
-rw-rw-r-- 1 shs shs 237K Jul 14 19:45 camper_small.jpg
```
#### free
free 命令允许你以字节B千字节KB兆字节MB和千兆字节GB为单位查看内存使用情况。
```
$ free -b
total used free shared buff/cache available
Mem: 6249144320 393076736 4851625984 1654784 1004441600 5561253888
Swap: 2147479552 0 2147479552
$ free -k
total used free shared buff/cache available
Mem: 6102680 383836 4737924 1616 980920 5430932
Swap: 2097148 0 2097148
$ free -m
total used free shared buff/cache available
Mem: 5959 374 4627 1 957 5303
Swap: 2047 0 2047
$ free -g
total used free shared buff/cache available
Mem: 5 0 4 0 0 5
Swap: 1 0 1
```
#### tree
虽然 tree 命令与文件或内存计算无关,但它也提供了非常人性化的文件视图,它分层显示文件以说明文件是如何组织的。当你试图了解如何安排目录内容时,这种显示方式非常有用。
```
$ tree
.g to
├── 123
├── appended.png
├── appts
├── arrow.jpg
├── arrow.png
├── bin
│ ├── append
│ ├── cpuhog1
│ ├── cpuhog2
│ ├── loop
│ ├── mkhome
│ ├── runme
```
#### stat
stat 命令是另一个以非常人性化的格式显示信息的命令。它提供了更多关于文件的元数据,包括文件大小(以字节和块为单位),文件类型,设备和 inode索引节点文件所有者和组名称和数字 ID以数字和 rwx 格式显示的文件权限以及文件的最后访问和修改日期。在某些情况下,它也可能显示最初创建文件的时间。
```
$ stat camper*
File: camper_10.jpg
Size: 365091 Blocks: 720 IO Block: 4096 regular file
Device: 801h/2049d Inode: 796059 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/ shs) Gid: ( 1000/ shs)
Access: 2018-07-19 18:56:31.841013385 -0400
Modify: 2018-07-14 19:42:25.230519509 -0400
Change: 2018-07-14 19:42:25.230519509 -0400
Birth: -
File: camper.jpg
Size: 5818597 Blocks: 11368 IO Block: 4096 regular file
Device: 801h/2049d Inode: 796058 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/ shs) Gid: ( 1000/ shs)
Access: 2018-07-19 18:56:31.845013872 -0400
Modify: 2018-07-14 19:41:46.882024039 -0400
Change: 2018-07-14 19:41:46.882024039 -0400
Birth: -
```
### 总结
Linux 命令提供了许多选项,可以让用户更容易理解或比较它们的输出。对于许多命令,**-h** 选项会显示更友好的输出格式。对于其它的,你可能必须通过使用某些特定选项或者按下某个键来查看你希望的输出。我希望这其中一些选项会让你的 Linux 系统看起来更友好一点。
加入[Facebook][1] 和 [LinkedIn][2] 上的网络世界社区,一起来评论重要的话题吧。
--------------------------------------------------------------------------------
via: https://www.networkworld.com/article/3296631/linux/displaying-data-in-a-human-friendly-way-on-linux.html
作者:[Sandra Henry-Stocker][a]
选题:[lujun9972](https://github.com/lujun9972)
译者:[MjSeven](https://github.com/MjSeven)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://www.networkworld.com/author/Sandra-Henry_Stocker/
[1]:https://www.facebook.com/NetworkWorld/
[2]:https://www.linkedin.com/company/network-world