权限
Linux访问权限
• The type of file (first
character) • The file’s access permissions (the next nine characters) •
The ACL flag (present if the file has an ACL) • The number of links to
the file • The name of the owner of the file (usually the person who
created the file) • The name of the group that the file is associated
with • The size of the file in characters (bytes) • The date and time
the file was created or last modified • The name of the file
有效用户组与初始用户组
/etc/passwd第4栏的GID就是初始用户组(initial
group)。当用户登录,立刻拥有这个用户组的相关权限。
当一个用户同时属于多个组时,该用户创建的文件的组会是哪个?这就涉及检查有效用户组了(effective
group)。
用groups命令可查看当前用户所属的组。groups输出的第一个用户组为有效用户组。用netgrp命令可更改有效用户组。
如果要让dmtsai切换到新组vbird中,必须满足下面两个条件:
- vbird用户组在/etc/gshadow的密码栏为合法(不用!开头)。
- 由root或组管理员将dmtsai加入到vbird组中。
/etc/gshadow的第2栏为密码栏,第3栏为组管理员。
real effective saved uid
Real
UID是指创建该进程的用户(或进程)的UID,它只在EUID=0的情况才能更改。
OS在评估某进程是否允许做某事情时,依据的是该进程的Effective UID。
EUID!=0情况下,EUID可被改成RUID或SUID。EUID=0情况下,可任意变动。
在设置了可执行文件的Set-UID位的情况下才有效,此时,Saved UID会是可
执行文件的属主的UID。其它情况下Saved UID与RUID相同。
比如passwd被普通用户用来修改密码,而普通用户无权修改/etc/passwd文件。
通过设置passwd的Set-UID位,进程的Saved-UID会是root,再调用seteuid,
可将Effective UID改为root的UID,这时就能够修改/etc/passwd文件的内容
了。Set-UID位的使用要与程序本身的代码逻辑结合起来才有效,单单设置该
位是没有意义的。
sudo的使用 sudo
`` example
sudo [options] [command]
–b (background) Runs command in the background. –i (initial login
environment) Spawns the shell that is specified for root (or another
user specified by –u) in /etc/passwd, running root’s (or the other
user’s) startup files,with some exceptions (e.g., TERM is not changed).
Does not take a command. –k (kill) Resets the timestamp (page 491) of
the user running the command, which means the user must enter a password
the next time she runs sudo. –L (list defaults) Lists the parameters
that you can set on a Defaults line in the sudoers file. Does not take a
command. –l (list commands) Lists the commands the user who is running
sudo is allowed to run on the local system. Does not take a command. –s
(shell) Spawns a new root (or another user specified by –u) shell as
specified in the /etc/passwd file. Similar to –i but does not change the
environment. Does not take a command. –u user Runs command with the
privileges of user. Without this option sudo runs command with root
privileges.
<h3 id="重定向">重定向</h3>
sudo用在重定向时会出问题,如
bash
$ sudo ls > /root/ls.sam
-bash: /root/ls.sam: Permission denied
解决方案有两种
bash
$ sudo bash -c 'ls > /root/ls.sam'
$ ls | sudo tee /root/ls.sam
tee: Sends Output in Two Directions The tee utility copies its standard
input both to a file and to standard output. The utility is aptly named:
It takes a single input and sends the output in two directions.
<h3 id="requiretty">requiretty</h3>
参看service.org中cron中执行sudo时碰到requiretty的问题。默认需要tty的情况下,ssh连接时要使用-t参数才能保证正确执行。
配置requiretty可以针对所有用户:
example
Defaults !requiretty
也可针对特定用户:
example
Defaults:root !requiretty
针对特定用户时,要注意是执行sudo命令的用户,而不是sudo需要切换过去的目标用户。
<h3 id="sudo的环境变量">sudo的环境变量</h3>
以root用户执行下面命令,可查看到调用sudo用户环境变量更改的情况
example
sudo -V
/etc/sudoers文件中的Default<sub>Entry设置决定了环境变量的继承情况</sub>,尤其是env<sub>reset</sub>。PATH环境变量的处理又更加特殊,如果设置了secure<sub>path</sub>,那么PATH即使在evn<sub>keep列表中</sub>,sudo也不会全部继承,而只使用secure<sub>path的设置</sub>。
<h3 id="NOPASSWD无效的情形">NOPASSWD无效的情形</h3>
如果某用户同时属于/etc/sudoers中的两个配置的组,其中一个使用NOPASSWD,另一个没有,那么最终结果还是需要输入密码。用下面的命令可以查看与当前用户匹配的设置:
example
sudo -l
另外,用户的effective group是否会影响到sudo的权限控制?
<h2 id="进程的六个ID">进程的六个ID</h2>
real user ID, real group
ID:用户登录后从/etc/passwd中获得的ID,即由login进程通过setuid系统调用设定好,除超级用户外,real
uid和gid都是不会变动的。 effective user ID, effective group ID,
supplementary group
IDs:内核用这几个ID来决定进程访问或修改文件的权限。通常,effective
uid与real
uid相同。不过,如果可执行文件设置了set-user-id位,那么exec加载该文件并执行时,会把effective
uid设置为文件所有者(owner)的uid。 saved set-user-ID, saved
set-group-ID:exec函数加载可执行文件时,先设置好effective
uid,然后将其复制到saved
set-user-ID。当可执行文件设置了set-user-id位,那么,该进程的saved
set-user-ID也会是文件所有者的uid。
<h2 id="检查文件访问权限">检查文件访问权限</h2>
- 打开任意文件,都需要用户拥有路径上所有目录的x权限。目录的x权限又
称为搜索位(search bit)。拥有目录的r权限表示“能够读取该目录文
件列表”,与x权限完全不同。
- 创建新文件,需要文件所在目录的w和x权限。
- 删除文件,需要文件所在目录的w和x权限,无需该文件的r或w权限。
- 需要用exec函数执行的文件才需要x权限。
<h2 id="tip程序是如何使用setuid的?">tip程序是如何使用setuid的?</h2>
setuid系统调用的声明如下: int setuid(uid<sub>t</sub> uid);
如果进程没有超级用户权限,那么setuid成功调用的前提条件是:参数uid等于real
user ID或saved set-user-ID。setuid只改变effective user
ID,其它uid不受影响。
tip通过文件锁与其它UUCP程序共享modem。访问文件锁和modem都需要uucp用户的权限,而用户在使用tip时却只能拥有普通用户的权限。因此,tip需要在开始和结束时用setuid,将effective
uid切换成uucp用户,用户使用时,则切换成普通用户的uid。
tip程序文件设置了set-user-id位,因此,即使普通用户启动该程序,该程序的euid和saved
uid都会是uucp。等到获得modem使用权限后,tip执行setuid(getuid()),将euid切换为当前用户的uid,而suid依旧是uucp。最后需要释放modem时,tip调用setuid(uucpuid),参数uucpuid与saved
uid相同,调用成功,tip的euid又切换回uucp用户了。
从tip程序的设计者来考虑权限问题:对普通用户隐藏特殊权限的使用细节。
<h1 id="磁盘管理 <span class="tag" tag-name="disk"><span class="smallcaps">disk</span></span>">磁盘管理 <span class="tag" tag-name="disk"><span class="smallcaps">disk</span></span></h1>
<h2 id="把/var或/tmp移动到新的分区的办法(参考:在系统工作时调整分区:整理数据)">把/var或/tmp移动到新的分区的办法(参考:http://www.ibm.com/developerworks/cn/linux/l-tip-prompt/tip04/">在系统工作时调整分区:整理数据)">把/var或/tmp移动到新的分区的办法(参考: href="在系统工作时调整分区:整理数据)</h2>" target="_blank">http://www.ibm.com/developerworks/cn/linux/l-tip-prompt/tip04/">在系统工作时调整分区:整理数据)</h2>
此种办法有很大缺陷,在移动/var目录时,由于有rpciod进程无法结束,原/var目录无法删除,导致后面的操作无法进行。因此更加稳健的做法还是在安装时分配好空间。
- 在新分区上创建文件系统
example
# mkfs.ext4 /dev/--?
- 将新分区挂载到 /mnt/rwstorage
example
# mkdir /mnt/rwstorage
# mount /dev/--? /mnt/rwstorage
- 创建新的 /tmp /var
example
cd /mnt/rwstorage
mkdir tmp
chmod 1777 tmp
mkdir var
- 进入单用户模式 我们的新目录 /mnt/rwstorage/tmp
具有临时目录应该具有的权限。现在,请进入单用户模式,因为我们必须复制整个
/var。像往常一样,我们将进入单用户模式推迟到最后一刻进行。从现在起我们不希望任何程序读写
/var
中的文件,所以必须停止所有守护程序,断开所有用户,执行一些快速的维护,为此请键入以下命令:
example
# init 1
- 复制 /var
example
# cd /var
# cp -ax * /mnt/rwstorage/var
- 执行备份并创建符号链接 这个命令完成以后,/mnt/rwstorage/var 就成为
/var 的一个完全相同的副本。现在您可能会问,如何让 Linux 正确使用
/mnt/rwstorage/var 和 /mnt/rwstorage/tmp
而不是根目录下的默认位置呢?使用符号链接很容易实现这一点 –
我们将创建新的符号链接 /tmp 和 /var,它们指向 /mnt/rwstorage
中的正确目录。我们首先备份原来的目录:
example
# cd /
# cp var var.old
# cp tmp tmp.old
最后一行可能没有必要,因为通常您不会将任何重要的文件放在 /tmp
中,但我们这样做更安全一些。现在开始创建符号链接:
example
cd /
ln -s /mnt/rwstorage/var /var
ln -s /mnt/rwstorage/tmp /tmp
- 最后修改 /etc/fstab 现在,当任何用户或程序使用 /var
时,他们将被自动转移到 /mnt/rwstorage/var!对于 /tmp
也一样。我们还有一个步骤要做;但是这一步可以安全地在多用户模式下执行。现在可以让
apache 恢复运行了,也可以让您的所有用户重新登录了。请按 CTRL-D 退出
runlevel 1。系统应该正常启动。请以 root
身份登录。我们必须完成的最后一个步骤是配置 /etc/fstab 文件,以便将
*dev*–? 挂载到 /mnt/rwstorage。必须在 /etc/fstab 中添加这样一行:
example
/dev/--? /mnt/rwstorage ext2 defaults 1 2
重要说明:如果您使用的内核版本属于 2.3+,则在您的 /etc/fstab
文件中很可能有这样一行:
example
none /var/shm shm defaults 0 0
该行在系统中启用共享内存,默认情况下它挂载在 /var
中。为了使该行正常工作,它必须出现在您刚添加的那一行的下一行。这样,当
Linux 启动时,/mnt/rwstorage 将首先被挂载(启用
/var)。然后,并且仅当在此以后,shm 设备将被挂载在
/var/shm,而后者实际上是
/mnt/rwstorage/var/shm。确保这两行的次序为:
example
/dev/--? /mnt/rwstorage ext2 defaults 1 2
none /var/shm shm defaults 0 0
在保存了对 /etc/fstab
的更改以后,您的系统已经成功升级了!在证实一切工作正常以后,您可删除所备份的
/tmp.old 和 /var.old 目录。恭喜您 –
为了获得最佳性能,您已经成功地对您的系统分区进行了重新配置。
<h2 id="文件系统 <span class="tag" tag-name="filesystem"><span class="smallcaps">filesystem</span></span>">文件系统 <span class="tag" tag-name="filesystem"><span class="smallcaps">filesystem</span></span></h2>
<h3 id="日志文件系统概念">日志文件系统概念</h3>
所谓日志文件系统,就是在文件系统中借用了数据库中“事务”(transaction)的概念,将文件的更新操作变成原子操作。具体来说,就是在修改文件系统内容的同时(或之前),将修改变化记录到日志中,这样就可以在意外发生的情况下,就可以根据日志将文件系统恢复到一致状态。这些操作完全可以在重新挂载文件系统时来完成,因此在重新启动机器时,并不需要对文件系统再进行一致性检查,这样可以大大提高系统的可用程度。
<h3 id="术语">术语</h3>
- The superblock
superblock并不真是block,它总是1024字节大小,并从第1024字节开始,通过以下命令可以查看superblock
\$ dd if=\$IMAGE bs=1024 skip=1 count=1 of=superblock
在/usr/include/linux/ext3<sub>fs</sub>.h中可以找到每个字节含义的说明。
- Groups
整个ext3文件系统被分成了多个group,每个group由固定数量的block组成(最后的group包含所有剩下的block),通过以下命令可以查看每个group有多少block:
dumpe2fs -h /dev/mapper/vg-rootfs \| grep 'Blocks per group'
每个group会用一个block用作位图,来标识block是否已经被使用。由此可以算出,每个group只能有4096×8=32768个block。还有一个block会用于标识group中已分配的inode位图。
- Group Descriptor Table(GDT)
GDT包括了以下内容:Blocks bitmap block,Inodes bitmap block,Inodes
table block,Free blocks count,Free inodes count,Directories
count。结构体ext3<sub>groupdesc</sub>,
在头文件/usr/include/linux/ext3<sub>fs</sub>.h中定义。
<h3 id="三种日志模式">三种日志模式</h3>
- data=journal
这会记录对所有文件系统数据和元数据的修改。这种模式可以将数据丢失的风险降至最低,但是速度也最慢。
- data=ordered
仅仅记录对文件系统元数据的修改,但是在修改相关文件系统元数据之前,需要将文件数据同步到磁盘上。
- data=writeback
仅仅记录对文件系统元数据的修改,对文件数据的修改按照标准文件系统的写操作过程进行处理。这种模式速度最快。
<h3 id="工具">工具</h3>
- 相关命令行工具
- tune2fs adjust tunable filesystem parameters on ext2/ext3/ext4
filesystems.
- dumpe2fs prints the super block and blocks group information for
the filesystem present on device. dumpe2fs /dev/sdb7 \| grep
"Group 0" -B 10 -A 21
- debugfs ext2/ext3/ext4文件系统调试器 用stat命令查看inode信息
- 数据恢复工具
<http://code.google.com/p/ext3grep/>
<http://e2undel.sourceforge.net/>
<http://dydou.cn/Linux/201008/0QQA142010.html>
- 磁盘基准测试(benchmarking)
- iozone
time /opt/iozone/bin/iozone -a -s 4G -q 256 -y 4
\>\|/root/ext4-iozone-stdout.txt iozone –Rab output.xls
生成Excel报告
通常情况下,测试的文件大小要求至少是系统内存的两倍以上,这样,测试的结果才是真是可信的。如果小于cache的两倍,文件的读写测试读写的将是cache的速度,测试的结果大打折扣。
如果要测试指定分区的性能,结合以下参数来测试:-f -U throughput
test 模式通过-t设置,参考-u -l -F。但不能与-U(remount)同时使用。
如果机器的内存太大,此时使用iozone测试时间太长,需要在grub的配置文件中设置mem的最大值,以减少测试使用的时间。
- 用dd作简单测试
测试磁盘写的能力
example
time dd if=/dev/zero of=/test.dbf bs=8k count=300000
测试磁盘读的能力
example
time dd if=/dev/sdb1 of=/dev/null bs=8k
同时测读写能力
example
time dd if=/dev/sdb1 of=/test.dbf bs=8k
<h3 id="将文件系统从ext3转换到ext4">将文件系统从ext3转换到ext4</h3>
example
tune2fs -O extent,uninit_bg,dir_index,flex_bg /dev/DEV
e2fsck -fD /dev/DEV #此命令可能会提示checksum invalid,并且选择不修正checksum错误,可以保留数据
CentOS6中初始安装ext3会有以下选项: has<sub>journal</sub>
ext<sub>attr</sub> resize<sub>inode</sub> dir<sub>index</sub> filetype
sparse<sub>super</sub> large<sub>file</sub>
初始安装的ext4会比ext3多出以下的选项: needs<sub>recovery</sub> extent
flex<sub>bg</sub> huge<sub>file</sub> uninit<sub>bg</sub>
dir<sub>nlink</sub> extra<sub>isize</sub>
其中的部分特性无法用tune2fs调整。 修改后注意更新/etc/fstab文件。
<h2 id="安装时磁盘布局">安装时磁盘布局</h2>
可用ext4的一定用ext4,目前ext4已经成熟,为性能考虑,优先选择ext4。 /boot
ext4 500M
其余分区使用lvm,每个挂载分区不必分配太大的空间,预留较多空闲空间以备机动。lvm中增加lv的容量是很方便的,然而缩减lv的容量却很不方便。
/root ext4 50G /var ext4 50G /opt ext4 100G /home ext4 50G
<h2 id="用noatime提高/var分区性能">用noatime提高/var分区性能</h2>
下面三种情形,且非常繁忙的服务器,可以考虑使用这个hack
- Squid caching file
- News server
- Web servers
不记录access time,可提高磁盘性能5-15%左右。
<h2 id="更改分区标签名">更改分区标签名</h2>
ext分区用e2label,ntfs分区用ntfslabel(ntfsprogs package)。
<h2 id="quota">quota</h2>
<h3 id="启用Journaled Quota">启用Journaled Quota</h3>
按通常的方式设置在/etc/fstab中quota,然后再用quotacheck时,quotacheck会报下面的错误信息:
example
quotacheck: Your kernel probably supports journaled quota but you are not using it. Consider switching to journaled quota to avoid running quotacheck after an unclean shutdown.
这是说,用journaled quota以后,免去了调用quotacheck的麻烦。使用journaled
quota其实只是使用了一组新的挂载参数而已:
example
/dev/sda1 / ext3 errors=remount-ro,usrjquota=aquota.user,grpjquota=aquota.group,jqfmt=vfsv0 0 1
ext3,ext4都是支持的。 当然不能忘记安装quota工具包。
example
sudo apt-get install quota
<h1 id="Redhat包管理 <span class="tag" tag-name="rpm"><span class="smallcaps">rpm</span></span>">Redhat包管理 <span class="tag" tag-name="rpm"><span class="smallcaps">rpm</span></span></h1>
<h2 id="yum配置文件的releasever变量 <span class="tag" tag-name="yum"><span class="smallcaps">yum</span></span>">yum配置文件的releasever变量 <span class="tag" tag-name="yum"><span class="smallcaps">yum</span></span></h2>
releasever变量由yum.conf文件中distroverpkg值来决定,如果不设定distroverpkg,那么默认为“redhat-release”。这个值与/etc/redhat-release
文件无关,实际上指的是 redhat-release
这个RPM包。所谓“distroverpkg=redhat-release”的意思,其实是将
\$releasever 设置为 redhat-release 这个RPM包的版本号。
在的RHEL改成CentOS后,最后安装centos-release包,删除redhat-release包,以防止yum使用错误的url,如:
example
http://mirror.centos.org/centos/6Server/sclo/x86_64/rh/repodata/repomd.xml: [Errno 14] PYCURL ERROR 22 - "The requested URL returned error: 404 Not Found"
这时要用rpm命令带–nodeps选项删除该redhat-release-server-6Server,然后安装centos-release。
有时也可以考虑在yum.conf中添加如下配置:
example
distroverpkg=centos-release
<h2 id="修复CentOS的yum仓库配置">修复CentOS的yum仓库配置</h2>
CentOS的yum仓库配置文件在centos-release包中,下载centos-release,然后用rpm命令安装,安装时用上–replacepkgs选项,这样就会把原始仓库配置文件再次安装到系统中。
<h2 id="CensOS中自带的一些软件仓库">CensOS中自带的一些软件仓库</h2>
example
yum search centos-release
可以找到很多新的配置仓库的包,这些仓库包含在centos的官方仓库中,因此国内镜像站是会同步的,安装时速度会快很多。
centos-release.x86_64 : CentOS release file
centos-release-SCL.x86_64 : CentOS Software Collections release configs
centos-release-cr.x86_64 : CentOS continuous release configs
centos-release-gluster37.noarch : GlusterFS 3.7 packages from the CentOS Storage SIG repository
centos-release-openstack.noarch : OpenStack from the CentOS Cloud SIG repo configs
centos-release-scl.noarch : Software collections from the CentOS SCLo SIG
centos-release-scl-rh.noarch : Software collections from the CentOS SCLo SIG (upstream scl only)
centos-release-virt-common.noarch : Common release file to establish shared metadata for CentOS Virt SIG
centos-release-xen.x86_64 : CentOS6 Xen Support repo configs
SIG指的是Special Interest
Groups,参考网址为:https://wiki.centos.org/SpecialInterestGroup
SCL与SCLo似乎有些区别。
<h2 id="RHEL配置额外的yum仓库">RHEL配置额外的yum仓库</h2>
- RPMForge
RPMForge" target="_blank">http://wiki.centos.org/AdditionalResources/Repositories/RPMForge">RPMForge
- EPEL(Extra Packages for Enterprise Linux)
example
rpm -Uvh http://download.fedora.redhat.com/pub/epel/6/x86_64/epel-release-6-5.noarch.rpm
- Les RPM de Remi - Packages <http://rpms.famillecollet.com/>
- 华中科大的镜像 <http://mirrors.hustunique.com/>
- 升级PHP版本 PHP最新版本在Remi仓库中有,Remi依赖EPEL。
<h2 id="CentOS上升级GCC">CentOS上升级GCC</h2>
example
$ sudo wget http://people.centos.org/tru/devtools-1.1/devtools-1.1.repo -P /etc/yum.repos.d
$ sudo sh -c 'echo "enabled=1" >> /etc/yum.repos.d/devtools-1.1.repo'
$ sudo yum install devtoolset-1.1
使用时:
example
$ scl enable devtoolset-1.1 bash
$ gcc --version
也可使用下面的配置: export
CC=/opt/centos/devtoolset-1.1/root/usr/bin/gcc export
CPP=/opt/centos/devtoolset-1.1/root/usr/bin/cpp export
CXX=/opt/centos/devtoolset-1.1/root/usr/bin/c++
<http://ask.xmodulo.com/upgrade-gcc-centos.html>
<h2 id="使用代理时yum需要配置proxy">使用代理时yum需要配置proxy</h2>
即使设置了环境变量http<sub>proxy</sub>,也仍旧需要在/etc/yum.conf中设置proxy:
example
proxy=http://10.2.3.77:3128/
<h2 id="failovermethod">failovermethod</h2>
failovermethode 有两个选项roundrobin
和priority,意思分别是有多个url可供选择时,yum 选择的次序,roundrobin
是随机选择,如果连接失败则使用下一个,依次循环,priority 则根据url
的次序从第一个开始。如果不指明,默认是roundrobin。
<h2 id="yum-cron自动更新">yum-cron自动更新</h2>
<h2 id="安装开发工具">安装开发工具</h2>
example
yum groupinstall "Development Tools"
<h1 id="xml <span class="tag" tag-name="xml"><span class="smallcaps">xml</span></span>">xml <span class="tag" tag-name="xml"><span class="smallcaps">xml</span></span></h1>
<h2 id="xmltools">xmltools</h2>
<h3 id="完成xslt转换的命令行工具一般用xalan">完成xslt转换的命令行工具一般用xalan</h3>
<h3 id="xmlstarlet命令行工具transform,query,validate,edit XML files">xmlstarlet命令行工具transform,query,validate,edit XML files</h3>
<h3 id="<http://xmlsoft.org/>"><http://xmlsoft.org/></h3>
<h1 id="Grub">Grub</h1>
<h2 id="更改默认启动项">更改默认启动项</h2>
1.) Press Ctrl+Alt+T to open terminal. Edit the “/etc/default/grub” via
below command and change GRUB<sub>DEFAULT</sub>=0 to
GRUB<sub>DEFAULT</sub>=saved. This will make it easy to change default
OS later.
example
sudo gedit /etc/default/grub
2.) Update grub to apply changes to grub configuration:
example
sudo update-grub
3.) After that, you can run sudo grub-set-default with the number of
menu entry to boot (the first entry is 0) at any time, which will set
the entry as default OS permanently. Or run sudo grub-reboot only for
next boot.
For example, below command will set Windows 7 as default OS (Windows 7
is 4 in picture at top) permanently.
sudo grub-set-default 4
<h2 id="set root">set root</h2>
example
set root='hd0,msdos1'
root的值为接下来grub操作的rootfs(猜测?),grub的linux等命令等会以此为出发点,比如:
example
linux /vmlinuz-3.18.12-gentoo root=/dev/sda8 ro
其中vmlinuz文件就是要从root指定的位置下开始找。至于linux启动命令中的root,就是另外一回事情了,它指定的是启动的linux内核的rootfs所在的磁盘分区。
<h1 id="键盘映射">键盘映射</h1>
Linux 虚拟控制台上,可利用 loadkeys 命令将特殊按键映射为特殊字符;而在 X
Window 中,必须使用 xmodmap
命令完成键盘映射。这些命令均按照字符映射表文件(文本文件)中的规定完成相应的转换。
字符映射表文件保存在 /usr/lib/kbd/keytables 目录下,defkeymap.map
是默认的字符映射表文件。利用命令: \$ loadkeys fr.map 可装入 fr.map
所规定的字符映射表。这时,按下“.”会显示“:”。命令 \$ loadkeys –d
可装入默认字符映射表。对于非默认的键盘映射,可在启动时在 shell
脚本中装入特殊的映射表。 对X Window
而言,它对键盘的处理过程分如下两个步骤: 1.X Server
首先将键码转换为键符号名(keysym)。文件 /usr/include/X11/keysymdef.h
中包含所有的符号名。X Server 能够区分修饰键带来的不同,因为 keysymdef.h
中区分了两种不同的键,例如对“a”和“A”,分别用“KS<sub>a</sub>”和“KS<sub>A</sub>”定义。
2.X Server 将键符号翻译为 ASCII
字符串。对于大多数的键来说,该字符串只包含一个字符,而对于功能键等特殊按键来说,则包含多个字符。例如,F5
键对应的默认 ASCII 字符串为 “5~”。 利用 xmodmap
工具可修改键盘和键符号名之间的对应关系。例如,X Window 中“A”的键码为
30,而“Q”的键码为16。如果建立文件 maptest: keycode 38 = A keycode 24 =
Q 在 xterm 中运行 \$ xmodmap maptest
之后,将发现“A”键和“Q”键交换了过来。 上述的 maptest
文件实际就是一个简单的 X Window 映射文件。但需要注意的是,文件中的
keycode 和 Linux 内核对键值的定义是不一样的,一般而言,X Window
中的键码要比内核的键值大 8。利用 showkey
命令可以查看内核对键值的定义。例如,运行 showkey
并前后按下“A”和“Q”后,程序的输出为: \$ showkey kb mode was RAW …
keycode 30 press keycode 30 press keycode 16 press keycode 16 press
该程序给出的是“A”和“Q”的内核键值。 在 XF86Config 文件中,Keyboard
段用来指定键盘参数,一般而言,这些参数不需要特殊设置: Section
“Keyboard” Protocol “Standard” AutoRepeat 500 5 EndSection xmodmap
tutorial <http://xahlee.info/linux/linux_xmodmap_tutorial.html>
MovingTheCtrlKey <https://www.emacswiki.org/emacs/MovingTheCtrlKey>
<h1 id="问题">问题</h1>
<h2 id="打印man或info文档">打印man或info文档</h2>
- Converting man page into text files man passwd \| col -b \>
passwd.txt lpr passwd.txt man passwd \| col -b \| lpr
- You can also print “info” pages with the same procedure: info passwd
\| col -b \| lpr
- Create PDF: man -t passwd \| ps2pdf - passwd.pdf
<h2 id="VPN拔号的注意事项 <span class="tag" tag-name="vpn"><span class="smallcaps">vpn</span></span> <span class="tag" tag-name="pptp"><span class="smallcaps">pptp</span></span>">VPN拔号的注意事项 <span class="tag" tag-name="vpn"><span class="smallcaps">vpn</span></span> <span class="tag" tag-name="pptp"><span class="smallcaps">pptp</span></span></h2>
- VPN网络的MTU比一般网络要小,因此要确定该网络的MTU Windows: ping -f
-l 1472 xxx.xx.xx.xxx 1472+28=1500 Linux: ping -M do -c 3 -s 1372
10.2.3.77 ping时通过改变包的大小,可以测出什么时候达到了MTU
修改/etc/ppp/options文件,添加一行mtu 1400
- Network-Manager中的PPTP设置必须选中PPTP高级选项中的"Use
Point-to-Point encryption(MPPE)"
<h2 id="Wubi安装的Ubuntu添加硬盘,调整swap的方法 <span class="tag" tag-name="wubi"><span class="smallcaps">wubi</span></span>">Wubi安装的Ubuntu添加硬盘,调整swap的方法 <span class="tag" tag-name="wubi"><span class="smallcaps">wubi</span></span></h2>
添加硬盘的办法 cd /host/ubuntu/disks dd if=/dev/zero of=extra.disk
bs=1MB count=10000 seek=1 mkfs.ext3 -F extra.disk 调整swap空间的办法
sudo su swapoff -a cd *host/ubuntu/disks* mv swap.disk swap.disk.bak dd
if=/dev/zero of=swap.disk bs=1024 count=2097152 mkswap swap.disk swapon
-a free -m
<h2 id="修改Linux主机名">修改Linux主机名</h2>
- RedHat系列
更改/etc/sysconfig/network文件中的HOSTNAME值及/etc/hosts,并用hostname命令更改。
- Debian系列 更改/etc/hostname及/etc/hosts,并用hostname命令更改。
<h2 id="内核报iwlagn错误">内核报iwlagn错误</h2>
example
iwlagn 0000:03:00.0: Microcode SW error detected. Resta ...: 4 Time(s)
需安装linux-backports-modules-wireless以保证更新kernel同时也更新wireless
driver。 与之相关的包linux-preempt。
<h2 id="用find合并文本文件,并且保留文件名">用find合并文本文件,并且保留文件名</h2>
example
find . -type f -print0 | xargs -0 -I % sh -c 'echo %; cat %'
<h2 id="命令行下查看电池状态">命令行下查看电池状态</h2>
下面的目录为电源相关的信息。
example
/sys/class/power_supply
BAT0目录即指向电池相关的信息。charge<sub>full和chargenow中的数据可算出当前电源充满的程度</sub>。
<h1 id="tools <span class="tag" tag-name="tools"><span class="smallcaps">tools</span></span>">tools <span class="tag" tag-name="tools"><span class="smallcaps">tools</span></span></h1>
<h2 id="Flash Tools">Flash Tools</h2>
- swftools SWF Tools is a collection of SWF (Flash) manipulation and
creation utilities. This package includes: pdf2swf, jpeg2swf,
png2swf, gif2swf, font2swf, wav2swf, swfcombine, swfextract,
swfdump, swfstrings, swfbbox and swfc.
- swfdec 开源Flash播放器
- gnash GNU Flash播放器
- Adobe Flash Standalone Player for Linux
<http://blogs.adobe.com/penguinswf/>
- Flash Extractor swftools图形化界面
- cclive cclive is a lightweight command line video extraction tool
for Youtube and other similar video websites. cclive is primarily a
"video download tool" but it can also be used along side with video
player software, like "mplayer", for viewing streamed videos instead
of the Adobe flash player.
<h2 id="PDF Tools">PDF Tools</h2>
- poppler-utils, poppler-data
- pdfcrack 破解密码
- pdfresurrect 分析PDF文件版本变化
- pdfchain 编辑PDF文件
- pdfjam
- pdftk 命令行PDF合并、分离工具
<h2 id="用lftp解决ftp编码问题">用lftp解决ftp编码问题</h2>
lftp可设置属性 set <ftp:charset> GBK set <file:charset> UTF-8
<h2 id="用gcolor2拾取屏幕颜色">用gcolor2拾取屏幕颜色</h2>
<h2 id="Font Tools">Font Tools</h2>
- Fontmatrix
- FontForge
- Fonty Python
- Font Manager
- Opcion Font Viewer
<h2 id="ISO Tools">ISO Tools</h2>
- Nero Image nrg2iso - Extracts ISO9660 data from Nero ".nrg" files
- Cue/Bin
example
bchunk myfile.bin myfile.cue myfile
- Clone CD 克隆CD
(.ccd/.img/.sub)文件必须转换才能使用。可以使用ccd2iso来转换。该软件安装包包含在extremely
unstable (不稳定版本),并被Ubuntu论坛( )
采用。它也是仅有的针对Pentium(i386)系统的软件包。
example
ccd2iso myfile.img myfile.iso
- Alcohol 120% Image
example
mdf2iso myfile.mdf myfile.iso
- cdemu
<h2 id="集中统一批量部署管理工具">集中统一批量部署管理工具</h2>
puppet, cfengine, func, lcfg, bcfg2, SmartFrog
<h1 id="command">command</h1>
<h2 id="at">at</h2>
结束代码输入用Ctl+D
<h2 id="与文件属性相关的几个命令">与文件属性相关的几个命令</h2>
stat,lsattr,chattr
<h2 id="libc-bin">libc-bin</h2>
catchsegv,getconf,getent,iconv,ldd,localedef,locale,tzselect,rpcinfo,zdump
<h2 id="dd">dd</h2>
<h3 id="从光盘镜像制作启动U盘">从光盘镜像制作启动U盘</h3>
example
dd if=ubuntu.iso of=/dev/sdb bs=512k
<h3 id="用dd切割或合并文件">用dd切割或合并文件</h3>
从800M的文件中切除前面1131字节,下面的办法会很慢:
example
dd bs=1 skip=1131 if=filtered.dump of=trimmed.dump
每次只处理1字节。下面是两种较好的办法:
example
dd bs=1131 skip=1 if=filtered.dump of=trimmed.dump
dd if=filtered.dump bs=512k | { dd bs=1131 count=1 of=/dev/null; dd bs=512k of=trimmed.dump; }
<http://unix.stackexchange.com/questions/6852/best-way-to-remove-bytes-from-the-start-of-a-file>
<h2 id="rsync">rsync</h2>
rsync的-S选项有时会有意想不到的缩小磁盘占用空间的效果。
<h2 id="查找文件percol">查找文件percol</h2>
<http://blog.binchen.org/posts/how-to-do-the-file-navigation-efficiently.html>
<h1 id="备份">备份</h1>
<h2 id="cpio">cpio</h2>
example
% find . -print -depth | cpio -ov > tree.cpio
使用-depth后,find命令输出时会把父目录放在文件之后显示,cpio在处理时会更少出错。
example
% cpio -idv < tree.cpio
<h1 id="Locale">Locale</h1>
<h2 id="设置locale">设置locale</h2>
设定locale就是设定12大类的locale分类属性,即
12个LC<sub>\*</sub>。除了这12个变量可以设定以外,为了简便起见,还有两个变量:LC<sub>ALL和LANG</sub>。它们之间有一个优先级的关系:
example
LC_ALL>LC_*>LANG
可以这么说,LC<sub>ALL是最上级设定或者强制设定</sub>,而LANG是默认设定值。
<h1 id="命令行神器">命令行神器</h1>
- cppman:C++ 98/11/14 手册查询 for Linux/MacOS
- cgasm:命令行查询汇编指令
- ncdu:可视化的空间分析程序
- glances:更强大的 htop / top 代替者
- cheat:命令行笔记 就是各种 cheat sheet ,比如经常搞忘 redis
命令的话,你可以新建 ~/.cheat/redis
这个文件,写一些内容。然后使用的时候,cheat redis
命令就可以显示出来刚才新建的 cheat sheet 了。
- multitail:多重 tail
- bro:以用例为主的帮助系统
example
gem install bropages
``