Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

My Mac environment



I know that every software engineer has a development environment that he is favorited. This article records all my Mac environment in order to set up faster when I change the computer in the future.

Basic on the Mac

Home-brew
Every Mac lover may know this package management tool. This is my first installation when I begin to use a new Mac.
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Wget
This is a powerful open-source software which I recommend you to install it. When you are a terminal passionate, you cannot miss this tool. I almost use it to download digital contents.
$ brew install wget
Nmap
As a computer security researcher, I always do something over the network. Hence, I need it. hmm…
$ brew install nmap

Building Development Environment

Xcode
Git
$ brew install git
Well, sometimes view the git graph in the terminal is very ugly. So I recommend you to use some GUI tool to assistant you. Such as SouceTree is a great visual tool. But now, I try Fork, which is light and has a simple view.
Package management in the Xcode project
As an iOS developer, I used a lot of third-party libraries. There is two most popular management tool you can choose. But it almost depends on your team’s choice.
CocoaPods
$ gem install cocoapods -v 1.7.1
$ pod setup
Carthage
$ brew install carthage

Must have tools

Browser
Safari is my main browser, but I will switch to Chrome for certain development requirements.
Chat software
Most startups choose Slack as their communication tool. However, many Taiwanese companies are still using Skype. I used Telegram, messages are encrypted, when I was in a gambling company. And I use Line with my friends.
Editor
Currently, I prefer to use Visual Studio Code which is made by Microsoft. You can install many extensions to enhance its features. I used to Sublime Text 2 which is similar but it is not free.

Online tools

Some simple requirement you can use the online tool to solve it.
JSON Editor Online
It is a web-based tool to view, edit, format, and validate JSON
WTF Auto layout?
UI bugs are not easy to understand on the Xcode’s console. This tool can convert constraint error log to the easy way.
Swagger Editor
I very recommend your team to edit the API document via Swagger. It has a clear interface to understand each API action and It can post API just like Postman. I think it is powerful than Postman.
Finally, I know there are many useful tools so that you have a lot of installation steps. In order to reduce installation steps, I write a shell script (mac_setup.sh) to automatically install all tools which I mentioned above. Please feel free to use it and edit it to fit your demands. You can also leave your comments to introduce other awesome tools which I’m not used.

Install NFS server on CentOS

Network FileSystem (NFS) is created by Sun company. It provide you a service to share files between different machine via network. It is similar with Samba which I mentioned before. But the transport speed is faster than Samba. Because it does not need user to type account and password. The server has white list to allow legal IP to login to access files directly.

Install server is very easy. We just use yum command to find necessary libraries and install automatically.
[nick1811@NAS ~]$ sudo yum install nfs-utils nfs-utils-lib
Create white list. We allows all IP in subnetwork(192.168.0.*). And all users have access right for read and write.
[nick1811@NAS ~]$ vim /etc/exports
/home/nick1811/Shared_Folder 192.168.0.1/24(rw)
Run several startup scripts for the NFS server
[nick1811@NAS ~]$ sudo service rpcbind start
[nick1811@NAS ~]$ sudo service nfs start
Finally, remember turn on the port in iptables.
[nick1811@NAS ~]$ sudo vim /etc/sysconfig/iptables
# NFS
-A INPUT -m state --state NEW -m tcp -p tcp --dport 2049 -j ACCEPT
[nick1811@NAS ~]$ sudo service iptables restart
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                               [  OK  ]
iptables: Applying firewall rules:       


Although we have been set complete. But how to check all setting are correctly? To reach this purpose. I choose other CentOS and install NFS client to do it.

First, we also need to install necessary libraries.
[nick1811@Client ~]$ sudo yum install nfs-utils nfs-utils-lib
Second, we use showmount command to check we can see the shared folder.
[nick1811@Client ~]$ showmount -e 192.168.0.1
Export list for 192.168.0.1:
/home/nick1811/Shared_Folder  192.168.0.1/24
Finally, we mount shared folder to local folder which is we created.
[nick1811@Client ~]$ mount 192.168.0.1:/home/nick1811/Shared_Folder /mnt/nfs
When we can put files to /mnt/nfs and we can see files on the server side, we successfully setup NFS server.

Reference: http://linux.vbird.org/linux_server/0330nfs.php

How to install Samba on CentOS ?

Is this article teach you do basic samba? Oh~ no. This article introduces the server of samba. This server can help you to share files between Linux and Windows. The difference with FTP server is you can revise file directly on your PC. So let me show you how to install Samba server on Cent OS.

  • Install samba
  • [nick1811@centos-6 ~]$ sudo yum install -y samba
    
  • To allow folder access
  • [nick1811@centos-6 ~]$ sudo vim /etc/selinux/config
    
    # This file controls the state of SELinux on the system.
    # SELINUX= can take one of these three values:
    #     enforcing - SELinux security policy is enforced.
    #     permissive - SELinux prints warnings instead of enforcing.
    #     disabled - No SELinux policy is loaded.
    SELINUX=disabled
    
  • To add samba user (Hint: Only Linux users can be added to samba user)
  • [nick1811@CentOS-6 ~]$ sudo pdbedit -a -u nick1811
    new password:
    retype new password:
    
  • Add user with password
  • [nick1811@CentOS-6 ~]$ sudo printf "********\n********\n" | sudo pdbedit -t -a -u nick1811
    Unix username:        nick1811
    
  • To delete samba user
  • [nick1811@centos-6 ~]$ sudo pdbedit -x -u nick1811
    
  • To check samba users
  • [nick1811@CentOS-6 ~]$ sudo pdbedit -L
    nick1811:500:nick1811
    
  • To enable samba on starting OS
  • [nick1811@centos-6 ~]$ sudo chkconfig smb on
    
  • Settings for creating shares by non-root users
    • create samba shares folder
    • [nick1811@CentOS-6 ~]$ sudo mkdir -p /usr/local/samba/lib/usershares
      [nick1811@CentOS-6 ~]$ sudo chown root:nick1811 /usr/local/samba/lib/usershares
      [nick1811@CentOS-6 ~]$ sudo chmod 1770 /usr/local/samba/lib/usershares
      [nick1811@CentOS-6 ~]$ sudo chcon -R -t samba_share_t /usr/local/samba/lib/usershares
      
    • set up your smb.conf by adding to the [global] section
    • [nick1811@CentOS-6 ~]$ sudo vim /etc/samba/smb.conf
      
      #======================= Global Settings =====================================
      
      [global]
      wins support = yes
      usershare path = /usr/local/samba/lib/usershares
      usershare max shares = 100
      usershare allow guests = true
      usershare owner only = false
      # Samba works on NFS-Shares
      admin users = nick1811
      strict locking = no
      oplocks = no
      acl check permissions = false
      
      
      
  • Now you can create folder shares
    • To share samba folder
    • [nick1811@centos-6 ~]$ net usershare add nick_samba /home/nick1811/samba/ "nick's samba folder" Everyone:F guest_ok=y
      
    • To unshare samba folder
    • [nick1811@centos-6 ~]$ net usershare delete nick_samba
      
  • To enable/disable samba service
  • [nick1811@centos-6 ~]$ sudo service smb start
    
    [nick1811@centos-6 ~]$ sudo service smb stop
    
    [nick1811@centos-6 ~]$ sudo service smb restart
    
  • Finally, remember to set iptables for samba
  • [nick1811@CentOS-6 ~]$ sudo vim /etc/sysconfig/iptables
    
    # SAMBA/CIFS
    -A INPUT -m state --state NEW -m tcp -p tcp --dport 139 -j ACCEPT
    -A INPUT -m state --state NEW -m tcp -p tcp --dport 445 -j ACCEPT
    
    [nick1811@CentOS-6 ~]$ sudo service iptables restart
    iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
    iptables: Flushing firewall rules:                         [  OK  ]
    iptables: Unloading modules:                               [  OK  ]
    iptables: Applying firewall rules:                         [  OK  ]
    
    

References: 

Change the login message

To know which server is own login, we should show welcome message before login. This article illustrates how to set display messages before/after login on the banner.

Before Login
To show welcome message before login, you should edit ssh configure file, /etc/ssh/sshd_cofig. Add banner message which is follows. We add a banner text file to /etc/profile.d, and specify this file path in the ssh configure file. The path, /etc/profile.d, stores all files that are executed when the user login.
[root@centos-6 ~]# vim /etc/ssh/sshd_config
# no default banner path
Banner /etc/profile.d/banner.txt

The file, banner.txt, can be whatever file you want and store in anywhere. However, we store it in the /etc/profile.d. Then edit the file and put in whatever welcome message you want.
,--.   ,--.       ,--.                              
|  |   |  | ,---. |  | ,---. ,---. ,--,--,--. ,---. 
|  |.'.|  || .-. :|  || .--'| .-. ||        || .-. :
|   ,'.   |\   --.|  |\ `--.' '-' '|  |  |  |\   --.
'--'   '--' `----'`--' `---' `---' `--`--`--' `----'

After all settings, you need to restart sshd before your changes take effect.
[root@centos-6 ~]# service sshd restart
Stopping sshd:                                             [  OK  ]
Starting sshd:                                             [  OK  ]

When you connect to the server, the welcome message will show in the terminal.
newmac:~ user$ ssh nick1811@192.168.0.90
,--.   ,--.       ,--.                            
|  |   |  | ,---. |  | ,---. ,---. ,--,--,--. ,---.
|  |.'.|  || .-. :|  || .--'| .-. ||        || .-. :
|   ,'.   |\   --.|  |\ `--.' '-' '|  |  |  |\   --.
'--'   '--' `----'`--' `---' `---' `--`--`--' `----'
nick1811@192.168.0.90's password:


After Login
In this part, I will show you how to set display message after login. To change the display message, you should edit /etc/motd. But in this article, we do not revise /etc/motd. We add a file, motd.sh, into the path, /etc/profile.d. All of files in this path will be automatically executed by /etc/profile when the user login.
[root@centos-6 ~]# vim /etc/profile.d/motd.sh
#!/bin/bash
echo -e "
 ######################
 # Welcome to Cent OS #
 ######################
"

So you can see the result after this setting.
Last login: Tue May 17 11:27:49 2016 from 192.168.1.138

 ######################
 # Welcome to Cent OS #
 ######################

[nick1811@centos-6 ~]$

Searching log for shutdown/reboot on Linux

To address problem, sometimes, we need to find log to know machine's shutdown/reboot time. However, how to do it?
last command
We use command, man last, to view the illustration(link). It lists login account, but reboot the machines use a pseudo account. Hence, we use the command, last reboot , to show the reboot time. For an instance, it shows last reboot time at 5/12 13:04.
[root@centos-6 ~]# last reboot
reboot   system boot  2.6.32-431.el6.x Thu May 12 13:04 - 12:29 (3+23:25)   
reboot   system boot  2.6.32-431.el6.x Mon May  9 09:55 - 13:01 (3+03:05)   
reboot   system boot  2.6.32-431.el6.x Thu Apr 14 18:20 - 09:52 (24+15:31)  
reboot   system boot  2.6.32-431.el6.x Thu Apr 14 15:00 - 18:19  (03:19)    
reboot   system boot  2.6.32-431.el6.x Thu Apr 14 14:42 - 14:59  (00:16)    
reboot   system boot  2.6.32-431.el6.x Thu Apr 14 14:31 - 14:42  (00:10)    
reboot   system boot  2.6.32-431.el6.x Thu Apr 14 14:27 - 14:30  (00:03)    
reboot   system boot  2.6.32-431.el6.x Thu Apr 14 14:20 - 14:26  (00:05)     
reboot   system boot  2.6.32-431.el6.x Tue Jan 26 14:12 - 14:28  (00:15)    
reboot   system boot  2.6.32-431.el6.x Tue Jan 26 13:47 - 14:11  (00:24)    
reboot   system boot  2.6.32-431.el6.x Tue Jan 26 13:35 - 13:46  (00:10)    
reboot   system boot  2.6.32-431.el6.x Tue Jan 26 11:50 - 13:35  (01:45)    
reboot   system boot  2.6.32-431.el6.x Tue Jan 26 11:18 - 11:49  (00:31)    
reboot   system boot  2.6.32-431.el6.x Tue Jan 26 11:10 - 11:18  (00:07)    
reboot   system boot  2.6.32-431.el6.x Tue Jan 26 10:48 - 11:10  (00:21)    
reboot   system boot  2.6.32-431.el6.x Tue Jan 26 10:10 - 10:48  (00:37)    

wtmp begins Tue Jan 26 09:57:17 2016

Then if you want to list logs of shutdown. Using the command, last -x shutdown, which is follows.
[root@centos-6 ~]# last -x shutdown
shutdown system down  2.6.32-431.el6.x Thu May 12 13:01 - 13:04  (00:02)    
shutdown system down  2.6.32-431.el6.x Mon May  9 09:52 - 09:55  (00:03)    
shutdown system down  2.6.32-431.el6.x Thu Apr 14 18:19 - 18:20  (00:00)    
shutdown system down  2.6.32-431.el6.x Thu Apr 14 14:59 - 15:00  (00:00)    
shutdown system down  2.6.32-431.el6.x Thu Apr 14 14:42 - 14:42  (00:00)    
shutdown system down  2.6.32-431.el6.x Thu Apr 14 14:31 - 14:31  (00:00)    
shutdown system down  2.6.32-431.el6.x Thu Apr 14 14:26 - 14:27  (00:00)    
shutdown system down  2.6.32-431.el6.x Tue Jan 26 13:46 - 13:47  (00:00)    
shutdown system down  2.6.32-431.el6.x Tue Jan 26 13:35 - 13:35  (00:00)    
shutdown system down  2.6.32-431.el6.x Tue Jan 26 11:49 - 11:50  (00:00)    
shutdown system down  2.6.32-431.el6.x Tue Jan 26 11:18 - 11:18  (00:00)    
shutdown system down  2.6.32-431.el6.x Tue Jan 26 11:10 - 11:10  (00:00)    
shutdown system down  2.6.32-431.el6.x Tue Jan 26 10:48 - 10:48  (00:00)    
shutdown system down  2.6.32-431.el6.x Tue Jan 26 10:09 - 10:10  (00:00) 

wtmp begins Tue Jan 26 09:57:17 2016

How to make a schedule to clean logs on Linux ?

In the software developing, we may make a lot of logs and stored on our developed systems. However, logs will increase by the time. To address increasable logs, we should make a schedule to clean it. In Linux system, we have a simple way to do it. This article will introduce two commands which are installed on Linux. First is crontab, that is a command which service for you to make a schedule to do something. Second, tmpwatch is a command which cleans tmp directory. This article illustrates how to use those two commands to achieve our work.

crontab
This command makes the work cycle to do it. The cycler time use minute, hour, week, month, and year. You can use crontab command to archive your work, and also edit /etc/crontab to do it. To security issues, /etc/cron.allow use to allow who can use this command. Vice versa, /etc/cron.deny use to deny who cannot use it.

The crontab content
[nick1811@centos-6 ~]$ cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed



tmpwatch
Temporary files are almost placed in /tmp directory, and be deleted by the system. To assurance the /tmp directory is not full, the system automatically cleans it in each day. This because the system makes a schedule to execute tmpwatch to do it. This package is not installed in minimum installation.

Using yum to install package:
[root@centos-6 ~]# yum install tmpwatch.x86_64

After installing, the tmpwatch file will be placed on this location( /etc/cron.daily). We can use cat to view it. All files in the /tmp will be deleted when it be not accessed in 30 days. This file shows the system recursively detects /tmp directory and deletes files.
[nick1811@centos-6 ~]$ cat /etc/cron.daily/tmpwatch 
#! /bin/sh
flags=-umc
/usr/sbin/tmpwatch "$flags" -x /tmp/.X11-unix -x /tmp/.XIM-unix \
 -x /tmp/.font-unix -x /tmp/.ICE-unix -x /tmp/.Test-unix \
 -X '/tmp/hsperfdata_*' 10d /tmp
/usr/sbin/tmpwatch "$flags" 30d /var/tmp
for d in /var/{cache/man,catman}/{cat?,X11R6/cat?,local/cat?}; do
    if [ -d "$d" ]; then
 /usr/sbin/tmpwatch "$flags" -f 30d "$d"
    fi
done



How to make a schedule to clean logs?
Now, we can make our schedule to clean logs. For an instance, we clean a directory at 1:30 in every day, and delete files which be not accessed in a month.
[nick1811@centos-6 ~]$ crontab -e
# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
 30  1  *  *  * /usr/sbin/tmpwatch -maf 30d /home/nick1811/logs

The concept of directory structure on Linux

This article is aimed to provide directory structure information on Linux Operation System. All of the directories are following the Filesystems Hierarchy Standard (FHS). Directories are specified purpose to store files.

  • /bin : Essential user command binaries (for use all users)
    • All the executable binary programs that can be executed by all users .
    • E.g. cat, chmod, chown, date, mv, mkdir, cp, bash, etc.
  • /boot : Static files of the boot loader
    • It contains every this required for the booting system.
  • /dev : Device files
    • It is the location of hardware device files.
  • /etc : Host-specific system configuration
    • It contains all configuration files for system and Applications. In general, all files that can be viewed by all users. But only root can revise it. It must be static and cannot be an executable binary.
  • /home : User home directories
    • Home directory of users. Therefore, no program should rely on this location. The space of each user directory is limited by the system.
  • /lib : Essential shared libraries and kernel modules
    • This directory contains shared library image which is needed to boot the system.
  • /media : Mount point for removable media
    • This directory contains subdirectories which are used as mount point for removable media.
    • E.g. cd-rom, floppy disk, etc.
  • /mnt : Mount point for a temporarily mounted filesystem
    • It provide you mount temporarily filesystem as needed.
  • /opt : Add-on application software packages
    • Optional is abbreviated as opt. This directory is reserved for the installation of add-on application software packages.
  • /root : Home directory for the root user
    • Super user’s home directory.
  • /sbin : System binaries
    • Executable binary programs that can be executed  only by root user.
  • /srv : Data for services provided by this system
    • Service is abbreviated as srv. It contains site-specific data which are serviced by a server.
    • E.g. WWW, FTP, etc.
  • /tmp : Temporary files
    • Temporary files are written by the program. It can also be stored by the user. But the important files cannot be stored in there. It has a daemon service of tmpwatch, which cleans files by a period time.
  • /usr : Secondary hierarchy
    • Unix Shared Resource is abbreviated as usr. It is the second major section of the filesystem, that contains software packages. /usr is shareable, read-only data.
  • /var : Variable data
    • This includes spool directories and files, administrative and logging data, and transient and temporary files.
  • /proc : Kernel and process information virtual filesystem
    • A virtual filesystem which contains information about running process.
    • E.g. cpuinfo, dma, interrupts, ioports, net, etc. 


Reference : http://www.pathname.com/fhs/ Filesystem Hierarchy Standard (FHS)