Linux Tweaks

1 删除残余的配置文件

通常Debian/Ubuntu删除软件包可以用两条命令

1
2
sudo apt-get remove <package-name>
sudo apt-get purge <package-name>

remove将会删除软件包,但会保留配置文件.purge会将软件包以及配置文件都删除.
找出系统上哪些软件包留下了残余的配置文件

1
dpkg --list | grep "^rc"
1
dpkg --list | grep "^rc" | cut -d " " -f 3 | xargs sudo dpkg --purge

2 清理下载的缓存包

sudo apt-get install安装软件时,其下载的包都缓存在/var/cache/apt/archives/目录中。如果要清理掉这些已下载的缓存包,那么可以执行命令0

1
2
$ sudo apt-get autoclean
$ sudo apt-get clean

3 清理孤立的库文件

有时候,你用apt-get安装一个软件包时会自动安装其他的依赖.当你删除掉这个软件包时,这些依赖也就没有用处了.这些没有用的依赖包叫做孤儿软件包,可以用下面的命令删除

1
$ sudo apt-get autoremove

不过apt-get autoremove只会删除经apt-get自动安装的依赖包,而你自己手动安装的依赖包则不会被删除,这时我们可以用deborphan来彻底删除.

1
2
3
4
5
6
7
$ sudo apt-get install deborphan
# 列出孤儿软件包
$ deborphan
gstreamer0.10-pocketsphinx:amd64
gstreamer0.10-dvswitch:amd64
# 删除它们
$ deborphan | xargs sudo apt-get purge -y

4 删除多余的映像

Ubuntu Linux内核升级频繁,如果不删除旧的内核,将会占用大量磁盘空间,而且开机启动有诸多选项(这点,可以通过vim /boot/grub/menu.lst手工修改,即删除多余启动项)。这些操作成为释放空间的主要来源,删除一个内核映射约可以释放100M空间:-)

4.1 查看已安装内核映像

查看已安装内核映像:

1
2
3
4
5
6
$ sudo dpkg --get-selections  | grep linux-image
linux-image-2.6.32-25-generic install /*linux-xx.xx.xx-xx(-generic)是升级的内核版本,可以删除*/
linux-image-2.6.32-26-generic install
linux-image-2.6.32-27-generic install
linux-image-2.6.32-31-generic install
linux-image-generic install /*Linux发布版完整内核映像,不可删除*/

4.2 查看当前内核版本

查看当前内核版本

1
2
$ uname -r
2.6.32-31-generic

4.3 删除多余内核映像

删除旧的内核映像(只须保留linux-image-generic即可,和当前内核版本即可)。

1
$ sudo apt-get purge linux-image-2.6.32-26-generic

5 删除旧的编译库

5.1 查看已有的编译库

查看已有的内核C编译库,即头文件库:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ sudo dpkg --get-selections  | grep linux-headers
linux-headers-2.6.28-19 install /*内核C编译库,即头文件库*/
linux-headers-2.6.28-19-generic install
linux-headers-2.6.32-24 install
linux-headers-2.6.32-24-generic install
linux-headers-2.6.32-25 install
linux-headers-2.6.32-25-generic install
linux-headers-2.6.32-26 install
linux-headers-2.6.32-26-generic install
linux-headers-2.6.32-27 install
linux-headers-2.6.32-27-generic install
linux-headers-2.6.32-30 install
linux-headers-2.6.32-30-generic install
linux-headers-2.6.32-31 install
linux-headers-2.6.32-31-generic install
linux-headers-generic install /*Linux发布版的C编译库,不可删除*/

5.2 删除旧的编译库

编译库与内核版本一一样对应,只须保留与当前内核版本相对应的编译库,如此例的linux-headers-2.6.32-31-generic,其他的皆可删除。

1
$ sudo apt-get purge linux-headers-2.6.28-19

Auto Login

Terminator

1
2
3
:~$ systemctl get-default
graphical.target
:~$ systemctl set-default multi-user.target

autologin

1
2
3
4
5
6
7
:~$ systemctl edit getty@tty1
[Service]
ExecStart=
ExecStart=-/sbin/agetty --autologin root --noclear %I 38400 linux
:~$ systemctl daemon-reload
:~$ systemctl restart getty@tty1.service
:~$ reboot

only start 1 tty

1
:~$ sed -i 's|^NAutoVTs=\n|NAutoVTs=1|' /etc/systemd/logind.conf

Graphical

1
2
3
:~$ systemctl get-default
multi-user.target
:~$ systemctl set-default graphical.target

GNOME

/etc/gdm3/daemon.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# GDM configuration storage - modified by kali-root-login
#
# See /usr/share/gdm/gdm.schemas for a list of available options.

[daemon]
# Uncomment the line below to force the login screen to use Xorg
WaylandEnable=false
# Enabling automatic login
AutomaticLoginEnable = true
AutomaticLogin = root

# Enabling timed login
# TimedLoginEnable = true
# TimedLogin = user1
# TimedLoginDelay = 10

# Reserving more VTs for test consoles (default is 7)
# FirstVT = 9

[security]
AllowRoot = false

[xdmcp]

[greeter]
# Only include selected logins in the greeter
# IncludeAll = false
# Include = user1,user2

[chooser]

[debug]
# More verbose logs
# Additionally lets the X server dump core if it crashes
# Enable = true

开机后直接进入Terminator

1
2
3
4
5
# auto login Terminator
systemctl set mult-users.target as default
# auto login GUI
systemctl set graphic-users.target as default

魔法键组合 reisub - force reboot linux

原文

如果某一天你的 Linux 死机了,键盘不听使唤了,Ctrl+Alt+F1 已经没有任何反应,该怎么办呢?
使用“魔法键”:Alt+SysRq + r,e,i,s,u,b

说具体一点, 是一种系统请求, 直接交给内核处理。
键盘上一般都有一个键SysRq, 和PrintScreen(截屏)在一个键位上,这就是系统请求的键。
这个方法可以在死机的情况下安全地重启计算机, 数据不会丢失。

这里解释下:
其实 SysRq是一种叫做系统请求的东西, 按住 Alt-Print 的时候就相当于按住了SysRq键,这个时候输入的一切都会直接由 Linux 内核来处理,它可以进行许多低级操作。

这个时候 reisub 中的每一个字母都是一个独立操作,分别表示:

  • r : unRaw 将键盘控制从 X Server 那里抢回来
  • e : tErminate 给所有进程发送 SIGTERM 信号,让它们自己解决善后
  • i : kIll 给所有进程发送 SIGKILL 信号,强制他们马上关闭
  • s : Sync 将所有数据同步至磁盘
  • u : Unmount 将所有分区挂载为只读模式
  • b : reBoot 重启

操作步骤(亲测)

究竟该怎么按这些按钮才会有效?

首先,你的系统要支持这个功能,查看和开启的方法大家应该很熟悉了,网上也有很多说明,而且最幸运的是:Ubuntu 默认已经开启了这个功能。

接下来就是操作:马上你就会发现,同时按下+压根儿行不通!只会蹦出来一个屏幕截图窗口。所以,真正的做法应该是:

  1. 伸出你的左手,同时按住<Ctrl>+<Alt>键,别松开
  2. 右手先按一下<SysRq>,左手别松开,等1秒
  3. 右手按一下 R,左手别松开,等1秒
  4. 右手按一下 E,左手别松开。这时包括桌面在内,所有程序都会终止,你会看到一个黑乎乎的屏幕,稍微等一段时间
  5. 右手依次按下 I,S,U,B,左手别松开。每按一次都等那么几秒种,你会发现每按一次,屏幕上信息都会有所变化。最后按下B时,屏幕显示reset,这时你的左手可以松开了,等几秒钟,计算机就会安全重启。

Disable IPv6

Use Sysctl

1
2
3
sudo sysctl -w net.ipv6.conf.all.disable_ipv6=1
sudo sysctl -w net.ipv6.conf.default.disable_ipv6=1
sudo sysctl -w net.ipv6.conf.lo.disable_ipv6=1

However, this only temporarily disables IPv6. The next time your system boots, IPv6 will be enabled again.

One method to make this option persist is modifying /etc/sysctl.conf.

1
2
3
4
5
6
:~$ cat >> /etc/sysctl.conf <<EOF
net.ipv6.conf.all.disable_ipv6=1
net.ipv6.conf.default.disable_ipv6=1
net.ipv6.conf.lo.disable_ipv6=1
EOF
:~$ sudo sysctl -p

Disable IPv6 using GRUB

An alternative method is to configure GRUB to pass kernel parameters at boot time. You’ll have to edit /etc/default/grub. Once again, make sure you have administrator privileges:

1
2
3
4
5
6
:~$ grep GRUB_CMDLINE_LINUX /etc/default/grub
GRUB_CMDLINE_LINUX_DEFAULT="quiet"
GRUB_CMDLINE_LINUX=""
:~$ sed -i 's/GRUB_CMDLINE_LINUX_DEFAULT="quiet"/GRUB_CMDLINE_LINUX_DEFAULT="quiet ipv6.disable=1"/' /etc/default/grub
:~$ sed -i 's/GRUB_CMDLINE_LINUX=""/GRUB_CMDLINE_LINUX="ipv6.disable=1"/' /etc/default/grub
:~$ update-grub

add aliases when using sudo

1
2
3
4
5
6
7
8
9
10
11
12
:~$ alias lah='ls -lah'
:~$ lah
drwxr-xr-x 35 haven200 haven200 20K Oct 5 16:57 ./
drwxr-xr-x 8 root root 4.0K Jul 26 21:31 ../
-rw-r--r-- 1 haven200 haven200 1.3K Sep 27 21:37 .bash_aliases
-rw-r--r-- 1 haven200 haven200 220 Jun 24 2016 .bash_logout
-rw-r--r-- 1 haven200 haven200 25 Jan 19 2017 .bash_profile
...
...
:~$ sudo lah
[sudo] password for haven200:
sudo: lah: command not found

Add the following line to ~/.bashrc:

1
alias sudo='sudo '

From the bash manual:

Aliases allow a string to be substituted for a word when it is used as the first word of a simple command. The shell maintains a list of aliases that may be set and unset with the alias and unalias builtin commands.
**The first word of each simple command, if unquoted, is checked to see if it has an alias. **If so, that word is replaced by the text of the alias. The characters ‘/’, ‘$’, ‘`’, ‘=’ and any of the shell metacharacters or quoting characters listed above may not appear in an alias name. The replacement text may contain any valid shell input, including shell metacharacters. The first word of the replacement text is tested for aliases, but a word that is identical to an alias being expanded is not expanded a second time. This means that one may alias ls to “ls -F”, for instance, and Bash does not try to recursively expand the replacement text. **If the last character of the alias value is a space or tab character, then the next command word following the alias is also checked for alias expansion. **

Bash only checks the first word of a command for an alias, any words after that are not checked. That means in a command like sudo ll, only the first word (sudo) is checked by bash for an alias, ll is ignored. We can tell bash to check the next word after the alias (i.e sudo) by adding a space to the end of the alias value.