- Back to Home »
- Linux Basic , Linux PC »
- Basic Network Commands
Tuesday, June 16, 2015
link1
link2
The network commands chapter explains various tools which can be useful when networking with other computers both within the network and accross the internet, obtaining more information about other computers. This chapter also includes information on tools for network configuration, file transfer and working with remote machines.
netstat
Displays contents of /proc/net files. It works with the Linux Network Subsystem, it will tell you what the status of ports are ie. open, closed, waiting, masquerade connections. It will also display various other things. It has many different options.
tcpdump
This is a sniffer, a program that captures packets off a network interface and interprets them for you. It understands all basic internet protocols, and can be used to save entire packets for later inspection.
ping
The ping command (named after the sound of an active sonar system) sends echo requests to the host you specify on the command line, and lists the responses received their round trip time.
You simply use ping as:
ping ip_or_host_nameNote to stop ping (otherwise it goes forever) use CTRL-C (break).
Please note: Using ping/smbmount/ssh or other UNIX system programs with a computer name rather than IP address will only work if you have the computer listed in your /etc/hosts file. Here is an example:
192.168.1.100 new
This line says that their is a computer called “new” with IP address 192.168.1.100. Now that it exists in the /etc/hosts file I don't have to type the IP address anymore, just the name “new”.
hostname
Tells the user the host name of the computer they are logged into. Note: may be called host.
traceroute
traceroute will show the route of a packet. It attempts to list the series of hosts through which your packets travel on their way to a given destination. Also have a look at xtraceroute (one of several graphicalequivalents of this program).
Command syntax:
traceroute machine_name_or_ip
tracepath
tracepath performs a very simlar function to traceroute the main difference is that tracepath doesn't take complicated options.
Command syntax:
tracepath machine_name_or_ip
findsmb
findsmb is used to list info about machines that respond to SMB name queries (for example windows based machines sharing their hard disk's).
Command syntax:
findsmb
This would find all machines possible, you may need to specify a particular subnet to query those machines only...
nmap
“ network exploration tool and security scanner”. nmap is a very advanced network tool used to query machines (local or remote) as to whether they are up and what ports are open on these machines.
A simple usage example:
nmap machine_nameThis would query your own machine as to what ports it keeps open. nmap is a very powerful tool, documentation is available on the nmap site as well as the information in the manual page.
Network Configuration
ifconfig
This command is used to configure network interfaces, or to display their current configuration. In addition to activating and deactivating interfaces with the “up” and “down” settings, this command is necessary for setting an interface's address information if you don't have the ifcfg script.
Use ifconfig as either:
ifconfig
This will simply list all information on all network devices currently up.
ifconfig eth0 downThis will take eth0 (assuming the device exists) down, it won't be able to receive or send anything until you put the device back “up” again.
Clearly there are a lot more options for this tool, you will need to read the manual/info page to learn more about them.
ifup
Use ifup device-name to bring an interface up by following a script (which will contain your default networking settings). Simply type ifup and you will get help on using the script.
For example typing:
ifup eth0Will bring eth0 up if it is currently down.
ifdown
Use ifdown device-name to bring an interface down using a script (which will contain your default network settings). Simply type ifdown and you will get help on using the script.
For example typing:
ifdown eth0Will bring eth0 down if it is currently up.
ifcfg
Use ifcfg to configure a particular interface. Simply type ifcfg to get help on using this script.
For example, to change eth0 from 192.168.0.1 to 192.168.0.2 you could do:
ifcfg eth0 del 192.168.0.1The first command takes eth0 down and removes that stored IP address and the second one brings it back up with the new address.
ifcfg eth0 add 192.168.0.2
route
The route command is the tool used to display or modify the routing table. To add a gateway as the default you would type:
route add default gw some_compute
Internet Specific Commands
Note that should DNS not be configured correctly on your machine, you need to edit “/etc/resolv.conf” to make things work...
host
Performs a simple lookup of an internet address (using the Domain Name System, DNS). Simply type:
host ip_addressdig
or
host domain_name
The "domain information groper" tool. More advanced then host... If you give a hostname as an argument to output information about that host, including it's IP address, hostname and various other information.
For example, to look up information about “www.amazon.com” type:
dig www.amazon.comTo find the host name for a given IP address (ie a reverse lookup), use dig with the `-x' option.
dig -x 100.42.30.95This will look up the address (which may or may not exist) and returns the address of the host, for example if that was the address of “http://slashdot.org” then it would return “http://slashdot.org”.
dig takes a huge number of options (at the point of being too many), refer to the manual page for more information.
whois
(now BW whois) is used to look up the contact information from the “whois” databases, the servers are only likely to hold major sites. Note that contact information is likely to be hidden or restricted as it is often abused by crackers and others looking for a way to cause malicious damage to organisation's.
wget
(GNU Web get) used to download files from the World Wide Web.
To archive a single web-site, use the -m or --mirror (mirror) option.
Use the -nc (no clobber) option to stop wget from overwriting a file if you already have it.
Use the -c or --continue option to continue a file that was unfinished by wget or another program.
Simple usage example:
wget url_for_fileThis would simply get a file from a site.
wget can also retrieve multiple files using standard wildcards, the same as the type used in bash, like *, [ ], ?. Simply use wget as per normal but use single quotation marks (' ') on the URL to prevent bash from expanding the wildcards. There are complications if you are retrieving from a http site (see below...).
Advanced usage example, (used from wget manual page):
wget --spider --force-html -i bookmarks.htmlThis will parse the file bookmarks.html and check that all the links exist.
Advanced usage: this is how you can download multiple files using http (using a wildcard...).
Notes: http doesn't support downloading using standard wildcards, ftp does so you may use wildcards with ftp and it will work fine. A work-around for this http limitation is shown below:
wget -r -l1 --no-parent -A.gif http://www.website.com[1]This will download (recursively), to a depth of one, in other words in the current directory and not below that. This command will ignore references to the parent directory, and downloads anything that ends in “.gif”. If you wanted to download say, anything that ends with “.pdf” as well than add a -A.pdf before the website address. Simply change the website address and the type of file being downloaded to download something else. Note that doing -A.gif is the same as doing -A “*.gif” (double quotes only, single quotes will not work).
wget has many more options refer to the examples section of the manual page, this tool is very well documented.
Alternative website downloaders: You may like to try alternatives like httrack. A full GUI website downloader written in python and available for GNU/Linux
curl
curl is another remote downloader. This remote downloader is designed to work without user interaction and supports a variety of protocols, can upload/download and has a large number of tricks/work-arounds for various things. It can access dictionary servers (dict), ldap servers, ftp, http, gopher, see the manual page for full details.
To access the full manual (which is huge) for this command type:
curl -M
For general usage you can use it like wget. You can also login using a user name by using the -u option and typing your username and password like this:
curl -u username:password http://www.placetodownload/fileTo upload using ftp you the -T option:
curl -T file_name ftp://ftp.uploadsite.comTo continue a file use the -C option:
curl -C - -o file http://www.site.com
Remote Administration Related
ssh
Secure shell, remotely login on a machine running the sshd daemon. Once you are logged in you have a secure shell and are able to execute various commands on that computer such as copy files, reboot the computer, just like it was your own GNU/Linux PC.
Or you can use ssh with a full hostname to connect to a remote machine (as in across the internet).
Examples:
ssh hostnameConnect to a remote system with your current username, you will obviously need the password of the user on the other machine.
ssh username@hostnameConnect to a remote system with your a different username, you will obviously need the password of the user on the other machine.
scp
Secure copy, part of the ssh package. Allows you to copy files from one computer to another computer, use -r to copy recursively (copy entire directories and subdirectories).
scp's syntax is always
scp machineToBeCopiedFrom machineToBeCopiedTo
Where either machine can be a local directory (on the current filesystem /) or a remote machine. Remote machines are usually machinesFullName:/directory (if you omit the directory part it will just assume the home directory of the username you are logging in with).
The example below copies all files from the current directory (not including any directories), the command will login to “new” using the username of the person currently logged in on the local computer, the files will be copied to the root directory of the remote computer called “new” (which is probably on the LAN):
scp * new:/
You could also copy files from another computer to another computer. Let's say you are on a computer called “p100”. And you want to copy files (and directories) from “hp166” (in the /tmp directory and anything below that) to “new” and put the files in new's temporary directory. You could do:
scp -r hp166:/tmp new:/tmp
Assuming you were logged in as “fred” you would need passwords for user “fred” on the computers hp166 and new. Add an user_name@ before the computer name to login under a different user name.
For example to perform the above command with user “root” on hp166 and “anon” on new you would type:
scp -r root@hp166:/tmp anon@new:/tmp
To copy from a remote machine to a local computer you simply do things in reverse:
scp remoteMachine:/mystuff/* .
This will copy files on the remote machine in the directory “mystuff” to your local computer.
Remote Machines: Please note that when working with a remote machine you need to have a : (colon) after the machine name even if you want the files in their home directory. Otherwise the command will fail.
sftp
Secure ftp, another part of the ssh package. This command is similar to ftp but uses an encrypted tunnel to connect to an ftp server and is therefore more secure than just plain ftp.
The command usage is very similar to ftp (the command-line tool), sftp (once running) uses commands such as help (for help), put (send files to the server), get (download files from the server) and various others, refer to the manual page and internal documentation for further details.
FTP
To connect to an FTP server, simply run the ftp(1) command and specify the host:
ftp <hostname> [port]If the host is running an FTP server, it will ask for a username and password. You can log in as yourself or as “anonymous”. Anonymous FTP sites are very popular for software archives. For example, to get Slackware Linux via FTP, you must use anonymous FTP.
Once connected, you will be at the ftp> prompt. There are special commands for FTP, but they are similar to other standard commands. The following shows some of the basic commands and what they do:
Command | Purpose |
---|---|
ls | List files |
cd <dirname> | Change directory |
bin | Set binary transfer mode |
ascii | Set ASCII transfer mode |
get <filename> | Download a file |
put <filename> | Upload a file |
hash | Toggle hash mark stats indicator |
tick | Toggle byte counter indicator |
prom | Toggle interactive mode for downloads |
mget <mask> | Download a file or group of files; wildcards are allowed |
mput <mask> | Upload a file or group of files; wildcards are allowed |
quit | Log off the FTP server |