2018年7月17日 星期二

Linux設定時間的方式


  1. copy /usr/share/zoneinfo/* 內的確切時區資料檔案為 /etc/localtime
  2. 執行 date -u
  3. 輸入 date 看看是否已經是正確時區
  4. 調整系統時間
    1. 透過 ntpdate -u time.stdtime.gov.tw 來校正目前時間
    2. 透過 date –set=“2003-02-19 17:44” 方式直接手動設定
  5. 輸入 hwclock 看看目前 BIOS 的時間是否正確
  6. 執行 clock –systohc 將目前系統時間寫入 BIOS 時間
  7. 輸入 date 與 hwclock 看看時間是否一致..

以下是執行範例

[root@tryboxap01 ~]# cp /usr/share/zoneinfo/Asia/Taipei /etc/localtime
[root@tryboxap01 ~]# md5sum /usr/share/zoneinfo/Asia/Taipei
2cb7700780c8b4963f72c9dab967cffa  /usr/share/zoneinfo/Asia/Taipei
[root@tryboxap01 ~]# md5sum /etc/localtime
2cb7700780c8b4963f72c9dab967cffa  /etc/localtime
[root@tryboxap01 ~]# date -u
Thu Jun 26 01:46:53 UTC 2008
[root@tryboxap01 ~]# date
Thu Jun 26 09:47:09 CST 2008
[root@tryboxap01 ~]# ntpdate -u time.stdtime.gov.tw
26 Jun 09:48:58 ntpdate[4517]: adjust time server 220.130.158.52 offset -0.000482 sec
[root@tryboxap01 ~]# hwclock
Sat Apr 26 09:49:22 2008  -0.812668 seconds
[root@tryboxap01 ~]# clock --systohc
[root@tryboxap01 ~]# date
Thu Jun 26 09:49:48 CST 2008
[root@tryboxap01 ~]# hwclock
Thu Jun 26 09:49:51 2008  -0.179890 seconds

Debian LightDM

LightDM is a cross-desktop Display Manager. It was built as a relatively light-weight and highly customizable alternative to GDM. It's developed at Canonical by Robert Ancell and all contributors are required to assign the their copyright to Canonical through a Contributor License Agreement that allows proprietarization.
  • No Gnome dependencies
  • Uses webkit to render themes
  • Supports Gtk and Qt
  • Highly customizable (basic gtk theme, unity theme in Ubuntu Precise)
LightDM was introduced in Wheezy, it's stable enough for use also in current stable and Sid.
To install lightdm, run as root
apt-get install lightdm

System-wide configuration

LightDM configuration file is found at /etc/lightdm/lightdm.conf. Making a backup of the original configuration file is recommended.
Alternatively, create /etc/lightdm/lightdm.conf.d/ and place your configuration files there.
To change the current default Display Manager, run
dpkg-reconfigure lightdm
If you're new to LightDM, it's recommended to have GDM, slim or another Display Manager installed as a backup.

Viewing current configuration

To view effective configuration, run
/usr/sbin/lightdm --show-config
This will show current settings, with the configuration files these settings were read from.

Testing

Install xserver-xephyr. Then, run LightDM as an X application:
$ lightdm --test-mode --debug

Enable autologin

Look up these lines in lightdm configuration file, uncomment them and customize to your preference.
[Seat:*]
#autologin-user=
#autologin-user-timeout=0

Enable user list

By default, LightDM is configured so that the user should enter login name and password. Login name is considered sensitive information.
It is possible to provide the user with selection of available user accounts. The most recently used login name will be selected in the list. The user still has to enter password to login. This provides useful compromise between security and convenience for a single-user desktop system.
To enable user list, place the following settings into /usr/share/lightdm/lightdm.conf.d/01_my.conf:
[Seat:*]
greeter-hide-users=false

Change the greeter's background

Debian's LightDM GTK greeter is configured in /etc/lightdm/lightdm-gtk-greeter.conf. There, we may find that the background image points to /usr/share/images/desktop-base/desktop-background, which is a symlink managed by update-alternatives and is also the main desktop background, or we may find (Debian 9 and up) that no value is set in the greeter.conf file.
If the lightdm greeter's background is using the symlink (debian 8 and below), the best way is to find some images that you like from /usr/share/images/desktop-base/, then use update-alternatives to change the desktop-background group:
update-alternatives --config desktop-background
If not, then edit /etc/lightdm/lightdm-gtk-greeter.conf and place an integer value (to set a color) or a filename of a .png or .svg file to be used as the greeter image in the file, like this:
[greeter]
background=/path/to/file.svg

User configuration


LightDM itself has no user configuration. Once you've authenticated with your username and password, LightDM will run an X session. If you wish to configure your login environment, you must use the configuration files for whichever type of X session your system is configured to use. By default, this will be the Debian Xsession.

2018年7月13日 星期五

Linux 用 ps 與 top 指令找出最耗費 CPU 與記憶體資源的程式

這裡介紹如何在 Linux 中使用 ps 與 top 指令列出系統上最吃 CPU 與記憶體的程式。
作為 Linux 系統的管理者,時常都需要查看系統的負載狀況,如果系統中出現不正常的程式,吃掉太多的 CPU 或記憶體資源,就會影響系統的效能,太嚴重的話甚至會造成當機等狀況。

查閱系統負載(loading)與行程(process)的狀況最常用的就是 ps 與 top 這兩個指令,以下我們示範如何利用這兩個指令撰寫簡單的指令稿,自動找出系統上最耗費資源的程式。

ps 指令

這行指令可利用 ps 指令列出行程的一些基本資訊,按照每個行程所使用的記憶體排序後,列出排名最前面的幾個行程,也就是列出系統上最耗費記憶體的程式:
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head
這裡的 -e 參數是代表輸出所有行程的資訊,而 -o 參數則是用來指定輸出欄位用的,後面接著所有想要輸出的欄位名稱,這裡我們讓 ps 輸出以下幾個欄位:
  • pid:行程 ID(process ID)。
  • ppid:父行程 ID(parent process ID)。
  • cmd:程式名稱。
  • %mem:記憶體使用量(百分比)。
  • %cpu:CPU 使用量(百分比)。
而 --sort 參數則是指定排序的依據欄位,預設會依照數值由小到大排序,若要由大到小的方式排序的話,可以在欄位名稱前加上一個負號。以這個例子來說,我們將排序的欄位指定為 -%mem,這樣就可以依照記憶體使用量,從大到小排序。
最後將 ps 的輸出以 Linux 管線(pipe)導向至 head,只保留前 10 行的資料,其餘的都丟棄,以下是輸出的結果。
  PID  PPID CMD                         %MEM %CPU
 2124  1278 /usr/lib/chromium-browser/c 25.0 14.4
 1446  1278 /usr/lib/chromium-browser/c 21.5 15.4
 1253     1 /usr/lib/chromium-browser/c 19.2 13.0
 1328  1278 /usr/lib/chromium-browser/c  7.6  2.9
 1392  1278 /usr/lib/chromium-browser/c  7.6  0.5
  732   669 /usr/bin/X :0 -seat seat0 -  5.7  2.0
 1060     1 /usr/lib/arm-linux-gnueabih  1.8  0.0
 1086   758 pcmanfm --desktop --profile  1.5  0.2
 1085   758 lxpanel --profile LXDE-pi    1.5  0.3

ps 指令輸出
若要找出系統上最耗費 CPU 的程式,也是使用類似的指令,只是在排序時,將排序的欄位換成 CPU 使用量:
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%cpu | head
輸出為:
  PID  PPID CMD                         %MEM %CPU
 1446  1278 /usr/lib/chromium-browser/c 33.8 19.6
 1253     1 /usr/lib/chromium-browser/c 19.6 12.1
 2124  1278 /usr/lib/chromium-browser/c 20.5 11.2
 2555     1 gvim -f                      2.5  6.3
 1328  1278 /usr/lib/chromium-browser/c  6.1  3.0
  732   669 /usr/bin/X :0 -seat seat0 -  7.1  2.2
 1392  1278 /usr/lib/chromium-browser/c  6.7  0.5
 1085   758 lxpanel --profile LXDE-pi    1.5  0.3
 1060     1 /usr/lib/arm-linux-gnueabih  1.3  0.1

ps 指令輸出

top 指令

top 指令是一個互動式(interactive)的工具,可以顯示即時的系統負載狀態,而它也可以用於指令稿中,輸出各種系統資訊。
這行指令可將系統行程以記憶體的使用賴排序後,以 batch 模式輸出報表,並且只保留前 10 個最耗費記憶體的行程:
top -b -o +%MEM | head -n 17
其中 -b 參數是 batch 模式的意思,而 -o 參數則是設定以記憶體用量來排序行程,最後面的 head -n 17 則是篩選 top 輸出的文字內容,只保留前 17 行,剩餘的內容則捨棄。輸出會類似這樣:
top - 14:54:24 up 7 min,  3 users,  load average: 0.99, 1.52, 0.87
Tasks: 164 total,   1 running, 163 sleeping,   0 stopped,   0 zombie
%Cpu(s): 17.0 us,  2.7 sy,  0.1 ni, 68.2 id, 11.9 wa,  0.0 hi,  0.1 si,  0.0 st
KiB Mem:    947732 total,   757336 used,   190396 free,     9892 buffers
KiB Swap:  1914876 total,     8588 used,  1906288 free.   288704 cached Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
 1392 pi        20   0  557272 206424  90776 S   0.0 21.8   1:46.69 chromium-b+
 1223 pi        20   0  674816 194156 126276 S   0.0 20.5   1:42.47 chromium-b+
 1490 pi        20   0  459804 164732  76728 S   5.6 17.4   0:44.62 chromium-b+
  732 root      20   0  245288  84204  52640 S   0.0  8.9   0:15.90 Xorg
 1353 pi        20   0  366756  82720  47504 S   0.0  8.7   0:07.21 chromium-b+
 1357 pi        20   0  355952  77080  55684 S   0.0  8.1   0:09.30 chromium-b+
 1293 pi        20   0  354048  73844  51024 S   0.0  7.8   0:13.32 chromium-b+
 1084 pi        20   0  105612  29380  24928 S   0.0  3.1   0:02.43 lxpanel
 1243 pi        20   0  189852  28780  23760 S   0.0  3.0   0:00.16 chromium-b+
 1086 pi        20   0  147124  26976  24256 S   0.0  2.8   0:01.63 pcmanfm

top 指令輸出
若要找出最耗費 CPU 資源的行程,則改用 CPU 使用量來排序即可:
top -b -o +%CPU | head -n 17
輸出會類似這樣:
top - 15:12:19 up 25 min,  4 users,  load average: 1.66, 1.43, 1.08
Tasks: 173 total,   1 running, 172 sleeping,   0 stopped,   0 zombie
%Cpu(s): 13.4 us,  1.8 sy,  0.1 ni, 79.0 id,  5.6 wa,  0.0 hi,  0.1 si,  0.0 st
KiB Mem:    947732 total,   870680 used,    77052 free,     5588 buffers
KiB Swap:  1914876 total,   148740 used,  1766136 free.   267360 cached Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
 1959 pi        20   0  327404  49012  22340 S 107.0  5.2   0:02.75 java
 1490 pi        20   0  459804  99192  49040 S   5.9 10.5   1:26.10 chromium-b+
 1809 pi        20   0  576268 162008  55228 S   5.9 17.1   0:57.49 chromium-b+
 1969 pi        20   0    7052   2380   2060 R   5.9  0.3   0:00.03 top
    1 root      20   0   22948   2768   2120 S   0.0  0.3   0:04.85 systemd
    2 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kthreadd
    3 root      20   0       0      0      0 S   0.0  0.0   0:00.14 ksoftirqd/0
    5 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/0:+
    6 root      20   0       0      0      0 S   0.0  0.0   0:02.67 kworker/u8+
    7 root      20   0       0      0      0 S   0.0  0.0   0:01.41 rcu_sched
通常如果發現異常的程式佔用了太多的 CPU 或記憶體,最好的處理方式就是把這些程式正常關閉,如果程式當掉無法關閉的話,就可以使用 kill 或 killall 這類的指令,中止不正常程式的執行,由於上面的報表中都有每個程式的 PID,所以使用 PID 來中止特定的程式是最直接的方式。
假設我們想要中止的程式其 PID 是 1959,首先我們會用一般的方式嘗試讓程式正常結束:
kill 1959
如過執行這行之後,沒有效果的話,再嘗試強制關閉程式:
kill -9 1959
這兩種 kill 指令是最常見的處理方式。

2018年6月19日 星期二

How do I clear the terminal History?

I am using Linux Mint 17.1 Rebecca for about 2 days and accidentally typed my password into the terminal which is now displayed in the history list of commands I have previously typed.
I want to clear the terminal history completely. I have tried using the following commands in the terminal which I thought would clear the history forever but they do not:
history -c
reset
tput reset
The above commands "will" clear the history from the terminal but when I exit and bring up a new one all my previous history is still there and can all be listed again using the - history command and also by pressing the UP arrow on my keyboard. I do not want this to happen until I have totally cleared my history, then I want to continue using it.
How can I clear my terminal history completely - forever and start fresh?
Please Note: I do not want to exit the terminal without saving history just clear it forever in this one instance.

2018年6月18日 星期一

(GUI) on an Ubuntu Server

How to Install the Desktop Components (GUI) on an Ubuntu Server


If you find yourself wanting a desktop environment after you have installed Ubuntu server you can easily add it.  Pick your favorite desktop environment from the list below and run the associated command.  Remember that installation software requires root privileges so use “sudo” or switch to the root user before you begin the installation.

Unity (The Default Desktop)

sudo apt-get install ubuntu-desktop

KDE

sudo apt-get install kubuntu-desktop

LXDE (Lubuntu)

sudo apt-get install lubuntu-desktop

MATE

sudo apt-get install ubuntu-mate-desktop

Gnome

sudo apt-get install ubuntu-gnome-desktop

XFCE (Xubuntu)

sudo apt-get install xubuntu-desktop

2018年6月7日 星期四

CentOS / RHEL 7 : Change default kernel (boot with old kernel)

GRUB2 is the most common bootloader for RHEL 7 systems. A symlink to the GRUB2 config file should be present at /etc/grub2.cfg. The post describes changing the default kernel to a old kernel.

How GRUB2 selects which kernel to boot from

By default, the value for the directive GRUB_DEFAULT in the /etc/default/grub file is “saved”.
# cat /etc/default/grub
GRUB_TIMEOUT=5
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="nomodeset crashkernel=auto rd.lvm.lv=vg_os/lv_root rd.lvm.lv=vg_os/lv_swap rhgb quiet"
GRUB_DISABLE_RECOVERY="true"
This instructs GRUB 2 to load the kernel specified by the saved_entry directive in the GRUB 2 environment file, located at /boot/grub2/grubenv.
# cat /boot/grub2/grubenv 
# GRUB Environment Block
saved_entry=Red Hat Enterprise Linux Server (3.10.0-327.10.1.el7.x86_64) 7.2 (Maipo)
One can set another GRUB record to be the default, using the grub2-set-default command, which will update the GRUB 2 environment file. By default, the saved_entry value is set to the name of latest installed kernel of package type kernel. This is defined in /etc/sysconfig/kernel by the UPDATEDEFAULT and DEFAULTKERNEL directives.
# cat /etc/sysconfig/kernel
# UPDATEDEFAULT specifies if new-kernel-pkg should make
# new kernels the default
UPDATEDEFAULT=yes

# DEFAULTKERNEL specifies the default kernel package type
DEFAULTKERNEL=kernel

# MAKEDEBUG specifies if new-kernel-pkg should create non-default
# "debug" entries for new kernels.
MAKEDEBUG=yes

Change default kernel

To force a system to always use a particular menu entry, use the menu entry name as the key to the GRUB_DEFAULT directive in the /etc/default/grub file. The following command will print a list of the menu entries present in GRUB2’s configuration.
# awk -F\' /^menuentry/{print\$2} /etc/grub2.cfg
Red Hat Enterprise Linux Server (3.10.0-327.10.1.el7.x86_64) 7.2 (Maipo)                          ===> entry 0
Red Hat Enterprise Linux Server (3.10.0-229.el7.x86_64) 7.2 (Maipo)                               ===> entry 1
Red Hat Enterprise Linux Server (0-rescue-0cb6313ed65e4b36ba5daace11f3ad50) 7.2 (Maipo)           ===> entry 2
GRUB 2 supports using a numeric value as the key for the saved_entry directive to change the default order in which the kernel or operating systems are loaded. To specify which kernel should be loaded first, pass its number to the grub2-set-default command. The IDs are assigned in order the menu entries appear in the /etc/grub2.cfg file starting with 0. So the kernel 3.10.0-229.el7.x86_64 get an ID of 1.
# grub2-set-default 1
This will make 3.10.0-229.el7.x86_64 as defaul kernel which was the old kernel in the system.

Verify the new default kernel

Check the below file to see the kernel which will be loaded at next boot, crosscheck the numeric value with the menuentry in the /etc/default/grub file.
# cat /boot/grub2/grubenv |grep saved
saved_entry=1

Rebuild GRUB2

Changes to /etc/default/grub require rebuilding the grub.cfg file as follows:
# grub2-mkconfig -o /boot/grub2/grub.cfg

Reboot

Once you have verified everything and rebuilt the GRUB2 configuration file, you can go ahead an reboot the server for changes to take effect.
# shutdown -r now