Sunday, January 28, 2018

[Shell script] Find user in system


The first way:

#!/bin/bash

if [ $# -eq 0 ]
then
 echo "Usage: $0 lawrence"
else
  grep "\<$1\>" /etc/passwd > /dev/null
  RETVAL=$?
  if [ $RETVAL -eq 0 ]
  then
   echo "Username $1 was found in the system"
  else
   echo "Cannot find username $1"
 fi
fi

The second way:

[ $# -eq 0 ] &&  { echo "Usage: $0 lawrence" ; exit 1; }

grep "\<$1\>" /etc/passwd > /dev/null
RETVAL=$?

[ $RETVAL -eq 0 ] && echo "Username $1 was found in the system" || echo "Cannot find username $1"

Thursday, January 04, 2018

How can I add EPEL reop on CentOS 7.x?

It's quite simple, just two commands below:

# yum install epel-release

# yum repolist

Sample output:

Friday, September 29, 2017

How can I disable dnsmasq listen port 53 on the Ubuntu 16.04 LTS?

By default the DNS Server of Ubuntu 16.04 LTS will be 127.0.0.1:53, i.e., localhost.




Therefore, system will forward DNS query to the localhost first, and forward to the DNS server which obtained from the DHCP server afterward.

Actually, I didn't prefer this method, so that I would like to turn off  this mechanism via steps were listed below:

Steps:
1. Open /etc/NetworkManager/NetworkManager.conf

2. To disable dnsmasq


3. Restart NetworkManager


4. Examine /etc/resolv.conf


Done.

Friday, September 22, 2017

How can I switch multi-user or graphical mode via one command? (Systemd)

To switch multi-user to graphical mode (runlevel 3 to runlevel 5)

# systemctl set-default graphical && reboot




To switch graphical to multi-user mode (runlevel 5 to runlevel 3)

# systemctl set-default multi-user && reboot


Monday, September 18, 2017

How to know the system and service manage daemon of your Linux? (SysVinit or Systemd)

It's simple, just check your pid 1. (ps --pid 1)

SysVinit:


Systemd:





Thursday, September 14, 2017

How can I backup and restore MBR via dd command?

Check device name of HD:
# lsblk








Backup MBR:
# dd if=/dev/sda of=file bs=1 count=512




Note:
1. The size of MBR is 512 bytes and located in the first sector.
2. Of cause you need to save MBR backup file somewhere else.

Restore MBR:
# dd if=file of=/dev/sda bs=1 count=512


Monday, September 11, 2017

Snap commands

Here I want to list some of snap commands that I often use.

Also here is a website to understand all of snap commands:
https://snapcraft.io/docs/reference/snap-command


Log on to a snap store:
lawchiu@localhost:~$ sudo snap login xxx.xxx@gmail.comPassword of "xxx.xxx@gmail.com":
Login successful

Log out snap store:
lawchiu@localhost:~$ snap logout

Update snap:
lawchiu@localhost:~$ snap refresh
All snaps up to date
List installed snap:
lawchiu@localhost:~$ snap list
Name             Version       Rev   Developer            Notes
checkbox-snappy  2.2           859   ce-certification-qa  devmode
core             16-2.27.5     2774  canonical            core
hello            2.10          20    canonical            -
pc               16.04-0.8     9     canonical            gadget
pc-kernel        4.4.0-83.106  68    canonical            kernel
lawchiu@localhost:~$

Remove snap:
lawchiu@localhost:~$ snap remove hello
hello removed

Find packages:
lawchiu@localhost:~$ snap find 




Thursday, September 07, 2017

Install and use glances on Ubuntu

Glances is a great tools which able to monitor CPU, Memory, Disk, Network statistics... etc.

Please refer to glances website for details.

Steps:
1. Install glances via apt:
lawrencechiu@lawrencechiu-HP-Compaq-6005-Pro-SFF-PC:~$ sudo apt-get install glances


2. Executed glances:
lawrencechiu@lawrencechiu-HP-Compaq-6005-Pro-SFF-PC:~$ sudo glances

3. Then you can see CPU usage, Memory status, SWAP, Network statistics, Disk IO, Disk usage, and PID info in one screen below:














Green means OK
Blue means CAREFUL
Purple means WARNING
Red means: CRITICAL

Hotkey:
 a  Sort processes automatically          b  Bytes or bits for network I/O
 c  Sort processes by CPU%                l  Show/hide alert logs
 m  Sort processes by MEM%            w  Delete warning alerts
 p  Sort processes by name                  x  Delete warning and critical alerts
 i  Sort processes by I/O rate               1  Global CPU or per-CPU stats
 t  Sort processes by CPU times          h  Show/hide this help screen
 d  Show/hide disk I/O stats                T  View network I/O as combination
 f  Show/hide filesystem stats             u  View cumulative network I/O
 n  Show/hide network stats                F  Show filesystem free space
 s  Show/hide sensors stats                  g  Generate graphs for current history
 2  Show/hide left sidebar                    r  Reset history
 z  Enable/disable processes stats        q  Quit (Esc and Ctrl-C also work)
 e  Enable/disable top extended stats
 /  Enable/disable short processes name
 D  Enable/disable Docker stats

Enjoy~


Thursday, August 31, 2017

How can I remote ssh login to the Ubuntu 16.04 LTS via root?

By default you are unable to ssh login to the Ubuntu 16.04 LTS via root, but you can enable it step by step as following:

1. Login to the system via regular user account.

2. Set password to root account (Due to root password is empty by default)
sudo passwd root
3. To modify the configuration file of sshd_config (Marked PermitRootLogin prohibit-password, and added PermitRootLogin yes )
sudo vi /etc/ssh/sshd_config
# Authentication:
LoginGraceTime 120
#PermitRootLogin prohibit-password 
PermitRootLogin yes 
StrictModes yes
4. Restart sshd
 sudo systemctl restart ssh

Done.

Tuesday, August 29, 2017

Unable to logon to the Ubuntu Core with 2nd SSH public key?

After I added a new SSH public key into my Ubuntu One. However, I am unable to logon to the Ubuntu Core system with this new key, therefore I logon to the Ubuntu Core by original public key.

Afterward, I noticed that my system didn't sync up a new public key from Ubuntu One automatically.

Until now I have no idea if this is a defect or limitation, please advice, if any.

Workaround:
Added 2nd public key via manually into the ~/.ssh/authorized_keys


Wednesday, August 16, 2017

How to deploy Ubuntu Core from KVM to VirtualBox?

Steps:
1. Download Ubuntu Core of KVM image from URL below:
https://developer.ubuntu.com/core/get-started/kvm

2. To decompress xz.

3. To convert the format of img to the vdi via VBoxManage command below:

VBoxManage.exe convertdd ubuntu-core-16-amd64.img ubuntu-core-16-amd64.vdi
4. Import vdi file above to the VirtualBox without issue.



Wednesday, June 14, 2017

Python的額外錯誤處理

Example:
try:
    data=open('open_file.txt')
    for each_line in data:
        try:
            (var1,var2)=each_line.split(":",1)
            print(var1)
            print(var2)
        except:
            print(each_line)
    data.close()
except:

    print('File is not existing'

透過Python的try & except,程式碼果真變得很簡潔~

Monday, May 22, 2017

如何將Python程式模組化?

今天學習了如何將Python function模組化.
實際的步驟:
1. 撰寫python, 存檔為*.py
2. 撰寫setup.py for metadata (*.py跟setup.py需在同一個目錄下)
3. 執行python setup.py sdist 以建構發行套件檔案4. 執行python setup.py install 來安裝發行套件到本地端

開啟一個新檔案把module 用import的方式匯入, 透過引用正確的module名稱跟函式就可以執行了.

Tuesday, April 25, 2017

Cockpit

I saw this tools recently, and I am definitely to try it. :)

Cockpit


Thursday, April 06, 2017

Root file system is mounted as read only ?

I encountered this symptom and found out similar issue and solution is listed as below:

http://www.unix.com/linux/168716-root-filesystem-goes-readonly.html

Method:
fsck -p /dev/sdX

Thursday, August 27, 2015

TIPS for Expect Script

在此筆記一些Expect Script的使用技巧。

1. 記錄Expect Script執行的過程:
 log_file -noappend ./$0.log
.......
.......
.......
log_file
2. 與遠端主機斷線:
expect eof
3.  For loop於Expect Script的用法:
expect -c "
for {set i 1} {\$i <= 10} {incr i 1} {

}
"
4. If於Expect Script的用法:
expect -c "
  if { \$i == 1 } {

 }
5. 於Expect Script跳脫"符號的方法:
expect -c
send \"test \\\"test\\ test\\ test\\\"\r\"
"

Tuesday, August 25, 2015

如何讓rsyslog 也支援TCP?

預設rsyslog只支援UDP with port 514,如果要支援TCP,可以載入TCP的模組即可使用。

$ModLoad imudp.so
$UDPServerRun 514

$ModLoad imtcp.so
$InputTCPServerRun 514

記得修改完畢後restart rsyslog。

Monday, February 09, 2015

Use Linux be an IPv6 Router

簡單筆記一下如何使用Linux當IPv6的Router,以利將來備查。順便此次的實作是使用CentOS7.0,所以啟動daemon的方式跟以往SysV的方式不太一樣。

Step1. Enable IPv6 forwarding for Kernel via /etc/sysctl.conf:
net.ipv6.conf.all.forwarding = 1
net.ipv6.conf.default.forwarding = 1
net.ipv6.conf.br0.forwarding = 1
net.ipv6.conf.br1.forwarding = 1
net.ipv6.conf.br2.forwarding = 1

Step2. Use radvd be a DHCPv6 Server:
Step2.1 Open then edit /etc/radvd.conf:interface br0
{
AdvSendAdvert on;
MinRtrAdvInterval 30;
MaxRtrAdvInterval 100;
prefix 3001:688::/64
{
AdvOnLink on;
AdvAutonomous on;
AdvRouterAddr on;
};
};
interface br1
{
AdvSendAdvert on;
MinRtrAdvInterval 30;
MaxRtrAdvInterval 100;
prefix 3001:588::/64
{
AdvOnLink on;
AdvAutonomous on;
AdvRouterAddr on;
};
};
interface br2
{
AdvSendAdvert on;
MinRtrAdvInterval 30;
MaxRtrAdvInterval 100;
prefix 3001:288::/64
{
AdvOnLink on;
AdvAutonomous on;
AdvRouterAddr on;
};
};

Step2.2: Launch radvd immediately as well as start running it when system boots up:[root@rhel7 network-scripts]# systemctl start radvd
[root@rhel7 network-scripts]# systemctl enable radvd
[root@rhel7 network-scripts]# systemctl status radvd
radvd.service - Router advertisement daemon for IPv6
   Loaded: loaded (/usr/lib/systemd/system/radvd.service; enabled)
   Active: active (running) since 五 2015-02-06 16:22:51 CST; 1h 41min ago
 Main PID: 9884 (radvd)
   CGroup: /system.slice/radvd.service
           ├─9882 /usr/sbin/radvd -u radvd
           └─9884 /usr/sbin/radvd -u radvd
 2月 06 16:49:43 rhel7 radvd[9884]: resuming normal operation
 2月 06 16:49:44 rhel7 radvd[9884]: attempting to reread config file
 2月 06 16:49:44 rhel7 radvd[9884]: resuming normal operation
 2月 06 16:49:51 rhel7 radvd[9884]: attempting to reread config file
 2月 06 16:49:51 rhel7 radvd[9884]: resuming normal operation
 2月 06 16:49:53 rhel7 radvd[9884]: attempting to reread config file
 2月 06 16:49:53 rhel7 radvd[9884]: resuming normal operation
 2月 06 16:49:53 rhel7 radvd[9884]: attempting to reread config file
 2月 06 16:49:53 rhel7 radvd[9884]: resuming normal operation
 2月 06 18:03:32 rhel7 systemd[1]: Started Router advertisement daemon for IPv6.
Verify:
HostA(300:288::253):

eth1      Link encap:Ethernet  HWaddr 08:00:27:45:E9:40
          inet addr:10.10.2.253  Bcast:10.10.2.255  Mask:255.255.255.0
          inet6 addr: 3001:288::253/64 Scope:Global
          inet6 addr: fe80::a00:27ff:fe45:e940/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:6439011 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6261045 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:2632403693 (2.4 GiB)  TX bytes:244180721 (232.8 MiB)
[root@server1 ~]# ip -6 route show
3001:588::/64 via 3001:288::1 dev eth1  metric 1024  expires 4293343sec mtu 1500 advmss 1440 hoplimit 4294967295
3001:688::/64 via 3001:288::1 dev eth1  metric 1024  expires 4293666sec mtu 1500 advmss 1440 hoplimit 4294967295

[root@server1 ~]# ping6 3001:588::100 -c 3
PING 3001:588::100(3001:588::100) 56 data bytes
64 bytes from 3001:588::100: icmp_seq=0 ttl=63 time=31.9 ms
64 bytes from 3001:588::100: icmp_seq=1 ttl=63 time=24.3 ms
64 bytes from 3001:588::100: icmp_seq=2 ttl=63 time=5.61 ms
--- 3001:588::100 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2015ms
rtt min/avg/max/mdev = 5.615/20.607/31.904/11.046 ms, pipe 2

HostB(3001:588::100):

Wireless LAN adapter Wireless Network Connection:
   Connection-specific DNS Suffix  . : example.com   IPv6 Address. . . . . . . . . . . : 3001:588::100
   IPv6 Address. . . . . . . . . . . : 3001:588::2677:3ff:fe77:f29c
   Link-local IPv6 Address . . . . . : fe80::2677:3ff:fe77:f29c%31
   IPv4 Address. . . . . . . . . . . : 10.10.5.188
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : fe80::6a05:caff:fe22:2b8b%31
                                       3001:588::1
                                       10.10.5.254

D:\Tools\wbox-3>ping -6 3001:288::253
Pinging 3001:288::253 with 32 bytes of data:
Reply from 3001:288::253: time=29ms
Reply from 3001:288::253: time=64ms
Reply from 3001:288::253: time=158ms
Reply from 3001:288::253: time=24ms
Ping statistics for 3001:288::253:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 24ms, Maximum = 158ms, Average = 68ms

Friday, August 22, 2014

如何避免remote syslog寫入到本機的/var/log/messages?

如題,假設今天log receive server將收集幾十台甚至幾百台裝置的log,預設上rsyslog會將這些裝置的log也同時寫入到本機的/var/log/messages。這是個非常令人感到困擾的一件事,因為本機的log將會被洗版.... 還好這是有方法可以解決的,以下用rsyslog舉個例子:

:fromhost-ip,isequal,"192.168.1.1"                        /var/log/remote.log
:fromhost-ip,isequal,"192.168.1.2"                        /var/log/remote.log
:fromhost-ip,isequal,"192.168.1.3"                        /var/log/remote.log
........
*.info;mail.none;authpriv.none;cron.none               /var/log/messages


192.168.1.1~3的log會寫入到/var/log/remote.log,但同時也會寫入
到/var/log/messages!


解決方法:

:fromhost-ip,isequal,"192.168.1.1"                        /var/log/remote.log
           & ~
:fromhost-ip,isequal,"192.168.1.2"                        /var/log/remote.log
           & ~
:fromhost-ip,isequal,"192.168.1.3"                        /var/log/remote.log
          & ~
........
*.info;mail.none;authpriv.none;cron.none               /var/log/messages

"& ~"能通知rsyslog,當把remote device log寫入到/var/log/remote.log後,就停止接下來的動作,那麼當然remote device log就不會寫入到/var/log/messages囉!

Wednesday, July 30, 2014

My PS1 in ~/.bashrc

export PS1="\[\e[00;30m\]\u@\h:\w\\$ \[\e[0m\]"

user@hostname:directory $or#

->

root@rhel7:/lib/systemd/system# 

[lawrence@rhel7 tmp]$