Linux tips

Terms and conditions of use

We have no responsibility or liability for any unintended consequences or damages that the use of the following information may cause. Please use them at your own risk.

Basics

SSH keys ("ssh-keygen")

# to generate a 4096 bit SSH RSA key
ssh-keygen -t rsa -b 4096
(make sure to set passphrase)

# recommended to register more than one public keys on the host machine
cat id_rsa.pub >> .ssh/authorized_keys

Remote login via SSH ("ssh")

The followings explain how to login to the host machine (server) remotely from the client machine (your terminal).

# to login with a password
ssh -l [user ID] [host machine]

(Example)
ssh -l masakazu [name].aori.u-tokyo.ac.jp

# to login with a SSH key (if already registered)
ssh -i id_rsa -l [user ID] [host machine]

(Example)
ssh -i ~/.ssh/id_rsa -l masakazu [name].aori.u-tokyo.ac.jp

# convenient to create a "config" file in your local .ssh/ (client machine) 
(Example)
Host [name1].aori.u-tokyo.ac.jp
    IdentityFile    ~/.ssh/id_rsa_host1
    User            masakazu
Host [name2].aori.u-tokyo.ac.jp
    IdentityFile    ~/.ssh/id_rsa_host2
    User            masakazu

# convenient to add the following to your .tcshrc
alias [host name] 'ssh -X -l [user ID] [host (target) machine name]'

# tips to export display with heavy graphics
alisa [host name] 'ssh -X -C -l [user ID] [host (target) machine name]'

Check disk usage ("df", "du")

df -h; df -k  # check the total disk usage
du -h; du -k  # check disk usage of the current directory

Backup ("rsync")

"rsync" is a very powerful and useful command with a capability of incremental backup. Recommended to use "rsync", rather than "scp".

# copy locally
rsync -av [source directory name] [target directory name]  # backup locally
rsync --delete-after -av [source directory name] [target directory name]  # backup locally with the "delete" option

# copy via SSH
rsync -ave ssh [source directory name] [target directory name]  # backup via SSH
rsync --delete-after -ave ssh [source directory name] [target directory name]  # backup via SSH with the "delete" option
rsync --copy-unsafe-links -ave ssh [source directory name] [target directory name]  # copy via SSH including softlinked files

Backup2 ("tar")

# tar without compression
tar cvf [file name].tar [directry name]  # combine directries and files to a single file
tar xvf [file name].tar                  # expand a file to directries and files

# tar with compression
tar cvzf [file name].tgz [directry name] # combine directries and files to a single file and compress
tar xvzf [file name].tgz                 # uncompress and expand a file to directries and files

File permission ("chmod")

You need to understand the command, "chmod", first.

# to change all at once
find . -type d | xargs chmod 755  # change all sub-directories to "755"
find . -type f | xargs chmod 644  # change all files to "644"

Jobs

top                            # check machine status
ps u; ps -f; ps -u[user name]  # list user processes
jobs                           # list background jobs
renice +10 [PID]               # lower CPU use
kill; kill -9 [PID]            # stop background process

Jobs2 ("bg", "fg", "&")

You need to understand the commands, "bg" and "fg", first.

# Start shell script
sh [name].sh
bash [name].bash
tcsh [name].tcsh

# Start shell script as a background process
sh [name].sh &
bash [name].bash &
tcsh [name].tcsh &

# Start shell script as a background process with a log output (recommended)
sh [name].sh >& log.[name] &
bash [name].bash >& log.[name] &
tcsh [name].tcsh >& log.[name] &

control+C                      # cancel a foreground process
control+Z                      # stop a foreground process (change it to background with "bg")

Appendix

convert figure file format

ps2pdf [].eps [].pdf   # eps -> PDF file
convert [].eps [].png  # eps -> PNG file
convert [].eps [].tif  # eps -> TIF file
convert [].eps [].jpg  # eps -> JPEG file

Animation

convert --delay ## [].ps [].gif
convert --delay ## *.png [].gif

ASCII format

nkf --unix dos.txt unix.txt  # convert DOS ASCII format to UNIX ASCII format 
dos2unix [windows file].txt  # DOS to UNIX
unix2dos [linux file].txt    # UNIX to DOS

pLATEX

platex [file name].tex  # complie and create DVI file
dvipdfmx [file name]    # convert to PDF file
evince [file name].pdf  # display PDF file

Format newly added HDD

sudo fdisk -l                 # check disks
sudo mkfs -t ext4 /dev/sd[x]  # format with ext4
sudo mkfs.ext4 /dev/sd[x]     # format with ext4

Others

ls -l | wc -l  # count the number of files in the directory (subtract 1)
rpm -ql [package name]  # find the path of the installed package with "yum"

Root administrator

Create an account for a new user

useradd -m -b /home -g users -s /bin/tcsh -p [password] [user ID]

Add RAID

parted -l

parted /dev/sd[x]
  mklabel gpt
  mkpart
  [Enter]
  xfs
  0%
  100%
  print
  quit

cat proc/partitions

mkfs.xfs /dev/sdc1

mkdir /data2
mount /dev/sdc1 /data2

df -hT

emacs /etc/fstab
  /dev/sdc1 /data2 xfs defaults 0 0