I am a big fan of linux. I enjoy ssh-ing into servers and using the command line, but since I don’t necessarily use linux every day, I sometimes forget some of the commands, so I keep this reference for myself.

Linux commands and other command line tools

Users and Groups

  • getent passwd shows a list of all the users on the server.
  • sudo passwd <username> allows a sudoer to change the password of a user.
  • groups shows a list of all the groups in use.
  • groups <username> shows which groups <username> is a member of.
  • sudo /usr/sbin/usermod -aG <groupname> <username> adds <username> to group <username> (/usr/sbin/ only necessary if usermod command is not found).
  • id -Gn shows which groups you belong to.

Files

  • df -ah shows the amount of Disk Free space on your drive, All file systems, Human readable.
  • du -sh <folder> shows the amount of Disk space Used by a folder.
  • free shows the amount of free memory on your machine.
  • ls lists the files in the current folder. ls -l is helpful because it shows the owner of each folder and file permissions. Permission denied problems can be very confusing to fix in linux. ls -alg is very comprehensive and shows the list of all files as a list and groups the directories first. The first column shows something like drwxrwxrwx where the first character indicates if it is a directory (d) or a file (-) and the next three sets of rwx specify the permissions for the owner, group, and world. Please read man ls for more info.
  • chmod 777 test_file.txt will change the permissions for the test_file.txt file to the most permissive. It is tempting to do this to solve permission errors, but is a bad idea for security reasons. This chart will show all the possible values:
Octal Bits Permissions
0 000
1 001 –x
2 010 -w-
3 011 -wx
4 100 r–
5 101 r-x
6 110 rw-
7 111 rwx
  • mkdir new_folder_name is used to make a directory.
  • pwd Prints the current Working Directory.
  • rmdir folder_name is used to remove a directory, recursively deleteing the files in the folder. If you get an error that the folder is not empty, you can add the -r option after ‘rmdir’, but make sure you don’t want the files because there is no recycle bin in linux.
  • rm -r <mydir> will delete a folder even if it is not empty.
  • rm -rf <mydir> will delete a folder even if it is not empty without any prompts.
  • find . -name <some file or directory> will search the working directory for the specified file or directory.
  • grep -rn "some text" will seach for any file containing “some text”. The `-rn`` flags specify recursive search of subdirectories and to show the line numbers of the text found.

Other

  • history shows a list of previous commands. What’s nice is that you can re-run any command by using the bang operator and the line number (i.e. !5). You can clear the history using history -c.
  • ps aux | grep <program name> to get a list of the Process State and ids (pid) associated with a running program.
  • lscpu shows CPU information.
  • uname -a shows OS information
  • ifconfig and ip addr show will give you the IP address of your server.
  • netstat -tulpn shows the TCP and UDP Listening Port Number. TIP: run as root to see the PID/Program name.
  • top or htop will show you how much memory the TOP processes are using.
  • mount is used to mount a new file system temporarily and /etc/fstab is where you can mount file systems at boot.
  • sudo reboot now will reboot the server immediately.

Getting help with Linux commands

  • man <command> will show you the help files with examples.

Run commands automatically at login

sudo nano ~/.bash_profile will open a file where you can add commands that you want to run automatically at login, or aliases. After you edit the file, use source ~/.bash_profile to reload it into memory. A shortcut for source is . ~/.bash_profile, or maybe even . !$ if it was the last file you edited.

Permissions

If you even get an error that you cannot connect to your home drive, say /home/bosr/, try this as root to fix it (don’t ask me how I know this).

chmod -R 755 /home/bosr

Services

Using the newer systemd method, you can stop, start, or restart a service with systemctl, using NGINX as an example:

systemctl stop nginx

systemctl start nginx

systemctl restart nginx

Git Commands / Notes

Initialization

git init is used to start a new repository. git status shows the current branches and HEAD. git config --global --list shows all the commands in the config file.

Commits

git add . # stage all files in current directory (not in top level directory) git reset # unstage all of the changes git commit -m “msg” # commit changes with a message git log # see a list of previous commits and their hash git checkout “branch-name” # checkout a branch to work on git checkout – . # git reset –soft # reset the HEAD to hash git reset # reset the HEAD to hash git reset –hard # reset the HEAD to hash and remove all later commits git diff # show changes git clean # removes all unmonitored files

Add

git add -A # stage all files - both current directory and top level (default, new to version 2) git add –no-all # stages all files except deleted ones git add -u # stages deleted and modified files, but not unmodified files git add * # do not use this command - ’*’ is a shell command, not a git command

Branches

git branch # view branches and see current one git checkout -b “new branch name” # add a branch git merge # merge the branch into the current branch git branch -D # delete a branch

Stash

git stash save “msg” # saves changes git stash list # shows all the stashs that have been saved git stash apply # apply the changes in the stash to the current branch (does not drop stash) git stash pop # grabs the first stash and applies the changes, and drops the stash git stash drop stash() # removes a stash from the list git stash clear # removes all stashes (be careful doing this)

Remotes

git remote add origin “https://github.com/rogerjbos/.git” # define origin git push -u origin master # push local code to remote repo git push origin my_new_branch # push new branch to the remove repo git pull origin master # get new version of code onto local computer


### Misc Notes

#### R command line commands for CentOS server

sudo R –vanilla yum update R



#### Create Symbolic (soft) link
My use case is that I cannot write to my linux drive from Windows (though I can write it my Windows drive from linux), so I write to a windows drive and create a symbolic link on my linux server so I can serve the file via the web server.

ln -s file1 link1 ln -s //media/research/R_HOME/linux/reports/LC.html //data/shiny/doc/LC.html ln -s //media/research/R_HOME/linux/reports/SC_SMC.html //data/shiny/doc/SC_SMC.html ln -s //media/research/R_HOME/linux/reports/SCG.html //data/shiny/doc/SCG.html

#### To verify new soft link run:

ls -l


#### Linux & Flask/yagmail/cifs

apt-get install cifs-utils apt-get install python-dev apt-get install python-twisted apt-get install python-pip pip install setuptools pip install requests pip install keyrings.alt pip install yagmail pip install Flask pip install flask_login dpkg-reconfigure tzdata


#### Map network drives using /etc/fstab entry

//192.168.1.1/bosdrive /mnt/bosdrive cifs rw,guest,iocharset=utf8,file_mode=0777,dir_mode=0777,noperm,users 0 0


#### Create a service for HoneyAlarmServer in a file called /etc/init.d/HoneyAlarmServer

#!/bin/sh # # /etc/init.d/HoneyAlarmServer

RETVAL=0 prog=“Honey”

start() { echo -n $"Starting \(prog:" RETVAL=\)? [ “$RETVAL” = 0 ] && touch /var/lock/subsys/$prog cd /home/rjbos/HoneyAlarmServer sudo python /home/rjbos/HoneyAlarmServer/alarmserver.py& echo }

stop() { echo -n $“Stopping $prog:” killproc \(prog -TERM RETVAL=\)? [ “$RETVAL” = 0 ] && rm -f /var/lock/subsys/$prog echo }

reload() { echo -n $“Reloading $prog:” killproc \(prog -HUP RETVAL=\)? echo }

case "\(1" in start) start ;; stop) stop ;; restart) stop start ;; reload) reload ;; condrestart) if [ -f /var/lock/subsys/\)prog ] ; then stop # avoid race sleep 3 start fi ;; status) status \(prog RETVAL=\)? ;; *) echo $“Usage: $0 {start|stop|restart|reload|condrestart|status}” RETVAL=1 esac exit $RETVAL



#### Configuring CUPS for network printing

http://localhost:631/ Administrator > Add Printer select Internet Printing Protocol something like socket://192.168.1.3:9100 Select PPD File: For Brother 2230 select 2170


#### Common Apache2 Tasks

Restating Apache2: sudo service apache2 restart Viewing the error log in Apache2: tail -40 /var/log/apache2/error.log


#### Other Packages Needed

sudo apt-get install r-cran-rodbc sudo apt-get install r-cran-xml # needed for devtools package sudo apt-get install libcurl4-gnutls-dev sudo apt-get install texinfo


### R configurations

#### /etc/R/Rprofile

Get your current repo name

current_repo <- getOption(“repos”) current_repo[“CRAN”] <- “http://lib.stat.cmu.edu/R/CRAN/” options(repos = current_repo) roger_env <- new.env() # If you don’t want to clutter this file, leave functions elsewhere. sys.source(“.my_custom_functions.r”, envir = roger_env) attach(roger_env)


Handy: Configure R to use more than one core when compile source code.

#### ~/.Renviron or /usr/lib64/R/etc/Renviron.site

MAKEFLAGS=-j4



#### Install Kodi to the Fire TV

adb kill-server adb start-server adb connect adb is connected when it reports the message “connected to :


### New Install

adb install Upgrade adb install -r ```

Installation is complete when it reports the message “success” (Note: For Android you need to type in the full path. e.g. >adb install /sdcard/Download/apk-file-name.apk)