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

The concept of directory structure on macOS

This article is aimed to introduce directories hierarchy for macOS. macOS is also followed by Filesystem Hierarchy Standard (FHS). But it has a little difference, that this article collects for you. The content of directories in macOS is divided to two part, standard directories and hidden directories.

Standard Directories

  • /Applications : Application’s contents
    • This directory is where you install apps intended for use by all users of a computer.
  • /Library : Shared libraries, files
    • Apps should use the Library directory to store app-specific (or system-specific) resources.
  • /Network : Network related files and libraries.
    • It contains the list of computers in the local area network.
  • /System : System related files, libraries, and preference.
    • This directory contains the system resource required by macOS to run. These resources are not modified by recommend.
  • /Users : User home directory
    • All of files involves Individual users are placed in there.
  • /Volumes : Mount point for removable media
    • This directory contains subdirectories which are used as mount point for removable media.
    • E.g. cd-rom, floppy disk, dmg mounts etc.


    Hidden Directories
    This directories are followed FHS on Linux. You can also reference “The concept of directory structure on Linux”.

  • /bin : Essential user command binaries
    • All the executable binary programs.
  • /dev : Devices files
    • It contains hardware device files.
  • /etc : Host-specific system configuration
    • It contains all configuration for system.
  • /sbin : System binaries
    • The essential binaries which only can be executed by system.
  • /tmp : Temporary files
    • The temporary files created by apps and system.
  • /usr : Secondary hierarchy
    • It contains nonessential binaries, header files and other data.
  • /var : Variable data
    • Intuitively, it contains log and other file content is variable.


    Reference: macOS File System

    Importing Swift file to Objective-C project

    I think it is very complex when I read Apple's reference. Hence, I decided to write a simple article which illustrates you how to import Swift file into Objective-C project step by step.

    First, you should create an Objective-C project.

    After project be created. We have some parameters should be set in Build Settings.

  • Find “Defines Module”, and set to “YES”. This setting let Xcode use same framework to import Swift to Objective-C project.



  • Find “Always Embed Swift Standard Libraries”, and set to “YES”.



  • The “Product Module Name” names by your project name. In there, we do not need to revise anything.


  • New or add exist Swift file into the project. Then Xcode pop-ups a dialog, follows image. We just click “Create Bridging Header” to automatically crate bridging header. After that, we can see the bridging header file which be names your product name followed by adding "-Bridging-Header.h".


    Finally, we need to import “MixDemo-Swift.h” to any Object-C .m. That header is created by Xcode, but you cannot see in the project folder. The file names your Product Module Name, which we mentioned above, followed by add “-Swift.h”. You can also search "Objective-C Generated Interface Header Name" under Build Settings.
    #import "MixDemo-Swift.h"
    

    Then, we can use swift object in the Objective-C project directly.
        BarcodeScannerViewController *barcodeViewController = [[BarcodeScannerViewController alloc] init];
        barcodeViewController.delegate = self;
        [self presentViewController:barcodeViewController animated:YES completion:nil];
    

    Reference: https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

    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