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
useradd mynameadd a new user with the name myname.
userdel ubuntudelete the username ubuntu. For example, after creating your own username, you may want to remove the default ubuntu username to make the server more secure.
getent passwdshows a list of all the users on the server.
sudo passwd <username>allows a sudoer to change the password of a user.
groupsshows 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
/usr/sbin/only necessary if
usermodcommand is not found).
id -Gnshows which groups you belong to.
pwdPrints the current Working Directory.
cd <path/to/new/dir>Change Directory to specified path.
cd ..moves up one level to the parent directory.
lslists the files in the current folder.
ls ..can be used to list the files in the parent directory.
ls -lis helpful because it shows the owner of each folder and file permissions. Permission denied problems can be very confusing to fix in Linux.
ls -algis very comprehensive and shows the list of all files as a list and groups the directories first. The first column shows something like
drwxrwxrwxwhere the first character indicates if it is a directory (
d) or a file (
-) and the next three sets of
rwxspecify the permissions for the owner, group, and world. Please read
man lsfor more info.
chmod 777 test_file.txtwill 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:
mkdir new_folder_nameis used to MaKe a DIRectory.
rmdir folder_nameis used to ReMove a DIRectory, recursively deleting the files in the folder. If you get an error that the folder is not empty, you can add the
-roption 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 search for any file containing “some text”. The `-rn`` flags specify recursive search of subdirectories and to show the line numbers of the text found.
df -ahshows 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.
freeshows the amount of free memory on your machine.
Zipping and Tarring
gzip <filename>compresses a file using gzip and adds the extension .gz (only works on files, not folders).
gzip -d <filename.gz>decompresses a gzipped file.
tar cf <tarred name.tar> *.txtwould tar all the .txt files in the working directory into a tarred file.
tar xf <tarred name.tar>would decompress the .tar file.
tar czf <tarred name.tar.gz> *.txtwould tar and gzip and all the .txt files in the working directory into a tar.gz file.
tar xzf <tarred name.tar.gz>would decompress the .tar.gz file.
Formatting & Mounting
ls -l /dev/disk/by-uuid/will show all the disks with their respective unique id. A usb drive will usually show up as /dev/sda1.
sudo umountwill unmount it (if it is already mounted)
sudo mkfs.ext4 /dev/sda1will format the usb drive at
mkfs.ext4is for linux format and
mkfs.vfatwould be for windows format.
Other / Misc
historyshows 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
timedatectl set-timezone America/New_Yorksets the timezone to New York.
ps aux | grep <program name>to get a list of the Process State and ids (pid) for All User eXecutables.
kill -9 <pid>kills (immediately) the process identified by . You can use
ps auxto identify the pid you want to kill. Killing a process immediately should not need to be done very often and should be used sparingly.
lscpuLiSts CPU information.
uname -ashows OS information, All information. Without
-ayou don’t get very much information.
ip addr showwill give you the IP address of your server. eth0 is the main interface you will be interested in.
netstat -tulpnshows the TCP and UDP Listening Port Number. TIP: run as root to see the PID/Program name.
htopwill show you how much memory the TOP processes are using.
mountis used to mount a new file system temporarily and
/etc/fstabis where you can mount file systems at boot.
sudo reboot nowwill reboot the server immediately.
Getting help with Linux commands
man <command>will show you the help files with examples.
Run commands automatically at log in
sudo nano ~/.bash_profile will open a file where you can add commands that you want to run automatically at log in, 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.
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
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
Example of creating a service. Create the following file in
[Unit] Description=Job that runs the geth (Go Ethereum) node as a service [Service] StandardOutput=syslog StandardError=syslog User=root ExecStart=nohup geth -mine --etherbase "0x2395114cdEb90542e5915A838f18108E2dcF8e4e" --cache 64 --maxpeers 12 Restart=always [Install] WantedBy=multi-user.target
You will then need to run this command to load the new service.
Then you can run any of these commands.
systemctl stop geth
systemctl start geth
systemctl restart geth
systemctl status geth
Finally you will want to run this command so the service will start automatically after every reboot:
systemctl enable geth
To see a list of all the installed services you can run the following command:
systemctl --all -t service
Once you have the geth serve set up and the geth service created and enabled, you can reboot your linux server and check to see that the geth service started and you are able to connect to the Java console as follows:
geth attach ipc:/media/usb/geth.ipc
Check your balance from within the geth console:
List all accounts:
List all peers:
List the latest synced block:
Using a USB flash drive in linux
sudo mkdir /media/usb
sudo chown -R ubuntu:ubuntu /media/usb
sudo mount /dev/sda1 /media/usb -o uid=ubuntu,gid=ubuntu
Git Commands and Notes
git initis used to start a new repository.
git statusshows the current branches and HEAD.
git config --global --listshows all the commands in the config file.
git add .stages all files in current directory (not in top level directory).
git resetunstage all of the changes without committing them.
git commit -m "msg"commit changes with a message.
git logshows a list of previous commits and their hash.
git checkout "branch-name"is used to checkout a branch to work on.
git checkout -- .
git reset --soft <hash>reset the HEAD to the commit indicated by .
git reset <hash>reset the HEAD to the commit indicted by .
git reset --hard <hash>reset the HEAD to hash and remove all later commits.
git diffshows changes.
git cleanremoves all unmonitored files.
git add -Astage all files - both current directory and top level (default, new to version 2).
git add --no-allstages all files except deleted ones.
git add -ustages deleted and modified files, but not unmodified files.
git add *is sometimes used to add all files, but you should not use this command because
*is a shell command, not a git command.
git branchshows all the branches and indicates the current one.
git checkout -b "new branch name"add a branch.
git merge <hash>merge the branch into the current branch.
git branch -D <hash>delete a branch.
git stash save "msg"saves changes.
git stash listshows all the stashes that have been saved.
git stash applyapplies the changes in the stash to the current branch, but does not drop stash.
git stash popgrabs the first stash and applies the changes, and drops the stash.
git stash drop stash(<num>)removes a stash from the list.
git stash clearremoves all stashes (be careful doing this).
git remote add origin "https://github.com/rogerjbos/<newrepo>.git"defines the origin of the remote repository.
git push -u origin masterpushes local code to remote repository.
git push origin my_new_branchpushes the new branch to the remote repository.
git pull origin masterpulls the new version of code from the master branch onto the local computer.
Updating a Linux server
For Ubuntu, Debian, Rasbian:
sudo apt-get update
sudo apt-get upgrade
For Red hat, CentOS, and Fedora:
sudo yum update
sudo yum upgrade
Specifically to update the version of R on CentOS:
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:
Flask app with yagmail and cifs functionality, install these necessary modules:
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
Map network drives using /etc/fstab entry
//192.168.86.2/bosdrive /mnt/bosdrive cifs rw,guest,iocharset=utf8,file_mode=0777,dir_mode=0777,noperm,users 0 0
Configuring CUPS for network printing
- Administrator > Add Printer
- select Internet Printing Protocol
- something like socket://192.168.86.2:9100
- Select PPD File: For Brother 2230 select 2170
Misc libraries that need to be installed for R / R Shiny Server
sudo apt-get install r-cran-rodbc
sudo apt-get install r-cran-xml
sudo apt-get install libcurl4-gnutls-dev
sudo apt-get install texinfo
Modifications to the
# Get your current repo name current_repo <- getOption("repos") current_repo["CRAN"] <- "http://lib.stat.cmu.edu/R/CRAN/" options(repos = current_repo)
Handy: Configure R to use more than one core when compiling source code.
Add following line to
~/.Renviron or /usr/lib64/R/etc/Renviron.site