UNIX COMMANDS

(vim, SQLite, MySQL)

Last Update Date: 07.06.23
Bottom

apt / apt-get

Debian Package Manager


.bash_profile

This is your login script


.bashrc

Used to customize the shell - loaded whenever you open a new instance of the shell

You can also just use /etc/profile instead of .bashrc or .bash_profile to edit your bash prompt, add aliases, etc. On a Raspberry Pi the custom Root Prompt is placed in /etc/bash.bashrc while the User prompt is placed in ~/.bashrc


In RHEL 7 for example the Alias 'll' with defaults of '-l --color=auto', lives in: /etc/profile.d/colorls.sh.


.htaccess

Example of .htaccess file:

AuthUserFile /var/www/html/private/.htpasswd
AuthGroupFile /dev/null
AuthName "My Private Directory"
AuthType Basic

<Limit GET POST
>
require valid-user
</Limit>

Another Example of .htaccess file:

Satisfy all
Order deny,allow
Deny from all
Allow from all
AuthUserFile /var/www/html/.htpasswd
AuthName "EnterPassword"
AuthType Basic

<Limit GET POST
>
require user ITS
</Limit>

Another Example of .htaccess file, deny IP or range:

Order allow,deny
Deny from 66.249.
Deny from 101.101.12.3
Allow from all

Note: Now create .htpasswd, see .htpasswd below

Note: changes to .htaccess are immediate, no httpd restart necessary

Note: you can also use: require user for specific users


Troubleshooting:


.htpasswd

Create the file:

To add users afterwards:


.profile

Create to automatically load .bashrc in the shell:


~/.bash_profile should be super-simple and just load .profile and .bashrc (in that order)

~/.profile has the stuff NOT specifically related to bash, such as environment variables (PATH and friends)

~/.bashrc has anything you'd want at an interactive command line. Command prompt, EDITOR variable, bash aliases for your use. It must NOT output anything

Anything that should be available to graphical applications OR to sh (or bash invoked as sh) MUST be in ~/.profile

Anything that should be available only to login shells should go in ~/.profile


ACL (adding Access Control List functionality)

Before using ACLs for a file or directory, the partition for the file or directory must be mounted with ACL support:


The file system being exported by an NFS server supports ACLs by default


Set ACLs for files and directories:


Set a Default ACL for a directory:


Remove an ACL for files and directories:


Remove all extended ACL entries:


Remove all the Default ACL entries:


Set ACLs Recursively:


View ACLs for files and directories:


Note: in an long directory listing you ACLs are indicated by a "+" at the end


Active Directory: Joining a Linux Machine to AD

  • SSSD vs. Winbind
  • Winbind-ing

  • Adding new Hard Drive / Local Storage

    1. Add drive to server
    2. Once booted, fdisk –l to see new drive
    3. Create Partitions: fdisk
      1. fdisk /dev/<newdisk>
      2. n (add a new partition)
      3. v (verify)
      4. p (print partition table)
      5. w (write partition table to disk)
    4. Make file system:
      1. mkfs.ext3 –v /dev/<newdisk>
      2. mkfs –v –t ext3 /dev/<newdisk>
    5. Make local directory: mkdir mymount
    6. Mount new drive:
      1. mount /dev/<newdisk> /mymount
    7. Make mount permanent:
      1. edit /etc/fstab:
        1. /dev/ /mymount defaults 1,1

    Add a User

    Useradd (See User Administration below)


    Alias

    Create temp alias: alias newcommand=’yourcommand –arguments’

    Remove temp alias: unalias newcommand

    Aliases with switches: alias ‘rm –rf’="rm –rfv"

    Disable an alias for the current session (ex. Preventing cp overwrite prompt): unalias cp / unalias <alias>

    Temporarily Bypass an Alias:

    To Load your new aliases immediately from .bashrc: source ~/.bashrc or . .bashrc

    Aliases, system-wide for all users, edit:

    # Source global definitions
    if [ -f /etc/bashrc ]; then
    . /etc/bashrc
    Fi

    Some Examples:


    Finding Rogue Alias Declarations:


    In RHEL 7 for example the Alias 'll' with defaults of '-l --color=auto', lives in: /etc/profile.d/colorls.sh. So if your Alias is being overridden, check under /etc/profile.d/ first.


    Apache / httpd

    Restart Apache: apachectl restart

    Alternate: service httpd restart

    Apache Version: apachectl –v or apachectl –V (Verbose output including the compile settings)

    Apache Version on other systems: try httpd –v or apache2 –v or apache2ctl –v

    Test Apache / HTTP configuration file for errors: apachectl configtest

    RHEL 7 / CentOS 7:


    Apache HTML files

    /var/www/html or /usr/local/apache/htdocs (See User Administration below)


    Authentication – setup

    To setup LDAP or authentication types, in general, use:


    bmon – Command Line

    Powerful Network Bandwidth Monitoring and Debugging Tool for Linux: bmon

    Command Line Switch (-p): bmon -p <network interface to display>

    Command Line Switch (-b): bmon -b <Use Bits per Second (Mb, Kb) instead of Bytes per Second (Mib, Kib)>

    Interface Command: d - for Detailed statistics

    Interface Command: i - for additional Information

    Interface Command: Shift+q - for Quick reference


    Bookmark – Command Line, Terminal

    To bookmark the current directory use: pushd .

    To return to the bookmarked directory use: popd


    Change a user password

    passwd


    Check your environment, path variables

    env


    Command line Calendar

    To display the prev, cur, next month calendar: cal -3

    To display the Year calendar: cal –y


    Command line editors

    pico, nano, vi, vim


    Compare Files (diff)

    cmp file1 file2

    diff file1 file2

    diff --ignore-space-change file1 file2

    diff --side-by-side –-suppress-common-lines file1 file2


    Console Resolution – changing

    Changing Framebuffer or Text Console Display Line Resolution:


    Copy – Copy files and Directories

    Recursive Copy All files in Current Dir to /tmp: cp –r * /tmp


    CPU Information

    Number of CPUs: nproc

    Get CPU information: cat /proc/cpuinfo

    CPU Information / Cores / Threads per Core / etc.: lscpu

    Is this Server a Virtual Machine?: dmidecode | grep –i product

    Get the number of CPUs: grep –i "physical id" /proc/cpuinfo | sort –u | wc -l

    Is Hyperthreading enabled? Show Threads Per Core: lscpu | grep –i thread

    XSOS tool for sysadmins (shows all this + more): wget people.redhat.com/rsawhill/rpms/latest-xsos.rpm


    Crontab, List crontab files

    User crontab files are kept in: /var/spool/cron

    crontab –lu <username>

    Crontab Positions and what they mean (1 2 3 4 5 6-task):

    1 = Minute (0-59)
    2 = Hour (0-23, 0=Midnight)
    3 = Day of Month (1-31)
    4 = Month (1-12)
    5 = Day of Week (0-6, Sunday=0) 6 = The Task (command) to Run at that Time

    Ex.: Delete all files in your tmp folder every morning at 4:45am, Mon-Fri: "45 4 * * 1-5 rm /home//tmp/*"

    Ex.: to run a job every 5 minutes: "*/5 * * * * /my/command"

    Note: It’s best to use the full path to any commands given, ex: /usr/bin/rm /home//tmp/*

    Note: If you don’t want an e-mail sent every time a cron job runs use: "45 4 * * * rm /home//tmp/*>/dev/null 2>&1"

    Note: To collect the cron execution in a log file: "45 4 * * * rm /home//tmp/*>/home//cronlogs/clean_tmp_dir.log"


    Current Login Session Info

    who –umH or w


    Cut

    Say you have a text file with lines of e-mail addresses and you only want the login names:

    Ex. File.txt
    mickeymouse@aol.com
    minimouse@aol.com
    donaldduck@aol.com

    Run: cat File.txt | cut –d’@’ –f1

    Where: d = delimiter, -f1 is the field number

    Produces:

    mickeymouse
    minimouse
    donaldduck

    Date (Show Date and Time, Set Date and Time)

    Show the Date and Time: date

    Set the Date and Time: date –s "02/16/2006 16:11:00"


    Delete / Remove a Directory and it’s contents

    rm –r –v mydir/ = recursive, verbose


    Delete a User

    userdel (See User Administration below)


    DHCPd

    On Redhat 4 (Standard Install)

    If you wish to test the configuration for any oddities, you can start dhcpd with the debugging mode. Typing the command below will allow you to see exactly what is going on with the server. Boot up a client. Take a look at the console of the dhcp server and you will see a number of debugging messages appear on the screen:

    Other Locations:


    Directory Listing All w/ Folder, Executable, Links, DIR

    ls –alF

    Show only Directories:

    Show only the Number of Directories: ls –lF | grep ^d | wc

    Display File Size in Human Readable Format: ls –lh

    Display File UID and GID instead of User and Group: ls –n

    Display One File Per Line: ls –1


    Disk space, file system disk space usage, file system type (ex. Ext3, etx4..)

    Simple Summary of Disk Usage in the Current Folder: du –hs

    Directory Summary of Specified Directory: du –hsc <mydir>, ex. du –hsc music

    Directory Summary w/ Subdirectory Summary: du –hc <mydir w/ subdirs>, see abve

    Directory Summary w/ Ind. File Summary: du –hac <mydir>


    Display the Kernel Ring buffer (prints out Bootup messages)

    dmesg


    DNS (named)

    DNS Server Control:

    Flush DNS Server Cache: rndc flush


    Environment Variables

    Export Environment Variables on System Reboot:

    For Regular Users, Edit one of these files:


    ethtool

    Display or change Ethernet card settings

    Can easily get your Link Speed, Duplex, etc.


    fdisk, Display Drive / Partition information

    fdisk –l

    Alternatively: dmesg | grep hd or dmesg | grep sd


    File Descriptors

    To increase file descriptors on Red Hat systems:

    To increase file descriptors in the Kernel on Red Hat systems:

    After making changes, reboot or sysctl –p

    Check your user limits: Sign in as user, issue: ulimit –n


    File Perusal Filter (Less), opposite of More

    ls | less (less allows scrolling by line or page)

    In less, use ‘/’ to search for a pattern: /searchterm


    Default file system

    RHEL5: ext3

    RHEL6: ext4

    RHEL7: xfs


    File Type Determination

    file <filename>


    Find

    Find all files w/ index in the name & htm in the extension, no regard to case:

    Find all files or directories modified in the last 24 hours:

    Search the whole file system for all files being modified / accessed on Jan 26,2006:

    Find all .txt files and show file datetime stamp sorted:


    Find file by filename

    whereis <filename>


    Firewall (iptables)

    /etc/sysconfig/iptables = firewall rules file

    To Enable logging to an Alternate File (ex. Iptables.log) the default = /var/log/messages:

    To Enable iptables logging:

    To List iptable Rules with Line Numbers: iptables –L –line-numbers

    RHEL / CentOS 7:


    Gawk – Pattern Matching

    Return only the usernames (9th column) and Ips (11th column) for those people who were able to login to ssh by searching the secure.x files


    Graphical Login / startx - Kill the GUI

    Enter a virtual terminal and then kill the GUI:

    init 5 = starts GUI again

    Ctrl-Alt-F7 is where the GUI usually is


    Grep

    Find all occurrences of in file(s), case-insensitive:

    Find all IP’s in 192.215. range:

    Find (using Extended Regular Expressions) this OR that, ignore case:

    Grep for a word and show the lines around the matching line:

    More Examples


    Groups

    List of groups is in: /etc/group

    Fields:

    List Groups a User belongs to:

    Show group ID of user: id –g jdoe

    Show group ID to Group Name of user: id –gn jdoe

    Show group ID and supplementary groups of a user: id –Gn root

    Create a New Group: groupadd gDevelopers


    Gzip / Gunzip / Compress / Expand Files

    gunzip myfiles.gz

    Ex. Write the uncompressed contents of README.gz to standard output. Pipe it into a pager for easy reading of a compressed file:


    History

    Search History for command: history | grep –i "<search term>"

    Execute a command by its number: !<history number>

    Prevent Saving your history upon shell Exit: Log out using: kill -9 $$

    To Manually Clear your history: Type: history –c

    Reverse-Search-History (reverse-i-search):

    To Search History Forward

    To add Date and Time to bash History (bash > 3.0) for all users, edit /etc/profile (for individual users, edit the /etc/bashrc file):


    Hostname / Machine Name

    hostname

    Update /etc/sysconfig/network to include: HOSTNAME=myserver.mydomain.com

    Update /etc/hosts file to include: 192.168.1.1 myserver.mydomain.com myserver

    Set hostname without rebooting: echo > /proc/sys/kernel/hostname

    Verify hostname is set:

    RHEL 7: /etc/hostname or hostnamectl


    HTML Redirect

    Redirect Code:


    IOSTAT (CPU and I/O Stats for Devices & Partitions)

    Prints CPU and Device I/O status, per second and overall: iostat

    Display 3 reports at 1 second intervals for device sda and all it’s partitions: iostat –p sda 1 3


    Ipconfig

    Ifconfig


    ISO – Make DVD .iso out of CDs (RHEL 5 – 5 CDs into 1 DVD)

    Create top directory, you will need almost 3gb of space:

    1. mkdir dvd

    Create directories on which to mount the CD ISO images using loop device mounts:

    1. mkdir rhel5{1,2,3,4,5} rhel5-docs

    Make an ISO out of each CD, drop ISO in correct directory:

    1. dd if=/dev/cdrom of=/dvd/rhel51/rhel51.iso
    2. dd if=/dev/cdrom of=/dvd/rhel52/rhel52.iso
    3. dd if=/dev/cdrom of=/dvd/rhel53/rhel53.iso
    4. dd if=/dev/cdrom of=/dvd/rhel54/rhel54.iso
    5. dd if=/dev/cdrom of=/dvd/rhel55/rhel55.iso

    Mount the new ISO images using a loop device mount:

    1. mount –o ro,loop /dvd/rhel51/rhel51.iso rhel51
    2. mount –o ro,loop /dvd/rhel52/rhel52.iso rhel52
    3. mount –o ro,loop /dvd/rhel53/rhel53.iso rhel53
    4. mount –o ro,loop /dvd/rhel54/rhel54.iso rhel54
    5. mount –o ro,loop /dvd/rhel55/rhel55.iso rhel55

    Copy the isolinux directory and the .discinfo from CD 1 to the current directory (i.e. dvd):

    1. cp –a rhel51/isolinux rhel51/.discinfo .

    Edit the .discinfo file, replace forth line with 1-5, this will tell the .discinfo file that it’s looking at 5 CDs total:

    1. vi .discinfo, change line 4 to read 1,2,3,4,5

    This is key, or you’ll get errors with mkisofs:

    1. rm isolinux/boot.cat, (if not, you will get an error about a null pointer having same rock ridge name boot.cat)

    Create the DVD ISO Image (l for long filenames, J so windows can read the disc, -R for filenames, -v verbose, -V volume label):

    1. mkisofs –l –J –R –v –V "RHEL5" –o redhatesdvd.iso –b isolinux/isolinux.bin –c isolinux/boot.cat –no-emul-boot –boot-load-size 4 –boot-info-table –m TRANS.TBL –x rhel51/.discinfo –x rhel51/isolinux –graft-points rhel51 .discinfo=.discinfo isolinux/=isolinux Server/=rhel52/Server Server/=rhel53/Server Server/=rhel54/Server Server/=rhel55/Server VT/=rhel55/VT docs/=rhel5-docs

    Create the DVD ISO Image for Oracle Enterprise Linux 4.7:

    1. mkisofs –l –J –R –v –V "OEL47" –o OEL47.iso –b isolinux/isolinux.bin –c isolinux/boot.cat –no-emul-boot –boot-load-size 4 –boot-info-table –m TRANS.TBL –x el1/.discinfo –x el1/isolinux –graft-points el1 .discinfo=.discinfo isolinux/=isolinux Enterprise/=el2/Enterprise Enterprise/=el3/Enterprise Enterprise/=el4/Enterprise Enterprise/=el5/Enterprise

    Test Mount the new DVD ISO:

    Burn the new DVD ISO with Nero and Install!

    Links: http://www.mjmwired.net/resources/redhat8-dvd.html

    Links: http://www.linuxquestions.org/questions/showthread.php?t=148702

    Network Install Prep: http://www.redhat.com/docs//manuals/enterprise/RHEL-5-manual/Installation_Guide-en-US/s1-steps-network-installs-x86.html


    ISO - Make a Backup of a DVD

    DVD to .ISO: dd if=/dev/cdrom of=/path/dvd.iso status=progress


    JAVA - Check Heap Size

    Check heap size of a running process: jmap -heap <pid_of_tomcat_process>


    Jobs – Background, Foreground, Killing

    To background a process or job use the & after the command:

    To foreground a process or job use:

    To list jobs currently running use:

    To kill a job use – this will request the job shutdown and clean up it’s tmp files:

    Note: If you’ve started a process in the foreground and you decide later that you’d like to background it, you can use Ctrl-Z to stop the process and then use "%&" on the command line to restart the process in the background


    Jumbo Frames

    Test it out: ifconfig eth0 mtu 9000

    Make it permanent:

    Check the configured MTU via netstat: netstat -i

    Check the MTU via ifconfig: ifconfig

    To confirm the MTU used between two specific devices. Use ip command as follows:

    You may need to tune the application / network protocol such as NFS and SMB to take advantage of Jumbo Frames.

    Add this to /etc/samba/smb.conf:

    1. read size = 262140
    2. max xmit = 262140
    3. socket options = TCP_NODELY SO_SNDBUF=262140 SO_RCVBUF=262140

    Note: This sped up jumbo frames from 30-40MB/sec to 60-70MB/sec


    Kernels - Removing Old and Unused Kernels on CentOS

    By default CentOS will keep last 5 kernels installed on your system. This behavior is defined by installonly_limit=5 line within /etc/yum.conf file


    Kill (terminate or signal a process)

    Escalating Kills:

    Signals (a signal is what linux uses for sending information between processes or between the kernel and a process):

    To Kill a user (disconnect them), kill their shell process (ex. PID sshd: root@pts/0):


    ldd – Print shared library dependencies

    [root@myserver bin]# ldd uxwdog

    libicuuc.so.2 => not found
    libicui18n.so.2 => not found
    libicudata.so.2 => not found
    libplc4.so => /usr/lib/libplc4.so (0xb75d4000)
    libplds4.so => /usr/lib/libplds4.so (0xb75d1000)
    libnspr4.so => /usr/lib/libnspr4.so (0xb759f000)
    libpthread.so.0 => /lib/tls/libpthread.so.0 (0xb758f000)
    libdl.so.2 => /lib/libdl.so.2 (0xb758c000)
    libcrypt.so.1 => /lib/libcrypt.so.1 (0xb755f000)
    libresolv.so.2 => /lib/libresolv.so.2 (0xb754d000)
    libstdc++-libc6.2-2.so.3 => /usr/lib/libstdc++-libc6.2-2.so.3 (0xb750b000)
    libm.so.6 => /lib/tls/libm.so.6 (0xb74e9000)
    libc.so.6 => /lib/tls/libc.so.6 (0xb73b0000)
    /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0xb75e9000)
    libgcc_s.so.1 => /lib/libgcc_s.so.1 (0xb73a7000)

    If you have program libraries that are stored in different directories on the system other than /lib, you might need to add something like this to your .bashrc file:


    List system PCI devices and all devices connected to them

    lspci


    Locale – Change your language locale, UTF-8 or not

    If you see weird characters in your Shell client, check to see if your Linux locale is set to use en_US.UTF-8 and you can change it to en_US:


    To fix Ubuntu Server Error via SSH: -bash: warning: setlocale: LC_ALL: cannot change locale (en_US)


    Login Shell – User Login Shell

    List available shells:

    Change a user’s Login Shell:


    LogWatch

    LogWatch configuration file: /etc/log.d/logwatch.conf

    View / Change the Services LogWatch monitors: /etc/log.d/conf/services/.conf

    LogWatch runs under cron.daily: /etc/cron.daily/00-logwatch (/etc/log.d/scripts/logwatch.pl)

    To change what time cron.daily (hourly / weekly / monthly) runs: vi /etc/crontab


    Ls – List Directory Contents

    ls –d win* = (DOS) dir win*.*

    ls –lh = lists files w/ Bytes, K units

    ls –F = Marks directories w/ trailing /, marks executables w/ trailing *, marks smbolic links w/ trailing @

    ls –ld */ = Show only directories (keep color codings)


    MCPAN

    Reconfigure MCPAN:

    Configure Perl CPAN URL List:


    MDADM / MDSTAT

    View the status of all multi disk arrays: cat /proc/mdstat

    View the status of a multi disk array: mdadm –detail /dev/md0

    Rebuid array after receiving error message: Kicking non-fresh sdc1 from array!:

    This can happen after a power failure or UPS problem. Try adding the kicked disk back into the array. This will rebuild the mirror from the good disk. It will take time.


    Memory (Investigate Memory Usage)

    To Investigate Memory Usage on a Linux system you can use a few different tools:

    Use Watch to monitor Memory and Highlight Differences: watch –d free –mt


    mkdir

    Create a directory and sub-directories:


    Modules – Lists modules currently loaded into the kernel

    lsmod


    mod_rewrite – for Apache / Apache2

    By default, Apache2 comes with mod_rewrite installed. To verify its existence, run:

    To enable and load mod_rewrite:

    To use it, in .htaccess to not dispaly .php extensions, add this to .htaccess file:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d   # is not a directory
    RewriteCond %{REQUEST_FILENAME}\.php -f   # is an existing .php file
    RewriteRule ^(.*)$ $1.php

    motd – How to change the Message of the Day

    vi /etc/motd

    Cool ASCII Art Generator: Link. Very useful for Linux logon banners using Font: small, Character Width: Default, Character height: Default.


    Mount

    Cause all the file systems mentioned in fstab to be mounted as indicated (except those w/ noauto):

    Mount CD-ROM:

    Mount USB device:

    Mount a SAMBA share on Linux:

    Mounted Devices – Display what’s mounted

    UnMount: umount uba1

    Note: /etc/fstab defines mountable file systems & devices on startup. Edit this to make the mount available on startup


    MySQL Commands

    Show Databases:

    mysql> show databases;
    shell> mysqlshow –u root@<hostname>

    Use a Database:

    mysql> use <databasename>;

    Show Database Tables:

    mysql> show tables;
    mysql> select * from mytable;

    Dump Database:

    shell> mysqldump –u <mysqlusername> -p <databasename> > mydb.sql;

    Log in as root & use the mysql DB:

    shell> mysql –user=root mysql
    shell> mysql –u root@<hostname>

    Add Users:

    shell> grant all privileges on *.* to ‘<mysqlusername>’@’localhost’ identified by ‘mypassword’ with grant option;

    Show mysql Users:

    mysql> use mysql;
    mysql> select host,user,password from user;

    Create new Database:

    mysql> create database <dbname>;

    Import Database SQL Dump into new DB:

    shell> mysql –u <mysqlusername> -p <databasename> < /tmp/mydb.sql

    Remove all Records from a Table:

    mysql> truncate table <tablename>;

    Remove a Table:

    mysql> drop table <tablename>;

    Count Records:

    mysql> select count(*) from <tablename>;
    mysql> select count(username)from users;

    Alter: Add a Column to a Table:

    mysql> alter table <tablename> add column <newcolumn> char(25);

    Select: Select from 2 Tables:

    mysql> select employees.Firstname, employees.Lastname, vehicles.Car from employees, vehicles;

    Show Version and Proc:

    mysql> mysqladmin –u root –p version proc

    MySQL Help, List Server-Side Help:

    mysql> help contents

    Name Resolution - Configure

    /etc/hosts

    /etc/resolv.conf


    netstat - network connections, routing tables, interface stats, etc.

    Show Listening Ports and Processess, numerically (i.e. not by service name):

    Show only Listening TCP sockets::

    Show only Listening UDP sockets::

    Show only Listening UNIX domain sockets::

    Note: netstat translates the default ports to the service name using the mappings from this file: /etc/services


    Network Card Configuration via Command Line

    Server Side, Edit /etc/sysconfig/network-scripts/ifcfg-eth0:

    Save file and then: service network restart


    NFS

    Server Side, Start NFS:

    Server Side, edit: /etc/exports

    Server Side, Notify NFS daemon of your change: /sbin/service nfs reload

    Client Side:

    Note: exportfs –a exports all directories in /etc/export

    Note: exportfs –au un-exports all directories in /etc/export


    NFS: Locking Down the Ports, Opening the Firewall - How can I configure a system as an NFS server which sits behind a firewall with NFS clients outside of the firewall?

    Server Side, Create or Edit /etc/sysconfig/nfs:

    # NFS port numbers
    STATD_PORT=11002
    STATD_OUTGOING_PORT=11003
    MOUNTD_PORT=11004
    RQUOTAD_PORT=11005
    LOCKD_UDPPORT=30001
    LOCKD_TCPPORT=30000

    Server Side (may not need this): reboot

    Server Side, Check the Port Assignments: rpcinfo –p localhost

    Server Side, Open these Ports in the Local Firewall:

    111: portmap (tcp/udp)
    2049: nfs (tcp/udp)
    4045: nfs lock manager port (tcp) ?? may not need this port
    30000: example lockd (tcp)
    30001: example lockd (udp)
    11002: example statd/status (tcp/udp)
    11003: example statd/status outgoing (tcp/udp)
    11004: example mountd (tcp/udp)
    11005: example rquotad (tcp/udp)div>

    NFS Troubleshooting

    Server-Side:

    Client-Side:


    NetworkManager on CentOS / RHEL 7.x

    Service

    Common Commands:


    NTP Server Setup (UDP 123)

    Setting up Linux as a Time Server:

    1. Install latest version of ntp (rpm –qa | grep ntp)
    2. Edit /etc/sysconfig/iptables add upd access for port 123 (ntp)
    3. Make sure Service Starts on boot in current runlevel: chkconfig ntpd on
    4. Modify /etc/ntp.conf (add time servers, add restrictions for local network access)
    5. ntpdate –b pool.ntp.org (initial sync, done the first time before ooking the daemon, do NOT need to do each time the daemon starts)
    6. Start Service: service ntpd start
    7. Verify it’s running: pgrep ntpd
    8. Verify it’s listening: netstat –tuna
    9. Check how it’s synchronized: ntpq –p

    Indication That Your Time Server is Working:

    Telltale Sign Your Server is Not Synchronizing:

    Note: Your internal workstation computers will not be able to use the server as a synchronization source until the LOCAL(0) clock has stable time. This may take up to 15 minutes after starting the NTP daemon

    Time Servers:

    server 0.pool.ntp.org
    server 1.pool.ntp.org
    server 2.pool.ntp.org
    server pool.ntp.org

    Working Configuration File: ntp.conf


    NTP Server Tools

    It may take a number of minutes for the clock to synchronize. You can use the ntpdc command to view information about ntpd’s status. It can be used in interactive mode, by simply entering ntpdc, or invoked with the –c switch to run the commands from the shell prompt.

    Note: these commands will only work on a server running ntpd, i.e. a Time Server, not a Client

    Here is a list of useful ntpdc commands:


    Oracle

    Get Oracle Version Number: select banner from v$version;


    OS / Kernel Version

    uname –sr = kernel name, kernel release

    uname –r = kernel release number

    uname –v = kernel version

    cat /etc/xxxxx-release = where xxxxx is redhat, fedora, etc. this shows what OS and version is installed


    Pam.d (Auto-create Home Directories on a Server when Logging in using LDAP authentication)

    Each client is responsible thru pam.d to create their own home folder when they log in to a server using ldap.

    Make sure each client uses authconfig to use the LDAP server (use MD5 passwords, Use Shadow Passwords, Use LDAP Authentication, Local authorization is sufficient)

    When the client logs in, the client pam uses the ldap attribute called: homedirectory as the path to create their home directory.

    To enable the client to create their home directory based on that ldap attribute, you add this line to: /etc/pam.d/system-auth:

    Here’s the Step by Step along with setting up permissions ACL on the home directory: Document


    Password – change users password

    passwd username

    Lock a User Account: passwd –l username

    Unlock a User Account: passwd –u username

    Set User Account Expiration: usermod –e 07/23/2006 username


    Path, Setting your Path

    export PATH=$PATH:/my/new/directory1/:/two

    Enter this each time you login or add to your .bashrc file so it will stick on subsequent logins

    Adding a ‘.’ As the last entry in the PATH means current directory for a program name. Thus preventing you from having to type ‘./myprogram’ to execute a program. Just type ‘myprogram’


    Perl – Installing Modules

    Manual Way:

    1. Download perl-ldap from cpan
    2. gunzip –c perl-ldap-0.29.tar.gz |tar xvf –
    3. cd perl-ldap-0.29
    4. perl Makefile.PL
    5. make
    6. make install

    Easy Way:

    1. perl –MCPAN –e ‘install Net::LDAP’

    Perl – Check for Existence of a Perl Module on your System

    Does a Module Exist:

    View @INC array (to see where Perl is searching for it’s modules): perl –V


    Permissions

    User / Owner (you)

    Group (a group of other users that you set up)

    Other / World (anyone else browsing on the file system)

    The value of each digit is set according to what rights each of the types of people above have to manipulate that file.

    Permissions are set according to numbers. Read is 4. Write is 2. Execute is 1. The sums of these numbers give combinations of these permissions:

    Ex. chmod 700 myfileonly.html

    Permissions: Change the group of a file:

    Permissions: Change the owner of a file:


    PGREP – Show Process ID

    pgrep looks through currently running processes & lists the PID’s which match the selection criteria


    PHP – php.ini

    vi /etc/php.ini


    Postfix

    Postfix mail system commands below are reserved for superuser / root users only.

    sudo postfix reload (Re-read configuration files, Running processes terminate at earliest convienence)

    sudo postfix status (Indicates if postfix is running, if so its PID)

    sudo postfix stop (Stops postfix in an orderly fashion)

    sudo postfix start (Starts postfix and runs a configuration check)

    sudo systemctl restart postfix (Restarts postfix using systemctl)

    sudo postfix check (Runs a configuration check)

    Note: Postfix configuration files are: /etc/postfix/main.cf and /etc/postfix/master.cf


    Power Off / Shutdown / Reboot

    poweroff (turns off computer)

    shutdown –r now (reboots)

    reboot (reboots)


    Print Screen (PrtScn) – in X

    Capture the entire screen: press "PrtScn" button

    Capture where Mouse points to: press "Alt+PrtScn" button


    Process Status

    ps


    Processes – Show

    ps –A –-sort command / cmd, Ex. ps -A --sort=uid,cmd or ps -A --sort=pid

    ps –A | grep httpd

    ps –wwaux

    Show in Tree Form: pstree


    Return to Your Home Directory

    cd ~


    Root Password, Forgot Root Password

    http://www.creativelogichome.com/unix/unixrpw.htm


    Route – Adding / Deleting

    Add a Static Route to the 10.199.1.0 Network Using the Gateway of 192.54.86.51:

    Delete that Static Route to the 10.199.1.0 Network Using the Gateway of 192.54.86.51:

    Show the Current Routes:

    Note: To Make the Routes Permanent (so they stick when the box is rebooted):


    Routing Table, Display Kernel IP Routing Table

    netstat –nr

    route –v


    RPM

    Query version of a package:

    Install a package:

    Force Install of package:

    List contents of a package (shows files and file destination paths):

    Remove / Erase a package:

    Show all installed packages:

    Show where package was installed (ex. Pine):

    Following command can be used to distinguish between a 32-it or 64-bit package:


    RSYNC – backup

    rsync –av source/ destination/

    rsync -av --delete source/ destination/

    rsync –n –av –delete source/ destination/

    Switches:


    Run Levels - (Runlevel)

    Ctrl-Alt-F1 = switch to console

    RHEL / CentOS 7:


    Running Remote X Applications

    On local machine, log into GUI, open terminal window:

    echo $DISPLAY to see what display is being exported

    Run remote X Applications – Export Display

    On local machine:

    On Remote Machine:


    Samba

    List info about machines that respond to SMB name queries on a subnet. Use this to see if your local host smb is started.

    Connect to Samba Share:

    List Local Samba Shares (w/o password):

    Create a samba password:

    Samba configuration file:


    Scheduling Jobs

    at <time> = runs commands at specified time

    atq = lists pending commands

    atrm <job> = cancels pending jobs

    batch = runs commands when system load permits


    SCP (Secure Copy) – Remote File Copy, uses SSH

    scp –P <target port> <file1> <user@host>:<location on host>

    Ex. scp –P 42XXX myfile.tar.bz2 uers@remotesystem.worldnet.net:/home/oracle


    Copy an entire Directory (all files below, recusively (-r) AND preserve modification times (-p)) to a Target system directory (If target directory doesn't exist, it will be created (-r)):


    Copy a file From a Remote Server to Your Local Server


    Screen – Screen Manager w/ VT100 / ANSI Terminal Emulation


    Install: yum install screen


    Screen Commands from within the Root Terminal:


    Screen Commands from within a Screen Session:


    Screen Scrollback Buffer:


    Script – Make a typescript of a terminal session

    This command makes a typescript of everything printed on your terminal:


    Search through files to find text (fgrep)

    fgrep "search_word" *.*

    fgrep = grep –F


    Sed – Search and Replace within Files (ex. *.html)

    This command will search all .html files and replace 'foo' with 'bar'

    If you want to get sed to make a copy of the original file prior to the change do:


    Sendmail

    Make changes to the /etc/mail/sendmail.mc file, then compile with:

    If you get this error: "sendmail.mc:10: m4: Cannot open /usr/share/sendmail-cf/m4/cf.m4: No such file or directory", then:

    Send Mail from the Command Line:

    ..OR..


    Sendmail - Configure to Send Outgoing Messages

    /etc/hosts file: xxx.xxx.xxx.xxx mail_hostname

    /etc/mail/sendmail.mc file: define(`SMART_HOST',`mail_hostname')

    Recompile sendmail.mc: m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf

    Restart sendmail: service sendmail restart

    Add this line to /etc/hosts.allow: sendmail : localhost : ALLOW

    How To:


    SELinux

    Check SELinux Status:

    Disable SELinux:

    Note: If you changed from Enabled to Disabled or vice versa, you need to restart the machine for the change to take effect.


    Services

    Apache = httpd

    DHCP Client = dhclient

    DNS Server = named

    Firewall = iptables


    Services – updates and queries runlevel information for system services

    chkconfig

    List which services are available and in which runlevel they are started:

    Update:

    List all services that are turned on in runlevel 5:

    Delete / Remove a service from chkconfig management and any symbolic links in /etc/rc[0-6].d which pertain to it are removed:

    Text GUI for managing which services are started in the current runlevel, can use the –level to modify other runlevels:


    Services, Start, Stop, Restart

    service <service_name> start|stop|restart

    To check the Status of a Service:

    RHEL 7 / CentOS 7:


    SFTP (Secure File Transfer Program)

    To open a secure, interactive FTP session that is encrypted: sftp user@server.com

    Once authenticated, use regular FTP commands


    Shell Commands: Bash, Bash Command Line Editing Commands

    Bash readline:

    To Enable VI style Key Bindings: set –o vi, press Esc to activate VI style Editing

    To Enable Emacs Bindings (default): set –o emacs

    To list all Bindings: bind –p


    Single User Mode

    Edit Kernel line, add "single" at the end, boot the modified kernel line. In GRUB:


    smartctl

    Verify the manufacturer, model, and SMART capability, and look at the current state of health (as last recorded):


    SSH - Change Terminal Title

    To have your hosting provider set your linux terminal title to username@host:directory, add this to your hosted accounts ~/.bashrc:


    SSH (OpenSSH Remote Login Client)

    To execute a command on a remote machine without logging into a shell prompt:

    Use your OpenSSH Private Key to Login to a Server that has your Public Key (default is ~/.ssh/id_rsa):


    SSH (Generating Authorization Key Pairs)

    Starting with OpenSSH 3.0, SSH protocol 1 and 2 share these files:

    Starting with OpenSSH 2.9, the default keygen uses RSA

    Note: A passphrase is a string of words and characters used to authenticate a user. Passphrases differ from passwords in that you can use spaces or tabs in the passphrase. Passphrases are generally longer than passwords because they are usually phrases instead of a single word.

    Generating Authorization Key Pairs – prevents having to use password each time you ssh, scp, or sftp into a remote machine

    Keys MUST be generated for each user. Follow these steps as the user who wants to connect to a remote machine (for ex. If you complete these steps as root, only root will be able to use the keys):

    Client (source) Machine:

    General Process: Generate keys on the Client, Copy the client .pub key to the desired user (their .ssh/authorized_keys file) who you want to connect as on the remote server


    Using the Keys:

    Getting / Comparing the Public Key from the Private Key:


    SSH Terminal Character Encoding

    If you see characters such as: †in your SSH session, it's the terminal program's character encoding that neets to be set to UTF-8


    SSH Daemon – sshd

    Disable direct ssh Root login:


    Check SSL Certificate Expiration Date:

    echo | openssl s_client -connect hostname.xxx.xxx:port 2>/dev/null | openssl x509 -noout -dates


    Dump SSL Certificate Data:

    openssl s_client -showcerts -connect hostname.xxx.xxx:port


    Startup – Xwindows (Windows Startup Folder Equivalent)

    To start a script or program when the user logs on using startx (KDE / GNOME) place a script here:

    Ex. To Start Synergy client:


    SUDO

    Config File is: /etc/sudoers

    Edit the Config File using visudo

    Create User Groups under “User Alias Specification” section:

    Give Groups permissions in “User Privilege Specification” section:

    Execute command as user (use users pass): sudo vi /etc/sudoers


    Swap File Usage

    To monitor Swap Space Usage: swapon –s

    ..or..: cat /proc/swaps


    Syslog

    To disable /var/log/messages output


    SystemD

    System Service Management, using the systemctl action service pattern:

    Note: Systemd provides a standard process for controlling what programs run when a Linux system boots up. While systemd is compatible with SysV and Linux Standard Base (LSB) init scripts, systemd is meant to be a drop-in replacement for these older ways of getting a Linux system running. Systemd, which was created by Red Hat's Lennart Poettering and Kay Sievers, does more than start the core programs running. It also starts a journal of system activity, the network stack, a cron-style job scheduler, user logins, and many other jobs. That may sound good to you, but some developers hate it.


    System Uptime

    uptime


    Tail

    Outputs the last part of files

    -f = output appended data as the file grows; follow output

    Ex. tail –f /var/dhcpd/dhcpd.leases


    Tar / Archiving Files


    Tar:


    Tar a Directory:


    UnTar:


    Append / Add files to Tar:


    List Files in Tar:


    Backup: Compare contents of a .tar with your backup directory:


    Backup: Easily add files not found in your backup .tar to the .tar:


    http://www.gnu.org/software/tar/manual/

    Tar Pipe

    cd <source_directory>


    Tar Pipe w/ Exclude File:


    Terminal Type (Checking your Terminal)

    echo $TERM

    Note: PuTTY uses: xterm, while others use ansi


    Terminal Colors (SSH)

    Change the Dark Blue directory color (XTERM color):

    Note: then logoff and logon again. If that doesn’t work, also edit: /etc/DIR_COLORS.xterm and try again


    Text File Conversions (Unix to Windows, Windows to Unix)

    Convert a text file from Windows/DOS to UNIX:

    Convert a text file from UNIX to Windows/DOS:


    Time (Benchmarking tool)

    Runs the specified program command with arguments and upon finishing, time writes a message to STDOUT giving timing statistics about the program.

    1. Prints the elapsed real time between invocation & termination

    2. Prints the user CPU time

    3. Prints the system CPU time

    time traceroute www.google.com


    Time Server – Manually set the host clock via time server

    ntpdate time.windows.com

    Query TS, doesn’t set clock: ntpdate –q <timeserver>

    Query the hosts CMOS clock: hwclock –show


    Top – Display Linux Tasks

    To sort by CPU (or other Field): Shift+f (or Shift+o), then K for %CPU, P makes Top order by CPU, W saves the configuration

    Note: in RHEL 7 (procps-ng version 3.3.10), you can use the arrow keys to choose sort fields when in field management mode.

    When Viewing Top in RHEL 7:


    Users

    users = list current users

    who = displays who is on the system

    w = displays who is on the system and what they are doing

    last = indicates last logins of users and ttys


    Undo typing in shell / terminal (deletes entire line)

    Ctrl-u


    Uniq – Unique

    Removing Duplicate Lines with: uniq

    Display only the Unique Lines

    Display only the Duplicate Lines

    Display Number (count) of Unique Lines

    Display Number (count) of Duplicate Lines

    Display Unique Lines in messages, skip field 3 (i.e. ignore unique timestamp field)

    Display Unique Lines in messages, skip field 3 and 9 more characters from there (i.e. ignore unique timestamp field + 9 characters)


    User Administration (Add / Delete / Modify / Change password / Switch)

    Add a new user: useradd


    Modify a user: usermod


    Rename a user: usermod –l


    Delete a user: userdel


    User’s password: passwd


    Force User to Change Password at Next Login:


    Switch to a Users: su


    Add a group: groupadd


    Delete a group: groupdel


    User / Group File Information:


    Vi (vim) – text editor

    VIM Complete

    Hilight / Visual Mode:


    As you Type in Insert Mode:


    Insert Mode:


    Command Mode:


    Shell


    Settings:


    Windowing:


    Search / Replace:


    Cursor Position:


    Other:


    VMSTAT (Reports Virtual Memory Stats)

    Display a Quick Summary: vmstat –s

    Display in 1 second intervals, 5 times: vmstat 1 5


    VNC Server

    On the Linux Server:

    To start KDE or Gnome in VNC:


    On VNC Client:


    Install VNC Server on Ubuntu MATE using Desktop Display:


    VSFTP

    By default, vsftpd displays directory listings in GMT, to set this to list files in your local time, use:


    Wall (Send a Message to Everybody’s Terminal)

    To send a broadcast message to everyone’s terminal: wall –n message ctrl-D

    Without –n the banner reads: ex.: "Broadcast message from root (pts/2) (Sat Jul 15 10:11:58 2006):"

    With –n the banner reads : "Remote broadcast message (Sat Jul 15 10:12:51 2006):"


    Watch

    Executes a program periodically, showing output fullscreen

    To watch the contents of a directory change every 15 seconds:


    Who is logged in and What they are doing

    w


    Wireless configuration

    iwconfig

    Wireless Monitoring:


    Write (Send a Message to another user)

    Usage: write user [ttyname]: write root pts/2 message


    Your Identity, Your Group, etc.

    id


    Yum

    Exclude Package(s) from Updating: yum update –exclude=389-*

    List Installed Packages: yum list installed

    Install packages automatically without asking any confirmation: yum –y install updates

    Remove a Package: yum remove package_name

    Update a Package: yum update package_name

    Search for a Specific Package in the Repository: yum list package_name

    Search for a Package, w/o knowing Exact Name: yum search package

    List Information about a Package: yum info package

    Find which Package a Specific File belongs to: Ex. yum provides /etc/httpd/coonf/httpd.conf

    List Enabled Package Repositories in your System: yum repolist

    List all Enabled and Disabled Repositories in your System: yum repolist all

    Install a Package from a Specific Repository: Ex. yum –-enablerepo=epel install phpmyadmin

    Temporarily allow a repo and install php from it: Ex.yum –-enablerepo=remi install php

    Temporarily disable a repo: Ex. yum –-disablerepo=epel-testing

    Permanently enable a repo. Here’s a remi example::

    Clean the Yum Cache: yum clean all

    View Yum History – List all Yum Operations

    View Update History Information by ID: yum history info <ID>

    Undo / Rollback a Transaction ID: yum history undo <ID>


    VMWare: Extending a logical volume in a virtual machine running Red Hat or Cent OS

    LVM – Expanding the Disk Size in Vmware:


    1. Expand the drive size (Disk Capacity) in VMWare
    2. Boot the OS, ID the device name and confirm the new disk size: fdisk –l (/dev/sda by default will be expanded)
    3. Create a new Primary Partition: fdisk /dev/sda
      1. p (you’ll see all the existing partitions, not the new space yet)
      2. n (create a new primary ookingn)
      3. p (it will auto-select the partition for you, ex. 4)
      4. Enter (default 1st cylinder)
      5. Enter (default last cylinder)
      6. p (now you’ll see the new space as the new partition, ex. /dev/sda4)
      7. t to change system’s partition ID to LVM
      8. <new_partition_number>, ex. 4
      9. 8e to change the Hex Code of the partition to Linux LVM
      10. w to Wrtie change to the partition table
    4. Reatart the VM
    5. fdisk –l (check to see the new partition, ex. /dev/sda4 is LVM w/ Id 8e)
    6. Convert the new partition to a physical volume: pvcreate /dev/sda4
    7. Extend the volume with the new partition / Physical volume: vgextend VolGroup00 /dev/sda4
    8. Confirm: vgdisplay – look for VG Size
    9. Verify how many physical extents are available to the VG by ooking at the "Free PE / Size" line of the vgdisplay command
    10. Extend the Logical Volume in order to use the new underlying storage we expanded: lvextend –L+<#G> /dev/VolGroup00/LogVol00 – where #G is size found in Step 9. OR extend by the amount of free space on the volume: lvextend /dev/VolGroup00/LogVol00 /dev/sda4
    11. Expand the ext3 filesystem online, inside the Logical Volume: resize2fs /dev/VolGroup00/LogVol00
    12. Verify extra space: df –h /

    zip

    Test the integrity of a zip file (verify contents): unzip -t <filename>.zip


    More Unix Commands

    UNIX COMMANDS continued…


    Top