Difference between revisions of "John's Linux page"
(→Vim: Why, oh WHY, do those #?@! nutheads use vi? and link to vim.org) |
(→Subversion: Setting svn:ignore from the command linev) |
||
Line 996: | Line 996: | ||
svn ci -m 'Adding svn:externals for rdfind-php...' | svn ci -m 'Adding svn:externals for rdfind-php...' | ||
svn up | svn up | ||
+ | |||
+ | == Setting svn:ignore from the command line == | ||
+ | |||
+ | See [http://tedone.typepad.com/blog/2010/03/setting-svnignore-from-the-command-line.html here]. | ||
+ | |||
+ | $ svn propset svn:ignore [file|folder] [path] | ||
+ | |||
+ | Or use a file and apply recursively: | ||
+ | |||
+ | $ svn propset svn:ignore -RF ./svn-ignore-list.txt . |
Revision as of 01:54, 11 December 2014
Hi there, I'm John. I just wanted a page where I could document various Linux things that I bump into. This is that page. Thank you ProgClub. :)
Note: I have some other disorganised notes on UNIX, which include a few tips for MacOS. I also have some tips for OS X.
System
Determining which Debian/Ubuntu release your are running
$ lsb_release -r
Or for more information:
$ lsb_release
Determining which Unix you are running
$ uname
Or,
$ uname -a
Configuring system swappiness
Swappiness is a number between 0 and 100 that regulates how much the system uses the swap file. I like setting this value to 0 to keep my apps as responsive as possible. Create a file /etc/sysctl.d/local.conf and add this line:
vm.swappiness = 0
If you want to set the value for the current session only:
echo 0 > /proc/sys/vm/swappiness
Environment
Configuring vim as your editor
Sometimes all you need is:
$ export EDITOR=/usr/bin/vim
Which works for svn, for example. Add it to your ~/.profile file to have it set for all login sessions.
Other times you need to run
# update-alternatives --config editor
And then select vim from the list. This is what you do to configure your visudo editor.
Configuring your locale
$ sudo /usr/sbin/locale-gen en_AU.UTF-8 $ sudo /usr/sbin/update-locale LANG=en_AU.UTF-8
User and group management
Adding a user
To add a new user on a linux system:
# useradd username # passwd username
To have the home directory created from '/etc/skel' use the 'adduser' script instead:
# adduser username
Adding a user to a group
To add an existing user to an existing group:
# gpasswd -a username group
e.g. to add user 'jj5' to the 'sudo' group:
# gpasswd -a jj5 sudo
Alternatively you can use adduser, passing the username and group:
# adduser username group
e.g. to add user 'sclaughl' to the 'staff' group:
# adduser sclaughl staff
Disabling a user account
You can disable a user account with:
# passwd -l user
Note: that's a lower-case L, not a one.
Enabling a disabled user account
To can re-enable a locked user account with:
# passwd -u user
Finding which user you are logged in as
To determine which user you are running as enter the command:
$ whoami
Finding which groups you are a member of
To find which groups you are a member of:
$ groups
or
$ groups username
Where 'username' is the username of the user you are querying, e.g.:
$ groups jj5
Finding who else is logged in to the system
To see who else is logged in,
$ who
Running a command as a particular user
To run "svn update" as the user www-data:
$ sudo su -c "svn update" www-data
Memory management
Checking available memory
To report memory statistics in megabytes:
$ free -m
Disk management
Checking available disk space
$ df -h
File management
Listing only directories
$ ls -l | egrep '^d'
Listing only files
$ ls -l | egrep -v '^d'
Creating a symbolic link
$ ln -s /path/to/target link-name
Creating a hard-link
$ ln /path/to/target file-name
Changing the owner of a file
$ chown user:group <files>
E.g.
$ chown jj5:staff README $ chown root:root *
To apply recursively into sub-directories use -R,
$ chown -R root:root /etc/*
Changing file permissions
User | Group | Other |
---|---|---|
u | g | o |
Read | Write | Exectue |
---|---|---|
r | w | x |
4 | 2 | 1 |
0 | None |
---|---|
1 | Execute |
2 | Write |
3 | Write, Execute |
4 | Read |
5 | Read, Execute |
6 | Read, Write |
7 | Read, Write, Execute |
$ chmod <user numeric code><group numeric code><other numeric code> <files> $ chmod <object codes>+|-<permission codes> <files>
E.g.
$ chmod 600 my-private-file $ chmod go-rwx my-private-file $ chmod u+rw my-private-file $ chmod +x my-script
Updating config files
If you get given a new config file called new.conf and you want to integrate it with your old config file old.conf then:
$ cp old.conf updated.conf $ merge -A updated.conf new.conf old.conf
Then go through and edit updated.conf resolving all the merge errors, picking and choosing what to update and what to keep. When you're done copy updated.conf to old.conf so it becomes the new config file.
Listing open files
Use lsof to list open files. E.g.:
# lsof
See man lsof for options.
File searching
Finding a file with a particular name
$ find -iname "*some-part-of-the-file-name*"
Will start searching from the current directory, so maybe
$ cd /
first. For a case-sensitive search:
$ find -name "*eXaCT CaSE*"
Finding a file with particular content
To search in /etc/ for a file with particular content:
$ grep -R "search-string" /etc/*
Using the locate command to find files
$ locate part-of-filename
E.g.
$ locate texvc
Updating locate command's database
# updatedb
Job control
Stopping a running process
Press Ctrl+Z to stop a running process.
Listing current jobs and their status
$ jobs
Resuming a stopped job in the backgroud
To resume a stopped process in the background
$ bg %1
where '1' is the job number reported by bash when you pressed Ctrl+Z (or ran 'jobs').
Resuming a stopped job in the foreground
To resume a stopped process in the foreground
$ fg %1
where '1' is the job number reported by bash when you pressed Ctrl+Z (or ran 'jobs').
Killing a stopped job
To kill a job
$ kill %1
where '1' is the job number reported by bash when you pressed Ctrl+Z (or ran 'jobs').
Debian/Ubuntu package management
configuring debconf
# dpkg-reconfigure debconf
Set priority to low to get asked detailed questions.
Showing list of installed packages
# dpkg --get-selections
Searching for installed package
# dpkg --get-selections | grep package-name
or
# aptitude search package-name
Showing which files are installed as part of a package
# dpkg -L package-name
Installing a package
# apt-get install package-name
Uninstalling a package
# apt-get remove package-name
Showing system architecture
$ dpkg --print-architecture
Showing which package a file belongs to
$ which echo /bin/echo $ dpkg -S /bin/echo coreutils: /bin/echo $ dpkg -l | grep coreutils ii coreutils 6.10-6 The GNU core utilities
Showing package information
$ apt-cache showpkg coreutils
Or for even more information:
$ apt-cache show coreutils
Networking
Restart networking
For servers:
# service networking restart
For desktops:
# service network-manager restart
Pinging with particular packet size
$ ping -M do -s <packet size in bytes> <host>
E.g.
$ ping -M do -s 1400 charity.progclub.org
Setting MSS for a particular IP address on a particular interface
# ip route add <host> dev <interface> advmss <packet size>
E.g.
# ip route add 10.0.0.1 dev eth0 advmss 1400
Dropping configured MMS for a particular IP address
# ip route flush <host>
E.g.
# ip route flush 10.0.0.1
Listing open ports and socket information
# netstat -tulpn
Or use the 'ss' command:
# ss -s # ss -l # ss -pl # ss -o state established '( dport = :smtp or sport = :smtp )'
Listing open IPv4 connections
# lsof -Pnl +M -i4
You might need to install the lsof package:
# apt-get install lsof
Query for DNS MX record
$ nslookup > server 127.0.0.1 > set q=mx > mail.blackbrick.com
Links
IPTables
Applying firewall rules
For configuration info see this article.
$ sudo vim /etc/iptables.test.rules $ sudo /sbin/iptables -F $ sudo /sbin/iptables-restore < /etc/iptables.test.rules $ sudo iptables -L $ sudo -s # iptables-save > /etc/iptables.up.rules # exit
IPSec
Disabling IPSec
# setkey -FP
OpenSSL
Debugging IMAPS with OpenSSL
# openssl s_client -connect localhost:993 > a1 LOGIN username@host password > a2 LOGOUT
Links
Pluggable Authentication Modules (PAM)
Links
SSH
Configuring SSH key login
On the client machine generate a key-pair (if necessary, check for existing ~/.ssh/id_rsa.pub):
$ ssh-keygen -t rsa
Copy the public key from the client to the server:
$ scp ~/.ssh/id_rsa.pub user@example.org:
Configure the authorized keys on the server:
$ ssh user@example.org $ mkdir ~/.ssh $ chmod go-w .ssh $ cat ~/id_rsa.pub >> ~/.ssh/authorized_keys $ chmod 600 ~/.ssh/authorized_keys $ rm ~/id_rsa.pub
Tunneling over SSH
For example, connecting a remote MySQL server to the localhost:
$ ssh -L 3306:localhost:3306 jselliot@ssh.progsoc.org
If the machine you want to connect to is not the localhost of the machine you're ssh'ing to,
$ ssh -L 3306:muspell.progsoc.uts.edu.au:3306 ssh.progsoc.uts.edu.au
The -L stanza is localport:remotehost:remoteport where localport is a port on your machine, forwarded to remoteport on remotehost.
Tunneling over SSH with PuTTY
See Connecting to the MySQL database remotely (via an SSH Tunnel)
- run putty.exe
- Connection -> SSH -> Tunnels
- Port forwarding: source port to 3306
- destination: 127.0.0.1:3306
- check Local
- click Add
Enabling verbose SSH logging
To see what's going on with your ssh connections,
$ ssh -v user@host
Or
$ ssh -vv user@host
Links
Standard IO
cat EOF
$ cat > output <<EOF > text > EOF
$ cat output text
Script
Creating a session log with script
$ script -t 2> timing
The session log is in the file 'typescript' and the timing data is in 'timing'.
Replaying a scripted session
$ scriptreplay timing
Uses the default file 'typescript' and the 'timing' file as specified.
Screen
Creating a new screen or reconnecting to a detached screen
$ screen -R
Detaching a screen
$ screen -D
Reconnecting to screen
$ screen -D $ screen -R
I have a script in ~/bin/reconnect like so,
#!/bin/bash screen -D screen -R
This will detach your last screen, and reconnect it on the current terminal.
Vim
First, why Vim?
Read Why, oh WHY, do those #?@! nutheads use vi?
Visual modes
Use 'v' for visual mode, 'V' for visual line mode and Ctrl+V for visual block mode.
Configuring spaces instead of tabs
I use two spaces instead of tabs. To configure, edit your .vimrc file:
$ vim ~/.vimrc
and include the following lines:
set tabstop=2 set shiftwidth=2 set expandtab
Configuring syntax highlighting
See here.
Use:
:syntax on
to turn on syntax highlighting.
Use:
:syntax off
to turn off syntax highlighting.
To always use syntax highlighting:
$ vim ~/.vimrc
and add:
syntax on
To get a list of supported colour schemes open vim and type:
:colorscheme[space][Ctrl+D]
To always use a particular colorscheme edit ~/.vimrc and add (for example):
colorscheme desert
Inserting a TAB character when expandtab is on
The problem here is that you have configured vim to insert spaces, but for a particular file (e.g. a Makefile) you need to insert a character.
Press Ctrl+V TAB to insert a literal tab character.
Or you can disable tab expansion altogether with:
:set expandtab!
Changing 2 space indent to 4 space indent (e.g. for python files)
:%s/^\s*/&&/g
For more information see here.
Recording and replaying a macro
To record a macro press 'q' and then a number between 1 and 9. E.g. press "q1". The macro is now recording. When you've finished issuing your commands press 'q' again to finish recording. To replay a macro press '@' followed by the number of the macro. That is, if you pressed "q1" to record the macro, press "@1" to replay the macro. To replay the last macro again press "@@".
Deleting to end of line
d$
Deleting to beginning of line
d^
Finding text
To search forward for "text":
/text
To search backward for "text":
?text
To repeat the last search in a forward direction press 'n', or to search again backwards press 'N'.
Finding and replacing text
To replace the first instance of "search" on the current line with "destroy":
:s/search/destroy/
To replace all instances of "search" on the current line with "destroy":
:s/search/destroy/g
To replace all instances of "search" on lines 13 to 37 with "destroy":
:13,37 s/search/destroy/g
To replace all instances of "search" in the entire file with "destroy":
:%s/search/destroy/g
Changing DOS/Windows line-endings to Unix line-endings
To set the line-ending to Unix line endings run the command:
:setlocal ff=unix
More information on managing file formats available here.
Disabling auto-indent etc. to paste from clipboard
To disable smart indenting when you're going to paste in text:
:set paste
To turn it off again:
:set nopaste
There's more info in this article: Toggle auto-indenting for code paste
Links
- Vim: the editor
- Learn Vim Progressively
- Vim cheat sheet for programmers
- How to insert Tab character when expandtab option is ON in VIM
- Vim tips: the basics of search and replace
- File format
- Graphical vi-vim Cheat Sheet and Tutorial
- Vim Commands Cheat Sheet
Write
Talking to other users on the system
write is a unix command for talking to other users on the system. To use write:
1. SSH to <username>@<hostname> and login with your username and password.
2. Issue the following command to find out who is logged onto the system:
$ who
3. Issue the following command to talk to a specific user:
$ write <username>
4. Enter the message you'd like to send the user, followed by Ctrl+C to send. Press Ctrl+D to cancel.
Date
Reporting the time on the server
$ date
Reporting UTC time
$ date --utc
Getting the date in yyyy-MM-dd-hhmmss format
$ date="`date +%F-%H%M%S`"
Getting the year in four digits
$ year="`date +%Y`"
Getting the month in two digits
$ month="`date +%m`"
Getting the day of the month in two digits
$ day="`date +%d`"
MySQL
Run mysql without authentication/authorisation
# service mysql stop # mysqld_safe --skip-grant-tables &
Then you can connect without a password, e.g.:
# mysql -u root mysql
To stop the unauthenticated service:
# mysqladmin shutdown
Then restart a normal service:
# service mysql start
Logging all database queries
# vim /etc/mysql/my.cnf
In the [mysqld] section add:
log=/tmp/mysql.log
Then:
# service mysql restart
Watch the log with:
# tail -f /tmp/mysql.log
Dumping a MySQL database
You can dump the database into a file using:
$ mysqldump -h hostname -u user --password=password databasename > filename
Loading a MySQL database from a dump file
You can create a database using:
$ echo create database databasename | mysql -h hostname -u user -p
You can restore a database using:
$ mysql -h hostname -u user --password=password databasename < filename
Creating a MySQL user
# mysql -h localhost -u root --password=<password> mysql> create user 'username'@'localhost' identified by '<password>';
Granting all MySQL user permissions
# mysql -h localhost -u root --password=<password> mysql> grant all privileges on dbname.* to user@host;
Apache
Maintaining .htaccess passwords
To add or modify the password for a user:
$ htpasswd /etc/apache2/passwd username
Configuring PHP session timeout in .htaccess
For a session timeout of 9 hours:
php_value session.cookie_lifetime 32400 php_value session.gc_maxlifetime 32400
Disabling PHP magic quotes in .htaccess
php_flag magic_quotes_gpc Off
Requiring HTTP Auth in .htaccess
AuthType Basic AuthName "Speak Friend And Enter" AuthUserFile /home/jj5/.htpasswd Require valid-user
Restarting Apache
The hard way
$ sudo /etc/init.d/apache2 restart
The graceful way (avoids dropping active connections)
$ sudo apache2ctl graceful
Allowing directory browsing
To show directory index pages, in the apache config file:
<Directory /var/www/data> Options Indexes </Directory>
C
Locating memset function
The memset function is in <string.h> as described in this article Using memset(), memcpy(), and memmove() in C
Links
PHP
Including a file relative to the including file
require_once( dirname( __FILE__ ) . '/relative/path/to.php' );
Enabling error reporting
error_reporting( E_ALL | E_STRICT ); ini_set( 'display_errors', 'On' );
Setting an error handler
set_error_handler( "error_handler", E_ALL | E_STRICT );
function error_handler( $error_code, $error_message, $error_file, $error_line, $error_context ) { // ... }
BASH scripting
For a primer on bash scripting see TFM: Erotic Fantasy: /bin/sh Programming.
Telling a script to run in bash
The first line of the file should be:
#!/bin/bash
Checking if a command-line argument was passed in
if [ -n "$1" ]; then echo "Missing parameter 1."; exit 1; fi
Checking if a command-line argument was not passed in
if [ "$1" = "" ]; then echo "Missing parameter 1."; exit 1; fi
Or:
if [ -z "$1" ]; then echo "Missing parameter 1."; exit 1; fi
Checking command exit status
cd /my/path if [ "$?" -ne "0" ]; then echo "Cannot change dir."; exit 1; fi
Checking if a directory doesn't exist
if [ ! -d "/my/dir" ]; then mkdir /my/dir fi
Deleting old backups
To keep only the latest five backups:
find . -maxdepth 1 -type f -printf '%T@ %p\0' | sort -r -z -n | awk 'BEGIN { RS="\0"; ORS="\0"; FS="" } NR > 5 { sub("^[0-9]*(.[0-9]*)? ", ""); print }' | xargs -0 rm -f
This script stolen from stackoverflow.
Requires GNU find for -printf, GNU sort for -z, GNU awk for "\0" and GNU xargs for -0, but handles files with embedded newlines or spaces.
Changing into the script's directory
cd `dirname $0`
Getting the absolute path of a relative path
readlink -f ./some/path
Creating a temp directory
dir=`mktemp -d` && cd $dir
Reading secret input from stdin
You can read a secret, such as a password, like this:
echo -n "Enter passphrase: " stty -echo read passphrase; stty echo echo ""
After running the above the secret will be in the $passphrase environment variable.
Awk
Listing IP addresses in an Apache web log
awk '/GET \/path\/for\/url/ { print $1 }' /var/log/apache2/access.log | sort | uniq
Subversion
Setting svn:externals from the command-line
To set an svn:externals from the command-line:
svn propset svn:externals 'rdfind-php https://www.progclub.org/svn/pcrepo/rdfind.php/branches/0.1' . svn ci -m 'Adding svn:externals for rdfind-php...' svn up
Setting svn:ignore from the command line
See here.
$ svn propset svn:ignore [file|folder] [path]
Or use a file and apply recursively:
$ svn propset svn:ignore -RF ./svn-ignore-list.txt .